sxt: b64 fix with size and table;
This commit is contained in:
parent
5c7a43753c
commit
7049a4082d
22
sxt/base64.c
22
sxt/base64.c
@ -36,16 +36,29 @@ static inline void encodeblock(unsigned char *in, unsigned char *out, int len)
|
||||
out[3] = (unsigned char) (len > 2 ? b64[ (int)(in[2] & 0x3f) ] : '=');
|
||||
}
|
||||
|
||||
static inline char __block(char b)
|
||||
{
|
||||
const char *j = strchr(b64, b);
|
||||
|
||||
if(j) return j - b64;
|
||||
else return 0;
|
||||
}
|
||||
|
||||
static inline void decodeblock(unsigned char *in, unsigned char *out)
|
||||
{
|
||||
out[0] = (unsigned char) (in[0] << 2 | in[1] >> 4);
|
||||
out[1] = (unsigned char) (in[1] << 4 | in[2] >> 2);
|
||||
out[2] = (unsigned char) (((in[2] << 6) & 0xc0) | in[3]);
|
||||
char a = __block(in[0]);
|
||||
char b = __block(in[1]);
|
||||
char c = __block(in[2]);
|
||||
char d = __block(in[3]);
|
||||
|
||||
out[0] = (unsigned char) (a << 2 | b >> 4);
|
||||
out[1] = (unsigned char) (b << 4 | c >> 2);
|
||||
out[2] = (unsigned char) (((c << 6) & 0xc0) | d);
|
||||
}
|
||||
|
||||
size_t sxt_rawlen2b64len(size_t raw_length)
|
||||
{
|
||||
return 4 * ((raw_length + 2) / 3);
|
||||
return (size_t)(((4 * raw_length / 3) + 3) & ~3);
|
||||
}
|
||||
|
||||
size_t sxt_b64encode_in(const char *data, char *bdata, size_t data_len)
|
||||
@ -73,6 +86,7 @@ size_t sxt_b64encode_in(const char *data, char *bdata, size_t data_len)
|
||||
} else ib[i] = (unsigned char)0;
|
||||
}
|
||||
encodeblock(ib, (unsigned char *)bdata + n, len);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user