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 "SkTSearch.h" |
michael@0 | 11 | #include <ctype.h> |
michael@0 | 12 | |
michael@0 | 13 | static inline const char* index_into_base(const char*const* base, int index, |
michael@0 | 14 | size_t elemSize) |
michael@0 | 15 | { |
michael@0 | 16 | return *(const char*const*)((const char*)base + index * elemSize); |
michael@0 | 17 | } |
michael@0 | 18 | |
michael@0 | 19 | int SkStrSearch(const char*const* base, int count, const char target[], |
michael@0 | 20 | size_t target_len, size_t elemSize) |
michael@0 | 21 | { |
michael@0 | 22 | if (count <= 0) |
michael@0 | 23 | return ~0; |
michael@0 | 24 | |
michael@0 | 25 | SkASSERT(base != NULL); |
michael@0 | 26 | |
michael@0 | 27 | int lo = 0; |
michael@0 | 28 | int hi = count - 1; |
michael@0 | 29 | |
michael@0 | 30 | while (lo < hi) |
michael@0 | 31 | { |
michael@0 | 32 | int mid = (hi + lo) >> 1; |
michael@0 | 33 | const char* elem = index_into_base(base, mid, elemSize); |
michael@0 | 34 | |
michael@0 | 35 | int cmp = strncmp(elem, target, target_len); |
michael@0 | 36 | if (cmp < 0) |
michael@0 | 37 | lo = mid + 1; |
michael@0 | 38 | else if (cmp > 0 || strlen(elem) > target_len) |
michael@0 | 39 | hi = mid; |
michael@0 | 40 | else |
michael@0 | 41 | return mid; |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | const char* elem = index_into_base(base, hi, elemSize); |
michael@0 | 45 | int cmp = strncmp(elem, target, target_len); |
michael@0 | 46 | if (cmp || strlen(elem) > target_len) |
michael@0 | 47 | { |
michael@0 | 48 | if (cmp < 0) |
michael@0 | 49 | hi += 1; |
michael@0 | 50 | hi = ~hi; |
michael@0 | 51 | } |
michael@0 | 52 | return hi; |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | int SkStrSearch(const char*const* base, int count, const char target[], |
michael@0 | 56 | size_t elemSize) |
michael@0 | 57 | { |
michael@0 | 58 | return SkStrSearch(base, count, target, strlen(target), elemSize); |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | int SkStrLCSearch(const char*const* base, int count, const char target[], |
michael@0 | 62 | size_t len, size_t elemSize) |
michael@0 | 63 | { |
michael@0 | 64 | SkASSERT(target); |
michael@0 | 65 | |
michael@0 | 66 | SkAutoAsciiToLC tolc(target, len); |
michael@0 | 67 | |
michael@0 | 68 | return SkStrSearch(base, count, tolc.lc(), len, elemSize); |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | int SkStrLCSearch(const char*const* base, int count, const char target[], |
michael@0 | 72 | size_t elemSize) |
michael@0 | 73 | { |
michael@0 | 74 | return SkStrLCSearch(base, count, target, strlen(target), elemSize); |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | ////////////////////////////////////////////////////////////////////////////// |
michael@0 | 78 | |
michael@0 | 79 | SkAutoAsciiToLC::SkAutoAsciiToLC(const char str[], size_t len) |
michael@0 | 80 | { |
michael@0 | 81 | // see if we need to compute the length |
michael@0 | 82 | if ((long)len < 0) { |
michael@0 | 83 | len = strlen(str); |
michael@0 | 84 | } |
michael@0 | 85 | fLength = len; |
michael@0 | 86 | |
michael@0 | 87 | // assign lc to our preallocated storage if len is small enough, or allocate |
michael@0 | 88 | // it on the heap |
michael@0 | 89 | char* lc; |
michael@0 | 90 | if (len <= STORAGE) { |
michael@0 | 91 | lc = fStorage; |
michael@0 | 92 | } else { |
michael@0 | 93 | lc = (char*)sk_malloc_throw(len + 1); |
michael@0 | 94 | } |
michael@0 | 95 | fLC = lc; |
michael@0 | 96 | |
michael@0 | 97 | // convert any asii to lower-case. we let non-ascii (utf8) chars pass |
michael@0 | 98 | // through unchanged |
michael@0 | 99 | for (int i = (int)(len - 1); i >= 0; --i) { |
michael@0 | 100 | int c = str[i]; |
michael@0 | 101 | if ((c & 0x80) == 0) { // is just ascii |
michael@0 | 102 | c = tolower(c); |
michael@0 | 103 | } |
michael@0 | 104 | lc[i] = c; |
michael@0 | 105 | } |
michael@0 | 106 | lc[len] = 0; |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | SkAutoAsciiToLC::~SkAutoAsciiToLC() |
michael@0 | 110 | { |
michael@0 | 111 | if (fLC != fStorage) { |
michael@0 | 112 | sk_free(fLC); |
michael@0 | 113 | } |
michael@0 | 114 | } |