1 /* Copyright (c) 2002, 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 Foundation,
21    51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
22 
23 /*
24   Functions to read and parse geometrical data.
25   NOTE: These functions assumes that the string is end \0 terminated!
26 */
27 
28 #include "gstream.h"
29 #include "mysql/mysql_lex_string.h"              // LEX_STRING
30 /* key_memory_Gis_read_stream_err_msg */
31 #include "mysqld.h"
32 
get_next_toc_type()33 enum Gis_read_stream::enum_tok_types Gis_read_stream::get_next_toc_type()
34 {
35   skip_space();
36   if (m_cur >= m_limit)
37     return eostream;
38   if (my_isvar_start(&my_charset_bin, *m_cur))
39     return word;
40   if ((*m_cur >= '0' && *m_cur <= '9') || *m_cur == '-' || *m_cur == '+')
41     return numeric;
42   if (*m_cur == '(')
43     return l_bra;
44   if (*m_cur == ')')
45     return r_bra;
46   if (*m_cur == ',')
47     return comma;
48   return unknown;
49 }
50 
51 
get_next_word(LEX_STRING * res)52 bool Gis_read_stream::get_next_word(LEX_STRING *res)
53 {
54   skip_space();
55   res->str= (char*) m_cur;
56   /* The following will also test for \0 */
57   if ((m_cur >= m_limit) || !my_isvar_start(&my_charset_bin, *m_cur))
58     return 1;
59 
60   /*
61     We can't combine the following increment with my_isvar() because
62     my_isvar() is a macro that would cause side effects
63   */
64   m_cur++;
65   while ((m_cur < m_limit) && my_isvar(&my_charset_bin, *m_cur))
66     m_cur++;
67 
68   res->length= (uint32) (m_cur - res->str);
69   return 0;
70 }
71 
72 
73 /*
74   Read a floating point number
75 
76   NOTE: Number must start with a digit or sign. It can't start with a decimal
77   point
78 */
79 
get_next_number(double * d)80 bool Gis_read_stream::get_next_number(double *d)
81 {
82   char *endptr;
83   int err;
84 
85   skip_space();
86 
87   if ((m_cur >= m_limit) ||
88       ((*m_cur < '0' || *m_cur > '9') && *m_cur != '-' && *m_cur != '+'))
89   {
90     set_error_msg("Numeric constant expected");
91     return 1;
92   }
93 
94   *d = my_strntod(m_charset, (char *)m_cur,
95 		  (uint) (m_limit-m_cur), &endptr, &err);
96   if (err)
97     return 1;
98   if (endptr)
99     m_cur = endptr;
100   return 0;
101 }
102 
103 
check_next_symbol(char symbol)104 bool Gis_read_stream::check_next_symbol(char symbol)
105 {
106   skip_space();
107   if ((m_cur >= m_limit) || (*m_cur != symbol))
108   {
109     char buff[32];
110     my_stpcpy(buff, "'?' expected");
111     buff[2]= symbol;
112     set_error_msg(buff);
113     return 1;
114   }
115   m_cur++;
116   return 0;
117 }
118 
119 
120 /*
121   Remember error message.
122 */
123 
set_error_msg(const char * msg)124 void Gis_read_stream::set_error_msg(const char *msg)
125 {
126   size_t len= strlen(msg);			// ok in this context
127   m_err_msg= (char *) my_realloc(key_memory_Gis_read_stream_err_msg,
128                                  m_err_msg, (uint) len + 1, MYF(MY_ALLOW_ZERO_PTR));
129   memcpy(m_err_msg, msg, len + 1);
130 }
131