1 /* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
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 Without limiting anything contained in the foregoing, this file,
15 which is part of C Driver for MySQL (Connector/C), is also subject to the
16 Universal FOSS Exception, version 1.0, a copy of which can be found at
17 http://oss.oracle.com/licenses/universal-foss-exception.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License, version 2.0, for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
27
28 #include <my_global.h>
29 #include <mysql_com.h>
30 #include <mysql.h>
31
32 /* Get the length of next field. Change parameter to point at fieldstart */
net_field_length(uchar ** packet)33 ulong STDCALL net_field_length(uchar **packet)
34 {
35 uchar *pos= (uchar *)*packet;
36 if (*pos < 251)
37 {
38 (*packet)++;
39 return (ulong) *pos;
40 }
41 if (*pos == 251)
42 {
43 (*packet)++;
44 return NULL_LENGTH;
45 }
46 if (*pos == 252)
47 {
48 (*packet)+=3;
49 return (ulong) uint2korr(pos+1);
50 }
51 if (*pos == 253)
52 {
53 (*packet)+=4;
54 return (ulong) uint3korr(pos+1);
55 }
56 (*packet)+=9; /* Must be 254 when here */
57 return (ulong) uint4korr(pos+1);
58 }
59
60 /* The same as above but with max length check */
net_field_length_checked(uchar ** packet,ulong max_length)61 ulong STDCALL net_field_length_checked(uchar **packet, ulong max_length)
62 {
63 ulong len;
64 uchar *pos= (uchar *)*packet;
65
66 if (*pos < 251)
67 {
68 (*packet)++;
69 len= (ulong) *pos;
70 return (len > max_length) ? max_length : len;
71 }
72 if (*pos == 251)
73 {
74 (*packet)++;
75 return NULL_LENGTH;
76 }
77 if (*pos == 252)
78 {
79 (*packet)+=3;
80 len= (ulong) uint2korr(pos+1);
81 return (len > max_length) ? max_length : len;
82 }
83 if (*pos == 253)
84 {
85 (*packet)+=4;
86 len= (ulong) uint3korr(pos+1);
87 return (len > max_length) ? max_length : len;
88 }
89 (*packet)+=9; /* Must be 254 when here */
90 len= (ulong) uint4korr(pos+1);
91 return (len > max_length) ? max_length : len;
92 }
93
94 /* The same as above but returns longlong */
net_field_length_ll(uchar ** packet)95 my_ulonglong net_field_length_ll(uchar **packet)
96 {
97 uchar *pos= *packet;
98 if (*pos < 251)
99 {
100 (*packet)++;
101 return (my_ulonglong) *pos;
102 }
103 if (*pos == 251)
104 {
105 (*packet)++;
106 return (my_ulonglong) NULL_LENGTH;
107 }
108 if (*pos == 252)
109 {
110 (*packet)+=3;
111 return (my_ulonglong) uint2korr(pos+1);
112 }
113 if (*pos == 253)
114 {
115 (*packet)+=4;
116 return (my_ulonglong) uint3korr(pos+1);
117 }
118 (*packet)+=9; /* Must be 254 when here */
119 return (my_ulonglong) uint8korr(pos+1);
120 }
121
122 /*
123 Store an integer with simple packing into a output package
124
125 SYNOPSIS
126 net_store_length()
127 pkg Store the packed integer here
128 length integers to store
129
130 NOTES
131 This is mostly used to store lengths of strings.
132 We have to cast the result of the LL because of a bug in Forte CC
133 compiler.
134
135 RETURN
136 Position in 'pkg' after the packed length
137 */
138
net_store_length(uchar * packet,ulonglong length)139 uchar *net_store_length(uchar *packet, ulonglong length)
140 {
141 if (length < (ulonglong) 251LL)
142 {
143 *packet=(uchar) length;
144 return packet+1;
145 }
146 /* 251 is reserved for NULL */
147 if (length < (ulonglong) 65536LL)
148 {
149 *packet++=252;
150 int2store(packet,(uint) length);
151 return packet+2;
152 }
153 if (length < (ulonglong) 16777216LL)
154 {
155 *packet++=253;
156 int3store(packet,(ulong) length);
157 return packet+3;
158 }
159 *packet++=254;
160 int8store(packet,length);
161 return packet+8;
162 }
163
164
165 /**
166 The length of space required to store the resulting length-encoded integer
167 for the given number. This function can be used at places where one needs to
168 dynamically allocate the buffer for a given number to be stored as length-
169 encoded integer.
170
171 @param num [IN] the input number
172
173 @return length of buffer needed to store this number [1, 3, 4, 9].
174 */
175
net_length_size(ulonglong num)176 uint net_length_size(ulonglong num)
177 {
178 if (num < (ulonglong) 252LL)
179 return 1;
180 if (num < (ulonglong) 65536LL)
181 return 3;
182 if (num < (ulonglong) 16777216LL)
183 return 4;
184 return 9;
185 }
186
187 /**
188 length of buffer required to represent a length-encoded string
189 give the length part of length encoded string. This function can
190 be used before calling net_field_lenth/net_field_length_ll.
191
192 @param [in] pos Length information of length-encoded string
193
194 @return length of buffer needed to store this number [1, 3, 4, 9].
195 */
196
net_field_length_size(const uchar * pos)197 uint net_field_length_size(const uchar *pos) {
198 if (*pos <= 251) return 1;
199 if (*pos == 252) return 3;
200 if (*pos == 253) return 4;
201 return 9;
202 }
203