PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

lib/libc/net/base64.c

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