From 4b86fe7c8b0ef51163bb6511de97c47cb4473986 Mon Sep 17 00:00:00 2001 From: Robert Edmonds Date: Thu, 17 Sep 2015 12:43:58 -0400 Subject: [PATCH 1/4] my/b32_decode: New --- b32_decode.c | 189 +++++++++++++++++++++++++++++++++++++++++++++++++++ b32_decode.h | 52 ++++++++++++++ 2 files changed, 241 insertions(+) create mode 100644 b32_decode.c create mode 100644 b32_decode.h diff --git a/b32_decode.c b/b32_decode.c new file mode 100644 index 000000000..53038114b --- /dev/null +++ b/b32_decode.c @@ -0,0 +1,189 @@ +/* + * Copyright (c) 2015 by Farsight Security, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * Copyright (c) 2006 Christian Biere + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the authors nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * See RFC 4648 for details about Base 32 hex encoding: + * http://tools.ietf.org/html/rfc4648 + */ + +#include +#include + +#include "b32_decode.h" + +#ifndef G_N_ELEMENTS +#define G_N_ELEMENTS(arr) (sizeof (arr) / sizeof ((arr)[0])) +#endif + +#define ZERO(x) memset((x), 0, sizeof *(x)) + +static inline void * +ptr_add_offset(void *p, size_t offset) +{ + /* Using size_t instead of 'char *' because pointers don't wrap. */ + return (void *) ((size_t) p + offset); +} + +static inline const void * +cast_to_constpointer(const void *p) +{ + return p; +} + +static inline bool +is_ascii_lower(int c) +{ + return c >= 97 && c <= 122; /* a-z */ +} + +static inline int +ascii_toupper(int c) +{ + return is_ascii_lower(c) ? c - 32 : c; +} + +static inline size_t +ptr_diff(const void *a, const void *b) +{ + return (const char *) a - (const char *) b; +} + +static const char base32_alphabet[32] = { + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', + 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', + 'U', 'V' +}; + +static char base32_map[(size_t) (unsigned char) -1 + 1]; + +/** + * Decode a base32 encoding of `len' bytes of `data' into the buffer `dst'. + * + * @param dst destination buffer + * @param size length of destination + * @param data start of data to decode + * @param len amount of encoded data to decode + * + * @return the amount of bytes decoded into the destination. + */ +size_t +base32_decode(void *dst, size_t size, const char *data, size_t len) +{ + const char *end = ptr_add_offset(dst, size); + const unsigned char *p = cast_to_constpointer(data); + char s[8]; + char *q = dst; + int pad = 0; + size_t i, si; + + if (0 == base32_map[0]) { + for (i = 0; i < G_N_ELEMENTS(base32_map); i++) { + const char *x; + + x = memchr(base32_alphabet, ascii_toupper(i), + sizeof base32_alphabet); + base32_map[i] = x ? (x - base32_alphabet) : (unsigned char) -1; + } + } + + ZERO(&s); + si = 0; + i = 0; + + while (i < len) { + unsigned char c; + + c = p[i++]; + if ('=' == c) { + pad++; + c = 0; + } else { + c = base32_map[c]; + if ((unsigned char) -1 == c) { + return -1; + } + } + + s[si++] = c; + + if (G_N_ELEMENTS(s) == si || pad > 0 || i == len) { + char b[5]; + size_t bi; + + memset(&s[si], 0, G_N_ELEMENTS(s) - si); + si = 0; + + b[0] = + ((s[0] << 3) & 0xf8) | + ((s[1] >> 2) & 0x07); + b[1] = + ((s[1] & 0x03) << 6) | + ((s[2] & 0x1f) << 1) | + ((s[3] >> 4) & 1); + b[2] = + ((s[3] & 0x0f) << 4) | + ((s[4] >> 1) & 0x0f); + b[3] = + ((s[4] & 1) << 7) | + ((s[5] & 0x1f) << 2) | + ((s[6] >> 3) & 0x03); + b[4] = + ((s[6] & 0x07) << 5) | + (s[7] & 0x1f); + + for (bi = 0; bi < G_N_ELEMENTS(b) && q != end; bi++) { + *q++ = b[bi]; + } + } + + if (end == q) { + break; + } + } + + return ptr_diff(q, dst); +} diff --git a/b32_decode.h b/b32_decode.h new file mode 100644 index 000000000..bc8d4ad9f --- /dev/null +++ b/b32_decode.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2015 by Farsight Security, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * Copyright (c) 2006 Christian Biere + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the authors nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef B32_DECODE_H +#define B32_DECODE_H + +size_t base32_decode(void *dst, size_t size, const char *data, size_t len); + +#endif /* B32_DECODE_H */ From f23da6aad58feec13befd51ed95e5c0a355a4662 Mon Sep 17 00:00:00 2001 From: Robert Edmonds Date: Thu, 17 Sep 2015 12:44:08 -0400 Subject: [PATCH 2/4] my/b32_encode: New --- b32_encode.c | 127 +++++++++++++++++++++++++++++++++++++++++++++++++++ b32_encode.h | 52 +++++++++++++++++++++ 2 files changed, 179 insertions(+) create mode 100644 b32_encode.c create mode 100644 b32_encode.h diff --git a/b32_encode.c b/b32_encode.c new file mode 100644 index 000000000..6487fc440 --- /dev/null +++ b/b32_encode.c @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2010 by Internet Systems Consortium, Inc. ("ISC") + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT + * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +/* + * Copyright (c) 2006 Christian Biere + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the authors nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * See RFC 4648 for details about Base 32 hex encoding: + * http://tools.ietf.org/html/rfc4648 + */ + +#include +#include + +#include "b32_encode.h" + +static const char base32_alphabet[32] = { + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', + 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', + 'U', 'V' +}; + +/** + * Encode in base32 `len' bytes of `data' into the buffer `dst'. + * + * @param dst destination buffer + * @param size length of destination + * @param data start of data to encode + * @param len amount of bytes to encode + * + * @return the amount of bytes generated into the destination. + */ +size_t +base32_encode(char *dst, size_t size, const void *data, size_t len) +{ + size_t i = 0; + const uint8_t *p = data; + const char *end = &dst[size]; + char *q = dst; + + do { + size_t j, k; + uint8_t x[5]; + char s[8]; + + switch (len - i) { + case 4: + k = 7; + break; + case 3: + k = 5; + break; + case 2: + k = 3; + break; + case 1: + k = 2; + break; + default: + k = 8; + } + + for (j = 0; j < 5; j++) + x[j] = i < len ? p[i++] : 0; + + s[0] = (x[0] >> 3); + s[1] = ((x[0] & 0x07) << 2) | (x[1] >> 6); + s[2] = (x[1] >> 1) & 0x1f; + s[3] = ((x[1] & 0x01) << 4) | (x[2] >> 4); + s[4] = ((x[2] & 0x0f) << 1) | (x[3] >> 7); + s[5] = (x[3] >> 2) & 0x1f; + s[6] = ((x[3] & 0x03) << 3) | (x[4] >> 5); + s[7] = x[4] & 0x1f; + + for (j = 0; j < k && q != end; j++) { + *q++ = base32_alphabet[(uint8_t) s[j]]; + } + + if (end == q) { + break; + } + + } while (i < len); + + return q - dst; +} diff --git a/b32_encode.h b/b32_encode.h new file mode 100644 index 000000000..2500f3900 --- /dev/null +++ b/b32_encode.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2010 by Internet Systems Consortium, Inc. ("ISC") + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT + * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +/* + * Copyright (c) 2006 Christian Biere + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the authors nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef B32_ENCODE_H +#define B32_ENCODE_H + +size_t base32_encode(char *dst, size_t dst_len, const void *src, size_t src_len); + +#endif /* B32_ENCODE_H */ From 06a6eb482b74f57a85caf908ddfdd0af4cc86cb5 Mon Sep 17 00:00:00 2001 From: Robert Edmonds Date: Thu, 17 Sep 2015 12:44:19 -0400 Subject: [PATCH 3/4] my/b64_decode: New --- b64_decode.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++ b64_decode.h | 28 +++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 b64_decode.c create mode 100644 b64_decode.h diff --git a/b64_decode.c b/b64_decode.c new file mode 100644 index 000000000..863839df5 --- /dev/null +++ b/b64_decode.c @@ -0,0 +1,87 @@ +/* +cdecoder.c - c source to a base64 decoding algorithm implementation + +This is part of the libb64 project, and has been placed in the public domain. +For details, see http://sourceforge.net/projects/libb64 +*/ + +#include + +int base64_decode_value(char value_in) +{ + static const char decoding[] = {62,-1,-1,-1,63,52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-2,-1,-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,-1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51}; + static const char decoding_size = sizeof(decoding); + value_in -= 43; + if (value_in < 0 || value_in > decoding_size) return -1; + return decoding[(int)value_in]; +} + +void base64_init_decodestate(base64_decodestate* state_in) +{ + state_in->step = step_a; + state_in->plainchar = 0; +} + +int base64_decode_block(const char* code_in, const int length_in, char* plaintext_out, base64_decodestate* state_in) +{ + const char* codechar = code_in; + char* plainchar = plaintext_out; + char fragment; + + *plainchar = state_in->plainchar; + + switch (state_in->step) + { + while (1) + { + case step_a: + do { + if (codechar == code_in+length_in) + { + state_in->step = step_a; + state_in->plainchar = *plainchar; + return plainchar - plaintext_out; + } + fragment = (char)base64_decode_value(*codechar++); + } while (fragment < 0); + *plainchar = (fragment & 0x03f) << 2; + case step_b: + do { + if (codechar == code_in+length_in) + { + state_in->step = step_b; + state_in->plainchar = *plainchar; + return plainchar - plaintext_out; + } + fragment = (char)base64_decode_value(*codechar++); + } while (fragment < 0); + *plainchar++ |= (fragment & 0x030) >> 4; + *plainchar = (fragment & 0x00f) << 4; + case step_c: + do { + if (codechar == code_in+length_in) + { + state_in->step = step_c; + state_in->plainchar = *plainchar; + return plainchar - plaintext_out; + } + fragment = (char)base64_decode_value(*codechar++); + } while (fragment < 0); + *plainchar++ |= (fragment & 0x03c) >> 2; + *plainchar = (fragment & 0x003) << 6; + case step_d: + do { + if (codechar == code_in+length_in) + { + state_in->step = step_d; + state_in->plainchar = *plainchar; + return plainchar - plaintext_out; + } + fragment = (char)base64_decode_value(*codechar++); + } while (fragment < 0); + *plainchar++ |= (fragment & 0x03f); + } + } + /* control should not reach here */ + return plainchar - plaintext_out; +} diff --git a/b64_decode.h b/b64_decode.h new file mode 100644 index 000000000..d0d7f489d --- /dev/null +++ b/b64_decode.h @@ -0,0 +1,28 @@ +/* +cdecode.h - c header for a base64 decoding algorithm + +This is part of the libb64 project, and has been placed in the public domain. +For details, see http://sourceforge.net/projects/libb64 +*/ + +#ifndef BASE64_CDECODE_H +#define BASE64_CDECODE_H + +typedef enum +{ + step_a, step_b, step_c, step_d +} base64_decodestep; + +typedef struct +{ + base64_decodestep step; + char plainchar; +} base64_decodestate; + +void base64_init_decodestate(base64_decodestate* state_in); + +int base64_decode_value(char value_in); + +int base64_decode_block(const char* code_in, const int length_in, char* plaintext_out, base64_decodestate* state_in); + +#endif /* BASE64_CDECODE_H */ From cfcc9b186624d7369b1f5433df972fc2af782dbf Mon Sep 17 00:00:00 2001 From: Robert Edmonds Date: Thu, 17 Sep 2015 12:44:30 -0400 Subject: [PATCH 4/4] my/b64_encode: New --- b64_encode.c | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++ b64_encode.h | 31 +++++++++++++++ 2 files changed, 140 insertions(+) create mode 100644 b64_encode.c create mode 100644 b64_encode.h diff --git a/b64_encode.c b/b64_encode.c new file mode 100644 index 000000000..50cedd11f --- /dev/null +++ b/b64_encode.c @@ -0,0 +1,109 @@ +/* +cencoder.c - c source to a base64 encoding algorithm implementation + +This is part of the libb64 project, and has been placed in the public domain. +For details, see http://sourceforge.net/projects/libb64 +*/ + +#include "b64_encode.h" + +const int CHARS_PER_LINE = 56; + +void base64_init_encodestate(base64_encodestate* state_in) +{ + state_in->step = step_A; + state_in->result = 0; + state_in->stepcount = 0; +} + +char base64_encode_value(char value_in) +{ + static const char* encoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + if (value_in > 63) return '='; + return encoding[(int)value_in]; +} + +int base64_encode_block(const char* plaintext_in, int length_in, char* code_out, base64_encodestate* state_in) +{ + const char* plainchar = plaintext_in; + const char* const plaintextend = plaintext_in + length_in; + char* codechar = code_out; + char result; + char fragment; + + result = state_in->result; + + switch (state_in->step) + { + while (1) + { + case step_A: + if (plainchar == plaintextend) + { + state_in->result = result; + state_in->step = step_A; + return codechar - code_out; + } + fragment = *plainchar++; + result = (fragment & 0x0fc) >> 2; + *codechar++ = base64_encode_value(result); + result = (fragment & 0x003) << 4; + case step_B: + if (plainchar == plaintextend) + { + state_in->result = result; + state_in->step = step_B; + return codechar - code_out; + } + fragment = *plainchar++; + result |= (fragment & 0x0f0) >> 4; + *codechar++ = base64_encode_value(result); + result = (fragment & 0x00f) << 2; + case step_C: + if (plainchar == plaintextend) + { + state_in->result = result; + state_in->step = step_C; + return codechar - code_out; + } + fragment = *plainchar++; + result |= (fragment & 0x0c0) >> 6; + *codechar++ = base64_encode_value(result); + result = (fragment & 0x03f) >> 0; + *codechar++ = base64_encode_value(result); + + ++(state_in->stepcount); + if (state_in->stepcount == CHARS_PER_LINE/4) + { + *codechar++ = ' '; + state_in->stepcount = 0; + } + } + } + /* control should not reach here */ + return codechar - code_out; +} + +int base64_encode_blockend(char* code_out, base64_encodestate* state_in) +{ + char* codechar = code_out; + + switch (state_in->step) + { + case step_B: + *codechar++ = base64_encode_value(state_in->result); + *codechar++ = '='; + *codechar++ = '='; + break; + case step_C: + *codechar++ = base64_encode_value(state_in->result); + *codechar++ = '='; + break; + case step_A: + break; + } + *codechar++ = ' '; + + return codechar - code_out; +} + diff --git a/b64_encode.h b/b64_encode.h new file mode 100644 index 000000000..c1e3464af --- /dev/null +++ b/b64_encode.h @@ -0,0 +1,31 @@ +/* +cencode.h - c header for a base64 encoding algorithm + +This is part of the libb64 project, and has been placed in the public domain. +For details, see http://sourceforge.net/projects/libb64 +*/ + +#ifndef BASE64_CENCODE_H +#define BASE64_CENCODE_H + +typedef enum +{ + step_A, step_B, step_C +} base64_encodestep; + +typedef struct +{ + base64_encodestep step; + char result; + int stepcount; +} base64_encodestate; + +void base64_init_encodestate(base64_encodestate* state_in); + +char base64_encode_value(char value_in); + +int base64_encode_block(const char* plaintext_in, int length_in, char* code_out, base64_encodestate* state_in); + +int base64_encode_blockend(char* code_out, base64_encodestate* state_in); + +#endif /* BASE64_CENCODE_H */