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., 59 Temple Place - Suite 330, Boston,
33 #include "pgppacket.h"
35 #define CHUNKSIZE 1024
37 static unsigned char *pbuf = NULL;
38 static size_t plen = 0;
40 static int read_material (size_t material, size_t * used, FILE * fp)
42 if (*used + material >= plen)
47 nplen = *used + material + CHUNKSIZE;
49 if (!(p = realloc (pbuf, nplen))) /* __MEM_CHECKED__ */
58 if (fread (pbuf + *used, 1, material, fp) < material)
68 unsigned char *pgp_read_packet (FILE * fp, size_t * len)
76 startpos = ftell (fp);
81 pbuf = safe_malloc (plen);
84 if (fread (&ctb, 1, 1, fp) < 1)
96 if (ctb & 0x40) /* handle PGP 5.0 packets. */
104 if (fread (&b, 1, 1, fp) < 1)
116 else if (192 <= b && b <= 223)
118 material = (b - 192) * 256;
119 if (fread (&b, 1, 1, fp) < 1)
130 material = 1 << (b & 0x1f);
137 unsigned char buf[4];
138 if (fread (buf, 4, 1, fp) < 1)
143 /*assert( sizeof(material) >= 4 ); */
144 material = buf[0] << 24;
145 material |= buf[1] << 16;
146 material |= buf[2] << 8;
152 if (read_material (material, &used, fp) == -1)
162 pbuf[0] = 0x80 | ((ctb >> 2) & 0x0f);
169 if (fread (&b, 1, 1, fp) < 1)
191 for (i = 0; i < bytes; i++)
193 if (fread (&b, 1, 1, fp) < 1)
199 material = (material << 8) + b;
208 if (read_material (material, &used, fp) == -1)
219 fseek (fp, startpos, SEEK_SET);
223 void pgp_release_packet (void)