Skip to content

Commit

Permalink
Make modp_b64 and base::base64 compile on Win64
Browse files Browse the repository at this point in the history
BUG=166496

Review URL: https://chromiumcodereview.appspot.com/11688006

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@174700 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
jschuh@chromium.org committed Dec 27, 2012
1 parent c071601 commit c525312
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 31 deletions.
10 changes: 5 additions & 5 deletions base/base64.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions third_party/modp_b64/README.chromium
Original file line number Diff line number Diff line change
Expand Up @@ -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.
44 changes: 23 additions & 21 deletions third_party/modp_b64/modp_b64.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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];
Expand All @@ -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;

Expand All @@ -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--;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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];
Expand All @@ -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;
}
Expand Down
14 changes: 9 additions & 5 deletions third_party/modp_b64/modp_b64.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#ifndef MODP_B64
#define MODP_B64

#include <stddef.h>

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit c525312

Please sign in to comment.