uuencode.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Copyright 2003, Glenn McGrath
  4. * Copyright 2006, Rob Landley <rob@landley.net>
  5. * Copyright 2010, Denys Vlasenko
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. #include "libbb.h"
  10. /* Conversion table. for base 64 */
  11. const char bb_uuenc_tbl_base64[65 + 1] ALIGN1 = {
  12. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
  13. 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
  14. 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
  15. 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
  16. 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  17. 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
  18. 'w', 'x', 'y', 'z', '0', '1', '2', '3',
  19. '4', '5', '6', '7', '8', '9', '+', '/',
  20. '=' /* termination character */,
  21. '\0' /* needed for uudecode.c only */
  22. };
  23. const char bb_uuenc_tbl_std[65] ALIGN1 = {
  24. '`', '!', '"', '#', '$', '%', '&', '\'',
  25. '(', ')', '*', '+', ',', '-', '.', '/',
  26. '0', '1', '2', '3', '4', '5', '6', '7',
  27. '8', '9', ':', ';', '<', '=', '>', '?',
  28. '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
  29. 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
  30. 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
  31. 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
  32. '`' /* termination character */
  33. };
  34. /*
  35. * Encode bytes at S of length LENGTH to uuencode or base64 format and place it
  36. * to STORE. STORE will be 0-terminated, and must point to a writable
  37. * buffer of at least 1+BASE64_LENGTH(length) bytes.
  38. * where BASE64_LENGTH(len) = (4 * ((LENGTH + 2) / 3))
  39. */
  40. void FAST_FUNC bb_uuencode(char *p, const void *src, int length, const char *tbl)
  41. {
  42. const unsigned char *s = src;
  43. /* Transform the 3x8 bits to 4x6 bits */
  44. while (length > 0) {
  45. unsigned s1, s2;
  46. /* Are s[1], s[2] valid or should be assumed 0? */
  47. s1 = s2 = 0;
  48. length -= 3; /* can be >=0, -1, -2 */
  49. if (length >= -1) {
  50. s1 = s[1];
  51. if (length >= 0)
  52. s2 = s[2];
  53. }
  54. *p++ = tbl[s[0] >> 2];
  55. *p++ = tbl[((s[0] & 3) << 4) + (s1 >> 4)];
  56. *p++ = tbl[((s1 & 0xf) << 2) + (s2 >> 6)];
  57. *p++ = tbl[s2 & 0x3f];
  58. s += 3;
  59. }
  60. /* Zero-terminate */
  61. *p = '\0';
  62. /* If length is -2 or -1, pad last char or two */
  63. while (length) {
  64. *--p = tbl[64];
  65. length++;
  66. }
  67. }
  68. /*
  69. * Decode base64 encoded string. Stops on '\0'.
  70. *
  71. * Returns: pointer to the undecoded part of source.
  72. * If points to '\0', then the source was fully decoded.
  73. * (*pp_dst): advanced past the last written byte.
  74. */
  75. const char* FAST_FUNC decode_base64(char **pp_dst, const char *src)
  76. {
  77. char *dst = *pp_dst;
  78. const char *src_tail;
  79. while (1) {
  80. unsigned char six_bit[4];
  81. int count = 0;
  82. /* Fetch up to four 6-bit values */
  83. src_tail = src;
  84. while (count < 4) {
  85. char *table_ptr;
  86. int ch;
  87. /* Get next _valid_ character.
  88. * bb_uuenc_tbl_base64[] contains this string:
  89. * 0 1 2 3 4 5 6
  90. * 01234567890123456789012345678901234567890123456789012345678901234
  91. * "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
  92. */
  93. do {
  94. ch = *src;
  95. if (ch == '\0') {
  96. if (count == 0) {
  97. /* Example:
  98. * If we decode "QUJD <NUL>", we want
  99. * to return ptr to NUL, not to ' ',
  100. * because we did fully decode
  101. * the string (to "ABC").
  102. */
  103. src_tail = src;
  104. }
  105. goto ret;
  106. }
  107. src++;
  108. table_ptr = strchr(bb_uuenc_tbl_base64, ch);
  109. //TODO: add BASE64_FLAG_foo to die on bad char?
  110. } while (!table_ptr);
  111. /* Convert encoded character to decimal */
  112. ch = table_ptr - bb_uuenc_tbl_base64;
  113. /* ch is 64 if char was '=', otherwise 0..63 */
  114. if (ch == 64)
  115. break;
  116. six_bit[count] = ch;
  117. count++;
  118. }
  119. /* Transform 6-bit values to 8-bit ones.
  120. * count can be < 4 when we decode the tail:
  121. * "eQ==" -> "y", not "y NUL NUL".
  122. * Note that (count > 1) is always true,
  123. * "x===" encoding is not valid:
  124. * even a single zero byte encodes as "AA==".
  125. * However, with current logic we come here with count == 1
  126. * when we decode "==" tail.
  127. */
  128. if (count > 1)
  129. *dst++ = six_bit[0] << 2 | six_bit[1] >> 4;
  130. if (count > 2)
  131. *dst++ = six_bit[1] << 4 | six_bit[2] >> 2;
  132. if (count > 3)
  133. *dst++ = six_bit[2] << 6 | six_bit[3];
  134. /* Note that if we decode "AA==" and ate first '=',
  135. * we just decoded one char (count == 2) and now we'll
  136. * do the loop once more to decode second '='.
  137. */
  138. } /* while (1) */
  139. ret:
  140. *pp_dst = dst;
  141. return src_tail;
  142. }
  143. /*
  144. * Decode base64 encoded stream.
  145. * Can stop on EOF, specified char, or on uuencode-style "====" line:
  146. * flags argument controls it.
  147. */
  148. void FAST_FUNC read_base64(FILE *src_stream, FILE *dst_stream, int flags)
  149. {
  150. /* Note that EOF _can_ be passed as exit_char too */
  151. #define exit_char ((int)(signed char)flags)
  152. #define uu_style_end (flags & BASE64_FLAG_UU_STOP)
  153. /* uuencoded files have 61 byte lines. Use 64 byte buffer
  154. * to process line at a time.
  155. */
  156. enum { BUFFER_SIZE = 64 };
  157. char in_buf[BUFFER_SIZE + 2];
  158. char out_buf[BUFFER_SIZE / 4 * 3 + 2];
  159. char *out_tail;
  160. const char *in_tail;
  161. int term_seen = 0;
  162. int in_count = 0;
  163. while (1) {
  164. while (in_count < BUFFER_SIZE) {
  165. int ch = fgetc(src_stream);
  166. if (ch == exit_char) {
  167. if (in_count == 0)
  168. return;
  169. term_seen = 1;
  170. break;
  171. }
  172. if (ch == EOF) {
  173. term_seen = 1;
  174. break;
  175. }
  176. /* Prevent "====" line to be split: stop if we see '\n'.
  177. * We can also skip other whitespace and skirt the problem
  178. * of files with NULs by stopping on any control char or space:
  179. */
  180. if (ch <= ' ')
  181. break;
  182. in_buf[in_count++] = ch;
  183. }
  184. in_buf[in_count] = '\0';
  185. /* Did we encounter "====" line? */
  186. if (uu_style_end && strcmp(in_buf, "====") == 0)
  187. return;
  188. out_tail = out_buf;
  189. in_tail = decode_base64(&out_tail, in_buf);
  190. fwrite(out_buf, (out_tail - out_buf), 1, dst_stream);
  191. if (term_seen) {
  192. /* Did we consume ALL characters? */
  193. if (*in_tail == '\0')
  194. return;
  195. /* No */
  196. bb_error_msg_and_die("truncated base64 input");
  197. }
  198. /* It was partial decode */
  199. in_count = strlen(in_tail);
  200. memmove(in_buf, in_tail, in_count);
  201. }
  202. }