summaryrefslogtreecommitdiffstats
path: root/third_party/modp_b64/modp_b64.cc
diff options
context:
space:
mode:
Diffstat (limited to 'third_party/modp_b64/modp_b64.cc')
-rw-r--r--third_party/modp_b64/modp_b64.cc44
1 files changed, 23 insertions, 21 deletions
diff --git a/third_party/modp_b64/modp_b64.cc b/third_party/modp_b64/modp_b64.cc
index ecf06c4..e5f6cf1 100644
--- a/third_party/modp_b64/modp_b64.cc
+++ b/third_party/modp_b64/modp_b64.cc
@@ -77,20 +77,22 @@
#define CHARPAD '\0'
#endif
-int modp_b64_encode(char* dest, const char* str, int len)
+size_t modp_b64_encode(char* dest, const char* str, size_t len)
{
- int i;
+ size_t i = 0;
uint8_t* p = (uint8_t*) dest;
/* unsigned here is important! */
uint8_t t1, t2, t3;
- for (i = 0; i < len - 2; i += 3) {
- t1 = str[i]; t2 = str[i+1]; t3 = str[i+2];
- *p++ = e0[t1];
- *p++ = e1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)];
- *p++ = e1[((t2 & 0x0F) << 2) | ((t3 >> 6) & 0x03)];
- *p++ = e2[t3];
+ if (len > 2) {
+ for (; i < len - 2; i += 3) {
+ t1 = str[i]; t2 = str[i+1]; t3 = str[i+2];
+ *p++ = e0[t1];
+ *p++ = e1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)];
+ *p++ = e1[((t2 & 0x0F) << 2) | ((t3 >> 6) & 0x03)];
+ *p++ = e2[t3];
+ }
}
switch (len - i) {
@@ -124,7 +126,7 @@ int modp_b64_decode(char* dest, const char* src, int len)
/* if padding is used, then the message must be at least
4 chars and be a multiple of 4.
there can be at most 2 pad chars at the end */
- if (len < 4 || (len % 4 != 0)) return -1;
+ if (len < 4 || (len % 4 != 0)) return MODP_B64_ERROR;
if (src[len-1] == CHARPAD) {
len--;
if (src[len -1] == CHARPAD) {
@@ -133,9 +135,9 @@ int modp_b64_decode(char* dest, const char* src, int len)
}
#endif /* DOPAD */
- int i;
+ size_t i;
int leftover = len % 4;
- int chunks = (leftover == 0) ? len / 4 - 1 : len /4;
+ size_t chunks = (leftover == 0) ? len / 4 - 1 : len /4;
uint8_t* p = (uint8_t*) dest;
uint32_t x = 0;
@@ -146,7 +148,7 @@ int modp_b64_decode(char* dest, const char* src, int len)
x = d0[y >> 24 & 0xff] | d1[y >> 16 & 0xff] |
d2[y >> 8 & 0xff] | d3[y & 0xff];
- if (x >= BADCHAR) return -1;
+ if (x >= BADCHAR) return MODP_B64_ERROR;
*destInt = x << 8;
p += 3;
destInt = (uint32_t*)p;
@@ -157,7 +159,7 @@ int modp_b64_decode(char* dest, const char* src, int len)
case 0:
x = d0[y >> 24 & 0xff] | d1[y >> 16 & 0xff] |
d2[y >> 8 & 0xff] | d3[y & 0xff];
- if (x >= BADCHAR) return -1;
+ if (x >= BADCHAR) return MODP_B64_ERROR;
*p++ = ((uint8_t*)&x)[1];
*p++ = ((uint8_t*)&x)[2];
*p = ((uint8_t*)&x)[3];
@@ -178,13 +180,13 @@ int modp_b64_decode(char* dest, const char* src, int len)
break;
}
- if (x >= BADCHAR) return -1;
+ if (x >= BADCHAR) return MODP_B64_ERROR;
return 3*chunks + (6*leftover)/8;
}
#else /* LITTLE ENDIAN -- INTEL AND FRIENDS */
-int modp_b64_decode(char* dest, const char* src, int len)
+size_t modp_b64_decode(char* dest, const char* src, size_t len)
{
if (len == 0) return 0;
@@ -193,7 +195,7 @@ int modp_b64_decode(char* dest, const char* src, int len)
* if padding is used, then the message must be at least
* 4 chars and be a multiple of 4
*/
- if (len < 4 || (len % 4 != 0)) return -1; /* error */
+ if (len < 4 || (len % 4 != 0)) return MODP_B64_ERROR; /* error */
/* there can be at most 2 pad chars at the end */
if (src[len-1] == CHARPAD) {
len--;
@@ -203,9 +205,9 @@ int modp_b64_decode(char* dest, const char* src, int len)
}
#endif
- int i;
+ size_t i;
int leftover = len % 4;
- int chunks = (leftover == 0) ? len / 4 - 1 : len /4;
+ size_t chunks = (leftover == 0) ? len / 4 - 1 : len /4;
uint8_t* p = (uint8_t*)dest;
uint32_t x = 0;
@@ -218,7 +220,7 @@ int modp_b64_decode(char* dest, const char* src, int len)
d2[(y >> 16) & 0xff] |
d3[(y >> 24) & 0xff];
- if (x >= BADCHAR) return -1;
+ if (x >= BADCHAR) return MODP_B64_ERROR;
*destInt = x ;
p += 3;
destInt = (uint32_t*)p;
@@ -232,7 +234,7 @@ int modp_b64_decode(char* dest, const char* src, int len)
d2[(y >> 16) & 0xff] |
d3[(y >> 24) & 0xff];
- if (x >= BADCHAR) return -1;
+ if (x >= BADCHAR) return MODP_B64_ERROR;
*p++ = ((uint8_t*)(&x))[0];
*p++ = ((uint8_t*)(&x))[1];
*p = ((uint8_t*)(&x))[2];
@@ -255,7 +257,7 @@ int modp_b64_decode(char* dest, const char* src, int len)
break;
}
- if (x >= BADCHAR) return -1;
+ if (x >= BADCHAR) return MODP_B64_ERROR;
return 3*chunks + (6*leftover)/8;
}