1 |
/* wptGPGMEData.cpp - WinPT specifc data extensions |
2 |
* Copyright (C) 2001-2006 Timo Schulz |
3 |
* |
4 |
* This file is part of WinPT. |
5 |
* |
6 |
* WinPT is free software; you can redistribute it and/or |
7 |
* modify it under the terms of the GNU General Public License |
8 |
* as published by the Free Software Foundation; either version 2 |
9 |
* of the License, or (at your option) any later version. |
10 |
* |
11 |
* WinPT 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 GNU |
14 |
* General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with WinPT; if not, write to the Free Software Foundation, |
18 |
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
*/ |
20 |
#ifdef HAVE_CONFIG_H |
21 |
#include <config.h> |
22 |
#endif |
23 |
|
24 |
#include <windows.h> |
25 |
#include <malloc.h> |
26 |
#include <string.h> |
27 |
#include <stdio.h> |
28 |
#include <gpgme.h> |
29 |
|
30 |
#include "wptUtil.h" |
31 |
#include "wptGPG.h" |
32 |
|
33 |
|
34 |
/* Implement a word based line break. @inp is the input buffer |
35 |
and @wraplen is the maximal line length. |
36 |
Return value: the wrapped buffer on success. */ |
37 |
char* |
38 |
wrap_lines (char *inp, size_t inlen, size_t wraplen, size_t *r_len) |
39 |
{ |
40 |
char *out; |
41 |
char *p, *inp_ptr; |
42 |
int end_is_le=0, add_le=0; |
43 |
size_t n; |
44 |
|
45 |
inp[inlen] = 0; |
46 |
if (inp[inlen-2] == '\r' && inp[inlen-1] == '\n') |
47 |
end_is_le = 1; |
48 |
out = (char *)calloc (1, 4*inlen/3+2+1); |
49 |
if (!out) |
50 |
return NULL; |
51 |
n = 0; |
52 |
inp_ptr = inp; |
53 |
while ((p = strsep (&inp_ptr, " \n"))) { |
54 |
if (strlen (p) == 0) { |
55 |
strcat (out, " "); |
56 |
n++; |
57 |
continue; |
58 |
} |
59 |
/* if we find an existing \r\n pair, we generate a break |
60 |
at the given position and reset the counter. */ |
61 |
if (p[strlen (p)-1] == '\r') { |
62 |
p[strlen (p)-1]=0; |
63 |
add_le=1; |
64 |
} |
65 |
else |
66 |
add_le=0; |
67 |
if (n + strlen (p) > wraplen) { |
68 |
strcat (out, "\r\n"); |
69 |
n = 0; |
70 |
} |
71 |
else if (n > 0 || add_le) { |
72 |
strcat (out, " "); |
73 |
n++; |
74 |
} |
75 |
|
76 |
n += strlen (p); |
77 |
strcat (out, p); |
78 |
if (add_le) { |
79 |
strcat (out, "\r\n"); |
80 |
n = 0; |
81 |
} |
82 |
} |
83 |
if (end_is_le) |
84 |
strcat (out, "\r\n"); |
85 |
*r_len = strlen (out); |
86 |
return out; |
87 |
} |
88 |
|
89 |
|
90 |
/* Wrap the lines of @r_dh with a line length of @wraplen. |
91 |
@r_dh will be released and on success it contains the |
92 |
handle to the wrapped data. |
93 |
Return value: 0 on success. */ |
94 |
gpgme_error_t |
95 |
gpg_data_wrap_lines (gpgme_data_t *r_dh, size_t wraplen) |
96 |
{ |
97 |
gpgme_error_t err; |
98 |
gpgme_data_t mdh; |
99 |
char *raw = NULL; |
100 |
char *p = NULL; |
101 |
size_t nlen; |
102 |
|
103 |
err = gpgme_data_new (&mdh); |
104 |
if (err) |
105 |
return err; |
106 |
|
107 |
raw = gpgme_data_release_and_get_mem (*r_dh, &nlen); |
108 |
if (!raw) { |
109 |
gpgme_data_release (mdh); |
110 |
return gpg_error (GPG_ERR_INV_ARG); |
111 |
} |
112 |
|
113 |
p = wrap_lines (raw, nlen, wraplen, &nlen); |
114 |
if (p) { |
115 |
gpgme_data_write (mdh, p, nlen); |
116 |
gpgme_data_rewind (mdh); |
117 |
free (p); |
118 |
} |
119 |
gpgme_free (raw); |
120 |
*r_dh = mdh; |
121 |
return 0; |
122 |
} |
123 |
|
124 |
|
125 |
/* Prepend '> ' to each line into @r_dh. On success @r_dh |
126 |
contains a handle to the quoted data. |
127 |
Return value: 0 on success. */ |
128 |
gpgme_error_t |
129 |
gpg_data_mail_quote (gpgme_data_t *r_dh) |
130 |
{ |
131 |
gpgme_data_t dh; |
132 |
char buf[128]; |
133 |
|
134 |
if (!*r_dh) |
135 |
return gpg_error (GPG_ERR_INV_ARG); |
136 |
gpgme_data_new (&dh); |
137 |
while (gpg_data_readline (*r_dh, buf, sizeof (buf)-1)) { |
138 |
gpgme_data_write (dh, "> ", 2); |
139 |
gpgme_data_write (dh, buf, strlen (buf)); |
140 |
} |
141 |
gpgme_data_release (*r_dh); |
142 |
*r_dh = dh; |
143 |
return 0; |
144 |
} |
145 |
|
146 |
|
147 |
static int |
148 |
is_armor_header (const char *line) |
149 |
{ |
150 |
const char *header[] = { |
151 |
"Version:", |
152 |
"Comment:", |
153 |
"Charset:", |
154 |
"Hash:", |
155 |
"MessageID", |
156 |
NULL |
157 |
}; |
158 |
int i; |
159 |
|
160 |
for (i=0; header[i] != NULL; i++) { |
161 |
if (!strncmp (line, header[i], strlen (header[i]))) |
162 |
return -1; |
163 |
} |
164 |
return 0; |
165 |
} |
166 |
|
167 |
|
168 |
|
169 |
/* Extract the plaintext data from the escaped data object @sig. |
170 |
The plaintxt is stored in @r_plain. |
171 |
Return value: 0 on success. */ |
172 |
gpgme_error_t |
173 |
gpg_data_extract_plaintext (gpgme_data_t sig, gpgme_data_t *r_plain) |
174 |
{ |
175 |
gpgme_data_t plain; |
176 |
gpgme_error_t err; |
177 |
char line[128+32]; |
178 |
int pos = 0; |
179 |
|
180 |
if (r_plain) |
181 |
*r_plain = NULL; |
182 |
err = gpgme_data_new (&plain); |
183 |
if (err) |
184 |
return err; |
185 |
|
186 |
while (gpg_data_readline (sig, line, 128) > 0) { |
187 |
if (!strncmp (line, "-----BEGIN PGP SIGNED MESSAGE", 29) || |
188 |
is_armor_header (line)) |
189 |
continue; |
190 |
if (strlen (line) <= 2) |
191 |
break; /* parsed all headers, now we reached the body */ |
192 |
} |
193 |
|
194 |
/* We just support 1 nesting level. */ |
195 |
while (gpg_data_readline (sig, line, 128) > 0 ) { |
196 |
if (!strncmp( line, "-----BEGIN PGP SIGNATURE", 24)) |
197 |
break; /* end of plaintext */ |
198 |
if (!strncmp (line, "- -", 3)) |
199 |
pos = 2; |
200 |
gpgme_data_write (plain, line+pos, strlen (line+pos)); |
201 |
pos = 0; |
202 |
} |
203 |
gpgme_data_write (plain, "", 1); |
204 |
if (r_plain) |
205 |
*r_plain = plain; |
206 |
else |
207 |
gpgme_data_release (plain); |
208 |
return err; |
209 |
} |
210 |
|
211 |
|
212 |
/* Release the data in @dh and store the contents in the file @fname. |
213 |
Return value: 0 on success. */ |
214 |
gpgme_error_t |
215 |
gpg_data_release_and_set_file (gpgme_data_t dh, const char *fname) |
216 |
{ |
217 |
char *p = NULL; |
218 |
FILE *fp; |
219 |
size_t n; |
220 |
|
221 |
fp = fopen (fname, "wb"); |
222 |
if (fp == NULL) |
223 |
return gpg_error (GPG_ERR_ENOENT); |
224 |
|
225 |
p = gpgme_data_release_and_get_mem (dh, &n); |
226 |
if (p) { |
227 |
fwrite (p, 1, n, fp); |
228 |
fflush (fp); |
229 |
memset (p, 0xFF, n); |
230 |
gpgme_free (p); |
231 |
} |
232 |
fclose (fp); |
233 |
return 0; |
234 |
} |
235 |
|
236 |
|
237 |
/* Try to read a line, terminated by \n from the given |
238 |
gpgme data object @dh into @line. |
239 |
Return value: numbers of chars read. */ |
240 |
size_t |
241 |
gpg_data_readline (gpgme_data_t dh, char *line, size_t nbytes) |
242 |
{ |
243 |
char ch = 0; |
244 |
int nread = 0, pos = 0; |
245 |
|
246 |
if (!dh) |
247 |
return 0; |
248 |
|
249 |
memset (line, 0, nbytes); |
250 |
while ((nread=gpgme_data_read (dh, &ch, 1)) > 0) { |
251 |
if (!nread) |
252 |
break; |
253 |
if (ch == '\n') { |
254 |
line[pos++] = ch; |
255 |
line[pos++] = '\0'; |
256 |
break; |
257 |
} |
258 |
line[pos++] = ch; |
259 |
if (pos > (int)nbytes) { |
260 |
line[pos++] = '\0'; |
261 |
break; |
262 |
} |
263 |
} |
264 |
|
265 |
return pos; |
266 |
} |
267 |
|
268 |
|
269 |
/* Write a single byte to data object @hd. */ |
270 |
void |
271 |
gpg_data_putc (gpgme_data_t hd, int c) |
272 |
{ |
273 |
BYTE ch[2]; |
274 |
|
275 |
ch[0] = (BYTE)c; |
276 |
ch[1] = '\0'; |
277 |
gpgme_data_write (hd, ch, 1); |
278 |
} |