Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/xpcom/base/nsCRTGlue.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "nsCRTGlue.h"
8
#include "nsXPCOM.h"
9
#include "nsDebug.h"
10
#include "prtime.h"
11
12
#include <stdlib.h>
13
#include <string.h>
14
#include <stdio.h>
15
#include <stdarg.h>
16
17
#include "mozilla/Sprintf.h"
18
19
#ifdef XP_WIN
20
#include <io.h>
21
#include <windows.h>
22
#include "mozilla/UniquePtr.h"
23
#endif
24
25
#ifdef ANDROID
26
#include <android/log.h>
27
#endif
28
29
using namespace mozilla;
30
31
const char*
32
NS_strspnp(const char* aDelims, const char* aStr)
33
3.17k
{
34
3.17k
  const char* d;
35
10.3k
  do {
36
81.3k
    for (d = aDelims; *d != '\0'; ++d) {
37
78.2k
      if (*aStr == *d) {
38
7.13k
        ++aStr;
39
7.13k
        break;
40
7.13k
      }
41
78.2k
    }
42
10.3k
  } while (*d);
43
3.17k
44
3.17k
  return aStr;
45
3.17k
}
46
47
char*
48
NS_strtok(const char* aDelims, char** aStr)
49
0
{
50
0
  if (!*aStr) {
51
0
    return nullptr;
52
0
  }
53
0
54
0
  char* ret = (char*)NS_strspnp(aDelims, *aStr);
55
0
56
0
  if (!*ret) {
57
0
    *aStr = ret;
58
0
    return nullptr;
59
0
  }
60
0
61
0
  char* i = ret;
62
0
  do {
63
0
    for (const char* d = aDelims; *d != '\0'; ++d) {
64
0
      if (*i == *d) {
65
0
        *i = '\0';
66
0
        *aStr = ++i;
67
0
        return ret;
68
0
      }
69
0
    }
70
0
    ++i;
71
0
  } while (*i);
72
0
73
0
  *aStr = nullptr;
74
0
  return ret;
75
0
}
76
77
uint32_t
78
NS_strlen(const char16_t* aString)
79
0
{
80
0
  MOZ_ASSERT(aString);
81
0
  const char16_t* end;
82
0
83
0
  for (end = aString; *end; ++end) {
84
0
    // empty loop
85
0
  }
86
0
87
0
  return end - aString;
88
0
}
89
90
int
91
NS_strcmp(const char16_t* aStrA, const char16_t* aStrB)
92
0
{
93
0
  while (*aStrB) {
94
0
    int r = *aStrA - *aStrB;
95
0
    if (r) {
96
0
      return r;
97
0
    }
98
0
99
0
    ++aStrA;
100
0
    ++aStrB;
101
0
  }
102
0
103
0
  return *aStrA != '\0';
104
0
}
105
106
int
107
NS_strncmp(const char16_t* aStrA, const char16_t* aStrB, size_t aLen)
108
0
{
109
0
  while (aLen && *aStrB) {
110
0
    int r = *aStrA - *aStrB;
111
0
    if (r) {
112
0
      return r;
113
0
    }
114
0
115
0
    ++aStrA;
116
0
    ++aStrB;
117
0
    --aLen;
118
0
  }
119
0
120
0
  return aLen ? *aStrA != '\0' : 0;
121
0
}
122
123
char16_t*
124
NS_xstrdup(const char16_t* aString)
125
0
{
126
0
  uint32_t len = NS_strlen(aString);
127
0
  return NS_xstrndup(aString, len);
128
0
}
129
130
template<typename CharT>
131
CharT*
132
NS_xstrndup(const CharT* aString, uint32_t aLen)
133
0
{
134
0
  auto newBuf = (CharT*)moz_xmalloc((aLen + 1) * sizeof(CharT));
135
0
  memcpy(newBuf, aString, aLen * sizeof(CharT));
136
0
  newBuf[aLen] = '\0';
137
0
  return newBuf;
138
0
}
Unexecuted instantiation: char16_t* NS_xstrndup<char16_t>(char16_t const*, unsigned int)
Unexecuted instantiation: char* NS_xstrndup<char>(char const*, unsigned int)
139
140
template char16_t* NS_xstrndup<char16_t>(const char16_t* aString, uint32_t aLen);
141
template char* NS_xstrndup<char>(const char* aString, uint32_t aLen);
142
143
char*
144
NS_xstrdup(const char* aString)
145
1.67M
{
146
1.67M
  uint32_t len = strlen(aString);
147
1.67M
  char* str = (char*)moz_xmalloc(len + 1);
148
1.67M
  memcpy(str, aString, len);
149
1.67M
  str[len] = '\0';
150
1.67M
  return str;
151
1.67M
}
152
153
// This table maps uppercase characters to lower case characters;
154
// characters that are neither upper nor lower case are unaffected.
155
const unsigned char nsLowerUpperUtils::kUpper2Lower[256] = {
156
    0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
157
   16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
158
   32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
159
   48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
160
   64,
161
162
    // upper band mapped to lower [A-Z] => [a-z]
163
       97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111,
164
  112,113,114,115,116,117,118,119,120,121,122,
165
166
                                               91, 92, 93, 94, 95,
167
   96, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111,
168
  112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,
169
  128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
170
  144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
171
  160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
172
  176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
173
  192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
174
  208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,
175
  224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,
176
  240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255
177
};
178
179
const unsigned char nsLowerUpperUtils::kLower2Upper[256] = {
180
    0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
181
   16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
182
   32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
183
   48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
184
   64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
185
   80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
186
   96,
187
188
    // lower band mapped to upper [a-z] => [A-Z]
189
       65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
190
   80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
191
192
                                              123,124,125,126,127,
193
  128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
194
  144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
195
  160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
196
  176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
197
  192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
198
  208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,
199
  224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,
200
  240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255
201
};
202
203
bool
204
NS_IsUpper(char aChar)
205
0
{
206
0
  return aChar != (char)nsLowerUpperUtils::kUpper2Lower[(unsigned char)aChar];
207
0
}
208
209
bool
210
NS_IsLower(char aChar)
211
0
{
212
0
  return aChar != (char)nsLowerUpperUtils::kLower2Upper[(unsigned char)aChar];
213
0
}
214
215
#ifndef XPCOM_GLUE_AVOID_NSPR
216
217
void
218
NS_MakeRandomString(char* aBuf, int32_t aBufLen)
219
0
{
220
0
#define TABLE_SIZE 36
221
0
  static const char table[] = {
222
0
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
223
0
    'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
224
0
    'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3',
225
0
    '4', '5', '6', '7', '8', '9'
226
0
  };
227
0
228
0
  // turn PR_Now() into milliseconds since epoch
229
0
  // and salt rand with that.
230
0
  static unsigned int seed = 0;
231
0
  if (seed == 0) {
232
0
    double fpTime = double(PR_Now());
233
0
    seed = (unsigned int)(fpTime * 1e-6 + 0.5); // use 1e-6, granularity of PR_Now() on the mac is seconds
234
0
    srand(seed);
235
0
  }
236
0
237
0
  int32_t i;
238
0
  for (i = 0; i < aBufLen; ++i) {
239
0
    *aBuf++ = table[rand() % TABLE_SIZE];
240
0
  }
241
0
  *aBuf = 0;
242
0
}
243
244
#endif
245
246
#ifdef HAVE_VA_COPY
247
#define VARARGS_ASSIGN(foo, bar)        VA_COPY(foo,bar)
248
#elif defined(HAVE_VA_LIST_AS_ARRAY)
249
#define VARARGS_ASSIGN(foo, bar)     foo[0] = bar[0]
250
#else
251
#define VARARGS_ASSIGN(foo, bar)     (foo) = (bar)
252
#endif
253
254
#if defined(XP_WIN)
255
void
256
vprintf_stderr(const char* aFmt, va_list aArgs)
257
{
258
  if (IsDebuggerPresent()) {
259
    int lengthNeeded = _vscprintf(aFmt, aArgs);
260
    if (lengthNeeded) {
261
      lengthNeeded++;
262
      auto buf = MakeUnique<char[]>(lengthNeeded);
263
      if (buf) {
264
        va_list argsCpy;
265
        VARARGS_ASSIGN(argsCpy, aArgs);
266
        vsnprintf(buf.get(), lengthNeeded, aFmt, argsCpy);
267
        buf[lengthNeeded - 1] = '\0';
268
        va_end(argsCpy);
269
        OutputDebugStringA(buf.get());
270
      }
271
    }
272
  }
273
274
  FILE* fp = _fdopen(_dup(2), "a");
275
  if (!fp) {
276
    return;
277
  }
278
279
  vfprintf(fp, aFmt, aArgs);
280
281
  fclose(fp);
282
}
283
284
#elif defined(ANDROID)
285
void
286
vprintf_stderr(const char* aFmt, va_list aArgs)
287
{
288
  __android_log_vprint(ANDROID_LOG_INFO, "Gecko", aFmt, aArgs);
289
}
290
#else
291
void
292
vprintf_stderr(const char* aFmt, va_list aArgs)
293
0
{
294
0
  vfprintf(stderr, aFmt, aArgs);
295
0
}
296
#endif
297
298
void
299
printf_stderr(const char* aFmt, ...)
300
0
{
301
0
  va_list args;
302
0
  va_start(args, aFmt);
303
0
  vprintf_stderr(aFmt, args);
304
0
  va_end(args);
305
0
}
306
307
void
308
fprintf_stderr(FILE* aFile, const char* aFmt, ...)
309
0
{
310
0
  va_list args;
311
0
  va_start(args, aFmt);
312
0
  if (aFile == stderr) {
313
0
    vprintf_stderr(aFmt, args);
314
0
  } else {
315
0
    vfprintf(aFile, aFmt, args);
316
0
  }
317
0
  va_end(args);
318
0
}