Source file mp_field_packet.ml
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
type field_packet_field_type =
Field_type_decimal
| Field_type_tiny
| Field_type_short
| Field_type_long
| Field_type_float
| Field_type_double
| Field_type_null
| Field_type_timestamp
| Field_type_longlong
| Field_type_int24
| Field_type_date
| Field_type_time
| Field_type_datetime
| Field_type_year
| Field_type_newdate
| Field_type_varchar
| Field_type_bit
| Field_type_newdecimal
| Field_type_enum
| Field_type_set
| Field_type_tiny_blob
| Field_type_medium_blob
| Field_type_long_blob
| Field_type_blob
| Field_type_var_string
| Field_type_string
| Field_type_geometry
let field_packet_field_type_to_string p =
let s =
match p with
Field_type_decimal -> "DECIMAL"
| Field_type_tiny -> "TINY"
| Field_type_short -> "SHORT"
| Field_type_long -> "LONG"
| Field_type_float -> "FLOAT"
| Field_type_double -> "DOUBLE"
| Field_type_null -> "NULL"
| Field_type_timestamp -> "TIMESTAMP"
| Field_type_longlong ->"LONGLONG"
| Field_type_int24 -> "INT24"
| Field_type_date -> "DATE"
| Field_type_time -> "TIME"
| Field_type_datetime -> "DATETIME"
| Field_type_year -> "YEAR"
| Field_type_newdate -> "NEWDATE"
| Field_type_varchar -> "VARCHAR"
| Field_type_bit -> "BIT"
| Field_type_newdecimal -> "NEWDECIMAL"
| Field_type_enum -> "ENUM"
| Field_type_set -> "SET"
| Field_type_tiny_blob -> "TINYBLOB"
| Field_type_medium_blob -> "MEDIUMBLOB"
| Field_type_long_blob -> "LONGBLOB"
| Field_type_blob -> "BLOB"
| Field_type_var_string -> "VARSTRING"
| Field_type_string -> "STRING"
| Field_type_geometry -> "GEOMETRY"
in
s
type field_packet_field_flag =
Field_flag_not_null
| Field_flag_pri_key
| Field_flag_unique_key
| Field_flag_multiple_key
| Field_flag_blob
| Field_flag_unsigned
| Field_flag_zerofill
| Field_flag_binary
| Field_flag_enum
| Field_flag_auto_increment
| Field_flag_timestamp
| Field_flag_set
let field_packet_field_flag_to_string p =
let s =
match p with
Field_flag_not_null -> "NOT NULL"
| Field_flag_pri_key -> "PRIMARY KEY"
| Field_flag_unique_key -> "UNIQUE KEY"
| Field_flag_multiple_key -> "MULTIPLE KEY"
| Field_flag_blob -> "BLOB"
| Field_flag_unsigned -> "UNSIGNED"
| Field_flag_zerofill -> "ZEROFILL"
| Field_flag_binary -> "BINARY"
| Field_flag_enum -> "ENUM"
| Field_flag_auto_increment -> "AUTO_INCREMENT"
| Field_flag_timestamp -> "TIMESTAMP"
| Field_flag_set -> "SET"
in
s
type field_packet = {
field_catalog : string;
field_db : string;
field_table : string;
field_org_table : string;
field_name : string;
field_org_name : string;
field_charset_number : int;
field_length : Int64.t;
field_type : field_packet_field_type;
field_flags : field_packet_field_flag list;
field_decimals : int;
field_default : Int64.t;
version : Mp_protocol.protocol_version
}
let field_packet_to_string p =
let version = Mp_protocol.protocol_version_to_string p.version in
let field_type = field_packet_field_type_to_string p.field_type in
let field_flags =
let f acc e =
let s = field_packet_field_flag_to_string e in
acc ^ s ^ " "
in
List.fold_left f "" p.field_flags
in
let fmt = format_of_string "field_catalog : %s\n"
^^ format_of_string "field_db : %s\n"
^^ format_of_string "field_table : %s\n"
^^ format_of_string "field_org_table : %s\n"
^^ format_of_string "field_name : %s\n"
^^ format_of_string "field_org_name : %s\n"
^^ format_of_string "field_charset_number : %u\n"
^^ format_of_string "field_length : %Lu\n"
^^ format_of_string "field_type : %s\n"
^^ format_of_string "field_flags : %s\n"
^^ format_of_string "field_decimals : %u\n"
^^ format_of_string "field_default : %Lu\n"
^^ format_of_string "version : %s\n"
in
Printf.sprintf fmt p.field_catalog
p.field_db
p.field_table
p.field_org_table
p.field_name
p.field_org_name
p.field_charset_number
p.field_length
field_type
field_flags
p.field_decimals
p.field_default
version
let decode_field_packet_field_type field_type =
match field_type with
0x00 -> Field_type_decimal
| 0x01 -> Field_type_tiny
| 0x02 -> Field_type_short
| 0x03 -> Field_type_long
| 0x04 -> Field_type_float
| 0x05 -> Field_type_double
| 0x06 -> Field_type_null
| 0x07 -> Field_type_timestamp
| 0x08 -> Field_type_longlong
| 0x09 -> Field_type_int24
| 0x0a -> Field_type_date
| 0x0b -> Field_type_time
| 0x0c -> Field_type_datetime
| 0x0d -> Field_type_year
| 0x0e -> Field_type_newdate
| 0x0f -> Field_type_varchar
| 0x10 -> Field_type_bit
| 0xf6 -> Field_type_newdecimal
| 0xf7 -> Field_type_enum
| 0xf8 -> Field_type_set
| 0xf9 -> Field_type_tiny_blob
| 0xfa -> Field_type_medium_blob
| 0xfb -> Field_type_long_blob
| 0xfc -> Field_type_blob
| 0xfd -> Field_type_var_string
| 0xfe -> Field_type_string
| 0xff -> Field_type_geometry
| _ -> failwith (Printf.sprintf "Unknown field type = %u in field packet" field_type)
let decode_field_packet_field_flag bits =
let length = Bitstring.bitstring_length bits in
let (binary, zerofill, unsigned, blob, multiple_key, unique_key, pri_key, not_null,
set, timestamp, auto_increment, enum) =
if length = 2*8 then
match%bitstring bits with
| {| binary : 1;
zerofill : 1;
unsigned : 1;
blob : 1;
multiple_key : 1;
unique_key : 1;
pri_key : 1;
not_null : 1;
_ : 1;
_ : 1;
_ : 1;
_ : 1;
set : 1;
timestamp : 1;
auto_increment : 1;
enum : 1
|} ->
(binary, zerofill, unsigned, blob, multiple_key, unique_key, pri_key, not_null,
set, timestamp, auto_increment, enum)
else if length = 1*8 then
match%bitstring bits with
| {| binary : 1;
zerofill : 1;
unsigned : 1;
blob : 1;
multiple_key : 1;
unique_key : 1;
pri_key : 1;
not_null : 1
|} ->
(binary, zerofill, unsigned, blob, multiple_key, unique_key, pri_key, not_null,
false, false, false, false)
else
failwith (Printf.sprintf "Bad length = %u for field flags in field packet" length)
in
let l = [] in
let l = if binary then Field_flag_binary::l else l in
let l = if zerofill then Field_flag_zerofill::l else l in
let l = if unsigned then Field_flag_unsigned::l else l in
let l = if blob then Field_flag_blob::l else l in
let l = if multiple_key then Field_flag_multiple_key::l else l in
let l = if unique_key then Field_flag_unique_key::l else l in
let l = if pri_key then Field_flag_pri_key::l else l in
let l = if not_null then Field_flag_not_null::l else l in
let l = if set then Field_flag_set::l else l in
let l = if timestamp then Field_flag_timestamp::l else l in
let l = if auto_increment then Field_flag_auto_increment::l else l in
let l = if enum then Field_flag_enum::l else l in
l
let field_packet acc ic oc =
let (_, _, bits) = Mp_packet.extract_packet ic oc in
let (catalog, rest) = Mp_string.length_coded_string bits in
let field =
if catalog = "def" then
let (db, rest) = Mp_string.length_coded_string rest in
let (table, rest) = Mp_string.length_coded_string rest in
let (org_table, rest) = Mp_string.length_coded_string rest in
let (name, rest) = Mp_string.length_coded_string rest in
let (org_name, rest) = Mp_string.length_coded_string rest in
let length_rest = (Bitstring.bitstring_length rest) - ((9*8) + Mp_bitstring.compute32) in
match%bitstring rest with
| {| _ : 1*8 : int, unsigned, bigendian; (* filler *)
charset_number : 2*8 : int, unsigned, littleendian;
length : Mp_bitstring.compute32 : int, unsigned, littleendian;
field_type : 1*8 : int, unsigned, bigendian;
flags : 2*8 : bitstring;
decimals : 1*8 : int, unsigned, bigendian;
0x00 : 1*8 : int, unsigned, bigendian;
0x00 : 1*8 : int, unsigned, bigendian;
rest : length_rest : bitstring |} -> (
let (default, _) =
if (Bitstring.bitstring_length rest > 0) then
Mp_binary.length_coded_binary rest
else
(Int64.zero, rest)
in
{
field_catalog = catalog;
field_db = db;
field_table = table;
field_org_table = org_table;
field_name = name;
field_org_name = org_name;
field_charset_number = charset_number;
field_length = length;
field_type = decode_field_packet_field_type field_type;
field_flags = decode_field_packet_field_flag flags;
field_decimals = decimals;
field_default = default;
version = Mp_protocol.Protocol_version_41
}
)
else
let (field_table, rest) = Mp_string.length_coded_string bits in
let (field_name, rest) = Mp_string.length_coded_string rest in
let length_rest = (Bitstring.bitstring_length rest) - (7*8) in
match%bitstring rest with
| {| 3 : 1*8 : int, unsigned, bigendian;
length : 3*8 : int, unsigned, littleendian;
1: 1*8 : int, unsigned, bigendian;
field_type : 1*8 : int, unsigned, bigendian;
head_flags : 1*8 : int, unsigned, bigendian;
rest : length_rest : bitstring |} ->
let (flags, rest) =
if (head_flags = 2) then
let length_rest = (Bitstring.bitstring_length rest) - 8 in
match%bitstring rest with
| {| flags : 1*8 : bitstring;
rest : length_rest : bitstring |} -> (flags, rest)
else if (head_flags = 3) then
let length_rest = (Bitstring.bitstring_length rest) - (2*8) in
match%bitstring rest with
| {| flags : 2*8 : bitstring;
rest : length_rest : bitstring |} -> (flags, rest)
else
failwith (Printf.sprintf "Bad head_flags = %u in field packet" head_flags)
in
let length_rest = (Bitstring.bitstring_length rest) - 8 in
match%bitstring rest with
| {| decimals : 1*8 : int, unsigned, bigendian;
rest : length_rest : bitstring |} -> (
let (default, _) =
if Bitstring.bitstring_length rest > 0 then
Mp_binary.length_coded_binary rest
else
(Int64.zero, Bitstring.empty_bitstring)
in
{ field_catalog = "";
field_db = "";
field_table = field_table;
field_org_table = "";
field_name = field_name;
field_org_name = "";
field_charset_number = 0;
field_length = (Int64.of_int length);
field_type = decode_field_packet_field_type field_type;
field_flags = decode_field_packet_field_flag flags;
field_decimals = decimals;
field_default = default;
version = Mp_protocol.Protocol_version_40 }
)
in
acc := field :: !acc