├── LICENSE ├── Makefile ├── README.md ├── b64.h └── tests └── basic.c /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | CFLAGS = -I. -O3 -Werror -Wpedantic -std=c89 3 | 4 | test: tests/basic.c 5 | @$(CC) tests/basic.c $(CFLAGS) -o basic 6 | @./basic 7 | @rm ./basic 8 | 9 | default: test 10 | 11 | all: test 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # b64.h 2 | base64 single header encode/decode 3 | 4 | ## Works with C89 5 | 6 | ```c 7 | #include 8 | #include "b64.h" 9 | 10 | int main(int argc, char **argv) 11 | { 12 | // External buffer 13 | char b64enc[1024]; 14 | char b64dec[1024]; 15 | 16 | b64Encode(b64enc, argv[1], strlen(argv[1])); 17 | printf("%s\n", b64enc); 18 | 19 | b64Decode(b64dec, b64enc, strlen(b64enc)); 20 | printf("%s\n", b64dec); 21 | 22 | // Heap allocation done by passing NULL to dst, don't forget to free() 23 | char* b64encoded = b64Encode(NULL, argv[1], strlen(argv[1])); 24 | printf("%s\n", b64enc); 25 | 26 | char* b64decoded = b64Decode(NULL, b64enc, strlen(b64enc)); 27 | printf("%s\n", b64dec); 28 | } 29 | ``` 30 | 31 | ```bash 32 | $ ./a.out sus 33 | c3Vz 34 | sus 35 | ``` 36 | 37 | 38 | -------------------------------------------------------------------------------- /b64.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed under unlicense 3 | */ 4 | 5 | #ifndef _B64_H_ 6 | #define _B64_H_ 7 | 8 | /* 9 | * C++ implementation was ripped from tomykaira's gist 10 | * https://gist.github.com/tomykaira/f0fd86b6c73063283afe550bc5d77594 11 | * 12 | * written by skullchap 13 | * https://github.com/skullchap/b64 14 | */ 15 | 16 | #include "stdlib.h" 17 | #include "string.h" 18 | 19 | #define B64_LEN(inlen) ((((inlen) + 2) / 3) * 4) 20 | #define B64_REV(inlen) (((inlen) / 4) * 3) 21 | 22 | /* b64Encode - encodes an array of bytes to base64 23 | * Inputs: 24 | * - dst : the destination array (if it's NULL, the function will allocate the 25 | * buffer) 26 | * - src : the source array 27 | * - srclen : the length of the source array 28 | * Safety: 29 | * if dst is not NULL, then it must point to an array that has the size of at 30 | * least ((srclen + 2) / 3 * 4) 31 | */ 32 | char *b64Encode(char *dst, char *src, size_t srclen) { 33 | static const char b64e[] = { 34 | 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 35 | 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 36 | 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 37 | 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 38 | '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'}; 39 | 40 | size_t i; 41 | char *p; 42 | size_t inlen = srclen; 43 | size_t outlen = B64_LEN(inlen); 44 | 45 | if (!dst) { 46 | dst = malloc(outlen + 1); 47 | /* malloc failed so return early */ 48 | if (!dst) { 49 | return NULL; 50 | } 51 | } 52 | dst[outlen] = '\0'; 53 | p = dst; 54 | 55 | for (i = 0; i < inlen - 2; i += 3) { 56 | *p++ = b64e[(src[i] >> 2) & 0x3F]; 57 | *p++ = b64e[((src[i] & 0x3) << 4) | ((src[i + 1] & 0xF0) >> 4)]; 58 | *p++ = b64e[((src[i + 1] & 0xF) << 2) | ((src[i + 2] & 0xC0) >> 6)]; 59 | *p++ = b64e[src[i + 2] & 0x3F]; 60 | } 61 | 62 | if (i < inlen) { 63 | *p++ = b64e[(src[i] >> 2) & 0x3F]; 64 | if (i == (inlen - 1)) { 65 | *p++ = b64e[((src[i] & 0x3) << 4)]; 66 | *p++ = '='; 67 | } else { 68 | *p++ = b64e[((src[i] & 0x3) << 4) | ((src[i + 1] & 0xF0) >> 4)]; 69 | *p++ = b64e[((src[i + 1] & 0xF) << 2)]; 70 | } 71 | *p++ = '='; 72 | } 73 | 74 | return dst; 75 | } 76 | 77 | /* b64Decode - decodes an array of bytes from base64 78 | * Inputs: 79 | * - dst : the destination array (if it's NULL, the function will allocate the 80 | * buffer) 81 | * - src : the source array 82 | * - srclen : the length of the source array 83 | * Safety: 84 | * if dst is not NULL, then it must point to an array that has the size of at 85 | * least (srclen / 4 * 3) 86 | */ 87 | char *b64Decode(char *dst, char *src, char srclen) { 88 | static const char b64d[] = { 89 | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 90 | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 91 | 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63, 52, 53, 54, 55, 56, 57, 92 | 58, 59, 60, 61, 64, 64, 64, 64, 64, 64, 64, 0, 1, 2, 3, 4, 5, 6, 93 | 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 94 | 25, 64, 64, 64, 64, 64, 64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 95 | 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 96 | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 97 | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 98 | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 99 | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 100 | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 101 | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 102 | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 103 | 64, 64, 64, 64}; 104 | size_t i, j; 105 | size_t inlen = srclen; 106 | size_t outlen = B64_REV(inlen); 107 | 108 | if (inlen == 0 || inlen % 4) 109 | return NULL; 110 | if (src[inlen - 1] == '=') 111 | outlen--; 112 | if (src[inlen - 2] == '=') 113 | outlen--; 114 | 115 | if (!dst) { 116 | dst = malloc(outlen + 1); 117 | /* malloc failed so return early */ 118 | if (!dst) 119 | return NULL; 120 | } 121 | dst[outlen] = '\0'; 122 | 123 | for (i = 0, j = 0; i < inlen;) { 124 | unsigned int a = src[i] == '=' ? 0 & i++ : b64d[(src[i++])]; 125 | unsigned int b = src[i] == '=' ? 0 & i++ : b64d[(src[i++])]; 126 | unsigned int c = src[i] == '=' ? 0 & i++ : b64d[(src[i++])]; 127 | unsigned int d = src[i] == '=' ? 0 & i++ : b64d[(src[i++])]; 128 | 129 | unsigned int triple = 130 | (a << 3 * 6) + (b << 2 * 6) + (c << 1 * 6) + (d << 0 * 6); 131 | 132 | if (j < outlen) 133 | dst[j++] = (triple >> 2 * 8) & 0xFF; 134 | if (j < outlen) 135 | dst[j++] = (triple >> 1 * 8) & 0xFF; 136 | if (j < outlen) 137 | dst[j++] = (triple >> 0 * 8) & 0xFF; 138 | } 139 | 140 | return dst; 141 | } 142 | 143 | #endif 144 | -------------------------------------------------------------------------------- /tests/basic.c: -------------------------------------------------------------------------------- 1 | #include "b64.h" 2 | #include 3 | #include 4 | 5 | int main() { 6 | char *reencoded; 7 | char *input = "gergerg,1414124124124124332\n"; 8 | char *result = b64Encode(NULL, (void *)input, strlen(input)); 9 | char *expected_result = "Z2VyZ2VyZywxNDE0MTI0MTI0MTI0MTI0MzMyCg=="; 10 | if (strcmp(result, expected_result)) { 11 | printf("The result for encoding of string is wrong, left: %s, right: %s", 12 | result, expected_result); 13 | return 1; 14 | } else { 15 | puts("Encoding basic test passed"); 16 | } 17 | reencoded = b64Decode(NULL, result, strlen(result)); 18 | if (strcmp(reencoded, input)) { 19 | printf("The result for decoding of string is wrong, left: %s, right: %s", 20 | reencoded, input); 21 | return 1; 22 | } else { 23 | puts("Decoding basic test passed"); 24 | } 25 | return 0; 26 | } 27 | --------------------------------------------------------------------------------