aboutsummaryrefslogtreecommitdiff
path: root/crypto
diff options
context:
space:
mode:
authorBrent Cook <bcook@openbsd.org>2014-07-28 19:26:15 -0500
committerBrent Cook <bcook@openbsd.org>2014-07-29 11:17:22 -0500
commit35e2d8d0485322558a8c4b726674a5293d6aec37 (patch)
treead0d41a52ecaa23306c8207f344598f83cbe16ed /crypto
parent02ad0041c4f055c541be15ce3f4df9fd65236004 (diff)
downloadportable-35e2d8d0485322558a8c4b726674a5293d6aec37.tar.gz
portable-35e2d8d0485322558a8c4b726674a5293d6aec37.tar.bz2
portable-35e2d8d0485322558a8c4b726674a5293d6aec37.zip
add asprintf / vasprintf from OpenSSH portable
ok deraadt@ beck@
Diffstat (limited to 'crypto')
-rw-r--r--crypto/Makefile.am.tpl4
-rw-r--r--crypto/compat/asprintf.c94
2 files changed, 98 insertions, 0 deletions
diff --git a/crypto/Makefile.am.tpl b/crypto/Makefile.am.tpl
index 6ac6c4f..7688fb9 100644
--- a/crypto/Makefile.am.tpl
+++ b/crypto/Makefile.am.tpl
@@ -38,6 +38,10 @@ libcompat_la_SOURCES += compat/strndup.c
38libcompat_la_SOURCES += compat/strnlen.c 38libcompat_la_SOURCES += compat/strnlen.c
39endif 39endif
40 40
41if NO_ASPRINTF
42libcompat_la_SOURCES += compat/asprintf.c
43endif
44
41if NO_REALLOCARRAY 45if NO_REALLOCARRAY
42libcompat_la_SOURCES += compat/reallocarray.c 46libcompat_la_SOURCES += compat/reallocarray.c
43endif 47endif
diff --git a/crypto/compat/asprintf.c b/crypto/compat/asprintf.c
new file mode 100644
index 0000000..45a539d
--- /dev/null
+++ b/crypto/compat/asprintf.c
@@ -0,0 +1,94 @@
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}