Browse Source

Add a helper function for encoding into mime base64

master
Con Kolivas 11 years ago
parent
commit
e06c72e483
  1. 53
      src/libckpool.c
  2. 1
      src/libckpool.h

53
src/libckpool.c

@ -370,6 +370,59 @@ void b58tobin(uchar *b58bin, const uchar *b58)
} }
} }
static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
/* Return a malloced string of *src encoded into mime base 64 */
uchar *http_base64(const uchar *src)
{
uchar *str, *dst;
size_t l, hlen;
int t, r;
l = strlen((const char *)src);
hlen = ((l + 2) / 3) * 4 + 1;
align_len(&hlen);
str = malloc(hlen);
if (unlikely(!str))
return str;
dst = str;
r = 0;
while (l >= 3) {
t = (src[0] << 16) | (src[1] << 8) | src[2];
dst[0] = base64[(t >> 18) & 0x3f];
dst[1] = base64[(t >> 12) & 0x3f];
dst[2] = base64[(t >> 6) & 0x3f];
dst[3] = base64[(t >> 0) & 0x3f];
src += 3; l -= 3;
dst += 4; r += 4;
}
switch (l) {
case 2:
t = (src[0] << 16) | (src[1] << 8);
dst[0] = base64[(t >> 18) & 0x3f];
dst[1] = base64[(t >> 12) & 0x3f];
dst[2] = base64[(t >> 6) & 0x3f];
dst[3] = '=';
dst += 4;
r += 4;
break;
case 1:
t = src[0] << 16;
dst[0] = base64[(t >> 18) & 0x3f];
dst[1] = base64[(t >> 12) & 0x3f];
dst[2] = dst[3] = '=';
dst += 4;
r += 4;
break;
case 0:
break;
}
*dst = 0;
return (str);
}
void address_to_pubkeytxn(uchar *pkh, const uchar *addr) void address_to_pubkeytxn(uchar *pkh, const uchar *addr)
{ {
uchar b58bin[25]; uchar b58bin[25];

1
src/libckpool.h

@ -116,6 +116,7 @@ void align_len(size_t *len);
void __bin2hex(uchar *s, const uchar *p, size_t len); void __bin2hex(uchar *s, const uchar *p, size_t len);
void *bin2hex(const uchar *p, size_t len); void *bin2hex(const uchar *p, size_t len);
bool hex2bin(uchar *p, const uchar *hexstr, size_t len); bool hex2bin(uchar *p, const uchar *hexstr, size_t len);
uchar *http_base64(const uchar *src);
void b58tobin(uchar *b58bin, const uchar *b58); void b58tobin(uchar *b58bin, const uchar *b58);
void address_to_pubkeytxn(uchar *pkh, const uchar *addr); void address_to_pubkeytxn(uchar *pkh, const uchar *addr);
int ser_number(uchar *s, int32_t val); int ser_number(uchar *s, int32_t val);

Loading…
Cancel
Save