2008-05-15 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / g10 / comment.c
blob7f9295ea5c785946802b930830ffe3346bb1f953
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 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 <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <assert.h>
27 #include "options.h"
28 #include "packet.h"
29 #include "status.h"
30 #include "iobuf.h"
31 #include "util.h"
32 #include "main.h"
33 #include "keydb.h"
37 int
38 write_comment( iobuf_t out, const char *s )
40 PACKET pkt;
41 size_t n = strlen(s);
42 int rc=0;
44 pkt.pkttype = PKT_COMMENT;
45 if( *s != '#' ) {
46 pkt.pkt.comment = xmalloc ( sizeof *pkt.pkt.comment + n );
47 pkt.pkt.comment->len = n+1;
48 *pkt.pkt.comment->data = '#';
49 strcpy(pkt.pkt.comment->data+1, s);
51 else {
52 pkt.pkt.comment = xmalloc ( sizeof *pkt.pkt.comment + n - 1 );
53 pkt.pkt.comment->len = n;
54 strcpy(pkt.pkt.comment->data, s);
56 if( (rc = build_packet( out, &pkt )) )
57 log_error("build_packet(comment) failed: %s\n", gpg_strerror (rc) );
58 free_packet( &pkt );
59 return rc;
63 KBNODE
64 make_comment_node_from_buffer (const char *s, size_t n)
66 PACKET *pkt;
68 pkt = gcry_xcalloc( 1, sizeof *pkt );
69 pkt->pkttype = PKT_COMMENT;
70 pkt->pkt.comment = gcry_xmalloc( sizeof *pkt->pkt.comment + n - 1 );
71 pkt->pkt.comment->len = n;
72 strcpy(pkt->pkt.comment->data, s);
73 return new_kbnode( pkt );
76 KBNODE
77 make_comment_node( const char *s )
79 return make_comment_node_from_buffer (s, strlen (s));
83 KBNODE
84 make_mpi_comment_node( const char *s, gcry_mpi_t a )
86 PACKET *pkt;
87 byte *buf, *pp;
88 size_t n1, nb1;
89 size_t n = strlen(s);
91 nb1 = mpi_get_nbits( a );
92 if (gcry_mpi_print (GCRYMPI_FMT_PGP, NULL, 0, &n1, a))
93 BUG ();
94 /* fixme: allocate it on the stack */
95 buf = xmalloc (n1);
96 if (gcry_mpi_print (GCRYMPI_FMT_PGP, buf, n1, &n1, a))
97 BUG ();
99 pkt = xcalloc (1, sizeof *pkt );
100 pkt->pkttype = PKT_COMMENT;
101 pkt->pkt.comment = xmalloc ( sizeof *pkt->pkt.comment + n + 2 + n1 );
102 pkt->pkt.comment->len = n+1+2+n1;
103 pp = pkt->pkt.comment->data;
104 memcpy(pp, s, n+1);
105 memcpy(pp+n+1, buf, n1 );
106 xfree (buf);
107 return new_kbnode( pkt );