1 /* Copyright (c) 2014, 2021, 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 also distributed 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 included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22    02110-1301 USA */
23 
24 #include "control_events.h"
25 
26 
27 /*
28 const size_t Uuid::TEXT_LENGTH;
29 const size_t Uuid::BYTE_LENGTH;
30 const size_t Uuid::BIT_LENGTH;
31 */
32 namespace binary_log
33 {
34 
35 const int Uuid::bytes_per_section[NUMBER_OF_SECTIONS]=
36 { 4, 2, 2, 2, 6 };
37 const int Uuid::hex_to_byte[]=
38 {
39   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
40   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
41   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
42   0,  1,  2,  3,  4,  5,  6,  7,  8,  9, -1, -1, -1, -1, -1, -1,
43   -1, 10, 11, 12, 13, 14, 15, -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,
45   -1, 10, 11, 12, 13, 14, 15, -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,
47   -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,
49   -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, -1, -1, -1, -1, -1, -1, -1,
51   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
52   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
53   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
54   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
55 };
56 
parse(const char * s)57 int Uuid::parse(const char *s)
58 {
59   unsigned char *u= bytes;
60   unsigned char *ss= (unsigned char *)s;
61   for (int i= 0; i < NUMBER_OF_SECTIONS; i++)
62   {
63     if (i > 0)
64     {
65       if (*ss != '-')
66         return 1;
67       ss++;
68     }
69     for (int j= 0; j < bytes_per_section[i]; j++)
70     {
71       int hi= hex_to_byte[*ss];
72       if (hi == -1)
73         return 1;
74       ss++;
75       int lo= hex_to_byte[*ss];
76       if (lo == -1)
77         return 1;
78       ss++;
79       *u= (hi << 4) + lo;
80       u++;
81     }
82   }
83   return 0;
84 }
85 
is_valid(const char * s)86 bool Uuid::is_valid(const char *s)
87 {
88   const unsigned char *ss= (const unsigned char *)s;
89   for (int i= 0; i < NUMBER_OF_SECTIONS; i++)
90   {
91     if (i > 0)
92     {
93       if (*ss != '-')
94         return (false);
95       ss++;
96     }
97     for (int j= 0; j < bytes_per_section[i]; j++)
98     {
99       if (hex_to_byte[*ss] == -1)
100         return (false);
101       ss++;
102       if (hex_to_byte[*ss] == -1)
103         return (false);
104       ss++;
105     }
106   }
107   return (true);
108 }
to_string(const unsigned char * bytes_arg,char * buf)109 size_t Uuid::to_string(const unsigned char* bytes_arg, char *buf)
110 {
111   static const char byte_to_hex[]= "0123456789abcdef";
112   const unsigned char *u= bytes_arg;
113   for (int i= 0; i < NUMBER_OF_SECTIONS; i++)
114   {
115     if (i > 0)
116     {
117       *buf= '-';
118       buf++;
119     }
120     for (int j= 0; j < bytes_per_section[i]; j++)
121     {
122       int byte= *u;
123       *buf= byte_to_hex[byte >> 4];
124       buf++;
125       *buf= byte_to_hex[byte & 0xf];
126       buf++;
127       u++;
128     }
129   }
130   *buf= '\0';
131   return TEXT_LENGTH;
132 }
133 
134 
to_string(char * buf) const135 size_t Uuid::to_string(char *buf) const
136 {
137   return to_string(bytes, buf);
138 }
139 }//namespace binary_log
140