Debian Codegraph @

libbinlogevents/src/uuid.cpp

raw
1 /* Copyright (c) 2014, 2026, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24 #include <stddef.h>
25
26 #include "control_events.h"
27
28 /*
29 const size_t Uuid::TEXT_LENGTH;
30 const size_t Uuid::BYTE_LENGTH;
31 const size_t Uuid::BIT_LENGTH;
32 */
33 namespace binary_log {
34
35 const int Uuid::bytes_per_section[NUMBER_OF_SECTIONS] = {4, 2, 2, 2, 6};
36 const int Uuid::hex_to_byte[] = {
37 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
39 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8,
40 9, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1,
41 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
42 -1, -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
43 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
44 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
45 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
46 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
47 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
48 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
49 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
50 -1, -1, -1, -1, -1, -1, -1, -1, -1,
51 };
52
53 int Uuid::parse(const char *string, size_t len) {
54 return parse(string, len, bytes);
55 }
56
57 int Uuid::parse(const char *in_string, size_t len,
58 const unsigned char *out_str) {
59 const unsigned char **p_out_str = out_str ? &out_str : nullptr;
60
61 switch (len) {
62 // UUID without dashes. ex 12345678123456781234567812345678
63 case TEXT_LENGTH - 4:
64 if (read_section((TEXT_LENGTH - 4) / 2, &in_string, p_out_str)) return 1;
65 break;
66 // UUID with braces ex {12345678-1234-5678-1234-567812345678}
67 case TEXT_LENGTH + 2:
68 if (*in_string != '{' || in_string[TEXT_LENGTH + 1] != '}') return 1;
69 in_string++;
70 [[fallthrough]];
71 // standard UUID ex 12345678-1234-5678-1234-567812345678
72 case TEXT_LENGTH:
73 for (int i = 0; i < NUMBER_OF_SECTIONS - 1; i++) {
74 if (read_section(bytes_per_section[i], &in_string, p_out_str)) return 1;
75 if (*in_string == '-')
76 in_string++;
77 else
78 return 1;
79 }
80 if (read_section(bytes_per_section[NUMBER_OF_SECTIONS - 1], &in_string,
81 p_out_str))
82 return 1;
83 break;
84 default:
85 return 1;
86 }
87 return 0;
88 }
89
90 bool Uuid::read_section(int section_len, const char **section_str,
91 const unsigned char **out_binary_str) {
92 const unsigned char **section_string =
93 reinterpret_cast<const unsigned char **>(section_str);
94 for (int j = 0; j < section_len; j++) {
95 int hi = hex_to_byte[**section_string];
96 if (hi == -1) return true;
97 (*section_string)++;
98 int lo = hex_to_byte[**section_string];
99 if (lo == -1) return true;
100 (*section_string)++;
101 if (out_binary_str) {
102 unsigned char *u = const_cast<unsigned char *>(*out_binary_str);
103 *u = ((hi << 4) + lo);
104 (*out_binary_str)++;
105 }
106 }
107 return false;
108 }
109
110 bool Uuid::is_valid(const char *s, size_t len) {
111 return parse(s, len, nullptr) == 0;
112 }
113
114 size_t Uuid::to_string(const unsigned char *bytes_arg, char *buf) {
115 static const char byte_to_hex[] = "0123456789abcdef";
116 const unsigned char *u = bytes_arg;
117 for (int i = 0; i < NUMBER_OF_SECTIONS; i++) {
118 if (i > 0) {
119 *buf = '-';
120 buf++;
121 }
122 for (int j = 0; j < bytes_per_section[i]; j++) {
123 int byte = *u;
124 *buf = byte_to_hex[byte >> 4];
125 buf++;
126 *buf = byte_to_hex[byte & 0xf];
127 buf++;
128 u++;
129 }
130 }
131 *buf = '\0';
132 return TEXT_LENGTH;
133 }
134
135 size_t Uuid::to_string(char *buf) const { return to_string(bytes, buf); }
136 } // namespace binary_log