File: encoding3548.c

package info (click to toggle)
mod-vhost-hash-alias 1.0-2
  • links: PTS
  • area: main
  • in suites: lenny, squeeze, wheezy
  • size: 1,676 kB
  • ctags: 114
  • sloc: sh: 8,468; ansic: 913; makefile: 123; perl: 28; python: 10
file content (365 lines) | stat: -rw-r--r-- 8,139 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/* 
 * encoding3548.c - RFC 3548 encodings
 *
 * Copyright (C) 2005 Yann Droneaud <ydroneaud@meuh.org>
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 */

/* This file implements some of the encoding described in 
   RFC 3548: The Base16, Base32, and Base64 Data Encodings */

#include <sys/types.h>
#include "encoding3548.h"

static inline char 
get(const char *conv, size_t count, unsigned int index)
{
  if (index >= (unsigned int) count) {
    return '!';
  }

  return conv[index];
}

/*
 * RFC 3548
 *  6.  Base 16 Encoding
 *
 * base16 (aka hexadecimal)
 * a well known encoding
 *
 */

static const char base16_up[]  = "0123456789ABCDEF";
static const char base16_low[] = "0123456789abcdef";

size_t
base16_encode_len(size_t bin_size)
{
  return bin_size * 2;
}

/* the common function */
static size_t
_base16_encode(char *str,
	       const unsigned char *bin, size_t bin_size,
	       const char *conv)
{
  size_t ret;

  unsigned char val;

  ret = base16_encode_len(bin_size);

  for(;bin_size > 0; bin_size --) {
    val = *bin ++;
  
    *(str ++) = get(conv, 16, (val >> 4));
    *(str ++) = get(conv, 16, (val & 0x0f));
  }

  return ret;
}

size_t
base16_encode(char *str,
	      const unsigned char *bin, size_t bin_size)
{
  return _base16_encode(str, bin, bin_size, base16_up);
}

size_t
base16_up_encode(char *str,
		 const unsigned char *bin, size_t bin_size)
{
  return _base16_encode(str, bin, bin_size, base16_up);
}

size_t
base16_low_encode(char *str,
		  const unsigned char *bin, size_t bin_size)
{
  return _base16_encode(str, bin, bin_size, base16_low);
}

/* 
 * RFC 3548
 *  5.  Base 32 Encoding
 *
 */

static char base32_up[]  = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
static char base32_low[] = "abcdefghijklmnopqrstuvwxyz234567";

size_t
base32_encode_len(size_t bin_size)
{
  if (bin_size % 5 == 0) {
    return (bin_size / 5) * 8;
  } else {
    return ((bin_size + 5) / 5) * 8;
  }
}

/* encode the binary buffer into base32 */
static size_t
_base32_encode(char *str, 
	       const unsigned char *bin, size_t bin_size,
	       const char *conv)
{
  size_t ret;
  int i;
  int padding;

  ret = base32_encode_len(bin_size);

  for(i = 0; (i + (size_t) 5) <= bin_size; i += 5) {

    *(str ++) = get(conv, 32,  (bin[i]   & 0xf8) >> 3);
    *(str ++) = get(conv, 32, ((bin[i]   & 0x07) << 2 |
			       (bin[i+1] & 0xc0) >> 6));
    *(str ++) = get(conv, 32,  (bin[i+1] & 0x3e) >> 1); 
    *(str ++) = get(conv, 32, ((bin[i+1] & 0x01) << 4 |  
			       (bin[i+2] & 0xf0) >> 4)); 
    *(str ++) = get(conv, 32, ((bin[i+2] & 0x0f) << 1 | 
			       (bin[i+3] & 0x80) >> 7));
    *(str ++) = get(conv, 32,  (bin[i+3] & 0x7c) >> 2);
    *(str ++) = get(conv, 32, ((bin[i+3] & 0x03) << 3 |
			       (bin[i+4] & 0xe0) >> 5));
    *(str ++) = get(conv, 32,   bin[i+4] & 0x1f      ); 
  }

  padding = 0;

  switch(bin_size - i) {
  case 1:
    *(str ++) = get(conv, 32,  (bin[i]   & 0xf8) >> 3);
    *(str ++) = get(conv, 32, ((bin[i]   & 0x07) << 2));
    padding = 6;
    break;

  case 2:
    *(str ++) = get(conv, 32,  (bin[i]   & 0xf8) >> 3);
    *(str ++) = get(conv, 32, ((bin[i]   & 0x07) << 2 |
			       (bin[i+1] & 0xc0) >> 6));
    *(str ++) = get(conv, 32,  (bin[i+1] & 0x3e) >> 1); 
    *(str ++) = get(conv, 32, ((bin[i+1] & 0x01) << 4));
    padding = 4;
    break;

  case 3:
    *(str ++) = get(conv, 32,  (bin[i]   & 0xf8) >> 3);
    *(str ++) = get(conv, 32, ((bin[i]   & 0x07) << 2 |
			       (bin[i+1] & 0xc0) >> 6));
    *(str ++) = get(conv, 32,  (bin[i+1] & 0x3e) >> 1); 
    *(str ++) = get(conv, 32, ((bin[i+1] & 0x01) << 4 |  
			       (bin[i+2] & 0xf0) >> 4)); 
    *(str ++) = get(conv, 32, ((bin[i+2] & 0x0f) << 1));
    padding = 3;
    break;

  case 4:

    *(str ++) = get(conv, 32,  (bin[i]   & 0xf8) >> 3);
    *(str ++) = get(conv, 32, ((bin[i]   & 0x07) << 2 |
			       (bin[i+1] & 0xc0) >> 6));
    *(str ++) = get(conv, 32,  (bin[i+1] & 0x3e) >> 1); 
    *(str ++) = get(conv, 32, ((bin[i+1] & 0x01) << 4 |  
			       (bin[i+2] & 0xf0) >> 4)); 
    *(str ++) = get(conv, 32, ((bin[i+2] & 0x0f) << 1 | 
			       (bin[i+3] & 0x80) >> 7));
    *(str ++) = get(conv, 32,  (bin[i+3] & 0x7c) >> 2);
    *(str ++) = get(conv, 32, ((bin[i+3] & 0x03) << 3));
    padding = 1;
    break;
  }
  
  for(;padding > 0; padding --) {
    *(str ++) = '=';
  }

  return ret;
}

size_t
base32_encode(char *str, 
	      const unsigned char *bin, size_t bin_size)
{
  return _base32_encode(str, bin, bin_size, base32_up);
}

size_t
base32_up_encode(char *str,
		 const unsigned char *bin, size_t bin_size)
{
  return _base32_encode(str, bin, bin_size, base32_up);
}

size_t
base32_low_encode(char *str, 
		  const unsigned char *bin, size_t bin_size)
{
  return _base32_encode(str, bin, bin_size, base32_low);
}

/*
 * RFC 3548
 *  4.  Base 64 Encoding with URL and Filename Safe Alphabet
 *
 * base64ufs:
 * base64 adapted for url/filename described in RFC 3548
 *
 */

static const char base64ufs[] = 
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";

size_t
base64_encode_len(size_t bin_size)
{
  if (bin_size % 3 == 0) {
    return (bin_size / 3) * 4;
  } else {
    return ((bin_size + 3) / 3) * 4;
  }
}

static size_t
_base64_encode(char *str,
	       const unsigned char *bin, size_t bin_size,
	       const char *conv)
{
  size_t ret;
  int i;
  int padding;

  ret = base64_encode_len(bin_size);

  for(i = 0; (i + (size_t) 3) <= bin_size; i += 3) {
    *(str ++) = get(conv, 64,  (bin[i]   & 0xfc) >> 2);
    *(str ++) = get(conv, 64, ((bin[i]   & 0x03) << 4 |
			       (bin[i+1] & 0xf0) >> 4));
    *(str ++) = get(conv, 64, ((bin[i+1] & 0x0f) << 2 |
			       (bin[i+2] & 0xc0) >> 6));
    *(str ++) = get(conv, 64,  (bin[i+2] & 0x3f));
  }

  padding = 0;

  switch(bin_size - i) {
  case 1:
    *(str ++) = get(conv, 64,  (bin[i]   & 0xfc) >> 2);
    *(str ++) = get(conv, 64, ((bin[i]   & 0x03) << 4));
    padding = 2;
    break;

  case 2:
    *(str ++) = get(conv, 64,  (bin[i]   & 0xfc) >> 2);
    *(str ++) = get(conv, 64, ((bin[i]   & 0x03) << 4 |
			       (bin[i+1] & 0xf0) >> 4));
    *(str ++) = get(conv, 64, ((bin[i+1] & 0x0f) << 2));
    padding = 1;
    break;
  }

  for(;padding > 0; padding --) {
    *(str ++) = '=';
  }

  return ret;
}

size_t
base64_ufs_encode(char *str, 
		  const unsigned char *bin, size_t bin_size)
{
  return _base64_encode(str, bin, bin_size, base64ufs);
}

#if defined(DO_TEST) && DO_TEST

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

int
test_base32_up(const char *str)
{
  size_t len;
  char *buffer;

  len = base32_encode_len(strlen(str));

  buffer = (char *) malloc(len + 1);

  base32_up_encode(buffer, str, strlen(str));
  buffer[len] = '\0';

  printf("'%s' -> '%s'\n", str, buffer);

  free(buffer);

  return 0;
}

int
test_base64_ufs(const char *str)
{
  size_t len;
  char *buffer;

  len = base64_encode_len(strlen(str));

  buffer = (char *) malloc(len + 1);

  base64_ufs_encode(buffer, str, strlen(str));
  buffer[len] = '\0';

  printf("'%s' -> '%s'\n", str, buffer);

  free(buffer);

  return 0;
}

#define test_func test_base64_ufs

int
main(void)
{

  test_func("a");
  test_func("ab");
  test_func("abc");
  test_func("abcd");
  test_func("abcde");
  test_func("abcdef");

  test_func("aa");
  test_func("aaa");
  test_func("aaaa");
  test_func("aaaaa");
  test_func("aaaaaa");

  test_func("Hell");
  test_func("Hello");
  test_func("Helloo");

  return 0;
}

#endif