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

Diff of /trunk/Src/wptGPGMEData.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 121 by twoaday, Mon Dec 12 11:19:56 2005 UTC revision 455 by twoaday, Thu Aug 2 20:01:53 2012 UTC
# Line 1  Line 1 
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>  #include <windows.h>
25  #include <malloc.h>  #include <malloc.h>
26  #include <string.h>  #include <string.h>
# Line 6  Line 29 
29    
30  #include "wptUtil.h"  #include "wptUtil.h"
31  #include "wptGPG.h"  #include "wptGPG.h"
32    #include "wptTypes.h"
33    
34    /* Default buffer size */
35    #define BUFSIZE 128
36    
37    
38  /* Implement a word based line break. @inp is the input buffer  /* Implement a word based line break. @inp is the input buffer
39     and @wraplen is the maximal line length.     and @wraplen is the maximal line length.
# Line 19  wrap_lines (char *inp, size_t inlen, siz Line 47  wrap_lines (char *inp, size_t inlen, siz
47      size_t n;      size_t n;
48    
49      inp[inlen] = 0;      inp[inlen] = 0;
50      if (inp[inlen-2] == '\r' && inp[inlen-1] == '\n')      if (inlen > 2 && inp[inlen-2] == '\r' && inp[inlen-1] == '\n')
51          end_is_le = 1;          end_is_le = 1;
52      out = (char *)calloc (1, 4*inlen/3+2+1);      out = (char *)calloc (1, 4*inlen/3+2+1);
53      if (!out)      if (!out)
# Line 105  gpgme_error_t Line 133  gpgme_error_t
133  gpg_data_mail_quote (gpgme_data_t *r_dh)  gpg_data_mail_quote (gpgme_data_t *r_dh)
134  {  {
135      gpgme_data_t dh;      gpgme_data_t dh;
136      char buf[128];      char buf[BUFSIZE+1];
137    
138      if (!*r_dh)      if (!*r_dh)
139          return gpg_error (GPG_ERR_INV_ARG);          return gpg_error (GPG_ERR_INV_ARG);
140      gpgme_data_new (&dh);      gpgme_data_new (&dh);
141      while (gpg_data_readline (*r_dh, buf, 127)) {      while (gpg_data_readline (*r_dh, buf, DIM (buf)-1)) {
142          gpgme_data_write (dh, "> ", 2);          gpgme_data_write (dh, "> ", 2);
143          gpgme_data_write (dh, buf, strlen (buf));          gpgme_data_write (dh, buf, strlen (buf));
144      }      }
# Line 120  gpg_data_mail_quote (gpgme_data_t *r_dh) Line 148  gpg_data_mail_quote (gpgme_data_t *r_dh)
148  }  }
149    
150    
151    static int
152    is_armor_header (const char *line)
153    {
154        const char *header[] = {
155            "Version:",
156            "Comment:",
157            "Charset:",
158            "Hash:",
159            "MessageID:",
160            NULL
161        };
162    
163        for (size_t i=0; header[i] != NULL; i++) {
164            if (!strncmp (line, header[i], strlen (header[i])))
165                return -1;
166        }
167        return 0;
168    }
169    
170    
171  /* Extract the plaintext data from the escaped data object @sig.  /* Extract the plaintext data from the escaped data object @sig.
172     The plaintxt is stored in @r_plain.     The plaintxt is stored in @r_plain.
173     Return value: 0 on success. */     Return value: 0 on success. */
# Line 128  gpg_data_extract_plaintext (gpgme_data_t Line 176  gpg_data_extract_plaintext (gpgme_data_t
176  {  {
177      gpgme_data_t plain;      gpgme_data_t plain;
178      gpgme_error_t err;      gpgme_error_t err;
179      char line[128+32];      char line[BUFSIZE+32];
180      int pos = 0;      int pos = 0;
     int sig_begin = 0;  
181            
182      if (r_plain)      if (r_plain)
183          *r_plain = NULL;          *r_plain = NULL;
# Line 138  gpg_data_extract_plaintext (gpgme_data_t Line 185  gpg_data_extract_plaintext (gpgme_data_t
185      if (err)      if (err)
186          return err;          return err;
187            
188      while (gpg_data_readline (sig, line, 128) > 0) {      while (gpg_data_readline (sig, line, BUFSIZE) > 0) {
189          if (!strncmp (line, "-----BEGIN PGP SIGNED MESSAGE", 29) ||          if (!strncmp (line, "-----BEGIN PGP SIGNED MESSAGE", 29) ||
190              !strncmp (line, "Version:", 8) ||              is_armor_header (line))
             !strncmp (line, "Comment:", 8) ||  
             !strncmp (line, "Charset:", 8) ||  
             !strncmp (line, "Hash:", 5) ||  
             !strncmp (line, "MessageID", 9))  
191              continue;              continue;
192          if (strlen (line) <= 2)          if (strlen (line) <= 2)
193              break; /* parsed all headers, now we reached the body */              break; /* parsed all headers, now we reached the body */
194      }      }
195      /* XXX handle multi dash escaped sequences */  
196      while (gpg_data_readline (sig, line, 128) > 0 ) {      /* We just support 1 nesting level. */
197          if (!strncmp( line, "-----BEGIN PGP SIGNATURE", 24))      while (gpg_data_readline (sig, line, BUFSIZE) > 0 ) {
198            if (!strncmp (line, "-----BEGIN PGP SIGNATURE", 24))
199              break; /* end of plaintext */              break; /* end of plaintext */
200          if (!strncmp (line, "- -", 3))          if (!strncmp (line, "- -", 3))
201              pos = 2;              pos = 2;
# Line 172  gpg_data_extract_plaintext (gpgme_data_t Line 216  gpg_data_extract_plaintext (gpgme_data_t
216  gpgme_error_t  gpgme_error_t
217  gpg_data_release_and_set_file (gpgme_data_t dh, const char *fname)  gpg_data_release_and_set_file (gpgme_data_t dh, const char *fname)
218  {  {
219      char *p = NULL;      FILE *fp = fopen (fname, "wb");
     FILE *fp;  
     size_t n;  
       
     fp = fopen (fname, "wb");  
220      if (fp == NULL)      if (fp == NULL)
221          return gpg_error (GPG_ERR_ENOENT);          return gpg_error (GPG_ERR_ENOENT);
222            
223      p = gpgme_data_release_and_get_mem (dh, &n);      size_t n;
224      if (p) {      char *p = gpgme_data_release_and_get_mem (dh, &n);
225        if (p != NULL) {
226          fwrite (p, 1, n, fp);          fwrite (p, 1, n, fp);
227          fflush (fp);          fflush (fp);
228          memset (p, 0xFF, n);          wipememory (p, n);
229          gpgme_free (p);          gpgme_free (p);
230      }      }
231      fclose (fp);      fclose (fp);
# Line 196  gpg_data_release_and_set_file (gpgme_dat Line 237  gpg_data_release_and_set_file (gpgme_dat
237     gpgme data object @dh into @line.     gpgme data object @dh into @line.
238     Return value: numbers of chars read. */     Return value: numbers of chars read. */
239  size_t  size_t
240  gpg_data_readline (gpgme_data_t dh, char *line, size_t nbytes)  gpg_data_readline (gpgme_data_t dh, char *line, size_t maxbytes)
241  {  {
242      char ch = 0;      char ch = 0;
243      int nread = 0, pos = 0;      int nread = 0, pos = 0;
# Line 204  gpg_data_readline (gpgme_data_t dh, char Line 245  gpg_data_readline (gpgme_data_t dh, char
245      if (!dh)      if (!dh)
246          return 0;          return 0;
247            
248      memset (line, 0, nbytes);      memset (line, 0, maxbytes);
249      while ((nread=gpgme_data_read (dh, &ch, 1)) > 0) {      while ((nread=gpgme_data_read (dh, &ch, 1)) > 0) {
250          if (!nread)          if (!nread)
251              break;              break;
# Line 214  gpg_data_readline (gpgme_data_t dh, char Line 255  gpg_data_readline (gpgme_data_t dh, char
255              break;              break;
256          }          }
257          line[pos++] = ch;          line[pos++] = ch;
258          if (pos > (int)nbytes) {          if (pos > (int)maxbytes-1) {
259              line[pos++] = '\0';              line[pos] = '\0';
260              break;              break;
261          }          }
262      }      }
263            
264      return pos;      return pos;
265  }  }
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    }

Legend:
Removed from v.121  
changed lines
  Added in v.455

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26