summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--base/base64.cc10
-rw-r--r--third_party/modp_b64/README.chromium3
-rw-r--r--third_party/modp_b64/modp_b64.cc44
-rw-r--r--third_party/modp_b64/modp_b64.h14
4 files changed, 40 insertions, 31 deletions
diff --git a/base/base64.cc b/base/base64.cc
index 1907978..9514b0a 100644
--- a/base/base64.cc
+++ b/base/base64.cc
@@ -16,8 +16,8 @@ bool Base64Encode(const StringPiece& input, std::string* output) {
int input_size = static_cast<int>(input.size());
// modp_b64_encode_len() returns at least 1, so temp[0] is safe to use.
- int output_size = modp_b64_encode(&(temp[0]), input.data(), input_size);
- if (output_size < 0)
+ size_t output_size = modp_b64_encode(&(temp[0]), input.data(), input_size);
+ if (output_size == MODP_B64_ERROR)
return false;
temp.resize(output_size); // strips off null byte
@@ -30,9 +30,9 @@ bool Base64Decode(const StringPiece& input, std::string* output) {
temp.resize(modp_b64_decode_len(input.size()));
// does not null terminate result since result is binary data!
- int input_size = static_cast<int>(input.size());
- int output_size = modp_b64_decode(&(temp[0]), input.data(), input_size);
- if (output_size < 0)
+ size_t input_size = input.size();
+ size_t output_size = modp_b64_decode(&(temp[0]), input.data(), input_size);
+ if (output_size == MODP_B64_ERROR)
return false;
temp.resize(output_size);
diff --git a/third_party/modp_b64/README.chromium b/third_party/modp_b64/README.chromium
index e59e69e..26eed1e 100644
--- a/third_party/modp_b64/README.chromium
+++ b/third_party/modp_b64/README.chromium
@@ -17,3 +17,6 @@ modified to not include <stdint.h> when COMPILER_MSVC is defined (since
that header file does not exist under VC8), but instead in that case to
include "base/basictypes.h" and provide the required typedefs for
uint8_t and uint32_t using uint8 and uint32.
+
+The modp_b64.cc and modp_b64.h files were modified to make them safe on
+64-bit systems. \ No newline at end of file
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;
}
diff --git a/third_party/modp_b64/modp_b64.h b/third_party/modp_b64/modp_b64.h
index f90528f..3270e5f 100644
--- a/third_party/modp_b64/modp_b64.h
+++ b/third_party/modp_b64/modp_b64.h
@@ -24,6 +24,8 @@
#ifndef MODP_B64
#define MODP_B64
+#include <stddef.h>
+
#ifdef __cplusplus
extern "C" {
#endif
@@ -53,7 +55,7 @@ extern "C" {
* \endcode
*
*/
-int modp_b64_encode(char* dest, const char* str, int len);
+size_t modp_b64_encode(char* dest, const char* str, size_t len);
/**
* Decode a base64 encoded string
@@ -76,7 +78,7 @@ int modp_b64_encode(char* dest, const char* str, int len);
* if (len == -1) { error }
* \endcode
*/
-int modp_b64_decode(char* dest, const char* src, int len);
+size_t modp_b64_decode(char* dest, const char* src, size_t len);
/**
* Given a source string of length len, this returns the amount of
@@ -126,6 +128,8 @@ int modp_b64_decode(char* dest, const char* src, int len);
*/
#define modp_b64_encode_strlen(A) ((A + 2)/ 3 * 4)
+#define MODP_B64_ERROR ((size_t)-1)
+
#ifdef __cplusplus
}
@@ -134,7 +138,7 @@ int modp_b64_decode(char* dest, const char* src, int len);
inline std::string& modp_b64_encode(std::string& s)
{
std::string x(modp_b64_encode_len(s.size()), '\0');
- int d = modp_b64_encode(const_cast<char*>(x.data()), s.data(), s.size());
+ size_t d = modp_b64_encode(const_cast<char*>(x.data()), s.data(), (int)s.size());
x.erase(d, std::string::npos);
s.swap(x);
return s;
@@ -152,8 +156,8 @@ inline std::string& modp_b64_encode(std::string& s)
inline std::string& modp_b64_decode(std::string& s)
{
std::string x(modp_b64_decode_len(s.size()), '\0');
- int d = modp_b64_decode(const_cast<char*>(x.data()), s.data(), s.size());
- if (d < 0) {
+ size_t d = modp_b64_decode(const_cast<char*>(x.data()), s.data(), (int)s.size());
+ if (d == MODP_B64_ERROR) {
x.clear();
} else {
x.erase(d, std::string::npos);