aboutsummaryrefslogtreecommitdiffstats
path: root/src/string.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2018-05-12 12:47:18 +0200
committerDavid Robillard <d@drobilla.net>2018-11-25 22:12:46 +0100
commit95e4ca4d49f8aa8e736d4a6743bf961c93cf7f69 (patch)
tree7863e81d6d920dab94f7ebbfad9a9445187b5c5d /src/string.c
parent3b4397f6ce5fc4aecd1074f78db308cc14f07bb5 (diff)
downloadserd-95e4ca4d49f8aa8e736d4a6743bf961c93cf7f69.tar.gz
serd-95e4ca4d49f8aa8e736d4a6743bf961c93cf7f69.tar.bz2
serd-95e4ca4d49f8aa8e736d4a6743bf961c93cf7f69.zip
Separate base64 implementation
Diffstat (limited to 'src/string.c')
-rw-r--r--src/string.c47
1 files changed, 0 insertions, 47 deletions
diff --git a/src/string.c b/src/string.c
index b00be2fa..eaffdb0d 100644
--- a/src/string.c
+++ b/src/string.c
@@ -119,50 +119,3 @@ serd_strtod(const char* str, size_t* end)
return result * sign;
}
-
-/**
- Base64 decoding table.
- This is indexed by encoded characters and returns the numeric value used
- for decoding, shifted up by 47 to be in the range of printable ASCII.
- A '$' is a placeholder for characters not in the base64 alphabet.
-*/
-static const char b64_unmap[] =
- "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$m$$$ncdefghijkl$$$$$$"
- "$/0123456789:;<=>?@ABCDEFGH$$$$$$IJKLMNOPQRSTUVWXYZ[\\]^_`ab$$$$"
- "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"
- "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$";
-
-static inline uint8_t unmap(const uint8_t in) { return b64_unmap[in] - 47; }
-
-/**
- Decode 4 base64 characters to 3 raw bytes.
-*/
-static inline size_t
-decode_chunk(const uint8_t in[4], uint8_t out[3])
-{
- out[0] = (uint8_t)(((unmap(in[0]) << 2)) | unmap(in[1]) >> 4);
- out[1] = (uint8_t)(((unmap(in[1]) << 4) & 0xF0) | unmap(in[2]) >> 2);
- out[2] = (uint8_t)(((unmap(in[2]) << 6) & 0xC0) | unmap(in[3]));
- return 1 + (in[2] != '=') + ((in[2] != '=') && (in[3] != '='));
-}
-
-void*
-serd_base64_decode(const char* str, size_t len, size_t* size)
-{
- const uint8_t* ustr = (const uint8_t*)str;
-
- void* buf = malloc((len * 3) / 4 + 2);
- *size = 0;
- for (size_t i = 0, j = 0; i < len; j += 3) {
- uint8_t in[] = "====";
- size_t n_in = 0;
- for (; i < len && n_in < 4; ++n_in) {
- for (; i < len && !is_base64(ustr[i]); ++i) {} // Skip junk
- in[n_in] = ustr[i++];
- }
- if (n_in > 1) {
- *size += decode_chunk(in, (uint8_t*)buf + j);
- }
- }
- return buf;
-}