1 /* Copyright (c) 2000, 2018 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 Street, 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 reg1 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 reg1 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 #ifdef NO_CLIENT_LONGLONG
120 return (my_ulonglong) uint4korr(pos+1);
121 #else
122 return (my_ulonglong) uint8korr(pos+1);
123 #endif
124 }
125
126 /*
127 Store an integer with simple packing into a output package
128
129 SYNOPSIS
130 net_store_length()
131 pkg Store the packed integer here
132 length integers to store
133
134 NOTES
135 This is mostly used to store lengths of strings.
136 We have to cast the result for the LL() becasue of a bug in Forte CC
137 compiler.
138
139 RETURN
140 Position in 'pkg' after the packed length
141 */
142
net_store_length(uchar * packet,ulonglong length)143 uchar *net_store_length(uchar *packet, ulonglong length)
144 {
145 if (length < (ulonglong) LL(251))
146 {
147 *packet=(uchar) length;
148 return packet+1;
149 }
150 /* 251 is reserved for NULL */
151 if (length < (ulonglong) LL(65536))
152 {
153 *packet++=252;
154 int2store(packet,(uint) length);
155 return packet+2;
156 }
157 if (length < (ulonglong) LL(16777216))
158 {
159 *packet++=253;
160 int3store(packet,(ulong) length);
161 return packet+3;
162 }
163 *packet++=254;
164 int8store(packet,length);
165 return packet+8;
166 }
167
168