50
51
if (dest_tot_size - dest_size > 0)
52
strncat (dest, src, (dest_tot_size - dest_size) - 1);
53
dest[dest_tot_size - 1] = 0;
53
strncat (dest, src, (dest_tot_size - dest_size) - 1);
54
dest[dest_tot_size - 1] = 0;
69
70
if (dest_tot_size > 0)
71
strncpy (dest, src, (dest_tot_size) - 1);
72
dest[dest_tot_size - 1] = 0;
72
strncpy (dest, src, (dest_tot_size) - 1);
73
dest[dest_tot_size - 1] = 0;
78
79
_gnutls_mem_cpy (char *dest, size_t dest_tot_size, const char *src,
82
83
if (dest_tot_size >= src_size)
88
89
if (dest_tot_size > 0)
90
memcpy (dest, src, dest_tot_size);
91
memcpy (dest, src, dest_tot_size);
96
_gnutls_string_init (gnutls_string * str,
97
gnutls_alloc_function alloc_func,
98
gnutls_realloc_function realloc_func,
99
gnutls_free_function free_func)
97
_gnutls_buffer_init (gnutls_buffer_st * str)
101
99
str->data = str->allocd = NULL;
102
100
str->max_length = 0;
105
str->alloc_func = alloc_func;
106
str->free_func = free_func;
107
str->realloc_func = realloc_func;
111
_gnutls_string_clear (gnutls_string * str)
105
_gnutls_buffer_clear (gnutls_buffer_st * str)
113
107
if (str == NULL || str->allocd == NULL)
115
str->free_func (str->allocd);
109
gnutls_free (str->allocd);
117
111
str->data = str->allocd = NULL;
118
112
str->max_length = 0;
122
#define MIN_CHUNK 256
116
#define MIN_CHUNK 1024
125
_gnutls_string_append_data (gnutls_string * dest, const void *data,
119
_gnutls_buffer_append_data (gnutls_buffer_st * dest, const void *data,
128
122
size_t tot_len = data_size + dest->length;
132
126
size_t unused = MEMSUB (dest->data, dest->allocd);
134
128
if (dest->max_length - unused <= tot_len)
136
if (dest->length && dest->data)
137
memmove (dest->allocd, dest->data, dest->length);
130
if (dest->length && dest->data)
131
memmove (dest->allocd, dest->data, dest->length);
139
dest->data = dest->allocd;
133
dest->data = dest->allocd;
141
135
memmove (&dest->data[dest->length], data, data_size);
142
136
dest->length = tot_len;
148
142
size_t unused = MEMSUB (dest->data, dest->allocd);
150
MAX (data_size, MIN_CHUNK) + MAX (dest->max_length, MIN_CHUNK);
144
MAX (data_size, MIN_CHUNK) + MAX (dest->max_length, MIN_CHUNK);
152
dest->allocd = dest->realloc_func (dest->allocd, new_len);
146
dest->allocd = gnutls_realloc (dest->allocd, new_len);
153
147
if (dest->allocd == NULL)
156
return GNUTLS_E_MEMORY_ERROR;
150
return GNUTLS_E_MEMORY_ERROR;
158
152
dest->max_length = new_len;
159
153
dest->data = dest->allocd + unused;
161
155
if (dest->length && dest->data)
162
memmove (dest->allocd, dest->data, dest->length);
156
memmove (dest->allocd, dest->data, dest->length);
163
157
dest->data = dest->allocd;
165
159
memcpy (&dest->data[dest->length], data, data_size);
173
_gnutls_string_resize (gnutls_string * dest, size_t new_size)
167
_gnutls_buffer_resize (gnutls_buffer_st * dest, size_t new_size)
175
169
if (dest->max_length >= new_size)
177
171
size_t unused = MEMSUB (dest->data, dest->allocd);
178
172
if (dest->max_length - unused <= new_size)
180
if (dest->length && dest->data)
181
memmove (dest->allocd, dest->data, dest->length);
182
dest->data = dest->allocd;
174
if (dest->length && dest->data)
175
memmove (dest->allocd, dest->data, dest->length);
176
dest->data = dest->allocd;
189
183
size_t unused = MEMSUB (dest->data, dest->allocd);
190
184
size_t alloc_len =
191
MAX (new_size, MIN_CHUNK) + MAX (dest->max_length, MIN_CHUNK);
185
MAX (new_size, MIN_CHUNK) + MAX (dest->max_length, MIN_CHUNK);
193
dest->allocd = dest->realloc_func (dest->allocd, alloc_len);
187
dest->allocd = gnutls_realloc (dest->allocd, alloc_len);
194
188
if (dest->allocd == NULL)
197
return GNUTLS_E_MEMORY_ERROR;
191
return GNUTLS_E_MEMORY_ERROR;
199
193
dest->max_length = alloc_len;
200
194
dest->data = dest->allocd + unused;
202
196
if (dest->length && dest->data)
203
memmove (dest->allocd, dest->data, dest->length);
197
memmove (dest->allocd, dest->data, dest->length);
204
198
dest->data = dest->allocd;
211
_gnutls_string_append_str (gnutls_string * dest, const char *src)
205
_gnutls_buffer_append_str (gnutls_buffer_st * dest, const char *src)
213
return _gnutls_string_append_data (dest, src, strlen (src));
207
return _gnutls_buffer_append_data (dest, src, strlen (src));
216
210
/* returns data from a string in a constant buffer.
244
/* converts the buffer to a datum if possible. After this call the buffer
245
* is at an usable state and might not be used or deinitialized */
247
_gnutls_buffer_to_datum (gnutls_buffer_st * str, gnutls_datum_t * data)
250
if (str->length == 0)
257
if (str->allocd != str->data)
259
data->data = gnutls_malloc (str->length);
260
if (data->data == NULL)
263
return GNUTLS_E_MEMORY_ERROR;
265
memcpy (data->data, str->data, str->length);
266
data->size = str->length;
267
_gnutls_buffer_clear (str);
271
data->data = str->data;
272
data->size = str->length;
250
278
/* returns data from a string in a constant buffer.
253
_gnutls_string_get_data (gnutls_string * str, void *data, size_t * req_size)
281
_gnutls_buffer_pop_data (gnutls_buffer_st * str, void *data,
255
284
gnutls_datum_t tdata;
257
_gnutls_string_get_datum (str, &tdata, *req_size);
286
_gnutls_buffer_pop_datum (str, &tdata, *req_size);
259
288
*req_size = tdata.size;
260
289
memcpy (data, tdata.data, tdata.size);
276
305
if (len < 0 || !str)
279
len = _gnutls_string_append_str (dest, str);
308
len = _gnutls_buffer_append_str (dest, str);
316
_gnutls_buffer_insert_data (gnutls_buffer_st * dest, int pos, const void *str,
319
size_t orig_length = dest->length;
322
ret = _gnutls_buffer_resize (dest, dest->length + str_size); /* resize to make space */
326
memmove (&dest->data[pos + str_size], &dest->data[pos], orig_length - pos);
328
memcpy (&dest->data[pos], str, str_size);
329
dest->length += str_size;
335
_gnutls_buffer_delete_data (gnutls_buffer_st * dest, int pos, size_t str_size)
337
memmove (&dest->data[pos], &dest->data[pos + str_size],
338
dest->length - pos - str_size);
340
dest->length -= str_size;
347
_gnutls_buffer_escape (gnutls_buffer_st * dest, int all,
348
const char *const invalid_chars)
354
while (pos < dest->length)
357
if (all != 0 || (dest->data[pos] == '\\' || strchr (invalid_chars, dest->data[pos])
358
|| !c_isgraph (dest->data[pos])))
361
snprintf (t, sizeof (t), "%%%.2X", (unsigned int) dest->data[pos]);
363
_gnutls_buffer_delete_data (dest, pos, 1);
365
if (_gnutls_buffer_insert_data (dest, pos, t, 3) < 0)
384
_gnutls_buffer_unescape (gnutls_buffer_st * dest)
389
while (pos < dest->length)
391
if (dest->data[pos] == '%')
397
b[0] = dest->data[pos + 1];
398
b[1] = dest->data[pos + 2];
401
sscanf (b, "%02x", &u);
405
_gnutls_buffer_delete_data (dest, pos, 3);
406
_gnutls_buffer_insert_data (dest, pos, &x, 1);
286
417
/* Converts the given string (old) to hex. A buffer must be provided
287
418
* to hold the new hex string. The new string will be null terminated.
288
419
* If the buffer does not have enough space to hold the string, a
292
423
_gnutls_bin2hex (const void *_old, size_t oldlen,
293
char *buffer, size_t buffer_size)
424
char *buffer, size_t buffer_size, const char *separator)
295
426
unsigned int i, j;
296
427
const opaque *old = _old;
298
for (i = j = 0; i < oldlen && j + 2 < buffer_size; j += 2)
300
sprintf (&buffer[j], "%.2x", old[i]);
429
const char empty[] = "";
431
if (separator != NULL && separator[0] != 0)
443
sprintf (&buffer[j], "%.2x", old[i]);
447
for (; i < oldlen && j + step < buffer_size; j += step)
449
sprintf (&buffer[j], "%s%.2x", separator, old[i]);
303
452
buffer[j] = '\0';
323
472
gnutls_hex2bin (const char *hex_data,
324
size_t hex_size, char *bin_data, size_t * bin_size)
473
size_t hex_size, char *bin_data, size_t * bin_size)
326
475
return _gnutls_hex2bin (hex_data, (int) hex_size, bin_data, bin_size);
330
479
_gnutls_hex2bin (const opaque * hex_data, int hex_size, opaque * bin_data,
334
483
opaque hex2_data[3];
335
484
unsigned long val;
337
/* FIXME: we don't handle whitespace.
341
if (*bin_size < (size_t) hex_size)
344
return GNUTLS_E_SHORT_MEMORY_BUFFER;
347
for (i = j = 0; j < hex_size; i += 2, j++)
488
for (i = j = 0; i < hex_size;)
490
if (!isxdigit (hex_data[i])) /* skip non-hex such as the ':' in 00:FF */
499
return GNUTLS_E_SHORT_MEMORY_BUFFER;
349
502
hex2_data[0] = hex_data[i];
350
503
hex2_data[1] = hex_data[i + 1];
352
506
val = strtoul ((char *) hex2_data, NULL, 16);
353
507
if (val == ULONG_MAX)
356
return GNUTLS_E_SRP_PWD_PARSING_ERROR;
510
return GNUTLS_E_PARSING_ERROR;
358
512
bin_data[j] = val;
366
522
* return 1 on success or 0 on error
368
524
* note: certnamesize is required as X509 certs can contain embedded NULs in
369
* the strings such as CN or subjectAltName
525
* the strings such as CN or subjectAltName.
527
* @level: is used for recursion. Use 0 when you call this function.
372
530
_gnutls_hostname_compare (const char *certname,
373
size_t certnamesize, const char *hostname)
531
size_t certnamesize, const char *hostname, int level)
375
537
/* find the first different character */
376
for (; *certname && *hostname && toupper (*certname) == toupper (*hostname);
538
for (; *certname && *hostname && c_toupper (*certname) == c_toupper (*hostname);
377
539
certname++, hostname++, certnamesize--)
393
/* Use a recursive call to allow multiple wildcards */
394
if (_gnutls_hostname_compare (certname, certnamesize, hostname))
555
/* Use a recursive call to allow multiple wildcards */
556
if (_gnutls_hostname_compare (certname, certnamesize, hostname, level+1))
397
/* wildcards are only allowed to match a single domain
398
component or component fragment */
399
if (*hostname == '\0' || *hostname == '.')
559
/* wildcards are only allowed to match a single domain
560
component or component fragment */
561
if (*hostname == '\0' || *hostname == '.')
573
_gnutls_buffer_append_prefix (gnutls_buffer_st * buf, size_t data_size)
576
_gnutls_write_uint32 (data_size, ss);
577
return _gnutls_buffer_append_data (buf, ss, 4);
580
/* Reads an uint32 number from the buffer. If check is non zero it will also check whether
581
* the number read, is less than the data in the buffer
584
_gnutls_buffer_pop_prefix (gnutls_buffer_st * buf, size_t * data_size,
592
return GNUTLS_E_PARSING_ERROR;
595
size = _gnutls_read_uint32 (buf->data);
596
if (check && size > buf->length - 4)
599
return GNUTLS_E_PARSING_ERROR;
611
_gnutls_buffer_pop_datum_prefix (gnutls_buffer_st * buf,
612
gnutls_datum_t * data)
617
ret = _gnutls_buffer_pop_prefix (buf, &size, 1);
627
_gnutls_buffer_pop_datum (buf, data, size);
628
if (osize != data->size)
631
return GNUTLS_E_PARSING_ERROR;
644
_gnutls_buffer_append_data_prefix (gnutls_buffer_st * buf, const void *data,
647
_gnutls_buffer_append_prefix (buf, data_size);
649
return _gnutls_buffer_append_data (buf, data, data_size);
655
_gnutls_buffer_pop_data_prefix (gnutls_buffer_st * buf, void *data,
661
ret = _gnutls_buffer_pop_prefix (buf, &size, 1);
669
_gnutls_buffer_pop_data (buf, data, data_size);