xpcom/glue/nsID.cpp

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:d1e9d069d593
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 #include "nsID.h"
7 #include "prprf.h"
8 #include "nsMemory.h"
9
10 /**
11 * Multiplies the_int_var with 16 (0x10) and adds the value of the
12 * hexadecimal digit the_char. If it fails it returns false from
13 * the function it's used in.
14 */
15
16 #define ADD_HEX_CHAR_TO_INT_OR_RETURN_FALSE(the_char, the_int_var) \
17 the_int_var = (the_int_var << 4) + the_char; \
18 if(the_char >= '0' && the_char <= '9') the_int_var -= '0'; \
19 else if(the_char >= 'a' && the_char <= 'f') the_int_var -= 'a'-10; \
20 else if(the_char >= 'A' && the_char <= 'F') the_int_var -= 'A'-10; \
21 else return false
22
23
24 /**
25 * Parses number_of_chars characters from the char_pointer pointer and
26 * puts the number in the dest_variable. The pointer is moved to point
27 * at the first character after the parsed ones. If it fails it returns
28 * false from the function the macro is used in.
29 */
30
31 #define PARSE_CHARS_TO_NUM(char_pointer, dest_variable, number_of_chars) \
32 do { int32_t _i=number_of_chars; \
33 dest_variable = 0; \
34 while(_i) { \
35 ADD_HEX_CHAR_TO_INT_OR_RETURN_FALSE(*char_pointer, dest_variable); \
36 char_pointer++; \
37 _i--; \
38 } } while(0)
39
40
41 /**
42 * Parses a hyphen from the char_pointer string. If there is no hyphen there
43 * the function returns false from the function it's used in. The
44 * char_pointer is advanced one step.
45 */
46
47 #define PARSE_HYPHEN(char_pointer) if(*(char_pointer++) != '-') return false
48
49 /*
50 * Turns a {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} string into
51 * an nsID. It can also handle the old format without the { and }.
52 */
53
54 bool nsID::Parse(const char *aIDStr)
55 {
56 /* Optimized for speed */
57 if(!aIDStr) {
58 return false;
59 }
60
61 bool expectFormat1 = (aIDStr[0] == '{');
62 if(expectFormat1) aIDStr++;
63
64 PARSE_CHARS_TO_NUM(aIDStr, m0, 8);
65 PARSE_HYPHEN(aIDStr);
66 PARSE_CHARS_TO_NUM(aIDStr, m1, 4);
67 PARSE_HYPHEN(aIDStr);
68 PARSE_CHARS_TO_NUM(aIDStr, m2, 4);
69 PARSE_HYPHEN(aIDStr);
70 int i;
71 for(i=0; i<2; i++)
72 PARSE_CHARS_TO_NUM(aIDStr, m3[i], 2);
73 PARSE_HYPHEN(aIDStr);
74 while(i < 8) {
75 PARSE_CHARS_TO_NUM(aIDStr, m3[i], 2);
76 i++;
77 }
78
79 return expectFormat1 ? *aIDStr == '}' : true;
80 }
81
82 #ifndef XPCOM_GLUE_AVOID_NSPR
83
84 static const char gIDFormat[] =
85 "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}";
86
87 /*
88 * Returns an allocated string in {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
89 * format. The string is allocated with NS_Alloc and should be freed by
90 * the caller.
91 */
92
93 char *nsID::ToString() const
94 {
95 char *res = (char*)NS_Alloc(NSID_LENGTH);
96
97 if (res != nullptr) {
98 PR_snprintf(res, NSID_LENGTH, gIDFormat,
99 m0, (uint32_t) m1, (uint32_t) m2,
100 (uint32_t) m3[0], (uint32_t) m3[1], (uint32_t) m3[2],
101 (uint32_t) m3[3], (uint32_t) m3[4], (uint32_t) m3[5],
102 (uint32_t) m3[6], (uint32_t) m3[7]);
103 }
104 return res;
105 }
106
107 void nsID::ToProvidedString(char (&dest)[NSID_LENGTH]) const
108 {
109 PR_snprintf(dest, NSID_LENGTH, gIDFormat,
110 m0, (uint32_t) m1, (uint32_t) m2,
111 (uint32_t) m3[0], (uint32_t) m3[1], (uint32_t) m3[2],
112 (uint32_t) m3[3], (uint32_t) m3[4], (uint32_t) m3[5],
113 (uint32_t) m3[6], (uint32_t) m3[7]);
114 }
115
116 #endif // XPCOM_GLUE_AVOID_NSPR

mercurial