-/*\r
- * Copyright (c) 1996, 1998 by Internet Software Consortium.\r
- *\r
- * Permission to use, copy, modify, and distribute this software for any\r
- * purpose with or without fee is hereby granted, provided that the above\r
- * copyright notice and this permission notice appear in all copies.\r
- *\r
- * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS\r
- * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES\r
- * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE\r
- * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL\r
- * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR\r
- * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS\r
- * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS\r
- * SOFTWARE.\r
- */\r
-\r
-/*\r
- * Portions Copyright (c) 1995 by International Business Machines, Inc.\r
- *\r
- * International Business Machines, Inc. (hereinafter called IBM) grants\r
- * permission under its copyrights to use, copy, modify, and distribute this\r
- * Software with or without fee, provided that the above copyright notice and\r
- * all paragraphs of this notice appear in all copies, and that the name of IBM\r
- * not be used in connection with the marketing of any product incorporating\r
- * the Software or modifications thereof, without specific, written prior\r
- * permission.\r
- *\r
- * To the extent it has a right to do so, IBM grants an immunity from suit\r
- * under its patents, if any, for the use, sale or manufacture of products to\r
- * the extent that such products are used for performing Domain Name System\r
- * dynamic updates in TCP/IP networks by means of the Software. No immunity is\r
- * granted for any product per se or for any other function of any product.\r
- *\r
- * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,\r
- * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A\r
- * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,\r
- * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING\r
- * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN\r
- * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.\r
- */\r
-\r
-/*\r
- * Portions copyright (c) 1999, 2000\r
- * Intel Corporation.\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- *\r
- * 1. Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- *\r
- * 2. Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- *\r
- * 3. All advertising materials mentioning features or use of this software\r
- * must display the following acknowledgement:\r
- *\r
- * This product includes software developed by Intel Corporation and\r
- * its contributors.\r
- *\r
- * 4. Neither the name of Intel Corporation or its contributors may be\r
- * used to endorse or promote products derived from this software\r
- * without specific prior written permission.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION AND CONTRIBUTORS ``AS IS''\r
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
- * ARE DISCLAIMED. IN NO EVENT SHALL INTEL CORPORATION OR CONTRIBUTORS BE\r
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF\r
- * THE POSSIBILITY OF SUCH DAMAGE.\r
- *\r
- base64.c,v 1.1.1.1 2003/11/19 01:51:25 kyu3 Exp\r
- */\r
-\r
-#include <sys/types.h>\r
-#include <sys/param.h>\r
-#include <sys/socket.h>\r
-\r
-#include <netinet/in.h>\r
-#include <arpa/inet.h>\r
-#include <arpa/nameser.h>\r
-\r
-#include <ctype.h>\r
-#include <resolv.h>\r
-#include <stdio.h>\r
-#include <stdlib.h>\r
-#include <string.h>\r
-\r
-#define Assert(Cond) if (!(Cond)) abort()\r
-\r
-static const char Base64[] =\r
- "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";\r
-static const char Pad64 = '=';\r
-\r
-/* (From RFC1521 and draft-ietf-dnssec-secext-03.txt)\r
- The following encoding technique is taken from RFC 1521 by Borenstein\r
- and Freed. It is reproduced here in a slightly edited form for\r
- convenience.\r
-\r
- A 65-character subset of US-ASCII is used, enabling 6 bits to be\r
- represented per printable character. (The extra 65th character, "=",\r
- is used to signify a special processing function.)\r
-\r
- The encoding process represents 24-bit groups of input bits as output\r
- strings of 4 encoded characters. Proceeding from left to right, a\r
- 24-bit input group is formed by concatenating 3 8-bit input groups.\r
- These 24 bits are then treated as 4 concatenated 6-bit groups, each\r
- of which is translated into a single digit in the base64 alphabet.\r
-\r
- Each 6-bit group is used as an index into an array of 64 printable\r
- characters. The character referenced by the index is placed in the\r
- output string.\r
-\r
- Table 1: The Base64 Alphabet\r
-\r
- Value Encoding Value Encoding Value Encoding Value Encoding\r
- 0 A 17 R 34 i 51 z\r
- 1 B 18 S 35 j 52 0\r
- 2 C 19 T 36 k 53 1\r
- 3 D 20 U 37 l 54 2\r
- 4 E 21 V 38 m 55 3\r
- 5 F 22 W 39 n 56 4\r
- 6 G 23 X 40 o 57 5\r
- 7 H 24 Y 41 p 58 6\r
- 8 I 25 Z 42 q 59 7\r
- 9 J 26 a 43 r 60 8\r
- 10 K 27 b 44 s 61 9\r
- 11 L 28 c 45 t 62 +\r
- 12 M 29 d 46 u 63 /\r
- 13 N 30 e 47 v\r
- 14 O 31 f 48 w (pad) =\r
- 15 P 32 g 49 x\r
- 16 Q 33 h 50 y\r
-\r
- Special processing is performed if fewer than 24 bits are available\r
- at the end of the data being encoded. A full encoding quantum is\r
- always completed at the end of a quantity. When fewer than 24 input\r
- bits are available in an input group, zero bits are added (on the\r
- right) to form an integral number of 6-bit groups. Padding at the\r
- end of the data is performed using the '=' character.\r
-\r
- Since all base64 input is an integral number of octets, only the\r
- -------------------------------------------------\r
- following cases can arise:\r
-\r
- (1) the final quantum of encoding input is an integral\r
- multiple of 24 bits; here, the final unit of encoded\r
- output will be an integral multiple of 4 characters\r
- with no "=" padding,\r
- (2) the final quantum of encoding input is exactly 8 bits;\r
- here, the final unit of encoded output will be two\r
- characters followed by two "=" padding characters, or\r
- (3) the final quantum of encoding input is exactly 16 bits;\r
- here, the final unit of encoded output will be three\r
- characters followed by one "=" padding character.\r
- */\r
-\r
-int\r
-b64_ntop(u_char const *src, size_t srclength, char *target, size_t targsize) {\r
- size_t datalength = 0;\r
- u_char input[3];\r
- u_char output[4];\r
- size_t i;\r
-\r
- input[0] = input[1] = input[2] = '\0';\r
- while (2 < srclength) {\r
- input[0] = *src++;\r
- input[1] = *src++;\r
- input[2] = *src++;\r
- srclength -= 3;\r
-\r
- output[0] = input[0] >> 2;\r
- output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);\r
- output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);\r
- output[3] = input[2] & 0x3f;\r
- Assert(output[0] < 64);\r
- Assert(output[1] < 64);\r
- Assert(output[2] < 64);\r
- Assert(output[3] < 64);\r
-\r
- if (datalength + 4 > targsize)\r
- return (-1);\r
- target[datalength++] = Base64[output[0]];\r
- target[datalength++] = Base64[output[1]];\r
- target[datalength++] = Base64[output[2]];\r
- target[datalength++] = Base64[output[3]];\r
- }\r
-\r
- /* Now we worry about padding. */\r
- if (0 != srclength) {\r
- /* Get what's left. */\r
- input[0] = input[1] = input[2] = '\0';\r
- for (i = 0; i < srclength; i++)\r
- input[i] = *src++;\r
-\r
- output[0] = input[0] >> 2;\r
- output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);\r
- output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);\r
- Assert(output[0] < 64);\r
- Assert(output[1] < 64);\r
- Assert(output[2] < 64);\r
-\r
- if (datalength + 4 > targsize)\r
- return (-1);\r
- target[datalength++] = Base64[output[0]];\r
- target[datalength++] = Base64[output[1]];\r
- if (srclength == 1)\r
- target[datalength++] = Pad64;\r
- else\r
- target[datalength++] = Base64[output[2]];\r
- target[datalength++] = Pad64;\r
- }\r
- if (datalength >= targsize)\r
- return (-1);\r
- target[datalength] = '\0'; /* Returned value doesn't count \0. */\r
- return ((int)datalength);\r
-}\r
-\r
-/* skips all whitespace anywhere.\r
- converts characters, four at a time, starting at (or after)\r
- src from base - 64 numbers into three 8 bit bytes in the target area.\r
- it returns the number of data bytes stored at the target, or -1 on error.\r
- */\r
-\r
-int\r
-b64_pton(\r
- char const *src,\r
- u_char *target,\r
- size_t targsize\r
- )\r
-{\r
- int tarindex, state, ch;\r
- char *pos;\r
-\r
- state = 0;\r
- tarindex = 0;\r
-\r
- while ((ch = *src++) != '\0') {\r
- if (isspace(ch)) /* Skip whitespace anywhere. */\r
- continue;\r
-\r
- if (ch == Pad64)\r
- break;\r
-\r
- pos = strchr(Base64, ch);\r
- if (pos == 0) /* A non-base64 character. */\r
- return (-1);\r
-\r
- switch (state) {\r
- case 0:\r
- if (target) {\r
- if ((size_t)tarindex >= targsize)\r
- return (-1);\r
- target[tarindex] = (u_char)((pos - Base64) << 2);\r
- }\r
- state = 1;\r
- break;\r
- case 1:\r
- if (target) {\r
- if ((size_t)tarindex + 1 >= targsize)\r
- return (-1);\r
- target[tarindex] |= (u_char)((pos - Base64) >> 4);\r
- target[tarindex+1] = (u_char)(((pos - Base64) & 0x0f)\r
- << 4) ;\r
- }\r
- tarindex++;\r
- state = 2;\r
- break;\r
- case 2:\r
- if (target) {\r
- if ((size_t)tarindex + 1 >= targsize)\r
- return (-1);\r
- target[tarindex] |= (u_char)((pos - Base64) >> 2);\r
- target[tarindex+1] = (u_char)(((pos - Base64) & 0x03)\r
- << 6);\r
- }\r
- tarindex++;\r
- state = 3;\r
- break;\r
- case 3:\r
- if (target) {\r
- if ((size_t)tarindex >= targsize)\r
- return (-1);\r
- target[tarindex] |= (u_char)(pos - Base64);\r
- }\r
- tarindex++;\r
- state = 0;\r
- break;\r
- default:\r
- abort();\r
- }\r
- }\r
-\r
- /*\r
- * We are done decoding Base-64 chars. Let's see if we ended\r
- * on a byte boundary, and/or with erroneous trailing characters.\r
- */\r
-\r
- if (ch == Pad64) { /* We got a pad char. */\r
- ch = *src++; /* Skip it, get next. */\r
- switch (state) {\r
- case 0: /* Invalid = in first position */\r
- case 1: /* Invalid = in second position */\r
- return (-1);\r
-\r
- case 2: /* Valid, means one byte of info */\r
- /* Skip any number of spaces. */\r
- for ((void)NULL; ch != '\0'; ch = *src++)\r
- if (!isspace(ch))\r
- break;\r
- /* Make sure there is another trailing = sign. */\r
- if (ch != Pad64)\r
- return (-1);\r
- ch = *src++; /* Skip the = */\r
- /* Fall through to "single trailing =" case. */\r
- /* FALLTHROUGH */\r
-\r
- case 3: /* Valid, means two bytes of info */\r
- /*\r
- * We know this char is an =. Is there anything but\r
- * whitespace after it?\r
- */\r
- for ((void)NULL; ch != '\0'; ch = *src++)\r
- if (!isspace(ch))\r
- return (-1);\r
-\r
- /*\r
- * Now make sure for cases 2 and 3 that the "extra"\r
- * bits that slopped past the last full byte were\r
- * zeros. If we don't check them, they become a\r
- * subliminal channel.\r
- */\r
- if (target && target[tarindex] != 0)\r
- return (-1);\r
- }\r
- } else {\r
- /*\r
- * We ended by seeing the end of the string. Make sure we\r
- * have no partial bytes lying around.\r
- */\r
- if (state != 0)\r
- return (-1);\r
- }\r
-\r
- return (tarindex);\r
-}\r