|
| 1 | +//===--- Compression.h - C decls for compression libraries ------*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2023 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +// |
| 13 | +// Includes and definitions to allow us to use the compression libraries |
| 14 | +// (zlib, zstd and liblzma) in the backtracing module. |
| 15 | +// |
| 16 | +//===----------------------------------------------------------------------===// |
| 17 | + |
| 18 | +#ifndef SWIFT_BACKTRACING_COMPRESSION_H |
| 19 | +#define SWIFT_BACKTRACING_COMPRESSION_H |
| 20 | + |
| 21 | +#include <stdint.h> |
| 22 | +#include <stdlib.h> |
| 23 | + |
| 24 | +// Right now, we're soft linking to zlib/zstd/liblzma, so that users don't |
| 25 | +// need it installed (but if they try to do something that requires it, |
| 26 | +// they'll see an error message). |
| 27 | +// |
| 28 | +// As a result, we've grabbed copies of the relevant definitions here so |
| 29 | +// that we don't need to install the -dev packages in order to build Swift. |
| 30 | + |
| 31 | +#if SWIFT_BACKTRACE_STATIC_ZLIB |
| 32 | +#include "zlib.h" |
| 33 | +#else |
| 34 | +// This is the version we took the z_stream structure from |
| 35 | +#define ZLIB_VERSION "1.2.11" |
| 36 | + |
| 37 | +#define Z_OK 0 |
| 38 | +#define Z_STREAM_END 1 |
| 39 | + |
| 40 | +#define Z_NO_FLUSH 0 |
| 41 | + |
| 42 | +typedef struct z_stream_s { |
| 43 | + uint8_t *next_in; |
| 44 | + unsigned avail_in; |
| 45 | + unsigned long total_in; |
| 46 | + |
| 47 | + uint8_t *next_out; |
| 48 | + unsigned avail_out; |
| 49 | + unsigned long total_out; |
| 50 | + |
| 51 | + const char *msg; |
| 52 | + struct internal_state *state; |
| 53 | + |
| 54 | + void (*zalloc)(void *, unsigned, unsigned); |
| 55 | + void (*zfree)(void *, void *); |
| 56 | + void *opaque; |
| 57 | + |
| 58 | + int data_type; |
| 59 | + |
| 60 | + unsigned long adler; |
| 61 | + unsigned long reserved; |
| 62 | +} z_stream; |
| 63 | + |
| 64 | +typedef z_stream *z_streamp; |
| 65 | +#endif |
| 66 | + |
| 67 | +#if SWIFT_BACKTRACE_STATIC_ZSTD |
| 68 | +#include "zstd.h" |
| 69 | +#else |
| 70 | +typedef struct ZSTD_inBuffer_s { |
| 71 | + const void *src; |
| 72 | + size_t size; |
| 73 | + size_t pos; |
| 74 | +} ZSTD_inBuffer; |
| 75 | + |
| 76 | +typedef struct ZSTD_outBuffer_s { |
| 77 | + void *dst; |
| 78 | + size_t size; |
| 79 | + size_t pos; |
| 80 | +} ZSTD_outBuffer; |
| 81 | +#endif |
| 82 | + |
| 83 | +#if SWIFT_BACKTRACE_STATIC_LIBLZMA |
| 84 | +#include "lzma.h" |
| 85 | +#else |
| 86 | +typedef enum { |
| 87 | + LZMA_OK = 0, |
| 88 | + LZMA_STREAM_END = 1, |
| 89 | + LZMA_NO_CHECK = 2, |
| 90 | + LZMA_UNSUPPORTED_CHECK = 3, |
| 91 | + LZMA_GET_CHECK = 4, |
| 92 | + LZMA_MEM_ERROR = 5, |
| 93 | + LZMA_MEMLIMIT_ERROR = 6, |
| 94 | + LZMA_FORMAT_ERROR = 7, |
| 95 | + LZMA_OPTIONS_ERROR = 8, |
| 96 | + LZMA_DATA_ERROR = 9, |
| 97 | + LZMA_BUF_ERROR = 10, |
| 98 | + LZMA_PROG_ERROR = 11, |
| 99 | +} lzma_ret; |
| 100 | + |
| 101 | +typedef enum { |
| 102 | + LZMA_RUN = 0, |
| 103 | + LZMA_SYNC_FLUSH = 1, |
| 104 | + LZMA_FULL_FLUSH = 2, |
| 105 | + LZMA_FULL_BARRIER = 4, |
| 106 | + LZMA_FINISH = 3 |
| 107 | +} lzma_action; |
| 108 | + |
| 109 | +typedef enum { |
| 110 | + LZMA_RESERVED_ENUM = 0, |
| 111 | +} lzma_reserved_enum; |
| 112 | + |
| 113 | +typedef struct { |
| 114 | + void *(*alloc)(void *, size_t, size_t); |
| 115 | + void (*free)(void *, void *); |
| 116 | + void *opaque; |
| 117 | +} lzma_allocator; |
| 118 | + |
| 119 | +typedef struct lzma_internal_s lzma_internal; |
| 120 | + |
| 121 | +typedef struct { |
| 122 | + const uint8_t *next_in; |
| 123 | + size_t avail_in; |
| 124 | + uint64_t total_in; |
| 125 | + |
| 126 | + uint8_t *next_out; |
| 127 | + size_t avail_out; |
| 128 | + uint64_t total_out; |
| 129 | + |
| 130 | + const lzma_allocator *allocator; |
| 131 | + |
| 132 | + lzma_internal *internal; |
| 133 | + |
| 134 | + void *reserved_ptr1; |
| 135 | + void *reserved_ptr2; |
| 136 | + void *reserved_ptr3; |
| 137 | + void *reserved_ptr4; |
| 138 | + uint64_t reserved_int1; |
| 139 | + uint64_t reserved_int2; |
| 140 | + size_t reserved_int3; |
| 141 | + size_t reserved_int4; |
| 142 | + lzma_reserved_enum reserved_enum1; |
| 143 | + lzma_reserved_enum reserved_enum2; |
| 144 | +} lzma_stream; |
| 145 | + |
| 146 | +#define LZMA_STREAM_INIT {0} |
| 147 | +#endif |
| 148 | + |
| 149 | +#ifdef __cplusplus |
| 150 | +namespace swift { |
| 151 | +extern "C" { |
| 152 | +#endif |
| 153 | + |
| 154 | +// The Swift importer can't cope with complex macros; it will do inline |
| 155 | +// functions, however. |
| 156 | +static inline lzma_stream lzma_stream_init() { |
| 157 | + return (lzma_stream)LZMA_STREAM_INIT; |
| 158 | +} |
| 159 | +static inline z_stream zlib_stream_init() { |
| 160 | + return (z_stream){ 0 }; |
| 161 | +} |
| 162 | + |
| 163 | +#ifdef __cplusplus |
| 164 | +} // extern "C" |
| 165 | +} // namespace swift |
| 166 | +#endif |
| 167 | + |
| 168 | +#endif // SWIFT_BACKTRACING_COMPRESSION_H |
0 commit comments