Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/xpcom/base/nsID.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 "nsID.h"
8
#include "nsMemory.h"
9
#include "mozilla/Sprintf.h"
10
11
void nsID::Clear()
12
11.3M
{
13
11.3M
  m0 = 0;
14
11.3M
  m1 = 0;
15
11.3M
  m2 = 0;
16
11.3M
  memset(m3, 0, sizeof(m3));
17
11.3M
}
18
19
/**
20
 * Multiplies the_int_var with 16 (0x10) and adds the value of the
21
 * hexadecimal digit the_char. If it fails it returns false from
22
 * the function it's used in.
23
 */
24
25
#define ADD_HEX_CHAR_TO_INT_OR_RETURN_FALSE(the_char, the_int_var) \
26
21.0k
    the_int_var = (the_int_var << 4) + the_char; \
27
21.0k
    if(the_char >= '0' && the_char <= '9') the_int_var -= '0'; \
28
21.0k
    else if(the_char >= 'a' && the_char <= 'f') the_int_var -= 'a'-10; \
29
7.61k
    else if(the_char >= 'A' && the_char <= 'F') the_int_var -= 'A'-10; \
30
600
    else return false
31
32
33
/**
34
 * Parses number_of_chars characters from the char_pointer pointer and
35
 * puts the number in the dest_variable. The pointer is moved to point
36
 * at the first character after the parsed ones. If it fails it returns
37
 * false from the function the macro is used in.
38
 */
39
40
#define PARSE_CHARS_TO_NUM(char_pointer, dest_variable, number_of_chars) \
41
7.23k
  do { int32_t _i=number_of_chars; \
42
7.23k
  dest_variable = 0; \
43
28.2k
  while(_i) { \
44
21.0k
    ADD_HEX_CHAR_TO_INT_OR_RETURN_FALSE(*char_pointer, dest_variable); \
45
21.0k
    char_pointer++; \
46
21.0k
    _i--; \
47
21.0k
  } } while(0)
48
49
50
/**
51
 * Parses a hyphen from the char_pointer string. If there is no hyphen there
52
 * the function returns false from the function it's used in. The
53
 * char_pointer is advanced one step.
54
 */
55
56
2.63k
#define PARSE_HYPHEN(char_pointer) if (*(char_pointer++) != '-') return false
57
58
/*
59
 * Turns a {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} string into
60
 * an nsID. It can also handle the old format without the { and }.
61
 */
62
63
bool
64
nsID::Parse(const char* aIDStr)
65
658
{
66
658
  /* Optimized for speed */
67
658
  if (!aIDStr) {
68
0
    return false;
69
0
  }
70
658
71
658
  bool expectFormat1 = (aIDStr[0] == '{');
72
658
  if (expectFormat1) {
73
658
    ++aIDStr;
74
658
  }
75
658
76
658
  PARSE_CHARS_TO_NUM(aIDStr, m0, 8);
77
658
  PARSE_HYPHEN(aIDStr);
78
658
  PARSE_CHARS_TO_NUM(aIDStr, m1, 4);
79
658
  PARSE_HYPHEN(aIDStr);
80
658
  PARSE_CHARS_TO_NUM(aIDStr, m2, 4);
81
658
  PARSE_HYPHEN(aIDStr);
82
658
  int i;
83
1.97k
  for (i = 0; i < 2; ++i) {
84
1.31k
    PARSE_CHARS_TO_NUM(aIDStr, m3[i], 2);
85
1.31k
  }
86
658
  PARSE_HYPHEN(aIDStr);
87
4.60k
  while (i < 8) {
88
3.94k
    PARSE_CHARS_TO_NUM(aIDStr, m3[i], 2);
89
3.94k
    i++;
90
3.94k
  }
91
658
92
658
  return expectFormat1 ? *aIDStr == '}' : true;
93
658
}
94
95
#ifndef XPCOM_GLUE_AVOID_NSPR
96
97
static const char gIDFormat[] =
98
  "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}";
99
100
/*
101
 * Returns an allocated string in {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
102
 * format. The string is allocated with moz_xmalloc and should be freed by
103
 * the caller.
104
 */
105
106
char*
107
nsID::ToString() const
108
2
{
109
2
  char* res = (char*)moz_xmalloc(NSID_LENGTH);
110
2
  snprintf(res, NSID_LENGTH, gIDFormat,
111
2
           m0, (uint32_t)m1, (uint32_t)m2,
112
2
           (uint32_t)m3[0], (uint32_t)m3[1], (uint32_t)m3[2],
113
2
           (uint32_t)m3[3], (uint32_t)m3[4], (uint32_t)m3[5],
114
2
           (uint32_t)m3[6], (uint32_t)m3[7]);
115
2
  return res;
116
2
}
117
118
void
119
nsID::ToProvidedString(char (&aDest)[NSID_LENGTH]) const
120
12
{
121
12
  SprintfLiteral(aDest, gIDFormat,
122
12
                 m0, (uint32_t)m1, (uint32_t)m2,
123
12
                 (uint32_t)m3[0], (uint32_t)m3[1], (uint32_t)m3[2],
124
12
                 (uint32_t)m3[3], (uint32_t)m3[4], (uint32_t)m3[5],
125
12
                 (uint32_t)m3[6], (uint32_t)m3[7]);
126
12
}
127
128
#endif // XPCOM_GLUE_AVOID_NSPR
129
130
nsID*
131
nsID::Clone() const
132
38
{
133
38
  auto id = static_cast<nsID*>(moz_xmalloc(sizeof(nsID)));
134
38
  *id = *this;
135
38
  return id;
136
38
}