NeoMutt  2024-02-01-25-ga71e95
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
pgppacket.c File Reference

Parse PGP data packets. More...

#include "config.h"
#include <stdbool.h>
#include <stdio.h>
#include "mutt/lib.h"
#include "pgppacket.h"
+ Include dependency graph for pgppacket.c:

Go to the source code of this file.

Macros

#define CHUNK_SIZE   1024
 Amount of data to read at once.
 

Functions

static int read_material (size_t material, size_t *used, FILE *fp)
 Read PGP data into a buffer.
 
unsigned char * pgp_read_packet (FILE *fp, size_t *len)
 Read a PGP packet from a file.
 
void pgp_release_packet (void)
 Free the cached PGP packet.
 

Variables

static unsigned char * PacketBuf = NULL
 Cached PGP data packet.
 
static size_t PacketBufLen = 0
 Length of cached packet.
 

Detailed Description

Parse PGP data packets.

Authors
  • Richard Russon
  • Pietro Cerutti

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file pgppacket.c.

Macro Definition Documentation

◆ CHUNK_SIZE

#define CHUNK_SIZE   1024

Amount of data to read at once.

Definition at line 36 of file pgppacket.c.

Function Documentation

◆ read_material()

static int read_material ( size_t  material,
size_t *  used,
FILE *  fp 
)
static

Read PGP data into a buffer.

Parameters
[in]materialNumber of bytes to read
[in,out]usedNumber of bytes already read
[in]fpFile to read from
Return values
0Success
-1Failure (see errno)

This function uses a cache to store the data: PacketBuf, PacketBufLen.

Definition at line 51 of file pgppacket.c.

52{
53 if ((*used + material) >= PacketBufLen)
54 {
55 PacketBufLen = *used + material + CHUNK_SIZE;
56
58 }
59
60 if (fread(PacketBuf + *used, 1, material, fp) < material)
61 {
62 mutt_perror("fread");
63 return -1;
64 }
65
66 *used += material;
67 return 0;
68}
#define mutt_perror(...)
Definition: logging2.h:93
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:114
static unsigned char * PacketBuf
Cached PGP data packet.
Definition: pgppacket.c:38
static size_t PacketBufLen
Length of cached packet.
Definition: pgppacket.c:39
#define CHUNK_SIZE
Amount of data to read at once.
Definition: pgppacket.c:36
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ pgp_read_packet()

unsigned char * pgp_read_packet ( FILE *  fp,
size_t *  len 
)

Read a PGP packet from a file.

Parameters
[in]fpFile to read from
[out]lenNumber of bytes read
Return values
ptrPGP data packet

This function uses a cache to store the data: PacketBuf, PacketBufLen.

Definition at line 78 of file pgppacket.c.

79{
80 size_t used = 0;
81 LOFF_T startpos;
82 unsigned char ctb;
83 unsigned char b;
84 size_t material;
85
86 startpos = ftello(fp);
87 if (startpos < 0)
88 return NULL;
89
90 if (PacketBufLen == 0)
91 {
94 }
95
96 if (fread(&ctb, 1, 1, fp) < 1)
97 {
98 if (!feof(fp))
99 mutt_perror("fread");
100 goto bail;
101 }
102
103 if (!(ctb & 0x80))
104 {
105 goto bail;
106 }
107
108 if (ctb & 0x40) /* handle PGP 5.0 packets. */
109 {
110 bool partial = false;
111 PacketBuf[0] = ctb;
112 used++;
113
114 do
115 {
116 if (fread(&b, 1, 1, fp) < 1)
117 {
118 mutt_perror("fread");
119 goto bail;
120 }
121
122 if (b < 192)
123 {
124 material = b;
125 partial = false;
126 }
127 else if (b <= 223)
128 {
129 material = (b - 192) * 256;
130 if (fread(&b, 1, 1, fp) < 1)
131 {
132 mutt_perror("fread");
133 goto bail;
134 }
135 material += b + 192;
136 partial = false;
137 }
138 else if (b < 255)
139 {
140 material = 1 << (b & 0x1f);
141 partial = true;
142 }
143 else /* b == 255 */
144 {
145 unsigned char buf[4];
146 if (fread(buf, 4, 1, fp) < 1)
147 {
148 mutt_perror("fread");
149 goto bail;
150 }
151 material = (size_t) buf[0] << 24;
152 material |= buf[1] << 16;
153 material |= buf[2] << 8;
154 material |= buf[3];
155 partial = false;
156 }
157
158 if (read_material(material, &used, fp) == -1)
159 goto bail;
160
161 } while (partial);
162 }
163 else /* Old-Style PGP */
164 {
165 int bytes = 0;
166 PacketBuf[0] = 0x80 | ((ctb >> 2) & 0x0f);
167 used++;
168
169 switch (ctb & 0x03)
170 {
171 case 0:
172 {
173 if (fread(&b, 1, 1, fp) < 1)
174 {
175 mutt_perror("fread");
176 goto bail;
177 }
178
179 material = b;
180 break;
181 }
182
183 case 1:
184 bytes = 2;
186
187 case 2:
188 {
189 if (!bytes)
190 bytes = 4;
191
192 material = 0;
193
194 for (int i = 0; i < bytes; i++)
195 {
196 if (fread(&b, 1, 1, fp) < 1)
197 {
198 mutt_perror("fread");
199 goto bail;
200 }
201
202 material = (material << 8) + b;
203 }
204 break;
205 }
206
207 default:
208 goto bail;
209 }
210
211 if (read_material(material, &used, fp) == -1)
212 goto bail;
213 }
214
215 if (len)
216 *len = used;
217
218 return PacketBuf;
219
220bail:
221
222 (void) mutt_file_seek(fp, startpos, SEEK_SET);
223 return NULL;
224}
bool mutt_file_seek(FILE *fp, LOFF_T offset, int whence)
Wrapper for fseeko with error handling.
Definition: file.c:771
void * mutt_mem_malloc(size_t size)
Allocate memory on the heap.
Definition: memory.c:90
#define FALLTHROUGH
Definition: lib.h:111
static int read_material(size_t material, size_t *used, FILE *fp)
Read PGP data into a buffer.
Definition: pgppacket.c:51
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ pgp_release_packet()

void pgp_release_packet ( void  )

Free the cached PGP packet.

Free the data stored in PacketBuf.

Definition at line 231 of file pgppacket.c.

232{
233 PacketBufLen = 0;
234 FREE(&PacketBuf);
235}
#define FREE(x)
Definition: memory.h:45
+ Here is the caller graph for this function:

Variable Documentation

◆ PacketBuf

unsigned char* PacketBuf = NULL
static

Cached PGP data packet.

Definition at line 38 of file pgppacket.c.

◆ PacketBufLen

size_t PacketBufLen = 0
static

Length of cached packet.

Definition at line 39 of file pgppacket.c.