Line | Count | Source |
1 | | /* |
2 | | * $Id: printbuf.c,v 1.5 2006/01/26 02:16:28 mclark Exp $ |
3 | | * |
4 | | * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd. |
5 | | * Michael Clark <michael@metaparadigm.com> |
6 | | * |
7 | | * This library is free software; you can redistribute it and/or modify |
8 | | * it under the terms of the MIT license. See COPYING for details. |
9 | | * |
10 | | * |
11 | | * Copyright (c) 2008-2009 Yahoo! Inc. All rights reserved. |
12 | | * The copyrights to the contents of this file are licensed under the MIT License |
13 | | * (https://www.opensource.org/licenses/mit-license.php) |
14 | | */ |
15 | | |
16 | | #include "config.h" |
17 | | |
18 | | #include <errno.h> |
19 | | #include <limits.h> |
20 | | #include <stdio.h> |
21 | | #include <stdlib.h> |
22 | | #include <string.h> |
23 | | |
24 | | #ifdef HAVE_STDARG_H |
25 | | #include <stdarg.h> |
26 | | #else /* !HAVE_STDARG_H */ |
27 | | #error Not enough var arg support! |
28 | | #endif /* HAVE_STDARG_H */ |
29 | | |
30 | | #include "debug.h" |
31 | | #include "json_inttypes.h" |
32 | | #include "printbuf.h" |
33 | | #include "snprintf_compat.h" |
34 | | #include "vasprintf_compat.h" |
35 | | |
36 | | static int printbuf_extend(struct printbuf *p, int min_size); |
37 | | |
38 | | struct printbuf *printbuf_new(void) |
39 | 11.5k | { |
40 | 11.5k | struct printbuf *p; |
41 | | |
42 | 11.5k | p = (struct printbuf *)calloc(1, sizeof(struct printbuf)); |
43 | 11.5k | if (!p) |
44 | 0 | return NULL; |
45 | 11.5k | p->size = 32; |
46 | 11.5k | p->bpos = 0; |
47 | 11.5k | if (!(p->buf = (char *)malloc(p->size))) |
48 | 0 | { |
49 | 0 | free(p); |
50 | 0 | return NULL; |
51 | 0 | } |
52 | 11.5k | p->buf[0] = '\0'; |
53 | 11.5k | return p; |
54 | 11.5k | } |
55 | | |
56 | | /** |
57 | | * Extend the buffer p so it has a size of at least min_size. |
58 | | * |
59 | | * If the current size is large enough, nothing is changed. |
60 | | * |
61 | | * If extension failed, errno is set to indicate the error. |
62 | | * |
63 | | * Note: this does not check the available space! The caller |
64 | | * is responsible for performing those calculations. |
65 | | */ |
66 | | static int printbuf_extend(struct printbuf *p, int min_size) |
67 | 22.3k | { |
68 | 22.3k | char *t; |
69 | 22.3k | int new_size; |
70 | | |
71 | 22.3k | if (p->size >= min_size) |
72 | 3.61k | return 0; |
73 | | /* Prevent signed integer overflows with large buffers. */ |
74 | 18.7k | if (min_size > INT_MAX - 8) |
75 | 0 | { |
76 | 0 | errno = EFBIG; |
77 | 0 | return -1; |
78 | 0 | } |
79 | 18.7k | if (p->size > INT_MAX / 2) |
80 | 0 | new_size = min_size + 8; |
81 | 18.7k | else { |
82 | 18.7k | new_size = p->size * 2; |
83 | 18.7k | if (new_size < min_size + 8) |
84 | 459 | new_size = min_size + 8; |
85 | 18.7k | } |
86 | | #ifdef PRINTBUF_DEBUG |
87 | | MC_DEBUG("printbuf_extend: realloc " |
88 | | "bpos=%d min_size=%d old_size=%d new_size=%d\n", |
89 | | p->bpos, min_size, p->size, new_size); |
90 | | #endif /* PRINTBUF_DEBUG */ |
91 | 18.7k | if (!(t = (char *)realloc(p->buf, new_size))) |
92 | 0 | return -1; |
93 | 18.7k | p->size = new_size; |
94 | 18.7k | p->buf = t; |
95 | 18.7k | return 0; |
96 | 18.7k | } |
97 | | |
98 | | int printbuf_memappend(struct printbuf *p, const char *buf, int size) |
99 | 978k | { |
100 | | /* Prevent signed integer overflows with large buffers. */ |
101 | 978k | if (size < 0 || size > INT_MAX - p->bpos - 1) |
102 | 0 | { |
103 | 0 | errno = EFBIG; |
104 | 0 | return -1; |
105 | 0 | } |
106 | 978k | if (p->size <= p->bpos + size + 1) |
107 | 22.3k | { |
108 | 22.3k | int buf_offset = -1; |
109 | 22.3k | const uintptr_t buf_addr = (uintptr_t)(const void *)buf; |
110 | 22.3k | const uintptr_t p_buf_addr = (uintptr_t)(const void *)p->buf; |
111 | | |
112 | | /* realloc() invalidates source pointers into the old buffer. */ |
113 | 22.3k | if (buf_addr >= p_buf_addr && buf_addr - p_buf_addr < (uintptr_t)p->size) |
114 | 0 | buf_offset = (int)(buf_addr - p_buf_addr); |
115 | 22.3k | if (printbuf_extend(p, p->bpos + size + 1) < 0) |
116 | 0 | return -1; |
117 | 22.3k | if (buf_offset >= 0) |
118 | 0 | buf = p->buf + buf_offset; |
119 | 22.3k | } |
120 | 978k | memmove(p->buf + p->bpos, buf, size); |
121 | 978k | p->bpos += size; |
122 | 978k | p->buf[p->bpos] = '\0'; |
123 | 978k | return size; |
124 | 978k | } |
125 | | |
126 | | int printbuf_memset(struct printbuf *pb, int offset, int charvalue, int len) |
127 | 0 | { |
128 | 0 | int size_needed; |
129 | |
|
130 | 0 | if (offset == -1) |
131 | 0 | offset = pb->bpos; |
132 | | /* Prevent signed integer overflows with large buffers. */ |
133 | 0 | if (len < 0 || offset < -1 || len > INT_MAX - offset) |
134 | 0 | { |
135 | 0 | errno = EFBIG; |
136 | 0 | return -1; |
137 | 0 | } |
138 | 0 | size_needed = offset + len; |
139 | 0 | if (pb->size < size_needed) |
140 | 0 | { |
141 | 0 | if (printbuf_extend(pb, size_needed) < 0) |
142 | 0 | return -1; |
143 | 0 | } |
144 | | |
145 | 0 | if (pb->bpos < offset) |
146 | 0 | memset(pb->buf + pb->bpos, '\0', offset - pb->bpos); |
147 | 0 | memset(pb->buf + offset, charvalue, len); |
148 | 0 | if (pb->bpos < size_needed) |
149 | 0 | pb->bpos = size_needed; |
150 | |
|
151 | 0 | return 0; |
152 | 0 | } |
153 | | |
154 | | int sprintbuf(struct printbuf *p, const char *msg, ...) |
155 | 0 | { |
156 | 0 | va_list ap; |
157 | 0 | char *t; |
158 | 0 | int size; |
159 | 0 | char buf[128]; |
160 | | |
161 | | /* use stack buffer first */ |
162 | 0 | va_start(ap, msg); |
163 | 0 | size = vsnprintf(buf, 128, msg, ap); |
164 | 0 | va_end(ap); |
165 | | /* if string is greater than stack buffer, then use dynamic string |
166 | | * with vasprintf. Note: some implementations of vsnprintf return -1 |
167 | | * if output is truncated whereas some return the number of bytes that |
168 | | * would have been written - this code handles both cases. |
169 | | */ |
170 | 0 | if (size < 0 || size > 127) |
171 | 0 | { |
172 | 0 | va_start(ap, msg); |
173 | 0 | if ((size = vasprintf(&t, msg, ap)) < 0) |
174 | 0 | { |
175 | 0 | va_end(ap); |
176 | 0 | return -1; |
177 | 0 | } |
178 | 0 | va_end(ap); |
179 | 0 | size = printbuf_memappend(p, t, size); |
180 | 0 | free(t); |
181 | 0 | } |
182 | 0 | else |
183 | 0 | { |
184 | 0 | size = printbuf_memappend(p, buf, size); |
185 | 0 | } |
186 | 0 | return size; |
187 | 0 | } |
188 | | |
189 | | void printbuf_reset(struct printbuf *p) |
190 | 193k | { |
191 | 193k | p->buf[0] = '\0'; |
192 | 193k | p->bpos = 0; |
193 | 193k | } |
194 | | |
195 | | void printbuf_free(struct printbuf *p) |
196 | 180k | { |
197 | 180k | if (p) |
198 | 11.5k | { |
199 | 11.5k | free(p->buf); |
200 | 11.5k | free(p); |
201 | 11.5k | } |
202 | 180k | } |