Update sdb from git and sync base64 fixes

This commit is contained in:
pancake 2014-03-26 23:17:40 +01:00
parent 27d7725950
commit 13fb09ee5f
3 changed files with 28 additions and 24 deletions

View File

@ -337,7 +337,7 @@ R_API int r_name_check(const char *name);
R_API int r_name_filter(char *name, int len);
R_API void r_base64_encode(ut8 *bout, const ut8 *bin, int len);
R_API int r_base64_decode(ut8 *bout, const ut8 *bin, int len);
R_API int r_base64_decode(ut8 *bout, const char *bin, int len);
/* strings */
static inline void r_str_rmch (char *s, char ch) {

View File

@ -13,17 +13,18 @@
static const char cb64[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static const char cd64[]="|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\\]^_`abcdefghijklmnopq";
static void b64_encode(const ut8 in[3], ut8 out[4], int len) {
static void b64_encode(const ut8 in[3], char out[4], int len) {
if (len<1) return;
out[0] = cb64[ in[0] >> 2 ];
out[1] = cb64[ ((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4) ];
out[2] = (len > 1 ? cb64[ ((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6) ] : '=');
out[1] = cb64[ ((in[0] & 0x03) << 4) | ((len>1)?((in[1] & 0xf0) >> 4):0) ];
out[2] = (len > 1 ? cb64[ ((in[1] & 0x0f) << 2) | (len > 2 ? ((in[2] & 0xc0) >> 6) : 0) ] : '=');
out[3] = (len > 2 ? cb64[ in[2] & 0x3f ] : '=');
}
static int b64_decode(const ut8 in[4], ut8 out[3]) {
static int b64_decode(const char in[4], ut8 out[3]) {
ut8 len = 3, i, v[4] = {0};
for (i=0; i<4; i++) {
if (in[i]<43||in[i]>122)
if (in[i]<43 || in[i]>122)
return -1;
v[i] = cd64[in[i]-43];
if (v[i]=='$') {
@ -37,20 +38,25 @@ static int b64_decode(const ut8 in[4], ut8 out[3]) {
return len;
}
R_API int r_base64_decode(ut8 *bout, const ut8 *bin, int len) {
R_API int r_base64_decode(ut8 *bout, const char *bin, int len) {
int in, out, ret;
if (len<1)
len = strlen (bin);
for (in=out=0; in<len-1;in+=4) {
ret = b64_decode (bin+in, bout+out);
if (ret <1)
break;
out += 3;
out += ret;
}
bout[out-1] = 0;
return (in != out);
bout[out] = 0;
return (in != out)? out: 0;
}
R_API void r_base64_encode(ut8 *bout, const ut8 *bin, int len) {
int in, out;
if (len<1)
len = strlen ((const char*)bin)+1;
for (in=out=0; in<len; in+=3,out+=4)
b64_encode (bin+in, bout+out, len-in>3?3:len-in);
b64_encode (bin+in, (char*)bout+out,
(len-in)>3?3:len-in);
}

View File

@ -14,7 +14,7 @@ static void b64_encode(const ut8 in[3], char out[4], int len) {
if (len<1) return;
out[0] = cb64[ in[0] >> 2 ];
out[1] = cb64[ ((in[0] & 0x03) << 4) | ((len>1)?((in[1] & 0xf0) >> 4):0) ];
out[2] = (len > 1 ? cb64[ ((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6) ] : '=');
out[2] = (len > 1 ? cb64[ ((in[1] & 0x0f) << 2) | (len > 2 ? ((in[2] & 0xc0) >> 6) : 0) ] : '=');
out[3] = (len > 2 ? cb64[ in[2] & 0x3f ] : '=');
}
@ -38,31 +38,28 @@ static int b64_decode(const char in[4], ut8 out[3]) {
SDB_API void sdb_encode_raw(char *bout, const ut8 *bin, int len) {
int in, out;
for (in=out=0; in<len; in+=3,out+=4)
b64_encode (bin+in, bout+out, len-in>3?3:len-in);
b64_encode (bin+in, bout+out,
(len-in)>3?3:(len-in));
bout[out] = 0;
}
SDB_API int sdb_decode_raw(ut8 *bout, const char *bin, int len) {
int in, out, ret;
for (in=out=0; in<len-1;in+=4) {
for (in=out=0; in<len; in+=4) {
if ((ret = b64_decode (bin+in, bout+out))<1)
break;
out += 3;
out += ret;
}
if (out>0)
bout[out-1] = 0;
return (in != out);
return (in != out)? out: 0;
}
SDB_API char *sdb_encode(const ut8 *bin, int len) {
char *out;
if (!bin || len<1)
len = strlen ((const char *)bin)+1;
if (len==0)
return strdup ("");
if (!bin) return NULL;
if (len<1) len = strlen ((const char *)bin);
if (len==0) return strdup ("");
out = malloc (8+(len*2));
if (!out)
return NULL;
if (!out) return NULL;
memset (out, 0, (len*2)+8);
sdb_encode_raw (out, bin, len);
return out;
@ -82,6 +79,7 @@ SDB_API ut8 *sdb_decode (const char *in, int *len) {
free (out);
return NULL;
}
out[olen] = 0;
if (len)
*len = olen;
return out;