summaryrefslogtreecommitdiff
path: root/plugin/type_uuid/sql_type_uuid.cc
blob: 9b4a731dbb2770b9befff3f1044124af9816c95f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* Copyright (c) 2019,2021 MariaDB Corporation

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335  USA */

#define MYSQL_SERVER
#include "mariadb.h"
#include "my_net.h"
#include "sql_class.h" // THD, SORT_FIELD_ATTR
#include "opt_range.h" // SEL_ARG
#include "sql_type_uuid.h"


static bool get_digit(char ch, uint *val)
{
  if (ch >= '0' && ch <= '9')
  {
    *val= (uint) ch - '0';
    return false;
  }
  if (ch >= 'a' && ch <= 'f')
  {
    *val= (uint) ch - 'a' + 0x0a;
    return false;
  }
  if (ch >= 'A' && ch <= 'F')
  {
    *val= (uint) ch - 'A' + 0x0a;
    return false;
  }
  return true;
}


static bool get_digit(uint *val, const char *str, const char *end)
{
  if (str >= end)
    return true;
  return get_digit(*str, val);
}


static size_t skip_hyphens(const char *str, const char *end)
{
  const char *str0= str;
  for ( ; str < end; str++)
  {
    if (str[0] != '-')
      break;
  }
  return str - str0;
}


static const char *get_two_digits(char *val, const char *str, const char *end)
{
  uint hi, lo;
  if (get_digit(&hi, str++, end))
    return NULL;
  str+= skip_hyphens(str, end);
  if (get_digit(&lo, str++, end))
    return NULL;
  *val= (char) ((hi << 4) + lo);
  return str;
}


bool UUID::ascii_to_fbt(const char *str, size_t str_length)
{
  const char *end= str + str_length;
  /*
    The format understood:
    - Hyphen is not allowed on the first and the last position.
    - Otherwise, hyphens are allowed on any (odd and even) position,
      with any amount.
  */
  if (str_length < 32)
    goto err;

  for (uint oidx= 0; oidx < binary_length(); oidx++)
  {
    if (!(str= get_two_digits(&m_buffer[oidx], str, end)))
      goto err;
    // Allow hypheps after two digits, but not after the last digit
    if (oidx + 1 < binary_length())
      str+= skip_hyphens(str, end);
  }
  if (str < end)
    goto err; // Some input left
  return false;
err:
  bzero(m_buffer, sizeof(m_buffer));
  return true;
}

size_t UUID::to_string(char *dst, size_t dstsize) const
{
  my_uuid2str((const uchar *) m_buffer, dst, 1);
  return MY_UUID_STRING_LENGTH;
}


const Name &UUID::default_value()
{
  static Name def(STRING_WITH_LEN("00000000-0000-0000-0000-000000000000"));
  return def;
}