MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
uuid.cc
1 /* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
2 
3  This program is free software; you can redistribute it and/or
4  modify it under the terms of the GNU General Public License as
5  published by the Free Software Foundation; version 2 of the
6  License.
7 
8  This program is distributed in the hope that it will be useful, but
9  WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
16  02110-1301 USA */
17 
18 #include "rpl_gtid.h"
19 
20 
21 /*
22 const size_t Uuid::TEXT_LENGTH;
23 const size_t Uuid::BYTE_LENGTH;
24 const size_t Uuid::BIT_LENGTH;
25 */
26 const int Uuid::bytes_per_section[Uuid::NUMBER_OF_SECTIONS]=
27 { 4, 2, 2, 2, 6 };
28 const int Uuid::hex_to_byte[]=
29 {
30  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
31  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
32  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
33  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
34  -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
35  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36  -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37  -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,
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  -1, -1, -1, -1, -1, -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,
44  -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,
46 };
47 
48 
49 enum_return_status Uuid::parse(const char *s)
50 {
51  DBUG_ENTER("Uuid::parse");
52  unsigned char *u= bytes;
53  unsigned char *ss= (unsigned char *)s;
54  for (int i= 0; i < NUMBER_OF_SECTIONS; i++)
55  {
56  if (i > 0)
57  {
58  if (*ss != '-')
59  RETURN_UNREPORTED_ERROR;
60  ss++;
61  }
62  for (int j= 0; j < bytes_per_section[i]; j++)
63  {
64  int hi= hex_to_byte[*ss];
65  if (hi == -1)
66  RETURN_UNREPORTED_ERROR;
67  ss++;
68  int lo= hex_to_byte[*ss];
69  if (lo == -1)
70  RETURN_UNREPORTED_ERROR;
71  ss++;
72  *u= (hi << 4) + lo;
73  u++;
74  }
75  }
76  RETURN_OK;
77 }
78 
79 
80 bool Uuid::is_valid(const char *s)
81 {
82  DBUG_ENTER("Uuid::is_valid");
83  const unsigned char *ss= (const unsigned char *)s;
84  for (int i= 0; i < NUMBER_OF_SECTIONS; i++)
85  {
86  if (i > 0)
87  {
88  if (*ss != '-')
89  DBUG_RETURN(false);
90  ss++;
91  }
92  for (int j= 0; j < bytes_per_section[i]; j++)
93  {
94  if (hex_to_byte[*ss] == -1)
95  DBUG_RETURN(false);
96  ss++;
97  if (hex_to_byte[*ss] == -1)
98  DBUG_RETURN(false);
99  ss++;
100  }
101  }
102  DBUG_RETURN(true);
103 }
104 
105 
106 size_t Uuid::to_string(const uchar* bytes_arg, char *buf)
107 {
108  DBUG_ENTER("Uuid::to_string");
109  static const char byte_to_hex[]= "0123456789abcdef";
110  const unsigned char *u= bytes_arg;
111  for (int i= 0; i < NUMBER_OF_SECTIONS; i++)
112  {
113  if (i > 0)
114  {
115  *buf= '-';
116  buf++;
117  }
118  for (int j= 0; j < bytes_per_section[i]; j++)
119  {
120  int byte= *u;
121  *buf= byte_to_hex[byte >> 4];
122  buf++;
123  *buf= byte_to_hex[byte & 0xf];
124  buf++;
125  u++;
126  }
127  }
128  *buf= '\0';
129  DBUG_RETURN(TEXT_LENGTH);
130 }
131 
132 
133 size_t Uuid::to_string(char *buf) const
134 {
135  return to_string(bytes, buf);
136 }