guid.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #include "guid.h"
  2. #include <library/cpp/yt/exception/exception.h>
  3. #include <util/random/random.h>
  4. #include <util/string/printf.h>
  5. namespace NYT {
  6. ////////////////////////////////////////////////////////////////////////////////
  7. namespace {
  8. const ui8 HexDigits1[16] = {
  9. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  10. };
  11. const ui16 HexDigits2[256] = {
  12. 0x3030, 0x3130, 0x3230, 0x3330, 0x3430, 0x3530, 0x3630, 0x3730, 0x3830, 0x3930, 0x6130, 0x6230, 0x6330, 0x6430, 0x6530, 0x6630,
  13. 0x3031, 0x3131, 0x3231, 0x3331, 0x3431, 0x3531, 0x3631, 0x3731, 0x3831, 0x3931, 0x6131, 0x6231, 0x6331, 0x6431, 0x6531, 0x6631,
  14. 0x3032, 0x3132, 0x3232, 0x3332, 0x3432, 0x3532, 0x3632, 0x3732, 0x3832, 0x3932, 0x6132, 0x6232, 0x6332, 0x6432, 0x6532, 0x6632,
  15. 0x3033, 0x3133, 0x3233, 0x3333, 0x3433, 0x3533, 0x3633, 0x3733, 0x3833, 0x3933, 0x6133, 0x6233, 0x6333, 0x6433, 0x6533, 0x6633,
  16. 0x3034, 0x3134, 0x3234, 0x3334, 0x3434, 0x3534, 0x3634, 0x3734, 0x3834, 0x3934, 0x6134, 0x6234, 0x6334, 0x6434, 0x6534, 0x6634,
  17. 0x3035, 0x3135, 0x3235, 0x3335, 0x3435, 0x3535, 0x3635, 0x3735, 0x3835, 0x3935, 0x6135, 0x6235, 0x6335, 0x6435, 0x6535, 0x6635,
  18. 0x3036, 0x3136, 0x3236, 0x3336, 0x3436, 0x3536, 0x3636, 0x3736, 0x3836, 0x3936, 0x6136, 0x6236, 0x6336, 0x6436, 0x6536, 0x6636,
  19. 0x3037, 0x3137, 0x3237, 0x3337, 0x3437, 0x3537, 0x3637, 0x3737, 0x3837, 0x3937, 0x6137, 0x6237, 0x6337, 0x6437, 0x6537, 0x6637,
  20. 0x3038, 0x3138, 0x3238, 0x3338, 0x3438, 0x3538, 0x3638, 0x3738, 0x3838, 0x3938, 0x6138, 0x6238, 0x6338, 0x6438, 0x6538, 0x6638,
  21. 0x3039, 0x3139, 0x3239, 0x3339, 0x3439, 0x3539, 0x3639, 0x3739, 0x3839, 0x3939, 0x6139, 0x6239, 0x6339, 0x6439, 0x6539, 0x6639,
  22. 0x3061, 0x3161, 0x3261, 0x3361, 0x3461, 0x3561, 0x3661, 0x3761, 0x3861, 0x3961, 0x6161, 0x6261, 0x6361, 0x6461, 0x6561, 0x6661,
  23. 0x3062, 0x3162, 0x3262, 0x3362, 0x3462, 0x3562, 0x3662, 0x3762, 0x3862, 0x3962, 0x6162, 0x6262, 0x6362, 0x6462, 0x6562, 0x6662,
  24. 0x3063, 0x3163, 0x3263, 0x3363, 0x3463, 0x3563, 0x3663, 0x3763, 0x3863, 0x3963, 0x6163, 0x6263, 0x6363, 0x6463, 0x6563, 0x6663,
  25. 0x3064, 0x3164, 0x3264, 0x3364, 0x3464, 0x3564, 0x3664, 0x3764, 0x3864, 0x3964, 0x6164, 0x6264, 0x6364, 0x6464, 0x6564, 0x6664,
  26. 0x3065, 0x3165, 0x3265, 0x3365, 0x3465, 0x3565, 0x3665, 0x3765, 0x3865, 0x3965, 0x6165, 0x6265, 0x6365, 0x6465, 0x6565, 0x6665,
  27. 0x3066, 0x3166, 0x3266, 0x3366, 0x3466, 0x3566, 0x3666, 0x3766, 0x3866, 0x3966, 0x6166, 0x6266, 0x6366, 0x6466, 0x6566, 0x6666
  28. };
  29. } // anonymous namespace
  30. ////////////////////////////////////////////////////////////////////////////////
  31. TGuid TGuid::Create()
  32. {
  33. return TGuid(RandomNumber<ui64>(), RandomNumber<ui64>());
  34. }
  35. TGuid TGuid::FromString(TStringBuf str)
  36. {
  37. TGuid guid;
  38. if (!FromString(str, &guid)) {
  39. throw TSimpleException(Sprintf("Error parsing GUID \"%s\"",
  40. TString(str).c_str()));
  41. }
  42. return guid;
  43. }
  44. bool TGuid::FromString(TStringBuf str, TGuid* result)
  45. {
  46. size_t partId = 3;
  47. ui64 partValue = 0;
  48. bool isEmptyPart = true;
  49. for (size_t i = 0; i != str.size(); ++i) {
  50. const char c = str[i];
  51. if (c == '-') {
  52. if (isEmptyPart || partId == 0) { // x-y--z, -x-y-z or x-y-z-m-...
  53. return false;
  54. }
  55. result->Parts32[partId] = static_cast<ui32>(partValue);
  56. --partId;
  57. partValue = 0;
  58. isEmptyPart = true;
  59. continue;
  60. }
  61. ui32 digit = 0;
  62. if ('0' <= c && c <= '9') {
  63. digit = c - '0';
  64. } else if ('a' <= c && c <= 'f') {
  65. digit = c - 'a' + 10;
  66. } else if ('A' <= c && c <= 'F') {
  67. digit = c - 'A' + 10;
  68. } else {
  69. return false; // non-hex character
  70. }
  71. partValue = partValue * 16 + digit;
  72. isEmptyPart = false;
  73. // overflow check
  74. if (partValue > Max<ui32>()) {
  75. return false;
  76. }
  77. }
  78. if (partId != 0 || isEmptyPart) { // x-y or x-y-z-
  79. return false;
  80. }
  81. result->Parts32[partId] = static_cast<ui32>(partValue);
  82. return true;
  83. }
  84. TGuid TGuid::FromStringHex32(TStringBuf str)
  85. {
  86. TGuid guid;
  87. if (!FromStringHex32(str, &guid)) {
  88. throw TSimpleException(Sprintf("Error parsing Hex32 GUID \"%s\"",
  89. TString(str).c_str()));
  90. }
  91. return guid;
  92. }
  93. bool TGuid::FromStringHex32(TStringBuf str, TGuid* result)
  94. {
  95. if (str.size() != 32) {
  96. return false;
  97. }
  98. bool ok = true;
  99. int i = 0;
  100. auto parseChar = [&] {
  101. const char c = str[i++];
  102. ui32 digit = 0;
  103. if ('0' <= c && c <= '9') {
  104. digit = c - '0';
  105. } else if ('a' <= c && c <= 'f') {
  106. digit = c - 'a' + 10;
  107. } else if ('A' <= c && c <= 'F') {
  108. digit = c - 'A' + 10;
  109. } else {
  110. ok = false;
  111. }
  112. return digit;
  113. };
  114. for (size_t j = 0; j < 16; ++j) {
  115. result->ReversedParts8[15 - j] = parseChar() * 16 + parseChar();
  116. }
  117. return ok;
  118. }
  119. char* WriteGuidToBuffer(char* ptr, TGuid value)
  120. {
  121. auto writeHex1 = [&] (ui8 x) {
  122. *ptr = HexDigits1[x];
  123. ptr += 1;
  124. };
  125. auto writeHex2 = [&] (ui8 x) {
  126. ::memcpy(ptr, &HexDigits2[x], 2);
  127. ptr += 2;
  128. };
  129. auto writeComponent = [&] (ui32 x) {
  130. /* */ if (x >= 0x10000000) {
  131. writeHex2((x >> 24) & 0xff);
  132. writeHex2((x >> 16) & 0xff);
  133. writeHex2((x >> 8) & 0xff);
  134. writeHex2( x & 0xff);
  135. } else if (x >= 0x1000000) {
  136. writeHex1( x >> 24);
  137. writeHex2((x >> 16) & 0xff);
  138. writeHex2((x >> 8) & 0xff);
  139. writeHex2( x & 0xff);
  140. } else if (x >= 0x100000) {
  141. writeHex2((x >> 16) & 0xff);
  142. writeHex2((x >> 8) & 0xff);
  143. writeHex2( x & 0xff);
  144. } else if (x >= 0x10000) {
  145. writeHex1( x >> 16);
  146. writeHex2((x >> 8) & 0xff);
  147. writeHex2( x & 0xff);
  148. } else if (x >= 0x1000) {
  149. writeHex2( x >> 8);
  150. writeHex2( x & 0xff);
  151. } else if (x >= 0x100) {
  152. writeHex1( x >> 8);
  153. writeHex2( x & 0xff);
  154. } else if (x >= 0x10) {
  155. writeHex2( x);
  156. } else {
  157. writeHex1( x);
  158. }
  159. };
  160. auto writeDash = [&] () {
  161. *ptr++ = '-';
  162. };
  163. writeComponent(value.Parts32[3]);
  164. writeDash();
  165. writeComponent(value.Parts32[2]);
  166. writeDash();
  167. writeComponent(value.Parts32[1]);
  168. writeDash();
  169. writeComponent(value.Parts32[0]);
  170. return ptr;
  171. }
  172. ////////////////////////////////////////////////////////////////////////////////
  173. TFormattableGuid::TFormattableGuid(TGuid guid)
  174. : End_(WriteGuidToBuffer(Buffer_.data(), guid))
  175. { }
  176. TStringBuf TFormattableGuid::ToStringBuf() const
  177. {
  178. return {Buffer_.data(), End_};
  179. }
  180. ////////////////////////////////////////////////////////////////////////////////
  181. } // namespace NYT