├── .travis.yml ├── test ├── makefile └── base85-test.c ├── example ├── makefile ├── example-02.c └── example-01.c ├── LICENSE ├── README.md ├── base85.h └── base85.c /.travis.yml: -------------------------------------------------------------------------------- 1 | language: c 2 | sudo: false 3 | script: 4 | - cd test 5 | - make test 6 | -------------------------------------------------------------------------------- /test/makefile: -------------------------------------------------------------------------------- 1 | 2 | CC = gcc 3 | CFLAGS = -std=c99 -Wall -pedantic 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: base85-test 13 | 14 | clean: 15 | rm -rf $(dep) 16 | rm -rf $(obj) 17 | rm -rf base85-test 18 | 19 | all: clean build 20 | 21 | test: base85-test 22 | ./base85-test 23 | 24 | base85-test: $(obj) 25 | gcc $(CFLAGS) -o $@ $^ 26 | 27 | -include $(dep) 28 | 29 | %.d: %.c 30 | $(CC) $(CFLAGS) $< -MM -MT $(@:.d=.o) >$@ 31 | -------------------------------------------------------------------------------- /example/makefile: -------------------------------------------------------------------------------- 1 | 2 | CC = gcc 3 | CFLAGS = -std=c99 -Wall -pedantic 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 ../base85.o 22 | gcc $(CFLAGS) -o $@ $^ 23 | 24 | example-02: example-02.o ../base85.o 25 | gcc $(CFLAGS) -o $@ $^ 26 | 27 | -include $(dep) 28 | 29 | %.d: %.c 30 | $(CC) $(CFLAGS) $< -MM -MT $(@:.d=.o) >$@ 31 | -------------------------------------------------------------------------------- /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 | # Base85 2 | [![Build Status](https://travis-ci.org/rafagafe/base85.svg?branch=master)](https://travis-ci.org/rafagafe/base85) 3 | 4 | Base85 is a C library to convert from plain to base 85 and vice versa suitable for embedded systems. It uses the character set of the Base85 RFC 1924. 5 | 6 | 7 | # API 8 | To convert from plain to base 85 the following function is used: 9 | ```C 10 | char* bintob85( char* dest, void const* src, size_t size ); 11 | ``` 12 | * The parameter 'src' is the source memory block to be converted. 13 | * The parameter 'size' is the size in bytes of the source memory block. 14 | * The parameter 'dest' is the destination buffer where the translation to base 85 is stored. Also a null character is added at the end to become a null-terminated string. 15 | * The return value is a pointer to the null character. 16 | 17 | To convert from base 85 to plain the following function is used: 18 | ```C 19 | void* b85tobin( void* dest, char const* src ); 20 | ``` 21 | * The parameter 'src' is the source base 85 string. It must be terminated in a non base 85 digit. A null-terminated string is OK. 22 | * The parameter 'dest' is the destination memory block where the result of convertion is stroed. 23 | * The return value is a null pointer if the base 85 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. 24 | 25 | Convert from base 85 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: 26 | ```C 27 | static inline void* b85decode( void* p ) { 28 | return b85tobin( p, (char*)p ); 29 | } 30 | ``` 31 | -------------------------------------------------------------------------------- /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 "../base85.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 base85 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 | bintob85( buffer, plain, sizeof plain ); 46 | 47 | printf( "%s%s%s", "The base85: '", buffer, "'.\n" ); 48 | 49 | char* const end = b85decode( buffer ); 50 | if ( !end ) { 51 | fputs( "Bad base85 format.", stderr ); 52 | return -1; 53 | } 54 | 55 | int const equal = !strcmp( plain, buffer ); 56 | if( !equal ) { 57 | fputs( "The output is different from the input.\n", stderr ); 58 | return -1; 59 | } 60 | 61 | puts( buffer ); 62 | 63 | return 0; 64 | } -------------------------------------------------------------------------------- /example/example-01.c: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | 4 | 5 | Licensed under the MIT License . 6 | SPDX-License-Identifier: MIT 7 | Copyright (c) 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 "../base85.h" 29 | 30 | /* 1) Fill an array named 'binary'. 31 | * 2) Convert 'binary' in base85 format in an array named 'base85'. 32 | * 3) Print 'base85'. 33 | * 4) Convert 'base85' 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 base85[128]; 44 | bintob85( base85, binary, sizeof binary ); 45 | 46 | printf( "%s%s%s", "The base85: '", base85, "'.\n" ); 47 | 48 | char output[64]; 49 | char* const end = b85tobin( output, base85 ); 50 | if ( !end ) { 51 | fputs( "Bad base85 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 | } -------------------------------------------------------------------------------- /base85.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 BASE85_H 27 | #define BASE85_H 28 | 29 | #ifdef __cplusplus 30 | extern "C" { 31 | #endif 32 | 33 | #include 34 | 35 | /** @defgroup base85 Base85 Converter. 36 | * Base85 RFC 1924 version. The character set is, in order, 0–9, A–Z, a–z, and 37 | * then the 23 characters !#$%&()*+-;<=>?@^_`{|}~. 38 | * @{ */ 39 | 40 | /** Convert a binary memory block in a base85 null-terminated string. 41 | * If the size of the source memory block is not a multiple of four, 42 | * as many zeros as necessary are added to convert it to a multiple of four. 43 | * @param dest Destination memory where to put the base85 null-terminated string. 44 | * @param src Source binary memory block. 45 | * @param size Size in bytes of source binary memory block. 46 | * @return A pointer to the null character of the base85 null-terminated string. */ 47 | char* bintob85( char* restrict dest, void const* restrict src, size_t size ); 48 | 49 | /** Convert a base85 string to binary format. 50 | * @param dest Destination memory block. 51 | * @param src Source base85 string. 52 | * @return If success a pointer to the next byte in memory block. 53 | * Null if string has a bad format. */ 54 | void* b85tobin( void* restrict dest, char const* restrict src ); 55 | 56 | /** Convert a base85 string to binary format. 57 | * @param p Source base85 string and destination memory block. 58 | * @return If success a pointer to the next byte in memory block. 59 | * Null if string has a bad format. */ 60 | static inline void* b85decode( void* p ) { 61 | return b85tobin( p, (char*)p ); 62 | } 63 | 64 | static inline char* b85encode( void* p, size_t size ) { 65 | return bintob85( (char*)p, p, size ); 66 | } 67 | 68 | /** @ } */ 69 | 70 | #ifdef __cplusplus 71 | } 72 | #endif 73 | 74 | #endif /* BASE85_H */ -------------------------------------------------------------------------------- /test/base85-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 "../base85.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 allDigits( void ) { 65 | 66 | static char const digits[] = "0123456789" 67 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 68 | "abcdefghijklmnopqrstuvwxyz" 69 | "!#$%&()*+-;<=>?@^_`{|}~"; 70 | 71 | char binary[68]; 72 | char* const end = b85tobin( binary, digits ); 73 | check( end ); 74 | 75 | int const binarylen = end - binary; 76 | int const digitslen = sizeof digits - 1; 77 | int const calclen = 4 * digitslen / 5; 78 | check( binarylen == calclen ); 79 | 80 | char base85[85]; 81 | char* const nullchar = bintob85( base85, binary, binarylen ); 82 | check( nullchar ); 83 | check( '\0' == *nullchar ); 84 | check( !strcmp( base85, digits ) ); 85 | 86 | done(); 87 | } 88 | 89 | static int bintob85size( int binsz ) { 90 | int const regularSize = 5 * ( binsz / 4 ); 91 | int const remainder = binsz % 4; 92 | int const extraSize = remainder? 5: 0; 93 | return regularSize + extraSize; 94 | } 95 | 96 | static int aliasing ( void ) { 97 | 98 | static char const message[] = "Aliasing"; 99 | 100 | char base85[32]; 101 | memcpy( base85, message, sizeof message ); 102 | char* const nullchar = b85encode( base85, sizeof message ); 103 | check( nullchar ); 104 | check( '\0' == *nullchar ); 105 | int const len = nullchar - base85; 106 | int const expectlen = bintob85size( sizeof message ); 107 | check( len == expectlen ); 108 | 109 | char* end = b85decode( base85 ); 110 | check( end ); 111 | check( !strcmp( message, base85 ) ); 112 | 113 | done(); 114 | } 115 | 116 | int badformats( void ) { 117 | { 118 | static char const base85[] = { 0 }; 119 | char binary[5]; 120 | char* end = b85tobin( binary, base85 ); 121 | check( end == binary ); 122 | } 123 | { 124 | static char const base85[] = { '0', -1, '1', '2', '3', 0 }; 125 | char binary[32]; 126 | char* end = b85tobin( binary, base85 ); 127 | check( !end ); 128 | } 129 | { 130 | static char const base85[] = "aaaaaa"; 131 | char binary[32]; 132 | char* end = b85tobin( binary, base85 ); 133 | check( !end ); 134 | } 135 | done(); 136 | } 137 | 138 | 139 | // --------------------------------------------------------- Execute tests: --- 140 | 141 | int main( void ) { 142 | static struct test const tests[] = { 143 | { allDigits, "All digits" }, 144 | { aliasing, "Aliasing" }, 145 | { badformats, "Bad Formats" } 146 | }; 147 | return test_suit( tests, sizeof tests / sizeof *tests ); 148 | } 149 | -------------------------------------------------------------------------------- /base85.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 "base85.h" 27 | 28 | /** Lookup table to convert a binary number in a base 85 digit. */ 29 | static char const bintodigit[] = { 30 | 31 | '0','1','2','3','4','5','6','7','8','9', 32 | 33 | 'A','B','C','D','E','F','G','H','I','J','K','L','M', 34 | 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z', 35 | 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 | 39 | '!','#','$','%','&','(',')','*','+','-',';', 40 | '<','=','>','?','@','^','_','`','{','|','}','~', 41 | }; 42 | 43 | /** Escape values. */ 44 | enum escape { 45 | notadigit = 85u /**< Return value when a non-digit-base-85 is found. */ 46 | }; 47 | 48 | /** Lookup table to convert a base 85 digit in a binary number. */ 49 | static unsigned char const digittobin[] = { 50 | 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 51 | 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 52 | 85, 62, 85, 63, 64, 65, 66, 85, 67, 68, 69, 70, 85, 71, 85, 85, 53 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 85, 72, 73, 74, 75, 76, 54 | 77, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 55 | 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 85, 85, 85, 78, 79, 56 | 80, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 57 | 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 81, 82, 83, 84, 85, 58 | 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 59 | 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 60 | 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 61 | 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 62 | 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 63 | 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 64 | 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 65 | 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 85, 66 | }; 67 | 68 | /* Some powers of 85. */ 69 | #define p850 1ul /*< 85^0 */ 70 | #define p851 85ul /*< 85^1 */ 71 | #define p852 (p851 * p851) /*< 85^2 */ 72 | #define p853 (p851 * p852) /*< 85^3 */ 73 | #define p854 (p851 * p853) /*< 85^4 */ 74 | 75 | 76 | /** Powers of 85 list. */ 77 | static unsigned long const pow85[] = { p854, p853, p852, p851, p850 }; 78 | 79 | /** Converts a integer of 4 bytes in 5 digits of base 85. 80 | * @param dest Memory block where to put the 5 digits of base 85. 81 | * @param value Value of the integer of 4 bytes. */ 82 | static void ultob85( char* dest, unsigned int long value ) { 83 | 84 | unsigned int const digitsQty = sizeof pow85 / sizeof *pow85; 85 | 86 | for( unsigned int i = 0; i < digitsQty; ++i ) { 87 | unsigned int const bin = value / pow85[ i ]; 88 | dest[ i ] = bintodigit[ bin ]; 89 | value -= bin * pow85[ i ]; 90 | } 91 | } 92 | 93 | /** Helper constant to get the endianness of the running machine. */ 94 | static short const endianness = 1; 95 | 96 | /** Points to 1 if little-endian or points to 0 if big-endian. */ 97 | static char const* const littleEndian = (char const*)&endianness; 98 | 99 | /** Convert a big-endian array of bytes in a unsigned long. 100 | * @param src Pointer to array of bytes. 101 | * @param sz Size in bytes of array from 0 until 4. 102 | * @return The unsigned long value. */ 103 | static unsigned long betoul( void const* src, int sz ) { 104 | 105 | unsigned long value = 0; 106 | char* const d = (char*)&value; 107 | char const* const s = (char const*)src; 108 | 109 | for( int i = 0; i < sz; ++i ) 110 | d[ *littleEndian ? 3 - i : i ] = s[ i ]; 111 | 112 | for( int i = sz; i < 4; ++i ) 113 | d[ *littleEndian ? 3 - i : i ] = 0; 114 | 115 | return value; 116 | } 117 | 118 | char* bintob85( char* restrict dest, void const* restrict src, size_t size ) { 119 | 120 | size_t const quartets = size / 4; 121 | char const* s = (char*)src + 4 * quartets; 122 | dest += 5 * quartets; 123 | 124 | int const remainder = size % 4; 125 | if ( remainder ) 126 | ultob85( dest, betoul( s, remainder ) ); 127 | 128 | char* rslt = dest + ( remainder ? 5 : 0 ); 129 | *rslt = '\0'; 130 | 131 | for( size_t i = 0; i < quartets; ++i ) 132 | ultob85( dest -= 5, betoul( s -= 4, 4 ) ); 133 | 134 | return rslt; 135 | } 136 | 137 | /** Copy a unsigned long in a big-endian array of 4 bytes. 138 | * @param dest Destination memory block. 139 | * @param value Value to copy. 140 | * @return dest + 4 */ 141 | static void* ultobe( void* dest, unsigned long value ) { 142 | 143 | char* const d = (char*)dest; 144 | char const* const s = (char*)&value; 145 | 146 | for( int i = 0; i < 4; ++i ) 147 | d[ i ] = s[ *littleEndian ? 3 - i : i ]; 148 | 149 | return d + 4; 150 | } 151 | 152 | /* Convert a base85 string to binary format. */ 153 | void* b85tobin( void* restrict dest, char const* restrict src ) { 154 | 155 | for( unsigned char const* s = (unsigned char const*)src;; ) { 156 | 157 | unsigned long value = 0; 158 | for( int i = 0; i < sizeof pow85 / sizeof *pow85; ++i, ++s ) { 159 | unsigned int const bin = digittobin[ *s ]; 160 | if ( bin == notadigit ) 161 | return i == 0 ? dest : 0; 162 | value += bin * pow85[ i ]; 163 | } 164 | 165 | dest = ultobe( dest, value ); 166 | } 167 | } 168 | --------------------------------------------------------------------------------