~ubuntu-branches/ubuntu/precise/gnupg2/precise-updates

« back to all changes in this revision

Viewing changes to kbx/keybox-file.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Mueller
  • Date: 2005-03-29 10:30:32 UTC
  • Revision ID: james.westby@ubuntu.com-20050329103032-sj42n2ain3ipx310
Tags: upstream-1.9.15
ImportĀ upstreamĀ versionĀ 1.9.15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* keybox-file.c - file oeprations
 
2
 *      Copyright (C) 2001, 2003 Free Software Foundation, Inc.
 
3
 *
 
4
 * This file is part of GnuPG.
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 
19
 */
 
20
 
 
21
#include <config.h>
 
22
#include <stdlib.h>
 
23
#include <stdio.h>
 
24
#include <string.h>
 
25
#include <errno.h>
 
26
#include <time.h>
 
27
 
 
28
#include "keybox-defs.h"
 
29
 
 
30
/* Read a block at the current postion and return it in r_blob.
 
31
   r_blob may be NULL to simply skip the current block */
 
32
int
 
33
_keybox_read_blob2 (KEYBOXBLOB *r_blob, FILE *fp, int *skipped_deleted)
 
34
{
 
35
  char *image;
 
36
  size_t imagelen = 0;
 
37
  int c1, c2, c3, c4, type;
 
38
  int rc;
 
39
  off_t off;
 
40
 
 
41
  *skipped_deleted = 0;
 
42
 again:
 
43
  *r_blob = NULL;
 
44
  off = ftello (fp);
 
45
  if (off == (off_t)-1)
 
46
    return gpg_error (gpg_err_code_from_errno (errno));
 
47
 
 
48
  if ((c1 = getc (fp)) == EOF
 
49
      || (c2 = getc (fp)) == EOF
 
50
      || (c3 = getc (fp)) == EOF
 
51
      || (c4 = getc (fp)) == EOF
 
52
      || (type = getc (fp)) == EOF)
 
53
    {
 
54
      if ( c1 == EOF && !ferror (fp) )
 
55
        return -1; /* eof */
 
56
      return gpg_error (gpg_err_code_from_errno (errno));
 
57
    }
 
58
 
 
59
  imagelen = (c1 << 24) | (c2 << 16) | (c3 << 8 ) | c4;
 
60
  if (imagelen > 500000) /* Sanity check. */
 
61
    return gpg_error (GPG_ERR_TOO_LARGE);
 
62
  
 
63
  if (imagelen < 5) 
 
64
    return gpg_error (GPG_ERR_TOO_SHORT);
 
65
 
 
66
  if (!type)
 
67
    {
 
68
      /* Special treatment for empty blobs. */
 
69
      if (fseek (fp, imagelen-5, SEEK_CUR))
 
70
        return gpg_error (gpg_err_code_from_errno (errno));
 
71
      *skipped_deleted = 1;
 
72
      goto again;
 
73
    }
 
74
 
 
75
  image = xtrymalloc (imagelen);
 
76
  if (!image) 
 
77
    return gpg_error (gpg_err_code_from_errno (errno));
 
78
 
 
79
  image[0] = c1; image[1] = c2; image[2] = c3; image[3] = c4; image[4] = type;
 
80
  if (fread (image+5, imagelen-5, 1, fp) != 1)
 
81
    {
 
82
      gpg_error_t tmperr = gpg_error (gpg_err_code_from_errno (errno));
 
83
      xfree (image);
 
84
      return tmperr;
 
85
    }
 
86
  
 
87
  rc = r_blob? _keybox_new_blob (r_blob, image, imagelen, off) : 0;
 
88
  if (rc || !r_blob)
 
89
        xfree (image);
 
90
  return rc;
 
91
}
 
92
 
 
93
int
 
94
_keybox_read_blob (KEYBOXBLOB *r_blob, FILE *fp)
 
95
{
 
96
  int dummy;
 
97
  return _keybox_read_blob2 (r_blob, fp, &dummy);
 
98
}
 
99
 
 
100
 
 
101
/* Write the block to the current file position */
 
102
int
 
103
_keybox_write_blob (KEYBOXBLOB blob, FILE *fp)
 
104
{
 
105
  const char *image;
 
106
  size_t length;
 
107
 
 
108
  image = _keybox_get_blob_image (blob, &length);
 
109
  if (fwrite (image, length, 1, fp) != 1)
 
110
    return gpg_error (gpg_err_code_from_errno (errno));
 
111
  return 0;
 
112
}
 
113
 
 
114
 
 
115
/* Write a fresh header type blob. */
 
116
int
 
117
_keybox_write_header_blob (FILE *fp)
 
118
{
 
119
  unsigned char image[32];
 
120
  u32 val;
 
121
 
 
122
  memset (image, 0, sizeof image);
 
123
  /* Length of this blob. */
 
124
  image[3] = 32;
 
125
 
 
126
  image[4] = BLOBTYPE_HEADER;
 
127
  image[5] = 1; /* Version */
 
128
  
 
129
  memcpy (image+8, "KBXf", 4);
 
130
  val = time (NULL);
 
131
  /* created_at and last maintenance run. */
 
132
  image[16]   = (val >> 24);
 
133
  image[16+1] = (val >> 16);
 
134
  image[16+2] = (val >>  8);
 
135
  image[16+3] = (val      );
 
136
  image[20]   = (val >> 24);
 
137
  image[20+1] = (val >> 16);
 
138
  image[20+2] = (val >>  8);
 
139
  image[20+3] = (val      );
 
140
 
 
141
  if (fwrite (image, 32, 1, fp) != 1)
 
142
    return gpg_error (gpg_err_code_from_errno (errno));
 
143
  return 0;
 
144
}
 
145
 
 
146