2006-10-02 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / kbx / keybox-file.c
blobe68e96cf91804588a4b1fc64e1fd6732736dea8d
1 /* keybox-file.c - file oeprations
2 * Copyright (C) 2001, 2003 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 * USA.
22 #include <config.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <time.h>
29 #include "keybox-defs.h"
32 #if !defined(HAVE_FTELLO) && !defined(ftello)
33 static off_t
34 ftello (FILE *stream)
36 long int off;
38 off = ftell (stream);
39 if (off == -1)
40 return (off_t)-1;
41 return off;
43 #endif /* !defined(HAVE_FTELLO) && !defined(ftello) */
47 /* Read a block at the current postion and return it in r_blob.
48 r_blob may be NULL to simply skip the current block */
49 int
50 _keybox_read_blob2 (KEYBOXBLOB *r_blob, FILE *fp, int *skipped_deleted)
52 unsigned char *image;
53 size_t imagelen = 0;
54 int c1, c2, c3, c4, type;
55 int rc;
56 off_t off;
58 *skipped_deleted = 0;
59 again:
60 *r_blob = NULL;
61 off = ftello (fp);
62 if (off == (off_t)-1)
63 return gpg_error (gpg_err_code_from_errno (errno));
65 if ((c1 = getc (fp)) == EOF
66 || (c2 = getc (fp)) == EOF
67 || (c3 = getc (fp)) == EOF
68 || (c4 = getc (fp)) == EOF
69 || (type = getc (fp)) == EOF)
71 if ( c1 == EOF && !ferror (fp) )
72 return -1; /* eof */
73 return gpg_error (gpg_err_code_from_errno (errno));
76 imagelen = (c1 << 24) | (c2 << 16) | (c3 << 8 ) | c4;
77 if (imagelen > 500000) /* Sanity check. */
78 return gpg_error (GPG_ERR_TOO_LARGE);
80 if (imagelen < 5)
81 return gpg_error (GPG_ERR_TOO_SHORT);
83 if (!type)
85 /* Special treatment for empty blobs. */
86 if (fseek (fp, imagelen-5, SEEK_CUR))
87 return gpg_error (gpg_err_code_from_errno (errno));
88 *skipped_deleted = 1;
89 goto again;
92 image = xtrymalloc (imagelen);
93 if (!image)
94 return gpg_error (gpg_err_code_from_errno (errno));
96 image[0] = c1; image[1] = c2; image[2] = c3; image[3] = c4; image[4] = type;
97 if (fread (image+5, imagelen-5, 1, fp) != 1)
99 gpg_error_t tmperr = gpg_error (gpg_err_code_from_errno (errno));
100 xfree (image);
101 return tmperr;
104 rc = r_blob? _keybox_new_blob (r_blob, image, imagelen, off) : 0;
105 if (rc || !r_blob)
106 xfree (image);
107 return rc;
111 _keybox_read_blob (KEYBOXBLOB *r_blob, FILE *fp)
113 int dummy;
114 return _keybox_read_blob2 (r_blob, fp, &dummy);
118 /* Write the block to the current file position */
120 _keybox_write_blob (KEYBOXBLOB blob, FILE *fp)
122 const unsigned char *image;
123 size_t length;
125 image = _keybox_get_blob_image (blob, &length);
126 if (fwrite (image, length, 1, fp) != 1)
127 return gpg_error (gpg_err_code_from_errno (errno));
128 return 0;
132 /* Write a fresh header type blob. */
134 _keybox_write_header_blob (FILE *fp)
136 unsigned char image[32];
137 u32 val;
139 memset (image, 0, sizeof image);
140 /* Length of this blob. */
141 image[3] = 32;
143 image[4] = BLOBTYPE_HEADER;
144 image[5] = 1; /* Version */
146 memcpy (image+8, "KBXf", 4);
147 val = time (NULL);
148 /* created_at and last maintenance run. */
149 image[16] = (val >> 24);
150 image[16+1] = (val >> 16);
151 image[16+2] = (val >> 8);
152 image[16+3] = (val );
153 image[20] = (val >> 24);
154 image[20+1] = (val >> 16);
155 image[20+2] = (val >> 8);
156 image[20+3] = (val );
158 if (fwrite (image, 32, 1, fp) != 1)
159 return gpg_error (gpg_err_code_from_errno (errno));
160 return 0;