@@ -203,38 +203,33 @@ crc24(const uint8 *data, unsigned len)
203
203
return crc & 0xffffffL ;
204
204
}
205
205
206
- int
207
- pgp_armor_encode (const uint8 * src , unsigned len , uint8 * dst )
206
+ void
207
+ pgp_armor_encode (const uint8 * src , int len , StringInfo dst )
208
208
{
209
- int n ;
210
- uint8 * pos = dst ;
209
+ int res ;
210
+ unsigned b64len ;
211
211
unsigned crc = crc24 (src , len );
212
212
213
- n = strlen (armor_header );
214
- memcpy (pos , armor_header , n );
215
- pos += n ;
216
-
217
- n = b64_encode (src , len , pos );
218
- pos += n ;
213
+ appendStringInfoString (dst , armor_header );
219
214
220
- if (* (pos - 1 ) != '\n' )
221
- * pos ++ = '\n' ;
215
+ /* make sure we have enough room to b64_encode() */
216
+ b64len = b64_enc_len (len );
217
+ enlargeStringInfo (dst , (int ) b64len );
218
+ res = b64_encode (src , len , (uint8 * ) dst -> data + dst -> len );
219
+ if (res > b64len )
220
+ elog (FATAL , "overflow - encode estimate too small" );
221
+ dst -> len += res ;
222
222
223
- * pos ++ = '=' ;
224
- pos [3 ] = _base64 [crc & 0x3f ];
225
- crc >>= 6 ;
226
- pos [2 ] = _base64 [crc & 0x3f ];
227
- crc >>= 6 ;
228
- pos [1 ] = _base64 [crc & 0x3f ];
229
- crc >>= 6 ;
230
- pos [0 ] = _base64 [crc & 0x3f ];
231
- pos += 4 ;
223
+ if (* (dst -> data + dst -> len - 1 ) != '\n' )
224
+ appendStringInfoChar (dst , '\n' );
232
225
233
- n = strlen (armor_footer );
234
- memcpy (pos , armor_footer , n );
235
- pos += n ;
226
+ appendStringInfoChar (dst , '=' );
227
+ appendStringInfoChar (dst , _base64 [(crc >> 18 ) & 0x3f ]);
228
+ appendStringInfoChar (dst , _base64 [(crc >> 12 ) & 0x3f ]);
229
+ appendStringInfoChar (dst , _base64 [(crc >> 6 ) & 0x3f ]);
230
+ appendStringInfoChar (dst , _base64 [crc & 0x3f ]);
236
231
237
- return pos - dst ;
232
+ appendStringInfoString ( dst , armor_footer ) ;
238
233
}
239
234
240
235
static const uint8 *
@@ -309,7 +304,7 @@ find_header(const uint8 *data, const uint8 *datend,
309
304
}
310
305
311
306
int
312
- pgp_armor_decode (const uint8 * src , unsigned len , uint8 * dst )
307
+ pgp_armor_decode (const uint8 * src , int len , StringInfo dst )
313
308
{
314
309
const uint8 * p = src ;
315
310
const uint8 * data_end = src + len ;
@@ -319,6 +314,7 @@ pgp_armor_decode(const uint8 *src, unsigned len, uint8 *dst)
319
314
const uint8 * base64_end = NULL ;
320
315
uint8 buf [4 ];
321
316
int hlen ;
317
+ int blen ;
322
318
int res = PXE_PGP_CORRUPT_ARMOR ;
323
319
324
320
/* armor start */
@@ -360,23 +356,18 @@ pgp_armor_decode(const uint8 *src, unsigned len, uint8 *dst)
360
356
crc = (((long ) buf [0 ]) << 16 ) + (((long ) buf [1 ]) << 8 ) + (long ) buf [2 ];
361
357
362
358
/* decode data */
363
- res = b64_decode (base64_start , base64_end - base64_start , dst );
364
-
365
- /* check crc */
366
- if (res >= 0 && crc24 (dst , res ) != crc )
367
- res = PXE_PGP_CORRUPT_ARMOR ;
359
+ blen = (int ) b64_dec_len (len );
360
+ enlargeStringInfo (dst , blen );
361
+ res = b64_decode (base64_start , base64_end - base64_start , (uint8 * ) dst -> data );
362
+ if (res > blen )
363
+ elog (FATAL , "overflow - decode estimate too small" );
364
+ if (res >= 0 )
365
+ {
366
+ if (crc24 ((uint8 * ) dst -> data , res ) == crc )
367
+ dst -> len += res ;
368
+ else
369
+ res = PXE_PGP_CORRUPT_ARMOR ;
370
+ }
368
371
out :
369
372
return res ;
370
373
}
371
-
372
- unsigned
373
- pgp_armor_enc_len (unsigned len )
374
- {
375
- return b64_enc_len (len ) + strlen (armor_header ) + strlen (armor_footer ) + 16 ;
376
- }
377
-
378
- unsigned
379
- pgp_armor_dec_len (unsigned len )
380
- {
381
- return b64_dec_len (len );
382
- }
0 commit comments