2 * Copyright notice from original mutt:
3 * Copyright (C) 2001 Thomas Roessler <roessler@does-not-exist.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
20 #include <lib-lib/mem.h>
21 #include <lib-hash/hash.h>
25 #include "pgppacket.h"
27 #define CHUNKSIZE 1024
29 static unsigned char *pbuf = NULL;
30 static size_t plen = 0;
32 static int read_material (size_t material, size_t * used, FILE * fp)
34 if (*used + material >= plen) {
38 nplen = *used + material + CHUNKSIZE;
40 if (!(p = realloc (pbuf, nplen))) { /* __MEM_CHECKED__ */
48 if (fread (pbuf + *used, 1, material, fp) < material) {
57 unsigned char *pgp_read_packet (FILE * fp, size_t * len)
65 startpos = ftello (fp);
68 pbuf = p_new(char, plen = CHUNKSIZE);
71 if (fread (&ctb, 1, 1, fp) < 1) {
81 if (ctb & 0x40) { /* handle PGP 5.0 packets. */
88 if (fread (&b, 1, 1, fp) < 1) {
98 else if (192 <= b && b <= 223) {
99 material = (b - 192) * 256;
100 if (fread (&b, 1, 1, fp) < 1) {
109 material = 1 << (b & 0x1f);
116 unsigned char buf[4];
118 if (fread (buf, 4, 1, fp) < 1) {
122 /*assert( sizeof(material) >= 4 ); */
123 material = buf[0] << 24;
124 material |= buf[1] << 16;
125 material |= buf[2] << 8;
131 if (read_material (material, &used, fp) == -1)
142 pbuf[0] = 0x80 | ((ctb >> 2) & 0x0f);
145 switch (ctb & 0x03) {
148 if (fread (&b, 1, 1, fp) < 1) {
169 for (i = 0; i < bytes; i++) {
170 if (fread (&b, 1, 1, fp) < 1) {
175 material = (material << 8) + b;
184 if (read_material (material, &used, fp) == -1)
195 fseeko (fp, startpos, SEEK_SET);
199 void pgp_release_packet (void)