Debian Codegraph @

storage/innobase/page/zipdecompress.cc

raw
1 /*****************************************************************************
2
3 Copyright (c) 2005, 2026, Oracle and/or its affiliates.
4 Copyright (c) 2012, Facebook Inc.
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, version 2.0, as published by the
8 Free Software Foundation.
9
10 This program is designed to work with certain software (including
11 but not limited to OpenSSL) that is licensed under separate terms,
12 as designated in a particular file or component or in included license
13 documentation. The authors of MySQL hereby grant you an additional
14 permission to link the program and your derivative works with the
15 separately licensed software that they have either included with
16 the program or referenced in the documentation.
17
18 This program is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
21 for more details.
22
23 You should have received a copy of the GNU General Public License along with
24 this program; if not, write to the Free Software Foundation, Inc.,
25 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26
27 *****************************************************************************/
28
29 /** @file page/zipdecompress.cc
30 Page Decompression interface
31
32 Created June 2005 by Marko Makela
33 *******************************************************/
34
35 /** NOTE: The functions in this file should only use functions from
36 other files in library. The code in this file is used to make a library for
37 external tools. */
38
39 #include <stdarg.h>
40 #include <sys/types.h>
41 #include <zlib.h>
42
43 #include "btr0btr.h"
44 #include "mem0mem.h"
45 #include "page/zipdecompress.h"
46 #include "page0page.h"
47 #include "rem0rec.h"
48 #include "rem0wrec.h"
49
50 /* Enable some extra debugging output. This code can be enabled
51 independently of any UNIV_ debugging conditions. */
52 #if defined UNIV_DEBUG || defined UNIV_ZIP_DEBUG
53 #include <stdarg.h>
54
55 MY_ATTRIBUTE((format(printf, 1, 2)))
56 /** Report a failure to decompress or compress.
57 @return number of characters printed */
58 int page_zip_fail_func(const char *fmt, /*!< in: printf(3) format string */
59 ...) /*!< in: arguments corresponding to fmt */
60 {
61 int res;
62 va_list ap;
63
64 ut_print_timestamp(stderr);
65 fputs(" InnoDB: ", stderr);
66 va_start(ap, fmt);
67 res = vfprintf(stderr, fmt, ap);
68 va_end(ap);
69
70 return (res);
71 }
72 /** Wrapper for page_zip_fail_func()
73 @param fmt_args in: printf(3) format string and arguments */
74 #define page_zip_fail(fmt_args) page_zip_fail_func fmt_args
75 #else /* UNIV_DEBUG || UNIV_ZIP_DEBUG */
76 /** Dummy wrapper for page_zip_fail_func()
77 @param fmt_args ignored: printf(3) format string and arguments */
78 #define page_zip_fail(fmt_args) /* empty */
79 #endif /* UNIV_DEBUG || UNIV_ZIP_DEBUG */
80
81 extern "C" {
82
83 /** Allocate memory for zlib. */
84 static void *page_zip_zalloc(void *opaque, /*!< in/out: memory heap */
85 uInt items, /*!< in: number of items to allocate */
86 uInt size) /*!< in: size of an item in bytes */
87 {
88 return (mem_heap_zalloc(static_cast<mem_heap_t *>(opaque), items * size));
89 }
90
91 /** Deallocate memory for zlib. */
92 static void page_zip_free(void *opaque [[maybe_unused]], /*!< in: memory heap */
93 void *address
94 [[maybe_unused]]) /*!< in: object to free */
95 {}
96
97 } /* extern "C" */
98
99 /** Deallocate the index information initialized by page_zip_fields_decode(). */
100 static void page_zip_fields_free(
101 dict_index_t *index) /*!< in: dummy index to be freed */
102 {
103 if (index) {
104 dict_table_t *table = index->table;
105 #ifndef UNIV_HOTBACKUP
106 dict_index_zip_pad_mutex_destroy(index);
107 #endif /* !UNIV_HOTBACKUP */
108 mem_heap_free(index->heap);
109
110 dict_mem_table_free(table);
111 }
112 }
113
114 /** Configure the zlib allocator to use the given memory heap.
115 @param[in,out] stream zlib stream
116 @param[in] heap Memory heap to use */
117 void page_zip_set_alloc(void *stream, mem_heap_t *heap) {
118 z_stream *strm = static_cast<z_stream *>(stream);
119
120 strm->zalloc = page_zip_zalloc;
121 strm->zfree = page_zip_free;
122 strm->opaque = heap;
123 }
124
125 /** Populate the sparse page directory from the dense directory.
126 @return true on success, false on failure */
127 [[nodiscard]] static bool page_zip_dir_decode(
128 const page_zip_des_t *page_zip, /*!< in: dense page directory on
129 compressed page */
130 page_t *page, /*!< in: compact page with valid header;
131 out: trailer and sparse page directory
132 filled in */
133 rec_t **recs, /*!< out: dense page directory sorted by
134 ascending address (and heap_no) */
135 ulint n_dense) /*!< in: number of user records, and
136 size of recs[] */
137 {
138 ulint i;
139 ulint n_recs;
140 byte *slot;
141
142 n_recs = page_get_n_recs(page);
143
144 if (UNIV_UNLIKELY(n_recs > n_dense)) {
145 page_zip_fail(
146 ("page_zip_dir_decode 1: %lu > %lu\n", (ulong)n_recs, (ulong)n_dense));
147 return false;
148 }
149
150 /* Traverse the list of stored records in the sorting order,
151 starting from the first user record. */
152
153 slot = page + (UNIV_PAGE_SIZE - PAGE_DIR - PAGE_DIR_SLOT_SIZE);
154 UNIV_PREFETCH_RW(slot);
155
156 /* Zero out the page trailer. */
157 memset(slot + PAGE_DIR_SLOT_SIZE, 0, PAGE_DIR);
158
159 mach_write_to_2(slot, PAGE_NEW_INFIMUM);
160 slot -= PAGE_DIR_SLOT_SIZE;
161 UNIV_PREFETCH_RW(slot);
162
163 /* Initialize the sparse directory and copy the dense directory. */
164 for (i = 0; i < n_recs; i++) {
165 ulint offs = page_zip_dir_get(page_zip, i);
166
167 if (offs & PAGE_ZIP_DIR_SLOT_OWNED) {
168 mach_write_to_2(slot, offs & PAGE_ZIP_DIR_SLOT_MASK);
169 slot -= PAGE_DIR_SLOT_SIZE;
170 UNIV_PREFETCH_RW(slot);
171 }
172
173 if (UNIV_UNLIKELY((offs & PAGE_ZIP_DIR_SLOT_MASK) <
174 PAGE_ZIP_START + REC_N_NEW_EXTRA_BYTES)) {
175 page_zip_fail(("page_zip_dir_decode 2: %u %u %lx\n", (unsigned)i,
176 (unsigned)n_recs, (ulong)offs));
177 return false;
178 }
179
180 recs[i] = page + (offs & PAGE_ZIP_DIR_SLOT_MASK);
181 }
182
183 mach_write_to_2(slot, PAGE_NEW_SUPREMUM);
184 {
185 const page_dir_slot_t *last_slot =
186 page_dir_get_nth_slot(page, page_dir_get_n_slots(page) - 1);
187
188 if (UNIV_UNLIKELY(slot != last_slot)) {
189 page_zip_fail(("page_zip_dir_decode 3: %p != %p\n", (const void *)slot,
190 (const void *)last_slot));
191 return false;
192 }
193 }
194
195 /* Copy the rest of the dense directory. */
196 for (; i < n_dense; i++) {
197 ulint offs = page_zip_dir_get(page_zip, i);
198
199 if (UNIV_UNLIKELY(offs & ~PAGE_ZIP_DIR_SLOT_MASK)) {
200 page_zip_fail(("page_zip_dir_decode 4: %u %u %lx\n", (unsigned)i,
201 (unsigned)n_dense, (ulong)offs));
202 return false;
203 }
204
205 recs[i] = page + offs;
206 }
207
208 std::sort(recs, recs + n_dense);
209 return true;
210 }
211
212 /** Read the index information for the compressed page.
213 @param[in] buf index information
214 @param[in] end end of buf
215 @param[in] trx_id_col NULL for non-leaf pages; for leaf pages,
216 pointer to where to store the position of the
217 trx_id column
218 @param[in] is_spatial is spatial index or not
219 @return own: dummy index describing the page, or NULL on error */
220 static dict_index_t *page_zip_fields_decode(const byte *buf, const byte *end,
221 ulint *trx_id_col,
222 bool is_spatial) {
223 const byte *b;
224 ulint n;
225 ulint i;
226 ulint val;
227 dict_table_t *table;
228 dict_index_t *index;
229
230 /* Determine the number of fields. */
231 for (b = buf, n = 0; b < end; n++) {
232 if (*b++ & 0x80) {
233 b++; /* skip the second byte */
234 }
235 }
236
237 n--; /* n_nullable or trx_id */
238
239 if (UNIV_UNLIKELY(n > REC_MAX_N_FIELDS)) {
240 page_zip_fail(("page_zip_fields_decode: n = %lu\n", (ulong)n));
241 return (nullptr);
242 }
243
244 if (UNIV_UNLIKELY(b > end)) {
245 page_zip_fail(("page_zip_fields_decode: %p > %p\n", (const void *)b,
246 (const void *)end));
247 return (nullptr);
248 }
249
250 table = dict_mem_table_create("ZIP_DUMMY", DICT_HDR_SPACE, n, 0, 0,
251 DICT_TF_COMPACT, 0);
252 index = dict_mem_index_create("ZIP_DUMMY", "ZIP_DUMMY", DICT_HDR_SPACE, 0, n);
253 index->table = table;
254 index->n_uniq = n;
255 /* avoid ut_ad(index->cached) in dict_index_get_n_unique_in_tree */
256 index->cached = true;
257
258 /* Initialize the fields. */
259 for (b = buf, i = 0; i < n; i++) {
260 ulint mtype;
261 ulint len;
262
263 val = *b++;
264
265 if (UNIV_UNLIKELY(val & 0x80)) {
266 /* fixed length > 62 bytes */
267 val = (val & 0x7f) << 8 | *b++;
268 len = val >> 1;
269 mtype = DATA_FIXBINARY;
270 } else if (UNIV_UNLIKELY(val >= 126)) {
271 /* variable length with max > 255 bytes */
272 len = 0x7fff;
273 mtype = DATA_BINARY;
274 } else if (val <= 1) {
275 /* variable length with max <= 255 bytes */
276 len = 0;
277 mtype = DATA_BINARY;
278 } else {
279 /* fixed length < 62 bytes */
280 len = val >> 1;
281 mtype = DATA_FIXBINARY;
282 }
283
284 dict_mem_table_add_col(table, nullptr, nullptr, mtype,
285 val & 1 ? DATA_NOT_NULL : 0, len, true);
286
287 /* The is_ascending flag does not matter during decompression,
288 because we do not compare for "less than" or "greater than" */
289 dict_index_add_col(index, table, table->get_col(i), 0, true);
290 }
291
292 val = *b++;
293 if (UNIV_UNLIKELY(val & 0x80)) {
294 val = (val & 0x7f) << 8 | *b++;
295 }
296
297 /* Decode the position of the trx_id column. */
298 if (trx_id_col) {
299 if (!val) {
300 val = ULINT_UNDEFINED;
301 } else if (UNIV_UNLIKELY(val >= n)) {
302 page_zip_fields_free(index);
303 index = nullptr;
304 } else {
305 index->type = DICT_CLUSTERED;
306 }
307
308 *trx_id_col = val;
309 } else {
310 /* Decode the number of nullable fields. */
311 if (UNIV_UNLIKELY(index->n_nullable > val)) {
312 page_zip_fields_free(index);
313 index = nullptr;
314 } else {
315 index->n_nullable = val;
316 }
317 }
318
319 ut_ad(b == end);
320
321 if (is_spatial) {
322 index->type |= DICT_SPATIAL;
323 }
324
325 index->set_instant_nullable(index->n_nullable);
326 if (index->is_clustered()) {
327 index->instant_cols = index->table->has_instant_cols();
328 index->row_versions = index->table->has_row_versions();
329 }
330
331 return (index);
332 }
333
334 /** Apply the modification log to a record containing externally stored
335 columns. Do not copy the fields that are stored separately.
336 @param[in] index index
337 @param[in,out] rec record
338 @param[in] offsets rec_get_offsets(rec)
339 @param[in] trx_id_col position of of DB_TRX_ID
340 @param[in] data modification log
341 @param[in] end end of modification log
342 @return pointer to modification log, or NULL on failure */
343 static const byte *page_zip_apply_log_ext(const dict_index_t *index, rec_t *rec,
344 const ulint *offsets,
345 ulint trx_id_col, const byte *data,
346 const byte *end) {
347 ulint i;
348 ulint len;
349 byte *next_out = rec;
350
351 /* Check if there are any externally stored columns.
352 For each externally stored column, skip the
353 BTR_EXTERN_FIELD_REF. */
354
355 for (i = 0; i < rec_offs_n_fields(offsets); i++) {
356 byte *dst;
357
358 if (UNIV_UNLIKELY(i == trx_id_col)) {
359 /* Skip trx_id and roll_ptr */
360 dst = rec_get_nth_field(index, rec, offsets, i, &len);
361 if (UNIV_UNLIKELY(dst - next_out >= end - data) ||
362 UNIV_UNLIKELY(len < (DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN)) ||
363 rec_offs_nth_extern(index, offsets, i)) {
364 page_zip_fail(
365 ("page_zip_apply_log_ext:"
366 " trx_id len %lu,"
367 " %p - %p >= %p - %p\n",
368 (ulong)len, (const void *)dst, (const void *)next_out,
369 (const void *)end, (const void *)data));
370 return (nullptr);
371 }
372
373 memcpy(next_out, data, dst - next_out);
374 data += dst - next_out;
375 next_out = dst + (DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN);
376 } else if (rec_offs_nth_extern(index, offsets, i)) {
377 dst = rec_get_nth_field(index, rec, offsets, i, &len);
378 ut_ad(len >= BTR_EXTERN_FIELD_REF_SIZE);
379
380 len += dst - next_out - BTR_EXTERN_FIELD_REF_SIZE;
381
382 if (UNIV_UNLIKELY(data + len >= end)) {
383 page_zip_fail(
384 ("page_zip_apply_log_ext:"
385 " ext %p+%lu >= %p\n",
386 (const void *)data, (ulong)len, (const void *)end));
387 return (nullptr);
388 }
389
390 memcpy(next_out, data, len);
391 data += len;
392 next_out += len + BTR_EXTERN_FIELD_REF_SIZE;
393 }
394 }
395
396 /* Copy the last bytes of the record. */
397 len = rec_get_end(rec, offsets) - next_out;
398 if (UNIV_UNLIKELY(data + len >= end)) {
399 page_zip_fail(
400 ("page_zip_apply_log_ext:"
401 " last %p+%lu >= %p\n",
402 (const void *)data, (ulong)len, (const void *)end));
403 return (nullptr);
404 }
405 memcpy(next_out, data, len);
406 data += len;
407
408 return (data);
409 }
410
411 /** Apply the modification log to an uncompressed page.
412 Do not copy the fields that are stored separately.
413 @return pointer to end of modification log, or NULL on failure */
414 static const byte *page_zip_apply_log(
415 const byte *data, /*!< in: modification log */
416 ulint size, /*!< in: maximum length of the log, in bytes */
417 rec_t **recs, /*!< in: dense page directory,
418 sorted by address (indexed by
419 heap_no - PAGE_HEAP_NO_USER_LOW) */
420 ulint n_dense, /*!< in: size of recs[] */
421 ulint trx_id_col, /*!< in: column number of trx_id in the index,
422 or ULINT_UNDEFINED if none */
423 ulint heap_status,
424 /*!< in: heap_no and status bits for
425 the next record to uncompress */
426 dict_index_t *index, /*!< in: index of the page */
427 ulint *offsets) /*!< in/out: work area for
428 rec_get_offsets_reverse() */
429 {
430 const byte *const end = data + size;
431
432 for (;;) {
433 ulint val;
434 rec_t *rec;
435 ulint len;
436 ulint hs;
437
438 val = *data++;
439 if (UNIV_UNLIKELY(!val)) {
440 return (data - 1);
441 }
442 if (val & 0x80) {
443 val = (val & 0x7f) << 8 | *data++;
444 if (UNIV_UNLIKELY(!val)) {
445 page_zip_fail(
446 ("page_zip_apply_log:"
447 " invalid val %x%x\n",
448 data[-2], data[-1]));
449 return (nullptr);
450 }
451 }
452 if (UNIV_UNLIKELY(data >= end)) {
453 page_zip_fail(("page_zip_apply_log: %p >= %p\n", (const void *)data,
454 (const void *)end));
455 return (nullptr);
456 }
457 if (UNIV_UNLIKELY((val >> 1) > n_dense)) {
458 page_zip_fail(
459 ("page_zip_apply_log: %lu>>1 > %lu\n", (ulong)val, (ulong)n_dense));
460 return (nullptr);
461 }
462
463 /* Determine the heap number and status bits of the record. */
464 rec = recs[(val >> 1) - 1];
465
466 hs = ((val >> 1) + 1) << REC_HEAP_NO_SHIFT;
467 hs |= heap_status & ((1 << REC_HEAP_NO_SHIFT) - 1);
468
469 /* This may either be an old record that is being
470 overwritten (updated in place, or allocated from
471 the free list), or a new record, with the next
472 available_heap_no. */
473 if (UNIV_UNLIKELY(hs > heap_status)) {
474 page_zip_fail(
475 ("page_zip_apply_log: %lu > %lu\n", (ulong)hs, (ulong)heap_status));
476 return (nullptr);
477 } else if (hs == heap_status) {
478 /* A new record was allocated from the heap. */
479 if (UNIV_UNLIKELY(val & 1)) {
480 /* Only existing records may be cleared. */
481 page_zip_fail(
482 ("page_zip_apply_log:"
483 " attempting to create"
484 " deleted rec %lu\n",
485 (ulong)hs));
486 return (nullptr);
487 }
488 heap_status += 1 << REC_HEAP_NO_SHIFT;
489 }
490
491 mach_write_to_2(rec - REC_NEW_HEAP_NO, hs);
492
493 if (val & 1) {
494 /* Clear the data bytes of the record. */
495 mem_heap_t *heap = nullptr;
496 ulint *offs;
497 offs = rec_get_offsets(rec, index, offsets, ULINT_UNDEFINED,
498 UT_LOCATION_HERE, &heap);
499 memset(rec, 0, rec_offs_data_size(offs));
500
501 if (UNIV_LIKELY_NULL(heap)) {
502 mem_heap_free(heap);
503 }
504 continue;
505 }
506
507 static_assert(REC_STATUS_NODE_PTR == true, "REC_STATUS_NODE_PTR != true");
508 rec_get_offsets_reverse(data, index, hs & REC_STATUS_NODE_PTR, offsets);
509 rec_offs_make_valid(rec, index, offsets);
510
511 /* Copy the extra bytes (backwards). */
512 {
513 byte *start = rec_get_start(rec, offsets);
514 byte *b = rec - REC_N_NEW_EXTRA_BYTES;
515 while (b != start) {
516 *--b = *data++;
517 }
518 }
519
520 /* Copy the data bytes. */
521 if (UNIV_UNLIKELY(rec_offs_any_extern(offsets))) {
522 /* Non-leaf nodes should not contain any
523 externally stored columns. */
524 if (UNIV_UNLIKELY(hs & REC_STATUS_NODE_PTR)) {
525 page_zip_fail(
526 ("page_zip_apply_log:"
527 " %lu&REC_STATUS_NODE_PTR\n",
528 (ulong)hs));
529 return (nullptr);
530 }
531
532 data = page_zip_apply_log_ext(index, rec, offsets, trx_id_col, data, end);
533
534 if (UNIV_UNLIKELY(!data)) {
535 return (nullptr);
536 }
537 } else if (UNIV_UNLIKELY(hs & REC_STATUS_NODE_PTR)) {
538 len = rec_offs_data_size(offsets) - REC_NODE_PTR_SIZE;
539 /* Copy the data bytes, except node_ptr. */
540 if (UNIV_UNLIKELY(data + len >= end)) {
541 page_zip_fail(
542 ("page_zip_apply_log:"
543 " node_ptr %p+%lu >= %p\n",
544 (const void *)data, (ulong)len, (const void *)end));
545 return (nullptr);
546 }
547 memcpy(rec, data, len);
548 data += len;
549 } else if (UNIV_LIKELY(trx_id_col == ULINT_UNDEFINED)) {
550 len = rec_offs_data_size(offsets);
551
552 /* Copy all data bytes of
553 a record in a secondary index. */
554 if (UNIV_UNLIKELY(data + len >= end)) {
555 page_zip_fail(
556 ("page_zip_apply_log:"
557 " sec %p+%lu >= %p\n",
558 (const void *)data, (ulong)len, (const void *)end));
559 return (nullptr);
560 }
561
562 memcpy(rec, data, len);
563 data += len;
564 } else {
565 /* Skip DB_TRX_ID and DB_ROLL_PTR. */
566 ulint l = rec_get_nth_field_offs(index, offsets, trx_id_col, &len);
567 byte *b;
568
569 if (UNIV_UNLIKELY(data + l >= end) ||
570 UNIV_UNLIKELY(len < (DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN))) {
571 page_zip_fail(
572 ("page_zip_apply_log:"
573 " trx_id %p+%lu >= %p\n",
574 (const void *)data, (ulong)l, (const void *)end));
575 return (nullptr);
576 }
577
578 /* Copy any preceding data bytes. */
579 memcpy(rec, data, l);
580 data += l;
581
582 /* Copy any bytes following DB_TRX_ID, DB_ROLL_PTR. */
583 b = rec + l + (DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN);
584 len = rec_get_end(rec, offsets) - b;
585 if (UNIV_UNLIKELY(data + len >= end)) {
586 page_zip_fail(
587 ("page_zip_apply_log:"
588 " clust %p+%lu >= %p\n",
589 (const void *)data, (ulong)len, (const void *)end));
590 return (nullptr);
591 }
592 memcpy(b, data, len);
593 data += len;
594 }
595 }
596 }
597
598 /** Set the heap_no in a record, and skip the fixed-size record header
599 that is not included in the d_stream.
600 @return true on success, false if d_stream does not end at rec */
601 static bool page_zip_decompress_heap_no(
602 z_stream *d_stream, /*!< in/out: compressed page stream */
603 rec_t *rec, /*!< in/out: record */
604 ulint &heap_status) /*!< in/out: heap_no and status bits */
605 {
606 if (d_stream->next_out != rec - REC_N_NEW_EXTRA_BYTES) {
607 /* n_dense has grown since the page was last compressed. */
608 return false;
609 }
610
611 /* Skip the REC_N_NEW_EXTRA_BYTES. */
612 d_stream->next_out = rec;
613
614 /* Set heap_no and the status bits. */
615 mach_write_to_2(rec - REC_NEW_HEAP_NO, heap_status);
616 heap_status += 1 << REC_HEAP_NO_SHIFT;
617
618 /* Clear the info bits, to make sure later assertion saying
619 that this record is not instant can pass in rec_init_offsets() */
620 rec[-REC_N_NEW_EXTRA_BYTES] = 0;
621
622 return true;
623 }
624
625 /** Decompress the records of a node pointer page.
626 @return true on success, false on failure */
627 static bool page_zip_decompress_node_ptrs(
628 page_zip_des_t *page_zip, /*!< in/out: compressed page */
629 z_stream *d_stream, /*!< in/out: compressed page stream */
630 rec_t **recs, /*!< in: dense page directory
631 sorted by address */
632 ulint n_dense, /*!< in: size of recs[] */
633 dict_index_t *index, /*!< in: the index of the page */
634 ulint *offsets, /*!< in/out: temporary offsets */
635 mem_heap_t *heap) /*!< in: temporary memory heap */
636 {
637 ulint heap_status = REC_STATUS_NODE_PTR | PAGE_HEAP_NO_USER_LOW
638 << REC_HEAP_NO_SHIFT;
639 ulint slot;
640 const byte *storage;
641
642 /* Subtract the space reserved for uncompressed data. */
643 d_stream->avail_in -=
644 static_cast<uInt>(n_dense * (PAGE_ZIP_DIR_SLOT_SIZE + REC_NODE_PTR_SIZE));
645
646 /* Decompress the records in heap_no order. */
647 for (slot = 0; slot < n_dense; slot++) {
648 rec_t *rec = recs[slot];
649
650 d_stream->avail_out =
651 static_cast<uInt>(rec - REC_N_NEW_EXTRA_BYTES - d_stream->next_out);
652
653 ut_ad(d_stream->avail_out < UNIV_PAGE_SIZE - PAGE_ZIP_START - PAGE_DIR);
654 switch (inflate(d_stream, Z_SYNC_FLUSH)) {
655 case Z_STREAM_END:
656 page_zip_decompress_heap_no(d_stream, rec, heap_status);
657 goto zlib_done;
658 case Z_OK:
659 case Z_BUF_ERROR:
660 if (!d_stream->avail_out) {
661 break;
662 }
663 [[fallthrough]];
664 default:
665 page_zip_fail(
666 ("page_zip_decompress_node_ptrs:"
667 " 1 inflate(Z_SYNC_FLUSH)=%s\n",
668 d_stream->msg));
669 goto zlib_error;
670 }
671
672 if (!page_zip_decompress_heap_no(d_stream, rec, heap_status)) {
673 ut_d(ut_error);
674 }
675
676 /* Read the offsets. The status bits are needed here. */
677 offsets = rec_get_offsets(rec, index, offsets, ULINT_UNDEFINED,
678 UT_LOCATION_HERE, &heap);
679
680 /* Non-leaf nodes should not have any externally
681 stored columns. */
682 ut_ad(!rec_offs_any_extern(offsets));
683
684 /* Decompress the data bytes, except node_ptr. */
685 d_stream->avail_out =
686 static_cast<uInt>(rec_offs_data_size(offsets) - REC_NODE_PTR_SIZE);
687
688 switch (inflate(d_stream, Z_SYNC_FLUSH)) {
689 case Z_STREAM_END:
690 goto zlib_done;
691 case Z_OK:
692 case Z_BUF_ERROR:
693 if (!d_stream->avail_out) {
694 break;
695 }
696 [[fallthrough]];
697 default:
698 page_zip_fail(
699 ("page_zip_decompress_node_ptrs:"
700 " 2 inflate(Z_SYNC_FLUSH)=%s\n",
701 d_stream->msg));
702 goto zlib_error;
703 }
704
705 /* Clear the node pointer in case the record
706 will be deleted and the space will be reallocated
707 to a smaller record. */
708 memset(d_stream->next_out, 0, REC_NODE_PTR_SIZE);
709 d_stream->next_out += REC_NODE_PTR_SIZE;
710
711 ut_ad(d_stream->next_out == rec_get_end(rec, offsets));
712 }
713
714 /* Decompress any trailing garbage, in case the last record was
715 allocated from an originally longer space on the free list. */
716 d_stream->avail_out =
717 static_cast<uInt>(page_header_get_field(page_zip->data, PAGE_HEAP_TOP) -
718 page_offset(d_stream->next_out));
719 if (UNIV_UNLIKELY(d_stream->avail_out >
720 UNIV_PAGE_SIZE - PAGE_ZIP_START - PAGE_DIR)) {
721 page_zip_fail(
722 ("page_zip_decompress_node_ptrs:"
723 " avail_out = %u\n",
724 d_stream->avail_out));
725 goto zlib_error;
726 }
727
728 if (UNIV_UNLIKELY(inflate(d_stream, Z_FINISH) != Z_STREAM_END)) {
729 page_zip_fail(
730 ("page_zip_decompress_node_ptrs:"
731 " inflate(Z_FINISH)=%s\n",
732 d_stream->msg));
733 zlib_error:
734 inflateEnd(d_stream);
735 return false;
736 }
737
738 /* Note that d_stream->avail_out > 0 may hold here
739 if the modification log is nonempty. */
740
741 zlib_done:
742 if (UNIV_UNLIKELY(inflateEnd(d_stream) != Z_OK)) {
743 ut_error;
744 }
745
746 {
747 page_t *page = page_align(d_stream->next_out);
748
749 /* Clear the unused heap space on the uncompressed page. */
750 memset(d_stream->next_out, 0,
751 page_dir_get_nth_slot(page, page_dir_get_n_slots(page) - 1) -
752 d_stream->next_out);
753 }
754
755 #ifdef UNIV_DEBUG
756 page_zip->m_start = PAGE_DATA + d_stream->total_in;
757 #endif /* UNIV_DEBUG */
758
759 /* Apply the modification log. */
760 {
761 const byte *mod_log_ptr;
762 mod_log_ptr = page_zip_apply_log(d_stream->next_in, d_stream->avail_in + 1,
763 recs, n_dense, ULINT_UNDEFINED,
764 heap_status, index, offsets);
765
766 if (UNIV_UNLIKELY(!mod_log_ptr)) {
767 return false;
768 }
769 page_zip->m_end = mod_log_ptr - page_zip->data;
770 page_zip->m_nonempty = mod_log_ptr != d_stream->next_in;
771 }
772
773 if (UNIV_UNLIKELY(page_zip_get_trailer_len(page_zip, index->is_clustered()) +
774 page_zip->m_end >=
775 page_zip_get_size(page_zip))) {
776 page_zip_fail(
777 ("page_zip_decompress_node_ptrs:"
778 " %lu + %lu >= %lu, %lu\n",
779 (ulong)page_zip_get_trailer_len(page_zip, index->is_clustered()),
780 (ulong)page_zip->m_end, (ulong)page_zip_get_size(page_zip),
781 (ulong)index->is_clustered()));
782 return false;
783 }
784
785 /* Restore the uncompressed columns in heap_no order. */
786 storage = page_zip_dir_start_low(page_zip, n_dense);
787
788 for (slot = 0; slot < n_dense; slot++) {
789 rec_t *rec = recs[slot];
790
791 offsets = rec_get_offsets(rec, index, offsets, ULINT_UNDEFINED,
792 UT_LOCATION_HERE, &heap);
793 /* Non-leaf nodes should not have any externally
794 stored columns. */
795 ut_ad(!rec_offs_any_extern(offsets));
796 storage -= REC_NODE_PTR_SIZE;
797
798 memcpy(rec_get_end(rec, offsets) - REC_NODE_PTR_SIZE, storage,
799 REC_NODE_PTR_SIZE);
800 }
801
802 return true;
803 }
804
805 /** Decompress the records of a leaf node of a secondary index.
806 @return true on success, false on failure */
807 static bool page_zip_decompress_sec(
808 page_zip_des_t *page_zip, /*!< in/out: compressed page */
809 z_stream *d_stream, /*!< in/out: compressed page stream */
810 rec_t **recs, /*!< in: dense page directory
811 sorted by address */
812 ulint n_dense, /*!< in: size of recs[] */
813 dict_index_t *index, /*!< in: the index of the page */
814 ulint *offsets) /*!< in/out: temporary offsets */
815 {
816 ulint heap_status = REC_STATUS_ORDINARY | PAGE_HEAP_NO_USER_LOW
817 << REC_HEAP_NO_SHIFT;
818 ulint slot;
819
820 ut_a(!index->is_clustered());
821
822 /* Subtract the space reserved for uncompressed data. */
823 d_stream->avail_in -= static_cast<uint>(n_dense * PAGE_ZIP_DIR_SLOT_SIZE);
824
825 for (slot = 0; slot < n_dense; slot++) {
826 rec_t *rec = recs[slot];
827
828 /* Decompress everything up to this record. */
829 d_stream->avail_out =
830 static_cast<uint>(rec - REC_N_NEW_EXTRA_BYTES - d_stream->next_out);
831
832 if (UNIV_LIKELY(d_stream->avail_out)) {
833 switch (inflate(d_stream, Z_SYNC_FLUSH)) {
834 case Z_STREAM_END:
835 page_zip_decompress_heap_no(d_stream, rec, heap_status);
836 goto zlib_done;
837 case Z_OK:
838 case Z_BUF_ERROR:
839 if (!d_stream->avail_out) {
840 break;
841 }
842 [[fallthrough]];
843 default:
844 page_zip_fail(
845 ("page_zip_decompress_sec:"
846 " inflate(Z_SYNC_FLUSH)=%s\n",
847 d_stream->msg));
848 goto zlib_error;
849 }
850 }
851
852 if (!page_zip_decompress_heap_no(d_stream, rec, heap_status)) {
853 ut_d(ut_error);
854 }
855 }
856
857 /* Decompress the data of the last record and any trailing garbage,
858 in case the last record was allocated from an originally longer space
859 on the free list. */
860 d_stream->avail_out =
861 static_cast<uInt>(page_header_get_field(page_zip->data, PAGE_HEAP_TOP) -
862 page_offset(d_stream->next_out));
863 if (UNIV_UNLIKELY(d_stream->avail_out >
864 UNIV_PAGE_SIZE - PAGE_ZIP_START - PAGE_DIR)) {
865 page_zip_fail(
866 ("page_zip_decompress_sec:"
867 " avail_out = %u\n",
868 d_stream->avail_out));
869 goto zlib_error;
870 }
871
872 if (UNIV_UNLIKELY(inflate(d_stream, Z_FINISH) != Z_STREAM_END)) {
873 page_zip_fail(
874 ("page_zip_decompress_sec:"
875 " inflate(Z_FINISH)=%s\n",
876 d_stream->msg));
877 zlib_error:
878 inflateEnd(d_stream);
879 return false;
880 }
881
882 /* Note that d_stream->avail_out > 0 may hold here
883 if the modification log is nonempty. */
884
885 zlib_done:
886 if (UNIV_UNLIKELY(inflateEnd(d_stream) != Z_OK)) {
887 ut_error;
888 }
889
890 {
891 page_t *page = page_align(d_stream->next_out);
892
893 /* Clear the unused heap space on the uncompressed page. */
894 memset(d_stream->next_out, 0,
895 page_dir_get_nth_slot(page, page_dir_get_n_slots(page) - 1) -
896 d_stream->next_out);
897 }
898
899 #ifdef UNIV_DEBUG
900 page_zip->m_start = PAGE_DATA + d_stream->total_in;
901 #endif /* UNIV_DEBUG */
902
903 /* Apply the modification log. */
904 {
905 const byte *mod_log_ptr;
906 mod_log_ptr = page_zip_apply_log(d_stream->next_in, d_stream->avail_in + 1,
907 recs, n_dense, ULINT_UNDEFINED,
908 heap_status, index, offsets);
909
910 if (UNIV_UNLIKELY(!mod_log_ptr)) {
911 return false;
912 }
913 page_zip->m_end = mod_log_ptr - page_zip->data;
914 page_zip->m_nonempty = mod_log_ptr != d_stream->next_in;
915 }
916
917 if (UNIV_UNLIKELY(page_zip_get_trailer_len(page_zip, false) +
918 page_zip->m_end >=
919 page_zip_get_size(page_zip))) {
920 page_zip_fail(("page_zip_decompress_sec: %lu + %lu >= %lu\n",
921 (ulong)page_zip_get_trailer_len(page_zip, false),
922 (ulong)page_zip->m_end, (ulong)page_zip_get_size(page_zip)));
923 return false;
924 }
925
926 /* There are no uncompressed columns on leaf pages of
927 secondary indexes. */
928
929 return true;
930 }
931
932 /** Initialize the REC_N_NEW_EXTRA_BYTES of each record.
933 @return true on success, false on failure */
934 static bool page_zip_set_extra_bytes(
935 const page_zip_des_t *page_zip, /*!< in: compressed page */
936 page_t *page, /*!< in/out: uncompressed page */
937 ulint info_bits) /*!< in: REC_INFO_MIN_REC_FLAG or 0 */
938 {
939 ulint n;
940 ulint i;
941 ulint n_owned = 1;
942 ulint offs;
943 rec_t *rec;
944
945 n = page_get_n_recs(page);
946 rec = page + PAGE_NEW_INFIMUM;
947
948 for (i = 0; i < n; i++) {
949 offs = page_zip_dir_get(page_zip, i);
950
951 if (offs & PAGE_ZIP_DIR_SLOT_DEL) {
952 info_bits |= REC_INFO_DELETED_FLAG;
953 }
954 if (UNIV_UNLIKELY(offs & PAGE_ZIP_DIR_SLOT_OWNED)) {
955 info_bits |= n_owned;
956 n_owned = 1;
957 } else {
958 n_owned++;
959 }
960 offs &= PAGE_ZIP_DIR_SLOT_MASK;
961 if (UNIV_UNLIKELY(offs < PAGE_ZIP_START + REC_N_NEW_EXTRA_BYTES)) {
962 page_zip_fail(
963 ("page_zip_set_extra_bytes 1:"
964 " %u %u %lx\n",
965 (unsigned)i, (unsigned)n, (ulong)offs));
966 return false;
967 }
968
969 rec_set_next_offs_new(rec, offs);
970 rec = page + offs;
971 rec[-REC_N_NEW_EXTRA_BYTES] = (byte)info_bits;
972 info_bits = 0;
973 }
974
975 /* Set the next pointer of the last user record. */
976 rec_set_next_offs_new(rec, PAGE_NEW_SUPREMUM);
977
978 /* Set n_owned of the supremum record. */
979 page[PAGE_NEW_SUPREMUM - REC_N_NEW_EXTRA_BYTES] = (byte)n_owned;
980
981 /* The dense directory excludes the infimum and supremum records. */
982 n = page_dir_get_n_heap(page) - PAGE_HEAP_NO_USER_LOW;
983
984 if (i >= n) {
985 if (UNIV_LIKELY(i == n)) {
986 return true;
987 }
988
989 page_zip_fail(
990 ("page_zip_set_extra_bytes 2: %u != %u\n", (unsigned)i, (unsigned)n));
991 return false;
992 }
993
994 offs = page_zip_dir_get(page_zip, i);
995
996 /* Set the extra bytes of deleted records on the free list. */
997 for (;;) {
998 if (UNIV_UNLIKELY(!offs) || UNIV_UNLIKELY(offs & ~PAGE_ZIP_DIR_SLOT_MASK)) {
999 page_zip_fail(("page_zip_set_extra_bytes 3: %lx\n", (ulong)offs));
1000 return false;
1001 }
1002
1003 rec = page + offs;
1004 rec[-REC_N_NEW_EXTRA_BYTES] = 0; /* info_bits and n_owned */
1005
1006 if (++i == n) {
1007 break;
1008 }
1009
1010 offs = page_zip_dir_get(page_zip, i);
1011 rec_set_next_offs_new(rec, offs);
1012 }
1013
1014 /* Terminate the free list. */
1015 rec[-REC_N_NEW_EXTRA_BYTES] = 0; /* info_bits and n_owned */
1016 rec_set_next_offs_new(rec, 0);
1017
1018 return true;
1019 }
1020
1021 /** Decompress a record of a leaf node of a clustered index that contains
1022 externally stored columns.
1023 @param[in,out] d_stream compressed page stream
1024 @param[in] index index
1025 @param[in,out] rec record
1026 @param[in] offsets rec_get_offsets(rec)
1027 @param[in] trx_id_col position of of DB_TRX_ID
1028 @return true on success */
1029 static bool page_zip_decompress_clust_ext(z_stream *d_stream,
1030 const dict_index_t *index, rec_t *rec,
1031 const ulint *offsets,
1032 ulint trx_id_col) {
1033 ulint i;
1034
1035 for (i = 0; i < rec_offs_n_fields(offsets); i++) {
1036 ulint len;
1037 byte *dst;
1038
1039 if (UNIV_UNLIKELY(i == trx_id_col)) {
1040 /* Skip trx_id and roll_ptr */
1041 dst = rec_get_nth_field(index, rec, offsets, i, &len);
1042 if (UNIV_UNLIKELY(len < DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN)) {
1043 page_zip_fail(
1044 ("page_zip_decompress_clust_ext:"
1045 " len[%lu] = %lu\n",
1046 (ulong)i, (ulong)len));
1047 return false;
1048 }
1049
1050 if (rec_offs_nth_extern(index, offsets, i)) {
1051 page_zip_fail(
1052 ("page_zip_decompress_clust_ext:"
1053 " DB_TRX_ID at %lu is ext\n",
1054 (ulong)i));
1055 return false;
1056 }
1057
1058 d_stream->avail_out = static_cast<uInt>(dst - d_stream->next_out);
1059
1060 switch (inflate(d_stream, Z_SYNC_FLUSH)) {
1061 case Z_STREAM_END:
1062 case Z_OK:
1063 case Z_BUF_ERROR:
1064 if (!d_stream->avail_out) {
1065 break;
1066 }
1067 [[fallthrough]];
1068 default:
1069 page_zip_fail(
1070 ("page_zip_decompress_clust_ext:"
1071 " 1 inflate(Z_SYNC_FLUSH)=%s\n",
1072 d_stream->msg));
1073 return false;
1074 }
1075
1076 ut_ad(d_stream->next_out == dst);
1077
1078 /* Clear DB_TRX_ID and DB_ROLL_PTR in order to
1079 avoid uninitialized bytes in case the record
1080 is affected by page_zip_apply_log(). */
1081 memset(dst, 0, DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN);
1082
1083 d_stream->next_out += DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN;
1084 } else if (rec_offs_nth_extern(index, offsets, i)) {
1085 dst = rec_get_nth_field(index, rec, offsets, i, &len);
1086 ut_ad(len >= BTR_EXTERN_FIELD_REF_SIZE);
1087 dst += len - BTR_EXTERN_FIELD_REF_SIZE;
1088
1089 d_stream->avail_out = static_cast<uInt>(dst - d_stream->next_out);
1090 switch (inflate(d_stream, Z_SYNC_FLUSH)) {
1091 case Z_STREAM_END:
1092 case Z_OK:
1093 case Z_BUF_ERROR:
1094 if (!d_stream->avail_out) {
1095 break;
1096 }
1097 [[fallthrough]];
1098 default:
1099 page_zip_fail(
1100 ("page_zip_decompress_clust_ext:"
1101 " 2 inflate(Z_SYNC_FLUSH)=%s\n",
1102 d_stream->msg));
1103 return false;
1104 }
1105
1106 ut_ad(d_stream->next_out == dst);
1107
1108 /* Clear the BLOB pointer in case
1109 the record will be deleted and the
1110 space will not be reused. Note that
1111 the final initialization of the BLOB
1112 pointers (copying from "externs"
1113 or clearing) will have to take place
1114 only after the page modification log
1115 has been applied. Otherwise, we
1116 could end up with an uninitialized
1117 BLOB pointer when a record is deleted,
1118 reallocated and deleted. */
1119 memset(d_stream->next_out, 0, BTR_EXTERN_FIELD_REF_SIZE);
1120 d_stream->next_out += BTR_EXTERN_FIELD_REF_SIZE;
1121 }
1122 }
1123
1124 return true;
1125 }
1126
1127 /** Compress the records of a leaf node of a clustered index.
1128 @return true on success, false on failure */
1129 static bool page_zip_decompress_clust(
1130 page_zip_des_t *page_zip, /*!< in/out: compressed page */
1131 z_stream *d_stream, /*!< in/out: compressed page stream */
1132 rec_t **recs, /*!< in: dense page directory
1133 sorted by address */
1134 ulint n_dense, /*!< in: size of recs[] */
1135 dict_index_t *index, /*!< in: the index of the page */
1136 ulint trx_id_col, /*!< index of the trx_id column */
1137 ulint *offsets, /*!< in/out: temporary offsets */
1138 mem_heap_t *heap) /*!< in: temporary memory heap */
1139 {
1140 int err;
1141 ulint slot;
1142 ulint heap_status = REC_STATUS_ORDINARY | PAGE_HEAP_NO_USER_LOW
1143 << REC_HEAP_NO_SHIFT;
1144 const byte *storage;
1145 const byte *externs;
1146
1147 ut_a(index->is_clustered());
1148
1149 /* Subtract the space reserved for uncompressed data. */
1150 d_stream->avail_in -=
1151 static_cast<uInt>(n_dense) * (PAGE_ZIP_CLUST_LEAF_SLOT_SIZE);
1152
1153 /* Decompress the records in heap_no order. */
1154 for (slot = 0; slot < n_dense; slot++) {
1155 rec_t *rec = recs[slot];
1156
1157 d_stream->avail_out =
1158 static_cast<uInt>(rec - REC_N_NEW_EXTRA_BYTES - d_stream->next_out);
1159
1160 ut_ad(d_stream->avail_out < UNIV_PAGE_SIZE - PAGE_ZIP_START - PAGE_DIR);
1161 err = inflate(d_stream, Z_SYNC_FLUSH);
1162 switch (err) {
1163 case Z_STREAM_END:
1164 page_zip_decompress_heap_no(d_stream, rec, heap_status);
1165 goto zlib_done;
1166 case Z_OK:
1167 case Z_BUF_ERROR:
1168 if (UNIV_LIKELY(!d_stream->avail_out)) {
1169 break;
1170 }
1171 [[fallthrough]];
1172 default:
1173 page_zip_fail(
1174 ("page_zip_decompress_clust:"
1175 " 1 inflate(Z_SYNC_FLUSH)=%s\n",
1176 d_stream->msg));
1177 goto zlib_error;
1178 }
1179
1180 if (!page_zip_decompress_heap_no(d_stream, rec, heap_status)) {
1181 ut_d(ut_error);
1182 }
1183
1184 /* Read the offsets. The status bits are needed here. */
1185 offsets = rec_get_offsets(rec, index, offsets, ULINT_UNDEFINED,
1186 UT_LOCATION_HERE, &heap);
1187
1188 /* This is a leaf page in a clustered index. */
1189
1190 /* Check if there are any externally stored columns.
1191 For each externally stored column, restore the
1192 BTR_EXTERN_FIELD_REF separately. */
1193
1194 if (rec_offs_any_extern(offsets)) {
1195 if (UNIV_UNLIKELY(!page_zip_decompress_clust_ext(d_stream, index, rec,
1196 offsets, trx_id_col))) {
1197 goto zlib_error;
1198 }
1199 } else {
1200 /* Skip trx_id and roll_ptr */
1201 ulint len;
1202 byte *dst = rec_get_nth_field(index, rec, offsets, trx_id_col, &len);
1203 if (UNIV_UNLIKELY(len < DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN)) {
1204 page_zip_fail(
1205 ("page_zip_decompress_clust:"
1206 " len = %lu\n",
1207 (ulong)len));
1208 goto zlib_error;
1209 }
1210
1211 d_stream->avail_out = static_cast<uInt>(dst - d_stream->next_out);
1212
1213 switch (inflate(d_stream, Z_SYNC_FLUSH)) {
1214 case Z_STREAM_END:
1215 case Z_OK:
1216 case Z_BUF_ERROR:
1217 if (!d_stream->avail_out) {
1218 break;
1219 }
1220 [[fallthrough]];
1221 default:
1222 page_zip_fail(
1223 ("page_zip_decompress_clust:"
1224 " 2 inflate(Z_SYNC_FLUSH)=%s\n",
1225 d_stream->msg));
1226 goto zlib_error;
1227 }
1228
1229 ut_ad(d_stream->next_out == dst);
1230
1231 /* Clear DB_TRX_ID and DB_ROLL_PTR in order to
1232 avoid uninitialized bytes in case the record
1233 is affected by page_zip_apply_log(). */
1234 memset(dst, 0, DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN);
1235
1236 d_stream->next_out += DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN;
1237 }
1238
1239 /* Decompress the last bytes of the record. */
1240 d_stream->avail_out =
1241 static_cast<uInt>(rec_get_end(rec, offsets) - d_stream->next_out);
1242
1243 switch (inflate(d_stream, Z_SYNC_FLUSH)) {
1244 case Z_STREAM_END:
1245 case Z_OK:
1246 case Z_BUF_ERROR:
1247 if (!d_stream->avail_out) {
1248 break;
1249 }
1250 [[fallthrough]];
1251 default:
1252 page_zip_fail(
1253 ("page_zip_decompress_clust:"
1254 " 3 inflate(Z_SYNC_FLUSH)=%s\n",
1255 d_stream->msg));
1256 goto zlib_error;
1257 }
1258 }
1259
1260 /* Decompress any trailing garbage, in case the last record was
1261 allocated from an originally longer space on the free list. */
1262 d_stream->avail_out =
1263 static_cast<uInt>(page_header_get_field(page_zip->data, PAGE_HEAP_TOP) -
1264 page_offset(d_stream->next_out));
1265 if (UNIV_UNLIKELY(d_stream->avail_out >
1266 UNIV_PAGE_SIZE - PAGE_ZIP_START - PAGE_DIR)) {
1267 page_zip_fail(
1268 ("page_zip_decompress_clust:"
1269 " avail_out = %u\n",
1270 d_stream->avail_out));
1271 goto zlib_error;
1272 }
1273
1274 if (UNIV_UNLIKELY(inflate(d_stream, Z_FINISH) != Z_STREAM_END)) {
1275 page_zip_fail(
1276 ("page_zip_decompress_clust:"
1277 " inflate(Z_FINISH)=%s\n",
1278 d_stream->msg));
1279 zlib_error:
1280 inflateEnd(d_stream);
1281 return false;
1282 }
1283
1284 /* Note that d_stream->avail_out > 0 may hold here
1285 if the modification log is nonempty. */
1286
1287 zlib_done:
1288 if (UNIV_UNLIKELY(inflateEnd(d_stream) != Z_OK)) {
1289 ut_error;
1290 }
1291
1292 {
1293 page_t *page = page_align(d_stream->next_out);
1294
1295 /* Clear the unused heap space on the uncompressed page. */
1296 memset(d_stream->next_out, 0,
1297 page_dir_get_nth_slot(page, page_dir_get_n_slots(page) - 1) -
1298 d_stream->next_out);
1299 }
1300
1301 #ifdef UNIV_DEBUG
1302 page_zip->m_start = PAGE_DATA + d_stream->total_in;
1303 #endif /* UNIV_DEBUG */
1304
1305 /* Apply the modification log. */
1306 {
1307 const byte *mod_log_ptr;
1308 mod_log_ptr =
1309 page_zip_apply_log(d_stream->next_in, d_stream->avail_in + 1, recs,
1310 n_dense, trx_id_col, heap_status, index, offsets);
1311
1312 if (UNIV_UNLIKELY(!mod_log_ptr)) {
1313 return false;
1314 }
1315 page_zip->m_end = mod_log_ptr - page_zip->data;
1316 page_zip->m_nonempty = mod_log_ptr != d_stream->next_in;
1317 }
1318
1319 if (UNIV_UNLIKELY(page_zip_get_trailer_len(page_zip, true) +
1320 page_zip->m_end >=
1321 page_zip_get_size(page_zip))) {
1322 page_zip_fail(("page_zip_decompress_clust: %lu + %lu >= %lu\n",
1323 (ulong)page_zip_get_trailer_len(page_zip, true),
1324 (ulong)page_zip->m_end, (ulong)page_zip_get_size(page_zip)));
1325 return false;
1326 }
1327
1328 storage = page_zip_dir_start_low(page_zip, n_dense);
1329
1330 externs = storage - n_dense * (DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN);
1331
1332 /* Restore the uncompressed columns in heap_no order. */
1333
1334 for (slot = 0; slot < n_dense; slot++) {
1335 ulint i;
1336 ulint len;
1337 byte *dst;
1338 rec_t *rec = recs[slot];
1339 auto exists = !page_zip_dir_find_free(page_zip, page_offset(rec));
1340 offsets = rec_get_offsets(rec, index, offsets, ULINT_UNDEFINED,
1341 UT_LOCATION_HERE, &heap);
1342
1343 dst = rec_get_nth_field(index, rec, offsets, trx_id_col, &len);
1344 ut_ad(len >= DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN);
1345 storage -= DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN;
1346 memcpy(dst, storage, DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN);
1347
1348 /* Check if there are any externally stored
1349 columns in this record. For each externally
1350 stored column, restore or clear the
1351 BTR_EXTERN_FIELD_REF. */
1352 if (!rec_offs_any_extern(offsets)) {
1353 continue;
1354 }
1355
1356 for (i = 0; i < rec_offs_n_fields(offsets); i++) {
1357 if (!rec_offs_nth_extern(index, offsets, i)) {
1358 continue;
1359 }
1360 dst = rec_get_nth_field(index, rec, offsets, i, &len);
1361
1362 if (UNIV_UNLIKELY(len < BTR_EXTERN_FIELD_REF_SIZE)) {
1363 page_zip_fail(
1364 ("page_zip_decompress_clust:"
1365 " %lu < 20\n",
1366 (ulong)len));
1367 return false;
1368 }
1369
1370 dst += len - BTR_EXTERN_FIELD_REF_SIZE;
1371
1372 if (UNIV_LIKELY(exists)) {
1373 /* Existing record:
1374 restore the BLOB pointer */
1375 externs -= BTR_EXTERN_FIELD_REF_SIZE;
1376
1377 if (UNIV_UNLIKELY(externs < page_zip->data + page_zip->m_end)) {
1378 page_zip_fail(
1379 ("page_zip_"
1380 "decompress_clust:"
1381 " %p < %p + %lu\n",
1382 (const void *)externs, (const void *)page_zip->data,
1383 (ulong)page_zip->m_end));
1384 return false;
1385 }
1386
1387 memcpy(dst, externs, BTR_EXTERN_FIELD_REF_SIZE);
1388
1389 page_zip->n_blobs++;
1390 } else {
1391 /* Deleted record:
1392 clear the BLOB pointer */
1393 memset(dst, 0, BTR_EXTERN_FIELD_REF_SIZE);
1394 }
1395 }
1396 }
1397
1398 return true;
1399 }
1400
1401 /** Decompress a page. This function should tolerate errors on the compressed
1402 page. Instead of letting assertions fail, it will return false if an
1403 inconsistency is detected.
1404 @return true on success, false on failure */
1405 bool page_zip_decompress_low(
1406 page_zip_des_t *page_zip, /*!< in: data, ssize;
1407 out: m_start, m_end, m_nonempty, n_blobs */
1408 page_t *page, /*!< out: uncompressed page, may be trashed */
1409 bool all) /*!< in: true=decompress the whole page;
1410 false=verify but do not copy some
1411 page header fields that should not change
1412 after page creation */
1413 {
1414 z_stream d_stream;
1415 dict_index_t *index = nullptr;
1416 rec_t **recs; /*!< dense page directory, sorted by address */
1417 ulint n_dense; /* number of user records on the page */
1418 ulint trx_id_col = ULINT_UNDEFINED;
1419 mem_heap_t *heap;
1420 ulint *offsets;
1421
1422 ut_ad(page_zip_simple_validate(page_zip));
1423 UNIV_MEM_ASSERT_W(page, UNIV_PAGE_SIZE);
1424 UNIV_MEM_ASSERT_RW(page_zip->data, page_zip_get_size(page_zip));
1425
1426 /* The dense directory excludes the infimum and supremum records. */
1427 n_dense = page_dir_get_n_heap(page_zip->data) - PAGE_HEAP_NO_USER_LOW;
1428 if (UNIV_UNLIKELY(n_dense * PAGE_ZIP_DIR_SLOT_SIZE >=
1429 page_zip_get_size(page_zip))) {
1430 page_zip_fail(("page_zip_decompress 1: %lu %lu\n", (ulong)n_dense,
1431 (ulong)page_zip_get_size(page_zip)));
1432 return false;
1433 }
1434
1435 heap = mem_heap_create(n_dense * (3 * sizeof *recs) + UNIV_PAGE_SIZE,
1436 UT_LOCATION_HERE);
1437
1438 recs = static_cast<rec_t **>(mem_heap_alloc(heap, n_dense * sizeof *recs));
1439
1440 if (all) {
1441 /* Copy the page header. */
1442 memcpy(page, page_zip->data, PAGE_DATA);
1443 } else {
1444 /* Check that the bytes that we skip are identical. */
1445 #if defined UNIV_DEBUG || defined UNIV_ZIP_DEBUG
1446 ut_a(!memcmp(FIL_PAGE_TYPE + page, FIL_PAGE_TYPE + page_zip->data,
1447 PAGE_HEADER - FIL_PAGE_TYPE));
1448 ut_a(!memcmp(PAGE_HEADER + PAGE_LEVEL + page,
1449 PAGE_HEADER + PAGE_LEVEL + page_zip->data,
1450 PAGE_DATA - (PAGE_HEADER + PAGE_LEVEL)));
1451 #endif /* UNIV_DEBUG || UNIV_ZIP_DEBUG */
1452
1453 /* Copy the mutable parts of the page header. */
1454 memcpy(page, page_zip->data, FIL_PAGE_TYPE);
1455 memcpy(PAGE_HEADER + page, PAGE_HEADER + page_zip->data,
1456 PAGE_LEVEL - PAGE_N_DIR_SLOTS);
1457
1458 #if defined UNIV_DEBUG || defined UNIV_ZIP_DEBUG
1459 /* Check that the page headers match after copying. */
1460 ut_a(!memcmp(page, page_zip->data, PAGE_DATA));
1461 #endif /* UNIV_DEBUG || UNIV_ZIP_DEBUG */
1462 }
1463
1464 #ifdef UNIV_ZIP_DEBUG
1465 /* Clear the uncompressed page, except the header. */
1466 memset(PAGE_DATA + page, 0x55, UNIV_PAGE_SIZE - PAGE_DATA);
1467 #endif /* UNIV_ZIP_DEBUG */
1468 UNIV_MEM_INVALID(PAGE_DATA + page, UNIV_PAGE_SIZE - PAGE_DATA);
1469
1470 /* Copy the page directory. */
1471 if (UNIV_UNLIKELY(!page_zip_dir_decode(page_zip, page, recs, n_dense))) {
1472 zlib_error:
1473 mem_heap_free(heap);
1474 return false;
1475 }
1476
1477 /* Copy the infimum and supremum records. */
1478 memcpy(page + (PAGE_NEW_INFIMUM - REC_N_NEW_EXTRA_BYTES), infimum_extra,
1479 sizeof infimum_extra);
1480 if (page_is_empty(page)) {
1481 rec_set_next_offs_new(page + PAGE_NEW_INFIMUM, PAGE_NEW_SUPREMUM);
1482 } else {
1483 rec_set_next_offs_new(
1484 page + PAGE_NEW_INFIMUM,
1485 page_zip_dir_get(page_zip, 0) & PAGE_ZIP_DIR_SLOT_MASK);
1486 }
1487 memcpy(page + PAGE_NEW_INFIMUM, infimum_data, sizeof infimum_data);
1488 memcpy(page + (PAGE_NEW_SUPREMUM - REC_N_NEW_EXTRA_BYTES + 1),
1489 supremum_extra_data, sizeof supremum_extra_data);
1490
1491 page_zip_set_alloc(&d_stream, heap);
1492
1493 d_stream.next_in = page_zip->data + PAGE_DATA;
1494 /* Subtract the space reserved for
1495 the page header and the end marker of the modification log. */
1496 d_stream.avail_in =
1497 static_cast<uInt>(page_zip_get_size(page_zip) - (PAGE_DATA + 1));
1498 d_stream.next_out = page + PAGE_ZIP_START;
1499 d_stream.avail_out = UNIV_PAGE_SIZE - PAGE_ZIP_START;
1500
1501 if (UNIV_UNLIKELY(inflateInit2(&d_stream, UNIV_PAGE_SIZE_SHIFT) != Z_OK)) {
1502 ut_error;
1503 }
1504
1505 /* Decode the zlib header and the index information. */
1506 if (UNIV_UNLIKELY(inflate(&d_stream, Z_BLOCK) != Z_OK)) {
1507 page_zip_fail(
1508 ("page_zip_decompress:"
1509 " 1 inflate(Z_BLOCK)=%s\n",
1510 d_stream.msg));
1511 goto zlib_error;
1512 }
1513
1514 if (UNIV_UNLIKELY(inflate(&d_stream, Z_BLOCK) != Z_OK)) {
1515 page_zip_fail(
1516 ("page_zip_decompress:"
1517 " 2 inflate(Z_BLOCK)=%s\n",
1518 d_stream.msg));
1519 goto zlib_error;
1520 }
1521
1522 index = page_zip_fields_decode(page + PAGE_ZIP_START, d_stream.next_out,
1523 page_is_leaf(page) ? &trx_id_col : nullptr,
1524 fil_page_get_type(page) == FIL_PAGE_RTREE);
1525
1526 if (UNIV_UNLIKELY(!index)) {
1527 goto zlib_error;
1528 }
1529
1530 /* Decompress the user records. */
1531 page_zip->n_blobs = 0;
1532 d_stream.next_out = page + PAGE_ZIP_START;
1533
1534 {
1535 /* Pre-allocate the offsets for rec_get_offsets_reverse(). */
1536 ulint n = 1 + 1 /* node ptr */ + REC_OFFS_HEADER_SIZE +
1537 dict_index_get_n_fields(index);
1538
1539 offsets = static_cast<ulint *>(mem_heap_alloc(heap, n * sizeof(ulint)));
1540
1541 *offsets = n;
1542 }
1543
1544 /* Decompress the records in heap_no order. */
1545 if (!page_is_leaf(page)) {
1546 /* This is a node pointer page. */
1547 ulint info_bits;
1548
1549 if (UNIV_UNLIKELY(!page_zip_decompress_node_ptrs(
1550 page_zip, &d_stream, recs, n_dense, index, offsets, heap))) {
1551 goto err_exit;
1552 }
1553
1554 info_bits = mach_read_from_4(page + FIL_PAGE_PREV) == FIL_NULL
1555 ? REC_INFO_MIN_REC_FLAG
1556 : 0;
1557
1558 if (UNIV_UNLIKELY(!page_zip_set_extra_bytes(page_zip, page, info_bits))) {
1559 goto err_exit;
1560 }
1561 } else if (UNIV_LIKELY(trx_id_col == ULINT_UNDEFINED)) {
1562 /* This is a leaf page in a secondary index. */
1563 if (UNIV_UNLIKELY(!page_zip_decompress_sec(page_zip, &d_stream, recs,
1564 n_dense, index, offsets))) {
1565 goto err_exit;
1566 }
1567
1568 if (UNIV_UNLIKELY(!page_zip_set_extra_bytes(page_zip, page, 0))) {
1569 err_exit:
1570 page_zip_fields_free(index);
1571 mem_heap_free(heap);
1572 return false;
1573 }
1574 } else {
1575 /* This is a leaf page in a clustered index. */
1576 if (UNIV_UNLIKELY(!page_zip_decompress_clust(page_zip, &d_stream, recs,
1577 n_dense, index, trx_id_col,
1578 offsets, heap))) {
1579 goto err_exit;
1580 }
1581
1582 if (UNIV_UNLIKELY(!page_zip_set_extra_bytes(page_zip, page, 0))) {
1583 goto err_exit;
1584 }
1585 }
1586
1587 ut_a(page_is_comp(page));
1588 UNIV_MEM_ASSERT_RW(page, UNIV_PAGE_SIZE);
1589
1590 page_zip_fields_free(index);
1591 mem_heap_free(heap);
1592
1593 return true;
1594 }