1 /* b64enc.c - Simple Base64 encoder.
2 * Copyright (C) 2001, 2003, 2004 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
30 #define B64ENC_DID_HEADER 1
31 #define B64ENC_DID_TRAILER 2
32 #define B64ENC_NO_LINEFEEDS 16
35 /* The base-64 character list */
36 static unsigned char bintoasc
[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
37 "abcdefghijklmnopqrstuvwxyz"
40 /* Prepare for base-64 writing to the stream FP. If TITLE is not NULL
41 and not an empty string, this string will be used as the title for
42 the armor lines, with TITLE being an empty string, we don't write
43 the header lines and furthermore even don't write any linefeeds.
44 With TITLE beeing NULL, we merely don't write header but make sure
45 that lines are not too long. Note, that we don't write any output
46 unless at least one byte get written using b64enc_write. */
48 b64enc_start (struct b64state
*state
, FILE *fp
, const char *title
)
50 memset (state
, 0, sizeof *state
);
53 state
->flags
|= B64ENC_NO_LINEFEEDS
;
56 state
->title
= xtrystrdup (title
);
58 return gpg_error_from_syserror ();
64 /* Write NBYTES from BUFFER to the Base 64 stream identified by
65 STATE. With BUFFER and NBYTES being 0, merely do a fflush on the
68 b64enc_write (struct b64state
*state
, const void *buffer
, size_t nbytes
)
70 unsigned char radbuf
[4];
72 const unsigned char *p
;
78 if (buffer
&& fflush (fp
))
83 if (!(state
->flags
& B64ENC_DID_HEADER
))
87 if ( fputs ("-----BEGIN ", fp
) == EOF
88 || fputs (state
->title
, fp
) == EOF
89 || fputs ("-----\n", fp
) == EOF
)
92 state
->flags
|= B64ENC_DID_HEADER
;
96 quad_count
= state
->quad_count
;
98 memcpy (radbuf
, state
->radbuf
, idx
);
100 for (p
=buffer
; nbytes
; p
++, nbytes
--)
107 tmp
[0] = bintoasc
[(*radbuf
>> 2) & 077];
108 tmp
[1] = bintoasc
[(((*radbuf
<<4)&060)|((radbuf
[1] >> 4)&017))&077];
109 tmp
[2] = bintoasc
[(((radbuf
[1]<<2)&074)|((radbuf
[2]>>6)&03))&077];
110 tmp
[3] = bintoasc
[radbuf
[2]&077];
111 for (idx
=0; idx
< 4; idx
++)
116 if (++quad_count
>= (64/4))
119 if (!(state
->flags
& B64ENC_NO_LINEFEEDS
)
120 && fputs ("\n", fp
) == EOF
)
125 memcpy (state
->radbuf
, radbuf
, idx
);
127 state
->quad_count
= quad_count
;
131 return gpg_error_from_syserror ();
135 b64enc_finish (struct b64state
*state
)
138 unsigned char radbuf
[4];
142 if (!(state
->flags
& B64ENC_DID_HEADER
))
145 /* Flush the base64 encoding */
148 quad_count
= state
->quad_count
;
150 memcpy (radbuf
, state
->radbuf
, idx
);
156 tmp
[0] = bintoasc
[(*radbuf
>>2)&077];
159 tmp
[1] = bintoasc
[((*radbuf
<< 4) & 060) & 077];
165 tmp
[1] = bintoasc
[(((*radbuf
<<4)&060)|((radbuf
[1]>>4)&017))&077];
166 tmp
[2] = bintoasc
[((radbuf
[1] << 2) & 074) & 077];
169 for (idx
=0; idx
< 4; idx
++)
175 if (++quad_count
>= (64/4))
178 if (!(state
->flags
& B64ENC_NO_LINEFEEDS
)
179 && fputs ("\n", fp
) == EOF
)
184 /* Finish the last line and write the trailer. */
186 && !(state
->flags
& B64ENC_NO_LINEFEEDS
)
187 && fputs ("\n", fp
) == EOF
)
192 if ( fputs ("-----END ", fp
) == EOF
193 || fputs (state
->title
, fp
) == EOF
194 || fputs ("-----\n", fp
) == EOF
)
201 err
= gpg_error_from_syserror ();
206 xfree (state
->title
);