lz4_common.h
Go to the documentation of this file.
1 #ifndef LZ4_COMMON_H_
2 #define LZ4_COMMON_H_
3 
4 //******************************************************************************
5 //
8 //
9 //******************************************************************************
10 
11 //*****************************************************************************
12 //
13 // If building with a C++ compiler, make all of the definitions in this header
14 // have a C binding.
15 //
16 //*****************************************************************************
17 #ifdef __cplusplus
18 extern "C"
19 {
20 #endif
21 
22 #include <stdint.h>
23 #include <stdbool.h>
24 
25 #define LZ4_MAGIC_NUMBER 0x184d2204
26 
27 #define LZ4_MAX_BLOCK_SIZE 0x00100000
28 
29 #define LZ4_FLG_VERSION 0x40
30 #define LZ4_FLG_BLOCK_INDEP 0x20
31 #define LZ4_FLG_BLOCK_CHECKSUM 0x10
32 #define LZ4_FLG_CONTENT_SIZE 0x08
33 #define LZ4_FLG_CONTENT_CHECKSUM 0x04
34 #define LZ4_BD_BYTE 0x70
35 
36 #define LZ4_MIN_MATCH 4
37 #define LZ4_LAST_MATCH_DIST 12
38 #define LZ4_LAST_LITERAL_LENGTH 5
39 
40 #define LZ4_TOKEN_LITERAL_MASK 0xf0
41 #define LZ4_TOKEN_MATCH_MASK 0x0f
42 
43 #define LZ4_HASH_VALUE 0x9E3779B1
44 
45 static inline uint16_t calculateHash(uint32_t input)
46 {
47  return (uint16_t)(((input) * LZ4_HASH_VALUE) >> 16);
48 }
49 
50 static inline uint16_t read16(const uint8_t* ptr)
51 {
52  /* Odd address */
53  if ((uintptr_t)ptr & 1) {
54  uint16_t value = ptr[0];
55  value += (uint16_t)ptr[1] << 8;
56  return value;
57  }
58  /* Even address */
59  else {
60  return *(uint16_t *)ptr;
61  }
62 }
63 
64 static inline uint32_t read32(const uint8_t* ptr)
65 {
66  /* Odd address */
67  if ((uintptr_t)ptr & 1) {
68  uint32_t value = ptr[0];
69  value += (uint32_t)(*(uint16_t *)(ptr+1)) << 8;
70  value += (uint32_t)(ptr[3]) << 24;
71  return value;
72  }
73  /* Even address */
74  else {
75  return *(uint32_t *)ptr;
76  }
77 }
78 
79 static inline uint8_t *write16(uint8_t *ptr, uint16_t data)
80 {
81  if ((uintptr_t)ptr & 1) {
82  *ptr++ = data & 0xff;
83  *ptr++ = (data >> 8) & 0xff;
84  return ptr;
85  }
86  else {
87  *((uint16_t *)ptr) = data;
88  return ptr+2;
89  }
90 }
91 
92 static inline uint8_t *write32(uint8_t *ptr, uint32_t data)
93 {
94  if ((uintptr_t)ptr & 1) {
95  *ptr++ = data & 0xff;
96  *ptr++ = (data >> 8) & 0xff;
97  *ptr++ = (data >> 16) & 0xff;
98  *ptr++ = (data >> 24) & 0xff;
99  return ptr;
100  }
101  else {
102  *((uint32_t *)ptr) = data;
103  return ptr+4;
104  }
105 }
106 
107 static inline uint16_t checkMatch(const uint8_t *pos1, const uint8_t *pos2)
108 {
109  uint16_t length = 0;
110  while ((*pos1++ == *pos2++) && (length != 0xffff)) {
111  length++;
112  }
113  return length;
114 }
115 
116 //*****************************************************************************
117 //
118 // Mark the end of the C bindings section for C++ compilers.
119 //
120 //*****************************************************************************
121 #ifdef __cplusplus
122 }
123 #endif
124 
125 //******************************************************************************
126 //
127 // Close the Doxygen group.
129 //
130 //******************************************************************************
131 
132 #endif /* LZ4_COMMON_H_ */
#define LZ4_HASH_VALUE
Definition: lz4_common.h:43
Copyright 2017, Texas Instruments Incorporated