dsniff  2.4b2
About: A collection of tools for network auditing
  Fossies Dox: dsniff-2.4b2.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

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