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

Contents of /trunk/Src/wptGPGMEData.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 129 - (show annotations)
Fri Dec 30 13:56:10 2005 UTC (19 years, 2 months ago) by twoaday
File size: 5167 byte(s)
2005-12-27  Timo Schulz  <ts@g10code.com>
                                                                                
        * wptListView.cpp (listview_set_view): New.
        (listview_del_column): New.
        * wptW32API.cpp (get_locale_date): New.
        (get_menu_state): New.
        (force_foreground_window): New.
        * wptVerifyList.cpp (strtimestamp): Support for
        locale date formats.
        * wptGPGUtil.cpp (gpg_revoke_cert): Handle bad
        passphrases.
        * wptKeyEditCB.cpp (editkey_command_handler): Immediately
        return when a bad passphrase was submitted.
        * wptKeyRevokersDlg.cpp (keyrevokers_dlg_proc): Change
        column order.
        * wptKeylist.cpp (keylist_upd_col): New.
        * wptKeyManagerDlg.cpp (update_ui_items): Deactivate
        'Revocation' for public keys.
        (translate_menu_strings): s/Revoke/Revoke Cert.
        (modify_listview_columns): New.


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
134 if (r_plain)
135 *r_plain = NULL;
136 err = gpgme_data_new (&plain);
137 if (err)
138 return err;
139
140 while (gpg_data_readline (sig, line, 128) > 0) {
141 if (!strncmp (line, "-----BEGIN PGP SIGNED MESSAGE", 29) ||
142 !strncmp (line, "Version:", 8) ||
143 !strncmp (line, "Comment:", 8) ||
144 !strncmp (line, "Charset:", 8) ||
145 !strncmp (line, "Hash:", 5) ||
146 !strncmp (line, "MessageID", 9))
147 continue;
148 if (strlen (line) <= 2)
149 break; /* parsed all headers, now we reached the body */
150 }
151 /* XXX handle multi dash escaped sequences */
152 while (gpg_data_readline (sig, line, 128) > 0 ) {
153 if (!strncmp( line, "-----BEGIN PGP SIGNATURE", 24))
154 break; /* end of plaintext */
155 if (!strncmp (line, "- -", 3))
156 pos = 2;
157 gpgme_data_write (plain, line+pos, strlen (line+pos));
158 pos = 0;
159 }
160 gpgme_data_write (plain, "", 1);
161 if (r_plain)
162 *r_plain = plain;
163 else
164 gpgme_data_release (plain);
165 return err;
166 }
167
168
169 /* Release the data in @dh and store the contents in the file @fname.
170 Return value: 0 on success. */
171 gpgme_error_t
172 gpg_data_release_and_set_file (gpgme_data_t dh, const char *fname)
173 {
174 char *p = NULL;
175 FILE *fp;
176 size_t n;
177
178 fp = fopen (fname, "wb");
179 if (fp == NULL)
180 return gpg_error (GPG_ERR_ENOENT);
181
182 p = gpgme_data_release_and_get_mem (dh, &n);
183 if (p) {
184 fwrite (p, 1, n, fp);
185 fflush (fp);
186 memset (p, 0xFF, n);
187 gpgme_free (p);
188 }
189 fclose (fp);
190 return 0;
191 }
192
193
194 /* Try to read a line, terminated by \n from the given
195 gpgme data object @dh into @line.
196 Return value: numbers of chars read. */
197 size_t
198 gpg_data_readline (gpgme_data_t dh, char *line, size_t nbytes)
199 {
200 char ch = 0;
201 int nread = 0, pos = 0;
202
203 if (!dh)
204 return 0;
205
206 memset (line, 0, nbytes);
207 while ((nread=gpgme_data_read (dh, &ch, 1)) > 0) {
208 if (!nread)
209 break;
210 if (ch == '\n') {
211 line[pos++] = ch;
212 line[pos++] = '\0';
213 break;
214 }
215 line[pos++] = ch;
216 if (pos > (int)nbytes) {
217 line[pos++] = '\0';
218 break;
219 }
220 }
221
222 return pos;
223 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26