2 * Copyright (C) 2001 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. */
38 #include "pgppacket.h"
40 #define CHUNKSIZE 1024
42 static unsigned char *pbuf = NULL;
43 static size_t plen = 0;
45 static int read_material (size_t material, size_t * used, FILE * fp)
47 if (*used + material >= plen)
52 nplen = *used + material + CHUNKSIZE;
54 if (!(p = realloc (pbuf, nplen))) /* __MEM_CHECKED__ */
63 if (fread (pbuf + *used, 1, material, fp) < material)
73 unsigned char *pgp_read_packet (FILE * fp, size_t * len)
81 startpos = ftello (fp);
86 pbuf = safe_malloc (plen);
89 if (fread (&ctb, 1, 1, fp) < 1)
101 if (ctb & 0x40) /* handle PGP 5.0 packets. */
109 if (fread (&b, 1, 1, fp) < 1)
121 else if (192 <= b && b <= 223)
123 material = (b - 192) * 256;
124 if (fread (&b, 1, 1, fp) < 1)
135 material = 1 << (b & 0x1f);
142 unsigned char buf[4];
143 if (fread (buf, 4, 1, fp) < 1)
148 /*assert( sizeof(material) >= 4 ); */
149 material = buf[0] << 24;
150 material |= buf[1] << 16;
151 material |= buf[2] << 8;
157 if (read_material (material, &used, fp) == -1)
167 pbuf[0] = 0x80 | ((ctb >> 2) & 0x0f);
174 if (fread (&b, 1, 1, fp) < 1)
196 for (i = 0; i < bytes; i++)
198 if (fread (&b, 1, 1, fp) < 1)
204 material = (material << 8) + b;
213 if (read_material (material, &used, fp) == -1)
224 fseeko (fp, startpos, SEEK_SET);
228 void pgp_release_packet (void)