| 1 | /* |
| 2 | Copyright The Soci Snapshotter Authors. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | Copyright (C) 1995-2017 Jean-loup Gailly and Mark Adler |
| 19 | This software is provided 'as-is', without any express or implied |
| 20 | warranty. In no event will the authors be held liable for any damages |
| 21 | arising from the use of this software. |
| 22 | Permission is granted to anyone to use this software for any purpose, |
| 23 | including commercial applications, and to alter it and redistribute it |
| 24 | freely, subject to the following restrictions: |
| 25 | 1. The origin of this software must not be misrepresented; you must not |
| 26 | claim that you wrote the original software. If you use this software |
| 27 | in a product, an acknowledgment in the product documentation would be |
| 28 | appreciated but is not required. |
| 29 | 2. Altered source versions must be plainly marked as such, and must not be |
| 30 | misrepresented as being the original software. |
| 31 | 3. This notice may not be removed or altered from any source distribution. |
| 32 | Jean-loup Gailly Mark Adler |
| 33 | [email protected] [email protected] |
| 34 | */ |
| 35 | /* |
| 36 | This source code is based on |
| 37 | https://github.com/madler/zlib/blob/master/examples/zran.c |
| 38 | and related code from that repository. It retains the copyright and |
| 39 | distribution restrictions of that work. It has been substantially modified |
| 40 | from the original. |
| 41 | */ |
| 42 | |
| 43 | #ifndef GZIP_ZINFO_H |
| 44 | #define GZIP_ZINFO_H |
| 45 | |
| 46 | #include <stdbool.h> |
| 47 | #include <stdint.h> |
| 48 | #include <stdio.h> |
| 49 | |
| 50 | #include <string.h> |
| 51 | #include <zlib.h> |
| 52 | |
| 53 | typedef unsigned char uchar; |
| 54 | typedef int64_t offset_t; |
| 55 | |
| 56 | #define ZINFO_VERSION_ONE 1 |
| 57 | #define ZINFO_VERSION_TWO 2 |
| 58 | |
| 59 | #define ZINFO_VERSION_CUR ZINFO_VERSION_TWO |
| 60 | |
| 61 | /* Since gzip is compressed with 32 KiB window size, WINDOW_SIZE is fixed */ |
| 62 | #define WINSIZE 32768U |
| 63 | |
| 64 | /* |
| 65 | - 8 bytes, compressed offset |
| 66 | - 8 bytes, uncompressed offset |
| 67 | - 1 byte, bits |
| 68 | - 32768 bytes, window |
| 69 | */ |
| 70 | #define PACKED_CHECKPOINT_SIZE (8 + 8 + 1 + WINSIZE) |
| 71 | |
| 72 | /* |
| 73 | - 4 bytes, number of checkpoints |
| 74 | - 8 bytes, span size |
| 75 | */ |
| 76 | #define BLOB_HEADER_SIZE (4 + 8) |
| 77 | |
| 78 | |
| 79 | enum { |
| 80 | GZIP_ZINFO_OK = 0, |
| 81 | GZIP_ZINFO_FILE_NOT_FOUND = -80, |
| 82 | GZIP_ZINFO_INDEX_NULL = -81, |
| 83 | GZIP_ZINFO_CANNOT_ALLOC = -82, |
| 84 | }; |
| 85 | |
| 86 | struct gzip_checkpoint { |
| 87 | offset_t out; /* corresponding offset in uncompressed data */ |
| 88 | offset_t in; /* offset in input file of first full byte */ |
| 89 | uint8_t bits; /* number of bits (1-7) from byte at in - 1, or 0 */ |
| 90 | unsigned char window[WINSIZE]; /* preceding 32K of uncompressed data */ |
| 91 | }; |
| 92 | |
| 93 | struct gzip_zinfo { |
| 94 | int32_t version; |
| 95 | int32_t have; /* number of list entries filled in */ |
| 96 | int32_t size; /* number of list entries allocated */ |
| 97 | struct gzip_checkpoint *list; /* allocated list */ |
| 98 | offset_t span_size; |
| 99 | }; |
| 100 | |
| 101 | // zinfo - metadata starts. |
| 102 | // Get index number of gzip zinfo within which the uncompressed offset is present |
| 103 | int pt_index_from_ucmp_offset(struct gzip_zinfo *index, offset_t off); |
| 104 | offset_t get_ucomp_off(struct gzip_zinfo *index, int checkpoint); |
| 105 | offset_t get_comp_off(struct gzip_zinfo *index, int checkpoint); |
| 106 | unsigned get_blob_size(struct gzip_zinfo *index); |
| 107 | int32_t get_max_span_id(struct gzip_zinfo *index); |
| 108 | int has_bits(struct gzip_zinfo *index, int checkpoint); |
| 109 | // zinfo - metadata ends. |
| 110 | |
| 111 | // zinfo - generation/extraction starts. |
| 112 | int generate_zinfo_from_file(const char* filepath, offset_t span, struct gzip_zinfo** index); |
| 113 | int extract_data_from_file(const char* file, struct gzip_zinfo* index, offset_t offset, void* buf, int len); |
| 114 | int extract_data_from_buffer(void* d, offset_t datalen, struct gzip_zinfo* index, offset_t offset, void* buffer, offset_t len, int first_checkpoint); |
| 115 | // zinfo - generation/extraction ends. |
| 116 | |
| 117 | // zinfo - zinfo <-> blob conversion starts. |
| 118 | /* Converts zinfo to blob |
| 119 | Returns the size of the buffer on success |
| 120 | This function assumes that the buffer is large enough already |
| 121 | to hold the entire zinfo |
| 122 | */ |
| 123 | int zinfo_to_blob(struct gzip_zinfo* index, void* buf); |
| 124 | struct gzip_zinfo* blob_to_zinfo(void* buf, offset_t len); |
| 125 | // zinfo - zinfo <-> blob conversion ends. |
| 126 | |
| 127 | #endif // GZIP_ZINFO_H |