/[winpt]/trunk/Src/wptGPGMEData.cpp
ViewVC logotype

Annotation of /trunk/Src/wptGPGMEData.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 247 - (hide annotations)
Fri Jul 21 08:19:24 2006 UTC (18 years, 7 months ago) by twoaday
File size: 6366 byte(s)


1 twoaday 133 /* wptGPGMEData.cpp - WinPT specifc data extensions
2 twoaday 200 * Copyright (C) 2001-2006 Timo Schulz
3 twoaday 133 *
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 twoaday 121 #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 twoaday 200
34 twoaday 121 /* 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 twoaday 193 while (gpg_data_readline (*r_dh, buf, sizeof (buf)-1)) {
138 twoaday 121 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 twoaday 200 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 twoaday 121 /* Extract the plaintext data from the escaped data object @sig.
169     The plaintxt is stored in @r_plain.
170     Return value: 0 on success. */
171     gpgme_error_t
172     gpg_data_extract_plaintext (gpgme_data_t sig, gpgme_data_t *r_plain)
173     {
174     gpgme_data_t plain;
175     gpgme_error_t err;
176     char line[128+32];
177     int pos = 0;
178    
179     if (r_plain)
180     *r_plain = NULL;
181     err = gpgme_data_new (&plain);
182     if (err)
183     return err;
184    
185     while (gpg_data_readline (sig, line, 128) > 0) {
186     if (!strncmp (line, "-----BEGIN PGP SIGNED MESSAGE", 29) ||
187 twoaday 200 is_armor_header (line))
188 twoaday 121 continue;
189     if (strlen (line) <= 2)
190     break; /* parsed all headers, now we reached the body */
191     }
192 twoaday 200
193     /* We just support 1 nesting level. */
194 twoaday 121 while (gpg_data_readline (sig, line, 128) > 0 ) {
195 twoaday 247 if (!strncmp (line, "-----BEGIN PGP SIGNATURE", 24))
196 twoaday 121 break; /* end of plaintext */
197     if (!strncmp (line, "- -", 3))
198     pos = 2;
199     gpgme_data_write (plain, line+pos, strlen (line+pos));
200     pos = 0;
201     }
202     gpgme_data_write (plain, "", 1);
203     if (r_plain)
204     *r_plain = plain;
205     else
206     gpgme_data_release (plain);
207     return err;
208     }
209    
210    
211     /* Release the data in @dh and store the contents in the file @fname.
212     Return value: 0 on success. */
213     gpgme_error_t
214     gpg_data_release_and_set_file (gpgme_data_t dh, const char *fname)
215     {
216     char *p = NULL;
217     FILE *fp;
218     size_t n;
219    
220     fp = fopen (fname, "wb");
221     if (fp == NULL)
222     return gpg_error (GPG_ERR_ENOENT);
223    
224     p = gpgme_data_release_and_get_mem (dh, &n);
225     if (p) {
226     fwrite (p, 1, n, fp);
227     fflush (fp);
228     memset (p, 0xFF, n);
229     gpgme_free (p);
230     }
231     fclose (fp);
232     return 0;
233     }
234    
235    
236     /* Try to read a line, terminated by \n from the given
237     gpgme data object @dh into @line.
238     Return value: numbers of chars read. */
239     size_t
240     gpg_data_readline (gpgme_data_t dh, char *line, size_t nbytes)
241     {
242     char ch = 0;
243     int nread = 0, pos = 0;
244    
245     if (!dh)
246     return 0;
247    
248     memset (line, 0, nbytes);
249     while ((nread=gpgme_data_read (dh, &ch, 1)) > 0) {
250     if (!nread)
251     break;
252     if (ch == '\n') {
253     line[pos++] = ch;
254     line[pos++] = '\0';
255     break;
256     }
257     line[pos++] = ch;
258     if (pos > (int)nbytes) {
259     line[pos++] = '\0';
260     break;
261     }
262     }
263    
264     return pos;
265     }
266 twoaday 144
267    
268 twoaday 226
269 twoaday 144 /* Write a single byte to data object @hd. */
270     void
271     gpg_data_putc (gpgme_data_t hd, int c)
272     {
273 twoaday 162 BYTE ch[2];
274 twoaday 144
275     ch[0] = (BYTE)c;
276     ch[1] = '\0';
277     gpgme_data_write (hd, ch, 1);
278     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26