diff options
Diffstat (limited to 'coreutils/uudecode.c')
-rw-r--r-- | coreutils/uudecode.c | 162 |
1 files changed, 140 insertions, 22 deletions
diff --git a/coreutils/uudecode.c b/coreutils/uudecode.c index 5b2edd649..164b208ea 100644 --- a/coreutils/uudecode.c +++ b/coreutils/uudecode.c | |||
@@ -168,9 +168,11 @@ int uudecode_main(int argc UNUSED_PARAM, char **argv) | |||
168 | } | 168 | } |
169 | #endif | 169 | #endif |
170 | 170 | ||
171 | //applet:IF_BASE64(APPLET(base64, BB_DIR_BIN, BB_SUID_DROP)) | 171 | //config:config BASE32 |
172 | 172 | //config: bool "base32 (4.9 kb)" | |
173 | //kbuild:lib-$(CONFIG_BASE64) += uudecode.o | 173 | //config: default y |
174 | //config: help | ||
175 | //config: Base32 encode and decode | ||
174 | 176 | ||
175 | //config:config BASE64 | 177 | //config:config BASE64 |
176 | //config: bool "base64 (4.9 kb)" | 178 | //config: bool "base64 (4.9 kb)" |
@@ -178,49 +180,165 @@ int uudecode_main(int argc UNUSED_PARAM, char **argv) | |||
178 | //config: help | 180 | //config: help |
179 | //config: Base64 encode and decode | 181 | //config: Base64 encode and decode |
180 | 182 | ||
183 | //usage:#define base32_trivial_usage | ||
184 | //usage: "[-d] [-w COL] [FILE]" | ||
185 | //usage:#define base32_full_usage "\n\n" | ||
186 | //usage: "Base32 encode or decode FILE to standard output" | ||
187 | //usage: "\n -d Decode data" | ||
188 | //usage: "\n -w COL Wrap lines at COL (default 76, 0 disables)" | ||
189 | ////usage: "\n -i When decoding, ignore non-alphabet characters" | ||
190 | |||
181 | //usage:#define base64_trivial_usage | 191 | //usage:#define base64_trivial_usage |
182 | //usage: "[-d] [FILE]" | 192 | //usage: "[-d] [-w COL] [FILE]" |
183 | //usage:#define base64_full_usage "\n\n" | 193 | //usage:#define base64_full_usage "\n\n" |
184 | //usage: "Base64 encode or decode FILE to standard output" | 194 | //usage: "Base64 encode or decode FILE to standard output" |
185 | //usage: "\n -d Decode data" | 195 | //usage: "\n -d Decode data" |
186 | ////usage: "\n -w COL Wrap lines at COL (default 76, 0 disables)" | 196 | //usage: "\n -w COL Wrap lines at COL (default 76, 0 disables)" |
187 | ////usage: "\n -i When decoding, ignore non-alphabet characters" | 197 | ////usage: "\n -i When decoding, ignore non-alphabet characters" |
188 | 198 | ||
189 | #if ENABLE_BASE64 | 199 | // APPLET_ODDNAME:name main location suid_type help |
190 | int base64_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 200 | //applet:IF_BASE32(APPLET_ODDNAME(base32, baseNUM, BB_DIR_BIN, BB_SUID_DROP, base32)) |
191 | int base64_main(int argc UNUSED_PARAM, char **argv) | 201 | //applet:IF_BASE64(APPLET_ODDNAME(base64, baseNUM, BB_DIR_BIN, BB_SUID_DROP, base64)) |
202 | |||
203 | //kbuild:lib-$(CONFIG_BASE64) += uudecode.o | ||
204 | //kbuild:lib-$(CONFIG_BASE32) += uudecode.o | ||
205 | |||
206 | #if ENABLE_BASE32 || ENABLE_BASE64 | ||
207 | |||
208 | # if ENABLE_BASE32 | ||
209 | static void bb_b32encode(char *p, const void *src, int length) | ||
210 | { | ||
211 | #define tbl bb_uuenc_tbl_base32 | ||
212 | const unsigned char *s = src; | ||
213 | |||
214 | /* Transform 5x8 bits to 8x5 bits */ | ||
215 | while (length > 0) { | ||
216 | unsigned cur, next; | ||
217 | |||
218 | length--; | ||
219 | cur = *s++; | ||
220 | *p++ = tbl[cur >> 3]; // xxxxx--- -------- -------- -------- -------- | ||
221 | cur &= 7; | ||
222 | |||
223 | next = 0; | ||
224 | if (--length >= 0) | ||
225 | next = *s++; | ||
226 | *p++ = tbl[(cur << 2) + (next >> 6)]; // -----xxx xx------ -------- -------- -------- | ||
227 | cur = next & 0x3f; | ||
228 | |||
229 | *p++ = tbl[cur >> 1]; // -------- --xxxxx- -------- -------- -------- | ||
230 | cur &= 1; | ||
231 | |||
232 | next = 0; | ||
233 | if (--length >= 0) | ||
234 | next = *s++; | ||
235 | *p++ = tbl[(cur << 4) + (next >> 4)]; // -------- -------x xxxx---- -------- -------- | ||
236 | cur = next & 0xf; | ||
237 | |||
238 | next = 0; | ||
239 | if (--length >= 0) | ||
240 | next = *s++; | ||
241 | *p++ = tbl[(cur << 1) + (next >> 7)]; // -------- -------- ----xxxx x------- -------- | ||
242 | cur = next & 0x7f; | ||
243 | |||
244 | *p++ = tbl[cur >> 2]; // -------- -------- -------- -xxxxx-- -------- | ||
245 | cur &= 3; | ||
246 | |||
247 | next = 0; | ||
248 | if (--length >= 0) | ||
249 | next = *s++; | ||
250 | *p++ = tbl[(cur << 3) + (next >> 5)]; // -------- -------- -------- ------xx xxx----- | ||
251 | cur = next & 0x1f; | ||
252 | |||
253 | *p++ = tbl[cur]; // -------- -------- -------- -------- ---xxxxx | ||
254 | } | ||
255 | #undef tbl | ||
256 | /* Zero-terminate */ | ||
257 | *p = '\0'; | ||
258 | /* Pad as necessary */ | ||
259 | length = ((-length) * 3) >> 1; /* -4 => 6 pad chars, -3 => 4, -2 => 3, -1 => 1 */ | ||
260 | while (length--) { | ||
261 | *--p = '='; | ||
262 | } | ||
263 | } | ||
264 | # else | ||
265 | void bb_b32encode(char *p, const void *src, int length); /* undefined */ | ||
266 | # endif | ||
267 | |||
268 | int baseNUM_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | ||
269 | int baseNUM_main(int argc UNUSED_PARAM, char **argv) | ||
192 | { | 270 | { |
193 | FILE *src_stream; | 271 | FILE *src_stream; |
194 | unsigned opts; | 272 | unsigned opts; |
273 | unsigned col = 76; | ||
195 | 274 | ||
196 | opts = getopt32(argv, "^" "d" "\0" "?1"/* 1 arg max*/); | 275 | opts = getopt32(argv, "^" "dw:+" "\0" "?1"/* 1 arg max*/, &col); |
197 | argv += optind; | 276 | argv += optind; |
198 | 277 | ||
199 | if (!argv[0]) | 278 | if (!argv[0]) |
200 | *--argv = (char*)"-"; | 279 | *--argv = (char*)"-"; |
201 | src_stream = xfopen_stdin(argv[0]); | 280 | src_stream = xfopen_stdin(argv[0]); |
202 | if (opts) { | 281 | if (opts & 1) { |
203 | read_base64(src_stream, stdout, /*flags:*/ (unsigned char)EOF); | 282 | /* -d: decode */ |
283 | int flags = (unsigned char)EOF; | ||
284 | if (ENABLE_BASE32 && (!ENABLE_BASE64 || applet_name[4] == '3')) | ||
285 | flags = ((unsigned char)EOF) | BASE64_32; | ||
286 | read_base64(src_stream, stdout, flags); | ||
204 | } else { | 287 | } else { |
205 | enum { | 288 | enum { |
206 | SRC_BUF_SIZE = 76 / 4 * 3, /* this *MUST* be a multiple of 3 */ | 289 | SRC_BUF_SIZE = 3 * 5 * 32, /* this *MUST* be a multiple of 3 and 5 */ |
207 | DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3), | 290 | DST_BUF_SIZE = 8 * ((SRC_BUF_SIZE + 4) / 5), /* max growth on encode (base32 case) */ |
208 | }; | 291 | }; |
209 | char src_buf[SRC_BUF_SIZE]; | 292 | /* Use one buffer for both input and output: |
210 | char dst_buf[DST_BUF_SIZE + 1]; | 293 | * encoding reads input "left-to-right", |
211 | int src_fd = fileno(src_stream); | 294 | * it's safe to place source at the end of the buffer and |
295 | * overwrite it while encoding, just be careful to have a gap. | ||
296 | */ | ||
297 | char dst_buf[((DST_BUF_SIZE + /*gap:*/ 16) /*round up to 16:*/ | 0xf) + 1]; | ||
298 | #define src_buf (dst_buf + sizeof(dst_buf) - SRC_BUF_SIZE) | ||
299 | int src_fd, rem; | ||
300 | |||
301 | src_fd = fileno(src_stream); | ||
302 | rem = 0; | ||
212 | while (1) { | 303 | while (1) { |
213 | size_t size = full_read(src_fd, src_buf, SRC_BUF_SIZE); | 304 | size_t size = full_read(src_fd, src_buf, SRC_BUF_SIZE); |
214 | if (!size) | ||
215 | break; | ||
216 | if ((ssize_t)size < 0) | 305 | if ((ssize_t)size < 0) |
217 | bb_simple_perror_msg_and_die(bb_msg_read_error); | 306 | bb_simple_perror_msg_and_die(bb_msg_read_error); |
307 | if (size == 0) { | ||
308 | if (rem != 0) bb_putchar('\n'); | ||
309 | break; | ||
310 | } | ||
311 | |||
218 | /* Encode the buffer we just read in */ | 312 | /* Encode the buffer we just read in */ |
219 | bb_uuencode(dst_buf, src_buf, size, bb_uuenc_tbl_base64); | 313 | if (ENABLE_BASE32 && (!ENABLE_BASE64 || applet_name[4] == '3')) { |
220 | xwrite(STDOUT_FILENO, dst_buf, 4 * ((size + 2) / 3)); | 314 | bb_b32encode(dst_buf, src_buf, size); |
221 | bb_putchar('\n'); | 315 | size = 8 * ((size + 4) / 5); |
222 | fflush(stdout); | 316 | } else { |
317 | bb_uuencode(dst_buf, src_buf, size, bb_uuenc_tbl_base64); | ||
318 | size = 4 * ((size + 2) / 3); | ||
319 | } | ||
320 | |||
321 | if (col == 0) { | ||
322 | fputs(dst_buf, stdout); | ||
323 | } else { | ||
324 | char *result = dst_buf; | ||
325 | if (rem == 0) | ||
326 | rem = col; | ||
327 | while (1) { | ||
328 | int out = size < rem ? size : rem; | ||
329 | rem -= out; | ||
330 | printf(rem != 0 ? "%.*s" : "%.*s\n", out, result); | ||
331 | if (rem != 0) | ||
332 | break; | ||
333 | size -= out; | ||
334 | if (size == 0) | ||
335 | break; | ||
336 | result += out; | ||
337 | rem = col; | ||
338 | } | ||
339 | } | ||
223 | } | 340 | } |
341 | #undef src_buf | ||
224 | } | 342 | } |
225 | 343 | ||
226 | fflush_stdout_and_exit(EXIT_SUCCESS); | 344 | fflush_stdout_and_exit(EXIT_SUCCESS); |