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 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "nsID.h" |
michael@0 | 7 | #include "prprf.h" |
michael@0 | 8 | #include "nsMemory.h" |
michael@0 | 9 | |
michael@0 | 10 | /** |
michael@0 | 11 | * Multiplies the_int_var with 16 (0x10) and adds the value of the |
michael@0 | 12 | * hexadecimal digit the_char. If it fails it returns false from |
michael@0 | 13 | * the function it's used in. |
michael@0 | 14 | */ |
michael@0 | 15 | |
michael@0 | 16 | #define ADD_HEX_CHAR_TO_INT_OR_RETURN_FALSE(the_char, the_int_var) \ |
michael@0 | 17 | the_int_var = (the_int_var << 4) + the_char; \ |
michael@0 | 18 | if(the_char >= '0' && the_char <= '9') the_int_var -= '0'; \ |
michael@0 | 19 | else if(the_char >= 'a' && the_char <= 'f') the_int_var -= 'a'-10; \ |
michael@0 | 20 | else if(the_char >= 'A' && the_char <= 'F') the_int_var -= 'A'-10; \ |
michael@0 | 21 | else return false |
michael@0 | 22 | |
michael@0 | 23 | |
michael@0 | 24 | /** |
michael@0 | 25 | * Parses number_of_chars characters from the char_pointer pointer and |
michael@0 | 26 | * puts the number in the dest_variable. The pointer is moved to point |
michael@0 | 27 | * at the first character after the parsed ones. If it fails it returns |
michael@0 | 28 | * false from the function the macro is used in. |
michael@0 | 29 | */ |
michael@0 | 30 | |
michael@0 | 31 | #define PARSE_CHARS_TO_NUM(char_pointer, dest_variable, number_of_chars) \ |
michael@0 | 32 | do { int32_t _i=number_of_chars; \ |
michael@0 | 33 | dest_variable = 0; \ |
michael@0 | 34 | while(_i) { \ |
michael@0 | 35 | ADD_HEX_CHAR_TO_INT_OR_RETURN_FALSE(*char_pointer, dest_variable); \ |
michael@0 | 36 | char_pointer++; \ |
michael@0 | 37 | _i--; \ |
michael@0 | 38 | } } while(0) |
michael@0 | 39 | |
michael@0 | 40 | |
michael@0 | 41 | /** |
michael@0 | 42 | * Parses a hyphen from the char_pointer string. If there is no hyphen there |
michael@0 | 43 | * the function returns false from the function it's used in. The |
michael@0 | 44 | * char_pointer is advanced one step. |
michael@0 | 45 | */ |
michael@0 | 46 | |
michael@0 | 47 | #define PARSE_HYPHEN(char_pointer) if(*(char_pointer++) != '-') return false |
michael@0 | 48 | |
michael@0 | 49 | /* |
michael@0 | 50 | * Turns a {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} string into |
michael@0 | 51 | * an nsID. It can also handle the old format without the { and }. |
michael@0 | 52 | */ |
michael@0 | 53 | |
michael@0 | 54 | bool nsID::Parse(const char *aIDStr) |
michael@0 | 55 | { |
michael@0 | 56 | /* Optimized for speed */ |
michael@0 | 57 | if(!aIDStr) { |
michael@0 | 58 | return false; |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | bool expectFormat1 = (aIDStr[0] == '{'); |
michael@0 | 62 | if(expectFormat1) aIDStr++; |
michael@0 | 63 | |
michael@0 | 64 | PARSE_CHARS_TO_NUM(aIDStr, m0, 8); |
michael@0 | 65 | PARSE_HYPHEN(aIDStr); |
michael@0 | 66 | PARSE_CHARS_TO_NUM(aIDStr, m1, 4); |
michael@0 | 67 | PARSE_HYPHEN(aIDStr); |
michael@0 | 68 | PARSE_CHARS_TO_NUM(aIDStr, m2, 4); |
michael@0 | 69 | PARSE_HYPHEN(aIDStr); |
michael@0 | 70 | int i; |
michael@0 | 71 | for(i=0; i<2; i++) |
michael@0 | 72 | PARSE_CHARS_TO_NUM(aIDStr, m3[i], 2); |
michael@0 | 73 | PARSE_HYPHEN(aIDStr); |
michael@0 | 74 | while(i < 8) { |
michael@0 | 75 | PARSE_CHARS_TO_NUM(aIDStr, m3[i], 2); |
michael@0 | 76 | i++; |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | return expectFormat1 ? *aIDStr == '}' : true; |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | #ifndef XPCOM_GLUE_AVOID_NSPR |
michael@0 | 83 | |
michael@0 | 84 | static const char gIDFormat[] = |
michael@0 | 85 | "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}"; |
michael@0 | 86 | |
michael@0 | 87 | /* |
michael@0 | 88 | * Returns an allocated string in {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} |
michael@0 | 89 | * format. The string is allocated with NS_Alloc and should be freed by |
michael@0 | 90 | * the caller. |
michael@0 | 91 | */ |
michael@0 | 92 | |
michael@0 | 93 | char *nsID::ToString() const |
michael@0 | 94 | { |
michael@0 | 95 | char *res = (char*)NS_Alloc(NSID_LENGTH); |
michael@0 | 96 | |
michael@0 | 97 | if (res != nullptr) { |
michael@0 | 98 | PR_snprintf(res, NSID_LENGTH, gIDFormat, |
michael@0 | 99 | m0, (uint32_t) m1, (uint32_t) m2, |
michael@0 | 100 | (uint32_t) m3[0], (uint32_t) m3[1], (uint32_t) m3[2], |
michael@0 | 101 | (uint32_t) m3[3], (uint32_t) m3[4], (uint32_t) m3[5], |
michael@0 | 102 | (uint32_t) m3[6], (uint32_t) m3[7]); |
michael@0 | 103 | } |
michael@0 | 104 | return res; |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | void nsID::ToProvidedString(char (&dest)[NSID_LENGTH]) const |
michael@0 | 108 | { |
michael@0 | 109 | PR_snprintf(dest, NSID_LENGTH, gIDFormat, |
michael@0 | 110 | m0, (uint32_t) m1, (uint32_t) m2, |
michael@0 | 111 | (uint32_t) m3[0], (uint32_t) m3[1], (uint32_t) m3[2], |
michael@0 | 112 | (uint32_t) m3[3], (uint32_t) m3[4], (uint32_t) m3[5], |
michael@0 | 113 | (uint32_t) m3[6], (uint32_t) m3[7]); |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | #endif // XPCOM_GLUE_AVOID_NSPR |