added a chunk_printable() function (replaces sanitize_chunk)
[strongswan.git] / src / libstrongswan / chunk.c
1 /*
2 * Copyright (C) 2008-2009 Tobias Brunner
3 * Copyright (C) 2005-2006 Martin Willi
4 * Copyright (C) 2005 Jan Hutter
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 #include <stdio.h>
19 #include <sys/stat.h>
20 #include <unistd.h>
21 #include <errno.h>
22 #include <ctype.h>
23
24 #include "chunk.h"
25
26 #include <debug.h>
27
28 /* required for chunk_hash */
29 #undef get16bits
30 #if (defined(__GNUC__) && defined(__i386__))
31 #define get16bits(d) (*((const u_int16_t*)(d)))
32 #endif
33 #if !defined (get16bits)
34 #define get16bits(d) ((((u_int32_t)(((const u_int8_t*)(d))[1])) << 8)\
35 + (u_int32_t)(((const u_int8_t*)(d))[0]) )
36 #endif
37
38 /**
39 * Empty chunk.
40 */
41 chunk_t chunk_empty = { NULL, 0 };
42
43 /**
44 * Described in header.
45 */
46 chunk_t chunk_create_clone(u_char *ptr, chunk_t chunk)
47 {
48 chunk_t clone = chunk_empty;
49
50 if (chunk.ptr && chunk.len > 0)
51 {
52 clone.ptr = ptr;
53 clone.len = chunk.len;
54 memcpy(clone.ptr, chunk.ptr, chunk.len);
55 }
56
57 return clone;
58 }
59
60 /**
61 * Decribed in header.
62 */
63 size_t chunk_length(const char* mode, ...)
64 {
65 va_list chunks;
66 size_t length = 0;
67
68 va_start(chunks, mode);
69 while (TRUE)
70 {
71 switch (*mode++)
72 {
73 case 'm':
74 case 'c':
75 {
76 chunk_t ch = va_arg(chunks, chunk_t);
77 length += ch.len;
78 continue;
79 }
80 default:
81 break;
82 }
83 break;
84 }
85 va_end(chunks);
86 return length;
87 }
88
89 /**
90 * Decribed in header.
91 */
92 chunk_t chunk_create_cat(u_char *ptr, const char* mode, ...)
93 {
94 va_list chunks;
95 chunk_t construct = chunk_create(ptr, 0);
96
97 va_start(chunks, mode);
98 while (TRUE)
99 {
100 bool free_chunk = FALSE;
101 switch (*mode++)
102 {
103 case 'm':
104 {
105 free_chunk = TRUE;
106 }
107 case 'c':
108 {
109 chunk_t ch = va_arg(chunks, chunk_t);
110 memcpy(ptr, ch.ptr, ch.len);
111 ptr += ch.len;
112 construct.len += ch.len;
113 if (free_chunk)
114 {
115 free(ch.ptr);
116 }
117 continue;
118 }
119 default:
120 break;
121 }
122 break;
123 }
124 va_end(chunks);
125
126 return construct;
127 }
128
129 /**
130 * Decribed in header.
131 */
132 void chunk_split(chunk_t chunk, const char *mode, ...)
133 {
134 va_list chunks;
135 u_int len;
136 chunk_t *ch;
137
138 va_start(chunks, mode);
139 while (TRUE)
140 {
141 if (*mode == '\0')
142 {
143 break;
144 }
145 len = va_arg(chunks, u_int);
146 ch = va_arg(chunks, chunk_t*);
147 /* a null chunk means skip len bytes */
148 if (ch == NULL)
149 {
150 chunk = chunk_skip(chunk, len);
151 continue;
152 }
153 switch (*mode++)
154 {
155 case 'm':
156 {
157 ch->len = min(chunk.len, len);
158 if (ch->len)
159 {
160 ch->ptr = chunk.ptr;
161 }
162 else
163 {
164 ch->ptr = NULL;
165 }
166 chunk = chunk_skip(chunk, ch->len);
167 continue;
168 }
169 case 'a':
170 {
171 ch->len = min(chunk.len, len);
172 if (ch->len)
173 {
174 ch->ptr = malloc(ch->len);
175 memcpy(ch->ptr, chunk.ptr, ch->len);
176 }
177 else
178 {
179 ch->ptr = NULL;
180 }
181 chunk = chunk_skip(chunk, ch->len);
182 continue;
183 }
184 case 'c':
185 {
186 ch->len = min(ch->len, chunk.len);
187 ch->len = min(ch->len, len);
188 if (ch->len)
189 {
190 memcpy(ch->ptr, chunk.ptr, ch->len);
191 }
192 else
193 {
194 ch->ptr = NULL;
195 }
196 chunk = chunk_skip(chunk, ch->len);
197 continue;
198 }
199 default:
200 break;
201 }
202 break;
203 }
204 va_end(chunks);
205 }
206
207 /**
208 * Described in header.
209 */
210 bool chunk_write(chunk_t chunk, char *path, char *label, mode_t mask, bool force)
211 {
212 mode_t oldmask;
213 FILE *fd;
214 bool good = FALSE;
215
216 if (!force && access(path, F_OK) == 0)
217 {
218 DBG1(" %s file '%s' already exists", label, path);
219 return FALSE;
220 }
221 oldmask = umask(mask);
222 fd = fopen(path, "w");
223 if (fd)
224 {
225 if (fwrite(chunk.ptr, sizeof(u_char), chunk.len, fd) == chunk.len)
226 {
227 DBG1(" written %s file '%s' (%d bytes)",
228 label, path, chunk.len);
229 good = TRUE;
230 }
231 else
232 {
233 DBG1(" writing %s file '%s' failed: %s",
234 label, path, strerror(errno));
235 }
236 fclose(fd);
237 }
238 else
239 {
240 DBG1(" could not open %s file '%s': %s", label, path, strerror(errno));
241 }
242 umask(oldmask);
243 return good;
244 }
245
246
247 /** hex conversion digits */
248 static char hexdig_upper[] = "0123456789ABCDEF";
249 static char hexdig_lower[] = "0123456789abcdef";
250
251 /**
252 * Described in header.
253 */
254 chunk_t chunk_to_hex(chunk_t chunk, char *buf, bool uppercase)
255 {
256 int i, len;
257 char *hexdig = hexdig_lower;
258
259 if (uppercase)
260 {
261 hexdig = hexdig_upper;
262 }
263
264 len = chunk.len * 2;
265 if (!buf)
266 {
267 buf = malloc(len + 1);
268 }
269 buf[len] = '\0';
270
271 for (i = 0; i < chunk.len; i++)
272 {
273 buf[i*2] = hexdig[(chunk.ptr[i] >> 4) & 0xF];
274 buf[i*2+1] = hexdig[(chunk.ptr[i] ) & 0xF];
275 }
276 return chunk_create(buf, len);
277 }
278
279 /**
280 * convert a signle hex character to its binary value
281 */
282 static char hex2bin(char hex)
283 {
284 switch (hex)
285 {
286 case '0' ... '9':
287 return hex - '0';
288 case 'A' ... 'F':
289 return hex - 'A' + 10;
290 case 'a' ... 'f':
291 return hex - 'a' + 10;
292 default:
293 return 0;
294 }
295 }
296
297 /**
298 * Described in header.
299 */
300 chunk_t chunk_from_hex(chunk_t hex, char *buf)
301 {
302 int i, len;
303 bool odd = FALSE;
304
305 len = (hex.len / 2);
306 if (hex.len % 2)
307 {
308 odd = TRUE;
309 len++;
310 }
311 if (!buf)
312 {
313 buf = malloc(len);
314 }
315 /* buffer is filled from the right */
316 memset(buf, 0, len);
317 hex.ptr += hex.len;
318 for (i = len - 1; i >= 0; i--)
319 {
320 buf[i] = hex2bin(*(--hex.ptr));
321 if (i > 0 || !odd)
322 {
323 buf[i] |= hex2bin(*(--hex.ptr)) << 4;
324 }
325 }
326 return chunk_create(buf, len);
327 }
328
329 /** base 64 conversion digits */
330 static char b64digits[] =
331 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
332
333 /**
334 * Described in header.
335 */
336 chunk_t chunk_to_base64(chunk_t chunk, char *buf)
337 {
338 int i, len;
339 char *pos;
340
341 len = chunk.len + ((3 - chunk.len % 3) % 3);
342 if (!buf)
343 {
344 buf = malloc(len * 4 / 3 + 1);
345 }
346 pos = buf;
347 for (i = 0; i < len; i+=3)
348 {
349 *pos++ = b64digits[chunk.ptr[i] >> 2];
350 if (i+1 >= chunk.len)
351 {
352 *pos++ = b64digits[(chunk.ptr[i] & 0x03) << 4];
353 *pos++ = '=';
354 *pos++ = '=';
355 break;
356 }
357 *pos++ = b64digits[((chunk.ptr[i] & 0x03) << 4) | (chunk.ptr[i+1] >> 4)];
358 if (i+2 >= chunk.len)
359 {
360 *pos++ = b64digits[(chunk.ptr[i+1] & 0x0F) << 2];
361 *pos++ = '=';
362 break;
363 }
364 *pos++ = b64digits[((chunk.ptr[i+1] & 0x0F) << 2) | (chunk.ptr[i+2] >> 6)];
365 *pos++ = b64digits[chunk.ptr[i+2] & 0x3F];
366 }
367 *pos = '\0';
368 return chunk_create(buf, len * 4 / 3);
369 }
370
371 /**
372 * convert a base 64 digit to its binary form (inversion of b64digits array)
373 */
374 static int b642bin(char b64)
375 {
376 switch (b64)
377 {
378 case 'A' ... 'Z':
379 return b64 - 'A';
380 case 'a' ... 'z':
381 return ('Z' - 'A' + 1) + b64 - 'a';
382 case '0' ... '9':
383 return ('Z' - 'A' + 1) + ('z' - 'a' + 1) + b64 - '0';
384 case '+':
385 case '-':
386 return 62;
387 case '/':
388 case '_':
389 return 63;
390 case '=':
391 return 0;
392 default:
393 return -1;
394 }
395 }
396
397 /**
398 * Described in header.
399 */
400 chunk_t chunk_from_base64(chunk_t base64, char *buf)
401 {
402 u_char *pos, byte[4];
403 int i, j, len, outlen;
404
405 len = base64.len / 4 * 3;
406 if (!buf)
407 {
408 buf = malloc(len);
409 }
410 pos = base64.ptr;
411 outlen = 0;
412 for (i = 0; i < len; i+=3)
413 {
414 outlen += 3;
415 for (j = 0; j < 4; j++)
416 {
417 if (*pos == '=')
418 {
419 outlen--;
420 }
421 byte[j] = b642bin(*pos++);
422 }
423 buf[i] = (byte[0] << 2) | (byte[1] >> 4);
424 buf[i+1] = (byte[1] << 4) | (byte[2] >> 2);
425 buf[i+2] = (byte[2] << 6) | (byte[3]);
426 }
427 return chunk_create(buf, outlen);
428 }
429
430 /**
431 * Described in header.
432 */
433 int chunk_compare(chunk_t a, chunk_t b)
434 {
435 int compare_len = a.len - b.len;
436 int len = (compare_len < 0)? a.len : b.len;
437
438 if (compare_len != 0 || len == 0)
439 {
440 return compare_len;
441 }
442 return memcmp(a.ptr, b.ptr, len);
443 };
444
445 /**
446 * Remove non-printable characters from a chunk.
447 */
448 bool chunk_printable(chunk_t chunk, chunk_t *sane, char replace)
449 {
450 bool printable = TRUE;
451 int i;
452
453 if (sane)
454 {
455 *sane = chunk_clone(chunk);
456 }
457 for (i = 0; i < chunk.len; i++)
458 {
459 if (!isprint(chunk.ptr[i]))
460 {
461 if (sane)
462 {
463 sane->ptr[i] = replace;
464 }
465 printable = FALSE;
466 }
467 }
468 return printable;
469 }
470
471 /**
472 * Described in header.
473 *
474 * The implementation is based on Paul Hsieh's SuperFastHash:
475 * http://www.azillionmonkeys.com/qed/hash.html
476 */
477 u_int32_t chunk_hash_inc(chunk_t chunk, u_int32_t hash)
478 {
479 u_char *data = chunk.ptr;
480 size_t len = chunk.len;
481 u_int32_t tmp;
482 int rem;
483
484 if (!len || data == NULL)
485 {
486 return 0;
487 }
488
489 rem = len & 3;
490 len >>= 2;
491
492 /* Main loop */
493 for (; len > 0; --len)
494 {
495 hash += get16bits(data);
496 tmp = (get16bits(data + 2) << 11) ^ hash;
497 hash = (hash << 16) ^ tmp;
498 data += 2 * sizeof(u_int16_t);
499 hash += hash >> 11;
500 }
501
502 /* Handle end cases */
503 switch (rem)
504 {
505 case 3:
506 {
507 hash += get16bits(data);
508 hash ^= hash << 16;
509 hash ^= data[sizeof(u_int16_t)] << 18;
510 hash += hash >> 11;
511 break;
512 }
513 case 2:
514 {
515 hash += get16bits(data);
516 hash ^= hash << 11;
517 hash += hash >> 17;
518 break;
519 }
520 case 1:
521 {
522 hash += *data;
523 hash ^= hash << 10;
524 hash += hash >> 1;
525 break;
526 }
527 }
528
529 /* Force "avalanching" of final 127 bits */
530 hash ^= hash << 3;
531 hash += hash >> 5;
532 hash ^= hash << 4;
533 hash += hash >> 17;
534 hash ^= hash << 25;
535 hash += hash >> 6;
536
537 return hash;
538 }
539
540 /**
541 * Described in header.
542 */
543 u_int32_t chunk_hash(chunk_t chunk)
544 {
545 return chunk_hash_inc(chunk, chunk.len);
546 }
547
548 /**
549 * Described in header.
550 */
551 int chunk_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
552 const void *const *args)
553 {
554 chunk_t *chunk = *((chunk_t**)(args[0]));
555 bool first = TRUE;
556 chunk_t copy = *chunk;
557 int written = 0;
558
559 if (!spec->hash)
560 {
561 const void *new_args[] = {&chunk->ptr, &chunk->len};
562 return mem_printf_hook(dst, len, spec, new_args);
563 }
564
565 while (copy.len > 0)
566 {
567 if (first)
568 {
569 first = FALSE;
570 }
571 else
572 {
573 written += print_in_hook(dst, len, ":");
574 }
575 written += print_in_hook(dst, len, "%02x", *copy.ptr++);
576 copy.len--;
577 }
578 return written;
579 }