logo

utils-std

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

base64.c (6934B)


  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 <assert.h> /* assert */
  7. #include <ctype.h> /* isspace */
  8. #include <errno.h> /* errno */
  9. #include <stdint.h> /* uint8_t */
  10. #include <stdio.h> /* fopen(), fprintf(), BUFSIZ */
  11. #include <stdlib.h> /* abort */
  12. #include <string.h> /* strerror(), strncmp() */
  13. #include <unistd.h> /* read(), write(), close(), getopt(), opt* */
  14. // 64(26+26+10+2) + NULL
  15. static const char *b64_encmap = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  16. static size_t c_out = 0;
  17. // 76 is lowest of all base64 related RFCs
  18. static long wrap_nl = 76;
  19. const char *argv0 = "base64";
  20. static int
  21. xputc(int c, FILE *stream)
  22. {
  23. if(fputc(c, stream) == EOF)
  24. {
  25. fprintf(stderr, "%s: error: Failed writing: %s\n", argv0, strerror(errno));
  26. errno = 0;
  27. return 1;
  28. }
  29. int err = ferror(stream);
  30. if(err != 0)
  31. {
  32. fprintf(stderr, "%s: error: Failed writing: %s\n", argv0, strerror(err));
  33. errno = 0;
  34. return 1;
  35. }
  36. return 0;
  37. }
  38. static inline uint8_t
  39. b64_get(uint8_t pos)
  40. {
  41. assert(pos <= 64);
  42. return b64_encmap[pos];
  43. }
  44. static void
  45. b64encode(uint8_t out[4], uint8_t in[3])
  46. {
  47. out[0] = b64_get(in[0] >> 2);
  48. out[1] = b64_get((uint8_t)(((in[0] & 0x03) << 4) | (in[1] >> 4)));
  49. out[2] = b64_get((uint8_t)(((in[1] & 0x0f) << 2) | (in[2] >> 6)));
  50. out[3] = b64_get(in[2] & 0x3f);
  51. }
  52. static int
  53. encode(FILE *fin, const char *name)
  54. {
  55. while(1)
  56. {
  57. uint8_t obuf[4] = "----";
  58. uint8_t ibuf[3] = {0, 0, 0};
  59. uint8_t pad = 0;
  60. size_t c = 0;
  61. for(; c < 3; c++)
  62. {
  63. int buf = getc(fin);
  64. if(buf == EOF)
  65. {
  66. break;
  67. }
  68. ibuf[c] = buf;
  69. }
  70. if(c == 0)
  71. {
  72. return 0;
  73. };
  74. if(ferror(fin))
  75. {
  76. fprintf(
  77. stderr, "%s: error: Failed reading from file '%s': %s\n", argv0, name, strerror(errno));
  78. errno = 0;
  79. return 1;
  80. }
  81. for(; c < 3; pad++, c++)
  82. {
  83. ibuf[c] = 0;
  84. }
  85. assert(c == 3);
  86. assert(pad <= 3);
  87. b64encode(obuf, (uint8_t *)ibuf);
  88. for(; pad > 0; pad--)
  89. {
  90. obuf[4 - pad] = '=';
  91. }
  92. if(fwrite((char *)obuf, 4, 1, stdout) <= 0)
  93. {
  94. fprintf(stderr, "%s: error: Failed writing: %s\n", argv0, strerror(errno));
  95. errno = 0;
  96. return 1;
  97. }
  98. c_out += 4;
  99. if(wrap_nl != 0 && (c_out + 4) > wrap_nl)
  100. {
  101. c_out = 0;
  102. if(xputc('\n', stdout) != 0) return 1;
  103. }
  104. if(feof(fin))
  105. {
  106. if(fflush(stdout))
  107. {
  108. fprintf(stderr, "%s: error: Failed writing: %s\n", argv0, strerror(errno));
  109. errno = 0;
  110. return 1;
  111. }
  112. int err = ferror(stdout);
  113. if(err != 0)
  114. {
  115. fprintf(stderr, "%s: error: Failed writing: %s\n", argv0, strerror(errno));
  116. return 1;
  117. }
  118. return 0;
  119. }
  120. }
  121. abort(); // unreachable
  122. }
  123. // This function is based on NetBSD's code, which contains the following notices:
  124. // Copyright (c) 2018 The NetBSD Foundation, Inc.
  125. //
  126. // This code is derived from software contributed to The NetBSD Foundation
  127. // by Christos Zoulas.
  128. static int
  129. decode(FILE *fin, const char *name)
  130. {
  131. int c = 0;
  132. uint8_t out = 0;
  133. int state = 0;
  134. while((c = getc(fin)) != EOF)
  135. {
  136. if(isspace(c)) continue;
  137. if(c == '=') break;
  138. uint8_t b = 65;
  139. for(uint8_t i = 0; i < 64; i++)
  140. {
  141. if(b64_encmap[i] == c)
  142. {
  143. b = i;
  144. break;
  145. }
  146. }
  147. if(b > 64)
  148. {
  149. fprintf(stderr, "%s: error: Invalid character '%c'\n", argv0, c);
  150. return 1;
  151. }
  152. //fprintf(stderr, "state: %d | c: %c (%d) | b: %d\n", state, c, c, b);
  153. switch(state)
  154. {
  155. case 0:
  156. out = (uint8_t)(b << 2);
  157. break;
  158. case 1:
  159. out |= b >> 4;
  160. if(xputc(out, stdout) != 0) return 1;
  161. out = (uint8_t)((b & 0xf) << 4);
  162. break;
  163. case 2:
  164. out |= b >> 2;
  165. if(xputc(out, stdout) != 0) return 1;
  166. out = (uint8_t)((b & 0x3) << 6);
  167. break;
  168. case 3:
  169. out |= b;
  170. if(xputc(out, stdout) != 0) return 1;
  171. out = 0;
  172. break;
  173. default:
  174. abort();
  175. }
  176. state = (state + 1) & 3;
  177. }
  178. //fprintf(stderr, "[outside] state: %d | c: %c (%d) | out: %d\n", state, c, c, out);
  179. if(c == '=')
  180. {
  181. switch(state)
  182. {
  183. case 0:
  184. case 1:
  185. fprintf(stderr, "%s: error: Invalid character '%c' (early '=')\n", argv0, c);
  186. return 1;
  187. case 2:
  188. while(isspace(c = getc(fin)))
  189. ;
  190. if(c != '=')
  191. {
  192. fprintf(stderr, "%s: error: Invalid character '%c' (not '=')\n", argv0, c);
  193. return 1;
  194. }
  195. /* fallthrough */
  196. case 3:
  197. while(isspace(c = getc(fin)))
  198. ;
  199. if(c != EOF)
  200. {
  201. fprintf(stderr, "%s: error: Invalid character '%c' (not EOF)\n", argv0, c);
  202. return 1;
  203. }
  204. return 0;
  205. default:
  206. abort();
  207. }
  208. }
  209. assert(c == EOF || state == 0);
  210. if(fflush(stdout))
  211. {
  212. fprintf(stderr, "%s: error: Failed writing: %s\n", argv0, strerror(errno));
  213. errno = 0;
  214. return 1;
  215. }
  216. int err = ferror(stdout);
  217. if(err != 0)
  218. {
  219. fprintf(stderr, "%s: error: Failed writing: %s\n", argv0, strerror(err));
  220. errno = 0;
  221. return 1;
  222. }
  223. return 0;
  224. }
  225. int
  226. main(int argc, char *argv[])
  227. {
  228. int (*process)(FILE *, const char *) = &encode;
  229. int c = 0, ret = 0;
  230. while((c = getopt(argc, argv, ":dw:")) != -1)
  231. {
  232. switch(c)
  233. {
  234. case 'd':
  235. process = &decode;
  236. break;
  237. case 'w':
  238. errno = 0;
  239. char *e = NULL;
  240. wrap_nl = strtol(optarg, &e, 10);
  241. // extraneous characters is invalid
  242. if(e && *e != 0) errno = EINVAL;
  243. if(errno != 0)
  244. {
  245. fprintf(stderr, "%s: error: Option '-w %s': %s\n", argv0, optarg, strerror(errno));
  246. return 1;
  247. }
  248. break;
  249. case ':':
  250. fprintf(stderr, "%s: error: Missing operand for option '-%c'\n", argv0, optopt);
  251. return 1;
  252. case '?':
  253. fprintf(stderr, "%s: error: Unrecognised option '-%c'\n", argv0, optopt);
  254. return 1;
  255. }
  256. }
  257. argc -= optind;
  258. argv += optind;
  259. if(argc <= 0)
  260. {
  261. ret = process(stdin, "<stdin>");
  262. goto end;
  263. }
  264. for(int argi = 0; argi < argc; argi++)
  265. {
  266. if(strncmp(argv[argi], "-", 2) == 0)
  267. {
  268. if(process(stdin, "<stdin>") != 0)
  269. {
  270. ret = 1;
  271. goto end;
  272. }
  273. }
  274. else if(strncmp(argv[argi], "--", 3) == 0)
  275. {
  276. continue;
  277. }
  278. else
  279. {
  280. FILE *fin = fopen(argv[argi], "r");
  281. if(fin == NULL || ferror(fin) != 0)
  282. {
  283. fprintf(stderr,
  284. "%s: error: Failed opening file '%s': %s\n",
  285. argv0,
  286. argv[argi],
  287. strerror(errno));
  288. ret = 1;
  289. goto end;
  290. }
  291. if(process(fin, argv[argi]) != 0) ret = 1;
  292. if(fclose(fin) < 0)
  293. {
  294. fprintf(stderr,
  295. "%s: error: Failed closing file '%s': %s\n",
  296. argv0,
  297. argv[argi],
  298. strerror(errno));
  299. ret = 1;
  300. goto end;
  301. }
  302. }
  303. }
  304. end:
  305. if(wrap_nl != 0 && c_out > 0)
  306. {
  307. printf("\n");
  308. }
  309. if(fclose(stdin) != 0)
  310. {
  311. fprintf(stderr, "%s: error: Failed closing file <stdin>: %s\n", argv0, strerror(errno));
  312. ret = 1;
  313. }
  314. if(fclose(stdout) != 0)
  315. {
  316. fprintf(stderr, "%s: error: Failed closing file <stdout>: %s\n", argv0, strerror(errno));
  317. ret = 1;
  318. }
  319. return ret;
  320. }