3
0

uuencode.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 + 2] 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. '\n', '\0' /* needed for uudecode.c */
  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 stream.
  70. * Can stop on EOF, specified char, or on uuencode-style "====" line:
  71. * flags argument controls it.
  72. */
  73. void FAST_FUNC read_base64(FILE *src_stream, FILE *dst_stream, int flags)
  74. {
  75. /* Note that EOF _can_ be passed as exit_char too */
  76. #define exit_char ((int)(signed char)flags)
  77. #define uu_style_end (flags & BASE64_FLAG_UU_STOP)
  78. int term_count = 0;
  79. while (1) {
  80. unsigned char translated[4];
  81. int count = 0;
  82. /* Process one group of 4 chars */
  83. while (count < 4) {
  84. char *table_ptr;
  85. int ch;
  86. /* Get next _valid_ character.
  87. * bb_uuenc_tbl_base64[] contains this string:
  88. * 0 1 2 3 4 5 6
  89. * 012345678901234567890123456789012345678901234567890123456789012345
  90. * "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\n"
  91. */
  92. do {
  93. ch = fgetc(src_stream);
  94. if (ch == exit_char && count == 0)
  95. return;
  96. if (ch == EOF)
  97. bb_error_msg_and_die("truncated base64 input");
  98. table_ptr = strchr(bb_uuenc_tbl_base64, ch);
  99. //TODO: add BASE64_FLAG_foo to die on bad char?
  100. //Note that then we may need to still allow '\r' (for mail processing)
  101. } while (!table_ptr);
  102. /* Convert encoded character to decimal */
  103. ch = table_ptr - bb_uuenc_tbl_base64;
  104. if (ch == 65 /* '\n' */) {
  105. /* Terminating "====" line? */
  106. if (uu_style_end && term_count == 4)
  107. return; /* yes */
  108. term_count = 0;
  109. continue;
  110. }
  111. /* ch is 64 if char was '=', otherwise 0..63 */
  112. translated[count] = ch & 63; /* 64 -> 0 */
  113. if (ch == 64) {
  114. term_count++;
  115. break;
  116. }
  117. count++;
  118. term_count = 0;
  119. }
  120. /* Merge 6 bit chars to 8 bit.
  121. * count can be < 4 when we decode the tail:
  122. * "eQ==" -> "y", not "y NUL NUL"
  123. */
  124. if (count > 1)
  125. fputc(translated[0] << 2 | translated[1] >> 4, dst_stream);
  126. if (count > 2)
  127. fputc(translated[1] << 4 | translated[2] >> 2, dst_stream);
  128. if (count > 3)
  129. fputc(translated[2] << 6 | translated[3], dst_stream);
  130. } /* while (1) */
  131. }