Root/package/ead/src/tinysrp/t_conv.c

1/*
2 * Copyright (c) 1997-1999 The Stanford SRP Authentication Project
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
18 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
19 *
20 * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
21 * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
22 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF
23 * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT
24 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 *
26 * In addition, the following conditions apply:
27 *
28 * 1. Any software that incorporates the SRP authentication technology
29 * must display the following acknowlegment:
30 * "This product uses the 'Secure Remote Password' cryptographic
31 * authentication system developed by Tom Wu (tjw@CS.Stanford.EDU)."
32 *
33 * 2. Any software that incorporates all or part of the SRP distribution
34 * itself must also display the following acknowledgment:
35 * "This product includes software developed by Tom Wu and Eugene
36 * Jhong for the SRP Distribution (http://srp.stanford.edu/srp/)."
37 *
38 * 3. Redistributions in source or binary form must retain an intact copy
39 * of this copyright notice and list of conditions.
40 */
41
42/*#define _POSIX_SOURCE*/
43#include <stdio.h>
44#include "t_defines.h"
45
46static int
47hexDigitToInt(c)
48     char c;
49{
50  if(c >= '0' && c <= '9')
51    return c - '0';
52  else if(c >= 'a' && c <= 'f')
53    return c - 'a' + 10;
54  else if(c >= 'A' && c <= 'F')
55    return c - 'A' + 10;
56  else
57    return 0;
58}
59
60/*
61 * Convert a hex string to a string of bytes; return size of dst
62 */
63_TYPE( int )
64t_fromhex(dst, src)
65     register char *dst, *src;
66{
67  register char *chp = dst;
68  register unsigned size = strlen(src);
69
70  /* FIXME: handle whitespace and non-hex digits by setting size and src
71     appropriately. */
72
73  if(size % 2 == 1) {
74    *chp++ = hexDigitToInt(*src++);
75    --size;
76  }
77  while(size > 0) {
78    *chp++ = (hexDigitToInt(*src) << 4) | hexDigitToInt(*(src + 1));
79    src += 2;
80    size -= 2;
81  }
82  return chp - dst;
83}
84
85/*
86 * Convert a string of bytes to their hex representation
87 */
88_TYPE( char * )
89t_tohex(dst, src, size)
90     register char *dst, *src;
91     register unsigned size;
92{
93   int notleading = 0;
94
95   register char *chp = dst;
96   if (size != 0) do {
97      if(notleading || *src != '\0') {
98    notleading = 1;
99    sprintf(chp, "%.2x", * (unsigned char *) src);
100    chp += 2;
101      }
102      ++src;
103   } while (--size != 0);
104   return dst;
105}
106
107static char b64table[] =
108  "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz./";
109
110/*
111 * Convert a base64 string into raw byte array representation.
112 */
113_TYPE( int )
114t_fromb64(dst, src)
115     register char *dst, *src;
116{
117  unsigned char *a;
118  char *loc;
119  int i, j;
120  unsigned int size;
121
122  while(*src && (*src == ' ' || *src == '\t' || *src == '\n'))
123      ++src;
124  size = strlen(src);
125
126  a = malloc((size + 1) * sizeof(unsigned char));
127  if(a == (unsigned char *) 0)
128    return -1;
129
130  i = 0;
131  while(i < size) {
132    loc = strchr(b64table, src[i]);
133    if(loc == (char *) 0)
134      break;
135    else
136      a[i] = loc - b64table;
137    ++i;
138  }
139  size = i;
140
141  i = size - 1;
142  j = size;
143  while(1) {
144    a[j] = a[i];
145    if(--i < 0)
146      break;
147    a[j] |= (a[i] & 3) << 6;
148    --j;
149    a[j] = (unsigned char) ((a[i] & 0x3c) >> 2);
150    if(--i < 0)
151      break;
152    a[j] |= (a[i] & 0xf) << 4;
153    --j;
154    a[j] = (unsigned char) ((a[i] & 0x30) >> 4);
155    if(--i < 0)
156      break;
157    a[j] |= (a[i] << 2);
158
159    a[--j] = 0;
160    if(--i < 0)
161      break;
162  }
163
164  while(a[j] == 0 && j <= size)
165    ++j;
166
167  memcpy(dst, a + j, size - j + 1);
168  free(a);
169  return size - j + 1;
170}
171
172/*
173 * Convert a raw byte string into a null-terminated base64 ASCII string.
174 */
175_TYPE( char * )
176t_tob64(dst, src, size)
177     register char *dst, *src;
178     register unsigned size;
179{
180  int c, pos = size % 3;
181  unsigned char b0 = 0, b1 = 0, b2 = 0, notleading = 0;
182  char *olddst = dst;
183
184  switch(pos) {
185  case 1:
186    b2 = src[0];
187    break;
188  case 2:
189    b1 = src[0];
190    b2 = src[1];
191    break;
192  }
193
194  while(1) {
195    c = (b0 & 0xfc) >> 2;
196    if(notleading || c != 0) {
197      *dst++ = b64table[c];
198      notleading = 1;
199    }
200    c = ((b0 & 3) << 4) | ((b1 & 0xf0) >> 4);
201    if(notleading || c != 0) {
202      *dst++ = b64table[c];
203      notleading = 1;
204    }
205    c = ((b1 & 0xf) << 2) | ((b2 & 0xc0) >> 6);
206    if(notleading || c != 0) {
207      *dst++ = b64table[c];
208      notleading = 1;
209    }
210    c = b2 & 0x3f;
211    if(notleading || c != 0) {
212      *dst++ = b64table[c];
213      notleading = 1;
214    }
215    if(pos >= size)
216      break;
217    else {
218      b0 = src[pos++];
219      b1 = src[pos++];
220      b2 = src[pos++];
221    }
222  }
223
224  *dst++ = '\0';
225  return olddst;
226}
227

Archive Download this file



interactive