base64.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. static const char rcsid[] = "#(@) $Id$";
  2. /*
  3. Encode or decode file as MIME base64 (RFC 1341)
  4. by John Walker
  5. http://www.fourmilab.ch/
  6. This program is in the public domain.
  7. */
  8. #include <stdio.h>
  9. /* ENCODE -- Encode binary file into base64. */
  10. #include <stdlib.h>
  11. #include <ctype.h>
  12. #include <limits.h>
  13. #include "base64.h"
  14. static unsigned char dtable[512];
  15. void buffer_new(struct buffer_st *b)
  16. {
  17. b->length = 512;
  18. b->data = malloc(sizeof(char)*(b->length));
  19. b->data[0] = 0;
  20. b->ptr = b->data;
  21. b->offset = 0;
  22. }
  23. void buffer_add(struct buffer_st *b, char c)
  24. {
  25. if ((INT_MAX - b->length) <= 512) {
  26. return;
  27. }
  28. *(b->ptr++) = c;
  29. b->offset++;
  30. if (b->offset == b->length) {
  31. b->length += 512;
  32. b->data = realloc(b->data, b->length);
  33. b->ptr = b->data + b->offset;
  34. }
  35. }
  36. void buffer_delete(struct buffer_st *b)
  37. {
  38. free(b->data);
  39. b->length = 0;
  40. b->offset = 0;
  41. b->ptr = NULL;
  42. b->data = NULL;
  43. }
  44. void base64_encode_xmlrpc(struct buffer_st *b, const char *source, int length)
  45. {
  46. int i, hiteof = 0;
  47. int offset = 0;
  48. int olen;
  49. olen = 0;
  50. buffer_new(b);
  51. /* Fill dtable with character encodings. */
  52. for (i = 0; i < 26; i++) {
  53. dtable[i] = 'A' + i;
  54. dtable[26 + i] = 'a' + i;
  55. }
  56. for (i = 0; i < 10; i++) {
  57. dtable[52 + i] = '0' + i;
  58. }
  59. dtable[62] = '+';
  60. dtable[63] = '/';
  61. while (!hiteof) {
  62. unsigned char igroup[3], ogroup[4];
  63. int c, n;
  64. igroup[0] = igroup[1] = igroup[2] = 0;
  65. for (n = 0; n < 3; n++) {
  66. c = *(source++);
  67. offset++;
  68. if (offset > length || offset <= 0) {
  69. hiteof = 1;
  70. break;
  71. }
  72. igroup[n] = (unsigned char) c;
  73. }
  74. if (n > 0) {
  75. ogroup[0] = dtable[igroup[0] >> 2];
  76. ogroup[1] = dtable[((igroup[0] & 3) << 4) | (igroup[1] >> 4)];
  77. ogroup[2] = dtable[((igroup[1] & 0xF) << 2) | (igroup[2] >> 6)];
  78. ogroup[3] = dtable[igroup[2] & 0x3F];
  79. /* Replace characters in output stream with "=" pad
  80. characters if fewer than three characters were
  81. read from the end of the input stream. */
  82. if (n < 3) {
  83. ogroup[3] = '=';
  84. if (n < 2) {
  85. ogroup[2] = '=';
  86. }
  87. }
  88. for (i = 0; i < 4; i++) {
  89. buffer_add(b, ogroup[i]);
  90. if (!(b->offset % 72)) {
  91. /* buffer_add(b, '\r'); */
  92. buffer_add(b, '\n');
  93. }
  94. }
  95. }
  96. }
  97. /* buffer_add(b, '\r'); */
  98. buffer_add(b, '\n');
  99. }
  100. void base64_decode_xmlrpc(struct buffer_st *bfr, const char *source, int length)
  101. {
  102. int i;
  103. int offset = 0;
  104. int endoffile;
  105. int count;
  106. buffer_new(bfr);
  107. for (i = 0; i < 255; i++) {
  108. dtable[i] = 0x80;
  109. }
  110. for (i = 'A'; i <= 'Z'; i++) {
  111. dtable[i] = 0 + (i - 'A');
  112. }
  113. for (i = 'a'; i <= 'z'; i++) {
  114. dtable[i] = 26 + (i - 'a');
  115. }
  116. for (i = '0'; i <= '9'; i++) {
  117. dtable[i] = 52 + (i - '0');
  118. }
  119. dtable['+'] = 62;
  120. dtable['/'] = 63;
  121. dtable['='] = 0;
  122. endoffile = 0;
  123. /*CONSTANTCONDITION*/
  124. while (1) {
  125. unsigned char a[4], b[4], o[3];
  126. for (i = 0; i < 4; i++) {
  127. int c;
  128. while (1) {
  129. c = *(source++);
  130. offset++;
  131. if (offset > length) endoffile = 1;
  132. if (isspace(c) || c == '\n' || c == '\r') continue;
  133. break;
  134. }
  135. if (endoffile) {
  136. /*
  137. if (i > 0) {
  138. fprintf(stderr, "Input file incomplete.\n");
  139. exit(1);
  140. }
  141. */
  142. return;
  143. }
  144. if (dtable[(unsigned char)c] & 0x80) {
  145. /*
  146. fprintf(stderr, "Offset %i length %i\n", offset, length);
  147. fprintf(stderr, "character '%c:%x:%c' in input file.\n", c, c, dtable[c]);
  148. exit(1);
  149. */
  150. i--;
  151. continue;
  152. }
  153. a[i] = (unsigned char) c;
  154. b[i] = (unsigned char) dtable[c];
  155. }
  156. o[0] = (b[0] << 2) | (b[1] >> 4);
  157. o[1] = (b[1] << 4) | (b[2] >> 2);
  158. o[2] = (b[2] << 6) | b[3];
  159. i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
  160. count = 0;
  161. while (count < i) {
  162. buffer_add(bfr, o[count++]);
  163. }
  164. if (i < 3) {
  165. return;
  166. }
  167. }
  168. }