home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / k95source / ckcb64.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  12KB  |  291 lines

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