^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: Zlib
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #ifndef DFLTCC_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #define DFLTCC_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include "../zlib_deflate/defutil.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <asm/facility.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <asm/setup.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Tuning parameters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #define DFLTCC_LEVEL_MASK 0x2 /* DFLTCC compression for level 1 only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #define DFLTCC_LEVEL_MASK_DEBUG 0x3fe /* DFLTCC compression for all levels */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #define DFLTCC_BLOCK_SIZE 1048576
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define DFLTCC_FIRST_FHT_BLOCK_SIZE 4096
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define DFLTCC_DHT_MIN_SAMPLE_SIZE 4096
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define DFLTCC_RIBM 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define DFLTCC_FACILITY 151
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * Parameter Block for Query Available Functions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct dfltcc_qaf_param {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) char fns[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) char reserved1[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) char fmts[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) char reserved2[6];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static_assert(sizeof(struct dfltcc_qaf_param) == 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define DFLTCC_FMT0 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * Parameter Block for Generate Dynamic-Huffman Table, Compress and Expand.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct dfltcc_param_v0 {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) uint16_t pbvn; /* Parameter-Block-Version Number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) uint8_t mvn; /* Model-Version Number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) uint8_t ribm; /* Reserved for IBM use */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) unsigned reserved32 : 31;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) unsigned cf : 1; /* Continuation Flag */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) uint8_t reserved64[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) unsigned nt : 1; /* New Task */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) unsigned reserved129 : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) unsigned cvt : 1; /* Check Value Type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) unsigned reserved131 : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) unsigned htt : 1; /* Huffman-Table Type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) unsigned bcf : 1; /* Block-Continuation Flag */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) unsigned bcc : 1; /* Block Closing Control */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) unsigned bhf : 1; /* Block Header Final */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) unsigned reserved136 : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) unsigned reserved137 : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) unsigned dhtgc : 1; /* DHT Generation Control */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) unsigned reserved139 : 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) unsigned reserved144 : 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) unsigned sbb : 3; /* Sub-Byte Boundary */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) uint8_t oesc; /* Operation-Ending-Supplemental Code */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) unsigned reserved160 : 12;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) unsigned ifs : 4; /* Incomplete-Function Status */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) uint16_t ifl; /* Incomplete-Function Length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) uint8_t reserved192[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) uint8_t reserved256[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) uint8_t reserved320[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) uint16_t hl; /* History Length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) unsigned reserved368 : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) uint16_t ho : 15; /* History Offset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) uint32_t cv; /* Check Value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) unsigned eobs : 15; /* End-of-block Symbol */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) unsigned reserved431: 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) uint8_t eobl : 4; /* End-of-block Length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) unsigned reserved436 : 12;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) unsigned reserved448 : 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) uint16_t cdhtl : 12; /* Compressed-Dynamic-Huffman Table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) Length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) uint8_t reserved464[6];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) uint8_t cdht[288];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) uint8_t reserved[32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) uint8_t csb[1152];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) static_assert(sizeof(struct dfltcc_param_v0) == 1536);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) #define CVT_CRC32 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) #define CVT_ADLER32 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) #define HTT_FIXED 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) #define HTT_DYNAMIC 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * Extension of inflate_state and deflate_state for DFLTCC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) struct dfltcc_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) struct dfltcc_param_v0 param; /* Parameter block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) struct dfltcc_qaf_param af; /* Available functions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) uLong level_mask; /* Levels on which to use DFLTCC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) uLong block_size; /* New block each X bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) uLong block_threshold; /* New block after total_in > X */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) uLong dht_threshold; /* New block only if avail_in >= X */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) char msg[64]; /* Buffer for strm->msg */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) /* Resides right after inflate_state or deflate_state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) #define GET_DFLTCC_STATE(state) ((struct dfltcc_state *)((state) + 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) /* External functions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) int dfltcc_can_deflate(z_streamp strm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) int dfltcc_deflate(z_streamp strm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) int flush,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) block_state *result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) void dfltcc_reset(z_streamp strm, uInt size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) int dfltcc_can_inflate(z_streamp strm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) typedef enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) DFLTCC_INFLATE_CONTINUE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) DFLTCC_INFLATE_BREAK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) DFLTCC_INFLATE_SOFTWARE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) } dfltcc_inflate_action;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) dfltcc_inflate_action dfltcc_inflate(z_streamp strm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) int flush, int *ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static inline int is_dfltcc_enabled(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) return (zlib_dfltcc_support != ZLIB_DFLTCC_DISABLED &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) test_facility(DFLTCC_FACILITY));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) #define DEFLATE_RESET_HOOK(strm) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) dfltcc_reset((strm), sizeof(deflate_state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) #define DEFLATE_HOOK dfltcc_deflate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) #define DEFLATE_NEED_CHECKSUM(strm) (!dfltcc_can_deflate((strm)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) #define DEFLATE_DFLTCC_ENABLED() is_dfltcc_enabled()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) #define INFLATE_RESET_HOOK(strm) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) dfltcc_reset((strm), sizeof(struct inflate_state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) #define INFLATE_TYPEDO_HOOK(strm, flush) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) if (dfltcc_can_inflate((strm))) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) dfltcc_inflate_action action; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) RESTORE(); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) action = dfltcc_inflate((strm), (flush), &ret); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) LOAD(); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) if (action == DFLTCC_INFLATE_CONTINUE) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) break; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) else if (action == DFLTCC_INFLATE_BREAK) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) goto inf_leave; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) #define INFLATE_NEED_CHECKSUM(strm) (!dfltcc_can_inflate((strm)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) #define INFLATE_NEED_UPDATEWINDOW(strm) (!dfltcc_can_inflate((strm)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) #endif /* DFLTCC_H */