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