2014-06-30 19:39:45 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2014-04-15 11:24:13 +04:00
|
|
|
|
1998-03-28 05:44:41 +03:00
|
|
|
#include "nsID.h"
|
2009-03-25 16:19:08 +03:00
|
|
|
#include "nsMemory.h"
|
2016-08-15 09:43:21 +03:00
|
|
|
#include "mozilla/Sprintf.h"
|
1998-03-28 05:44:41 +03:00
|
|
|
|
2015-07-31 23:50:08 +03:00
|
|
|
void nsID::Clear()
|
|
|
|
{
|
|
|
|
m0 = 0;
|
|
|
|
m1 = 0;
|
|
|
|
m2 = 0;
|
2016-11-16 15:24:21 +03:00
|
|
|
memset(m3, 0, sizeof(m3));
|
2015-07-31 23:50:08 +03:00
|
|
|
}
|
|
|
|
|
2005-08-09 04:20:28 +04:00
|
|
|
/**
|
|
|
|
* Multiplies the_int_var with 16 (0x10) and adds the value of the
|
2011-10-17 18:59:28 +04:00
|
|
|
* hexadecimal digit the_char. If it fails it returns false from
|
2005-08-09 04:20:28 +04:00
|
|
|
* the function it's used in.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define ADD_HEX_CHAR_TO_INT_OR_RETURN_FALSE(the_char, the_int_var) \
|
|
|
|
the_int_var = (the_int_var << 4) + the_char; \
|
|
|
|
if(the_char >= '0' && the_char <= '9') the_int_var -= '0'; \
|
|
|
|
else if(the_char >= 'a' && the_char <= 'f') the_int_var -= 'a'-10; \
|
|
|
|
else if(the_char >= 'A' && the_char <= 'F') the_int_var -= 'A'-10; \
|
2011-10-17 18:59:28 +04:00
|
|
|
else return false
|
2005-08-09 04:20:28 +04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses number_of_chars characters from the char_pointer pointer and
|
|
|
|
* puts the number in the dest_variable. The pointer is moved to point
|
|
|
|
* at the first character after the parsed ones. If it fails it returns
|
2011-10-17 18:59:28 +04:00
|
|
|
* false from the function the macro is used in.
|
2005-08-09 04:20:28 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define PARSE_CHARS_TO_NUM(char_pointer, dest_variable, number_of_chars) \
|
2012-08-22 19:56:38 +04:00
|
|
|
do { int32_t _i=number_of_chars; \
|
2005-08-09 04:20:28 +04:00
|
|
|
dest_variable = 0; \
|
|
|
|
while(_i) { \
|
|
|
|
ADD_HEX_CHAR_TO_INT_OR_RETURN_FALSE(*char_pointer, dest_variable); \
|
|
|
|
char_pointer++; \
|
|
|
|
_i--; \
|
|
|
|
} } while(0)
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses a hyphen from the char_pointer string. If there is no hyphen there
|
2011-10-17 18:59:28 +04:00
|
|
|
* the function returns false from the function it's used in. The
|
2005-08-09 04:20:28 +04:00
|
|
|
* char_pointer is advanced one step.
|
|
|
|
*/
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
#define PARSE_HYPHEN(char_pointer) if (*(char_pointer++) != '-') return false
|
2014-04-15 11:24:13 +04:00
|
|
|
|
|
|
|
/*
|
1998-03-28 05:44:41 +03:00
|
|
|
* Turns a {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} string into
|
2005-08-09 04:20:28 +04:00
|
|
|
* an nsID. It can also handle the old format without the { and }.
|
1998-03-28 05:44:41 +03:00
|
|
|
*/
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
bool
|
|
|
|
nsID::Parse(const char* aIDStr)
|
1998-05-23 01:56:30 +04:00
|
|
|
{
|
2005-08-09 04:20:28 +04:00
|
|
|
/* Optimized for speed */
|
2014-06-27 05:35:39 +04:00
|
|
|
if (!aIDStr) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-08-09 04:20:28 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool expectFormat1 = (aIDStr[0] == '{');
|
2014-06-27 05:35:39 +04:00
|
|
|
if (expectFormat1) {
|
|
|
|
++aIDStr;
|
|
|
|
}
|
2005-08-09 04:20:28 +04:00
|
|
|
|
|
|
|
PARSE_CHARS_TO_NUM(aIDStr, m0, 8);
|
|
|
|
PARSE_HYPHEN(aIDStr);
|
|
|
|
PARSE_CHARS_TO_NUM(aIDStr, m1, 4);
|
|
|
|
PARSE_HYPHEN(aIDStr);
|
|
|
|
PARSE_CHARS_TO_NUM(aIDStr, m2, 4);
|
|
|
|
PARSE_HYPHEN(aIDStr);
|
2005-08-09 04:20:29 +04:00
|
|
|
int i;
|
2014-06-27 05:35:39 +04:00
|
|
|
for (i = 0; i < 2; ++i) {
|
2005-08-09 04:20:28 +04:00
|
|
|
PARSE_CHARS_TO_NUM(aIDStr, m3[i], 2);
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2005-08-09 04:20:28 +04:00
|
|
|
PARSE_HYPHEN(aIDStr);
|
2014-06-27 05:35:39 +04:00
|
|
|
while (i < 8) {
|
2005-08-09 04:20:28 +04:00
|
|
|
PARSE_CHARS_TO_NUM(aIDStr, m3[i], 2);
|
|
|
|
i++;
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
2014-04-15 11:24:13 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return expectFormat1 ? *aIDStr == '}' : true;
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
|
2008-02-07 22:49:18 +03:00
|
|
|
#ifndef XPCOM_GLUE_AVOID_NSPR
|
|
|
|
|
2014-04-15 11:24:13 +04:00
|
|
|
static const char gIDFormat[] =
|
|
|
|
"{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}";
|
|
|
|
|
1998-03-28 05:44:41 +03:00
|
|
|
/*
|
|
|
|
* Returns an allocated string in {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
|
2009-03-25 16:19:08 +03:00
|
|
|
* format. The string is allocated with NS_Alloc and should be freed by
|
2005-08-09 04:20:28 +04:00
|
|
|
* the caller.
|
1998-03-28 05:44:41 +03:00
|
|
|
*/
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
char*
|
|
|
|
nsID::ToString() const
|
1998-03-28 05:44:41 +03:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
char* res = (char*)NS_Alloc(NSID_LENGTH);
|
1998-03-28 05:44:41 +03:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
if (res) {
|
2015-08-23 03:57:52 +03:00
|
|
|
snprintf(res, NSID_LENGTH, gIDFormat,
|
|
|
|
m0, (uint32_t)m1, (uint32_t)m2,
|
|
|
|
(uint32_t)m3[0], (uint32_t)m3[1], (uint32_t)m3[2],
|
|
|
|
(uint32_t)m3[3], (uint32_t)m3[4], (uint32_t)m3[5],
|
|
|
|
(uint32_t)m3[6], (uint32_t)m3[7]);
|
1998-03-28 05:44:41 +03:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
void
|
|
|
|
nsID::ToProvidedString(char (&aDest)[NSID_LENGTH]) const
|
2008-01-12 07:30:42 +03:00
|
|
|
{
|
2016-08-15 09:44:00 +03:00
|
|
|
SprintfLiteral(aDest, gIDFormat,
|
|
|
|
m0, (uint32_t)m1, (uint32_t)m2,
|
|
|
|
(uint32_t)m3[0], (uint32_t)m3[1], (uint32_t)m3[2],
|
|
|
|
(uint32_t)m3[3], (uint32_t)m3[4], (uint32_t)m3[5],
|
|
|
|
(uint32_t)m3[6], (uint32_t)m3[7]);
|
2008-01-12 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
2008-02-07 22:49:18 +03:00
|
|
|
#endif // XPCOM_GLUE_AVOID_NSPR
|