17
17
*****************************************************************************/
19
/**********************************************************************
19
/******************************************************************//**
20
@file include/mach0data.ic
20
21
Utilities for converting data from the database file
21
22
to the machine format.
26
27
#include "ut0mem.h"
28
/***********************************************************
29
/*******************************************************//**
29
30
The following function is used to store data in one byte. */
34
byte* b, /* in: pointer to byte where to store */
35
ulint n) /* in: ulint integer to be stored, >= 0, < 256 */
35
byte* b, /*!< in: pointer to byte where to store */
36
ulint n) /*!< in: ulint integer to be stored, >= 0, < 256 */
38
39
ut_ad(n <= 0xFFUL);
43
/************************************************************
44
The following function is used to fetch data from one byte. */
44
/********************************************************//**
45
The following function is used to fetch data from one byte.
46
@return ulint integer, >= 0, < 256 */
49
/* out: ulint integer, >= 0, < 256 */
50
const byte* b) /* in: pointer to byte */
51
const byte* b) /*!< in: pointer to byte */
53
54
return((ulint)(b[0]));
56
/***********************************************************
57
/*******************************************************//**
57
58
The following function is used to store data in two consecutive
58
59
bytes. We store the most significant byte to the lowest address. */
63
byte* b, /* in: pointer to two bytes where to store */
64
ulint n) /* in: ulint integer to be stored */
64
byte* b, /*!< in: pointer to two bytes where to store */
65
ulint n) /*!< in: ulint integer to be stored */
67
68
ut_ad(n <= 0xFFFFUL);
73
/************************************************************
74
/********************************************************//**
74
75
The following function is used to fetch data from 2 consecutive
75
bytes. The most significant byte is at the lowest address. */
76
bytes. The most significant byte is at the lowest address.
77
@return ulint integer */
80
/* out: ulint integer */
81
const byte* b) /* in: pointer to 2 bytes */
82
const byte* b) /*!< in: pointer to 2 bytes */
84
85
return( ((ulint)(b[0]) << 8)
89
/************************************************************
90
/********************************************************//**
90
91
The following function is used to convert a 16-bit data item
91
92
to the canonical format, for fast bytewise equality test
94
@return 16-bit integer in canonical format */
97
/* out: 16-bit integer in canonical format */
98
ulint n) /* in: integer in machine-dependent format */
99
ulint n) /*!< in: integer in machine-dependent format */
101
102
ut_ad(2 == sizeof ret);
102
103
mach_write_to_2((byte*) &ret, n);
105
/************************************************************
106
/********************************************************//**
106
107
The following function is used to convert a 16-bit data item
107
108
from the canonical format, for fast bytewise equality test
110
@return integer in machine-dependent format */
113
/* out: integer in machine-dependent format */
114
uint16 n) /* in: 16-bit integer in canonical format */
115
uint16 n) /*!< in: 16-bit integer in canonical format */
116
117
ut_ad(2 == sizeof n);
117
118
return(mach_read_from_2((const byte*) &n));
120
/***********************************************************
121
/*******************************************************//**
121
122
The following function is used to store data in 3 consecutive
122
123
bytes. We store the most significant byte to the lowest address. */
127
byte* b, /* in: pointer to 3 bytes where to store */
128
ulint n) /* in: ulint integer to be stored */
128
byte* b, /*!< in: pointer to 3 bytes where to store */
129
ulint n) /*!< in: ulint integer to be stored */
131
132
ut_ad(n <= 0xFFFFFFUL);
135
136
b[2] = (byte)(n);
138
/************************************************************
139
/********************************************************//**
139
140
The following function is used to fetch data from 3 consecutive
140
bytes. The most significant byte is at the lowest address. */
141
bytes. The most significant byte is at the lowest address.
142
@return ulint integer */
143
145
mach_read_from_3(
144
146
/*=============*/
145
/* out: ulint integer */
146
const byte* b) /* in: pointer to 3 bytes */
147
const byte* b) /*!< in: pointer to 3 bytes */
149
150
return( ((ulint)(b[0]) << 16)
155
/***********************************************************
156
/*******************************************************//**
156
157
The following function is used to store data in four consecutive
157
158
bytes. We store the most significant byte to the lowest address. */
162
byte* b, /* in: pointer to four bytes where to store */
163
ulint n) /* in: ulint integer to be stored */
163
byte* b, /*!< in: pointer to four bytes where to store */
164
ulint n) /*!< in: ulint integer to be stored */
173
/************************************************************
174
/********************************************************//**
174
175
The following function is used to fetch data from 4 consecutive
175
bytes. The most significant byte is at the lowest address. */
176
bytes. The most significant byte is at the lowest address.
177
@return ulint integer */
178
180
mach_read_from_4(
179
181
/*=============*/
180
/* out: ulint integer */
181
const byte* b) /* in: pointer to four bytes */
182
const byte* b) /*!< in: pointer to four bytes */
184
185
return( ((ulint)(b[0]) << 24)
191
/*************************************************************
192
/*********************************************************//**
192
193
Writes a ulint in a compressed form where the first byte codes the
193
194
length of the stored ulint. We look at the most significant bits of
194
195
the byte. If the most significant bit is zero, it means 1-byte storage,
195
196
else if the 2nd bit is 0, it means 2-byte storage, else if 3rd is 0,
196
197
it means 3-byte storage, else if 4th is 0, it means 4-byte storage,
197
else the storage is 5-byte. */
198
else the storage is 5-byte.
199
@return compressed size in bytes */
200
202
mach_write_compressed(
201
203
/*==================*/
202
/* out: compressed size in bytes */
203
byte* b, /* in: pointer to memory where to store */
204
ulint n) /* in: ulint integer (< 2^32) to be stored */
204
byte* b, /*!< in: pointer to memory where to store */
205
ulint n) /*!< in: ulint integer (< 2^32) to be stored */
227
/*************************************************************
228
Returns the size of a ulint when written in the compressed form. */
228
/*********************************************************//**
229
Returns the size of a ulint when written in the compressed form.
230
@return compressed size in bytes */
231
233
mach_get_compressed_size(
232
234
/*=====================*/
233
/* out: compressed size in bytes */
234
ulint n) /* in: ulint integer (< 2^32) to be stored */
235
ulint n) /*!< in: ulint integer (< 2^32) to be stored */
236
237
if (n < 0x80UL) {
249
/*************************************************************
250
Reads a ulint in a compressed form. */
250
/*********************************************************//**
251
Reads a ulint in a compressed form.
252
@return read integer (< 2^32) */
253
255
mach_read_compressed(
254
256
/*=================*/
255
/* out: read integer (< 2^32) */
256
const byte* b) /* in: pointer to memory from where to read */
257
const byte* b) /*!< in: pointer to memory from where to read */
278
/***********************************************************
279
/*******************************************************//**
279
280
The following function is used to store data in 8 consecutive
280
281
bytes. We store the most significant byte to the lowest address. */
285
byte* b, /* in: pointer to 8 bytes where to store */
286
dulint n) /* in: dulint integer to be stored */
286
byte* b, /*!< in: pointer to 8 bytes where to store */
287
dulint n) /*!< in: dulint integer to be stored */
291
292
mach_write_to_4(b + 4, ut_dulint_get_low(n));
294
/***********************************************************
295
/*******************************************************//**
295
296
The following function is used to store data in 8 consecutive
296
297
bytes. We store the most significant byte to the lowest address. */
301
byte* b, /* in: pointer to 8 bytes where to store */
302
ib_uint64_t n) /* in: 64-bit integer to be stored */
302
byte* b, /*!< in: pointer to 8 bytes where to store */
303
ib_uint64_t n) /*!< in: 64-bit integer to be stored */
307
308
mach_write_to_4(b + 4, (ulint) n);
310
/************************************************************
311
/********************************************************//**
311
312
The following function is used to fetch data from 8 consecutive
312
bytes. The most significant byte is at the lowest address. */
313
bytes. The most significant byte is at the lowest address.
314
@return dulint integer */
315
317
mach_read_from_8(
316
318
/*=============*/
317
/* out: dulint integer */
318
const byte* b) /* in: pointer to 8 bytes */
319
const byte* b) /*!< in: pointer to 8 bytes */
328
329
return(ut_dulint_create(high, low));
331
/************************************************************
332
/********************************************************//**
332
333
The following function is used to fetch data from 8 consecutive
333
bytes. The most significant byte is at the lowest address. */
334
bytes. The most significant byte is at the lowest address.
335
@return 64-bit integer */
338
/* out: 64-bit integer */
339
const byte* b) /* in: pointer to 8 bytes */
340
const byte* b) /*!< in: pointer to 8 bytes */
349
/***********************************************************
350
/*******************************************************//**
350
351
The following function is used to store data in 7 consecutive
351
352
bytes. We store the most significant byte to the lowest address. */
356
byte* b, /* in: pointer to 7 bytes where to store */
357
dulint n) /* in: dulint integer to be stored */
357
byte* b, /*!< in: pointer to 7 bytes where to store */
358
dulint n) /*!< in: dulint integer to be stored */
362
363
mach_write_to_4(b + 3, ut_dulint_get_low(n));
365
/************************************************************
366
/********************************************************//**
366
367
The following function is used to fetch data from 7 consecutive
367
bytes. The most significant byte is at the lowest address. */
368
bytes. The most significant byte is at the lowest address.
369
@return dulint integer */
370
372
mach_read_from_7(
371
373
/*=============*/
372
/* out: dulint integer */
373
const byte* b) /* in: pointer to 7 bytes */
374
const byte* b) /*!< in: pointer to 7 bytes */
383
384
return(ut_dulint_create(high, low));
386
/***********************************************************
387
/*******************************************************//**
387
388
The following function is used to store data in 6 consecutive
388
389
bytes. We store the most significant byte to the lowest address. */
393
byte* b, /* in: pointer to 6 bytes where to store */
394
dulint n) /* in: dulint integer to be stored */
394
byte* b, /*!< in: pointer to 6 bytes where to store */
395
dulint n) /*!< in: dulint integer to be stored */
399
400
mach_write_to_4(b + 2, ut_dulint_get_low(n));
402
/************************************************************
403
/********************************************************//**
403
404
The following function is used to fetch data from 6 consecutive
404
bytes. The most significant byte is at the lowest address. */
405
bytes. The most significant byte is at the lowest address.
406
@return dulint integer */
407
409
mach_read_from_6(
408
410
/*=============*/
409
/* out: dulint integer */
410
const byte* b) /* in: pointer to 6 bytes */
411
const byte* b) /*!< in: pointer to 6 bytes */
420
421
return(ut_dulint_create(high, low));
423
/*************************************************************
424
Writes a dulint in a compressed form (5..9 bytes). */
424
/*********************************************************//**
425
Writes a dulint in a compressed form (5..9 bytes).
426
@return size in bytes */
427
429
mach_dulint_write_compressed(
428
430
/*=========================*/
429
/* out: size in bytes */
430
byte* b, /* in: pointer to memory where to store */
431
dulint n) /* in: dulint integer to be stored */
431
byte* b, /*!< in: pointer to memory where to store */
432
dulint n) /*!< in: dulint integer to be stored */
440
441
return(size + 4);
443
/*************************************************************
444
Returns the size of a dulint when written in the compressed form. */
444
/*********************************************************//**
445
Returns the size of a dulint when written in the compressed form.
446
@return compressed size in bytes */
447
449
mach_dulint_get_compressed_size(
448
450
/*============================*/
449
/* out: compressed size in bytes */
450
dulint n) /* in: dulint integer to be stored */
451
dulint n) /*!< in: dulint integer to be stored */
452
453
return(4 + mach_get_compressed_size(ut_dulint_get_high(n)));
455
/*************************************************************
456
Reads a dulint in a compressed form. */
456
/*********************************************************//**
457
Reads a dulint in a compressed form.
458
@return read dulint */
459
461
mach_dulint_read_compressed(
460
462
/*========================*/
461
/* out: read dulint */
462
const byte* b) /* in: pointer to memory from where to read */
463
const byte* b) /*!< in: pointer to memory from where to read */
476
477
return(ut_dulint_create(high, low));
479
/*************************************************************
480
Writes a dulint in a compressed form (1..11 bytes). */
480
/*********************************************************//**
481
Writes a dulint in a compressed form (1..11 bytes).
482
@return size in bytes */
483
485
mach_dulint_write_much_compressed(
484
486
/*==============================*/
485
/* out: size in bytes */
486
byte* b, /* in: pointer to memory where to store */
487
dulint n) /* in: dulint integer to be stored */
487
byte* b, /*!< in: pointer to memory where to store */
488
dulint n) /*!< in: dulint integer to be stored */
505
/*************************************************************
506
Returns the size of a dulint when written in the compressed form. */
506
/*********************************************************//**
507
Returns the size of a dulint when written in the compressed form.
508
@return compressed size in bytes */
509
511
mach_dulint_get_much_compressed_size(
510
512
/*=================================*/
511
/* out: compressed size in bytes */
512
dulint n) /* in: dulint integer to be stored */
513
dulint n) /*!< in: dulint integer to be stored */
514
515
if (0 == ut_dulint_get_high(n)) {
515
516
return(mach_get_compressed_size(ut_dulint_get_low(n)));
519
520
+ mach_get_compressed_size(ut_dulint_get_low(n)));
522
/*************************************************************
523
Reads a dulint in a compressed form. */
523
/*********************************************************//**
524
Reads a dulint in a compressed form.
525
@return read dulint */
526
528
mach_dulint_read_much_compressed(
527
529
/*=============================*/
528
/* out: read dulint */
529
const byte* b) /* in: pointer to memory from where to read */
530
const byte* b) /*!< in: pointer to memory from where to read */
548
549
return(ut_dulint_create(high, low));
551
/*************************************************************
552
Reads a double. It is stored in a little-endian format. */
551
#ifndef UNIV_HOTBACKUP
552
/*********************************************************//**
553
Reads a double. It is stored in a little-endian format.
554
@return double read */
555
557
mach_double_read(
556
558
/*=============*/
557
/* out: double read */
558
const byte* b) /* in: pointer to memory from where to read */
559
const byte* b) /*!< in: pointer to memory from where to read */
577
/*************************************************************
578
/*********************************************************//**
578
579
Writes a double. It is stored in a little-endian format. */
581
582
mach_double_write(
582
583
/*==============*/
583
byte* b, /* in: pointer to memory where to write */
584
double d) /* in: double */
584
byte* b, /*!< in: pointer to memory where to write */
585
double d) /*!< in: double */
600
/*************************************************************
601
Reads a float. It is stored in a little-endian format. */
601
/*********************************************************//**
602
Reads a float. It is stored in a little-endian format.
603
@return float read */
606
/* out: float read */
607
const byte* b) /* in: pointer to memory from where to read */
608
const byte* b) /*!< in: pointer to memory from where to read */
626
/*************************************************************
627
/*********************************************************//**
627
628
Writes a float. It is stored in a little-endian format. */
630
631
mach_float_write(
631
632
/*=============*/
632
byte* b, /* in: pointer to memory where to write */
633
float d) /* in: float */
633
byte* b, /*!< in: pointer to memory where to write */
634
float d) /*!< in: float */
649
/*************************************************************
650
Reads a ulint stored in the little-endian format. */
650
/*********************************************************//**
651
Reads a ulint stored in the little-endian format.
652
@return unsigned long int */
653
655
mach_read_from_n_little_endian(
654
656
/*===========================*/
655
/* out: unsigned long int */
656
const byte* buf, /* in: from where to read */
657
ulint buf_size) /* in: from how many bytes to read */
657
const byte* buf, /*!< in: from where to read */
658
ulint buf_size) /*!< in: from how many bytes to read */
682
/*************************************************************
683
/*********************************************************//**
683
684
Writes a ulint in the little-endian format. */
686
687
mach_write_to_n_little_endian(
687
688
/*==========================*/
688
byte* dest, /* in: where to write */
689
ulint dest_size, /* in: into how many bytes to write */
690
ulint n) /* in: unsigned long int to write */
689
byte* dest, /*!< in: where to write */
690
ulint dest_size, /*!< in: into how many bytes to write */
691
ulint n) /*!< in: unsigned long int to write */
714
/*************************************************************
715
Reads a ulint stored in the little-endian format. */
715
/*********************************************************//**
716
Reads a ulint stored in the little-endian format.
717
@return unsigned long int */
718
720
mach_read_from_2_little_endian(
719
721
/*===========================*/
720
/* out: unsigned long int */
721
const byte* buf) /* in: from where to read */
722
const byte* buf) /*!< in: from where to read */
723
724
return((ulint)(*buf) + ((ulint)(*(buf + 1))) * 256);
726
/*************************************************************
727
/*********************************************************//**
727
728
Writes a ulint in the little-endian format. */
730
731
mach_write_to_2_little_endian(
731
732
/*==========================*/
732
byte* dest, /* in: where to write */
733
ulint n) /* in: unsigned long int to write */
733
byte* dest, /*!< in: where to write */
734
ulint n) /*!< in: unsigned long int to write */
735
736
ut_ad(n < 256 * 256);
742
743
*dest = (byte)(n & 0xFFUL);
745
/*************************************************************
746
/*********************************************************//**
746
747
Convert integral type from storage byte order (big endian) to
749
@return integer value */
750
752
mach_read_int_type(
751
753
/*===============*/
752
/* out: integer value */
753
const byte* src, /* in: where to read from */
754
ulint len, /* in: length of src */
755
ibool unsigned_type) /* in: signed or unsigned flag */
754
const byte* src, /*!< in: where to read from */
755
ulint len, /*!< in: length of src */
756
ibool unsigned_type) /*!< in: signed or unsigned flag */
757
758
/* XXX this can be optimized on big-endian machines */