From 13fb09ee5f4f0e084169124c61b4f941bd02cd7f Mon Sep 17 00:00:00 2001 From: pancake Date: Wed, 26 Mar 2014 23:17:40 +0100 Subject: [PATCH] Update sdb from git and sync base64 fixes --- libr/include/r_util.h | 2 +- libr/util/base64.c | 26 ++++++++++++++++---------- shlr/sdb/src/base64.c | 24 +++++++++++------------- 3 files changed, 28 insertions(+), 24 deletions(-) diff --git a/libr/include/r_util.h b/libr/include/r_util.h index 1e41e182ce..bc4dd51ce4 100644 --- a/libr/include/r_util.h +++ b/libr/include/r_util.h @@ -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) { diff --git a/libr/util/base64.c b/libr/util/base64.c index bda445a81c..2f53a1c048 100644 --- a/libr/util/base64.c +++ b/libr/util/base64.c @@ -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; in3?3:len-in); + b64_encode (bin+in, (char*)bout+out, + (len-in)>3?3:len-in); } diff --git a/shlr/sdb/src/base64.c b/shlr/sdb/src/base64.c index 59d00739c3..0394dd568a 100644 --- a/shlr/sdb/src/base64.c +++ b/shlr/sdb/src/base64.c @@ -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; in3?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; in0) - 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;