summaryrefslogtreecommitdiff
path: root/crypto/compat
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/compat')
-rw-r--r--crypto/compat/asprintf.c94
-rw-r--r--crypto/compat/bsd-asprintf.c29
2 files changed, 13 insertions, 110 deletions
diff --git a/crypto/compat/asprintf.c b/crypto/compat/asprintf.c
deleted file mode 100644
index 45a539d..0000000
--- a/crypto/compat/asprintf.c
+++ /dev/null
@@ -1,94 +0,0 @@
1/*
2 * Copyright (c) 2004 Darren Tucker.
3 *
4 * Based originally on asprintf.c from OpenBSD:
5 * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#include <errno.h>
21#include <stdarg.h>
22#include <stdlib.h>
23
24#ifndef VA_COPY
25# ifdef HAVE_VA_COPY
26# define VA_COPY(dest, src) va_copy(dest, src)
27# else
28# ifdef HAVE___VA_COPY
29# define VA_COPY(dest, src) __va_copy(dest, src)
30# else
31# define VA_COPY(dest, src) (dest) = (src)
32# endif
33# endif
34#endif
35
36#define INIT_SZ 128
37
38int
39vasprintf(char **str, const char *fmt, va_list ap)
40{
41 int ret = -1;
42 va_list ap2;
43 char *string, *newstr;
44 size_t len;
45
46 VA_COPY(ap2, ap);
47 if ((string = malloc(INIT_SZ)) == NULL)
48 goto fail;
49
50 ret = vsnprintf(string, INIT_SZ, fmt, ap2);
51 if (ret >= 0 && ret < INIT_SZ) { /* succeeded with initial alloc */
52 *str = string;
53 } else if (ret == INT_MAX || ret < 0) { /* Bad length */
54 free(string);
55 goto fail;
56 } else { /* bigger than initial, realloc allowing for nul */
57 len = (size_t)ret + 1;
58 if ((newstr = realloc(string, len)) == NULL) {
59 free(string);
60 goto fail;
61 } else {
62 va_end(ap2);
63 VA_COPY(ap2, ap);
64 ret = vsnprintf(newstr, len, fmt, ap2);
65 if (ret >= 0 && (size_t)ret < len) {
66 *str = newstr;
67 } else { /* failed with realloc'ed string, give up */
68 free(newstr);
69 goto fail;
70 }
71 }
72 }
73 va_end(ap2);
74 return (ret);
75
76fail:
77 *str = NULL;
78 errno = ENOMEM;
79 va_end(ap2);
80 return (-1);
81}
82
83int asprintf(char **str, const char *fmt, ...)
84{
85 va_list ap;
86 int ret;
87
88 *str = NULL;
89 va_start(ap, fmt);
90 ret = vasprintf(str, fmt, ap);
91 va_end(ap);
92
93 return ret;
94}
diff --git a/crypto/compat/bsd-asprintf.c b/crypto/compat/bsd-asprintf.c
index 3368195..8ccfa22 100644
--- a/crypto/compat/bsd-asprintf.c
+++ b/crypto/compat/bsd-asprintf.c
@@ -17,12 +17,12 @@
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */ 18 */
19 19
20#include "includes.h"
21
22#ifndef HAVE_VASPRINTF 20#ifndef HAVE_VASPRINTF
23 21
24#include <errno.h> 22#include <errno.h>
23#include <limits.h> /* for INT_MAX */
25#include <stdarg.h> 24#include <stdarg.h>
25#include <stdio.h> /* for vsnprintf */
26#include <stdlib.h> 26#include <stdlib.h>
27 27
28#ifndef VA_COPY 28#ifndef VA_COPY
@@ -42,16 +42,17 @@
42int 42int
43vasprintf(char **str, const char *fmt, va_list ap) 43vasprintf(char **str, const char *fmt, va_list ap)
44{ 44{
45 int ret = -1; 45 int ret;
46 va_list ap2; 46 va_list ap2;
47 char *string, *newstr; 47 char *string, *newstr;
48 size_t len; 48 size_t len;
49 49
50 VA_COPY(ap2, ap);
51 if ((string = malloc(INIT_SZ)) == NULL) 50 if ((string = malloc(INIT_SZ)) == NULL)
52 goto fail; 51 goto fail;
53 52
53 VA_COPY(ap2, ap);
54 ret = vsnprintf(string, INIT_SZ, fmt, ap2); 54 ret = vsnprintf(string, INIT_SZ, fmt, ap2);
55 va_end(ap2);
55 if (ret >= 0 && ret < INIT_SZ) { /* succeeded with initial alloc */ 56 if (ret >= 0 && ret < INIT_SZ) { /* succeeded with initial alloc */
56 *str = string; 57 *str = string;
57 } else if (ret == INT_MAX || ret < 0) { /* Bad length */ 58 } else if (ret == INT_MAX || ret < 0) { /* Bad length */
@@ -62,25 +63,21 @@ vasprintf(char **str, const char *fmt, va_list ap)
62 if ((newstr = realloc(string, len)) == NULL) { 63 if ((newstr = realloc(string, len)) == NULL) {
63 free(string); 64 free(string);
64 goto fail; 65 goto fail;
65 } else {
66 va_end(ap2);
67 VA_COPY(ap2, ap);
68 ret = vsnprintf(newstr, len, fmt, ap2);
69 if (ret >= 0 && (size_t)ret < len) {
70 *str = newstr;
71 } else { /* failed with realloc'ed string, give up */
72 free(newstr);
73 goto fail;
74 }
75 } 66 }
67 VA_COPY(ap2, ap);
68 ret = vsnprintf(newstr, len, fmt, ap2);
69 va_end(ap2);
70 if (ret < 0 || (size_t)ret >= len) { /* failed with realloc'ed string */
71 free(newstr);
72 goto fail;
73 }
74 *str = newstr;
76 } 75 }
77 va_end(ap2);
78 return (ret); 76 return (ret);
79 77
80fail: 78fail:
81 *str = NULL; 79 *str = NULL;
82 errno = ENOMEM; 80 errno = ENOMEM;
83 va_end(ap2);
84 return (-1); 81 return (-1);
85} 82}
86#endif 83#endif