xpcom/glue/nsCRTGlue.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

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 /* 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 "nsCRTGlue.h"
michael@0 6 #include "nsXPCOM.h"
michael@0 7 #include "nsDebug.h"
michael@0 8 #include "prtime.h"
michael@0 9
michael@0 10 #include <stdlib.h>
michael@0 11 #include <string.h>
michael@0 12 #include <stdio.h>
michael@0 13 #include <stdarg.h>
michael@0 14
michael@0 15 #ifdef XP_WIN
michael@0 16 #include <io.h>
michael@0 17 #include <windows.h>
michael@0 18 #endif
michael@0 19
michael@0 20 #ifdef ANDROID
michael@0 21 #include <android/log.h>
michael@0 22 #endif
michael@0 23
michael@0 24 const char*
michael@0 25 NS_strspnp(const char *delims, const char *str)
michael@0 26 {
michael@0 27 const char *d;
michael@0 28 do {
michael@0 29 for (d = delims; *d != '\0'; ++d) {
michael@0 30 if (*str == *d) {
michael@0 31 ++str;
michael@0 32 break;
michael@0 33 }
michael@0 34 }
michael@0 35 } while (*d);
michael@0 36
michael@0 37 return str;
michael@0 38 }
michael@0 39
michael@0 40 char*
michael@0 41 NS_strtok(const char *delims, char **str)
michael@0 42 {
michael@0 43 if (!*str)
michael@0 44 return nullptr;
michael@0 45
michael@0 46 char *ret = (char*) NS_strspnp(delims, *str);
michael@0 47
michael@0 48 if (!*ret) {
michael@0 49 *str = ret;
michael@0 50 return nullptr;
michael@0 51 }
michael@0 52
michael@0 53 char *i = ret;
michael@0 54 do {
michael@0 55 for (const char *d = delims; *d != '\0'; ++d) {
michael@0 56 if (*i == *d) {
michael@0 57 *i = '\0';
michael@0 58 *str = ++i;
michael@0 59 return ret;
michael@0 60 }
michael@0 61 }
michael@0 62 ++i;
michael@0 63 } while (*i);
michael@0 64
michael@0 65 *str = nullptr;
michael@0 66 return ret;
michael@0 67 }
michael@0 68
michael@0 69 uint32_t
michael@0 70 NS_strlen(const char16_t *aString)
michael@0 71 {
michael@0 72 const char16_t *end;
michael@0 73
michael@0 74 for (end = aString; *end; ++end) {
michael@0 75 // empty loop
michael@0 76 }
michael@0 77
michael@0 78 return end - aString;
michael@0 79 }
michael@0 80
michael@0 81 int
michael@0 82 NS_strcmp(const char16_t *a, const char16_t *b)
michael@0 83 {
michael@0 84 while (*b) {
michael@0 85 int r = *a - *b;
michael@0 86 if (r)
michael@0 87 return r;
michael@0 88
michael@0 89 ++a;
michael@0 90 ++b;
michael@0 91 }
michael@0 92
michael@0 93 return *a != '\0';
michael@0 94 }
michael@0 95
michael@0 96 char16_t*
michael@0 97 NS_strdup(const char16_t *aString)
michael@0 98 {
michael@0 99 uint32_t len = NS_strlen(aString);
michael@0 100 return NS_strndup(aString, len);
michael@0 101 }
michael@0 102
michael@0 103 char16_t*
michael@0 104 NS_strndup(const char16_t *aString, uint32_t aLen)
michael@0 105 {
michael@0 106 char16_t *newBuf = (char16_t*) NS_Alloc((aLen + 1) * sizeof(char16_t));
michael@0 107 if (newBuf) {
michael@0 108 memcpy(newBuf, aString, aLen * sizeof(char16_t));
michael@0 109 newBuf[aLen] = '\0';
michael@0 110 }
michael@0 111 return newBuf;
michael@0 112 }
michael@0 113
michael@0 114 char*
michael@0 115 NS_strdup(const char *aString)
michael@0 116 {
michael@0 117 uint32_t len = strlen(aString);
michael@0 118 char *str = (char*) NS_Alloc(len + 1);
michael@0 119 if (str) {
michael@0 120 memcpy(str, aString, len);
michael@0 121 str[len] = '\0';
michael@0 122 }
michael@0 123 return str;
michael@0 124 }
michael@0 125
michael@0 126 // This table maps uppercase characters to lower case characters;
michael@0 127 // characters that are neither upper nor lower case are unaffected.
michael@0 128 const unsigned char nsLowerUpperUtils::kUpper2Lower[256] = {
michael@0 129 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
michael@0 130 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
michael@0 131 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
michael@0 132 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
michael@0 133 64,
michael@0 134
michael@0 135 // upper band mapped to lower [A-Z] => [a-z]
michael@0 136 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111,
michael@0 137 112,113,114,115,116,117,118,119,120,121,122,
michael@0 138
michael@0 139 91, 92, 93, 94, 95,
michael@0 140 96, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111,
michael@0 141 112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,
michael@0 142 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
michael@0 143 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
michael@0 144 160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
michael@0 145 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
michael@0 146 192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
michael@0 147 208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,
michael@0 148 224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,
michael@0 149 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255
michael@0 150 };
michael@0 151
michael@0 152 const unsigned char nsLowerUpperUtils::kLower2Upper[256] = {
michael@0 153 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
michael@0 154 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
michael@0 155 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
michael@0 156 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
michael@0 157 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
michael@0 158 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
michael@0 159 96,
michael@0 160
michael@0 161 // lower band mapped to upper [a-z] => [A-Z]
michael@0 162 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
michael@0 163 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
michael@0 164
michael@0 165 123,124,125,126,127,
michael@0 166 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
michael@0 167 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
michael@0 168 160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
michael@0 169 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
michael@0 170 192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
michael@0 171 208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,
michael@0 172 224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,
michael@0 173 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255
michael@0 174 };
michael@0 175
michael@0 176 bool NS_IsUpper(char aChar)
michael@0 177 {
michael@0 178 return aChar != (char)nsLowerUpperUtils::kUpper2Lower[(unsigned char)aChar];
michael@0 179 }
michael@0 180
michael@0 181 bool NS_IsLower(char aChar)
michael@0 182 {
michael@0 183 return aChar != (char)nsLowerUpperUtils::kLower2Upper[(unsigned char)aChar];
michael@0 184 }
michael@0 185
michael@0 186 bool NS_IsAscii(char16_t aChar)
michael@0 187 {
michael@0 188 return (0x0080 > aChar);
michael@0 189 }
michael@0 190
michael@0 191 bool NS_IsAscii(const char16_t *aString)
michael@0 192 {
michael@0 193 while(*aString) {
michael@0 194 if( 0x0080 <= *aString)
michael@0 195 return false;
michael@0 196 aString++;
michael@0 197 }
michael@0 198 return true;
michael@0 199 }
michael@0 200
michael@0 201 bool NS_IsAscii(const char *aString)
michael@0 202 {
michael@0 203 while(*aString) {
michael@0 204 if( 0x80 & *aString)
michael@0 205 return false;
michael@0 206 aString++;
michael@0 207 }
michael@0 208 return true;
michael@0 209 }
michael@0 210
michael@0 211 bool NS_IsAscii(const char* aString, uint32_t aLength)
michael@0 212 {
michael@0 213 const char* end = aString + aLength;
michael@0 214 while (aString < end) {
michael@0 215 if (0x80 & *aString)
michael@0 216 return false;
michael@0 217 ++aString;
michael@0 218 }
michael@0 219 return true;
michael@0 220 }
michael@0 221
michael@0 222 bool NS_IsAsciiAlpha(char16_t aChar)
michael@0 223 {
michael@0 224 return ((aChar >= 'A') && (aChar <= 'Z')) ||
michael@0 225 ((aChar >= 'a') && (aChar <= 'z'));
michael@0 226 }
michael@0 227
michael@0 228 bool NS_IsAsciiWhitespace(char16_t aChar)
michael@0 229 {
michael@0 230 return aChar == ' ' ||
michael@0 231 aChar == '\r' ||
michael@0 232 aChar == '\n' ||
michael@0 233 aChar == '\t';
michael@0 234 }
michael@0 235
michael@0 236 bool NS_IsAsciiDigit(char16_t aChar)
michael@0 237 {
michael@0 238 return aChar >= '0' && aChar <= '9';
michael@0 239 }
michael@0 240
michael@0 241
michael@0 242 #ifndef XPCOM_GLUE_AVOID_NSPR
michael@0 243 #define TABLE_SIZE 36
michael@0 244 static const char table[] = {
michael@0 245 'a','b','c','d','e','f','g','h','i','j',
michael@0 246 'k','l','m','n','o','p','q','r','s','t',
michael@0 247 'u','v','w','x','y','z','0','1','2','3',
michael@0 248 '4','5','6','7','8','9'
michael@0 249 };
michael@0 250
michael@0 251 void NS_MakeRandomString(char *aBuf, int32_t aBufLen)
michael@0 252 {
michael@0 253 // turn PR_Now() into milliseconds since epoch
michael@0 254 // and salt rand with that.
michael@0 255 static unsigned int seed = 0;
michael@0 256 if (seed == 0) {
michael@0 257 double fpTime = double(PR_Now());
michael@0 258 seed = (unsigned int)(fpTime * 1e-6 + 0.5); // use 1e-6, granularity of PR_Now() on the mac is seconds
michael@0 259 srand(seed);
michael@0 260 }
michael@0 261
michael@0 262 int32_t i;
michael@0 263 for (i=0;i<aBufLen;i++) {
michael@0 264 *aBuf++ = table[rand()%TABLE_SIZE];
michael@0 265 }
michael@0 266 *aBuf = 0;
michael@0 267 }
michael@0 268
michael@0 269 #endif
michael@0 270 #if defined(XP_WIN)
michael@0 271
michael@0 272 #define va_copy(dest, src) (dest = src)
michael@0 273
michael@0 274 void
michael@0 275 vprintf_stderr(const char *fmt, va_list args)
michael@0 276 {
michael@0 277 if (IsDebuggerPresent()) {
michael@0 278 char buf[2048];
michael@0 279 va_list argsCpy;
michael@0 280 va_copy(argsCpy, args);
michael@0 281 vsnprintf(buf, sizeof(buf), fmt, argsCpy);
michael@0 282 buf[sizeof(buf) - 1] = '\0';
michael@0 283 va_end(argsCpy);
michael@0 284 OutputDebugStringA(buf);
michael@0 285 }
michael@0 286
michael@0 287 FILE *fp = _fdopen(_dup(2), "a");
michael@0 288 if (!fp)
michael@0 289 return;
michael@0 290
michael@0 291 vfprintf(fp, fmt, args);
michael@0 292
michael@0 293 fclose(fp);
michael@0 294 }
michael@0 295
michael@0 296 #undef va_copy
michael@0 297
michael@0 298 #elif defined(ANDROID)
michael@0 299 void
michael@0 300 vprintf_stderr(const char *fmt, va_list args)
michael@0 301 {
michael@0 302 __android_log_vprint(ANDROID_LOG_INFO, "Gecko", fmt, args);
michael@0 303 }
michael@0 304 #else
michael@0 305 void
michael@0 306 vprintf_stderr(const char *fmt, va_list args)
michael@0 307 {
michael@0 308 vfprintf(stderr, fmt, args);
michael@0 309 }
michael@0 310 #endif
michael@0 311
michael@0 312 void
michael@0 313 printf_stderr(const char *fmt, ...)
michael@0 314 {
michael@0 315 va_list args;
michael@0 316 va_start(args, fmt);
michael@0 317 vprintf_stderr(fmt, args);
michael@0 318 va_end(args);
michael@0 319 }
michael@0 320
michael@0 321 void
michael@0 322 fprintf_stderr(FILE* aFile, const char *fmt, ...)
michael@0 323 {
michael@0 324 va_list args;
michael@0 325 va_start(args, fmt);
michael@0 326 if (aFile == stderr) {
michael@0 327 vprintf_stderr(fmt, args);
michael@0 328 } else {
michael@0 329 vfprintf(aFile, fmt, args);
michael@0 330 }
michael@0 331 va_end(args);
michael@0 332 }
michael@0 333
michael@0 334

mercurial