1/*****************************************************************************
2
3Copyright (c) 1995, 2016, Oracle and/or its affiliates. All Rights Reserved.
4Copyright (c) 2017, MariaDB Corporation.
5
6This program is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free Software
8Foundation; version 2 of the License.
9
10This program is distributed in the hope that it will be useful, but WITHOUT
11ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License along with
15this program; if not, write to the Free Software Foundation, Inc.,
1651 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
17
18*****************************************************************************/
19
20/******************************************************************//**
21@file include/mach0data.h
22Utilities for converting data from the database file
23to the machine format.
24
25Created 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
37in the same format: ascii, big-endian, ... .
38All data in the files MUST be accessed using the functions in this
39module. */
40
41/*******************************************************//**
42The following function is used to store data in one byte. */
43UNIV_INLINE
44void
45mach_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 */
52UNIV_INLINE
53uint8_t
54mach_read_from_1(
55 const byte* b)
56 MY_ATTRIBUTE((warn_unused_result));
57/*******************************************************//**
58The following function is used to store data in two consecutive
59bytes. We store the most significant byte to the lower address. */
60UNIV_INLINE
61void
62mach_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
68bytes. 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 */
71UNIV_INLINE
72uint16_t
73mach_read_from_2(
74 const byte* b)
75 MY_ATTRIBUTE((warn_unused_result));
76
77#ifndef UNIV_INNOCHECKSUM
78/********************************************************//**
79The following function is used to convert a 16-bit data item
80to the canonical format, for fast bytewise equality test
81against memory.
82@return 16-bit integer in canonical format */
83UNIV_INLINE
84uint16
85mach_encode_2(
86/*==========*/
87 ulint n) /*!< in: integer in machine-dependent format */
88 MY_ATTRIBUTE((const));
89/********************************************************//**
90The following function is used to convert a 16-bit data item
91from the canonical format, for fast bytewise equality test
92against memory.
93@return integer in machine-dependent format */
94UNIV_INLINE
95ulint
96mach_decode_2(
97/*==========*/
98 uint16 n) /*!< in: 16-bit integer in canonical format */
99 MY_ATTRIBUTE((const));
100/*******************************************************//**
101The following function is used to store data in 3 consecutive
102bytes. We store the most significant byte to the lowest address. */
103UNIV_INLINE
104void
105mach_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
110bytes. The most significant byte is at the lowest address.
111@param[in] b pointer to 3 bytes to read
112@return 32 bit integer */
113UNIV_INLINE
114uint32_t
115mach_read_from_3(
116 const byte* b)
117 MY_ATTRIBUTE((warn_unused_result));
118/*******************************************************//**
119The following function is used to store data in four consecutive
120bytes. We store the most significant byte to the lowest address. */
121UNIV_INLINE
122void
123mach_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
128bytes. The most significant byte is at the lowest address.
129@param[in] b pointer to 4 bytes to read
130@return 32 bit integer */
131UNIV_INLINE
132uint32_t
133mach_read_from_4(
134 const byte* b)
135 MY_ATTRIBUTE((warn_unused_result));
136/*********************************************************//**
137Writes a ulint in a compressed form (1..5 bytes).
138@return stored size in bytes */
139UNIV_INLINE
140ulint
141mach_write_compressed(
142/*==================*/
143 byte* b, /*!< in: pointer to memory where to store */
144 ulint n); /*!< in: ulint integer to be stored */
145/*********************************************************//**
146Returns the size of an ulint when written in the compressed form.
147@return compressed size in bytes */
148UNIV_INLINE
149ulint
150mach_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;
156advanced by the number of bytes consumed
157@return unsigned value */
158UNIV_INLINE
159ib_uint32_t
160mach_read_next_compressed(
161 const byte** b);
162/*******************************************************//**
163The following function is used to store data in 6 consecutive
164bytes. We store the most significant byte to the lowest address. */
165UNIV_INLINE
166void
167mach_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/********************************************************//**
172The following function is used to fetch data from 6 consecutive
173bytes. The most significant byte is at the lowest address.
174@return 48-bit integer */
175UNIV_INLINE
176ib_uint64_t
177mach_read_from_6(
178/*=============*/
179 const byte* b) /*!< in: pointer to 6 bytes */
180 MY_ATTRIBUTE((warn_unused_result));
181/*******************************************************//**
182The following function is used to store data in 7 consecutive
183bytes. We store the most significant byte to the lowest address. */
184UNIV_INLINE
185void
186mach_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/********************************************************//**
191The following function is used to fetch data from 7 consecutive
192bytes. The most significant byte is at the lowest address.
193@return 56-bit integer */
194UNIV_INLINE
195ib_uint64_t
196mach_read_from_7(
197/*=============*/
198 const byte* b) /*!< in: pointer to 7 bytes */
199 MY_ATTRIBUTE((warn_unused_result));
200/*******************************************************//**
201The following function is used to store data in 8 consecutive
202bytes. We store the most significant byte to the lowest address. */
203UNIV_INLINE
204void
205mach_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/********************************************************//**
210The following function is used to fetch data from 8 consecutive
211bytes. The most significant byte is at the lowest address.
212@return 64-bit integer */
213UNIV_INLINE
214ib_uint64_t
215mach_read_from_8(
216/*=============*/
217 const byte* b) /*!< in: pointer to 8 bytes */
218 MY_ATTRIBUTE((warn_unused_result));
219/*********************************************************//**
220Writes a 64-bit integer in a compressed form (5..9 bytes).
221@return size in bytes */
222UNIV_INLINE
223ulint
224mach_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;
230advanced by the number of bytes consumed
231@return unsigned value */
232UNIV_INLINE
233ib_uint64_t
234mach_u64_read_next_compressed(
235 const byte** b);
236/*********************************************************//**
237Writes a 64-bit integer in a compressed form (1..11 bytes).
238@return size in bytes */
239UNIV_INLINE
240ulint
241mach_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/*********************************************************//**
246Reads a 64-bit integer in a compressed form.
247@return the value read */
248UNIV_INLINE
249ib_uint64_t
250mach_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;
256advanced 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 */
259ib_uint32_t
260mach_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;
265advanced 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 */
268UNIV_INLINE
269ib_uint64_t
270mach_u64_parse_compressed(
271 const byte** ptr,
272 const byte* end_ptr);
273
274/*********************************************************//**
275Reads a double. It is stored in a little-endian format.
276@return double read */
277UNIV_INLINE
278double
279mach_double_read(
280/*=============*/
281 const byte* b) /*!< in: pointer to memory from where to read */
282 MY_ATTRIBUTE((warn_unused_result));
283/*********************************************************//**
284Writes a double. It is stored in a little-endian format. */
285UNIV_INLINE
286void
287mach_double_write(
288/*==============*/
289 byte* b, /*!< in: pointer to memory where to write */
290 double d); /*!< in: double */
291/*********************************************************//**
292Reads a float. It is stored in a little-endian format.
293@return float read */
294UNIV_INLINE
295float
296mach_float_read(
297/*============*/
298 const byte* b) /*!< in: pointer to memory from where to read */
299 MY_ATTRIBUTE((warn_unused_result));
300/*********************************************************//**
301Writes a float. It is stored in a little-endian format. */
302UNIV_INLINE
303void
304mach_float_write(
305/*=============*/
306 byte* b, /*!< in: pointer to memory where to write */
307 float d); /*!< in: float */
308/*********************************************************//**
309Reads a ulint stored in the little-endian format.
310@return unsigned long int */
311UNIV_INLINE
312ulint
313mach_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/*********************************************************//**
319Writes a ulint in the little-endian format. */
320UNIV_INLINE
321void
322mach_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/*********************************************************//**
328Reads a ulint stored in the little-endian format.
329@return unsigned long int */
330UNIV_INLINE
331ulint
332mach_read_from_2_little_endian(
333/*===========================*/
334 const byte* buf) /*!< in: from where to read */
335 MY_ATTRIBUTE((warn_unused_result));
336/*********************************************************//**
337Writes a ulint in the little-endian format. */
338UNIV_INLINE
339void
340mach_write_to_2_little_endian(
341/*==========================*/
342 byte* dest, /*!< in: where to write */
343 ulint n); /*!< in: unsigned long int to write */
344/*********************************************************//**
345Convert integral type from storage byte order (big endian) to
346host byte order.
347@return integer value */
348UNIV_INLINE
349ib_uint64_t
350mach_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/*************************************************************
357Convert a ulonglong integer from host byte order to (big-endian)
358storage byte order. */
359UNIV_INLINE
360void
361mach_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 */
374UNIV_INLINE
375ulint
376mach_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