base64.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /* $OpenBSD: base64.c,v 1.7 2013/12/31 02:32:56 tedu Exp $ */
  2. /*
  3. * Copyright (c) 1996 by Internet Software Consortium.
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
  10. * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
  11. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
  12. * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  13. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  14. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  15. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  16. * SOFTWARE.
  17. */
  18. /*
  19. * Portions Copyright (c) 1995 by International Business Machines, Inc.
  20. *
  21. * International Business Machines, Inc. (hereinafter called IBM) grants
  22. * permission under its copyrights to use, copy, modify, and distribute this
  23. * Software with or without fee, provided that the above copyright notice and
  24. * all paragraphs of this notice appear in all copies, and that the name of IBM
  25. * not be used in connection with the marketing of any product incorporating
  26. * the Software or modifications thereof, without specific, written prior
  27. * permission.
  28. *
  29. * To the extent it has a right to do so, IBM grants an immunity from suit
  30. * under its patents, if any, for the use, sale or manufacture of products to
  31. * the extent that such products are used for performing Domain Name System
  32. * dynamic updates in TCP/IP networks by means of the Software. No immunity is
  33. * granted for any product per se or for any other function of any product.
  34. *
  35. * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
  36. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  37. * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
  38. * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
  39. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
  40. * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #include <sys/types.h>
  43. #include <ctype.h>
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <string.h>
  47. #include "base64.h"
  48. static const char Base64[] =
  49. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  50. static const char Pad64 = '=';
  51. /* (From RFC1521 and draft-ietf-dnssec-secext-03.txt)
  52. The following encoding technique is taken from RFC 1521 by Borenstein
  53. and Freed. It is reproduced here in a slightly edited form for
  54. convenience.
  55. A 65-character subset of US-ASCII is used, enabling 6 bits to be
  56. represented per printable character. (The extra 65th character, "=",
  57. is used to signify a special processing function.)
  58. The encoding process represents 24-bit groups of input bits as output
  59. strings of 4 encoded characters. Proceeding from left to right, a
  60. 24-bit input group is formed by concatenating 3 8-bit input groups.
  61. These 24 bits are then treated as 4 concatenated 6-bit groups, each
  62. of which is translated into a single digit in the base64 alphabet.
  63. Each 6-bit group is used as an index into an array of 64 printable
  64. characters. The character referenced by the index is placed in the
  65. output string.
  66. Table 1: The Base64 Alphabet
  67. Value Encoding Value Encoding Value Encoding Value Encoding
  68. 0 A 17 R 34 i 51 z
  69. 1 B 18 S 35 j 52 0
  70. 2 C 19 T 36 k 53 1
  71. 3 D 20 U 37 l 54 2
  72. 4 E 21 V 38 m 55 3
  73. 5 F 22 W 39 n 56 4
  74. 6 G 23 X 40 o 57 5
  75. 7 H 24 Y 41 p 58 6
  76. 8 I 25 Z 42 q 59 7
  77. 9 J 26 a 43 r 60 8
  78. 10 K 27 b 44 s 61 9
  79. 11 L 28 c 45 t 62 +
  80. 12 M 29 d 46 u 63 /
  81. 13 N 30 e 47 v
  82. 14 O 31 f 48 w (pad) =
  83. 15 P 32 g 49 x
  84. 16 Q 33 h 50 y
  85. Special processing is performed if fewer than 24 bits are available
  86. at the end of the data being encoded. A full encoding quantum is
  87. always completed at the end of a quantity. When fewer than 24 input
  88. bits are available in an input group, zero bits are added (on the
  89. right) to form an integral number of 6-bit groups. Padding at the
  90. end of the data is performed using the '=' character.
  91. Since all base64 input is an integral number of octets, only the
  92. -------------------------------------------------
  93. following cases can arise:
  94. (1) the final quantum of encoding input is an integral
  95. multiple of 24 bits; here, the final unit of encoded
  96. output will be an integral multiple of 4 characters
  97. with no "=" padding,
  98. (2) the final quantum of encoding input is exactly 8 bits;
  99. here, the final unit of encoded output will be two
  100. characters followed by two "=" padding characters, or
  101. (3) the final quantum of encoding input is exactly 16 bits;
  102. here, the final unit of encoded output will be three
  103. characters followed by one "=" padding character.
  104. */
  105. int b64_ntop(const void *_src, size_t srclength,
  106. void *dest, size_t targsize)
  107. {
  108. const unsigned char *src = _src;
  109. char *target = dest;
  110. size_t datalength = 0;
  111. u_char input[3];
  112. u_char output[4];
  113. int i;
  114. while (2 < srclength) {
  115. input[0] = *src++;
  116. input[1] = *src++;
  117. input[2] = *src++;
  118. srclength -= 3;
  119. output[0] = input[0] >> 2;
  120. output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
  121. output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
  122. output[3] = input[2] & 0x3f;
  123. if (datalength + 4 > targsize)
  124. return (-1);
  125. target[datalength++] = Base64[output[0]];
  126. target[datalength++] = Base64[output[1]];
  127. target[datalength++] = Base64[output[2]];
  128. target[datalength++] = Base64[output[3]];
  129. }
  130. /* Now we worry about padding. */
  131. if (0 != srclength) {
  132. /* Get what's left. */
  133. input[0] = input[1] = input[2] = '\0';
  134. for (i = 0; i < srclength; i++)
  135. input[i] = *src++;
  136. output[0] = input[0] >> 2;
  137. output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
  138. output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
  139. if (datalength + 4 > targsize)
  140. return (-1);
  141. target[datalength++] = Base64[output[0]];
  142. target[datalength++] = Base64[output[1]];
  143. if (srclength == 1)
  144. target[datalength++] = Pad64;
  145. else
  146. target[datalength++] = Base64[output[2]];
  147. target[datalength++] = Pad64;
  148. }
  149. if (datalength >= targsize)
  150. return (-1);
  151. target[datalength] = '\0'; /* Returned value doesn't count \0. */
  152. return (datalength);
  153. }
  154. /* skips all whitespace anywhere.
  155. converts characters, four at a time, starting at (or after)
  156. src from base - 64 numbers into three 8 bit bytes in the target area.
  157. it returns the number of data bytes stored at the target, or -1 on error.
  158. */
  159. int b64_pton(const void *_src, void *dest, size_t targsize)
  160. {
  161. const char *src = _src;
  162. unsigned char *target = dest;
  163. int tarindex, state, ch;
  164. u_char nextbyte;
  165. char *pos;
  166. state = 0;
  167. tarindex = 0;
  168. while ((ch = (unsigned char)*src++) != '\0') {
  169. if (isspace(ch)) /* Skip whitespace anywhere. */
  170. continue;
  171. if (ch == Pad64)
  172. break;
  173. pos = strchr(Base64, ch);
  174. if (pos == 0) /* A non-base64 character. */
  175. return (-1);
  176. switch (state) {
  177. case 0:
  178. if (target) {
  179. if (tarindex >= targsize)
  180. return (-1);
  181. target[tarindex] = (pos - Base64) << 2;
  182. }
  183. state = 1;
  184. break;
  185. case 1:
  186. if (target) {
  187. if (tarindex >= targsize)
  188. return (-1);
  189. target[tarindex] |= (pos - Base64) >> 4;
  190. nextbyte = ((pos - Base64) & 0x0f) << 4;
  191. if (tarindex + 1 < targsize)
  192. target[tarindex+1] = nextbyte;
  193. else if (nextbyte)
  194. return (-1);
  195. }
  196. tarindex++;
  197. state = 2;
  198. break;
  199. case 2:
  200. if (target) {
  201. if (tarindex >= targsize)
  202. return (-1);
  203. target[tarindex] |= (pos - Base64) >> 2;
  204. nextbyte = ((pos - Base64) & 0x03) << 6;
  205. if (tarindex + 1 < targsize)
  206. target[tarindex+1] = nextbyte;
  207. else if (nextbyte)
  208. return (-1);
  209. }
  210. tarindex++;
  211. state = 3;
  212. break;
  213. case 3:
  214. if (target) {
  215. if (tarindex >= targsize)
  216. return (-1);
  217. target[tarindex] |= (pos - Base64);
  218. }
  219. tarindex++;
  220. state = 0;
  221. break;
  222. }
  223. }
  224. /*
  225. * We are done decoding Base-64 chars. Let's see if we ended
  226. * on a byte boundary, and/or with erroneous trailing characters.
  227. */
  228. if (ch == Pad64) { /* We got a pad char. */
  229. ch = (unsigned char)*src++; /* Skip it, get next. */
  230. switch (state) {
  231. case 0: /* Invalid = in first position */
  232. case 1: /* Invalid = in second position */
  233. return (-1);
  234. case 2: /* Valid, means one byte of info */
  235. /* Skip any number of spaces. */
  236. for (; ch != '\0'; ch = (unsigned char)*src++)
  237. if (!isspace(ch))
  238. break;
  239. /* Make sure there is another trailing = sign. */
  240. if (ch != Pad64)
  241. return (-1);
  242. ch = (unsigned char)*src++; /* Skip the = */
  243. /* Fall through to "single trailing =" case. */
  244. /* FALLTHROUGH */
  245. case 3: /* Valid, means two bytes of info */
  246. /*
  247. * We know this char is an =. Is there anything but
  248. * whitespace after it?
  249. */
  250. for (; ch != '\0'; ch = (unsigned char)*src++)
  251. if (!isspace(ch))
  252. return (-1);
  253. /*
  254. * Now make sure for cases 2 and 3 that the "extra"
  255. * bits that slopped past the last full byte were
  256. * zeros. If we don't check them, they become a
  257. * subliminal channel.
  258. */
  259. if (target && tarindex < targsize &&
  260. target[tarindex] != 0)
  261. return (-1);
  262. }
  263. } else {
  264. /*
  265. * We ended by seeing the end of the string. Make sure we
  266. * have no partial bytes lying around.
  267. */
  268. if (state != 0)
  269. return (-1);
  270. }
  271. return (tarindex);
  272. }