sxt: b64 fix with size and table;

master
Alexander Vdolainen 9 years ago
parent 5c7a43753c
commit 7049a4082d

@ -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) ] : '='); 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) static inline void decodeblock(unsigned char *in, unsigned char *out)
{ {
out[0] = (unsigned char) (in[0] << 2 | in[1] >> 4); char a = __block(in[0]);
out[1] = (unsigned char) (in[1] << 4 | in[2] >> 2); char b = __block(in[1]);
out[2] = (unsigned char) (((in[2] << 6) & 0xc0) | in[3]); 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) 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) 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; } else ib[i] = (unsigned char)0;
} }
encodeblock(ib, (unsigned char *)bdata + n, len); encodeblock(ib, (unsigned char *)bdata + n, len);
break;
} }
} }

Loading…
Cancel
Save