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,
30 #include "pgppacket.h"
32 #define CHUNKSIZE 1024
34 static unsigned char *pbuf = NULL;
35 static size_t plen = 0;
37 static int read_material (size_t material, size_t * used, FILE * fp)
39 if (*used + material >= plen)
44 nplen = *used + material + CHUNKSIZE;
46 if (!(p = realloc (pbuf, nplen))) /* __MEM_CHECKED__ */
55 if (fread (pbuf + *used, 1, material, fp) < material)
65 unsigned char *pgp_read_packet (FILE * fp, size_t * len)
73 startpos = ftell (fp);
78 pbuf = safe_malloc (plen);
81 if (fread (&ctb, 1, 1, fp) < 1)
93 if (ctb & 0x40) /* handle PGP 5.0 packets. */
101 if (fread (&b, 1, 1, fp) < 1)
113 else if (192 <= b && b <= 223)
115 material = (b - 192) * 256;
116 if (fread (&b, 1, 1, fp) < 1)
127 material = 1 << (b & 0x1f);
134 unsigned char buf[4];
135 if (fread (buf, 4, 1, fp) < 1)
140 /*assert( sizeof(material) >= 4 ); */
141 material = buf[0] << 24;
142 material |= buf[1] << 16;
143 material |= buf[2] << 8;
149 if (read_material (material, &used, fp) == -1)
159 pbuf[0] = 0x80 | ((ctb >> 2) & 0x0f);
166 if (fread (&b, 1, 1, fp) < 1)
188 for (i = 0; i < bytes; i++)
190 if (fread (&b, 1, 1, fp) < 1)
196 material = (material << 8) + b;
205 if (read_material (material, &used, fp) == -1)
216 fseek (fp, startpos, SEEK_SET);
220 void pgp_release_packet (void)