Nuke the base64 code. It was wrong
This commit is contained in:
@@ -9,10 +9,6 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
size_t
|
||||
uc_base64_enc(const unsigned char *data,size_t len,unsigned char *result);
|
||||
size_t
|
||||
uc_base64_dec(const unsigned char *data, size_t len, unsigned char *result);
|
||||
|
||||
char*
|
||||
uc_hex_encode(const uint8_t *in, size_t len, char *out);
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
#include <stdint.h>
|
||||
#include "ucore/string.h"
|
||||
|
||||
static const uint8_t basecode[128] =
|
||||
{
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 62, 0xff, 0xff, 0xff, 63,
|
||||
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
|
||||
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
|
||||
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0xff, 0xff, 0xff, 0xff, 0xff
|
||||
};
|
||||
|
||||
#define deccode(c) ((c > 127) ? 0xff : basecode[(c)])
|
||||
|
||||
size_t
|
||||
uc_base64_dec(const unsigned char *data, size_t len, unsigned char *result)
|
||||
{
|
||||
size_t i = 0, j = 0, pad;
|
||||
uint8_t c[4];
|
||||
|
||||
while ((i + 3) < len) {
|
||||
|
||||
pad = 0;
|
||||
c[0] = deccode(data[i ]); pad += (c[0] == 0xff);
|
||||
c[1] = deccode(data[i+1]); pad += (c[1] == 0xff);
|
||||
c[2] = deccode(data[i+2]); pad += (c[2] == 0xff);
|
||||
c[3] = deccode(data[i+3]); pad += (c[3] == 0xff);
|
||||
switch (pad) {
|
||||
case 1:
|
||||
result[j++] = (c[0] << 2) | ((c[1] & 0x30) >> 4);
|
||||
result[j++] = ((c[1] & 0x0f) << 4) | ((c[2] & 0x3c) >> 2);
|
||||
result[j] = (c[2] & 0x03) << 6;
|
||||
break;
|
||||
case 2:
|
||||
result[j++] = (c[0] << 2) | ((c[1] & 0x30) >> 4);
|
||||
result[j] = (c[1] & 0x0f) << 4;
|
||||
break;
|
||||
default:
|
||||
result[j++] = (c[0] << 2) | ((c[1] & 0x30) >> 4);
|
||||
result[j++] = ((c[1] & 0x0f) << 4) | ((c[2] & 0x3c) >> 2);
|
||||
result[j++] = ((c[2] & 0x03) << 6) | (c[3] & 0x3f);
|
||||
}
|
||||
|
||||
i += 4;
|
||||
}
|
||||
|
||||
return j;
|
||||
}
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include "ucore/string.h"
|
||||
|
||||
static const char basecode[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
#define PAD '='
|
||||
|
||||
size_t
|
||||
uc_base64_enc(const unsigned char *data,size_t len,unsigned char *result)
|
||||
{
|
||||
|
||||
size_t i = 0, j = 0;
|
||||
size_t pad;
|
||||
|
||||
|
||||
while (i < len) {
|
||||
pad = 3 - (len - i);
|
||||
switch(pad) {
|
||||
case 2:
|
||||
result[j] = basecode[data[i]>>2];
|
||||
result[j+1] = basecode[(data[i] & 0x03) << 4];
|
||||
result[j+2] = PAD;
|
||||
result[j+3] = PAD;
|
||||
break;
|
||||
case 3:
|
||||
result[j ] = basecode[data[i]>>2];
|
||||
result[j+1] = basecode[((data[i] & 0x03) << 4) | ((data[i+1] & 0xf0) >> 4)];
|
||||
result[j+2] = basecode[(data[i+1] & 0x0f) << 2];
|
||||
result[j+3] = PAD;
|
||||
break;
|
||||
default:
|
||||
result[j ] = basecode[data[i]>>2];
|
||||
result[j+1] = basecode[((data[i] & 0x03) << 4) | ((data[i+1] & 0xf0) >> 4)];
|
||||
result[j+2] = basecode[((data[i+1] & 0x0f) << 2) | ((data[i+2] & 0xc0) >> 6)];
|
||||
result[j+3] = basecode[data[i+2] & 0x3f];
|
||||
break;
|
||||
}
|
||||
j += 4;
|
||||
i += 3;
|
||||
}
|
||||
|
||||
return j;
|
||||
}
|
||||
+1
-1
@@ -39,7 +39,7 @@ static suite_func suites[] = {
|
||||
timers_suite,
|
||||
mbuf_suite,
|
||||
human_bytesz_suite,
|
||||
dbuf_suite
|
||||
dbuf_suite,
|
||||
};
|
||||
|
||||
int
|
||||
|
||||
Reference in New Issue
Block a user