michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsID.h" michael@0: #include "prprf.h" michael@0: #include "nsMemory.h" michael@0: michael@0: /** michael@0: * Multiplies the_int_var with 16 (0x10) and adds the value of the michael@0: * hexadecimal digit the_char. If it fails it returns false from michael@0: * the function it's used in. michael@0: */ michael@0: michael@0: #define ADD_HEX_CHAR_TO_INT_OR_RETURN_FALSE(the_char, the_int_var) \ michael@0: the_int_var = (the_int_var << 4) + the_char; \ michael@0: if(the_char >= '0' && the_char <= '9') the_int_var -= '0'; \ michael@0: else if(the_char >= 'a' && the_char <= 'f') the_int_var -= 'a'-10; \ michael@0: else if(the_char >= 'A' && the_char <= 'F') the_int_var -= 'A'-10; \ michael@0: else return false michael@0: michael@0: michael@0: /** michael@0: * Parses number_of_chars characters from the char_pointer pointer and michael@0: * puts the number in the dest_variable. The pointer is moved to point michael@0: * at the first character after the parsed ones. If it fails it returns michael@0: * false from the function the macro is used in. michael@0: */ michael@0: michael@0: #define PARSE_CHARS_TO_NUM(char_pointer, dest_variable, number_of_chars) \ michael@0: do { int32_t _i=number_of_chars; \ michael@0: dest_variable = 0; \ michael@0: while(_i) { \ michael@0: ADD_HEX_CHAR_TO_INT_OR_RETURN_FALSE(*char_pointer, dest_variable); \ michael@0: char_pointer++; \ michael@0: _i--; \ michael@0: } } while(0) michael@0: michael@0: michael@0: /** michael@0: * Parses a hyphen from the char_pointer string. If there is no hyphen there michael@0: * the function returns false from the function it's used in. The michael@0: * char_pointer is advanced one step. michael@0: */ michael@0: michael@0: #define PARSE_HYPHEN(char_pointer) if(*(char_pointer++) != '-') return false michael@0: michael@0: /* michael@0: * Turns a {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} string into michael@0: * an nsID. It can also handle the old format without the { and }. michael@0: */ michael@0: michael@0: bool nsID::Parse(const char *aIDStr) michael@0: { michael@0: /* Optimized for speed */ michael@0: if(!aIDStr) { michael@0: return false; michael@0: } michael@0: michael@0: bool expectFormat1 = (aIDStr[0] == '{'); michael@0: if(expectFormat1) aIDStr++; michael@0: michael@0: PARSE_CHARS_TO_NUM(aIDStr, m0, 8); michael@0: PARSE_HYPHEN(aIDStr); michael@0: PARSE_CHARS_TO_NUM(aIDStr, m1, 4); michael@0: PARSE_HYPHEN(aIDStr); michael@0: PARSE_CHARS_TO_NUM(aIDStr, m2, 4); michael@0: PARSE_HYPHEN(aIDStr); michael@0: int i; michael@0: for(i=0; i<2; i++) michael@0: PARSE_CHARS_TO_NUM(aIDStr, m3[i], 2); michael@0: PARSE_HYPHEN(aIDStr); michael@0: while(i < 8) { michael@0: PARSE_CHARS_TO_NUM(aIDStr, m3[i], 2); michael@0: i++; michael@0: } michael@0: michael@0: return expectFormat1 ? *aIDStr == '}' : true; michael@0: } michael@0: michael@0: #ifndef XPCOM_GLUE_AVOID_NSPR michael@0: michael@0: static const char gIDFormat[] = michael@0: "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}"; michael@0: michael@0: /* michael@0: * Returns an allocated string in {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} michael@0: * format. The string is allocated with NS_Alloc and should be freed by michael@0: * the caller. michael@0: */ michael@0: michael@0: char *nsID::ToString() const michael@0: { michael@0: char *res = (char*)NS_Alloc(NSID_LENGTH); michael@0: michael@0: if (res != nullptr) { michael@0: PR_snprintf(res, NSID_LENGTH, gIDFormat, michael@0: m0, (uint32_t) m1, (uint32_t) m2, michael@0: (uint32_t) m3[0], (uint32_t) m3[1], (uint32_t) m3[2], michael@0: (uint32_t) m3[3], (uint32_t) m3[4], (uint32_t) m3[5], michael@0: (uint32_t) m3[6], (uint32_t) m3[7]); michael@0: } michael@0: return res; michael@0: } michael@0: michael@0: void nsID::ToProvidedString(char (&dest)[NSID_LENGTH]) const michael@0: { michael@0: PR_snprintf(dest, NSID_LENGTH, gIDFormat, michael@0: m0, (uint32_t) m1, (uint32_t) m2, michael@0: (uint32_t) m3[0], (uint32_t) m3[1], (uint32_t) m3[2], michael@0: (uint32_t) m3[3], (uint32_t) m3[4], (uint32_t) m3[5], michael@0: (uint32_t) m3[6], (uint32_t) m3[7]); michael@0: } michael@0: michael@0: #endif // XPCOM_GLUE_AVOID_NSPR