]>
git.ipfire.org Git - people/ms/strongswan.git/blob - src/libfreeswan/libfreeswan/datatot.c
2 * convert from binary data (e.g. key) to text form
3 * Copyright (C) 2000 Henry Spencer.
5 * This library is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU Library General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 * License for more details.
15 * RCSID $Id: datatot.c,v 1.1 2004/03/15 20:35:26 as Exp $
20 static void convert(const char *src
, size_t nreal
, int format
, char *out
);
23 - datatot - convert data bytes to text
25 size_t /* true length (with NUL) for success */
26 datatot(src
, srclen
, format
, dst
, dstlen
)
29 int format
; /* character indicating what format */
30 char *dst
; /* need not be valid if dstlen is 0 */
33 size_t inblocksize
; /* process this many bytes at a time */
34 size_t outblocksize
; /* producing this many */
35 size_t breakevery
; /* add a _ every this many (0 means don't) */
36 size_t sincebreak
; /* output bytes since last _ */
37 char breakchar
; /* character used to break between groups */
38 char inblock
[10]; /* enough for any format */
39 char outblock
[10]; /* enough for any format */
40 char fake
[1]; /* fake output area for dstlen == 0 */
41 size_t needed
; /* return value */
42 char *stop
; /* where the terminating NUL will go */
43 size_t ntodo
; /* remaining input */
78 case 64: /* beware, equals ' ' */
88 assert(inblocksize
< sizeof(inblock
));
89 assert(outblocksize
< sizeof(outblock
));
90 assert(breakevery
% outblocksize
== 0);
96 if (dstlen
== 0) { /* dispose of awkward special case */
100 stop
= dst
+ dstlen
- 1;
102 nreal
= strlen(prefix
);
103 needed
= nreal
; /* for starters */
104 if (dstlen
<= nreal
) { /* prefix won't fit */
105 strncpy(dst
, prefix
, dstlen
- 1);
115 if (ntodo
< inblocksize
) { /* incomplete input */
116 memset(inblock
, 0, sizeof(inblock
));
117 memcpy(inblock
, src
, ntodo
);
123 out
= (outblocksize
> stop
- dst
) ? outblock
: dst
;
125 convert(src
, nreal
, format
, out
);
126 needed
+= outblocksize
;
127 sincebreak
+= outblocksize
;
130 assert(outblocksize
> stop
- dst
);
131 memcpy(dst
, out
, stop
- dst
);
138 ntodo
-= inblocksize
;
139 if (breakevery
!= 0 && sincebreak
>= breakevery
&& ntodo
> 0) {
155 - convert - convert one input block to one output block
158 convert(src
, nreal
, format
, out
)
160 size_t nreal
; /* how much of the input block is real */
164 static char hex
[] = "0123456789abcdef";
165 static char base64
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
166 "abcdefghijklmnopqrstuvwxyz"
169 unsigned char c1
, c2
, c3
;
175 c
= (unsigned char)*src
;
176 *out
++ = hex
[c
>> 4];
177 *out
++ = hex
[c
& 0xf];
180 c1
= (unsigned char)*src
++;
181 c2
= (unsigned char)*src
++;
182 c3
= (unsigned char)*src
++;
183 *out
++ = base64
[c1
>> 2]; /* top 6 bits of c1 */
184 c
= (c1
& 0x3) << 4; /* bottom 2 of c1... */
185 c
|= c2
>> 4; /* ...top 4 of c2 */
190 c
= (c2
& 0xf) << 2; /* bottom 4 of c2... */
191 c
|= c3
>> 6; /* ...top 2 of c3 */
197 *out
++ = base64
[c3
& 0x3f]; /* bottom 6 of c3 */
200 assert(nreal
== 0); /* unknown format */
206 - datatoa - convert data to ASCII
207 * backward-compatibility synonym for datatot
209 size_t /* true length (with NUL) for success */
210 datatoa(src
, srclen
, format
, dst
, dstlen
)
213 int format
; /* character indicating what format */
214 char *dst
; /* need not be valid if dstlen is 0 */
217 return datatot(src
, srclen
, format
, dst
, dstlen
);
221 - bytestoa - convert data bytes to ASCII
222 * backward-compatibility synonym for datatot
224 size_t /* true length (with NUL) for success */
225 bytestoa(src
, srclen
, format
, dst
, dstlen
)
228 int format
; /* character indicating what format */
229 char *dst
; /* need not be valid if dstlen is 0 */
232 return datatot(src
, srclen
, format
, dst
, dstlen
);