mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-01-09 01:18:00 +00:00
New utility function base64_valid().
For when you want to tell the difference between a base64-encoded string and some other kind of string that might replace it.
This commit is contained in:
parent
2a44b6354f
commit
d06ae2f5c3
1
misc.h
1
misc.h
@ -114,6 +114,7 @@ strbuf *base64_decode_sb(ptrlen data);
|
||||
void base64_encode_bs(BinarySink *bs, ptrlen data, int cpl);
|
||||
void base64_encode_fp(FILE *fp, ptrlen data, int cpl);
|
||||
strbuf *base64_encode_sb(ptrlen data, int cpl);
|
||||
bool base64_valid(ptrlen data);
|
||||
|
||||
struct bufchain_granule;
|
||||
struct bufchain_tag {
|
||||
|
@ -5,6 +5,7 @@ add_sources_from_current_dir(utils
|
||||
base64_decode.c
|
||||
base64_encode_atom.c
|
||||
base64_encode.c
|
||||
base64_valid.c
|
||||
bufchain.c
|
||||
buildinfo.c
|
||||
burnstr.c
|
||||
|
54
utils/base64_valid.c
Normal file
54
utils/base64_valid.c
Normal file
@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Determine whether a string looks like valid base64-encoded data.
|
||||
*/
|
||||
|
||||
#include "misc.h"
|
||||
|
||||
static inline bool valid_char_main(char c)
|
||||
{
|
||||
return ((c >= 'A' && c <= 'Z') ||
|
||||
(c >= 'a' && c <= 'z') ||
|
||||
(c >= '0' && c <= '9') ||
|
||||
c == '+' || c == '/');
|
||||
}
|
||||
|
||||
bool base64_valid(ptrlen data)
|
||||
{
|
||||
size_t blocklen = 0, nequals = 0;
|
||||
|
||||
for (size_t i = 0; i < data.len; i++) {
|
||||
char c = ((const char *)data.ptr)[i];
|
||||
|
||||
if (c == '\n' || c == '\r')
|
||||
continue;
|
||||
|
||||
if (valid_char_main(c)) {
|
||||
if (nequals) /* can't go back to data after = */
|
||||
return false;
|
||||
blocklen++;
|
||||
if (blocklen == 4)
|
||||
blocklen = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c == '=') {
|
||||
if (blocklen == 0 && nequals) /* started a fresh block */
|
||||
return false;
|
||||
|
||||
nequals++;
|
||||
blocklen++;
|
||||
if (blocklen == 4) {
|
||||
if (nequals > 2)
|
||||
return false; /* nonsensical final block */
|
||||
blocklen = 0;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
return false; /* bad character */
|
||||
}
|
||||
|
||||
if (blocklen == 0 || blocklen == 2 || blocklen == 3)
|
||||
return true; /* permit eliding the trailing = */
|
||||
return false;
|
||||
}
|
Loading…
Reference in New Issue
Block a user