Mercurial > foo_out_sdl
comparison foosdk/sdk/pfc/guid.cpp @ 1:20d02a178406 default tip
*: check in everything else
yay
| author | Paper <paper@tflc.us> |
|---|---|
| date | Mon, 05 Jan 2026 02:15:46 -0500 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 0:e9bb126753e7 | 1:20d02a178406 |
|---|---|
| 1 #include "pfc-lite.h" | |
| 2 #include "guid.h" | |
| 3 #include "debug.h" // pfc::crash() | |
| 4 #include "string_base.h" | |
| 5 | |
| 6 #ifdef _WIN32 | |
| 7 #include <Objbase.h> | |
| 8 #else | |
| 9 #include "nix-objects.h" | |
| 10 #endif | |
| 11 | |
| 12 /* | |
| 13 6B29FC40-CA47-1067-B31D-00DD010662DA | |
| 14 . | |
| 15 typedef struct _GUID { // size is 16 | |
| 16 DWORD Data1; | |
| 17 WORD Data2; | |
| 18 WORD Data3; | |
| 19 BYTE Data4[8]; | |
| 20 } GUID; | |
| 21 | |
| 22 // {B296CF59-4D51-466f-8E0B-E57D3F91D908} | |
| 23 static const GUID <<name>> = | |
| 24 { 0xb296cf59, 0x4d51, 0x466f, { 0x8e, 0xb, 0xe5, 0x7d, 0x3f, 0x91, 0xd9, 0x8 } }; | |
| 25 | |
| 26 */ | |
| 27 namespace { | |
| 28 class _GUID_from_text | |
| 29 { | |
| 30 unsigned read_hex(char c); | |
| 31 unsigned read_byte(const char * ptr); | |
| 32 unsigned read_word(const char * ptr); | |
| 33 unsigned read_dword(const char * ptr); | |
| 34 void read_bytes(unsigned char * out,unsigned num,const char * ptr); | |
| 35 | |
| 36 public: | |
| 37 GUID m_val = pfc::guid_null; | |
| 38 _GUID_from_text(const char * text); | |
| 39 }; | |
| 40 | |
| 41 unsigned _GUID_from_text::read_hex(char c) | |
| 42 { | |
| 43 if (c>='0' && c<='9') return (unsigned)c - '0'; | |
| 44 else if (c>='a' && c<='f') return 0xa + (unsigned)c - 'a'; | |
| 45 else if (c>='A' && c<='F') return 0xa + (unsigned)c - 'A'; | |
| 46 else return 0; | |
| 47 } | |
| 48 | |
| 49 unsigned _GUID_from_text::read_byte(const char * ptr) | |
| 50 { | |
| 51 return (read_hex(ptr[0])<<4) | read_hex(ptr[1]); | |
| 52 } | |
| 53 unsigned _GUID_from_text::read_word(const char * ptr) | |
| 54 { | |
| 55 return (read_byte(ptr)<<8) | read_byte(ptr+2); | |
| 56 } | |
| 57 | |
| 58 unsigned _GUID_from_text::read_dword(const char * ptr) | |
| 59 { | |
| 60 return (read_word(ptr)<<16) | read_word(ptr+4); | |
| 61 } | |
| 62 | |
| 63 void _GUID_from_text::read_bytes(uint8_t * out,unsigned num,const char * ptr) | |
| 64 { | |
| 65 for(;num;num--) | |
| 66 { | |
| 67 *out = read_byte(ptr); | |
| 68 out++;ptr+=2; | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 | |
| 73 _GUID_from_text::_GUID_from_text(const char * text) | |
| 74 { | |
| 75 if (*text=='{') text++; | |
| 76 const char * max; | |
| 77 | |
| 78 { | |
| 79 const char * t = strchr(text,'}'); | |
| 80 if (t) max = t; | |
| 81 else max = text + strlen(text); | |
| 82 } | |
| 83 | |
| 84 | |
| 85 bool OK = false; | |
| 86 | |
| 87 do { | |
| 88 if (text+8>max) break; | |
| 89 m_val.Data1 = read_dword(text); | |
| 90 text += 8; | |
| 91 while(*text=='-') text++; | |
| 92 if (text+4>max) break; | |
| 93 m_val.Data2 = read_word(text); | |
| 94 text += 4; | |
| 95 while(*text=='-') text++; | |
| 96 if (text+4>max) break; | |
| 97 m_val.Data3 = read_word(text); | |
| 98 text += 4; | |
| 99 while(*text=='-') text++; | |
| 100 if (text+4>max) break; | |
| 101 read_bytes(m_val.Data4,2,text); | |
| 102 text += 4; | |
| 103 while(*text=='-') text++; | |
| 104 if (text+12>max) break; | |
| 105 read_bytes(m_val.Data4+2,6,text); | |
| 106 OK = true; | |
| 107 } while(false); | |
| 108 | |
| 109 if (!OK) { | |
| 110 m_val= pfc::guid_null; | |
| 111 } | |
| 112 } | |
| 113 } | |
| 114 | |
| 115 namespace pfc { | |
| 116 | |
| 117 GUID GUID_from_text(const char * text) { | |
| 118 return _GUID_from_text( text ).m_val; | |
| 119 } | |
| 120 | |
| 121 static inline char print_hex_digit(unsigned val) | |
| 122 { | |
| 123 static constexpr char table[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; | |
| 124 PFC_ASSERT((val & ~0xF) == 0); | |
| 125 return table[val]; | |
| 126 } | |
| 127 | |
| 128 static void print_hex(unsigned val,char * &out,unsigned bytes) | |
| 129 { | |
| 130 unsigned n; | |
| 131 for(n=0;n<bytes;n++) | |
| 132 { | |
| 133 unsigned char c = (unsigned char)((val >> ((bytes - 1 - n) << 3)) & 0xFF); | |
| 134 *(out++) = print_hex_digit( c >> 4 ); | |
| 135 *(out++) = print_hex_digit( c & 0xF ); | |
| 136 } | |
| 137 *out = 0; | |
| 138 } | |
| 139 | |
| 140 | |
| 141 pfc::string8 print_guid(const GUID & p_guid) | |
| 142 { | |
| 143 char data[64]; | |
| 144 char * out = data; | |
| 145 print_hex(p_guid.Data1,out,4); | |
| 146 *(out++) = '-'; | |
| 147 print_hex(p_guid.Data2,out,2); | |
| 148 *(out++) = '-'; | |
| 149 print_hex(p_guid.Data3,out,2); | |
| 150 *(out++) = '-'; | |
| 151 print_hex(p_guid.Data4[0],out,1); | |
| 152 print_hex(p_guid.Data4[1],out,1); | |
| 153 *(out++) = '-'; | |
| 154 print_hex(p_guid.Data4[2],out,1); | |
| 155 print_hex(p_guid.Data4[3],out,1); | |
| 156 print_hex(p_guid.Data4[4],out,1); | |
| 157 print_hex(p_guid.Data4[5],out,1); | |
| 158 print_hex(p_guid.Data4[6],out,1); | |
| 159 print_hex(p_guid.Data4[7],out,1); | |
| 160 *out = 0; | |
| 161 return data; | |
| 162 } | |
| 163 | |
| 164 | |
| 165 void print_hex_raw(const void * buffer,unsigned bytes,char * p_out) | |
| 166 { | |
| 167 char * out = p_out; | |
| 168 const unsigned char * in = (const unsigned char *) buffer; | |
| 169 unsigned n; | |
| 170 for(n=0;n<bytes;n++) | |
| 171 print_hex(in[n],out,1); | |
| 172 } | |
| 173 | |
| 174 GUID createGUID() { | |
| 175 GUID out; | |
| 176 #ifdef _WIN32 | |
| 177 if (FAILED(CoCreateGuid( & out ) ) ) crash(); | |
| 178 #else | |
| 179 pfc::nixGetRandomData( &out, sizeof(out) ); | |
| 180 #endif | |
| 181 return out; | |
| 182 } | |
| 183 | |
| 184 } | |
| 185 | |
| 186 | |
| 187 | |
| 188 namespace pfc { | |
| 189 pfc::string8 format_guid_cpp(const GUID & guid) { | |
| 190 pfc::string8 s; | |
| 191 s << "{0x" << pfc::format_hex(guid.Data1,8) << ", 0x" << pfc::format_hex(guid.Data2, 4) << ", 0x" << pfc::format_hex(guid.Data3,4) << ", {0x" << pfc::format_hex(guid.Data4[0],2); | |
| 192 for(int n = 1; n < 8; ++n) { | |
| 193 s << ", 0x" << pfc::format_hex(guid.Data4[n],2); | |
| 194 } | |
| 195 s << "}}"; | |
| 196 return s; | |
| 197 } | |
| 198 | |
| 199 uint64_t halveGUID(const GUID & id) { | |
| 200 static_assert(sizeof(id) == 2 * sizeof(uint64_t), "sanity" ); | |
| 201 union { | |
| 202 GUID g; | |
| 203 uint64_t u[2]; | |
| 204 } u; | |
| 205 u.g = id; | |
| 206 return u.u[0] ^ u.u[1]; | |
| 207 } | |
| 208 } |
