Fri, 09 Nov 2018 17:11:12 +0100
fixes document switching with textareas (Motif)
0 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * | |
152
62921b370c60
fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
4 | * Copyright 2016 Olaf Wintermann. All rights reserved. |
0 | 5 | * |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * | |
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
26 | * POSSIBILITY OF SUCH DAMAGE. | |
27 | */ | |
28 | ||
29 | #include "buffer.h" | |
30 | #include <stdarg.h> | |
31 | #include <stdlib.h> | |
32 | #include <string.h> | |
33 | ||
124
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
34 | UcxBuffer *ucx_buffer_new(void *space, size_t capacity, int flags) { |
0 | 35 | UcxBuffer *buffer = (UcxBuffer*) malloc(sizeof(UcxBuffer)); |
36 | if (buffer) { | |
37 | buffer->flags = flags; | |
38 | if (!space) { | |
124
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
39 | buffer->space = (char*)malloc(capacity); |
0 | 40 | if (!buffer->space) { |
41 | free(buffer); | |
42 | return NULL; | |
43 | } | |
124
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
44 | memset(buffer->space, 0, capacity); |
0 | 45 | buffer->flags |= UCX_BUFFER_AUTOFREE; |
46 | } else { | |
47 | buffer->space = (char*)space; | |
48 | } | |
124
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
49 | buffer->capacity = capacity; |
0 | 50 | buffer->size = 0; |
51 | ||
52 | buffer->pos = 0; | |
53 | } | |
54 | ||
55 | return buffer; | |
56 | } | |
57 | ||
58 | void ucx_buffer_free(UcxBuffer *buffer) { | |
59 | if ((buffer->flags & UCX_BUFFER_AUTOFREE) == UCX_BUFFER_AUTOFREE) { | |
60 | free(buffer->space); | |
61 | } | |
62 | free(buffer); | |
63 | } | |
64 | ||
65 | UcxBuffer* ucx_buffer_extract( | |
66 | UcxBuffer *src, size_t start, size_t length, int flags) { | |
124
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
67 | |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
68 | if (src->size == 0 || length == 0 || start+length > src->capacity) { |
0 | 69 | return NULL; |
70 | } | |
71 | ||
72 | UcxBuffer *dst = (UcxBuffer*) malloc(sizeof(UcxBuffer)); | |
73 | if (dst) { | |
74 | dst->space = (char*)malloc(length); | |
75 | if (!dst->space) { | |
76 | free(dst); | |
77 | return NULL; | |
78 | } | |
79 | dst->capacity = length; | |
80 | dst->size = length; | |
81 | dst->flags = flags | UCX_BUFFER_AUTOFREE; | |
82 | dst->pos = 0; | |
83 | memcpy(dst->space, src->space+start, length); | |
84 | } | |
85 | return dst; | |
86 | } | |
87 | ||
88 | int ucx_buffer_seek(UcxBuffer *buffer, off_t offset, int whence) { | |
89 | size_t npos; | |
90 | switch (whence) { | |
91 | case SEEK_CUR: | |
92 | npos = buffer->pos; | |
93 | break; | |
94 | case SEEK_END: | |
95 | npos = buffer->size; | |
96 | break; | |
124
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
97 | case SEEK_SET: |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
98 | npos = 0; |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
99 | break; |
0 | 100 | default: |
124
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
101 | return -1; |
0 | 102 | } |
103 | ||
124
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
104 | size_t opos = npos; |
0 | 105 | npos += offset; |
106 | ||
124
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
107 | if ((offset > 0 && npos < opos) || (offset < 0 && npos > opos)) { |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
108 | return -1; |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
109 | } |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
110 | |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
111 | if (npos >= buffer->size) { |
0 | 112 | return -1; |
113 | } else { | |
114 | buffer->pos = npos; | |
115 | return 0; | |
116 | } | |
117 | ||
118 | } | |
119 | ||
120 | int ucx_buffer_eof(UcxBuffer *buffer) { | |
121 | return buffer->pos >= buffer->size; | |
122 | } | |
123 | ||
124 | int ucx_buffer_extend(UcxBuffer *buffer, size_t len) { | |
125 | size_t newcap = buffer->capacity; | |
124
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
126 | |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
127 | if (buffer->capacity + len < buffer->capacity) { |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
128 | return -1; |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
129 | } |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
130 | |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
131 | while (buffer->capacity + len > newcap) { |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
132 | newcap <<= 1; |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
133 | if (newcap < buffer->capacity) { |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
134 | return -1; |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
135 | } |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
136 | } |
0 | 137 | |
138 | char *newspace = (char*)realloc(buffer->space, newcap); | |
139 | if (newspace) { | |
140 | memset(newspace+buffer->size, 0, newcap-buffer->size); | |
141 | buffer->space = newspace; | |
142 | buffer->capacity = newcap; | |
143 | } else { | |
144 | return -1; | |
145 | } | |
146 | ||
147 | return 0; | |
148 | } | |
149 | ||
150 | size_t ucx_buffer_write(const void *ptr, size_t size, size_t nitems, | |
151 | UcxBuffer *buffer) { | |
152 | size_t len = size * nitems; | |
152
62921b370c60
fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
124
diff
changeset
|
153 | const char *string = ptr; |
124
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
154 | size_t required = buffer->pos + len; |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
155 | if (buffer->pos > required) { |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
156 | return 0; |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
157 | } |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
158 | |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
159 | if (required > buffer->capacity) { |
0 | 160 | if ((buffer->flags & UCX_BUFFER_AUTOEXTEND) == UCX_BUFFER_AUTOEXTEND) { |
124
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
161 | if (ucx_buffer_extend(buffer, required - buffer->capacity)) { |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
162 | return 0; |
0 | 163 | } |
164 | } else { | |
165 | len = buffer->capacity - buffer->pos; | |
124
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
166 | if (size > 1) { |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
167 | len -= len%size; |
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
168 | } |
0 | 169 | } |
170 | } | |
171 | ||
124
80609f9675f1
added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
172 | if (len == 0) { |
0 | 173 | return len; |
174 | } | |
175 | ||
176 | memcpy(buffer->space + buffer->pos, ptr, len); | |
177 | buffer->pos += len; | |
178 | if(buffer->pos > buffer->size) { | |
179 | buffer->size = buffer->pos; | |
180 | } | |
181 | ||
182 | return len / size; | |
183 | } | |
184 | ||
185 | size_t ucx_buffer_read(void *ptr, size_t size, size_t nitems, | |
186 | UcxBuffer *buffer) { | |
187 | size_t len = size * nitems; | |
188 | if (buffer->pos + len > buffer->size) { | |
189 | len = buffer->size - buffer->pos; | |
190 | if (size > 1) len -= len%size; | |
191 | } | |
192 | ||
193 | if (len <= 0) { | |
194 | return len; | |
195 | } | |
196 | ||
197 | memcpy(ptr, buffer->space + buffer->pos, len); | |
198 | buffer->pos += len; | |
199 | ||
200 | return len / size; | |
201 | } | |
202 | ||
203 | int ucx_buffer_putc(UcxBuffer *buffer, int c) { | |
204 | if(buffer->pos >= buffer->capacity) { | |
205 | if ((buffer->flags & UCX_BUFFER_AUTOEXTEND) == UCX_BUFFER_AUTOEXTEND) { | |
206 | if(ucx_buffer_extend(buffer, 1)) { | |
207 | return EOF; | |
208 | } | |
209 | } else { | |
210 | return EOF; | |
211 | } | |
212 | } | |
213 | ||
214 | c &= 0xFF; | |
215 | buffer->space[buffer->pos] = (char) c; | |
216 | buffer->pos++; | |
217 | if(buffer->pos > buffer->size) { | |
218 | buffer->size = buffer->pos; | |
219 | } | |
220 | return c; | |
221 | } | |
222 | ||
223 | int ucx_buffer_getc(UcxBuffer *buffer) { | |
224 | if (ucx_buffer_eof(buffer)) { | |
225 | return EOF; | |
226 | } else { | |
227 | int c = buffer->space[buffer->pos]; | |
228 | buffer->pos++; | |
229 | return c; | |
230 | } | |
231 | } | |
232 | ||
233 | size_t ucx_buffer_puts(UcxBuffer *buffer, char *str) { | |
234 | return ucx_buffer_write((const void*)str, 1, strlen(str), buffer); | |
235 | } |