summaryrefslogtreecommitdiff
path: root/deps/base64
diff options
context:
space:
mode:
authoryangwei <[email protected]>2024-11-25 19:22:19 +0800
committeryangwei <[email protected]>2024-11-25 19:22:19 +0800
commit1199e9d83f1b2c2d47d8fb7de911674319379651 (patch)
treec6c5f49405af8a0c33ab239fac3d488d61feb1fa /deps/base64
parentcce1155ae366077cca72047887f476bae7b0856a (diff)
✨ feat(integrate utable): deps/utable
Diffstat (limited to 'deps/base64')
-rw-r--r--deps/base64/CMakeLists.txt1
-rw-r--r--deps/base64/LICENSE21
-rw-r--r--deps/base64/README.md84
-rw-r--r--deps/base64/b64.h84
-rw-r--r--deps/base64/base64.c164
-rw-r--r--deps/base64/base64.h28
-rw-r--r--deps/base64/buffer.c33
-rw-r--r--deps/base64/decode.c117
-rw-r--r--deps/base64/encode.c93
9 files changed, 433 insertions, 192 deletions
diff --git a/deps/base64/CMakeLists.txt b/deps/base64/CMakeLists.txt
new file mode 100644
index 0000000..a1972e9
--- /dev/null
+++ b/deps/base64/CMakeLists.txt
@@ -0,0 +1 @@
+add_library(base64 decode.c encode.c buffer.c) \ No newline at end of file
diff --git a/deps/base64/LICENSE b/deps/base64/LICENSE
new file mode 100644
index 0000000..78b34d5
--- /dev/null
+++ b/deps/base64/LICENSE
@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) 2014 Little Star Media, Inc.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE. \ No newline at end of file
diff --git a/deps/base64/README.md b/deps/base64/README.md
new file mode 100644
index 0000000..0555c0d
--- /dev/null
+++ b/deps/base64/README.md
@@ -0,0 +1,84 @@
+b64.c
+=====
+
+Base64 encode/decode
+
+## install
+
+```sh
+$ clib install jwerle/b64.c
+```
+
+## usage
+
+```c
+#include <b64/b64.h>
+```
+
+or
+
+```c
+#include <b64.h>
+```
+
+## example
+
+```c
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include "b64.h"
+
+int
+main (void) {
+ unsigned char *str = "brian the monkey and bradley the kinkajou are friends";
+ char *enc = b64_encode(str, strlen(str));
+
+ printf("%s\n", enc); // YnJpYW4gdGhlIG1vbmtleSBhbmQgYnJhZGxleSB0aGUga2lua2Fqb3UgYXJlIGZyaWVuZHM=
+
+ char *dec = b64_decode(enc, strlen(enc));
+
+ printf("%s\n", dec); // brian the monkey and bradley the kinkajou are friends
+ free(enc);
+ free(dec);
+ return 0;
+}
+```
+
+## api
+
+Base64 index table
+
+```c
+
+static const char b64_table[] = {
+ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
+ 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
+ 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
+ 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
+ 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
+ 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
+ 'w', 'x', 'y', 'z', '0', '1', '2', '3',
+ '4', '5', '6', '7', '8', '9', '+', '/'
+};
+```
+
+Encode `unsigned char *` source with `size_t` size.
+Returns a `char *` base64 encoded string
+
+```c
+char *
+b64_encode (const unsigned char *, size_t);
+```
+
+Decode `char *` source with `size_t` size.
+Returns a `unsigned char *` base64 decoded string
+
+```c
+unsigned char *
+b64_decode (const char *, size_t);
+```
+
+## license
+
+MIT
diff --git a/deps/base64/b64.h b/deps/base64/b64.h
new file mode 100644
index 0000000..e39d746
--- /dev/null
+++ b/deps/base64/b64.h
@@ -0,0 +1,84 @@
+
+/**
+ * `b64.h' - b64
+ *
+ * copyright (c) 2014 joseph werle
+ */
+
+#ifndef B64_H
+#define B64_H 1
+
+typedef struct b64_buffer {
+ char * ptr;
+ int bufc;
+} b64_buffer_t;
+
+/**
+ * Memory allocation functions to use. You can define b64_malloc and
+ * b64_realloc to custom functions if you want.
+ */
+
+#ifndef b64_malloc
+# define b64_malloc(ptr) malloc(ptr)
+#endif
+#ifndef b64_realloc
+# define b64_realloc(ptr, size) realloc(ptr, size)
+#endif
+
+ // How much memory to allocate per buffer
+#define B64_BUFFER_SIZE (1024 * 64) // 64K
+
+ // Start buffered memory
+int b64_buf_malloc(b64_buffer_t * buffer);
+
+// Update memory size. Returns the same pointer if we
+// have enough space in the buffer. Otherwise, we add
+// additional buffers.
+int b64_buf_realloc(b64_buffer_t * buffer, size_t size);
+
+/**
+ * Base64 index table.
+ */
+
+static const char b64_table[] = {
+ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
+ 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
+ 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
+ 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
+ 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
+ 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
+ 'w', 'x', 'y', 'z', '0', '1', '2', '3',
+ '4', '5', '6', '7', '8', '9', '+', '/'
+};
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * Encode `unsigned char *' source with `size_t' size.
+ * Returns a `char *' base64 encoded string.
+ */
+
+char *
+b64_encode (const unsigned char *, size_t);
+
+/**
+ * Decode `char *' source with `size_t' size.
+ * Returns a `unsigned char *' base64 decoded string.
+ */
+unsigned char *
+b64_decode (const char *, size_t);
+
+/**
+ * Decode `char *' source with `size_t' size.
+ * Returns a `unsigned char *' base64 decoded string + size of decoded string.
+ */
+unsigned char *
+b64_decode_ex (const char *, size_t, size_t *);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/deps/base64/base64.c b/deps/base64/base64.c
deleted file mode 100644
index eeded25..0000000
--- a/deps/base64/base64.c
+++ /dev/null
@@ -1,164 +0,0 @@
-/* This is a public domain base64 implementation written by WEI Zhicheng. */
-
-#include "base64.h"
-
-#define BASE64_PAD '='
-#define BASE64DE_FIRST '+'
-#define BASE64DE_LAST 'z'
-
-/* BASE 64 encode table */
-static const char base64en[] = {
- 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
- 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
- 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
- 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
- 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
- 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
- 'w', 'x', 'y', 'z', '0', '1', '2', '3',
- '4', '5', '6', '7', '8', '9', '+', '/',
-};
-
-/* ASCII order for BASE 64 decode, 255 in unused character */
-static const unsigned char base64de[] = {
- /* nul, soh, stx, etx, eot, enq, ack, bel, */
- 255, 255, 255, 255, 255, 255, 255, 255,
-
- /* bs, ht, nl, vt, np, cr, so, si, */
- 255, 255, 255, 255, 255, 255, 255, 255,
-
- /* dle, dc1, dc2, dc3, dc4, nak, syn, etb, */
- 255, 255, 255, 255, 255, 255, 255, 255,
-
- /* can, em, sub, esc, fs, gs, rs, us, */
- 255, 255, 255, 255, 255, 255, 255, 255,
-
- /* sp, '!', '"', '#', '$', '%', '&', ''', */
- 255, 255, 255, 255, 255, 255, 255, 255,
-
- /* '(', ')', '*', '+', ',', '-', '.', '/', */
- 255, 255, 255, 62, 255, 255, 255, 63,
-
- /* '0', '1', '2', '3', '4', '5', '6', '7', */
- 52, 53, 54, 55, 56, 57, 58, 59,
-
- /* '8', '9', ':', ';', '<', '=', '>', '?', */
- 60, 61, 255, 255, 255, 255, 255, 255,
-
- /* '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', */
- 255, 0, 1, 2, 3, 4, 5, 6,
-
- /* 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', */
- 7, 8, 9, 10, 11, 12, 13, 14,
-
- /* 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', */
- 15, 16, 17, 18, 19, 20, 21, 22,
-
- /* 'X', 'Y', 'Z', '[', '\', ']', '^', '_', */
- 23, 24, 25, 255, 255, 255, 255, 255,
-
- /* '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', */
- 255, 26, 27, 28, 29, 30, 31, 32,
-
- /* 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', */
- 33, 34, 35, 36, 37, 38, 39, 40,
-
- /* 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', */
- 41, 42, 43, 44, 45, 46, 47, 48,
-
- /* 'x', 'y', 'z', '{', '|', '}', '~', del, */
- 49, 50, 51, 255, 255, 255, 255, 255
-};
-
-unsigned int
-base64_encode(const unsigned char *in, unsigned int inlen, char *out)
-{
- int s;
- unsigned int i;
- unsigned int j;
- unsigned char c;
- unsigned char l;
-
- s = 0;
- l = 0;
- for (i = j = 0; i < inlen; i++) {
- c = in[i];
-
- switch (s) {
- case 0:
- s = 1;
- out[j++] = base64en[(c >> 2) & 0x3F];
- break;
- case 1:
- s = 2;
- out[j++] = base64en[((l & 0x3) << 4) | ((c >> 4) & 0xF)];
- break;
- case 2:
- s = 0;
- out[j++] = base64en[((l & 0xF) << 2) | ((c >> 6) & 0x3)];
- out[j++] = base64en[c & 0x3F];
- break;
- }
- l = c;
- }
-
- switch (s) {
- case 1:
- out[j++] = base64en[(l & 0x3) << 4];
- out[j++] = BASE64_PAD;
- out[j++] = BASE64_PAD;
- break;
- case 2:
- out[j++] = base64en[(l & 0xF) << 2];
- out[j++] = BASE64_PAD;
- break;
- }
-
- out[j] = 0;
-
- return j;
-}
-
-unsigned int
-base64_decode(const char *in, unsigned int inlen, unsigned char *out)
-{
- unsigned int i;
- unsigned int j;
- unsigned char c;
-
- if (inlen & 0x3) {
- return 0;
- }
-
- for (i = j = 0; i < inlen; i++) {
- if (in[i] == BASE64_PAD) {
- break;
- }
- if (in[i] < BASE64DE_FIRST || in[i] > BASE64DE_LAST) {
- return 0;
- }
-
- c = base64de[(unsigned char)in[i]];
- if (c == 255) {
- return 0;
- }
-
- switch (i & 0x3) {
- case 0:
- out[j] = (c << 2) & 0xFF;
- break;
- case 1:
- out[j++] |= (c >> 4) & 0x3;
- out[j] = (c & 0xF) << 4;
- break;
- case 2:
- out[j++] |= (c >> 2) & 0xF;
- out[j] = (c & 0x3) << 6;
- break;
- case 3:
- out[j++] |= c;
- break;
- }
- }
-
- return j;
-}
diff --git a/deps/base64/base64.h b/deps/base64/base64.h
deleted file mode 100644
index 9bea95a..0000000
--- a/deps/base64/base64.h
+++ /dev/null
@@ -1,28 +0,0 @@
-#ifndef BASE64_H
-#define BASE64_H
-
-#define BASE64_ENCODE_OUT_SIZE(s) ((unsigned int)((((s) + 2) / 3) * 4 + 1))
-#define BASE64_DECODE_OUT_SIZE(s) ((unsigned int)(((s) / 4) * 3))
-
-#ifdef __cplusplus
-extern "C"
-{
-#endif
-/*
- * out is null-terminated encode string.
- * return values is out length, exclusive terminating `\0'
- */
-unsigned int
-base64_encode(const unsigned char *in, unsigned int inlen, char *out);
-
-/*
- * return values is out length
- */
-unsigned int
-base64_decode(const char *in, unsigned int inlen, unsigned char *out);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* BASE64_H */
diff --git a/deps/base64/buffer.c b/deps/base64/buffer.c
new file mode 100644
index 0000000..aad598c
--- /dev/null
+++ b/deps/base64/buffer.c
@@ -0,0 +1,33 @@
+#include <stdlib.h>
+#include <ctype.h>
+#include "b64.h"
+
+#ifdef b64_USE_CUSTOM_MALLOC
+extern void* b64_malloc(size_t);
+#endif
+
+#ifdef b64_USE_CUSTOM_REALLOC
+extern void* b64_realloc(void*, size_t);
+#endif
+
+int b64_buf_malloc(b64_buffer_t * buf)
+{
+ buf->ptr = b64_malloc(B64_BUFFER_SIZE);
+ if(!buf->ptr) return -1;
+
+ buf->bufc = 1;
+
+ return 0;
+}
+
+int b64_buf_realloc(b64_buffer_t* buf, size_t size)
+{
+ if ((int)size > buf->bufc * B64_BUFFER_SIZE)
+ {
+ while ((int)size > buf->bufc * B64_BUFFER_SIZE) buf->bufc++;
+ buf->ptr = b64_realloc(buf->ptr, B64_BUFFER_SIZE * buf->bufc);
+ if (!buf->ptr) return -1;
+ }
+
+ return 0;
+}
diff --git a/deps/base64/decode.c b/deps/base64/decode.c
new file mode 100644
index 0000000..38093bb
--- /dev/null
+++ b/deps/base64/decode.c
@@ -0,0 +1,117 @@
+
+/**
+ * `decode.c' - b64
+ *
+ * copyright (c) 2014 joseph werle
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <ctype.h>
+#include "b64.h"
+
+#ifdef b64_USE_CUSTOM_MALLOC
+extern void* b64_malloc(size_t);
+#endif
+
+#ifdef b64_USE_CUSTOM_REALLOC
+extern void* b64_realloc(void*, size_t);
+#endif
+
+unsigned char *
+b64_decode (const char *src, size_t len) {
+ return b64_decode_ex(src, len, NULL);
+}
+
+unsigned char *
+b64_decode_ex (const char *src, size_t len, size_t *decsize) {
+ int i = 0;
+ int j = 0;
+ int l = 0;
+ size_t size = 0;
+ b64_buffer_t decbuf;
+ unsigned char buf[3];
+ unsigned char tmp[4];
+
+ // alloc
+ if (b64_buf_malloc(&decbuf) == -1) { return NULL; }
+
+ // parse until end of source
+ while (len--) {
+ // break if char is `=' or not base64 char
+ if ('=' == src[j]) { break; }
+ if (!(isalnum(src[j]) || '+' == src[j] || '/' == src[j])) { break; }
+
+ // read up to 4 bytes at a time into `tmp'
+ tmp[i++] = src[j++];
+
+ // if 4 bytes read then decode into `buf'
+ if (4 == i) {
+ // translate values in `tmp' from table
+ for (i = 0; i < 4; ++i) {
+ // find translation char in `b64_table'
+ for (l = 0; l < 64; ++l) {
+ if (tmp[i] == b64_table[l]) {
+ tmp[i] = l;
+ break;
+ }
+ }
+ }
+
+ // decode
+ buf[0] = (tmp[0] << 2) + ((tmp[1] & 0x30) >> 4);
+ buf[1] = ((tmp[1] & 0xf) << 4) + ((tmp[2] & 0x3c) >> 2);
+ buf[2] = ((tmp[2] & 0x3) << 6) + tmp[3];
+
+ // write decoded buffer to `decbuf.ptr'
+ if (b64_buf_realloc(&decbuf, size + 3) == -1) return NULL;
+ for (i = 0; i < 3; ++i) {
+ ((unsigned char*)decbuf.ptr)[size++] = buf[i];
+ }
+
+ // reset
+ i = 0;
+ }
+ }
+
+ // remainder
+ if (i > 0) {
+ // fill `tmp' with `\0' at most 4 times
+ for (j = i; j < 4; ++j) {
+ tmp[j] = '\0';
+ }
+
+ // translate remainder
+ for (j = 0; j < 4; ++j) {
+ // find translation char in `b64_table'
+ for (l = 0; l < 64; ++l) {
+ if (tmp[j] == b64_table[l]) {
+ tmp[j] = l;
+ break;
+ }
+ }
+ }
+
+ // decode remainder
+ buf[0] = (tmp[0] << 2) + ((tmp[1] & 0x30) >> 4);
+ buf[1] = ((tmp[1] & 0xf) << 4) + ((tmp[2] & 0x3c) >> 2);
+ buf[2] = ((tmp[2] & 0x3) << 6) + tmp[3];
+
+ // write remainer decoded buffer to `decbuf.ptr'
+ if (b64_buf_realloc(&decbuf, size + (i - 1)) == -1) return NULL;
+ for (j = 0; (j < i - 1); ++j) {
+ ((unsigned char*)decbuf.ptr)[size++] = buf[j];
+ }
+ }
+
+ // Make sure we have enough space to add '\0' character at end.
+ if (b64_buf_realloc(&decbuf, size + 1) == -1) return NULL;
+ ((unsigned char*)decbuf.ptr)[size] = '\0';
+
+ // Return back the size of decoded string if demanded.
+ if (decsize != NULL) {
+ *decsize = size;
+ }
+
+ return (unsigned char*) decbuf.ptr;
+}
diff --git a/deps/base64/encode.c b/deps/base64/encode.c
new file mode 100644
index 0000000..68e7924
--- /dev/null
+++ b/deps/base64/encode.c
@@ -0,0 +1,93 @@
+
+/**
+ * `encode.c' - b64
+ *
+ * copyright (c) 2014 joseph werle
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include "b64.h"
+
+#ifdef b64_USE_CUSTOM_MALLOC
+extern void* b64_malloc(size_t);
+#endif
+
+#ifdef b64_USE_CUSTOM_REALLOC
+extern void* b64_realloc(void*, size_t);
+#endif
+
+char *
+b64_encode (const unsigned char *src, size_t len) {
+ int i = 0;
+ int j = 0;
+ b64_buffer_t encbuf;
+ size_t size = 0;
+ unsigned char buf[4];
+ unsigned char tmp[3];
+
+ // alloc
+ if(b64_buf_malloc(&encbuf) == -1) { return NULL; }
+
+ // parse until end of source
+ while (len--) {
+ // read up to 3 bytes at a time into `tmp'
+ tmp[i++] = *(src++);
+
+ // if 3 bytes read then encode into `buf'
+ if (3 == i) {
+ buf[0] = (tmp[0] & 0xfc) >> 2;
+ buf[1] = ((tmp[0] & 0x03) << 4) + ((tmp[1] & 0xf0) >> 4);
+ buf[2] = ((tmp[1] & 0x0f) << 2) + ((tmp[2] & 0xc0) >> 6);
+ buf[3] = tmp[2] & 0x3f;
+
+ // allocate 4 new byts for `enc` and
+ // then translate each encoded buffer
+ // part by index from the base 64 index table
+ // into `encbuf.ptr' unsigned char array
+ if (b64_buf_realloc(&encbuf, size + 4) == -1) return NULL;
+
+ for (i = 0; i < 4; ++i) {
+ encbuf.ptr[size++] = b64_table[buf[i]];
+ }
+
+ // reset index
+ i = 0;
+ }
+ }
+
+ // remainder
+ if (i > 0) {
+ // fill `tmp' with `\0' at most 3 times
+ for (j = i; j < 3; ++j) {
+ tmp[j] = '\0';
+ }
+
+ // perform same codec as above
+ buf[0] = (tmp[0] & 0xfc) >> 2;
+ buf[1] = ((tmp[0] & 0x03) << 4) + ((tmp[1] & 0xf0) >> 4);
+ buf[2] = ((tmp[1] & 0x0f) << 2) + ((tmp[2] & 0xc0) >> 6);
+ buf[3] = tmp[2] & 0x3f;
+
+ // perform same write to `encbuf->ptr` with new allocation
+ for (j = 0; (j < i + 1); ++j) {
+ if (b64_buf_realloc(&encbuf, size + 1) == -1) return NULL;
+
+ encbuf.ptr[size++] = b64_table[buf[j]];
+ }
+
+ // while there is still a remainder
+ // append `=' to `encbuf.ptr'
+ while ((i++ < 3)) {
+ if (b64_buf_realloc(&encbuf, size + 1) == -1) return NULL;
+
+ encbuf.ptr[size++] = '=';
+ }
+ }
+
+ // Make sure we have enough space to add '\0' character at end.
+ if (b64_buf_realloc(&encbuf, size + 1) == -1) return NULL;
+ encbuf.ptr[size] = '\0';
+
+ return encbuf.ptr;
+}