2001-11-10 19:06:11 +01:00
|
|
|
/* keybox-file.c - file oeprations
|
|
|
|
* Copyright (C) 2001 Free Software Foundation, Inc.
|
|
|
|
*
|
|
|
|
* This file is part of GnuPG.
|
|
|
|
*
|
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* GnuPG 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, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "keybox-defs.h"
|
|
|
|
|
2002-06-19 10:29:41 +02:00
|
|
|
/* Read a block at the current postion and return it in r_blob.
|
|
|
|
r_blob may be NULL to simply skip the current block */
|
2001-11-10 19:06:11 +01:00
|
|
|
int
|
|
|
|
_keybox_read_blob (KEYBOXBLOB *r_blob, FILE *fp)
|
|
|
|
{
|
|
|
|
char *image;
|
|
|
|
size_t imagelen = 0;
|
2002-07-22 12:21:04 +02:00
|
|
|
int c1, c2, c3, c4, type;
|
2001-11-10 19:06:11 +01:00
|
|
|
int rc;
|
2002-07-22 12:21:04 +02:00
|
|
|
off_t off;
|
2001-11-10 19:06:11 +01:00
|
|
|
|
2002-07-22 12:21:04 +02:00
|
|
|
again:
|
2001-11-10 19:06:11 +01:00
|
|
|
*r_blob = NULL;
|
2002-07-22 12:21:04 +02:00
|
|
|
off = ftello (fp);
|
|
|
|
if (off == (off_t)-1)
|
|
|
|
return KEYBOX_Read_Error;
|
|
|
|
|
2001-11-10 19:06:11 +01:00
|
|
|
if ((c1 = getc (fp)) == EOF
|
|
|
|
|| (c2 = getc (fp)) == EOF
|
|
|
|
|| (c3 = getc (fp)) == EOF
|
2002-07-22 12:21:04 +02:00
|
|
|
|| (c4 = getc (fp)) == EOF
|
|
|
|
|| (type = getc (fp)) == EOF)
|
|
|
|
{
|
|
|
|
if ( c1 == EOF && !ferror (fp) )
|
|
|
|
return -1; /* eof */
|
|
|
|
return KEYBOX_Read_Error;
|
|
|
|
}
|
2001-11-10 19:06:11 +01:00
|
|
|
|
|
|
|
imagelen = (c1 << 24) | (c2 << 16) | (c3 << 8 ) | c4;
|
|
|
|
if (imagelen > 500000) /* sanity check */
|
|
|
|
return KEYBOX_Blob_Too_Large;
|
|
|
|
|
2002-07-22 12:21:04 +02:00
|
|
|
if (imagelen < 5)
|
2001-11-10 19:06:11 +01:00
|
|
|
return KEYBOX_Blob_Too_Short;
|
2002-07-22 12:21:04 +02:00
|
|
|
|
|
|
|
if (!type)
|
|
|
|
{
|
|
|
|
/* special treatment for empty blobs. */
|
|
|
|
if (fseek (fp, imagelen-5, SEEK_CUR))
|
|
|
|
return KEYBOX_Read_Error;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
2001-11-10 19:06:11 +01:00
|
|
|
image = xtrymalloc (imagelen);
|
|
|
|
if (!image)
|
|
|
|
return KEYBOX_Out_Of_Core;
|
|
|
|
|
2002-07-22 12:21:04 +02:00
|
|
|
image[0] = c1; image[1] = c2; image[2] = c3; image[3] = c4; image[4] = type;
|
|
|
|
if (fread (image+5, imagelen-5, 1, fp) != 1)
|
2001-11-10 19:06:11 +01:00
|
|
|
{
|
2001-11-13 13:50:14 +01:00
|
|
|
xfree (image);
|
2001-11-10 19:06:11 +01:00
|
|
|
return KEYBOX_Read_Error;
|
|
|
|
}
|
|
|
|
|
2002-07-22 12:21:04 +02:00
|
|
|
rc = r_blob? _keybox_new_blob (r_blob, image, imagelen, off) : 0;
|
2001-11-13 13:50:14 +01:00
|
|
|
if (rc || !r_blob)
|
|
|
|
xfree (image);
|
2001-11-10 19:06:11 +01:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-13 13:50:14 +01:00
|
|
|
/* Write the block to the current file position */
|
|
|
|
int
|
|
|
|
_keybox_write_blob (KEYBOXBLOB blob, FILE *fp)
|
2001-11-10 19:06:11 +01:00
|
|
|
{
|
2001-11-13 13:50:14 +01:00
|
|
|
const char *image;
|
|
|
|
size_t length;
|
2001-11-10 19:06:11 +01:00
|
|
|
|
2001-11-13 13:50:14 +01:00
|
|
|
image = _keybox_get_blob_image (blob, &length);
|
|
|
|
if (fwrite (image, length, 1, fp) != 1)
|
|
|
|
{
|
|
|
|
return KEYBOX_Write_Error;
|
2001-11-10 19:06:11 +01:00
|
|
|
}
|
2001-11-13 13:50:14 +01:00
|
|
|
return 0;
|
2001-11-10 19:06:11 +01:00
|
|
|
}
|