Loading...
1#ifndef __LZ4DEFS_H__
2#define __LZ4DEFS_H__
3
4/*
5 * lz4defs.h -- common and architecture specific defines for the kernel usage
6
7 * LZ4 - Fast LZ compression algorithm
8 * Copyright (C) 2011-2016, Yann Collet.
9 * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are
12 * met:
13 * * Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
18 * distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * You can contact the author at :
31 * - LZ4 homepage : http://www.lz4.org
32 * - LZ4 source repository : https://github.com/lz4/lz4
33 *
34 * Changed for kernel usage by:
35 * Sven Schmidt <4sschmid@informatik.uni-hamburg.de>
36 */
37
38#include <asm/unaligned.h>
39#include <linux/string.h> /* memset, memcpy */
40
41#define FORCE_INLINE __always_inline
42
43/*-************************************
44 * Basic Types
45 **************************************/
46#include <linux/types.h>
47
48typedef uint8_t BYTE;
49typedef uint16_t U16;
50typedef uint32_t U32;
51typedef int32_t S32;
52typedef uint64_t U64;
53typedef uintptr_t uptrval;
54
55/*-************************************
56 * Architecture specifics
57 **************************************/
58#if defined(CONFIG_64BIT)
59#define LZ4_ARCH64 1
60#else
61#define LZ4_ARCH64 0
62#endif
63
64#if defined(__LITTLE_ENDIAN)
65#define LZ4_LITTLE_ENDIAN 1
66#else
67#define LZ4_LITTLE_ENDIAN 0
68#endif
69
70/*-************************************
71 * Constants
72 **************************************/
73#define MINMATCH 4
74
75#define WILDCOPYLENGTH 8
76#define LASTLITERALS 5
77#define MFLIMIT (WILDCOPYLENGTH + MINMATCH)
78/*
79 * ensure it's possible to write 2 x wildcopyLength
80 * without overflowing output buffer
81 */
82#define MATCH_SAFEGUARD_DISTANCE ((2 * WILDCOPYLENGTH) - MINMATCH)
83
84/* Increase this value ==> compression run slower on incompressible data */
85#define LZ4_SKIPTRIGGER 6
86
87#define HASH_UNIT sizeof(size_t)
88
89#define KB (1 << 10)
90#define MB (1 << 20)
91#define GB (1U << 30)
92
93#define MAXD_LOG 16
94#define MAX_DISTANCE ((1 << MAXD_LOG) - 1)
95#define STEPSIZE sizeof(size_t)
96
97#define ML_BITS 4
98#define ML_MASK ((1U << ML_BITS) - 1)
99#define RUN_BITS (8 - ML_BITS)
100#define RUN_MASK ((1U << RUN_BITS) - 1)
101
102/*-************************************
103 * Reading and writing into memory
104 **************************************/
105static FORCE_INLINE U16 LZ4_read16(const void *ptr)
106{
107 return get_unaligned((const U16 *)ptr);
108}
109
110static FORCE_INLINE U32 LZ4_read32(const void *ptr)
111{
112 return get_unaligned((const U32 *)ptr);
113}
114
115static FORCE_INLINE size_t LZ4_read_ARCH(const void *ptr)
116{
117 return get_unaligned((const size_t *)ptr);
118}
119
120static FORCE_INLINE void LZ4_write16(void *memPtr, U16 value)
121{
122 put_unaligned(value, (U16 *)memPtr);
123}
124
125static FORCE_INLINE void LZ4_write32(void *memPtr, U32 value)
126{
127 put_unaligned(value, (U32 *)memPtr);
128}
129
130static FORCE_INLINE U16 LZ4_readLE16(const void *memPtr)
131{
132 return get_unaligned_le16(memPtr);
133}
134
135static FORCE_INLINE void LZ4_writeLE16(void *memPtr, U16 value)
136{
137 return put_unaligned_le16(value, memPtr);
138}
139
140static FORCE_INLINE void LZ4_copy8(void *dst, const void *src)
141{
142#if LZ4_ARCH64
143 U64 a = get_unaligned((const U64 *)src);
144
145 put_unaligned(a, (U64 *)dst);
146#else
147 U32 a = get_unaligned((const U32 *)src);
148 U32 b = get_unaligned((const U32 *)src + 1);
149
150 put_unaligned(a, (U32 *)dst);
151 put_unaligned(b, (U32 *)dst + 1);
152#endif
153}
154
155/*
156 * customized variant of memcpy,
157 * which can overwrite up to 7 bytes beyond dstEnd
158 */
159static FORCE_INLINE void LZ4_wildCopy(void *dstPtr,
160 const void *srcPtr, void *dstEnd)
161{
162 BYTE *d = (BYTE *)dstPtr;
163 const BYTE *s = (const BYTE *)srcPtr;
164 BYTE *const e = (BYTE *)dstEnd;
165
166 do {
167 LZ4_copy8(d, s);
168 d += 8;
169 s += 8;
170 } while (d < e);
171}
172
173static FORCE_INLINE unsigned int LZ4_NbCommonBytes(register size_t val)
174{
175#if LZ4_LITTLE_ENDIAN
176 return __ffs(val) >> 3;
177#else
178 return (BITS_PER_LONG - 1 - __fls(val)) >> 3;
179#endif
180}
181
182static FORCE_INLINE unsigned int LZ4_count(
183 const BYTE *pIn,
184 const BYTE *pMatch,
185 const BYTE *pInLimit)
186{
187 const BYTE *const pStart = pIn;
188
189 while (likely(pIn < pInLimit - (STEPSIZE - 1))) {
190 size_t const diff = LZ4_read_ARCH(pMatch) ^ LZ4_read_ARCH(pIn);
191
192 if (!diff) {
193 pIn += STEPSIZE;
194 pMatch += STEPSIZE;
195 continue;
196 }
197
198 pIn += LZ4_NbCommonBytes(diff);
199
200 return (unsigned int)(pIn - pStart);
201 }
202
203#if LZ4_ARCH64
204 if ((pIn < (pInLimit - 3))
205 && (LZ4_read32(pMatch) == LZ4_read32(pIn))) {
206 pIn += 4;
207 pMatch += 4;
208 }
209#endif
210
211 if ((pIn < (pInLimit - 1))
212 && (LZ4_read16(pMatch) == LZ4_read16(pIn))) {
213 pIn += 2;
214 pMatch += 2;
215 }
216
217 if ((pIn < pInLimit) && (*pMatch == *pIn))
218 pIn++;
219
220 return (unsigned int)(pIn - pStart);
221}
222
223typedef enum { noLimit = 0, limitedOutput = 1 } limitedOutput_directive;
224typedef enum { byPtr, byU32, byU16 } tableType_t;
225
226typedef enum { noDict = 0, withPrefix64k, usingExtDict } dict_directive;
227typedef enum { noDictIssue = 0, dictSmall } dictIssue_directive;
228
229typedef enum { endOnOutputSize = 0, endOnInputSize = 1 } endCondition_directive;
230typedef enum { decode_full_block = 0, partial_decode = 1 } earlyEnd_directive;
231
232#define LZ4_STATIC_ASSERT(c) BUILD_BUG_ON(!(c))
233
234#endif
1/*
2 * lz4defs.h -- architecture specific defines
3 *
4 * Copyright (C) 2013, LG Electronics, Kyungsik Lee <kyungsik.lee@lge.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11/*
12 * Detects 64 bits mode
13 */
14#if defined(CONFIG_64BIT)
15#define LZ4_ARCH64 1
16#else
17#define LZ4_ARCH64 0
18#endif
19
20/*
21 * Architecture-specific macros
22 */
23#define BYTE u8
24typedef struct _U16_S { u16 v; } U16_S;
25typedef struct _U32_S { u32 v; } U32_S;
26typedef struct _U64_S { u64 v; } U64_S;
27#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
28
29#define A16(x) (((U16_S *)(x))->v)
30#define A32(x) (((U32_S *)(x))->v)
31#define A64(x) (((U64_S *)(x))->v)
32
33#define PUT4(s, d) (A32(d) = A32(s))
34#define PUT8(s, d) (A64(d) = A64(s))
35
36#define LZ4_READ_LITTLEENDIAN_16(d, s, p) \
37 (d = s - A16(p))
38
39#define LZ4_WRITE_LITTLEENDIAN_16(p, v) \
40 do { \
41 A16(p) = v; \
42 p += 2; \
43 } while (0)
44#else /* CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS */
45
46#define A64(x) get_unaligned((u64 *)&(((U16_S *)(x))->v))
47#define A32(x) get_unaligned((u32 *)&(((U16_S *)(x))->v))
48#define A16(x) get_unaligned((u16 *)&(((U16_S *)(x))->v))
49
50#define PUT4(s, d) \
51 put_unaligned(get_unaligned((const u32 *) s), (u32 *) d)
52#define PUT8(s, d) \
53 put_unaligned(get_unaligned((const u64 *) s), (u64 *) d)
54
55#define LZ4_READ_LITTLEENDIAN_16(d, s, p) \
56 (d = s - get_unaligned_le16(p))
57
58#define LZ4_WRITE_LITTLEENDIAN_16(p, v) \
59 do { \
60 put_unaligned_le16(v, (u16 *)(p)); \
61 p += 2; \
62 } while (0)
63#endif
64
65#define COPYLENGTH 8
66#define ML_BITS 4
67#define ML_MASK ((1U << ML_BITS) - 1)
68#define RUN_BITS (8 - ML_BITS)
69#define RUN_MASK ((1U << RUN_BITS) - 1)
70#define MEMORY_USAGE 14
71#define MINMATCH 4
72#define SKIPSTRENGTH 6
73#define LASTLITERALS 5
74#define MFLIMIT (COPYLENGTH + MINMATCH)
75#define MINLENGTH (MFLIMIT + 1)
76#define MAXD_LOG 16
77#define MAXD (1 << MAXD_LOG)
78#define MAXD_MASK (u32)(MAXD - 1)
79#define MAX_DISTANCE (MAXD - 1)
80#define HASH_LOG (MAXD_LOG - 1)
81#define HASHTABLESIZE (1 << HASH_LOG)
82#define MAX_NB_ATTEMPTS 256
83#define OPTIMAL_ML (int)((ML_MASK-1)+MINMATCH)
84#define LZ4_64KLIMIT ((1<<16) + (MFLIMIT - 1))
85#define HASHLOG64K ((MEMORY_USAGE - 2) + 1)
86#define HASH64KTABLESIZE (1U << HASHLOG64K)
87#define LZ4_HASH_VALUE(p) (((A32(p)) * 2654435761U) >> \
88 ((MINMATCH * 8) - (MEMORY_USAGE-2)))
89#define LZ4_HASH64K_VALUE(p) (((A32(p)) * 2654435761U) >> \
90 ((MINMATCH * 8) - HASHLOG64K))
91#define HASH_VALUE(p) (((A32(p)) * 2654435761U) >> \
92 ((MINMATCH * 8) - HASH_LOG))
93
94#if LZ4_ARCH64/* 64-bit */
95#define STEPSIZE 8
96
97#define LZ4_COPYSTEP(s, d) \
98 do { \
99 PUT8(s, d); \
100 d += 8; \
101 s += 8; \
102 } while (0)
103
104#define LZ4_COPYPACKET(s, d) LZ4_COPYSTEP(s, d)
105
106#define LZ4_SECURECOPY(s, d, e) \
107 do { \
108 if (d < e) { \
109 LZ4_WILDCOPY(s, d, e); \
110 } \
111 } while (0)
112#define HTYPE u32
113
114#ifdef __BIG_ENDIAN
115#define LZ4_NBCOMMONBYTES(val) (__builtin_clzll(val) >> 3)
116#else
117#define LZ4_NBCOMMONBYTES(val) (__builtin_ctzll(val) >> 3)
118#endif
119
120#else /* 32-bit */
121#define STEPSIZE 4
122
123#define LZ4_COPYSTEP(s, d) \
124 do { \
125 PUT4(s, d); \
126 d += 4; \
127 s += 4; \
128 } while (0)
129
130#define LZ4_COPYPACKET(s, d) \
131 do { \
132 LZ4_COPYSTEP(s, d); \
133 LZ4_COPYSTEP(s, d); \
134 } while (0)
135
136#define LZ4_SECURECOPY LZ4_WILDCOPY
137#define HTYPE const u8*
138
139#ifdef __BIG_ENDIAN
140#define LZ4_NBCOMMONBYTES(val) (__builtin_clz(val) >> 3)
141#else
142#define LZ4_NBCOMMONBYTES(val) (__builtin_ctz(val) >> 3)
143#endif
144
145#endif
146
147#define LZ4_WILDCOPY(s, d, e) \
148 do { \
149 LZ4_COPYPACKET(s, d); \
150 } while (d < e)
151
152#define LZ4_BLINDCOPY(s, d, l) \
153 do { \
154 u8 *e = (d) + l; \
155 LZ4_WILDCOPY(s, d, e); \
156 d = e; \
157 } while (0)