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

Contents of /trunk/Src/wptGPGMEData.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 121 - (show annotations)
Mon Dec 12 11:19:56 2005 UTC (19 years, 2 months ago) by twoaday
File size: 5190 byte(s)
2005-12-11  Timo Schulz  <ts@g10code.com>
 
        * wptW32API.cpp (get_file_version): New.
        * wptGPGUtil.cpp (create_process): Always hide window.
        * wptClipEditDlg.cpp (clipedit_dlg_proc): Use 'Close'
        instead of 'Exit'.
        * wptKeyManager.cpp (km_http_import): New filename
        generation code.
        (km_send_to_mail_recipient): Cleanups.
        * wptKeyEditDlg.cpp (showpref_dlg_proc): Localize dialog.
        * wptKeyManagerDlg.cpp (update_ui_items): Handle the case
        when multiple keys are selected.
        (popup_multiple): New.
        * WinPT.cpp (WinMain): Check that the PTD.dll and WinPT.exe
        file versions are equal. Rewrote --keymanager code.
         
Removed temporary w32gpgme dirctory, all code is now in Src.
Changed configure files.


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26