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 "SkUtils.h" |
michael@0 | 11 | |
michael@0 | 12 | #if 0 |
michael@0 | 13 | #define assign_16_longs(dst, value) \ |
michael@0 | 14 | do { \ |
michael@0 | 15 | (dst)[0] = value; (dst)[1] = value; \ |
michael@0 | 16 | (dst)[2] = value; (dst)[3] = value; \ |
michael@0 | 17 | (dst)[4] = value; (dst)[5] = value; \ |
michael@0 | 18 | (dst)[6] = value; (dst)[7] = value; \ |
michael@0 | 19 | (dst)[8] = value; (dst)[9] = value; \ |
michael@0 | 20 | (dst)[10] = value; (dst)[11] = value; \ |
michael@0 | 21 | (dst)[12] = value; (dst)[13] = value; \ |
michael@0 | 22 | (dst)[14] = value; (dst)[15] = value; \ |
michael@0 | 23 | } while (0) |
michael@0 | 24 | #else |
michael@0 | 25 | #define assign_16_longs(dst, value) \ |
michael@0 | 26 | do { \ |
michael@0 | 27 | *(dst)++ = value; *(dst)++ = value; \ |
michael@0 | 28 | *(dst)++ = value; *(dst)++ = value; \ |
michael@0 | 29 | *(dst)++ = value; *(dst)++ = value; \ |
michael@0 | 30 | *(dst)++ = value; *(dst)++ = value; \ |
michael@0 | 31 | *(dst)++ = value; *(dst)++ = value; \ |
michael@0 | 32 | *(dst)++ = value; *(dst)++ = value; \ |
michael@0 | 33 | *(dst)++ = value; *(dst)++ = value; \ |
michael@0 | 34 | *(dst)++ = value; *(dst)++ = value; \ |
michael@0 | 35 | } while (0) |
michael@0 | 36 | #endif |
michael@0 | 37 | |
michael@0 | 38 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 39 | |
michael@0 | 40 | void sk_memset16_portable(uint16_t dst[], uint16_t value, int count) { |
michael@0 | 41 | SkASSERT(dst != NULL && count >= 0); |
michael@0 | 42 | |
michael@0 | 43 | if (count <= 0) { |
michael@0 | 44 | return; |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | // not sure if this helps to short-circuit on small values of count |
michael@0 | 48 | if (count < 8) { |
michael@0 | 49 | do { |
michael@0 | 50 | *dst++ = (uint16_t)value; |
michael@0 | 51 | } while (--count != 0); |
michael@0 | 52 | return; |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | // ensure we're on a long boundary |
michael@0 | 56 | if ((size_t)dst & 2) { |
michael@0 | 57 | *dst++ = (uint16_t)value; |
michael@0 | 58 | count -= 1; |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | uint32_t value32 = ((uint32_t)value << 16) | value; |
michael@0 | 62 | |
michael@0 | 63 | // handle the bulk with our unrolled macro |
michael@0 | 64 | { |
michael@0 | 65 | int sixteenlongs = count >> 5; |
michael@0 | 66 | if (sixteenlongs) { |
michael@0 | 67 | uint32_t* dst32 = (uint32_t*)dst; |
michael@0 | 68 | do { |
michael@0 | 69 | assign_16_longs(dst32, value32); |
michael@0 | 70 | } while (--sixteenlongs != 0); |
michael@0 | 71 | dst = (uint16_t*)dst32; |
michael@0 | 72 | count &= 31; |
michael@0 | 73 | } |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | // handle (most) of the rest |
michael@0 | 77 | { |
michael@0 | 78 | int longs = count >> 1; |
michael@0 | 79 | if (longs) { |
michael@0 | 80 | do { |
michael@0 | 81 | *(uint32_t*)dst = value32; |
michael@0 | 82 | dst += 2; |
michael@0 | 83 | } while (--longs != 0); |
michael@0 | 84 | } |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | // cleanup a possible trailing short |
michael@0 | 88 | if (count & 1) { |
michael@0 | 89 | *dst = (uint16_t)value; |
michael@0 | 90 | } |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | void sk_memset32_portable(uint32_t dst[], uint32_t value, int count) { |
michael@0 | 94 | SkASSERT(dst != NULL && count >= 0); |
michael@0 | 95 | |
michael@0 | 96 | int sixteenlongs = count >> 4; |
michael@0 | 97 | if (sixteenlongs) { |
michael@0 | 98 | do { |
michael@0 | 99 | assign_16_longs(dst, value); |
michael@0 | 100 | } while (--sixteenlongs != 0); |
michael@0 | 101 | count &= 15; |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | if (count) { |
michael@0 | 105 | do { |
michael@0 | 106 | *dst++ = value; |
michael@0 | 107 | } while (--count != 0); |
michael@0 | 108 | } |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | static void sk_memset16_stub(uint16_t dst[], uint16_t value, int count) { |
michael@0 | 112 | SkMemset16Proc proc = SkMemset16GetPlatformProc(); |
michael@0 | 113 | sk_memset16 = proc ? proc : sk_memset16_portable; |
michael@0 | 114 | sk_memset16(dst, value, count); |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | SkMemset16Proc sk_memset16 = sk_memset16_stub; |
michael@0 | 118 | |
michael@0 | 119 | static void sk_memset32_stub(uint32_t dst[], uint32_t value, int count) { |
michael@0 | 120 | SkMemset32Proc proc = SkMemset32GetPlatformProc(); |
michael@0 | 121 | sk_memset32 = proc ? proc : sk_memset32_portable; |
michael@0 | 122 | sk_memset32(dst, value, count); |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | SkMemset32Proc sk_memset32 = sk_memset32_stub; |
michael@0 | 126 | |
michael@0 | 127 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 128 | |
michael@0 | 129 | /* 0xxxxxxx 1 total |
michael@0 | 130 | 10xxxxxx // never a leading byte |
michael@0 | 131 | 110xxxxx 2 total |
michael@0 | 132 | 1110xxxx 3 total |
michael@0 | 133 | 11110xxx 4 total |
michael@0 | 134 | |
michael@0 | 135 | 11 10 01 01 xx xx xx xx 0... |
michael@0 | 136 | 0xE5XX0000 |
michael@0 | 137 | 0xE5 << 24 |
michael@0 | 138 | */ |
michael@0 | 139 | |
michael@0 | 140 | #ifdef SK_DEBUG |
michael@0 | 141 | static void assert_utf8_leadingbyte(unsigned c) { |
michael@0 | 142 | SkASSERT(c <= 0xF7); // otherwise leading byte is too big (more than 4 bytes) |
michael@0 | 143 | SkASSERT((c & 0xC0) != 0x80); // can't begin with a middle char |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | int SkUTF8_LeadByteToCount(unsigned c) { |
michael@0 | 147 | assert_utf8_leadingbyte(c); |
michael@0 | 148 | return (((0xE5 << 24) >> (c >> 4 << 1)) & 3) + 1; |
michael@0 | 149 | } |
michael@0 | 150 | #else |
michael@0 | 151 | #define assert_utf8_leadingbyte(c) |
michael@0 | 152 | #endif |
michael@0 | 153 | |
michael@0 | 154 | int SkUTF8_CountUnichars(const char utf8[]) { |
michael@0 | 155 | SkASSERT(utf8); |
michael@0 | 156 | |
michael@0 | 157 | int count = 0; |
michael@0 | 158 | |
michael@0 | 159 | for (;;) { |
michael@0 | 160 | int c = *(const uint8_t*)utf8; |
michael@0 | 161 | if (c == 0) { |
michael@0 | 162 | break; |
michael@0 | 163 | } |
michael@0 | 164 | utf8 += SkUTF8_LeadByteToCount(c); |
michael@0 | 165 | count += 1; |
michael@0 | 166 | } |
michael@0 | 167 | return count; |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | int SkUTF8_CountUnichars(const char utf8[], size_t byteLength) { |
michael@0 | 171 | SkASSERT(NULL != utf8 || 0 == byteLength); |
michael@0 | 172 | |
michael@0 | 173 | int count = 0; |
michael@0 | 174 | const char* stop = utf8 + byteLength; |
michael@0 | 175 | |
michael@0 | 176 | while (utf8 < stop) { |
michael@0 | 177 | utf8 += SkUTF8_LeadByteToCount(*(const uint8_t*)utf8); |
michael@0 | 178 | count += 1; |
michael@0 | 179 | } |
michael@0 | 180 | return count; |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | SkUnichar SkUTF8_ToUnichar(const char utf8[]) { |
michael@0 | 184 | SkASSERT(NULL != utf8); |
michael@0 | 185 | |
michael@0 | 186 | const uint8_t* p = (const uint8_t*)utf8; |
michael@0 | 187 | int c = *p; |
michael@0 | 188 | int hic = c << 24; |
michael@0 | 189 | |
michael@0 | 190 | assert_utf8_leadingbyte(c); |
michael@0 | 191 | |
michael@0 | 192 | if (hic < 0) { |
michael@0 | 193 | uint32_t mask = (uint32_t)~0x3F; |
michael@0 | 194 | hic <<= 1; |
michael@0 | 195 | do { |
michael@0 | 196 | c = (c << 6) | (*++p & 0x3F); |
michael@0 | 197 | mask <<= 5; |
michael@0 | 198 | } while ((hic <<= 1) < 0); |
michael@0 | 199 | c &= ~mask; |
michael@0 | 200 | } |
michael@0 | 201 | return c; |
michael@0 | 202 | } |
michael@0 | 203 | |
michael@0 | 204 | SkUnichar SkUTF8_NextUnichar(const char** ptr) { |
michael@0 | 205 | SkASSERT(NULL != ptr && NULL != *ptr); |
michael@0 | 206 | |
michael@0 | 207 | const uint8_t* p = (const uint8_t*)*ptr; |
michael@0 | 208 | int c = *p; |
michael@0 | 209 | int hic = c << 24; |
michael@0 | 210 | |
michael@0 | 211 | assert_utf8_leadingbyte(c); |
michael@0 | 212 | |
michael@0 | 213 | if (hic < 0) { |
michael@0 | 214 | uint32_t mask = (uint32_t)~0x3F; |
michael@0 | 215 | hic <<= 1; |
michael@0 | 216 | do { |
michael@0 | 217 | c = (c << 6) | (*++p & 0x3F); |
michael@0 | 218 | mask <<= 5; |
michael@0 | 219 | } while ((hic <<= 1) < 0); |
michael@0 | 220 | c &= ~mask; |
michael@0 | 221 | } |
michael@0 | 222 | *ptr = (char*)p + 1; |
michael@0 | 223 | return c; |
michael@0 | 224 | } |
michael@0 | 225 | |
michael@0 | 226 | SkUnichar SkUTF8_PrevUnichar(const char** ptr) { |
michael@0 | 227 | SkASSERT(NULL != ptr && NULL != *ptr); |
michael@0 | 228 | |
michael@0 | 229 | const char* p = *ptr; |
michael@0 | 230 | |
michael@0 | 231 | if (*--p & 0x80) { |
michael@0 | 232 | while (*--p & 0x40) { |
michael@0 | 233 | ; |
michael@0 | 234 | } |
michael@0 | 235 | } |
michael@0 | 236 | |
michael@0 | 237 | *ptr = (char*)p; |
michael@0 | 238 | return SkUTF8_NextUnichar(&p); |
michael@0 | 239 | } |
michael@0 | 240 | |
michael@0 | 241 | size_t SkUTF8_FromUnichar(SkUnichar uni, char utf8[]) { |
michael@0 | 242 | if ((uint32_t)uni > 0x10FFFF) { |
michael@0 | 243 | SkDEBUGFAIL("bad unichar"); |
michael@0 | 244 | return 0; |
michael@0 | 245 | } |
michael@0 | 246 | |
michael@0 | 247 | if (uni <= 127) { |
michael@0 | 248 | if (utf8) { |
michael@0 | 249 | *utf8 = (char)uni; |
michael@0 | 250 | } |
michael@0 | 251 | return 1; |
michael@0 | 252 | } |
michael@0 | 253 | |
michael@0 | 254 | char tmp[4]; |
michael@0 | 255 | char* p = tmp; |
michael@0 | 256 | size_t count = 1; |
michael@0 | 257 | |
michael@0 | 258 | SkDEBUGCODE(SkUnichar orig = uni;) |
michael@0 | 259 | |
michael@0 | 260 | while (uni > 0x7F >> count) { |
michael@0 | 261 | *p++ = (char)(0x80 | (uni & 0x3F)); |
michael@0 | 262 | uni >>= 6; |
michael@0 | 263 | count += 1; |
michael@0 | 264 | } |
michael@0 | 265 | |
michael@0 | 266 | if (utf8) { |
michael@0 | 267 | p = tmp; |
michael@0 | 268 | utf8 += count; |
michael@0 | 269 | while (p < tmp + count - 1) { |
michael@0 | 270 | *--utf8 = *p++; |
michael@0 | 271 | } |
michael@0 | 272 | *--utf8 = (char)(~(0xFF >> count) | uni); |
michael@0 | 273 | } |
michael@0 | 274 | |
michael@0 | 275 | SkASSERT(utf8 == NULL || orig == SkUTF8_ToUnichar(utf8)); |
michael@0 | 276 | return count; |
michael@0 | 277 | } |
michael@0 | 278 | |
michael@0 | 279 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 280 | |
michael@0 | 281 | int SkUTF16_CountUnichars(const uint16_t src[]) { |
michael@0 | 282 | SkASSERT(src); |
michael@0 | 283 | |
michael@0 | 284 | int count = 0; |
michael@0 | 285 | unsigned c; |
michael@0 | 286 | while ((c = *src++) != 0) { |
michael@0 | 287 | SkASSERT(!SkUTF16_IsLowSurrogate(c)); |
michael@0 | 288 | if (SkUTF16_IsHighSurrogate(c)) { |
michael@0 | 289 | c = *src++; |
michael@0 | 290 | SkASSERT(SkUTF16_IsLowSurrogate(c)); |
michael@0 | 291 | } |
michael@0 | 292 | count += 1; |
michael@0 | 293 | } |
michael@0 | 294 | return count; |
michael@0 | 295 | } |
michael@0 | 296 | |
michael@0 | 297 | int SkUTF16_CountUnichars(const uint16_t src[], int numberOf16BitValues) { |
michael@0 | 298 | SkASSERT(src); |
michael@0 | 299 | |
michael@0 | 300 | const uint16_t* stop = src + numberOf16BitValues; |
michael@0 | 301 | int count = 0; |
michael@0 | 302 | while (src < stop) { |
michael@0 | 303 | unsigned c = *src++; |
michael@0 | 304 | SkASSERT(!SkUTF16_IsLowSurrogate(c)); |
michael@0 | 305 | if (SkUTF16_IsHighSurrogate(c)) { |
michael@0 | 306 | SkASSERT(src < stop); |
michael@0 | 307 | c = *src++; |
michael@0 | 308 | SkASSERT(SkUTF16_IsLowSurrogate(c)); |
michael@0 | 309 | } |
michael@0 | 310 | count += 1; |
michael@0 | 311 | } |
michael@0 | 312 | return count; |
michael@0 | 313 | } |
michael@0 | 314 | |
michael@0 | 315 | SkUnichar SkUTF16_NextUnichar(const uint16_t** srcPtr) { |
michael@0 | 316 | SkASSERT(srcPtr && *srcPtr); |
michael@0 | 317 | |
michael@0 | 318 | const uint16_t* src = *srcPtr; |
michael@0 | 319 | SkUnichar c = *src++; |
michael@0 | 320 | |
michael@0 | 321 | SkASSERT(!SkUTF16_IsLowSurrogate(c)); |
michael@0 | 322 | if (SkUTF16_IsHighSurrogate(c)) { |
michael@0 | 323 | unsigned c2 = *src++; |
michael@0 | 324 | SkASSERT(SkUTF16_IsLowSurrogate(c2)); |
michael@0 | 325 | |
michael@0 | 326 | // c = ((c & 0x3FF) << 10) + (c2 & 0x3FF) + 0x10000 |
michael@0 | 327 | // c = (((c & 0x3FF) + 64) << 10) + (c2 & 0x3FF) |
michael@0 | 328 | c = (c << 10) + c2 + (0x10000 - (0xD800 << 10) - 0xDC00); |
michael@0 | 329 | } |
michael@0 | 330 | *srcPtr = src; |
michael@0 | 331 | return c; |
michael@0 | 332 | } |
michael@0 | 333 | |
michael@0 | 334 | SkUnichar SkUTF16_PrevUnichar(const uint16_t** srcPtr) { |
michael@0 | 335 | SkASSERT(srcPtr && *srcPtr); |
michael@0 | 336 | |
michael@0 | 337 | const uint16_t* src = *srcPtr; |
michael@0 | 338 | SkUnichar c = *--src; |
michael@0 | 339 | |
michael@0 | 340 | SkASSERT(!SkUTF16_IsHighSurrogate(c)); |
michael@0 | 341 | if (SkUTF16_IsLowSurrogate(c)) { |
michael@0 | 342 | unsigned c2 = *--src; |
michael@0 | 343 | SkASSERT(SkUTF16_IsHighSurrogate(c2)); |
michael@0 | 344 | c = (c2 << 10) + c + (0x10000 - (0xD800 << 10) - 0xDC00); |
michael@0 | 345 | } |
michael@0 | 346 | *srcPtr = src; |
michael@0 | 347 | return c; |
michael@0 | 348 | } |
michael@0 | 349 | |
michael@0 | 350 | size_t SkUTF16_FromUnichar(SkUnichar uni, uint16_t dst[]) { |
michael@0 | 351 | SkASSERT((unsigned)uni <= 0x10FFFF); |
michael@0 | 352 | |
michael@0 | 353 | int extra = (uni > 0xFFFF); |
michael@0 | 354 | |
michael@0 | 355 | if (dst) { |
michael@0 | 356 | if (extra) { |
michael@0 | 357 | // dst[0] = SkToU16(0xD800 | ((uni - 0x10000) >> 10)); |
michael@0 | 358 | // dst[0] = SkToU16(0xD800 | ((uni >> 10) - 64)); |
michael@0 | 359 | dst[0] = SkToU16((0xD800 - 64) + (uni >> 10)); |
michael@0 | 360 | dst[1] = SkToU16(0xDC00 | (uni & 0x3FF)); |
michael@0 | 361 | |
michael@0 | 362 | SkASSERT(SkUTF16_IsHighSurrogate(dst[0])); |
michael@0 | 363 | SkASSERT(SkUTF16_IsLowSurrogate(dst[1])); |
michael@0 | 364 | } else { |
michael@0 | 365 | dst[0] = SkToU16(uni); |
michael@0 | 366 | SkASSERT(!SkUTF16_IsHighSurrogate(dst[0])); |
michael@0 | 367 | SkASSERT(!SkUTF16_IsLowSurrogate(dst[0])); |
michael@0 | 368 | } |
michael@0 | 369 | } |
michael@0 | 370 | return 1 + extra; |
michael@0 | 371 | } |
michael@0 | 372 | |
michael@0 | 373 | size_t SkUTF16_ToUTF8(const uint16_t utf16[], int numberOf16BitValues, |
michael@0 | 374 | char utf8[]) { |
michael@0 | 375 | SkASSERT(numberOf16BitValues >= 0); |
michael@0 | 376 | if (numberOf16BitValues <= 0) { |
michael@0 | 377 | return 0; |
michael@0 | 378 | } |
michael@0 | 379 | |
michael@0 | 380 | SkASSERT(utf16 != NULL); |
michael@0 | 381 | |
michael@0 | 382 | const uint16_t* stop = utf16 + numberOf16BitValues; |
michael@0 | 383 | size_t size = 0; |
michael@0 | 384 | |
michael@0 | 385 | if (utf8 == NULL) { // just count |
michael@0 | 386 | while (utf16 < stop) { |
michael@0 | 387 | size += SkUTF8_FromUnichar(SkUTF16_NextUnichar(&utf16), NULL); |
michael@0 | 388 | } |
michael@0 | 389 | } else { |
michael@0 | 390 | char* start = utf8; |
michael@0 | 391 | while (utf16 < stop) { |
michael@0 | 392 | utf8 += SkUTF8_FromUnichar(SkUTF16_NextUnichar(&utf16), utf8); |
michael@0 | 393 | } |
michael@0 | 394 | size = utf8 - start; |
michael@0 | 395 | } |
michael@0 | 396 | return size; |
michael@0 | 397 | } |