|
| 1 | +// |
| 2 | +// Copyright (C) 2024-2026 The Goldfish Scheme Authors |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied See the |
| 13 | +// License for the specific language governing permissions and limitations |
| 14 | +// under the License. |
| 15 | +// |
| 16 | + |
| 17 | +#include "s7.h" |
| 18 | +#include <cstring> |
| 19 | + |
| 20 | +namespace goldfish { |
| 21 | + |
| 22 | +static s7_pointer |
| 23 | +base64_error (s7_scheme* sc, const char* caller, const char* kind, const char* msg, s7_pointer arg) { |
| 24 | + return s7_error (sc, s7_make_symbol (sc, kind), s7_list (sc, 2, s7_make_string (sc, msg), arg)); |
| 25 | +} |
| 26 | + |
| 27 | +static s7_pointer |
| 28 | +f_bytevector_base64_decode (s7_scheme* sc, s7_pointer args) { |
| 29 | + s7_pointer arg= s7_car (args); |
| 30 | + |
| 31 | + if (!s7_is_byte_vector (arg)) { |
| 32 | + return base64_error (sc, "bytevector-base64-decode", "type-error", |
| 33 | + "bytevector-base64-decode: input must be bytevector", arg); |
| 34 | + } |
| 35 | + |
| 36 | + s7_int in_len= s7_integer (s7_cadr (args)); |
| 37 | + uint8_t* in = (uint8_t*) s7_byte_vector_elements (arg); |
| 38 | + |
| 39 | + if (in_len % 4 != 0) { |
| 40 | + return base64_error (sc, "bytevector-base64-decode", "value-error", |
| 41 | + "bytevector-base64-decode: length of the input bytevector must be 4X", arg); |
| 42 | + } |
| 43 | + |
| 44 | + static const uint8_t decode_table[256]= { |
| 45 | + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 46 | + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, |
| 47 | + 255, 255, 255, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 255, 255, 255, 255, 255, 0, |
| 48 | + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, |
| 49 | + 23, 24, 25, 255, 255, 255, 255, 255, 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, |
| 50 | + 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 51 | + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 52 | + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 53 | + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 54 | + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 55 | + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
| 56 | + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}; |
| 57 | + |
| 58 | + const uint8_t PAD= (uint8_t) '='; |
| 59 | + |
| 60 | + s7_int out_cap= (in_len / 4) * 3; |
| 61 | + s7_pointer out = s7_make_byte_vector (sc, out_cap, 1, NULL); |
| 62 | + uint8_t* out_buf= (uint8_t*) s7_byte_vector_elements (out); |
| 63 | + s7_int out_len= 0; |
| 64 | + |
| 65 | + for (s7_int i= 0; i < in_len; i+= 4) { |
| 66 | + uint8_t c1= in[i]; |
| 67 | + uint8_t c2= in[i + 1]; |
| 68 | + uint8_t c3= in[i + 2]; |
| 69 | + uint8_t c4= in[i + 3]; |
| 70 | + |
| 71 | + bool c3_pad= (c3 == PAD); |
| 72 | + bool c4_pad= (c4 == PAD); |
| 73 | + |
| 74 | + if (c1 == PAD || c2 == PAD) { |
| 75 | + return base64_error (sc, "bytevector-base64-decode", "value-error", |
| 76 | + "bytevector-base64-decode: Invalid base64 input", arg); |
| 77 | + } |
| 78 | + |
| 79 | + uint8_t v1= decode_table[c1]; |
| 80 | + uint8_t v2= decode_table[c2]; |
| 81 | + uint8_t v3= c3_pad ? 0 : decode_table[c3]; |
| 82 | + uint8_t v4= c4_pad ? 0 : decode_table[c4]; |
| 83 | + |
| 84 | + if (v1 == 0xFF || v2 == 0xFF || (!c3_pad && v3 == 0xFF) || (!c4_pad && v4 == 0xFF) || (c3_pad && !c4_pad)) { |
| 85 | + return base64_error (sc, "bytevector-base64-decode", "value-error", |
| 86 | + "bytevector-base64-decode: Invalid base64 input", arg); |
| 87 | + } |
| 88 | + |
| 89 | + out_buf[out_len++]= (uint8_t) ((v1 << 2) | (v2 >> 4)); |
| 90 | + if (!c3_pad) { |
| 91 | + out_buf[out_len++]= (uint8_t) ((v2 << 4) | (v3 >> 2)); |
| 92 | + if (!c4_pad) { |
| 93 | + out_buf[out_len++]= (uint8_t) ((v3 << 6) | v4); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + if (out_len != out_cap) { |
| 99 | + s7_pointer final_out= s7_make_byte_vector (sc, out_len, 1, NULL); |
| 100 | + memcpy (s7_byte_vector_elements (final_out), out_buf, out_len); |
| 101 | + return final_out; |
| 102 | + } |
| 103 | + return out; |
| 104 | +} |
| 105 | + |
| 106 | +static s7_pointer |
| 107 | +f_bytevector_base64_encode (s7_scheme* sc, s7_pointer args) { |
| 108 | + s7_pointer arg= s7_car (args); |
| 109 | + |
| 110 | + if (!s7_is_byte_vector (arg)) { |
| 111 | + return base64_error (sc, "bytevector-base64-encode", "type-error", |
| 112 | + "bytevector-base64-encode: input must be bytevector", arg); |
| 113 | + } |
| 114 | + |
| 115 | + s7_int in_len= s7_integer (s7_cadr (args)); |
| 116 | + uint8_t* in = (uint8_t*) s7_byte_vector_elements (arg); |
| 117 | + |
| 118 | + static const uint8_t encode_table[64]= { |
| 119 | + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', |
| 120 | + 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', |
| 121 | + 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'}; |
| 122 | + |
| 123 | + const uint8_t PAD= (uint8_t) '='; |
| 124 | + |
| 125 | + s7_int out_len= (in_len == 0) ? 0 : 4 * ((in_len + 2) / 3); |
| 126 | + s7_pointer out = s7_make_byte_vector (sc, out_len, 1, NULL); |
| 127 | + uint8_t* out_buf= (uint8_t*) s7_byte_vector_elements (out); |
| 128 | + |
| 129 | + s7_int i= 0; |
| 130 | + s7_int j= 0; |
| 131 | + |
| 132 | + while (i + 2 < in_len) { |
| 133 | + uint32_t triple= ((uint32_t) in[i] << 16) | ((uint32_t) in[i + 1] << 8) | in[i + 2]; |
| 134 | + out_buf[j] = encode_table[(triple >> 18) & 0x3F]; |
| 135 | + out_buf[j + 1] = encode_table[(triple >> 12) & 0x3F]; |
| 136 | + out_buf[j + 2] = encode_table[(triple >> 6) & 0x3F]; |
| 137 | + out_buf[j + 3] = encode_table[triple & 0x3F]; |
| 138 | + i+= 3; |
| 139 | + j+= 4; |
| 140 | + } |
| 141 | + |
| 142 | + s7_int rem= in_len - i; |
| 143 | + if (rem == 1) { |
| 144 | + uint32_t triple= (uint32_t) in[i] << 16; |
| 145 | + out_buf[j] = encode_table[(triple >> 18) & 0x3F]; |
| 146 | + out_buf[j + 1] = encode_table[(triple >> 12) & 0x3F]; |
| 147 | + out_buf[j + 2] = PAD; |
| 148 | + out_buf[j + 3] = PAD; |
| 149 | + } |
| 150 | + else if (rem == 2) { |
| 151 | + uint32_t triple= ((uint32_t) in[i] << 16) | ((uint32_t) in[i + 1] << 8); |
| 152 | + out_buf[j] = encode_table[(triple >> 18) & 0x3F]; |
| 153 | + out_buf[j + 1] = encode_table[(triple >> 12) & 0x3F]; |
| 154 | + out_buf[j + 2] = encode_table[(triple >> 6) & 0x3F]; |
| 155 | + out_buf[j + 3] = PAD; |
| 156 | + } |
| 157 | + |
| 158 | + return out; |
| 159 | +} |
| 160 | + |
| 161 | +static void |
| 162 | +glue_bytevector_base64_decode (s7_scheme* sc) { |
| 163 | + const char* name= "g_bytevector-base64-decode"; |
| 164 | + const char* desc= "(g_bytevector-base64-decode bv len) => bytevector"; |
| 165 | + s7_define_function (sc, name, f_bytevector_base64_decode, 2, 0, false, desc); |
| 166 | +} |
| 167 | + |
| 168 | +static void |
| 169 | +glue_bytevector_base64_encode (s7_scheme* sc) { |
| 170 | + const char* name= "g_bytevector-base64-encode"; |
| 171 | + const char* desc= "(g_bytevector-base64-encode bv len) => bytevector"; |
| 172 | + s7_define_function (sc, name, f_bytevector_base64_encode, 2, 0, false, desc); |
| 173 | +} |
| 174 | + |
| 175 | +void |
| 176 | +glue_liii_base64 (s7_scheme* sc) { |
| 177 | + glue_bytevector_base64_decode (sc); |
| 178 | + glue_bytevector_base64_encode (sc); |
| 179 | +} |
| 180 | + |
| 181 | +} // namespace goldfish |
0 commit comments