1 | /***************************************************************************** |
2 | |
3 | Copyright (c) 1995, 2016, Oracle and/or its affiliates. All Rights Reserved. |
4 | Copyright (c) 2017, MariaDB Corporation. |
5 | |
6 | This program is free software; you can redistribute it and/or modify it under |
7 | the terms of the GNU General Public License as published by the Free Software |
8 | Foundation; version 2 of the License. |
9 | |
10 | This program is distributed in the hope that it will be useful, but WITHOUT |
11 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
12 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU General Public License along with |
15 | this program; if not, write to the Free Software Foundation, Inc., |
16 | 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA |
17 | |
18 | *****************************************************************************/ |
19 | |
20 | /******************************************************************//** |
21 | @file include/mach0data.h |
22 | Utilities for converting data from the database file |
23 | to the machine format. |
24 | |
25 | Created 11/28/1995 Heikki Tuuri |
26 | ***********************************************************************/ |
27 | |
28 | #ifndef mach0data_h |
29 | #define mach0data_h |
30 | |
31 | #ifndef UNIV_INNOCHECKSUM |
32 | |
33 | #include "univ.i" |
34 | #include "mtr0types.h" |
35 | |
36 | /* The data and all fields are always stored in a database file |
37 | in the same format: ascii, big-endian, ... . |
38 | All data in the files MUST be accessed using the functions in this |
39 | module. */ |
40 | |
41 | /*******************************************************//** |
42 | The following function is used to store data in one byte. */ |
43 | UNIV_INLINE |
44 | void |
45 | mach_write_to_1( |
46 | /*============*/ |
47 | byte* b, /*!< in: pointer to byte where to store */ |
48 | ulint n); /*!< in: ulint integer to be stored, >= 0, < 256 */ |
49 | /** The following function is used to fetch data from one byte. |
50 | @param[in] b pointer to a byte to read |
51 | @return ulint integer, >= 0, < 256 */ |
52 | UNIV_INLINE |
53 | uint8_t |
54 | mach_read_from_1( |
55 | const byte* b) |
56 | MY_ATTRIBUTE((warn_unused_result)); |
57 | /*******************************************************//** |
58 | The following function is used to store data in two consecutive |
59 | bytes. We store the most significant byte to the lower address. */ |
60 | UNIV_INLINE |
61 | void |
62 | mach_write_to_2( |
63 | /*============*/ |
64 | byte* b, /*!< in: pointer to two bytes where to store */ |
65 | ulint n); /*!< in: ulint integer to be stored, >= 0, < 64k */ |
66 | #endif /* !UNIV_INNOCHECKSUM */ |
67 | /** The following function is used to fetch data from 2 consecutive |
68 | bytes. The most significant byte is at the lowest address. |
69 | @param[in] b pointer to 2 bytes where to store |
70 | @return 2-byte integer, >= 0, < 64k */ |
71 | UNIV_INLINE |
72 | uint16_t |
73 | mach_read_from_2( |
74 | const byte* b) |
75 | MY_ATTRIBUTE((warn_unused_result)); |
76 | |
77 | #ifndef UNIV_INNOCHECKSUM |
78 | /********************************************************//** |
79 | The following function is used to convert a 16-bit data item |
80 | to the canonical format, for fast bytewise equality test |
81 | against memory. |
82 | @return 16-bit integer in canonical format */ |
83 | UNIV_INLINE |
84 | uint16 |
85 | mach_encode_2( |
86 | /*==========*/ |
87 | ulint n) /*!< in: integer in machine-dependent format */ |
88 | MY_ATTRIBUTE((const)); |
89 | /********************************************************//** |
90 | The following function is used to convert a 16-bit data item |
91 | from the canonical format, for fast bytewise equality test |
92 | against memory. |
93 | @return integer in machine-dependent format */ |
94 | UNIV_INLINE |
95 | ulint |
96 | mach_decode_2( |
97 | /*==========*/ |
98 | uint16 n) /*!< in: 16-bit integer in canonical format */ |
99 | MY_ATTRIBUTE((const)); |
100 | /*******************************************************//** |
101 | The following function is used to store data in 3 consecutive |
102 | bytes. We store the most significant byte to the lowest address. */ |
103 | UNIV_INLINE |
104 | void |
105 | mach_write_to_3( |
106 | /*============*/ |
107 | byte* b, /*!< in: pointer to 3 bytes where to store */ |
108 | ulint n); /*!< in: ulint integer to be stored */ |
109 | /** The following function is used to fetch data from 3 consecutive |
110 | bytes. The most significant byte is at the lowest address. |
111 | @param[in] b pointer to 3 bytes to read |
112 | @return 32 bit integer */ |
113 | UNIV_INLINE |
114 | uint32_t |
115 | mach_read_from_3( |
116 | const byte* b) |
117 | MY_ATTRIBUTE((warn_unused_result)); |
118 | /*******************************************************//** |
119 | The following function is used to store data in four consecutive |
120 | bytes. We store the most significant byte to the lowest address. */ |
121 | UNIV_INLINE |
122 | void |
123 | mach_write_to_4( |
124 | /*============*/ |
125 | byte* b, /*!< in: pointer to four bytes where to store */ |
126 | ulint n); /*!< in: ulint integer to be stored */ |
127 | /** The following function is used to fetch data from 4 consecutive |
128 | bytes. The most significant byte is at the lowest address. |
129 | @param[in] b pointer to 4 bytes to read |
130 | @return 32 bit integer */ |
131 | UNIV_INLINE |
132 | uint32_t |
133 | mach_read_from_4( |
134 | const byte* b) |
135 | MY_ATTRIBUTE((warn_unused_result)); |
136 | /*********************************************************//** |
137 | Writes a ulint in a compressed form (1..5 bytes). |
138 | @return stored size in bytes */ |
139 | UNIV_INLINE |
140 | ulint |
141 | mach_write_compressed( |
142 | /*==================*/ |
143 | byte* b, /*!< in: pointer to memory where to store */ |
144 | ulint n); /*!< in: ulint integer to be stored */ |
145 | /*********************************************************//** |
146 | Returns the size of an ulint when written in the compressed form. |
147 | @return compressed size in bytes */ |
148 | UNIV_INLINE |
149 | ulint |
150 | mach_get_compressed_size( |
151 | /*=====================*/ |
152 | ulint n) /*!< in: ulint integer to be stored */ |
153 | MY_ATTRIBUTE((const)); |
154 | /** Read a 32-bit integer in a compressed form. |
155 | @param[in,out] b pointer to memory where to read; |
156 | advanced by the number of bytes consumed |
157 | @return unsigned value */ |
158 | UNIV_INLINE |
159 | ib_uint32_t |
160 | mach_read_next_compressed( |
161 | const byte** b); |
162 | /*******************************************************//** |
163 | The following function is used to store data in 6 consecutive |
164 | bytes. We store the most significant byte to the lowest address. */ |
165 | UNIV_INLINE |
166 | void |
167 | mach_write_to_6( |
168 | /*============*/ |
169 | byte* b, /*!< in: pointer to 6 bytes where to store */ |
170 | ib_uint64_t id); /*!< in: 48-bit integer */ |
171 | /********************************************************//** |
172 | The following function is used to fetch data from 6 consecutive |
173 | bytes. The most significant byte is at the lowest address. |
174 | @return 48-bit integer */ |
175 | UNIV_INLINE |
176 | ib_uint64_t |
177 | mach_read_from_6( |
178 | /*=============*/ |
179 | const byte* b) /*!< in: pointer to 6 bytes */ |
180 | MY_ATTRIBUTE((warn_unused_result)); |
181 | /*******************************************************//** |
182 | The following function is used to store data in 7 consecutive |
183 | bytes. We store the most significant byte to the lowest address. */ |
184 | UNIV_INLINE |
185 | void |
186 | mach_write_to_7( |
187 | /*============*/ |
188 | byte* b, /*!< in: pointer to 7 bytes where to store */ |
189 | ib_uint64_t n); /*!< in: 56-bit integer */ |
190 | /********************************************************//** |
191 | The following function is used to fetch data from 7 consecutive |
192 | bytes. The most significant byte is at the lowest address. |
193 | @return 56-bit integer */ |
194 | UNIV_INLINE |
195 | ib_uint64_t |
196 | mach_read_from_7( |
197 | /*=============*/ |
198 | const byte* b) /*!< in: pointer to 7 bytes */ |
199 | MY_ATTRIBUTE((warn_unused_result)); |
200 | /*******************************************************//** |
201 | The following function is used to store data in 8 consecutive |
202 | bytes. We store the most significant byte to the lowest address. */ |
203 | UNIV_INLINE |
204 | void |
205 | mach_write_to_8( |
206 | /*============*/ |
207 | void* b, /*!< in: pointer to 8 bytes where to store */ |
208 | ib_uint64_t n); /*!< in: 64-bit integer to be stored */ |
209 | /********************************************************//** |
210 | The following function is used to fetch data from 8 consecutive |
211 | bytes. The most significant byte is at the lowest address. |
212 | @return 64-bit integer */ |
213 | UNIV_INLINE |
214 | ib_uint64_t |
215 | mach_read_from_8( |
216 | /*=============*/ |
217 | const byte* b) /*!< in: pointer to 8 bytes */ |
218 | MY_ATTRIBUTE((warn_unused_result)); |
219 | /*********************************************************//** |
220 | Writes a 64-bit integer in a compressed form (5..9 bytes). |
221 | @return size in bytes */ |
222 | UNIV_INLINE |
223 | ulint |
224 | mach_u64_write_compressed( |
225 | /*======================*/ |
226 | byte* b, /*!< in: pointer to memory where to store */ |
227 | ib_uint64_t n); /*!< in: 64-bit integer to be stored */ |
228 | /** Read a 64-bit integer in a compressed form. |
229 | @param[in,out] b pointer to memory where to read; |
230 | advanced by the number of bytes consumed |
231 | @return unsigned value */ |
232 | UNIV_INLINE |
233 | ib_uint64_t |
234 | mach_u64_read_next_compressed( |
235 | const byte** b); |
236 | /*********************************************************//** |
237 | Writes a 64-bit integer in a compressed form (1..11 bytes). |
238 | @return size in bytes */ |
239 | UNIV_INLINE |
240 | ulint |
241 | mach_u64_write_much_compressed( |
242 | /*===========================*/ |
243 | byte* b, /*!< in: pointer to memory where to store */ |
244 | ib_uint64_t n); /*!< in: 64-bit integer to be stored */ |
245 | /*********************************************************//** |
246 | Reads a 64-bit integer in a compressed form. |
247 | @return the value read */ |
248 | UNIV_INLINE |
249 | ib_uint64_t |
250 | mach_u64_read_much_compressed( |
251 | /*==========================*/ |
252 | const byte* b) /*!< in: pointer to memory from where to read */ |
253 | MY_ATTRIBUTE((warn_unused_result)); |
254 | /** Read a 32-bit integer in a compressed form. |
255 | @param[in,out] ptr pointer to memory where to read; |
256 | advanced by the number of bytes consumed, or set NULL if out of space |
257 | @param[in] end_ptr end of the buffer |
258 | @return unsigned value */ |
259 | ib_uint32_t |
260 | mach_parse_compressed( |
261 | const byte** ptr, |
262 | const byte* end_ptr); |
263 | /** Read a 64-bit integer in a compressed form. |
264 | @param[in,out] ptr pointer to memory where to read; |
265 | advanced by the number of bytes consumed, or set NULL if out of space |
266 | @param[in] end_ptr end of the buffer |
267 | @return unsigned value */ |
268 | UNIV_INLINE |
269 | ib_uint64_t |
270 | mach_u64_parse_compressed( |
271 | const byte** ptr, |
272 | const byte* end_ptr); |
273 | |
274 | /*********************************************************//** |
275 | Reads a double. It is stored in a little-endian format. |
276 | @return double read */ |
277 | UNIV_INLINE |
278 | double |
279 | mach_double_read( |
280 | /*=============*/ |
281 | const byte* b) /*!< in: pointer to memory from where to read */ |
282 | MY_ATTRIBUTE((warn_unused_result)); |
283 | /*********************************************************//** |
284 | Writes a double. It is stored in a little-endian format. */ |
285 | UNIV_INLINE |
286 | void |
287 | mach_double_write( |
288 | /*==============*/ |
289 | byte* b, /*!< in: pointer to memory where to write */ |
290 | double d); /*!< in: double */ |
291 | /*********************************************************//** |
292 | Reads a float. It is stored in a little-endian format. |
293 | @return float read */ |
294 | UNIV_INLINE |
295 | float |
296 | mach_float_read( |
297 | /*============*/ |
298 | const byte* b) /*!< in: pointer to memory from where to read */ |
299 | MY_ATTRIBUTE((warn_unused_result)); |
300 | /*********************************************************//** |
301 | Writes a float. It is stored in a little-endian format. */ |
302 | UNIV_INLINE |
303 | void |
304 | mach_float_write( |
305 | /*=============*/ |
306 | byte* b, /*!< in: pointer to memory where to write */ |
307 | float d); /*!< in: float */ |
308 | /*********************************************************//** |
309 | Reads a ulint stored in the little-endian format. |
310 | @return unsigned long int */ |
311 | UNIV_INLINE |
312 | ulint |
313 | mach_read_from_n_little_endian( |
314 | /*===========================*/ |
315 | const byte* buf, /*!< in: from where to read */ |
316 | ulint buf_size) /*!< in: from how many bytes to read */ |
317 | MY_ATTRIBUTE((warn_unused_result)); |
318 | /*********************************************************//** |
319 | Writes a ulint in the little-endian format. */ |
320 | UNIV_INLINE |
321 | void |
322 | mach_write_to_n_little_endian( |
323 | /*==========================*/ |
324 | byte* dest, /*!< in: where to write */ |
325 | ulint dest_size, /*!< in: into how many bytes to write */ |
326 | ulint n); /*!< in: unsigned long int to write */ |
327 | /*********************************************************//** |
328 | Reads a ulint stored in the little-endian format. |
329 | @return unsigned long int */ |
330 | UNIV_INLINE |
331 | ulint |
332 | mach_read_from_2_little_endian( |
333 | /*===========================*/ |
334 | const byte* buf) /*!< in: from where to read */ |
335 | MY_ATTRIBUTE((warn_unused_result)); |
336 | /*********************************************************//** |
337 | Writes a ulint in the little-endian format. */ |
338 | UNIV_INLINE |
339 | void |
340 | mach_write_to_2_little_endian( |
341 | /*==========================*/ |
342 | byte* dest, /*!< in: where to write */ |
343 | ulint n); /*!< in: unsigned long int to write */ |
344 | /*********************************************************//** |
345 | Convert integral type from storage byte order (big endian) to |
346 | host byte order. |
347 | @return integer value */ |
348 | UNIV_INLINE |
349 | ib_uint64_t |
350 | mach_read_int_type( |
351 | /*===============*/ |
352 | const byte* src, /*!< in: where to read from */ |
353 | ulint len, /*!< in: length of src */ |
354 | ibool unsigned_type); /*!< in: signed or unsigned flag */ |
355 | |
356 | /************************************************************* |
357 | Convert a ulonglong integer from host byte order to (big-endian) |
358 | storage byte order. */ |
359 | UNIV_INLINE |
360 | void |
361 | mach_write_ulonglong( |
362 | /*=================*/ |
363 | byte* dest, /*!< in: where to write */ |
364 | ulonglong src, /*!< in: where to read from */ |
365 | ulint len, /*!< in: length of dest */ |
366 | bool usign); /*!< in: signed or unsigned flag */ |
367 | |
368 | #endif /* !UNIV_INNOCHECKSUM */ |
369 | |
370 | /** Read 1 to 4 bytes from a file page buffered in the buffer pool. |
371 | @param[in] ptr pointer where to read |
372 | @param[in] type MLOG_1BYTE, MLOG_2BYTES, or MLOG_4BYTES |
373 | @return value read */ |
374 | UNIV_INLINE |
375 | ulint |
376 | mach_read_ulint( |
377 | const byte* ptr, |
378 | mlog_id_t type) |
379 | MY_ATTRIBUTE((warn_unused_result)); |
380 | |
381 | #include "mach0data.ic" |
382 | |
383 | #endif |
384 | |