Mercurial > dovecot > core-2.2
annotate src/lib/hex-binary.c @ 1741:9df02b1533b3 HEAD
Removed most of the license comments from src/lib/*.c. It's just fine to
keep them in a single COPYING.MIT file. Changed a few other comments as well.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Wed, 27 Aug 2003 00:18:16 +0300 |
parents | fd8888f6f037 |
children | 05b8b6963b02 |
rev | line source |
---|---|
1741
9df02b1533b3
Removed most of the license comments from src/lib/*.c. It's just fine to
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
1 /* Copyright (c) 2002-2003 Timo Sirainen */ |
0 | 2 |
3 #include "lib.h" | |
765
553f050c8313
Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
183
diff
changeset
|
4 #include "buffer.h" |
0 | 5 #include "hex-binary.h" |
6 | |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
7 const char *binary_to_hex(const unsigned char *data, size_t size) |
0 | 8 { |
9 char *buf, *p; | |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
10 size_t i; |
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
11 int value; |
0 | 12 |
805
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
765
diff
changeset
|
13 /* @UNSAFE */ |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
765
diff
changeset
|
14 |
0 | 15 buf = p = t_malloc(size * 2 + 1); |
16 for (i = 0; i < size; i++) { | |
17 value = data[i] >> 4; | |
18 *p++ = value < 10 ? value + '0' : value - 10 + 'a'; | |
19 | |
20 value = data[i] & 0x0f; | |
21 *p++ = value < 10 ? value + '0' : value - 10 + 'a'; | |
22 } | |
23 | |
24 *p = '\0'; | |
25 return buf; | |
26 } | |
27 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
28 int hex_to_binary(const char *data, buffer_t *dest) |
0 | 29 { |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
30 int value; |
0 | 31 |
32 while (*data != '\0') { | |
33 if (*data >= '0' && *data <= '9') | |
34 value = (*data - '0') << 4; | |
35 else if (*data >= 'a' && *data <= 'f') | |
36 value = (*data - 'a' + 10) << 4; | |
37 else if (*data >= 'A' && *data <= 'F') | |
38 value = (*data - 'A' + 10) << 4; | |
39 else | |
40 return -1; | |
41 | |
42 data++; | |
43 if (*data >= '0' && *data <= '9') | |
44 value |= *data - '0'; | |
45 else if (*data >= 'a' && *data <= 'f') | |
46 value |= *data - 'a' + 10; | |
47 else if (*data >= 'A' && *data <= 'F') | |
48 value |= *data - 'A' + 10; | |
49 else | |
50 return -1; | |
51 | |
765
553f050c8313
Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
183
diff
changeset
|
52 if (buffer_append_c(dest, value) != 1) |
553f050c8313
Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
183
diff
changeset
|
53 return 0; |
0 | 54 data++; |
55 } | |
56 | |
765
553f050c8313
Added buffer API. Point is to hide all buffer writing behind this API which
Timo Sirainen <tss@iki.fi>
parents:
183
diff
changeset
|
57 return 1; |
0 | 58 } |