Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | |
michael@0 | 2 | /* |
michael@0 | 3 | * Copyright 2006 The Android Open Source Project |
michael@0 | 4 | * |
michael@0 | 5 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 6 | * found in the LICENSE file. |
michael@0 | 7 | */ |
michael@0 | 8 | |
michael@0 | 9 | |
michael@0 | 10 | #include "SkParse.h" |
michael@0 | 11 | |
michael@0 | 12 | static inline bool is_between(int c, int min, int max) |
michael@0 | 13 | { |
michael@0 | 14 | return (unsigned)(c - min) <= (unsigned)(max - min); |
michael@0 | 15 | } |
michael@0 | 16 | |
michael@0 | 17 | static inline bool is_ws(int c) |
michael@0 | 18 | { |
michael@0 | 19 | return is_between(c, 1, 32); |
michael@0 | 20 | } |
michael@0 | 21 | |
michael@0 | 22 | static inline bool is_digit(int c) |
michael@0 | 23 | { |
michael@0 | 24 | return is_between(c, '0', '9'); |
michael@0 | 25 | } |
michael@0 | 26 | |
michael@0 | 27 | static inline bool is_sep(int c) |
michael@0 | 28 | { |
michael@0 | 29 | return is_ws(c) || c == ',' || c == ';'; |
michael@0 | 30 | } |
michael@0 | 31 | |
michael@0 | 32 | static int to_hex(int c) |
michael@0 | 33 | { |
michael@0 | 34 | if (is_digit(c)) |
michael@0 | 35 | return c - '0'; |
michael@0 | 36 | |
michael@0 | 37 | c |= 0x20; // make us lower-case |
michael@0 | 38 | if (is_between(c, 'a', 'f')) |
michael@0 | 39 | return c + 10 - 'a'; |
michael@0 | 40 | else |
michael@0 | 41 | return -1; |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | static inline bool is_hex(int c) |
michael@0 | 45 | { |
michael@0 | 46 | return to_hex(c) >= 0; |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | static const char* skip_ws(const char str[]) |
michael@0 | 50 | { |
michael@0 | 51 | SkASSERT(str); |
michael@0 | 52 | while (is_ws(*str)) |
michael@0 | 53 | str++; |
michael@0 | 54 | return str; |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | static const char* skip_sep(const char str[]) |
michael@0 | 58 | { |
michael@0 | 59 | SkASSERT(str); |
michael@0 | 60 | while (is_sep(*str)) |
michael@0 | 61 | str++; |
michael@0 | 62 | return str; |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | int SkParse::Count(const char str[]) |
michael@0 | 66 | { |
michael@0 | 67 | char c; |
michael@0 | 68 | int count = 0; |
michael@0 | 69 | goto skipLeading; |
michael@0 | 70 | do { |
michael@0 | 71 | count++; |
michael@0 | 72 | do { |
michael@0 | 73 | if ((c = *str++) == '\0') |
michael@0 | 74 | goto goHome; |
michael@0 | 75 | } while (is_sep(c) == false); |
michael@0 | 76 | skipLeading: |
michael@0 | 77 | do { |
michael@0 | 78 | if ((c = *str++) == '\0') |
michael@0 | 79 | goto goHome; |
michael@0 | 80 | } while (is_sep(c)); |
michael@0 | 81 | } while (true); |
michael@0 | 82 | goHome: |
michael@0 | 83 | return count; |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | int SkParse::Count(const char str[], char separator) |
michael@0 | 87 | { |
michael@0 | 88 | char c; |
michael@0 | 89 | int count = 0; |
michael@0 | 90 | goto skipLeading; |
michael@0 | 91 | do { |
michael@0 | 92 | count++; |
michael@0 | 93 | do { |
michael@0 | 94 | if ((c = *str++) == '\0') |
michael@0 | 95 | goto goHome; |
michael@0 | 96 | } while (c != separator); |
michael@0 | 97 | skipLeading: |
michael@0 | 98 | do { |
michael@0 | 99 | if ((c = *str++) == '\0') |
michael@0 | 100 | goto goHome; |
michael@0 | 101 | } while (c == separator); |
michael@0 | 102 | } while (true); |
michael@0 | 103 | goHome: |
michael@0 | 104 | return count; |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | const char* SkParse::FindHex(const char str[], uint32_t* value) |
michael@0 | 108 | { |
michael@0 | 109 | SkASSERT(str); |
michael@0 | 110 | str = skip_ws(str); |
michael@0 | 111 | |
michael@0 | 112 | if (!is_hex(*str)) |
michael@0 | 113 | return NULL; |
michael@0 | 114 | |
michael@0 | 115 | uint32_t n = 0; |
michael@0 | 116 | int max_digits = 8; |
michael@0 | 117 | int digit; |
michael@0 | 118 | |
michael@0 | 119 | while ((digit = to_hex(*str)) >= 0) |
michael@0 | 120 | { |
michael@0 | 121 | if (--max_digits < 0) |
michael@0 | 122 | return NULL; |
michael@0 | 123 | n = (n << 4) | digit; |
michael@0 | 124 | str += 1; |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | if (*str == 0 || is_ws(*str)) |
michael@0 | 128 | { |
michael@0 | 129 | if (value) |
michael@0 | 130 | *value = n; |
michael@0 | 131 | return str; |
michael@0 | 132 | } |
michael@0 | 133 | return NULL; |
michael@0 | 134 | } |
michael@0 | 135 | |
michael@0 | 136 | const char* SkParse::FindS32(const char str[], int32_t* value) |
michael@0 | 137 | { |
michael@0 | 138 | SkASSERT(str); |
michael@0 | 139 | str = skip_ws(str); |
michael@0 | 140 | |
michael@0 | 141 | int sign = 0; |
michael@0 | 142 | if (*str == '-') |
michael@0 | 143 | { |
michael@0 | 144 | sign = -1; |
michael@0 | 145 | str += 1; |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | if (!is_digit(*str)) |
michael@0 | 149 | return NULL; |
michael@0 | 150 | |
michael@0 | 151 | int n = 0; |
michael@0 | 152 | while (is_digit(*str)) |
michael@0 | 153 | { |
michael@0 | 154 | n = 10*n + *str - '0'; |
michael@0 | 155 | str += 1; |
michael@0 | 156 | } |
michael@0 | 157 | if (value) |
michael@0 | 158 | *value = (n ^ sign) - sign; |
michael@0 | 159 | return str; |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | const char* SkParse::FindMSec(const char str[], SkMSec* value) |
michael@0 | 163 | { |
michael@0 | 164 | SkASSERT(str); |
michael@0 | 165 | str = skip_ws(str); |
michael@0 | 166 | |
michael@0 | 167 | int sign = 0; |
michael@0 | 168 | if (*str == '-') |
michael@0 | 169 | { |
michael@0 | 170 | sign = -1; |
michael@0 | 171 | str += 1; |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | if (!is_digit(*str)) |
michael@0 | 175 | return NULL; |
michael@0 | 176 | |
michael@0 | 177 | int n = 0; |
michael@0 | 178 | while (is_digit(*str)) |
michael@0 | 179 | { |
michael@0 | 180 | n = 10*n + *str - '0'; |
michael@0 | 181 | str += 1; |
michael@0 | 182 | } |
michael@0 | 183 | int remaining10s = 3; |
michael@0 | 184 | if (*str == '.') { |
michael@0 | 185 | str++; |
michael@0 | 186 | while (is_digit(*str)) |
michael@0 | 187 | { |
michael@0 | 188 | n = 10*n + *str - '0'; |
michael@0 | 189 | str += 1; |
michael@0 | 190 | if (--remaining10s == 0) |
michael@0 | 191 | break; |
michael@0 | 192 | } |
michael@0 | 193 | } |
michael@0 | 194 | while (--remaining10s >= 0) |
michael@0 | 195 | n *= 10; |
michael@0 | 196 | if (value) |
michael@0 | 197 | *value = (n ^ sign) - sign; |
michael@0 | 198 | return str; |
michael@0 | 199 | } |
michael@0 | 200 | |
michael@0 | 201 | const char* SkParse::FindScalar(const char str[], SkScalar* value) { |
michael@0 | 202 | SkASSERT(str); |
michael@0 | 203 | str = skip_ws(str); |
michael@0 | 204 | |
michael@0 | 205 | char* stop; |
michael@0 | 206 | float v = (float)strtod(str, &stop); |
michael@0 | 207 | if (str == stop) { |
michael@0 | 208 | return NULL; |
michael@0 | 209 | } |
michael@0 | 210 | if (value) { |
michael@0 | 211 | *value = v; |
michael@0 | 212 | } |
michael@0 | 213 | return stop; |
michael@0 | 214 | } |
michael@0 | 215 | |
michael@0 | 216 | const char* SkParse::FindScalars(const char str[], SkScalar value[], int count) |
michael@0 | 217 | { |
michael@0 | 218 | SkASSERT(count >= 0); |
michael@0 | 219 | |
michael@0 | 220 | if (count > 0) |
michael@0 | 221 | { |
michael@0 | 222 | for (;;) |
michael@0 | 223 | { |
michael@0 | 224 | str = SkParse::FindScalar(str, value); |
michael@0 | 225 | if (--count == 0 || str == NULL) |
michael@0 | 226 | break; |
michael@0 | 227 | |
michael@0 | 228 | // keep going |
michael@0 | 229 | str = skip_sep(str); |
michael@0 | 230 | if (value) |
michael@0 | 231 | value += 1; |
michael@0 | 232 | } |
michael@0 | 233 | } |
michael@0 | 234 | return str; |
michael@0 | 235 | } |
michael@0 | 236 | |
michael@0 | 237 | static bool lookup_str(const char str[], const char** table, int count) |
michael@0 | 238 | { |
michael@0 | 239 | while (--count >= 0) |
michael@0 | 240 | if (!strcmp(str, table[count])) |
michael@0 | 241 | return true; |
michael@0 | 242 | return false; |
michael@0 | 243 | } |
michael@0 | 244 | |
michael@0 | 245 | bool SkParse::FindBool(const char str[], bool* value) |
michael@0 | 246 | { |
michael@0 | 247 | static const char* gYes[] = { "yes", "1", "true" }; |
michael@0 | 248 | static const char* gNo[] = { "no", "0", "false" }; |
michael@0 | 249 | |
michael@0 | 250 | if (lookup_str(str, gYes, SK_ARRAY_COUNT(gYes))) |
michael@0 | 251 | { |
michael@0 | 252 | if (value) *value = true; |
michael@0 | 253 | return true; |
michael@0 | 254 | } |
michael@0 | 255 | else if (lookup_str(str, gNo, SK_ARRAY_COUNT(gNo))) |
michael@0 | 256 | { |
michael@0 | 257 | if (value) *value = false; |
michael@0 | 258 | return true; |
michael@0 | 259 | } |
michael@0 | 260 | return false; |
michael@0 | 261 | } |
michael@0 | 262 | |
michael@0 | 263 | int SkParse::FindList(const char target[], const char list[]) |
michael@0 | 264 | { |
michael@0 | 265 | size_t len = strlen(target); |
michael@0 | 266 | int index = 0; |
michael@0 | 267 | |
michael@0 | 268 | for (;;) |
michael@0 | 269 | { |
michael@0 | 270 | const char* end = strchr(list, ','); |
michael@0 | 271 | size_t entryLen; |
michael@0 | 272 | |
michael@0 | 273 | if (end == NULL) // last entry |
michael@0 | 274 | entryLen = strlen(list); |
michael@0 | 275 | else |
michael@0 | 276 | entryLen = end - list; |
michael@0 | 277 | |
michael@0 | 278 | if (entryLen == len && memcmp(target, list, len) == 0) |
michael@0 | 279 | return index; |
michael@0 | 280 | if (end == NULL) |
michael@0 | 281 | break; |
michael@0 | 282 | |
michael@0 | 283 | list = end + 1; // skip the ',' |
michael@0 | 284 | index += 1; |
michael@0 | 285 | } |
michael@0 | 286 | return -1; |
michael@0 | 287 | } |
michael@0 | 288 | |
michael@0 | 289 | #ifdef SK_SUPPORT_UNITTEST |
michael@0 | 290 | void SkParse::UnitTest() |
michael@0 | 291 | { |
michael@0 | 292 | // !!! additional parse tests go here |
michael@0 | 293 | SkParse::TestColor(); |
michael@0 | 294 | } |
michael@0 | 295 | #endif |