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