├── .travis.yml ├── LICENSE ├── README.md ├── base64.c ├── base64.h ├── example ├── example-01.c ├── example-02.c └── makefile └── test ├── makefile └── test.c /.travis.yml: -------------------------------------------------------------------------------- 1 | language: c 2 | sudo: false 3 | script: 4 | - cd test 5 | - make test 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Rafa García 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Base64 2 | [![Build Status](https://travis-ci.org/rafagafe/base64.svg?branch=master)](https://travis-ci.org/rafagafe/base64) 3 | 4 | Base64 is a C library to convert from plain to base64 and vice versa suitable for embedded systems. 5 | 6 | # API 7 | To convert from plain to base64 the following function is used: 8 | ```C 9 | char* bintob64( char* dest, void const* src, size_t size ); 10 | ``` 11 | * The parameter 'src' is the source memory block to be converted. 12 | * The parameter 'size' is the size in bytes of the source memory block. 13 | * The parameter 'dest' is the destination buffer where the translation to base64 is stored. Also a null character is added at the end to become a null-terminated string. 14 | * The return value is a pointer to the null character. 15 | 16 | To convert from base64 to plain the following function is used: 17 | ```C 18 | void* b64tobin( void* dest, char const* src ); 19 | ``` 20 | * The parameter 'src' is the source base64 string. It must be terminated in a non base64-digit. A null-terminated string is OK. 21 | * The parameter 'dest' is the destination memory block where the result of convertion is stroed. 22 | * The return value is a null pointer if the base64 string has bad format. If the convertion is successfully the return value is a pointer to the end of the convertion. With this pointer you can get the length of the result. 23 | 24 | Convert from base64 to plain in the same memory block is possible using the previous function by setting the same pointer for the destination and source. Or just use the following wrapper function: 25 | ```C 26 | static inline void* b64decode( void* p ) { 27 | return b64tobin( p, (char*)p ); 28 | } 29 | ``` 30 | -------------------------------------------------------------------------------- /base64.c: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | 4 | 5 | Licensed under the MIT License . 6 | SPDX-License-Identifier: MIT 7 | Copyright (c) 2016-2018 Rafa Garcia . 8 | Permission is hereby granted, free of charge, to any person obtaining a copy 9 | of this software and associated documentation files (the "Software"), to deal 10 | in the Software without restriction, including without limitation the rights 11 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | copies of the Software, and to permit persons to whom the Software is 13 | furnished to do so, subject to the following conditions: 14 | The above copyright notice and this permission notice shall be included in all 15 | copies or substantial portions of the Software. 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. 23 | 24 | */ 25 | 26 | #include "base64.h" 27 | 28 | /** Escape values. */ 29 | enum special_e { 30 | notabase64 = 64, /**< Value to return when a non base64 digit is found. */ 31 | terminator = 65, /**< Value to return when the character '=' is found. */ 32 | }; 33 | 34 | /** Lookup table that converts a base64 digit to integer. */ 35 | static char const digittobin[] = { 36 | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 37 | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 38 | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63, 39 | 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 65, 64, 64, 40 | 64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 41 | 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64, 42 | 64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 43 | 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64, 44 | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 45 | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 46 | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 47 | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 48 | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 49 | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 50 | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 51 | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 52 | }; 53 | 54 | 55 | /* Convert a base64 null-terminated string to binary format.*/ 56 | void* b64tobin( void* dest, char const* src ) { 57 | unsigned char const* s = (unsigned char*)src; 58 | char* p = dest; 59 | for(;;) { 60 | 61 | int const a = digittobin[ *s ]; 62 | if ( a == notabase64 ) return p; 63 | if ( a == terminator ) return p; 64 | 65 | int const b = digittobin[ *++s ]; 66 | if ( b == notabase64 ) return 0; 67 | if ( b == terminator ) return 0; 68 | 69 | *p++ = ( a << 2u ) | ( b >> 4u ); 70 | 71 | int const c = digittobin[ *++s ]; 72 | if ( c == notabase64 ) return 0; 73 | 74 | int const d = digittobin[ *++s ]; 75 | if ( d == notabase64 ) return 0; 76 | if ( c == terminator ) { 77 | if ( d != terminator ) return 0; 78 | return p; 79 | } 80 | 81 | *p++ = ( b << 4u ) | ( c >> 2u ); 82 | 83 | if ( d == terminator ) return p; 84 | 85 | *p++ = ( c << 6u ) | ( d >> 0u ); 86 | ++s; 87 | } 88 | 89 | return p; 90 | } 91 | 92 | /** Lookup table that converts a integer to base64 digit. */ 93 | static char const bintodigit[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 94 | "abcdefghijklmnopqrstuvwxyz" 95 | "0123456789" 96 | "+/"; 97 | 98 | /** Get the first base 64 digit of a block of 4. 99 | * @param a The first byte of the source block of 3. 100 | * @return A base 64 digit. */ 101 | static int get0( int a ) { 102 | int const index = a >> 2u; 103 | return bintodigit[ index ]; 104 | } 105 | 106 | /** Get the second base 64 digit of a block of 4. 107 | * @param a The first byte of the source block of 3. 108 | * @param b The second byte of the source block of 3. 109 | * @return A base 64 digit. */ 110 | static int get1( int a, int b ) { 111 | int const indexA = ( a & 3 ) << 4u; 112 | int const indexB = b >> 4u; 113 | int const index = indexA | indexB; 114 | return bintodigit[ index ]; 115 | } 116 | 117 | /** Get the third base 64 digit of a block of 4. 118 | * @param b The second byte of the source block of 3. 119 | * @param c The third byte of the source block of 3. 120 | * @return A base 64 digit. */ 121 | static unsigned int get2( unsigned int b, unsigned int c ) { 122 | int const indexB = ( b & 15 ) << 2u; 123 | int const indexC = c >> 6u; 124 | int const index = indexB | indexC; 125 | return bintodigit[ index ]; 126 | } 127 | 128 | /** Get the fourth base 64 digit of a block of 4. 129 | * @param c The third byte of the source block of 3. 130 | * @return A base 64 digit. */ 131 | static int get3( int c ) { 132 | int const index = c & 0x3f; 133 | return bintodigit[ index ]; 134 | } 135 | 136 | /* Convert a binary memory block in a base64 null-terminated string. */ 137 | char* bintob64( char* dest, void const* src, size_t size ) { 138 | 139 | typedef struct { unsigned char a; unsigned char b; unsigned char c; } block_t; 140 | block_t const* block = (block_t*)src; 141 | for( ; size >= sizeof( block_t ); size -= sizeof( block_t ), ++block ) { 142 | *dest++ = get0( block->a ); 143 | *dest++ = get1( block->a, block->b ); 144 | *dest++ = get2( block->b, block->c ); 145 | *dest++ = get3( block->c ); 146 | } 147 | 148 | if ( !size ) goto final; 149 | 150 | *dest++ = get0( block->a ); 151 | if ( !--size ) { 152 | *dest++ = get1( block->a, 0 ); 153 | *dest++ = '='; 154 | *dest++ = '='; 155 | goto final; 156 | } 157 | 158 | *dest++ = get1( block->a, block->b ); 159 | *dest++ = get2( block->b, 0 ); 160 | *dest++ = '='; 161 | 162 | final: 163 | *dest = '\0'; 164 | return dest; 165 | } 166 | -------------------------------------------------------------------------------- /base64.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | 4 | 5 | Licensed under the MIT License . 6 | SPDX-License-Identifier: MIT 7 | Copyright (c) 2016-2018 Rafa Garcia . 8 | Permission is hereby granted, free of charge, to any person obtaining a copy 9 | of this software and associated documentation files (the "Software"), to deal 10 | in the Software without restriction, including without limitation the rights 11 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | copies of the Software, and to permit persons to whom the Software is 13 | furnished to do so, subject to the following conditions: 14 | The above copyright notice and this permission notice shall be included in all 15 | copies or substantial portions of the Software. 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. 23 | 24 | */ 25 | 26 | #ifndef _BASE64_H_ 27 | #define _BASE64_H_ 28 | 29 | #ifdef __cplusplus 30 | extern "C" { 31 | #endif 32 | 33 | #include 34 | 35 | /** @defgroup base64 Base64 Converter. 36 | * @{ */ 37 | 38 | /** Convert a binary memory block in a base64 null-terminated string. 39 | * @param dest Destination memory wher to put the base64 null-terminated string. 40 | * @param src Source binary memory block. 41 | * @param size Size in bytes of source binary memory block. 42 | * @return A pointer to the null character of the base64 null-terminated string. */ 43 | char* bintob64( char* dest, void const* src, size_t size ); 44 | 45 | /** Convert a base64 string to binary format. 46 | * @param dest Destination memory block. 47 | * @param src Source base64 string. 48 | * @return If success a pointer to the next byte in memory block. 49 | * Null if string has a bad format. */ 50 | void* b64tobin( void* dest, char const* src ); 51 | 52 | /** Convert a base64 string to binary format. 53 | * @param p Source base64 string and destination memory block. 54 | * @return If success a pointer to the next byte in memory block. 55 | * Null if string has a bad format. */ 56 | static inline void* b64decode( void* p ) { 57 | return b64tobin( p, (char*)p ); 58 | } 59 | 60 | /** @ } */ 61 | 62 | #ifdef __cplusplus 63 | } 64 | #endif 65 | 66 | #endif /* _BASE64_H_ */ 67 | -------------------------------------------------------------------------------- /example/example-01.c: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | 4 | 5 | Licensed under the MIT License . 6 | SPDX-License-Identifier: MIT 7 | Copyright (c) 2016-2018 Rafa Garcia . 8 | Permission is hereby granted, free of charge, to any person obtaining a copy 9 | of this software and associated documentation files (the "Software"), to deal 10 | in the Software without restriction, including without limitation the rights 11 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | copies of the Software, and to permit persons to whom the Software is 13 | furnished to do so, subject to the following conditions: 14 | The above copyright notice and this permission notice shall be included in all 15 | copies or substantial portions of the Software. 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. 23 | 24 | */ 25 | 26 | #include 27 | #include 28 | #include "base64.h" 29 | 30 | /* 1) Fill an array named 'binary'. 31 | * 2) Convert 'binary' in base64 format in an array named 'base64'. 32 | * 3) Print 'base64'. 33 | * 4) Convert 'base64' in binary format in an array named 'output'. 34 | * 5) Check if the conversion was successful. 35 | * 6) Check the length of 'output'. 36 | * 7) Compare 'binary' with 'output'. */ 37 | int main( void ) { 38 | 39 | char binary[64]; 40 | for( int i = 0; i < 64; ++i ) 41 | binary[i] = i; 42 | 43 | char base64[128]; 44 | bintob64( base64, binary, sizeof binary ); 45 | 46 | printf( "%s%s%s", "The base64: '", base64, "'.\n" ); 47 | 48 | char output[64]; 49 | char* const end = b64tobin( output, base64 ); 50 | if ( !end ) { 51 | fputs( "Bad base64 format.", stderr ); 52 | return -1; 53 | } 54 | 55 | int const outputlen = end - output; 56 | if ( outputlen != sizeof binary ) { 57 | fputs( "The length of the output is not as expected.\n", stderr ); 58 | return -1; 59 | } 60 | 61 | int const equal = !memcmp( binary, output, sizeof binary ); 62 | if( !equal ) { 63 | fputs( "The output is different from the input.\n", stderr ); 64 | return -1; 65 | } 66 | 67 | return 0; 68 | } -------------------------------------------------------------------------------- /example/example-02.c: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | 4 | 5 | Licensed under the MIT License . 6 | SPDX-License-Identifier: MIT 7 | Copyright (c) 2016-2018 Rafa Garcia . 8 | Permission is hereby granted, free of charge, to any person obtaining a copy 9 | of this software and associated documentation files (the "Software"), to deal 10 | in the Software without restriction, including without limitation the rights 11 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | copies of the Software, and to permit persons to whom the Software is 13 | furnished to do so, subject to the following conditions: 14 | The above copyright notice and this permission notice shall be included in all 15 | copies or substantial portions of the Software. 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. 23 | 24 | */ 25 | 26 | #include 27 | #include 28 | #include "base64.h" 29 | 30 | /* 1) A null-terminated string is stored in an array named 'plain'. 31 | * 2) The text in 'plain' is printed. 32 | * 3) Convert 'plain' in base64 format in an array named 'buffer'. 33 | * 4) Print 'buffer'. 34 | * 5) Convert 'buffer' in plain text in the same array 'buffer'. 35 | * 6) Check if the conversion was successful. 36 | * 7) Check the length of 'buffer'. 37 | * 8) Compare 'plain' with 'buffer'. 38 | * 9) Print the plain text stored in 'buffer'. */ 39 | int main( void ) { 40 | 41 | static char const plain[] = "This is a plain text."; 42 | puts( plain ); 43 | 44 | char buffer[128]; 45 | bintob64( buffer, plain, sizeof plain ); 46 | 47 | printf( "%s%s%s", "The base64: '", buffer, "'.\n" ); 48 | 49 | char* const end = b64decode( buffer ); 50 | if ( !end ) { 51 | fputs( "Bad base64 format.", stderr ); 52 | return -1; 53 | } 54 | 55 | int const outputlen = end - buffer; 56 | if ( outputlen != sizeof plain ) { 57 | fputs( "The length of the output is not as expected.\n", stderr ); 58 | return -1; 59 | } 60 | 61 | int const equal = !strcmp( plain, buffer ); 62 | if( !equal ) { 63 | fputs( "The output is different from the input.\n", stderr ); 64 | return -1; 65 | } 66 | 67 | puts( buffer ); 68 | 69 | return 0; 70 | } -------------------------------------------------------------------------------- /example/makefile: -------------------------------------------------------------------------------- 1 | 2 | CC = gcc 3 | CFLAGS = -std=c99 -Wall -pedantic -I../ 4 | 5 | src = $(wildcard *.c) 6 | src += $(wildcard ../*.c) 7 | obj = $(src:.c=.o) 8 | dep = $(obj:.o=.d) 9 | 10 | .PHONY: build all clean 11 | 12 | build: example-01 example-02 13 | 14 | all: clean build 15 | 16 | clean: 17 | rm -rf $(obj) 18 | rm -rf $(dep) 19 | rm -rf example-01 example-02 20 | 21 | example-01: example-01.o ../base64.o 22 | gcc $(CFLAGS) -o $@ $^ 23 | 24 | example-02: example-02.o ../base64.o 25 | gcc $(CFLAGS) -o $@ $^ 26 | 27 | -include $(dep) 28 | 29 | %.d: %.c 30 | $(CC) $(CFLAGS) $< -MM -MT $(@:.d=.o) >$@ 31 | -------------------------------------------------------------------------------- /test/makefile: -------------------------------------------------------------------------------- 1 | 2 | CC = gcc 3 | CFLAGS = -std=c99 -Wall -pedantic -I../ 4 | 5 | src = $(wildcard *.c) 6 | src += $(wildcard ../*.c) 7 | obj = $(src:.c=.o) 8 | dep = $(obj:.o=.d) 9 | 10 | .PHONY: build all clean test 11 | 12 | build: unit-test 13 | 14 | clean: 15 | rm -rf $(dep) 16 | rm -rf $(obj) 17 | rm -rf unit-test 18 | 19 | all: clean build 20 | 21 | test: unit-test 22 | ./unit-test 23 | 24 | unit-test: $(obj) 25 | gcc $(CFLAGS) -o $@ $^ 26 | 27 | -include $(dep) 28 | 29 | %.d: %.c 30 | $(CC) $(CFLAGS) $< -MM -MT $(@:.d=.o) >$@ 31 | -------------------------------------------------------------------------------- /test/test.c: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | 4 | 5 | Licensed under the MIT License . 6 | SPDX-License-Identifier: MIT 7 | Copyright (c) 2016-2018 Rafa Garcia . 8 | Permission is hereby granted, free of charge, to any person obtaining a copy 9 | of this software and associated documentation files (the "Software"), to deal 10 | in the Software without restriction, including without limitation the rights 11 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | copies of the Software, and to permit persons to whom the Software is 13 | furnished to do so, subject to the following conditions: 14 | The above copyright notice and this permission notice shall be included in all 15 | copies or substantial portions of the Software. 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. 23 | 24 | */ 25 | 26 | #include 27 | #include 28 | #include "base64.h" 29 | 30 | 31 | // ----------------------------------------------------- Test "framework": --- 32 | 33 | #define done() return 0 34 | #define fail() return __LINE__ 35 | static int checkqty = 0; 36 | #define check( x ) do { ++checkqty; if (!(x)) fail(); } while ( 0 ) 37 | 38 | struct test { 39 | int(*func)(void); 40 | char const* name; 41 | }; 42 | 43 | static int test_suit( struct test const* tests, int numtests ) { 44 | printf( "%s", "\n\nTests:\n" ); 45 | int failed = 0; 46 | for( int i = 0; i < numtests; ++i ) { 47 | printf( " %02d%s%-25s ", i, ": ", tests[i].name ); 48 | int linerr = tests[i].func(); 49 | if ( 0 == linerr ) 50 | printf( "%s", "OK\n" ); 51 | else { 52 | printf( "%s%d\n", "Failed, line: ", linerr ); 53 | ++failed; 54 | } 55 | } 56 | printf( "\n%s%d\n", "Total checks: ", checkqty ); 57 | printf( "%s[ %d / %d ]\r\n\n\n", "Tests PASS: ", numtests - failed, numtests ); 58 | return failed; 59 | } 60 | 61 | 62 | // ----------------------------------------------------------- Unit tests: --- 63 | 64 | static int wikiExample( void ) { 65 | 66 | static char const src[] = "any carnal pleasure."; 67 | int const srclen = sizeof src - 1; 68 | 69 | char buffer[32]; 70 | bintob64( buffer, src, srclen ); 71 | check( !strcmp( buffer, "YW55IGNhcm5hbCBwbGVhc3VyZS4=" ) ); 72 | 73 | char* end = b64tobin( buffer, buffer ); 74 | check( end ); 75 | 76 | *end = '\0'; 77 | check( !strcmp( buffer, src ) ); 78 | 79 | bintob64( buffer, src, srclen - 1 ); 80 | check( !strcmp( buffer, "YW55IGNhcm5hbCBwbGVhc3VyZQ==" ) ); 81 | 82 | bintob64( buffer, src, srclen - 2 ); 83 | check( !strcmp( buffer, "YW55IGNhcm5hbCBwbGVhc3Vy" ) ); 84 | 85 | done(); 86 | } 87 | 88 | static int allDigits( void ) { 89 | 90 | static char const digits[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 91 | "abcdefghijklmnopqrstuvwxyz" 92 | "0123456789" 93 | "+/"; 94 | 95 | char binary[72]; 96 | char* const end = b64tobin( binary, digits ); 97 | check( end ); 98 | 99 | int const binarylen = end - binary; 100 | int const digitslen = sizeof digits - 1; 101 | int const calclen = 3 * digitslen / 4; 102 | check( binarylen == calclen ); 103 | 104 | char base64[72]; 105 | char* const nullchar = bintob64( base64, binary, binarylen ); 106 | check( nullchar ); 107 | check( '\0' == *nullchar ); 108 | check( !strcmp( base64, digits ) ); 109 | 110 | done(); 111 | } 112 | 113 | static int aliasing ( void ) { 114 | 115 | static char const message[] = "aliasing"; 116 | 117 | char base64[32]; 118 | char* const nullchar = bintob64( base64, message, sizeof message ); 119 | check( nullchar ); 120 | check( '\0' == *nullchar ); 121 | check( nullchar - base64 == 4 * sizeof message / 3 ); 122 | 123 | char* end = b64decode( base64 ); 124 | check( end ); 125 | check( !strcmp( message, base64 ) ); 126 | 127 | done(); 128 | } 129 | 130 | static int sizes( void ) { 131 | char binary[6]; 132 | for( int i = 1; i < sizeof binary; ++i ) 133 | binary[i] = i; 134 | 135 | char base64[24]; 136 | for( int i = 1; i < sizeof binary; ++i ) { 137 | char* const b64end = bintob64( base64, binary, i ); 138 | int const b64len = b64end - base64; 139 | int const expectedLen = 4 * ( 1 + i / 4 ); 140 | check( b64len == expectedLen ); 141 | char* binend = b64decode( base64 ); 142 | check( binend ); 143 | int const binlen = binend - base64; 144 | check( binlen == i ); 145 | int const equal = 0 == memcmp( binary, base64, i ); 146 | check( equal ); 147 | } 148 | done(); 149 | } 150 | 151 | int badformats( void ) { 152 | { 153 | static char const base64[] = { 0 }; 154 | char binary[5]; 155 | char* end = b64tobin( binary, base64 ); 156 | check( end == binary ); 157 | } 158 | { 159 | static char const base64[] = { 'a', 'a', -5, 'a', 0 }; 160 | char binary[5]; 161 | char* end = b64tobin( binary, base64 ); 162 | check( !end ); 163 | } 164 | { 165 | char binary[5]; 166 | char* end = b64tobin( binary, "aaaa_" ); 167 | check( end - binary == 3 ); 168 | } 169 | { 170 | char binary[5]; 171 | char* end = b64tobin( binary, "aa_" ); 172 | check( !end ); 173 | } 174 | { 175 | char binary[5]; 176 | char* end = b64tobin( binary, "=aaaa_" ); 177 | check( end == binary ); 178 | } 179 | done(); 180 | } 181 | 182 | 183 | // --------------------------------------------------------- Execute tests: --- 184 | 185 | int main( void ) { 186 | static struct test const tests[] = { 187 | { wikiExample, "Wikipedia example" }, 188 | { allDigits, "All digits" }, 189 | { aliasing, "Aliasing" }, 190 | { sizes, "Sizes" }, 191 | { badformats, "Bad Formats" } 192 | }; 193 | return test_suit( tests, sizeof tests / sizeof *tests ); 194 | } 195 | --------------------------------------------------------------------------------