Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
1 /*
2 *
3 * c2ru.c
4 *
5 * $Source: /Users/ekr/tmp/nrappkit-dump/nrappkit/src/registry/c2ru.c,v $
6 * $Revision: 1.3 $
7 * $Date: 2007/06/26 22:37:50 $
8 *
9 * c2r utility methods
10 *
11 *
12 * Copyright (C) 2005, Network Resonance, Inc.
13 * Copyright (C) 2006, Network Resonance, Inc.
14 * All Rights Reserved
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 *
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 * 3. Neither the name of Network Resonance, Inc. nor the name of any
26 * contributors to this software may be used to endorse or promote
27 * products derived from this software without specific prior written
28 * permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
31 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
34 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 * POSSIBILITY OF SUCH DAMAGE.
41 *
42 *
43 */
45 #include <sys/queue.h>
46 #include <string.h>
47 #include <registry.h>
48 #include "nr_common.h"
49 #include <r_errors.h>
50 #include <r_macros.h>
51 #include <ctype.h>
52 #include "c2ru.h"
55 #define NRGET(func, type, get) \
56 int \
57 func(NR_registry parent, char *child, type **out) \
58 { \
59 int r, _status; \
60 NR_registry registry; \
61 type tmp; \
62 \
63 if ((r = nr_c2ru_make_registry(parent, child, registry))) \
64 ABORT(r); \
65 \
66 if ((r = get(registry, &tmp))) { \
67 if (r != R_NOT_FOUND) \
68 ABORT(r); \
69 *out = 0; \
70 } \
71 else { \
72 *out = RCALLOC(sizeof(tmp)); \
73 if (*out == 0) \
74 ABORT(R_NO_MEMORY); \
75 **out = tmp; \
76 } \
77 \
78 _status = 0; \
79 abort: \
80 return (_status); \
81 }
83 int
84 nr_c2ru_get_char(NR_registry parent, char *child, char **out)
85 {
86 int r, _status;
87 NR_registry registry;
88 char tmp;
90 if ((r = nr_c2ru_make_registry(parent, child, registry)))
91 ABORT(r);
93 if ((r = NR_reg_get_char(registry, &tmp))) {
94 if (r != R_NOT_FOUND)
95 ABORT(r);
96 *out = 0;
97 }
98 else {
99 *out = RCALLOC(sizeof(tmp));
100 if (*out == 0)
101 ABORT(R_NO_MEMORY);
102 **out = tmp;
103 }
105 _status = 0;
106 abort:
107 return (_status);
108 }
109 NRGET(nr_c2ru_get_uchar, UCHAR, NR_reg_get_uchar)
110 NRGET(nr_c2ru_get_int2, INT2, NR_reg_get_int2)
111 NRGET(nr_c2ru_get_uint2, UINT2, NR_reg_get_uint2)
112 NRGET(nr_c2ru_get_int4, INT4, NR_reg_get_int4)
113 NRGET(nr_c2ru_get_uint4, UINT4, NR_reg_get_uint4)
114 NRGET(nr_c2ru_get_int8, INT8, NR_reg_get_int8)
115 NRGET(nr_c2ru_get_uint8, UINT8, NR_reg_get_uint8)
116 NRGET(nr_c2ru_get_double, double, NR_reg_get_double)
117 NRGET(nr_c2ru_get_string, char*, NR_reg_alloc_string)
118 NRGET(nr_c2ru_get_data, Data, NR_reg_alloc_data)
121 #define NRSET(func, type, set) \
122 int \
123 func(NR_registry parent, char *child, type *in) \
124 { \
125 int r, _status; \
126 NR_registry registry; \
127 \
128 if (in == 0) \
129 return 0; \
130 \
131 if ((r = nr_c2ru_make_registry(parent, child, registry))) \
132 ABORT(r); \
133 \
134 if ((r = set(registry, *in))) \
135 ABORT(r); \
136 \
137 _status = 0; \
138 abort: \
139 return (_status); \
140 }
142 NRSET(nr_c2ru_set_char, char, NR_reg_set_char)
143 NRSET(nr_c2ru_set_uchar, UCHAR, NR_reg_set_uchar)
144 NRSET(nr_c2ru_set_int2, INT2, NR_reg_set_int2)
145 NRSET(nr_c2ru_set_uint2, UINT2, NR_reg_set_uint2)
146 NRSET(nr_c2ru_set_int4, INT4, NR_reg_set_int4)
147 NRSET(nr_c2ru_set_uint4, UINT4, NR_reg_set_uint4)
148 NRSET(nr_c2ru_set_int8, INT8, NR_reg_set_int8)
149 NRSET(nr_c2ru_set_uint8, UINT8, NR_reg_set_uint8)
150 NRSET(nr_c2ru_set_double, double, NR_reg_set_double)
151 NRSET(nr_c2ru_set_string, char*, NR_reg_set_string)
153 int
154 nr_c2ru_set_data(NR_registry parent, char *child, Data *in)
155 {
156 int r, _status;
157 NR_registry registry;
159 if (in == 0)
160 return 0;
162 if ((r = nr_c2ru_make_registry(parent, child, registry)))
163 ABORT(r);
165 if ((r = NR_reg_set_bytes(registry, in->data, in->len)))
166 ABORT(r);
168 _status = 0;
169 abort:
170 return (_status);
171 }
173 #define NRFREE(func, type) \
174 int \
175 func(type *in) \
176 { \
177 if (in) \
178 RFREE(in); \
179 return 0; \
180 }
182 NRFREE(nr_c2ru_free_char, char)
183 NRFREE(nr_c2ru_free_uchar, UCHAR)
184 NRFREE(nr_c2ru_free_int2, INT2)
185 NRFREE(nr_c2ru_free_uint2, UINT2)
186 NRFREE(nr_c2ru_free_int4, INT4)
187 NRFREE(nr_c2ru_free_uint4, UINT4)
188 NRFREE(nr_c2ru_free_int8, INT8)
189 NRFREE(nr_c2ru_free_uint8, UINT8)
190 NRFREE(nr_c2ru_free_double, double)
193 int
194 nr_c2ru_free_string(char **in)
195 {
196 if (*in)
197 RFREE(*in);
198 if (in)
199 RFREE(in);
200 return 0;
201 }
203 int
204 nr_c2ru_free_data(Data *in)
205 {
206 int r, _status;
208 if (in) {
209 if ((r=r_data_destroy(&in)))
210 ABORT(r);
211 }
213 _status = 0;
214 abort:
215 return (_status);
216 }
218 int
219 nr_c2ru_get_children(NR_registry parent, char *child, void *ptr, size_t size, int (*get)(NR_registry, void*))
220 {
221 int r, _status;
222 NR_registry registry;
223 unsigned int count;
224 int i;
225 NR_registry name;
226 struct entry { TAILQ_ENTRY(entry) entries; } *entry;
227 TAILQ_HEAD(, entry) *tailq = (void*)ptr;
229 TAILQ_INIT(tailq);
231 if ((r=nr_c2ru_make_registry(parent, child, registry)))
232 ABORT(r);
234 if ((r=NR_reg_get_child_count(registry, &count))) {
235 if (r != R_NOT_FOUND)
236 ABORT(r);
237 }
238 else {
239 for (i = 0; i < count; ++i) {
240 if ((r=NR_reg_get_child_registry(registry, i, name))) {
241 /* ignore R_NOT_FOUND errors */
242 if (r == R_NOT_FOUND)
243 continue;
244 else
245 ABORT(r);
246 }
248 if ((r=get(name, &entry))) {
249 /* ignore R_NOT_FOUND errors */
250 if (r == R_NOT_FOUND)
251 continue;
252 else
253 ABORT(r);
254 }
256 TAILQ_INSERT_TAIL(tailq, entry, entries);
257 }
258 }
260 _status = 0;
261 abort:
262 return (_status);
263 }
265 int
266 nr_c2ru_set_children(NR_registry parent, char *child, void *ptr, int (*set)(NR_registry, void*), int (*label)(NR_registry, void*, char[NR_REG_MAX_NR_REGISTRY_LEN]))
267 {
268 int r, _status;
269 NR_registry registry;
270 int i;
271 NR_registry name;
272 char buffer[NR_REG_MAX_NR_REGISTRY_LEN];
273 struct entry { TAILQ_ENTRY(entry) entries; } *entry;
274 TAILQ_HEAD(, entry) *tailq = (void*)ptr;
276 if ((r=nr_c2ru_make_registry(parent, child, registry)))
277 ABORT(r);
279 (void)NR_reg_del(registry);
281 i = 0;
282 TAILQ_FOREACH(entry, tailq, entries) {
283 if (label == 0 || (r=label(registry, entry, buffer))) {
284 snprintf(buffer, sizeof(buffer), "%d", i);
285 }
286 if ((r=nr_c2ru_make_registry(registry, buffer, name)))
287 ABORT(r);
289 if ((r=set(name, entry)))
290 ABORT(r);
292 ++i;
293 }
295 _status = 0;
296 abort:
297 return (_status);
298 }
300 int
301 nr_c2ru_free_children(void *ptr, int (*free)(void*))
302 {
303 struct entry { TAILQ_ENTRY(entry) entries; } *entry;
304 TAILQ_HEAD(, entry) *tailq = (void*)ptr;
306 while (! TAILQ_EMPTY(tailq)) {
307 entry = TAILQ_FIRST(tailq);
308 TAILQ_REMOVE(tailq, entry, entries);
309 (void)free(entry);
310 }
312 return 0;
313 }
315 /* requires parent already in legal form */
316 int
317 nr_c2ru_make_registry(NR_registry parent, char *child, NR_registry out)
318 {
319 return NR_reg_make_registry(parent, child, out);
320 }