2008-01-15 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / kbx / keybox-file.c
blob7a40e964b43f3e5697014dd6bfff18fd50e3aa5d
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 3 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, see <http://www.gnu.org/licenses/>.
20 #include <config.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <time.h>
27 #include "keybox-defs.h"
30 #if !defined(HAVE_FTELLO) && !defined(ftello)
31 static off_t
32 ftello (FILE *stream)
34 long int off;
36 off = ftell (stream);
37 if (off == -1)
38 return (off_t)-1;
39 return off;
41 #endif /* !defined(HAVE_FTELLO) && !defined(ftello) */
45 /* Read a block at the current postion and return it in r_blob.
46 r_blob may be NULL to simply skip the current block */
47 int
48 _keybox_read_blob2 (KEYBOXBLOB *r_blob, FILE *fp, int *skipped_deleted)
50 unsigned char *image;
51 size_t imagelen = 0;
52 int c1, c2, c3, c4, type;
53 int rc;
54 off_t off;
56 *skipped_deleted = 0;
57 again:
58 *r_blob = NULL;
59 off = ftello (fp);
60 if (off == (off_t)-1)
61 return gpg_error (gpg_err_code_from_errno (errno));
63 if ((c1 = getc (fp)) == EOF
64 || (c2 = getc (fp)) == EOF
65 || (c3 = getc (fp)) == EOF
66 || (c4 = getc (fp)) == EOF
67 || (type = getc (fp)) == EOF)
69 if ( c1 == EOF && !ferror (fp) )
70 return -1; /* eof */
71 return gpg_error (gpg_err_code_from_errno (errno));
74 imagelen = (c1 << 24) | (c2 << 16) | (c3 << 8 ) | c4;
75 if (imagelen > 500000) /* Sanity check. */
76 return gpg_error (GPG_ERR_TOO_LARGE);
78 if (imagelen < 5)
79 return gpg_error (GPG_ERR_TOO_SHORT);
81 if (!type)
83 /* Special treatment for empty blobs. */
84 if (fseek (fp, imagelen-5, SEEK_CUR))
85 return gpg_error (gpg_err_code_from_errno (errno));
86 *skipped_deleted = 1;
87 goto again;
90 image = xtrymalloc (imagelen);
91 if (!image)
92 return gpg_error (gpg_err_code_from_errno (errno));
94 image[0] = c1; image[1] = c2; image[2] = c3; image[3] = c4; image[4] = type;
95 if (fread (image+5, imagelen-5, 1, fp) != 1)
97 gpg_error_t tmperr = gpg_error (gpg_err_code_from_errno (errno));
98 xfree (image);
99 return tmperr;
102 rc = r_blob? _keybox_new_blob (r_blob, image, imagelen, off) : 0;
103 if (rc || !r_blob)
104 xfree (image);
105 return rc;
109 _keybox_read_blob (KEYBOXBLOB *r_blob, FILE *fp)
111 int dummy;
112 return _keybox_read_blob2 (r_blob, fp, &dummy);
116 /* Write the block to the current file position */
118 _keybox_write_blob (KEYBOXBLOB blob, FILE *fp)
120 const unsigned char *image;
121 size_t length;
123 image = _keybox_get_blob_image (blob, &length);
124 if (fwrite (image, length, 1, fp) != 1)
125 return gpg_error (gpg_err_code_from_errno (errno));
126 return 0;
130 /* Write a fresh header type blob. */
132 _keybox_write_header_blob (FILE *fp)
134 unsigned char image[32];
135 u32 val;
137 memset (image, 0, sizeof image);
138 /* Length of this blob. */
139 image[3] = 32;
141 image[4] = BLOBTYPE_HEADER;
142 image[5] = 1; /* Version */
144 memcpy (image+8, "KBXf", 4);
145 val = time (NULL);
146 /* created_at and last maintenance run. */
147 image[16] = (val >> 24);
148 image[16+1] = (val >> 16);
149 image[16+2] = (val >> 8);
150 image[16+3] = (val );
151 image[20] = (val >> 24);
152 image[20+1] = (val >> 16);
153 image[20+2] = (val >> 8);
154 image[20+3] = (val );
156 if (fwrite (image, 32, 1, fp) != 1)
157 return gpg_error (gpg_err_code_from_errno (errno));
158 return 0;