Commit 42f00867 authored by Niels Möller's avatar Niels Möller

* src/digits.c (decode_base64): New function.

(simple_decode_base64): New function.

Rev: src/digits.c:1.5
Rev: src/digits.h:1.4
parent 4b18a096
......@@ -101,3 +101,39 @@ simple_decode_hex(const unsigned char *in)
return out;
}
struct lsh_string *
decode_base64(UINT32 length, const UINT8 *in)
{
struct base64_state state;
struct lsh_string *out = lsh_string_alloc(length * 3 / 4 + 1);
UINT32 i;
UINT32 j;
base64_init(&state, 0);
for (i=0, j=0; i<length; i++)
{
int digit = base64_decode(&state, in[i]);
switch (digit)
{
case BASE64_INVALID:
case BASE64_END:
lsh_string_free(out);
return NULL;
case BASE64_SPACE:
case BASE64_PARTIAL:
continue;
default:
assert(j < out->length);
out->data[j++] = digit;
}
}
out->length = j;
return out;
}
struct lsh_string *
simple_decode_base64(const unsigned char *in)
{
return decode_base64(strlen(in), in);
}
......@@ -56,7 +56,15 @@ base64_init(struct base64_state *state, UINT8 terminator);
int
base64_decode(struct base64_state *state, UINT8 c);
struct lsh_string *
decode_base64(UINT32 length, const UINT8 *in);
/* Only used by the testsuite */
struct lsh_string *simple_decode_hex(const unsigned char *in);
struct lsh_string *
simple_decode_hex(const unsigned char *in);
struct lsh_string *
simple_decode_base64(const unsigned char *in);
#endif /* LSH_DIGITS_H_INCLUDED */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment