1 /*
2  * Copyright © 2017 Thomas Helland
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  */
24 
25 #include "string_buffer.h"
26 
27 static bool
ensure_capacity(struct _mesa_string_buffer * str,uint32_t needed_capacity)28 ensure_capacity(struct _mesa_string_buffer *str, uint32_t needed_capacity)
29 {
30    if (needed_capacity <= str->capacity)
31       return true;
32 
33    /* Too small, double until we can fit the new string */
34    uint32_t new_capacity = str->capacity * 2;
35    while (needed_capacity > new_capacity)
36       new_capacity *= 2;
37 
38    str->buf = reralloc_array_size(str, str->buf, sizeof(char), new_capacity);
39    if (str->buf == NULL)
40       return false;
41 
42    str->capacity = new_capacity;
43    return true;
44 }
45 
46 struct _mesa_string_buffer *
_mesa_string_buffer_create(void * mem_ctx,uint32_t initial_capacity)47 _mesa_string_buffer_create(void *mem_ctx, uint32_t initial_capacity)
48 {
49    struct _mesa_string_buffer *str;
50    str = ralloc(mem_ctx, struct _mesa_string_buffer);
51 
52    if (str == NULL)
53       return NULL;
54 
55    /* If no initial capacity is set then set it to something */
56    str->capacity = initial_capacity ? initial_capacity : 32;
57    str->buf = ralloc_array(str, char, str->capacity);
58 
59    if (!str->buf) {
60       ralloc_free(str);
61       return NULL;
62    }
63 
64    str->length = 0;
65    str->buf[str->length] = '\0';
66    return str;
67 }
68 
69 bool
_mesa_string_buffer_append_all(struct _mesa_string_buffer * str,uint32_t num_args,...)70 _mesa_string_buffer_append_all(struct _mesa_string_buffer *str,
71                                uint32_t num_args, ...)
72 {
73    int i;
74    char* s;
75    va_list args;
76    va_start(args, num_args);
77    for (i = 0; i < num_args; i++) {
78       s = va_arg(args, char*);
79       if (!_mesa_string_buffer_append_len(str, s, strlen(s))) {
80          va_end(args);
81          return false;
82       }
83    }
84    va_end(args);
85    return true;
86 }
87 
88 bool
_mesa_string_buffer_append_len(struct _mesa_string_buffer * str,const char * c,uint32_t len)89 _mesa_string_buffer_append_len(struct _mesa_string_buffer *str,
90                                const char *c, uint32_t len)
91 {
92    uint32_t needed_length = str->length + len + 1;
93 
94    /* Check if we're overflowing uint32_t */
95    if (needed_length < str->length)
96       return false;
97 
98    if (!ensure_capacity(str, needed_length))
99       return false;
100 
101    memcpy(str->buf + str->length, c, len);
102    str->length += len;
103    str->buf[str->length] = '\0';
104    return true;
105 }
106 
107 bool
_mesa_string_buffer_vprintf(struct _mesa_string_buffer * str,const char * format,va_list args)108 _mesa_string_buffer_vprintf(struct _mesa_string_buffer *str,
109                             const char *format, va_list args)
110 {
111    /* We're looping two times to avoid duplicating code */
112    for (uint32_t i = 0; i < 2; i++) {
113       va_list arg_copy;
114       va_copy(arg_copy, args);
115       uint32_t space_left = str->capacity - str->length;
116 
117       int32_t len = util_vsnprintf(str->buf + str->length,
118                                    space_left, format, arg_copy);
119       va_end(arg_copy);
120 
121       /* Error in vsnprintf() or measured len overflows size_t */
122       if (unlikely(len < 0 || str->length + len + 1 < str->length))
123          return false;
124 
125       /* There was enough space for the string; we're done */
126       if (len < space_left) {
127          str->length += len;
128          return true;
129       }
130 
131       /* Not enough space, resize and retry */
132       ensure_capacity(str, str->length + len + 1);
133    }
134 
135    return false;
136 }
137 
138 bool
_mesa_string_buffer_printf(struct _mesa_string_buffer * str,const char * format,...)139 _mesa_string_buffer_printf(struct _mesa_string_buffer *str,
140                             const char *format, ...)
141 {
142    bool res;
143    va_list args;
144    va_start(args, format);
145    res = _mesa_string_buffer_vprintf(str, format, args);
146    va_end(args);
147    return res;
148 }
149