~ubuntu-branches/ubuntu/natty/gnupg2/natty

« back to all changes in this revision

Viewing changes to common/convert.c

  • Committer: Bazaar Package Importer
  • Author(s): Eric Dorland
  • Date: 2009-03-08 22:46:47 UTC
  • mfrom: (1.1.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20090308224647-gq17gatcl71lrc2k
Tags: 2.0.11-1
* New upstream release. (Closes: #496663)
* debian/control: Make the description a little more distinctive than
  gnupg v1's. Thanks Jari Aalto. (Closes: #496323)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* convert.c - Hex conversion functions.
2
 
 *      Copyright (C) 2006 Free Software Foundation, Inc.
 
2
 *      Copyright (C) 2006, 2008 Free Software Foundation, Inc.
3
3
 *
4
4
 * This file is part of GnuPG.
5
5
 *
30
30
 
31
31
/* Convert STRING consisting of hex characters into its binary
32
32
   representation and store that at BUFFER.  BUFFER needs to be of
33
 
   LENGTH bytes.  The function check that the STRING will convert
 
33
   LENGTH bytes.  The function checks that the STRING will convert
34
34
   exactly to LENGTH bytes. The string is delimited by either end of
35
35
   string or a white space character.  The function returns -1 on
36
36
   error or the length of the parsed string.  */
100
100
}
101
101
 
102
102
 
 
103
 
103
104
static char *
104
105
do_bin2hex (const void *buffer, size_t length, char *stringbuf, int with_colon)
105
106
{
160
161
}
161
162
 
162
163
 
 
164
 
 
165
/* Convert HEXSTRING consisting of hex characters into string and
 
166
   store that at BUFFER.  HEXSTRING is either delimited by end of
 
167
   string or a white space character.  The function makes sure that
 
168
   the resulting string in BUFFER is terminated by a Nul character.
 
169
   BUFSIZE is the availabe length of BUFFER; if the converted result
 
170
   plus a possible required Nul character does not fit into this
 
171
   buffer, the function returns NULL and won't change the existing
 
172
   conent of buffer.  In-place conversion is possible as long as
 
173
   BUFFER points to HEXSTRING.
 
174
   
 
175
   If BUFFER is NULL and bufsize is 0 the function scans HEXSTRING but
 
176
   does not store anything.  This may be used to find the end of
 
177
   hexstring.
 
178
 
 
179
   On sucess the function returns a pointer to the next character
 
180
   after HEXSTRING (which is either end-of-string or a the next white
 
181
   space).  If BUFLEN is not NULL the strlen of buffer is stored
 
182
   there; this will even be done if BUFFER has been passed as NULL. */
 
183
const char *
 
184
hex2str (const char *hexstring, char *buffer, size_t bufsize, size_t *buflen)
 
185
{
 
186
  const char *s = hexstring;
 
187
  int idx, count;
 
188
  int need_nul = 0;
 
189
 
 
190
  if (buflen)
 
191
    *buflen = 0;
 
192
 
 
193
  for (s=hexstring, count=0; hexdigitp (s) && hexdigitp (s+1); s += 2, count++)
 
194
    ;
 
195
  if (*s && (!isascii (*s) || !isspace (*s)) )
 
196
    return NULL;   /* Not followed by Nul or white space.  */
 
197
  /* We need to append a nul character.  However we don't want that if
 
198
     the hexstring already ends with "00".  */
 
199
  need_nul = ((s == hexstring) || !(s[-2] == '0' && s[-1] == '0'));
 
200
  if (need_nul)
 
201
    count++;
 
202
 
 
203
  if (buffer)
 
204
    {
 
205
      if (count > bufsize)
 
206
        return NULL; /* Too long.  */
 
207
      
 
208
      for (s=hexstring, idx=0; hexdigitp (s) && hexdigitp (s+1); s += 2)
 
209
        ((unsigned char*)buffer)[idx++] = xtoi_2 (s);
 
210
      if (need_nul)
 
211
        buffer[idx] = 0;
 
212
    }
 
213
 
 
214
  if (buflen)
 
215
    *buflen = count - 1;
 
216
  return s;
 
217
}
 
218
 
 
219
 
 
220
/* Same as hex2str but this function allocated a new string.  Returns
 
221
   NULL on error.  If R_COUNT is not NULL, the number of scanned bytes
 
222
   will be stored there.  ERRNO is set on error. */
 
223
char *
 
224
hex2str_alloc (const char *hexstring, size_t *r_count)
 
225
{
 
226
  const char *tail;
 
227
  size_t nbytes;
 
228
  char *result;
 
229
 
 
230
  tail = hex2str (hexstring, NULL, 0, &nbytes);
 
231
  if (!tail)
 
232
    {
 
233
      if (r_count)
 
234
        *r_count = 0;
 
235
      errno = EINVAL;
 
236
      return NULL;
 
237
    }
 
238
  if (r_count)
 
239
    *r_count = tail - hexstring;
 
240
  result = xtrymalloc (nbytes+1);
 
241
  if (!result)
 
242
    return NULL;
 
243
  if (!hex2str (hexstring, result, nbytes+1, NULL))
 
244
    BUG ();
 
245
  return result;
 
246
}
 
247
 
 
248
 
 
249