~ubuntu-branches/ubuntu/jaunty/gnupg2/jaunty

« back to all changes in this revision

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