Imported Upstream version 0.17
[platform/upstream/json-c.git] / printbuf.c
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 "printbuf.h"
32 #include "snprintf_compat.h"
33 #include "vasprintf_compat.h"
34
35 static int printbuf_extend(struct printbuf *p, int min_size);
36
37 struct printbuf *printbuf_new(void)
38 {
39         struct printbuf *p;
40
41         p = (struct printbuf *)calloc(1, sizeof(struct printbuf));
42         if (!p)
43                 return NULL;
44         p->size = 32;
45         p->bpos = 0;
46         if (!(p->buf = (char *)malloc(p->size)))
47         {
48                 free(p);
49                 return NULL;
50         }
51         p->buf[0] = '\0';
52         return p;
53 }
54
55 /**
56  * Extend the buffer p so it has a size of at least min_size.
57  *
58  * If the current size is large enough, nothing is changed.
59  *
60  * If extension failed, errno is set to indicate the error.
61  *
62  * Note: this does not check the available space!  The caller
63  *  is responsible for performing those calculations.
64  */
65 static int printbuf_extend(struct printbuf *p, int min_size)
66 {
67         char *t;
68         int new_size;
69
70         if (p->size >= min_size)
71                 return 0;
72         /* Prevent signed integer overflows with large buffers. */
73         if (min_size > INT_MAX - 8)
74         {
75                 errno = EFBIG;
76                 return -1;
77         }
78         if (p->size > INT_MAX / 2)
79                 new_size = min_size + 8;
80         else {
81                 new_size = p->size * 2;
82                 if (new_size < min_size + 8)
83                         new_size = min_size + 8;
84         }
85 #ifdef PRINTBUF_DEBUG
86         MC_DEBUG("printbuf_extend: realloc "
87                  "bpos=%d min_size=%d old_size=%d new_size=%d\n",
88                  p->bpos, min_size, p->size, new_size);
89 #endif /* PRINTBUF_DEBUG */
90         if (!(t = (char *)realloc(p->buf, new_size)))
91                 return -1;
92         p->size = new_size;
93         p->buf = t;
94         return 0;
95 }
96
97 int printbuf_memappend(struct printbuf *p, const char *buf, int size)
98 {
99         /* Prevent signed integer overflows with large buffers. */
100         if (size < 0 || size > INT_MAX - p->bpos - 1)
101         {
102                 errno = EFBIG;
103                 return -1;
104         }
105         if (p->size <= p->bpos + size + 1)
106         {
107                 if (printbuf_extend(p, p->bpos + size + 1) < 0)
108                         return -1;
109         }
110         memcpy(p->buf + p->bpos, buf, size);
111         p->bpos += size;
112         p->buf[p->bpos] = '\0';
113         return size;
114 }
115
116 int printbuf_memset(struct printbuf *pb, int offset, int charvalue, int len)
117 {
118         int size_needed;
119
120         if (offset == -1)
121                 offset = pb->bpos;
122         /* Prevent signed integer overflows with large buffers. */
123         if (len < 0 || offset < -1 || len > INT_MAX - offset)
124         {
125                 errno = EFBIG;
126                 return -1;
127         }
128         size_needed = offset + len;
129         if (pb->size < size_needed)
130         {
131                 if (printbuf_extend(pb, size_needed) < 0)
132                         return -1;
133         }
134
135         if (pb->bpos < offset)
136                 memset(pb->buf + pb->bpos, '\0', offset - pb->bpos);
137         memset(pb->buf + offset, charvalue, len);
138         if (pb->bpos < size_needed)
139                 pb->bpos = size_needed;
140
141         return 0;
142 }
143
144 int sprintbuf(struct printbuf *p, const char *msg, ...)
145 {
146         va_list ap;
147         char *t;
148         int size;
149         char buf[128];
150
151         /* use stack buffer first */
152         va_start(ap, msg);
153         size = vsnprintf(buf, 128, msg, ap);
154         va_end(ap);
155         /* if string is greater than stack buffer, then use dynamic string
156          * with vasprintf.  Note: some implementations of vsnprintf return -1
157          * if output is truncated whereas some return the number of bytes that
158          * would have been written - this code handles both cases.
159          */
160         if (size < 0 || size > 127)
161         {
162                 va_start(ap, msg);
163                 if ((size = vasprintf(&t, msg, ap)) < 0)
164                 {
165                         va_end(ap);
166                         return -1;
167                 }
168                 va_end(ap);
169                 size = printbuf_memappend(p, t, size);
170                 free(t);
171         }
172         else
173         {
174                 size = printbuf_memappend(p, buf, size);
175         }
176         return size;
177 }
178
179 void printbuf_reset(struct printbuf *p)
180 {
181         p->buf[0] = '\0';
182         p->bpos = 0;
183 }
184
185 void printbuf_free(struct printbuf *p)
186 {
187         if (p)
188         {
189                 free(p->buf);
190                 free(p);
191         }
192 }