summaryrefslogtreecommitdiffstats
path: root/kbx
diff options
context:
space:
mode:
authorWerner Koch <wk@gnupg.org>2002-07-22 12:21:04 +0200
committerWerner Koch <wk@gnupg.org>2002-07-22 12:21:04 +0200
commit508ce100c975692648325cdd839d9b49496659d6 (patch)
treefd5ed7741a7c880902be499be71a4fcbb9c295cf /kbx
parent* fseeko.c, ftello.c: New. (diff)
downloadgnupg2-508ce100c975692648325cdd839d9b49496659d6.tar.xz
gnupg2-508ce100c975692648325cdd839d9b49496659d6.zip
* keybox-defs.h: New BLOBTYPTE_EMPTY.
* keybox-dump.c (_keybox_dump_blob): Handle new type. * keybox-file.c (_keybox_read_blob): Skip over empty blobs. Store the file offset. * keybox-blob.c (_keybox_new_blob): Add new arg OFF. (_keybox_get_blob_fileoffset): New. * keybox-update.c (keybox_delete): Implemented.
Diffstat (limited to 'kbx')
-rw-r--r--kbx/ChangeLog10
-rw-r--r--kbx/keybox-blob.c15
-rw-r--r--kbx/keybox-defs.h5
-rw-r--r--kbx/keybox-dump.c4
-rw-r--r--kbx/keybox-file.c38
-rw-r--r--kbx/keybox-update.c45
6 files changed, 101 insertions, 16 deletions
diff --git a/kbx/ChangeLog b/kbx/ChangeLog
index 715acbdcf..c53d48a84 100644
--- a/kbx/ChangeLog
+++ b/kbx/ChangeLog
@@ -1,3 +1,13 @@
+2002-07-22 Werner Koch <wk@gnupg.org>
+
+ * keybox-defs.h: New BLOBTYPTE_EMPTY.
+ * keybox-dump.c (_keybox_dump_blob): Handle new type.
+ * keybox-file.c (_keybox_read_blob): Skip over empty blobs. Store
+ the file offset.
+ * keybox-blob.c (_keybox_new_blob): Add new arg OFF.
+ (_keybox_get_blob_fileoffset): New.
+ * keybox-update.c (keybox_delete): Implemented.
+
2002-06-19 Werner Koch <wk@gnupg.org>
* keybox-init.c (keybox_set_ephemeral): New.
diff --git a/kbx/keybox-blob.c b/kbx/keybox-blob.c
index 34e2c9712..48e2b5b87 100644
--- a/kbx/keybox-blob.c
+++ b/kbx/keybox-blob.c
@@ -171,6 +171,7 @@ struct fixup_list {
struct keyboxblob {
byte *blob;
size_t bloblen;
+ off_t fileoffset;
/* stuff used only by keybox_create_blob */
unsigned char *serialbuf;
@@ -956,7 +957,7 @@ _keybox_create_x509_blob (KEYBOXBLOB *r_blob, KsbaCert cert,
int
-_keybox_new_blob (KEYBOXBLOB *r_blob, char *image, size_t imagelen)
+_keybox_new_blob (KEYBOXBLOB *r_blob, char *image, size_t imagelen, off_t off)
{
KEYBOXBLOB blob;
@@ -967,6 +968,7 @@ _keybox_new_blob (KEYBOXBLOB *r_blob, char *image, size_t imagelen)
blob->blob = image;
blob->bloblen = imagelen;
+ blob->fileoffset = off;
*r_blob = blob;
return 0;
}
@@ -993,6 +995,13 @@ _keybox_release_blob (KEYBOXBLOB blob)
const char *
_keybox_get_blob_image ( KEYBOXBLOB blob, size_t *n )
{
- *n = blob->bloblen;
- return blob->blob;
+ *n = blob->bloblen;
+ return blob->blob;
}
+
+off_t
+_keybox_get_blob_fileoffset (KEYBOXBLOB blob)
+{
+ return blob->fileoffset;
+}
+
diff --git a/kbx/keybox-defs.h b/kbx/keybox-defs.h
index 978bb229e..1c66020f7 100644
--- a/kbx/keybox-defs.h
+++ b/kbx/keybox-defs.h
@@ -35,6 +35,7 @@ typedef unsigned int u32; /* fixme */
#endif
enum {
+ BLOBTYPE_EMPTY = 0,
BLOBTYPE_HEADER = 1,
BLOBTYPE_PGP = 2,
BLOBTYPE_X509 = 3
@@ -97,9 +98,11 @@ int _keybox_create_x509_blob (KEYBOXBLOB *r_blob, KsbaCert cert,
unsigned char *sha1_digest, int as_ephemeral);
#endif /*KEYBOX_WITH_X509*/
-int _keybox_new_blob (KEYBOXBLOB *r_blob, char *image, size_t imagelen);
+int _keybox_new_blob (KEYBOXBLOB *r_blob, char *image, size_t imagelen,
+ off_t off);
void _keybox_release_blob (KEYBOXBLOB blob);
const char *_keybox_get_blob_image (KEYBOXBLOB blob, size_t *n);
+off_t _keybox_get_blob_fileoffset (KEYBOXBLOB blob);
/*-- keybox-file.c --*/
int _keybox_read_blob (KEYBOXBLOB *r_blob, FILE *fp);
diff --git a/kbx/keybox-dump.c b/kbx/keybox-dump.c
index 530b4647f..4fb3a4eb2 100644
--- a/kbx/keybox-dump.c
+++ b/kbx/keybox-dump.c
@@ -117,6 +117,10 @@ _keybox_dump_blob (KEYBOXBLOB blob, FILE *fp)
type = buffer[4];
switch (type)
{
+ case BLOBTYPE_EMPTY:
+ fprintf (fp, "Type: Empty\n");
+ return 0;
+
case BLOBTYPE_HEADER:
fprintf (fp, "Type: Header\n");
return dump_header_blob (buffer, length, fp);
diff --git a/kbx/keybox-file.c b/kbx/keybox-file.c
index 79a2e1719..bbbf78e3c 100644
--- a/kbx/keybox-file.c
+++ b/kbx/keybox-file.c
@@ -32,38 +32,54 @@ _keybox_read_blob (KEYBOXBLOB *r_blob, FILE *fp)
{
char *image;
size_t imagelen = 0;
- int c1, c2, c3, c4;
+ int c1, c2, c3, c4, type;
int rc;
+ off_t off;
+ again:
*r_blob = NULL;
+ off = ftello (fp);
+ if (off == (off_t)-1)
+ return KEYBOX_Read_Error;
+
if ((c1 = getc (fp)) == EOF
|| (c2 = getc (fp)) == EOF
|| (c3 = getc (fp)) == EOF
- || (c4 = getc (fp)) == EOF ) {
- if ( c1 == EOF && !ferror (fp) )
- return -1; /* eof */
- return KEYBOX_Read_Error;
- }
+ || (c4 = getc (fp)) == EOF
+ || (type = getc (fp)) == EOF)
+ {
+ if ( c1 == EOF && !ferror (fp) )
+ return -1; /* eof */
+ return KEYBOX_Read_Error;
+ }
imagelen = (c1 << 24) | (c2 << 16) | (c3 << 8 ) | c4;
if (imagelen > 500000) /* sanity check */
return KEYBOX_Blob_Too_Large;
- if (imagelen < 4)
+ if (imagelen < 5)
return KEYBOX_Blob_Too_Short;
-
+
+ if (!type)
+ {
+ /* special treatment for empty blobs. */
+ if (fseek (fp, imagelen-5, SEEK_CUR))
+ return KEYBOX_Read_Error;
+ goto again;
+ }
+
image = xtrymalloc (imagelen);
if (!image)
return KEYBOX_Out_Of_Core;
- image[0] = c1; image[1] = c2; image[2] = c3; image[3] = c4;
- if (fread (image+4, imagelen-4, 1, fp) != 1)
+ image[0] = c1; image[1] = c2; image[2] = c3; image[3] = c4; image[4] = type;
+ if (fread (image+5, imagelen-5, 1, fp) != 1)
{
xfree (image);
return KEYBOX_Read_Error;
}
- rc = r_blob? _keybox_new_blob (r_blob, image, imagelen) : 0;
+ rc = r_blob? _keybox_new_blob (r_blob, image, imagelen, off) : 0;
if (rc || !r_blob)
xfree (image);
return rc;
diff --git a/kbx/keybox-update.c b/kbx/keybox-update.c
index 96a30b9c1..8b189bbed 100644
--- a/kbx/keybox-update.c
+++ b/kbx/keybox-update.c
@@ -384,7 +384,50 @@ keybox_update_cert (KEYBOX_HANDLE hd, KsbaCert cert,
int
keybox_delete (KEYBOX_HANDLE hd)
{
- return -1;
+ off_t off;
+ const char *fname;
+ FILE *fp;
+ int rc;
+
+ if (!hd)
+ return KEYBOX_Invalid_Value;
+ if (!hd->found.blob)
+ return KEYBOX_Nothing_Found;
+ if (!hd->kb)
+ return KEYBOX_Invalid_Handle;
+ fname = hd->kb->fname;
+ if (!fname)
+ return KEYBOX_Invalid_Handle;
+
+ off = _keybox_get_blob_fileoffset (hd->found.blob);
+ if (off == (off_t)-1)
+ return KEYBOX_General_Error;
+ off += 4;
+
+ if (hd->fp)
+ {
+ fclose (hd->fp);
+ hd->fp = NULL;
+ }
+
+ fp = fopen (hd->kb->fname, "r+b");
+ if (!fp)
+ return KEYBOX_File_Open_Error;
+
+ if (fseeko (fp, off, SEEK_SET))
+ rc = KEYBOX_Write_Error;
+ else if (putc (0, fp) == EOF)
+ rc = KEYBOX_Write_Error;
+ else
+ rc = 0;
+
+ if (fclose (fp))
+ {
+ if (!rc)
+ rc = KEYBOX_File_Close_Error;
+ }
+
+ return rc;
}