logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git/

base64.c (11432B)


  1. // utils-std: Collection of commonly available Unix tools
  2. // SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  3. // SPDX-FileCopyrightText: 2018 The NetBSD Foundation, Inc.
  4. // SPDX-License-Identifier: MPL-2.0 AND BSD-2-Clause
  5. #define _POSIX_C_SOURCE 200809L
  6. #include "../lib/getopt_nolong.h"
  7. #include <assert.h> /* assert */
  8. #include <ctype.h> /* isspace */
  9. #include <errno.h> /* errno */
  10. #include <stdint.h> /* uint8_t */
  11. #include <stdio.h> /* fopen(), fprintf() */
  12. #include <stdlib.h> /* abort */
  13. #include <string.h> /* strerror(), strncmp() */
  14. #include <sys/stat.h> /* fstat */
  15. #include <unistd.h> /* read(), write(), close(), getopt() */
  16. // 64(26+26+10+2) + NULL
  17. static const char *b64_encmap = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  18. static size_t c_out = 0;
  19. // 76 is lowest of all base64 related RFCs
  20. static long wrap_nl = 76;
  21. const char *argv0 = "base64";
  22. static int
  23. xputc(int c, FILE *stream)
  24. {
  25. if(fputc(c, stream) == EOF)
  26. {
  27. fprintf(stderr, "%s: error: Failed writing: %s\n", argv0, strerror(errno));
  28. errno = 0;
  29. return 1;
  30. }
  31. int err = ferror(stream);
  32. if(err != 0)
  33. {
  34. fprintf(stderr, "%s: error: Failed writing: %s\n", argv0, strerror(err));
  35. errno = 0;
  36. return 1;
  37. }
  38. return 0;
  39. }
  40. static inline uint8_t
  41. b64_get(uint8_t pos)
  42. {
  43. assert(pos <= 64);
  44. return b64_encmap[pos];
  45. }
  46. static void
  47. b64encode(uint8_t out[4], uint8_t in[3])
  48. {
  49. out[0] = b64_get(in[0] >> 2);
  50. out[1] = b64_get((uint8_t)(((in[0] & 0x03) << 4) | (in[1] >> 4)));
  51. out[2] = b64_get((uint8_t)(((in[1] & 0x0f) << 2) | (in[2] >> 6)));
  52. out[3] = b64_get(in[2] & 0x3f);
  53. }
  54. static int
  55. base64_encode(FILE *fin, const char *iname)
  56. {
  57. while(1)
  58. {
  59. uint8_t obuf[4] = "----";
  60. uint8_t ibuf[3] = {0, 0, 0};
  61. uint8_t pad = 0;
  62. size_t c = 0;
  63. for(; c < 3; c++)
  64. {
  65. int buf = getc(fin);
  66. if(buf == EOF)
  67. {
  68. break;
  69. }
  70. ibuf[c] = buf;
  71. }
  72. if(c == 0)
  73. {
  74. return 0;
  75. };
  76. if(ferror(fin))
  77. {
  78. fprintf(
  79. stderr, "%s: error: Failed reading from file '%s': %s\n", argv0, iname, strerror(errno));
  80. errno = 0;
  81. return 1;
  82. }
  83. for(; c < 3; pad++, c++)
  84. {
  85. ibuf[c] = 0;
  86. }
  87. assert(c == 3);
  88. assert(pad <= 3);
  89. b64encode(obuf, (uint8_t *)ibuf);
  90. for(; pad > 0; pad--)
  91. {
  92. obuf[4 - pad] = '=';
  93. }
  94. if(fwrite((char *)obuf, 4, 1, stdout) <= 0)
  95. {
  96. fprintf(stderr, "%s: error: Failed writing: %s\n", argv0, strerror(errno));
  97. errno = 0;
  98. return 1;
  99. }
  100. c_out += 4;
  101. if(wrap_nl != 0 && (c_out + 4) > wrap_nl)
  102. {
  103. c_out = 0;
  104. if(xputc('\n', stdout) != 0) return 1;
  105. }
  106. if(feof(fin))
  107. {
  108. if(fflush(stdout))
  109. {
  110. fprintf(stderr, "%s: error: Failed writing: %s\n", argv0, strerror(errno));
  111. errno = 0;
  112. return 1;
  113. }
  114. int err = ferror(stdout);
  115. if(err != 0)
  116. {
  117. fprintf(stderr, "%s: error: Failed writing: %s\n", argv0, strerror(errno));
  118. return 1;
  119. }
  120. return 0;
  121. }
  122. }
  123. abort(); // unreachable
  124. }
  125. // This function is based on NetBSD's code, which contains the following notices:
  126. // Copyright (c) 2018 The NetBSD Foundation, Inc.
  127. //
  128. // This code is derived from software contributed to The NetBSD Foundation
  129. // by Christos Zoulas.
  130. static int
  131. base64_decode(FILE *fin, const char *iname)
  132. {
  133. int c = 0;
  134. uint8_t out = 0;
  135. int state = 0;
  136. while((c = getc(fin)) != EOF)
  137. {
  138. if(isspace(c)) continue;
  139. if(c == '=') break;
  140. uint8_t b = 65;
  141. for(uint8_t i = 0; i < 64; i++)
  142. {
  143. if(b64_encmap[i] == c)
  144. {
  145. b = i;
  146. break;
  147. }
  148. }
  149. if(b > 64)
  150. {
  151. fprintf(stderr, "%s: error: Invalid character '%c'\n", argv0, c);
  152. return 1;
  153. }
  154. //fprintf(stderr, "state: %d | c: %c (%d) | b: %d\n", state, c, c, b);
  155. switch(state)
  156. {
  157. case 0:
  158. out = (uint8_t)(b << 2);
  159. break;
  160. case 1:
  161. out |= b >> 4;
  162. if(xputc(out, stdout) != 0) return 1;
  163. out = (uint8_t)((b & 0xf) << 4);
  164. break;
  165. case 2:
  166. out |= b >> 2;
  167. if(xputc(out, stdout) != 0) return 1;
  168. out = (uint8_t)((b & 0x3) << 6);
  169. break;
  170. case 3:
  171. out |= b;
  172. if(xputc(out, stdout) != 0) return 1;
  173. out = 0;
  174. break;
  175. default:
  176. abort();
  177. }
  178. state = (state + 1) & 3;
  179. }
  180. //fprintf(stderr, "[outside] state: %d | c: %c (%d) | out: %d\n", state, c, c, out);
  181. if(c == '=')
  182. {
  183. switch(state)
  184. {
  185. case 0:
  186. case 1:
  187. fprintf(stderr, "%s: error: Invalid character '%c' (early '=')\n", argv0, c);
  188. return 1;
  189. case 2:
  190. while(isspace(c = getc(fin)))
  191. ;
  192. if(c != '=')
  193. {
  194. fprintf(stderr, "%s: error: Invalid character '%c' (not '=')\n", argv0, c);
  195. return 1;
  196. }
  197. /* fallthrough */
  198. case 3:
  199. while(isspace(c = getc(fin)))
  200. ;
  201. if(c != EOF)
  202. {
  203. fprintf(stderr, "%s: error: Invalid character '%c' (not EOF)\n", argv0, c);
  204. return 1;
  205. }
  206. return 0;
  207. default:
  208. abort();
  209. }
  210. }
  211. assert(c == EOF || state == 0);
  212. if(fflush(stdout))
  213. {
  214. fprintf(stderr, "%s: error: Failed writing: %s\n", argv0, strerror(errno));
  215. errno = 0;
  216. return 1;
  217. }
  218. int err = ferror(stdout);
  219. if(err != 0)
  220. {
  221. fprintf(stderr, "%s: error: Failed writing: %s\n", argv0, strerror(err));
  222. errno = 0;
  223. return 1;
  224. }
  225. return 0;
  226. }
  227. static int
  228. base64_main(int argc, char *argv[])
  229. {
  230. argv0 = "base64";
  231. int (*process)(FILE *, const char *) = &base64_encode;
  232. int ret = 0;
  233. for(int c = -1; (c = getopt_nolong(argc, argv, ":dw:")) != -1;)
  234. {
  235. switch(c)
  236. {
  237. case 'd':
  238. process = &base64_decode;
  239. break;
  240. case 'w':
  241. errno = 0;
  242. char *e = NULL;
  243. wrap_nl = strtol(optarg, &e, 10);
  244. // extraneous characters is invalid
  245. if(e && *e != 0) errno = EINVAL;
  246. if(errno != 0)
  247. {
  248. fprintf(stderr, "%s: error: Option '-w %s': %s\n", argv0, optarg, strerror(errno));
  249. return 1;
  250. }
  251. break;
  252. case ':':
  253. fprintf(stderr, "%s: error: Missing operand for option '-%c'\n", argv0, optopt);
  254. return 1;
  255. case '?':
  256. GETOPT_UNKNOWN_OPT
  257. return 1;
  258. }
  259. }
  260. argc -= optind;
  261. argv += optind;
  262. if(argc <= 0)
  263. {
  264. ret = process(stdin, "<stdin>");
  265. goto end;
  266. }
  267. for(int argi = 0; argi < argc; argi++)
  268. {
  269. if(strncmp(argv[argi], "-", 2) == 0)
  270. {
  271. if(process(stdin, "<stdin>") != 0)
  272. {
  273. ret = 1;
  274. goto end;
  275. }
  276. }
  277. else if(strncmp(argv[argi], "--", 3) == 0)
  278. {
  279. continue;
  280. }
  281. else
  282. {
  283. FILE *fin = fopen(argv[argi], "r");
  284. if(fin == NULL || ferror(fin) != 0)
  285. {
  286. fprintf(stderr,
  287. "%s: error: Failed opening file '%s': %s\n",
  288. argv0,
  289. argv[argi],
  290. strerror(errno));
  291. ret = 1;
  292. goto end;
  293. }
  294. if(process(fin, argv[argi]) != 0) ret = 1;
  295. if(fclose(fin) < 0)
  296. {
  297. fprintf(stderr,
  298. "%s: error: Failed closing file '%s': %s\n",
  299. argv0,
  300. argv[argi],
  301. strerror(errno));
  302. ret = 1;
  303. goto end;
  304. }
  305. }
  306. }
  307. end:
  308. if(wrap_nl != 0 && c_out > 0)
  309. {
  310. printf("\n");
  311. }
  312. if(fclose(stdin) != 0)
  313. {
  314. fprintf(stderr, "%s: error: Failed closing file <stdin>: %s\n", argv0, strerror(errno));
  315. ret = 1;
  316. }
  317. if(fclose(stdout) != 0)
  318. {
  319. fprintf(stderr, "%s: error: Failed closing file <stdout>: %s\n", argv0, strerror(errno));
  320. ret = 1;
  321. }
  322. return ret;
  323. }
  324. static int
  325. uuencode(FILE *fin, const char *iname)
  326. {
  327. bool fin_eof = false;
  328. while(!fin_eof)
  329. {
  330. char output[60] = "";
  331. size_t pos = 0;
  332. int len = 0;
  333. for(; (len < 45) && !fin_eof; pos += 4, len += 3)
  334. {
  335. uint8_t ibuf[3] = {0, 0, 0};
  336. size_t c = 0;
  337. for(; c < 3; c++)
  338. {
  339. int buf = getc(fin);
  340. if(buf == EOF)
  341. {
  342. fin_eof = true;
  343. break;
  344. }
  345. ibuf[c] = buf;
  346. }
  347. if(c == 0)
  348. {
  349. break;
  350. }
  351. if(ferror(fin))
  352. {
  353. fprintf(stderr,
  354. "%s: error: Failed reading from file '%s': %s\n",
  355. argv0,
  356. iname,
  357. strerror(errno));
  358. errno = 0;
  359. return 1;
  360. }
  361. assert((3 + pos) < 60);
  362. /* conversion math taken from POSIX.1-2024 specification */
  363. /* clang-format off */
  364. output[0+pos] = 0x20 + (( ibuf[0] >> 2 ) & 0x3F);
  365. output[1+pos] = 0x20 + (((ibuf[0] << 4) | ((ibuf[1] >> 4) & 0x0F)) & 0x3F);
  366. output[2+pos] = 0x20 + (((ibuf[1] << 2) | ((ibuf[2] >> 6) & 0x03)) & 0x3F);
  367. output[3+pos] = 0x20 + (( ibuf[2] ) & 0x3F);
  368. /* clang-format on */
  369. for(int i = 0; i < 4; i++)
  370. if(output[i + pos] == 0x20) output[i + pos] = 0x60;
  371. }
  372. if(printf("%c", len == 0 ? 0x60 : 0x20 + len) < 0)
  373. {
  374. fprintf(stderr, "%s: error: Failed writing length: %s\n", argv0, strerror(errno));
  375. errno = 0;
  376. return 1;
  377. }
  378. if(pos > 0)
  379. if(fwrite(output, pos, 1, stdout) <= 0)
  380. {
  381. fprintf(stderr, "%s: error: Failed writing data: %s\n", argv0, strerror(errno));
  382. errno = 0;
  383. return 1;
  384. }
  385. if(printf("\n") < 0)
  386. {
  387. fprintf(stderr, "%s: error: Failed writing newline: %s\n", argv0, strerror(errno));
  388. errno = 0;
  389. return 1;
  390. }
  391. }
  392. if(fflush(stdout) != 0)
  393. {
  394. fprintf(stderr, "%s: error: Failed writing (flush): %s\n", argv0, strerror(errno));
  395. errno = 0;
  396. return 1;
  397. }
  398. int err = ferror(stdout);
  399. if(err != 0)
  400. {
  401. fprintf(stderr, "%s: error: Failed writing (ferror): %s\n", argv0, strerror(errno));
  402. return 1;
  403. }
  404. return 0;
  405. }
  406. static int
  407. uuencode_main(int argc, char *argv[])
  408. {
  409. argv0 = "uuencode";
  410. const char *begin_str = "begin";
  411. const char *end_str = "end\n";
  412. int (*process)(FILE *, const char *) = &uuencode;
  413. // get old, then reset to old. Yay to POSIX nonsense
  414. mode_t mask = umask(0);
  415. umask(mask);
  416. // negate to get a normal bitmask
  417. mask ^= 0777;
  418. wrap_nl = 45;
  419. int ret = 0;
  420. for(int c = -1; (c = getopt_nolong(argc, argv, ":m")) != -1;)
  421. {
  422. switch(c)
  423. {
  424. case 'm':
  425. process = &base64_encode;
  426. begin_str = "begin-base64";
  427. end_str = "====\n";
  428. wrap_nl = 76;
  429. break;
  430. case ':':
  431. fprintf(stderr, "%s: error: Missing operand for option '-%c'\n", argv0, optopt);
  432. return 1;
  433. case '?':
  434. GETOPT_UNKNOWN_OPT
  435. return 1;
  436. }
  437. }
  438. argc -= optind;
  439. argv += optind;
  440. if(argc > 2)
  441. {
  442. fprintf(stderr, "%s: error: Expected 2 arguments or less, got %d\n", argv0, argc);
  443. return 1;
  444. }
  445. FILE *fin = stdin;
  446. const char *decode_path = "-";
  447. const char *iname = "<stdin>";
  448. mode_t mode = 0666 & mask;
  449. if(argc >= 1) decode_path = argv[argc - 1];
  450. if(argc == 2)
  451. {
  452. iname = argv[0];
  453. fin = fopen(iname, "rb");
  454. if(!fin)
  455. {
  456. fprintf(
  457. stderr, "%s: error: Failed opening input file '%s': %s\n", argv0, iname, strerror(errno));
  458. return 1;
  459. }
  460. struct stat fin_stat;
  461. if(fstat(fileno(fin), &fin_stat) != 0)
  462. {
  463. fprintf(stderr,
  464. "%s: warning: Failed getting status for file '%s': %s\n",
  465. argv0,
  466. iname,
  467. strerror(errno));
  468. ret = 1;
  469. }
  470. mode = fin_stat.st_mode & 0777;
  471. }
  472. printf("%s %03o %s\n", begin_str, mode, decode_path);
  473. if(process(fin, iname) != 0)
  474. {
  475. ret = 1;
  476. goto end;
  477. }
  478. end:
  479. if(ret == 0)
  480. {
  481. if(wrap_nl != 0 && c_out > 0) printf("\n");
  482. printf("%s", end_str);
  483. }
  484. if(fclose(fin) != 0)
  485. {
  486. fprintf(stderr, "%s: error: Failed closing file \"%s\": %s\n", argv0, iname, strerror(errno));
  487. ret = 1;
  488. }
  489. if(fclose(stdout) != 0)
  490. {
  491. fprintf(stderr, "%s: error: Failed closing file <stdout>: %s\n", argv0, strerror(errno));
  492. ret = 1;
  493. }
  494. return ret;
  495. }
  496. int
  497. main(int argc, char *argv[])
  498. {
  499. const char *arg0 = argv[0];
  500. const char *s = strrchr(arg0, '/');
  501. if(s) arg0 = s + 1;
  502. if(strcmp(arg0, "base64") == 0) return base64_main(argc, argv);
  503. if(strcmp(arg0, "uuencode") == 0) return uuencode_main(argc, argv);
  504. fprintf(stderr, "%s: error: Unknown utility '%s' expected 'base64' or 'uuencode'\n", arg0, arg0);
  505. return -1;
  506. }