Thu, 15 Jan 2015 15:59:08 +0100
Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include "cpr_types.h" |
michael@0 | 6 | #include "cpr_stdlib.h" |
michael@0 | 7 | #include "cpr_string.h" |
michael@0 | 8 | #include "cpr_strings.h" |
michael@0 | 9 | |
michael@0 | 10 | |
michael@0 | 11 | /** |
michael@0 | 12 | * cpr_strdup |
michael@0 | 13 | * |
michael@0 | 14 | * @brief The CPR wrapper for strdup |
michael@0 | 15 | |
michael@0 | 16 | * The cpr_strdup shall return a pointer to a new string, which is a duplicate |
michael@0 | 17 | * of the string pointed to by "str" argument. A null pointer is returned if the |
michael@0 | 18 | * new string cannot be created. |
michael@0 | 19 | * |
michael@0 | 20 | * @param[in] str - The string that needs to be duplicated |
michael@0 | 21 | * |
michael@0 | 22 | * @return The duplicated string or NULL in case of no memory |
michael@0 | 23 | * |
michael@0 | 24 | */ |
michael@0 | 25 | char * |
michael@0 | 26 | cpr_strdup (const char *str) |
michael@0 | 27 | { |
michael@0 | 28 | char *dup; |
michael@0 | 29 | size_t len; |
michael@0 | 30 | |
michael@0 | 31 | if (!str) { |
michael@0 | 32 | return (char *) NULL; |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | len = strlen(str); |
michael@0 | 36 | if (len == 0) { |
michael@0 | 37 | return (char *) NULL; |
michael@0 | 38 | } |
michael@0 | 39 | len++; |
michael@0 | 40 | |
michael@0 | 41 | dup = cpr_malloc(len * sizeof(char)); |
michael@0 | 42 | if (!dup) { |
michael@0 | 43 | return (char *) NULL; |
michael@0 | 44 | } |
michael@0 | 45 | (void) memcpy(dup, str, len); |
michael@0 | 46 | return dup; |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | |
michael@0 | 50 | /** |
michael@0 | 51 | * cpr_strcasecmp |
michael@0 | 52 | * |
michael@0 | 53 | * @brief The CPR wrapper for strcasecmp |
michael@0 | 54 | * |
michael@0 | 55 | * The cpr_strcasecmp performs case insensitive string comparison of the "s1" |
michael@0 | 56 | * and the "s2" strings. |
michael@0 | 57 | * |
michael@0 | 58 | * @param[in] s1 - The first string |
michael@0 | 59 | * @param[in] s2 - The second string |
michael@0 | 60 | * |
michael@0 | 61 | * @return integer <,=,> 0 depending on whether s1 is <,=,> s2 |
michael@0 | 62 | */ |
michael@0 | 63 | #ifndef CPR_USE_OS_STRCASECMP |
michael@0 | 64 | int |
michael@0 | 65 | cpr_strcasecmp (const char *s1, const char *s2) |
michael@0 | 66 | { |
michael@0 | 67 | const unsigned char *us1 = (const unsigned char *) s1; |
michael@0 | 68 | const unsigned char *us2 = (const unsigned char *) s2; |
michael@0 | 69 | |
michael@0 | 70 | /* No match if only one ptr is NULL */ |
michael@0 | 71 | if ((!s1 && s2) || (s1 && !s2)) { |
michael@0 | 72 | /* |
michael@0 | 73 | * If one of these is NULL it will be the lesser of the two |
michael@0 | 74 | * values and therefore we'll get the proper sign in the int |
michael@0 | 75 | */ |
michael@0 | 76 | return (int) (s1 - s2); |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | /* Match if both ptrs the same (e.g. NULL) */ |
michael@0 | 80 | if (s1 == s2) |
michael@0 | 81 | return 0; |
michael@0 | 82 | |
michael@0 | 83 | while (*us1 != '\0' && *us2 != '\0' && toupper(*us1) == toupper(*us2)) { |
michael@0 | 84 | us1++; |
michael@0 | 85 | us2++; |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | return (toupper(*us1) - toupper(*us2)); |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | /** |
michael@0 | 92 | * cpr_strncasecmp |
michael@0 | 93 | * |
michael@0 | 94 | * @brief The CPR wrapper for strncasecmp |
michael@0 | 95 | * |
michael@0 | 96 | * The cpr_strncasecmp performs case insensitive string comparison for specific |
michael@0 | 97 | * length "len". |
michael@0 | 98 | * |
michael@0 | 99 | * @param[in] s1 - The first string |
michael@0 | 100 | * @param[in] s2 - The second string |
michael@0 | 101 | * @param[in] len - The length to be compared |
michael@0 | 102 | * |
michael@0 | 103 | * @return integer <,=,> 0 depending on whether s1 is <,=,> s2 |
michael@0 | 104 | */ |
michael@0 | 105 | int |
michael@0 | 106 | cpr_strncasecmp (const char *s1, const char *s2, size_t len) |
michael@0 | 107 | { |
michael@0 | 108 | const unsigned char *us1 = (const unsigned char *) s1; |
michael@0 | 109 | const unsigned char *us2 = (const unsigned char *) s2; |
michael@0 | 110 | |
michael@0 | 111 | /* No match if only one ptr is NULL */ |
michael@0 | 112 | if ((!s1 && s2) || (s1 && !s2)) |
michael@0 | 113 | return ((int) (s1 - s2)); |
michael@0 | 114 | |
michael@0 | 115 | if ((len == 0) || (s1 == s2)) |
michael@0 | 116 | return 0; |
michael@0 | 117 | |
michael@0 | 118 | while (len-- > 0 && toupper(*us1) == toupper(*us2)) { |
michael@0 | 119 | if (len == 0 || *us1 == '\0' || *us2 == '\0') |
michael@0 | 120 | break; |
michael@0 | 121 | us1++; |
michael@0 | 122 | us2++; |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | return (toupper(*us1) - toupper(*us2)); |
michael@0 | 126 | } |
michael@0 | 127 | #endif |
michael@0 | 128 | |
michael@0 | 129 | /** |
michael@0 | 130 | * strcasestr |
michael@0 | 131 | * |
michael@0 | 132 | * @brief The same as strstr, but ignores case |
michael@0 | 133 | * |
michael@0 | 134 | * The strcasestr performs the strstr function, but ignores the case. |
michael@0 | 135 | * This function shall locate the first occurrence in the string |
michael@0 | 136 | * pointed to by s1 of the sequence of bytes (excluding the terminating |
michael@0 | 137 | * null byte) in the string pointed to by s2. |
michael@0 | 138 | * |
michael@0 | 139 | * @param[in] s1 - The input string |
michael@0 | 140 | * @param[in] s2 - The pattern to be matched |
michael@0 | 141 | * |
michael@0 | 142 | * @return A pointer to the first occurrence of string s2 found |
michael@0 | 143 | * in string s1 or NULL if not found. If s2 is an empty |
michael@0 | 144 | * string then s1 is returned. |
michael@0 | 145 | */ |
michael@0 | 146 | char * |
michael@0 | 147 | strcasestr (const char *s1, const char *s2) |
michael@0 | 148 | { |
michael@0 | 149 | unsigned int i; |
michael@0 | 150 | |
michael@0 | 151 | if (!s1) |
michael@0 | 152 | return (char *) NULL; |
michael@0 | 153 | |
michael@0 | 154 | if (!s2 || (s1 == s2) || (*s2 == '\0')) |
michael@0 | 155 | return (char *) s1; |
michael@0 | 156 | |
michael@0 | 157 | while (*s1) { |
michael@0 | 158 | i = 0; |
michael@0 | 159 | do { |
michael@0 | 160 | if (s2[i] == '\0') |
michael@0 | 161 | return (char *) s1; |
michael@0 | 162 | if (s1[i] == '\0') |
michael@0 | 163 | return (char *) NULL; |
michael@0 | 164 | if (toupper(s1[i]) != toupper(s2[i])) |
michael@0 | 165 | break; |
michael@0 | 166 | i++; |
michael@0 | 167 | } while (1); |
michael@0 | 168 | s1++; |
michael@0 | 169 | } |
michael@0 | 170 | |
michael@0 | 171 | return (char *) NULL; |
michael@0 | 172 | } |
michael@0 | 173 |