2014-06-30 08:39:45 -07: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 04:12:37 -07: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 00:24:13 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsID.h"
|
2009-03-25 06:33:03 -07:00
|
|
|
#include "prprf.h"
|
2009-03-25 06:19:08 -07:00
|
|
|
#include "nsMemory.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Multiplies the_int_var with 16 (0x10) and adds the value of the
|
2011-10-17 07:59:28 -07:00
|
|
|
* hexadecimal digit the_char. If it fails it returns false from
|
2007-03-22 10:30:00 -07: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 07:59:28 -07:00
|
|
|
else return false
|
2007-03-22 10:30:00 -07: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 07:59:28 -07:00
|
|
|
* false from the function the macro is used in.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define PARSE_CHARS_TO_NUM(char_pointer, dest_variable, number_of_chars) \
|
2012-08-22 08:56:38 -07:00
|
|
|
do { int32_t _i=number_of_chars; \
|
2007-03-22 10:30:00 -07: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 07:59:28 -07:00
|
|
|
* the function returns false from the function it's used in. The
|
2007-03-22 10:30:00 -07:00
|
|
|
* char_pointer is advanced one step.
|
|
|
|
*/
|
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
#define PARSE_HYPHEN(char_pointer) if (*(char_pointer++) != '-') return false
|
2014-04-15 00:24:13 -07:00
|
|
|
|
|
|
|
/*
|
2007-03-22 10:30:00 -07:00
|
|
|
* Turns a {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} string into
|
|
|
|
* an nsID. It can also handle the old format without the { and }.
|
|
|
|
*/
|
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
bool
|
|
|
|
nsID::Parse(const char* aIDStr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
/* Optimized for speed */
|
2014-06-26 18:35:39 -07:00
|
|
|
if (!aIDStr) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool expectFormat1 = (aIDStr[0] == '{');
|
2014-06-26 18:35:39 -07:00
|
|
|
if (expectFormat1) {
|
|
|
|
++aIDStr;
|
|
|
|
}
|
2007-03-22 10:30:00 -07: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);
|
|
|
|
int i;
|
2014-06-26 18:35:39 -07:00
|
|
|
for (i = 0; i < 2; ++i) {
|
2007-03-22 10:30:00 -07:00
|
|
|
PARSE_CHARS_TO_NUM(aIDStr, m3[i], 2);
|
2014-06-26 18:35:39 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
PARSE_HYPHEN(aIDStr);
|
2014-06-26 18:35:39 -07:00
|
|
|
while (i < 8) {
|
2007-03-22 10:30:00 -07:00
|
|
|
PARSE_CHARS_TO_NUM(aIDStr, m3[i], 2);
|
|
|
|
i++;
|
|
|
|
}
|
2014-04-15 00:24:13 -07:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return expectFormat1 ? *aIDStr == '}' : true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-02-07 11:49:18 -08:00
|
|
|
#ifndef XPCOM_GLUE_AVOID_NSPR
|
|
|
|
|
2014-04-15 00:24:13 -07:00
|
|
|
static const char gIDFormat[] =
|
|
|
|
"{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}";
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
|
|
|
* Returns an allocated string in {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
|
2009-03-25 06:19:08 -07:00
|
|
|
* format. The string is allocated with NS_Alloc and should be freed by
|
2007-03-22 10:30:00 -07:00
|
|
|
* the caller.
|
|
|
|
*/
|
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
char*
|
|
|
|
nsID::ToString() const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
char* res = (char*)NS_Alloc(NSID_LENGTH);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
if (res) {
|
2008-01-11 20:30:42 -08:00
|
|
|
PR_snprintf(res, NSID_LENGTH, gIDFormat,
|
2014-06-26 18:35:39 -07:00
|
|
|
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]);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
void
|
|
|
|
nsID::ToProvidedString(char (&aDest)[NSID_LENGTH]) const
|
2008-01-11 20:30:42 -08:00
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
PR_snprintf(aDest, 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]);
|
2008-01-11 20:30:42 -08:00
|
|
|
}
|
|
|
|
|
2008-02-07 11:49:18 -08:00
|
|
|
#endif // XPCOM_GLUE_AVOID_NSPR
|