2 * Copyright (C) 2001-2,2007 Thomas Roessler <roessler@does-not-exist.org>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public
15 * License along with this program; if not, write to the Free
16 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
32 /* yuck, we were including this one somewhere below. */
36 #include "pgppacket.h"
38 #define CHUNKSIZE 1024
40 static unsigned char *pbuf = NULL;
41 static size_t plen = 0;
43 static int read_material (size_t material, size_t * used, FILE * fp)
45 if (*used + material >= plen)
50 nplen = *used + material + CHUNKSIZE;
52 if (!(p = realloc (pbuf, nplen))) /* __MEM_CHECKED__ */
61 if (fread (pbuf + *used, 1, material, fp) < material)
71 unsigned char *pgp_read_packet (FILE * fp, size_t * len)
79 startpos = ftello (fp);
84 pbuf = safe_malloc (plen);
87 if (fread (&ctb, 1, 1, fp) < 1)
99 if (ctb & 0x40) /* handle PGP 5.0 packets. */
107 if (fread (&b, 1, 1, fp) < 1)
119 else if (192 <= b && b <= 223)
121 material = (b - 192) * 256;
122 if (fread (&b, 1, 1, fp) < 1)
133 material = 1 << (b & 0x1f);
140 unsigned char buf[4];
141 if (fread (buf, 4, 1, fp) < 1)
146 /*assert( sizeof(material) >= 4 ); */
147 material = buf[0] << 24;
148 material |= buf[1] << 16;
149 material |= buf[2] << 8;
155 if (read_material (material, &used, fp) == -1)
165 pbuf[0] = 0x80 | ((ctb >> 2) & 0x0f);
172 if (fread (&b, 1, 1, fp) < 1)
194 for (i = 0; i < bytes; i++)
196 if (fread (&b, 1, 1, fp) < 1)
202 material = (material << 8) + b;
211 if (read_material (material, &used, fp) == -1)
222 fseeko (fp, startpos, SEEK_SET);
226 void pgp_release_packet (void)