aboutsummaryrefslogtreecommitdiffstats
path: root/kbx/keybox-file.c
blob: 66bca504f69eb3d2820cd9ca96873ec9fd7daa12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* 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"


int
_keybox_read_blob (KEYBOXBLOB *r_blob, FILE *fp)
{
  char *image;
  size_t imagelen = 0;
  int c1, c2, c3, c4;
  int rc;

  *r_blob = NULL;
  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;
  }

  imagelen = (c1 << 24) | (c2 << 16) | (c3 << 8 ) | c4;
  if (imagelen > 500000) /* sanity check */
    return KEYBOX_Blob_Too_Large;
  
  if (imagelen < 4) 
    return KEYBOX_Blob_Too_Short;
    
  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)
    {
      return KEYBOX_Read_Error;
    }
  
  rc = _keybox_new_blob (r_blob, image, imagelen);
  if (rc)
      xfree (image);
  return rc;
}





void
export_as_kbxfile(void)
{
#if 0
    KBPOS kbpos;
    KBNODE keyblock = NULL;
    int rc=0;

    rc = enum_keyblocks_begin( &kbpos, 0 );
    if( rc ) {
	if( rc != -1 )
	    log_error("enum_keyblocks(open) failed: %s\n", gpg_errstr(rc) );
	goto leave;
    }

    while( !(rc = enum_keyblocks_next( kbpos, 1, &keyblock )) ) {
	KBXBLOB blob;
	const char *p;
	size_t n;

	merge_keys_and_selfsig( keyblock );
	rc = kbx_create_blob ( &blob, keyblock );
	if( rc ) {
	    log_error("kbx_create_blob failed: %s\n", gpg_errstr(rc) );
	    goto leave;
	}
	p = kbx_get_blob_image ( blob, &n );
	fwrite( p, n, 1, stdout );
	kbx_release_blob ( blob );
    }

    if( rc && rc != -1 )
	log_error("enum_keyblocks(read) failed: %s\n", gpg_errstr(rc));

  leave:
    enum_keyblocks_end( kbpos );
    release_kbnode( keyblock );
#endif
}