1 |
/* wptGPGMEData.cpp - WinPT specifc data extensions |
2 |
* Copyright (C) 2001-2005 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 |
/* Implement a word based line break. @inp is the input buffer |
34 |
and @wraplen is the maximal line length. |
35 |
Return value: the wrapped buffer on success. */ |
36 |
char* |
37 |
wrap_lines (char *inp, size_t inlen, size_t wraplen, size_t *r_len) |
38 |
{ |
39 |
char *out; |
40 |
char *p, *inp_ptr; |
41 |
int end_is_le=0, add_le=0; |
42 |
size_t n; |
43 |
|
44 |
inp[inlen] = 0; |
45 |
if (inp[inlen-2] == '\r' && inp[inlen-1] == '\n') |
46 |
end_is_le = 1; |
47 |
out = (char *)calloc (1, 4*inlen/3+2+1); |
48 |
if (!out) |
49 |
return NULL; |
50 |
n = 0; |
51 |
inp_ptr = inp; |
52 |
while ((p = strsep (&inp_ptr, " \n"))) { |
53 |
if (strlen (p) == 0) { |
54 |
strcat (out, " "); |
55 |
n++; |
56 |
continue; |
57 |
} |
58 |
/* if we find an existing \r\n pair, we generate a break |
59 |
at the given position and reset the counter. */ |
60 |
if (p[strlen (p)-1] == '\r') { |
61 |
p[strlen (p)-1]=0; |
62 |
add_le=1; |
63 |
} |
64 |
else |
65 |
add_le=0; |
66 |
if (n + strlen (p) > wraplen) { |
67 |
strcat (out, "\r\n"); |
68 |
n = 0; |
69 |
} |
70 |
else if (n > 0 || add_le) { |
71 |
strcat (out, " "); |
72 |
n++; |
73 |
} |
74 |
|
75 |
n += strlen (p); |
76 |
strcat (out, p); |
77 |
if (add_le) { |
78 |
strcat (out, "\r\n"); |
79 |
n = 0; |
80 |
} |
81 |
} |
82 |
if (end_is_le) |
83 |
strcat (out, "\r\n"); |
84 |
*r_len = strlen (out); |
85 |
return out; |
86 |
} |
87 |
|
88 |
|
89 |
/* Wrap the lines of @r_dh with a line length of @wraplen. |
90 |
@r_dh will be released and on success it contains the |
91 |
handle to the wrapped data. |
92 |
Return value: 0 on success. */ |
93 |
gpgme_error_t |
94 |
gpg_data_wrap_lines (gpgme_data_t *r_dh, size_t wraplen) |
95 |
{ |
96 |
gpgme_error_t err; |
97 |
gpgme_data_t mdh; |
98 |
char *raw = NULL; |
99 |
char *p = NULL; |
100 |
size_t nlen; |
101 |
|
102 |
err = gpgme_data_new (&mdh); |
103 |
if (err) |
104 |
return err; |
105 |
|
106 |
raw = gpgme_data_release_and_get_mem (*r_dh, &nlen); |
107 |
if (!raw) { |
108 |
gpgme_data_release (mdh); |
109 |
return gpg_error (GPG_ERR_INV_ARG); |
110 |
} |
111 |
|
112 |
p = wrap_lines (raw, nlen, wraplen, &nlen); |
113 |
if (p) { |
114 |
gpgme_data_write (mdh, p, nlen); |
115 |
gpgme_data_rewind (mdh); |
116 |
free (p); |
117 |
} |
118 |
gpgme_free (raw); |
119 |
*r_dh = mdh; |
120 |
return 0; |
121 |
} |
122 |
|
123 |
|
124 |
/* Prepend '> ' to each line into @r_dh. On success @r_dh |
125 |
contains a handle to the quoted data. |
126 |
Return value: 0 on success. */ |
127 |
gpgme_error_t |
128 |
gpg_data_mail_quote (gpgme_data_t *r_dh) |
129 |
{ |
130 |
gpgme_data_t dh; |
131 |
char buf[128]; |
132 |
|
133 |
if (!*r_dh) |
134 |
return gpg_error (GPG_ERR_INV_ARG); |
135 |
gpgme_data_new (&dh); |
136 |
while (gpg_data_readline (*r_dh, buf, 127)) { |
137 |
gpgme_data_write (dh, "> ", 2); |
138 |
gpgme_data_write (dh, buf, strlen (buf)); |
139 |
} |
140 |
gpgme_data_release (*r_dh); |
141 |
*r_dh = dh; |
142 |
return 0; |
143 |
} |
144 |
|
145 |
|
146 |
/* Extract the plaintext data from the escaped data object @sig. |
147 |
The plaintxt is stored in @r_plain. |
148 |
Return value: 0 on success. */ |
149 |
gpgme_error_t |
150 |
gpg_data_extract_plaintext (gpgme_data_t sig, gpgme_data_t *r_plain) |
151 |
{ |
152 |
gpgme_data_t plain; |
153 |
gpgme_error_t err; |
154 |
char line[128+32]; |
155 |
int pos = 0; |
156 |
|
157 |
if (r_plain) |
158 |
*r_plain = NULL; |
159 |
err = gpgme_data_new (&plain); |
160 |
if (err) |
161 |
return err; |
162 |
|
163 |
while (gpg_data_readline (sig, line, 128) > 0) { |
164 |
if (!strncmp (line, "-----BEGIN PGP SIGNED MESSAGE", 29) || |
165 |
!strncmp (line, "Version:", 8) || |
166 |
!strncmp (line, "Comment:", 8) || |
167 |
!strncmp (line, "Charset:", 8) || |
168 |
!strncmp (line, "Hash:", 5) || |
169 |
!strncmp (line, "MessageID", 9)) |
170 |
continue; |
171 |
if (strlen (line) <= 2) |
172 |
break; /* parsed all headers, now we reached the body */ |
173 |
} |
174 |
/* XXX handle multi dash escaped sequences */ |
175 |
while (gpg_data_readline (sig, line, 128) > 0 ) { |
176 |
if (!strncmp( line, "-----BEGIN PGP SIGNATURE", 24)) |
177 |
break; /* end of plaintext */ |
178 |
if (!strncmp (line, "- -", 3)) |
179 |
pos = 2; |
180 |
gpgme_data_write (plain, line+pos, strlen (line+pos)); |
181 |
pos = 0; |
182 |
} |
183 |
gpgme_data_write (plain, "", 1); |
184 |
if (r_plain) |
185 |
*r_plain = plain; |
186 |
else |
187 |
gpgme_data_release (plain); |
188 |
return err; |
189 |
} |
190 |
|
191 |
|
192 |
/* Release the data in @dh and store the contents in the file @fname. |
193 |
Return value: 0 on success. */ |
194 |
gpgme_error_t |
195 |
gpg_data_release_and_set_file (gpgme_data_t dh, const char *fname) |
196 |
{ |
197 |
char *p = NULL; |
198 |
FILE *fp; |
199 |
size_t n; |
200 |
|
201 |
fp = fopen (fname, "wb"); |
202 |
if (fp == NULL) |
203 |
return gpg_error (GPG_ERR_ENOENT); |
204 |
|
205 |
p = gpgme_data_release_and_get_mem (dh, &n); |
206 |
if (p) { |
207 |
fwrite (p, 1, n, fp); |
208 |
fflush (fp); |
209 |
memset (p, 0xFF, n); |
210 |
gpgme_free (p); |
211 |
} |
212 |
fclose (fp); |
213 |
return 0; |
214 |
} |
215 |
|
216 |
|
217 |
/* Try to read a line, terminated by \n from the given |
218 |
gpgme data object @dh into @line. |
219 |
Return value: numbers of chars read. */ |
220 |
size_t |
221 |
gpg_data_readline (gpgme_data_t dh, char *line, size_t nbytes) |
222 |
{ |
223 |
char ch = 0; |
224 |
int nread = 0, pos = 0; |
225 |
|
226 |
if (!dh) |
227 |
return 0; |
228 |
|
229 |
memset (line, 0, nbytes); |
230 |
while ((nread=gpgme_data_read (dh, &ch, 1)) > 0) { |
231 |
if (!nread) |
232 |
break; |
233 |
if (ch == '\n') { |
234 |
line[pos++] = ch; |
235 |
line[pos++] = '\0'; |
236 |
break; |
237 |
} |
238 |
line[pos++] = ch; |
239 |
if (pos > (int)nbytes) { |
240 |
line[pos++] = '\0'; |
241 |
break; |
242 |
} |
243 |
} |
244 |
|
245 |
return pos; |
246 |
} |