2006-09-06 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / g10 / comment.c
blob1930871073dc1f802df239ce1ad1a66e168a77ee
1 /* comment.c - write comment stuff
2 * Copyright (C) 1998, 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 <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <assert.h>
29 #include "options.h"
30 #include "packet.h"
31 #include "errors.h"
32 #include "iobuf.h"
33 #include "memory.h"
34 #include "util.h"
35 #include "main.h"
36 #include "keydb.h"
40 int
41 write_comment( iobuf_t out, const char *s )
43 PACKET pkt;
44 size_t n = strlen(s);
45 int rc=0;
47 pkt.pkttype = PKT_COMMENT;
48 if( *s != '#' ) {
49 pkt.pkt.comment = xmalloc ( sizeof *pkt.pkt.comment + n );
50 pkt.pkt.comment->len = n+1;
51 *pkt.pkt.comment->data = '#';
52 strcpy(pkt.pkt.comment->data+1, s);
54 else {
55 pkt.pkt.comment = xmalloc ( sizeof *pkt.pkt.comment + n - 1 );
56 pkt.pkt.comment->len = n;
57 strcpy(pkt.pkt.comment->data, s);
59 if( (rc = build_packet( out, &pkt )) )
60 log_error("build_packet(comment) failed: %s\n", gpg_strerror (rc) );
61 free_packet( &pkt );
62 return rc;
66 KBNODE
67 make_comment_node_from_buffer (const char *s, size_t n)
69 PACKET *pkt;
71 pkt = gcry_xcalloc( 1, sizeof *pkt );
72 pkt->pkttype = PKT_COMMENT;
73 pkt->pkt.comment = gcry_xmalloc( sizeof *pkt->pkt.comment + n - 1 );
74 pkt->pkt.comment->len = n;
75 strcpy(pkt->pkt.comment->data, s);
76 return new_kbnode( pkt );
79 KBNODE
80 make_comment_node( const char *s )
82 return make_comment_node_from_buffer (s, strlen (s));
86 KBNODE
87 make_mpi_comment_node( const char *s, gcry_mpi_t a )
89 PACKET *pkt;
90 byte *buf, *pp;
91 size_t n1, nb1;
92 size_t n = strlen(s);
94 nb1 = mpi_get_nbits( a );
95 if (gcry_mpi_print (GCRYMPI_FMT_PGP, NULL, 0, &n1, a))
96 BUG ();
97 /* fixme: allocate it on the stack */
98 buf = xmalloc (n1);
99 if (gcry_mpi_print (GCRYMPI_FMT_PGP, buf, n1, &n1, a))
100 BUG ();
102 pkt = xcalloc (1, sizeof *pkt );
103 pkt->pkttype = PKT_COMMENT;
104 pkt->pkt.comment = xmalloc ( sizeof *pkt->pkt.comment + n + 2 + n1 );
105 pkt->pkt.comment->len = n+1+2+n1;
106 pp = pkt->pkt.comment->data;
107 memcpy(pp, s, n+1);
108 memcpy(pp+n+1, buf, n1 );
109 xfree (buf);
110 return new_kbnode( pkt );