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