VeraCrypt
aboutsummaryrefslogtreecommitdiff
path: root/src/Common/lzma/Compiler.h
blob: 185a52deb5049ddca6761951df0f7641c5813209 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* Compiler.h : Compiler specific defines and pragmas
2023-04-02 : Igor Pavlov : Public domain */

#ifndef ZIP7_INC_COMPILER_H
#define ZIP7_INC_COMPILER_H

#if defined(__clang__)
# define Z7_CLANG_VERSION  (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__)
#endif
#if defined(__clang__) && defined(__apple_build_version__)
# define Z7_APPLE_CLANG_VERSION   Z7_CLANG_VERSION
#elif defined(__clang__)
# define Z7_LLVM_CLANG_VERSION    Z7_CLANG_VERSION
#elif defined(__GNUC__)
# define Z7_GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
#endif

#ifdef _MSC_VER
#if !defined(__clang__) && !defined(__GNUC__)
#define Z7_MSC_VER_ORIGINAL _MSC_VER
#endif
#endif

#if defined(__MINGW32__) || defined(__MINGW64__)
#define Z7_MINGW
#endif

// #pragma GCC diagnostic ignored "-Wunknown-pragmas"

#ifdef __clang__
// padding size of '' with 4 bytes to alignment boundary
#pragma GCC diagnostic ignored "-Wpadded"
#endif


#ifdef _MSC_VER

  #ifdef UNDER_CE
    #define RPC_NO_WINDOWS_H
    /* #pragma warning(disable : 4115) // '_RPC_ASYNC_STATE' : named type definition in parentheses */
    #pragma warning(disable : 4201) // nonstandard extension used : nameless struct/union
    #pragma warning(disable : 4214) // nonstandard extension used : bit field types other than int
  #endif

#if defined(_MSC_VER) && _MSC_VER >= 1800
#pragma warning(disable : 4464) // relative include path contains '..'
#endif

// == 1200 : -O1 : for __forceinline
// >= 1900 : -O1 : for printf
#pragma warning(disable : 4710) // function not inlined

#if _MSC_VER < 1900
// winnt.h: 'Int64ShllMod32'
#pragma warning(disable : 4514) // unreferenced inline function has been removed
#endif
    
#if _MSC_VER < 1300
// #pragma warning(disable : 4702) // unreachable code
// Bra.c : -O1:
#pragma warning(disable : 4714) // function marked as __forceinline not inlined
#endif

/*
#if _MSC_VER > 1400 && _MSC_VER <= 1900
// strcat: This function or variable may be unsafe
// sysinfoapi.h: kit10: GetVersion was declared deprecated
#pragma warning(disable : 4996)
#endif
*/

#if _MSC_VER > 1200
// -Wall warnings

#pragma warning(disable : 4711) // function selected for automatic inline expansion
#pragma warning(disable : 4820) // '2' bytes padding added after data member

#if _MSC_VER >= 1400 && _MSC_VER < 1920
// 1400: string.h: _DBG_MEMCPY_INLINE_
// 1600 - 191x : smmintrin.h __cplusplus'
// is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'
#pragma warning(disable : 4668)

// 1400 - 1600 : WinDef.h : 'FARPROC' :
// 1900 - 191x : immintrin.h: _readfsbase_u32
// no function prototype given : converting '()' to '(void)'
#pragma warning(disable : 4255)
#endif

#if _MSC_VER >= 1914
// Compiler will insert Spectre mitigation for memory load if /Qspectre switch specified
#pragma warning(disable : 5045)
#endif

#endif // _MSC_VER > 1200
#endif // _MSC_VER


#if defined(__clang__) && (__clang_major__ >= 4)
  #define Z7_PRAGMA_OPT_DISABLE_LOOP_UNROLL_VECTORIZE \
    _Pragma("clang loop unroll(disable)") \
    _Pragma("clang loop vectorize(disable)")
  #define Z7_ATTRIB_NO_VECTORIZE
#elif defined(__GNUC__) && (__GNUC__ >= 5)
  #define Z7_ATTRIB_NO_VECTORIZE __attribute__((optimize("no-tree-vectorize")))
  // __attribute__((optimize("no-unroll-loops")));
  #define Z7_PRAGMA_OPT_DISABLE_LOOP_UNROLL_VECTORIZE
#elif defined(_MSC_VER) && (_MSC_VER >= 1920)
  #define Z7_PRAGMA_OPT_DISABLE_LOOP_UNROLL_VECTORIZE \
    _Pragma("loop( no_vector )")
  #define Z7_ATTRIB_NO_VECTORIZE
#else
  #define Z7_PRAGMA_OPT_DISABLE_LOOP_UNROLL_VECTORIZE
  #define Z7_ATTRIB_NO_VECTORIZE
#endif

#if defined(MY_CPU_X86_OR_AMD64) && ( \
       defined(__clang__) && (__clang_major__ >= 4) \
    || defined(__GNUC__) && (__GNUC__ >= 5))
  #define Z7_ATTRIB_NO_SSE  __attribute__((__target__("no-sse")))
#else
  #define Z7_ATTRIB_NO_SSE
#endif

#define Z7_ATTRIB_NO_VECTOR \
  Z7_ATTRIB_NO_VECTORIZE \
  Z7_ATTRIB_NO_SSE


#if defined(__clang__) && (__clang_major__ >= 8) \
  || defined(__GNUC__) && (__GNUC__ >= 1000) \
  /* || defined(_MSC_VER) && (_MSC_VER >= 1920) */
  // GCC is not good for __builtin_expect()
  #define Z7_LIKELY(x)   (__builtin_expect((x), 1))
  #define Z7_UNLIKELY(x) (__builtin_expect((x), 0))
  // #define Z7_unlikely [[unlikely]]
  // #define Z7_likely [[likely]]
#else
  #define Z7_LIKELY(x)   (x)
  #define Z7_UNLIKELY(x) (x)
  // #define Z7_likely
#endif


#if (defined(Z7_CLANG_VERSION) && (Z7_CLANG_VERSION >= 36000))
#define Z7_DIAGNOSCTIC_IGNORE_BEGIN_RESERVED_MACRO_IDENTIFIER \
  _Pragma("GCC diagnostic push") \
  _Pragma("GCC diagnostic ignored \"-Wreserved-macro-identifier\"")
#define Z7_DIAGNOSCTIC_IGNORE_END_RESERVED_MACRO_IDENTIFIER \
  _Pragma("GCC diagnostic pop")
#else
#define Z7_DIAGNOSCTIC_IGNORE_BEGIN_RESERVED_MACRO_IDENTIFIER
#define Z7_DIAGNOSCTIC_IGNORE_END_RESERVED_MACRO_IDENTIFIER
#endif

#define UNUSED_VAR(x) (void)x;
/* #define UNUSED_VAR(x) x=x; */

#endif
ution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "zipint.h" #include <stdlib.h> #include <string.h> /* parameter for the string hash function */ #define HASH_MULTIPLIER 33 #define HASH_START 5381 /* hash table's fill ratio is kept between these by doubling/halfing its size as necessary */ #define HASH_MAX_FILL .75 #define HASH_MIN_FILL .01 /* but hash table size is kept between these */ #define HASH_MIN_SIZE 256 #define HASH_MAX_SIZE 0x80000000ul struct zip_hash_entry { const zip_uint8_t *name; zip_int64_t orig_index; zip_int64_t current_index; struct zip_hash_entry *next; zip_uint32_t hash_value; }; typedef struct zip_hash_entry zip_hash_entry_t; struct zip_hash { zip_uint32_t table_size; zip_uint64_t nentries; zip_hash_entry_t **table; }; /* free list of entries */ static void free_list(zip_hash_entry_t *entry) { while (entry != NULL) { zip_hash_entry_t *next = entry->next; free(entry); entry = next; } } /* compute hash of string, full 32 bit value */ static zip_uint32_t hash_string(const zip_uint8_t *name) { zip_uint64_t value = HASH_START; if (name == NULL) { return 0; } while (*name != 0) { value = (zip_uint64_t)(((value * HASH_MULTIPLIER) + (zip_uint8_t)*name) % 0x100000000ul); name++; } return (zip_uint32_t)value; } /* resize hash table; new_size must be a power of 2, can be larger or smaller than current size */ static bool hash_resize(zip_hash_t *hash, zip_uint32_t new_size, zip_error_t *error) { zip_hash_entry_t **new_table; if (new_size == hash->table_size) { return true; } if ((new_table = (zip_hash_entry_t **)calloc(new_size, sizeof(zip_hash_entry_t *))) == NULL) { zip_error_set(error, ZIP_ER_MEMORY, 0); return false; } if (hash->nentries > 0) { zip_uint32_t i; for (i = 0; i < hash->table_size; i++) { zip_hash_entry_t *entry = hash->table[i]; while (entry) { zip_hash_entry_t *next = entry->next; zip_uint32_t new_index = entry->hash_value % new_size; entry->next = new_table[new_index]; new_table[new_index] = entry; entry = next; } } } free(hash->table); hash->table = new_table; hash->table_size = new_size; return true; } static zip_uint32_t size_for_capacity(zip_uint64_t capacity) { double needed_size = capacity / HASH_MAX_FILL; zip_uint32_t v; if (needed_size > ZIP_UINT32_MAX) { v = ZIP_UINT32_MAX; } else { v = (zip_uint32_t)needed_size; } if (v > HASH_MAX_SIZE) { return HASH_MAX_SIZE; } /* From Bit Twiddling Hacks by Sean Eron Anderson <seander@cs.stanford.edu> (http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2). */ v--; v |= v >> 1; v |= v >> 2; v |= v >> 4; v |= v >> 8; v |= v >> 16; v++; return v; } zip_hash_t * _zip_hash_new(zip_error_t *error) { zip_hash_t *hash; if ((hash = (zip_hash_t *)malloc(sizeof(zip_hash_t))) == NULL) { zip_error_set(error, ZIP_ER_MEMORY, 0); return NULL; } hash->table_size = 0; hash->nentries = 0; hash->table = NULL; return hash; } void _zip_hash_free(zip_hash_t *hash) { zip_uint32_t i; if (hash == NULL) { return; } if (hash->table != NULL) { for (i = 0; i < hash->table_size; i++) { if (hash->table[i] != NULL) { free_list(hash->table[i]); } } free(hash->table); } free(hash); } /* insert into hash, return error on existence or memory issues */ bool _zip_hash_add(zip_hash_t *hash, const zip_uint8_t *name, zip_uint64_t index, zip_flags_t flags, zip_error_t *error) { zip_uint32_t hash_value, table_index; zip_hash_entry_t *entry; if (hash == NULL || name == NULL || index > ZIP_INT64_MAX) { zip_error_set(error, ZIP_ER_INVAL, 0); return false; } if (hash->table_size == 0) { if (!hash_resize(hash, HASH_MIN_SIZE, error)) { return false; } } hash_value = hash_string(name); table_index = hash_value % hash->table_size; for (entry = hash->table[table_index]; entry != NULL; entry = entry->next) { if (entry->hash_value == hash_value && strcmp((const char *)name, (const char *)entry->name) == 0) { if (((flags & ZIP_FL_UNCHANGED) && entry->orig_index != -1) || entry->current_index != -1) { zip_error_set(error, ZIP_ER_EXISTS, 0); return false; } else { break; } } } if (entry == NULL) { if ((entry = (zip_hash_entry_t *)malloc(sizeof(zip_hash_entry_t))) == NULL) { zip_error_set(error, ZIP_ER_MEMORY, 0); return false; } entry->name = name; entry->next = hash->table[table_index]; hash->table[table_index] = entry; entry->hash_value = hash_value; entry->orig_index = -1; hash->nentries++; if (hash->nentries > hash->table_size * HASH_MAX_FILL && hash->table_size < HASH_MAX_SIZE) { if (!hash_resize(hash, hash->table_size * 2, error)) { return false; } } } if (flags & ZIP_FL_UNCHANGED) { entry->orig_index = (zip_int64_t)index; } entry->current_index = (zip_int64_t)index; return true; } /* remove entry from hash, error if not found */ bool _zip_hash_delete(zip_hash_t *hash, const zip_uint8_t *name, zip_error_t *error) { zip_uint32_t hash_value, index; zip_hash_entry_t *entry, *previous; if (hash == NULL || name == NULL) { zip_error_set(error, ZIP_ER_INVAL, 0); return false; } if (hash->nentries > 0) { hash_value = hash_string(name); index = hash_value % hash->table_size; previous = NULL; entry = hash->table[index]; while (entry) { if (entry->hash_value == hash_value && strcmp((const char *)name, (const char *)entry->name) == 0) { if (entry->orig_index == -1) { if (previous) { previous->next = entry->next; } else { hash->table[index] = entry->next; } free(entry); hash->nentries--; if (hash->nentries < hash->table_size * HASH_MIN_FILL && hash->table_size > HASH_MIN_SIZE) { if (!hash_resize(hash, hash->table_size / 2, error)) { return false; } } } else { entry->current_index = -1; } return true; } previous = entry; entry = entry->next; } } zip_error_set(error, ZIP_ER_NOENT, 0); return false; } /* find value for entry in hash, -1 if not found */ zip_int64_t _zip_hash_lookup(zip_hash_t *hash, const zip_uint8_t *name, zip_flags_t flags, zip_error_t *error) { zip_uint32_t hash_value, index; zip_hash_entry_t *entry; if (hash == NULL || name == NULL) { zip_error_set(error, ZIP_ER_INVAL, 0); return -1; } if (hash->nentries > 0) { hash_value = hash_string(name); index = hash_value % hash->table_size; for (entry = hash->table[index]; entry != NULL; entry = entry->next) { if (strcmp((const char *)name, (const char *)entry->name) == 0) { if (flags & ZIP_FL_UNCHANGED) { if (entry->orig_index != -1) { return entry->orig_index; } } else { if (entry->current_index != -1) { return entry->current_index; } } break; } } } zip_error_set(error, ZIP_ER_NOENT, 0); return -1; } bool _zip_hash_reserve_capacity(zip_hash_t *hash, zip_uint64_t capacity, zip_error_t *error) { zip_uint32_t new_size; if (capacity == 0) { return true; } new_size = size_for_capacity(capacity); if (new_size <= hash->table_size) { return true; } if (!hash_resize(hash, new_size, error)) { return false; } return true; } bool _zip_hash_revert(zip_hash_t *hash, zip_error_t *error) { zip_uint32_t i; zip_hash_entry_t *entry, *previous; for (i = 0; i < hash->table_size; i++) { previous = NULL; entry = hash->table[i]; while (entry) { if (entry->orig_index == -1) { zip_hash_entry_t *p; if (previous) { previous->next = entry->next; } else { hash->table[i] = entry->next; } p = entry; entry = entry->next; /* previous does not change */ free(p); hash->nentries--; } else { entry->current_index = entry->orig_index; previous = entry; entry = entry->next; } } } if (hash->nentries < hash->table_size * HASH_MIN_FILL && hash->table_size > HASH_MIN_SIZE) { zip_uint32_t new_size = hash->table_size / 2; while (hash->nentries < new_size * HASH_MIN_FILL && new_size > HASH_MIN_SIZE) { new_size /= 2; } if (!hash_resize(hash, new_size, error)) { return false; } } return true; }