1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/glue/nsID.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,116 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "nsID.h" 1.10 +#include "prprf.h" 1.11 +#include "nsMemory.h" 1.12 + 1.13 +/** 1.14 + * Multiplies the_int_var with 16 (0x10) and adds the value of the 1.15 + * hexadecimal digit the_char. If it fails it returns false from 1.16 + * the function it's used in. 1.17 + */ 1.18 + 1.19 +#define ADD_HEX_CHAR_TO_INT_OR_RETURN_FALSE(the_char, the_int_var) \ 1.20 + the_int_var = (the_int_var << 4) + the_char; \ 1.21 + if(the_char >= '0' && the_char <= '9') the_int_var -= '0'; \ 1.22 + else if(the_char >= 'a' && the_char <= 'f') the_int_var -= 'a'-10; \ 1.23 + else if(the_char >= 'A' && the_char <= 'F') the_int_var -= 'A'-10; \ 1.24 + else return false 1.25 + 1.26 + 1.27 +/** 1.28 + * Parses number_of_chars characters from the char_pointer pointer and 1.29 + * puts the number in the dest_variable. The pointer is moved to point 1.30 + * at the first character after the parsed ones. If it fails it returns 1.31 + * false from the function the macro is used in. 1.32 + */ 1.33 + 1.34 +#define PARSE_CHARS_TO_NUM(char_pointer, dest_variable, number_of_chars) \ 1.35 + do { int32_t _i=number_of_chars; \ 1.36 + dest_variable = 0; \ 1.37 + while(_i) { \ 1.38 + ADD_HEX_CHAR_TO_INT_OR_RETURN_FALSE(*char_pointer, dest_variable); \ 1.39 + char_pointer++; \ 1.40 + _i--; \ 1.41 + } } while(0) 1.42 + 1.43 + 1.44 +/** 1.45 + * Parses a hyphen from the char_pointer string. If there is no hyphen there 1.46 + * the function returns false from the function it's used in. The 1.47 + * char_pointer is advanced one step. 1.48 + */ 1.49 + 1.50 + #define PARSE_HYPHEN(char_pointer) if(*(char_pointer++) != '-') return false 1.51 + 1.52 +/* 1.53 + * Turns a {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} string into 1.54 + * an nsID. It can also handle the old format without the { and }. 1.55 + */ 1.56 + 1.57 +bool nsID::Parse(const char *aIDStr) 1.58 +{ 1.59 + /* Optimized for speed */ 1.60 + if(!aIDStr) { 1.61 + return false; 1.62 + } 1.63 + 1.64 + bool expectFormat1 = (aIDStr[0] == '{'); 1.65 + if(expectFormat1) aIDStr++; 1.66 + 1.67 + PARSE_CHARS_TO_NUM(aIDStr, m0, 8); 1.68 + PARSE_HYPHEN(aIDStr); 1.69 + PARSE_CHARS_TO_NUM(aIDStr, m1, 4); 1.70 + PARSE_HYPHEN(aIDStr); 1.71 + PARSE_CHARS_TO_NUM(aIDStr, m2, 4); 1.72 + PARSE_HYPHEN(aIDStr); 1.73 + int i; 1.74 + for(i=0; i<2; i++) 1.75 + PARSE_CHARS_TO_NUM(aIDStr, m3[i], 2); 1.76 + PARSE_HYPHEN(aIDStr); 1.77 + while(i < 8) { 1.78 + PARSE_CHARS_TO_NUM(aIDStr, m3[i], 2); 1.79 + i++; 1.80 + } 1.81 + 1.82 + return expectFormat1 ? *aIDStr == '}' : true; 1.83 +} 1.84 + 1.85 +#ifndef XPCOM_GLUE_AVOID_NSPR 1.86 + 1.87 +static const char gIDFormat[] = 1.88 + "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}"; 1.89 + 1.90 +/* 1.91 + * Returns an allocated string in {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} 1.92 + * format. The string is allocated with NS_Alloc and should be freed by 1.93 + * the caller. 1.94 + */ 1.95 + 1.96 +char *nsID::ToString() const 1.97 +{ 1.98 + char *res = (char*)NS_Alloc(NSID_LENGTH); 1.99 + 1.100 + if (res != nullptr) { 1.101 + PR_snprintf(res, NSID_LENGTH, gIDFormat, 1.102 + m0, (uint32_t) m1, (uint32_t) m2, 1.103 + (uint32_t) m3[0], (uint32_t) m3[1], (uint32_t) m3[2], 1.104 + (uint32_t) m3[3], (uint32_t) m3[4], (uint32_t) m3[5], 1.105 + (uint32_t) m3[6], (uint32_t) m3[7]); 1.106 + } 1.107 + return res; 1.108 +} 1.109 + 1.110 +void nsID::ToProvidedString(char (&dest)[NSID_LENGTH]) const 1.111 +{ 1.112 + PR_snprintf(dest, NSID_LENGTH, gIDFormat, 1.113 + m0, (uint32_t) m1, (uint32_t) m2, 1.114 + (uint32_t) m3[0], (uint32_t) m3[1], (uint32_t) m3[2], 1.115 + (uint32_t) m3[3], (uint32_t) m3[4], (uint32_t) m3[5], 1.116 + (uint32_t) m3[6], (uint32_t) m3[7]); 1.117 +} 1.118 + 1.119 +#endif // XPCOM_GLUE_AVOID_NSPR