|
1 | 1 | /*
|
2 |
| - * Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/> |
3 |
| - * (C) 2020 Vladimir Sadovnikov <[email protected]> |
| 2 | + * Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 | + * (C) 2025 Vladimir Sadovnikov <[email protected]> |
4 | 4 | *
|
5 | 5 | * This file is part of lsp-dsp-lib
|
6 | 6 | * Created on: 31 мар. 2020 г.
|
@@ -57,168 +57,168 @@ namespace lsp
|
57 | 57 | namespace generic
|
58 | 58 | {
|
59 | 59 | static const char *base64_table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
60 |
| - static const int8_t base64_lookup[] = { |
61 |
| - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
62 |
| - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
63 |
| - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0x3e, -1, -1, -1, 0x3f, |
64 |
| - 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, -1, -1, -1, -1, -1, -1, |
65 |
| - -1, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, |
66 |
| - 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, -1, -1, -1, -1, -1, |
67 |
| - -1, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, |
68 |
| - 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, -1, -1, -1, -1, -1, |
69 |
| - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
70 |
| - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
71 |
| - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
72 |
| - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
73 |
| - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
74 |
| - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
75 |
| - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
76 |
| - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
77 |
| - }; |
78 |
| - |
79 |
| - size_t base64_enc(void *dst, size_t *dst_left, const void *src, size_t *src_left) |
| 60 | + static const int8_t base64_lookup[] = { |
| 61 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 62 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 63 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0x3e, -1, -1, -1, 0x3f, |
| 64 | + 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, -1, -1, -1, -1, -1, -1, |
| 65 | + -1, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, |
| 66 | + 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, -1, -1, -1, -1, -1, |
| 67 | + -1, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, |
| 68 | + 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, -1, -1, -1, -1, -1, |
| 69 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 70 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 71 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 72 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 73 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 74 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 75 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 76 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 77 | + }; |
| 78 | + |
| 79 | + size_t base64_enc(void *dst, size_t *dst_left, const void *src, size_t *src_left) |
| 80 | + { |
| 81 | + uint8_t *d = reinterpret_cast<uint8_t *>(dst); |
| 82 | + const uint8_t *s = reinterpret_cast<const uint8_t *>(src); |
| 83 | + size_t dl = *dst_left, sl = *src_left, n = 0; |
| 84 | + |
| 85 | + // Main body |
| 86 | + while (sl >= 3) |
80 | 87 | {
|
81 |
| - uint8_t *d = reinterpret_cast<uint8_t *>(dst); |
82 |
| - const uint8_t *s = reinterpret_cast<const uint8_t *>(src); |
83 |
| - size_t dl = *dst_left, sl = *src_left, n = 0; |
| 88 | + // Enough space? |
| 89 | + if (dl < 4) |
| 90 | + break; |
| 91 | + |
| 92 | + // Perform encode |
| 93 | + uint32_t x = (s[0] << 16) | (s[1] << 8) | s[2]; |
| 94 | + d[0] = base64_table[x >> 18]; |
| 95 | + d[1] = base64_table[(x >> 12) & 0x3f]; |
| 96 | + d[2] = base64_table[(x >> 6) & 0x3f]; |
| 97 | + d[3] = base64_table[x & 0x3f]; |
| 98 | + |
| 99 | + n += 3; |
| 100 | + s += 3; |
| 101 | + d += 4; |
| 102 | + sl -= 3; |
| 103 | + dl -= 4; |
| 104 | + } |
84 | 105 |
|
85 |
| - // Main body |
86 |
| - while (sl >= 3) |
| 106 | + // Tail |
| 107 | + if (sl > 0) |
| 108 | + { |
| 109 | + if ((sl == 1) && (dl >= 2)) |
87 | 110 | {
|
88 |
| - // Enough space? |
89 |
| - if (dl < 4) |
90 |
| - break; |
91 |
| - |
92 |
| - // Perform encode |
93 |
| - uint32_t x = (s[0] << 16) | (s[1] << 8) | s[2]; |
94 |
| - d[0] = base64_table[x >> 18]; |
95 |
| - d[1] = base64_table[(x >> 12) & 0x3f]; |
96 |
| - d[2] = base64_table[(x >> 6) & 0x3f]; |
97 |
| - d[3] = base64_table[x & 0x3f]; |
98 |
| - |
99 |
| - n += 3; |
100 |
| - s += 3; |
101 |
| - d += 4; |
102 |
| - sl -= 3; |
103 |
| - dl -= 4; |
104 |
| - } |
| 111 | + uint32_t x = s[0]; |
| 112 | + d[0] = base64_table[x >> 2]; |
| 113 | + d[1] = base64_table[(x << 4) & 0x3f]; |
105 | 114 |
|
106 |
| - // Tail |
107 |
| - if (sl > 0) |
| 115 | + n += 1; |
| 116 | + sl -= 1; |
| 117 | + dl -= 2; |
| 118 | + } |
| 119 | + else if ((sl == 2) && (dl >= 3)) |
108 | 120 | {
|
109 |
| - if ((sl == 1) && (dl >= 2)) |
110 |
| - { |
111 |
| - uint32_t x = s[0]; |
112 |
| - d[0] = base64_table[x >> 2]; |
113 |
| - d[1] = base64_table[(x << 4) & 0x3f]; |
114 |
| - |
115 |
| - n += 1; |
116 |
| - sl -= 1; |
117 |
| - dl -= 2; |
118 |
| - } |
119 |
| - else if ((sl == 2) && (dl >= 3)) |
120 |
| - { |
121 |
| - uint32_t x = (s[0] << 8) | s[1]; |
122 |
| - d[0] = base64_table[x >> 10]; |
123 |
| - d[1] = base64_table[(x >> 4) & 0x3f]; |
124 |
| - d[2] = base64_table[(x << 2) & 0x3f]; |
125 |
| - |
126 |
| - n += 2; |
127 |
| - sl -= 2; |
128 |
| - dl -= 3; |
129 |
| - } |
| 121 | + uint32_t x = (s[0] << 8) | s[1]; |
| 122 | + d[0] = base64_table[x >> 10]; |
| 123 | + d[1] = base64_table[(x >> 4) & 0x3f]; |
| 124 | + d[2] = base64_table[(x << 2) & 0x3f]; |
| 125 | + |
| 126 | + n += 2; |
| 127 | + sl -= 2; |
| 128 | + dl -= 3; |
130 | 129 | }
|
| 130 | + } |
131 | 131 |
|
132 |
| - *dst_left = dl; |
133 |
| - *src_left = sl; |
| 132 | + *dst_left = dl; |
| 133 | + *src_left = sl; |
134 | 134 |
|
135 |
| - return n; |
136 |
| - } |
| 135 | + return n; |
| 136 | + } |
| 137 | + |
| 138 | + ssize_t base64_dec(void *dst, size_t *dst_left, const void *src, size_t *src_left) |
| 139 | + { |
| 140 | + uint8_t *d = reinterpret_cast<uint8_t *>(dst); |
| 141 | + const uint8_t *s = reinterpret_cast<const uint8_t *>(src); |
| 142 | + size_t dl = *dst_left, sl = *src_left, n = 0; |
| 143 | + int32_t x; |
137 | 144 |
|
138 |
| - ssize_t base64_dec(void *dst, size_t *dst_left, const void *src, size_t *src_left) |
| 145 | + // Main body |
| 146 | + while (sl >= 4) |
139 | 147 | {
|
140 |
| - uint8_t *d = reinterpret_cast<uint8_t *>(dst); |
141 |
| - const uint8_t *s = reinterpret_cast<const uint8_t *>(src); |
142 |
| - size_t dl = *dst_left, sl = *src_left, n = 0; |
143 |
| - int32_t x; |
| 148 | + // Enough space? |
| 149 | + if (dl < 3) |
| 150 | + break; |
144 | 151 |
|
145 |
| - // Main body |
146 |
| - while (sl >= 4) |
| 152 | + x = (base64_lookup[s[0]] << 18) | |
| 153 | + (base64_lookup[s[1]] << 12) | |
| 154 | + (base64_lookup[s[2]] << 6) | |
| 155 | + base64_lookup[s[3]]; |
| 156 | + |
| 157 | + if (x < 0) |
147 | 158 | {
|
148 |
| - // Enough space? |
149 |
| - if (dl < 3) |
150 |
| - break; |
| 159 | + if (n > 0) break; |
| 160 | + else return -1; |
| 161 | + } |
151 | 162 |
|
152 |
| - x = (base64_lookup[s[0]] << 18) | |
153 |
| - (base64_lookup[s[1]] << 12) | |
154 |
| - (base64_lookup[s[2]] << 6) | |
155 |
| - base64_lookup[s[3]]; |
| 163 | + d[0] = uint8_t(x >> 16); |
| 164 | + d[1] = uint8_t(x >> 8); |
| 165 | + d[2] = uint8_t(x); |
| 166 | + |
| 167 | + d += 3; |
| 168 | + s += 4; |
| 169 | + dl -= 3; |
| 170 | + sl -= 4; |
| 171 | + n += 3; |
| 172 | + } |
156 | 173 |
|
157 |
| - if (x < 0) |
| 174 | + // Tail |
| 175 | + if (sl > 0) |
| 176 | + { |
| 177 | + if ((sl == 3) && (dl >= 2)) |
| 178 | + { |
| 179 | + x = (base64_lookup[s[0]] << 12) | |
| 180 | + (base64_lookup[s[1]] << 6) | |
| 181 | + base64_lookup[s[2]]; |
| 182 | + |
| 183 | + if (x >= 0) |
158 | 184 | {
|
159 |
| - if (n > 0) break; |
160 |
| - else return -1; |
161 |
| - } |
| 185 | + d[0] = uint8_t(x >> 10); |
| 186 | + d[1] = uint8_t(x >> 2); |
162 | 187 |
|
163 |
| - d[0] = uint8_t(x >> 16); |
164 |
| - d[1] = uint8_t(x >> 8); |
165 |
| - d[2] = uint8_t(x); |
| 188 | + dl -= 2; |
| 189 | + sl -= 3; |
| 190 | + n += 2; |
166 | 191 |
|
167 |
| - d += 3; |
168 |
| - s += 4; |
169 |
| - dl -= 3; |
170 |
| - sl -= 4; |
171 |
| - n += 3; |
| 192 | + } |
| 193 | + else if (n <= 0) |
| 194 | + return -1; |
172 | 195 | }
|
173 |
| - |
174 |
| - // Tail |
175 |
| - if (sl > 0) |
| 196 | + else if ((sl == 2) && (dl >= 1)) |
176 | 197 | {
|
177 |
| - if ((sl == 3) && (dl >= 2)) |
178 |
| - { |
179 |
| - x = (base64_lookup[s[0]] << 12) | |
180 |
| - (base64_lookup[s[1]] << 6) | |
181 |
| - base64_lookup[s[2]]; |
182 |
| - |
183 |
| - if (x >= 0) |
184 |
| - { |
185 |
| - d[0] = uint8_t(x >> 10); |
186 |
| - d[1] = uint8_t(x >> 2); |
187 |
| - |
188 |
| - dl -= 2; |
189 |
| - sl -= 3; |
190 |
| - n += 2; |
191 |
| - |
192 |
| - } |
193 |
| - else if (n <= 0) |
194 |
| - return -1; |
195 |
| - } |
196 |
| - else if ((sl == 2) && (dl >= 1)) |
| 198 | + x = (base64_lookup[s[0]] << 6) | |
| 199 | + base64_lookup[s[1]]; |
| 200 | + |
| 201 | + if (x >= 0) |
197 | 202 | {
|
198 |
| - x = (base64_lookup[s[0]] << 6) | |
199 |
| - base64_lookup[s[1]]; |
200 |
| - |
201 |
| - if (x >= 0) |
202 |
| - { |
203 |
| - d[0] = uint8_t(x >> 4); |
204 |
| - |
205 |
| - dl -= 1; |
206 |
| - sl -= 2; |
207 |
| - n += 1; |
208 |
| - } |
209 |
| - else if (n <= 0) |
210 |
| - return -1; |
| 203 | + d[0] = uint8_t(x >> 4); |
| 204 | + |
| 205 | + dl -= 1; |
| 206 | + sl -= 2; |
| 207 | + n += 1; |
211 | 208 | }
|
212 |
| - else if ((sl == 1) && (n <= 0)) |
| 209 | + else if (n <= 0) |
213 | 210 | return -1;
|
214 | 211 | }
|
| 212 | + else if ((sl == 1) && (n <= 0)) |
| 213 | + return -1; |
| 214 | + } |
215 | 215 |
|
216 |
| - *dst_left = dl; |
217 |
| - *src_left = sl; |
| 216 | + *dst_left = dl; |
| 217 | + *src_left = sl; |
218 | 218 |
|
219 |
| - return n; |
220 |
| - } |
221 |
| - } |
222 |
| -} |
| 219 | + return n; |
| 220 | + } |
| 221 | + } /* namespace generic */ |
| 222 | +} /* namespace lsp */ |
223 | 223 |
|
224 | 224 | #endif /* PRIVATE_DSP_ARCH_GENERIC_CODING_H_ */
|
0 commit comments