cryptlib  3.4.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros
brg_types.h
Go to the documentation of this file.
1 /*
2  ---------------------------------------------------------------------------
3  Copyright (c) 1998-2008, Brian Gladman, Worcester, UK. All rights reserved.
4 
5  LICENSE TERMS
6 
7  The redistribution and use of this software (with or without changes)
8  is allowed without the payment of fees or royalties provided that:
9 
10  1. source code distributions include the above copyright notice, this
11  list of conditions and the following disclaimer;
12 
13  2. binary distributions include the above copyright notice, this list
14  of conditions and the following disclaimer in their documentation;
15 
16  3. the name of the copyright holder is not used to endorse products
17  built using this software without specific written permission.
18 
19  DISCLAIMER
20 
21  This software is provided 'as is' with no explicit or implied warranties
22  in respect of its properties, including, but not limited to, correctness
23  and/or fitness for purpose.
24  ---------------------------------------------------------------------------
25  Issue Date: 20/12/2007
26 
27  The unsigned integer types defined here are of the form uint_<nn>t where
28  <nn> is the length of the type; for example, the unsigned 32-bit type is
29  'uint_32t'. These are NOT the same as the 'C99 integer types' that are
30  defined in the inttypes.h and stdint.h headers since attempts to use these
31  types have shown that support for them is still highly variable. However,
32  since the latter are of the form uint<nn>_t, a regular expression search
33  and replace (in VC++ search on 'uint_{:z}t' and replace with 'uint\1_t')
34  can be used to convert the types used here to the C99 standard types.
35 */
36 
37 #ifndef _BRG_TYPES_H
38 #define _BRG_TYPES_H
39 
40 #if defined(__cplusplus)
41 extern "C" {
42 #endif
43 
44 #include <limits.h>
45 
46 #if defined( _MSC_VER ) && ( _MSC_VER >= 1300 )
47 # include <stddef.h>
48 # define ptrint_t intptr_t
49 #elif defined( __ECOS__ )
50 # define intptr_t unsigned int
51 # define ptrint_t intptr_t
52 #elif defined( __GNUC__ ) && ( __GNUC__ >= 3 )
53 # include <stdint.h>
54 # define ptrint_t intptr_t
55 #else
56 # define ptrint_t int
57 #endif
58 
59 #ifndef BRG_UI8
60 # define BRG_UI8
61 # if UCHAR_MAX == 255u
62  typedef unsigned char uint_8t;
63 # else
64 # error Please define uint_8t as an 8-bit unsigned integer type in brg_types.h
65 # endif
66 #endif
67 
68 #ifndef BRG_UI16
69 # define BRG_UI16
70 # if USHRT_MAX == 65535u
71  typedef unsigned short uint_16t;
72 # else
73 # error Please define uint_16t as a 16-bit unsigned short type in brg_types.h
74 # endif
75 #endif
76 
77 #ifndef BRG_UI32
78 # define BRG_UI32
79 # if UINT_MAX == 4294967295u
80 # define li_32(h) 0x##h##u
81  typedef unsigned int uint_32t;
82 # elif ULONG_MAX == 4294967295u
83 # define li_32(h) 0x##h##ul
84  typedef unsigned long uint_32t;
85 # elif defined( _CRAY )
86  /* USE_AES is undefined on Crays, however we define a dummy data type
87  to get the code to compile - pcg */
88 /*#error Crays don't support 32-bit data types, this code won't compile on a Cray*/
89  typedef unsigned int uint_32t;
90 # else
91 # error Please define uint_32t as a 32-bit unsigned integer type in brg_types.h
92 # endif
93 #endif
94 
95 #ifndef BRG_UI64
96 # if defined( __BORLANDC__ ) && !defined( __MSDOS__ )
97 # define BRG_UI64
98 # define li_64(h) 0x##h##ui64
99  typedef unsigned __int64 uint_64t;
100 # elif defined( _MSC_VER ) && ( _MSC_VER < 1300 ) /* 1300 == VC++ 7.0 */
101 # define BRG_UI64
102 # define li_64(h) 0x##h##ui64
103  typedef unsigned __int64 uint_64t;
104 # elif defined( __sun ) && defined( ULONG_MAX ) && ULONG_MAX == 0xfffffffful
105 # define BRG_UI64
106 # define li_64(h) 0x##h##ull
107  typedef unsigned long long uint_64t;
108 # elif defined( __MVS__ )
109 # define BRG_UI64
110 # define li_64(h) 0x##h##ull
111  typedef unsigned int long long uint_64t;
112 # elif defined( UINT_MAX ) && UINT_MAX > 4294967295u
113 # if UINT_MAX == 18446744073709551615u
114 # define BRG_UI64
115 # define li_64(h) 0x##h##u
116  typedef unsigned int uint_64t;
117 # endif
118 # elif defined( ULONG_MAX ) && ULONG_MAX > 4294967295u
119 # if ULONG_MAX == 18446744073709551615ul
120 # define BRG_UI64
121 # define li_64(h) 0x##h##ul
122  typedef unsigned long uint_64t;
123 # endif
124 # elif defined( ULLONG_MAX ) && ULLONG_MAX > 4294967295u
125 # if ULLONG_MAX == 18446744073709551615ull
126 # define BRG_UI64
127 # define li_64(h) 0x##h##ull
128  typedef unsigned long long uint_64t;
129 # endif
130 # elif defined( ULONG_LONG_MAX ) && ULONG_LONG_MAX > 4294967295u
131 # if ULONG_LONG_MAX == 18446744073709551615ull
132 # define BRG_UI64
133 # define li_64(h) 0x##h##ull
134  typedef unsigned long long uint_64t;
135 # endif
136 # endif
137 #endif
138 
139 #if !defined( BRG_UI64 )
140 # if defined( NEED_UINT_64T )
141 # error Please define uint_64t as an unsigned 64 bit type in brg_types.h
142 # endif
143 #endif
144 
145 #ifndef RETURN_VALUES
146 # define RETURN_VALUES
147 # if defined( DLL_EXPORT )
148 # if defined( _MSC_VER ) || defined ( __INTEL_COMPILER )
149 # define VOID_RETURN __declspec( dllexport ) void __stdcall
150 # define INT_RETURN __declspec( dllexport ) int __stdcall
151 # elif defined( __GNUC__ )
152 # define VOID_RETURN __declspec( __dllexport__ ) void
153 # define INT_RETURN __declspec( __dllexport__ ) int
154 # else
155 # error Use of the DLL is only available on the Microsoft, Intel and GCC compilers
156 # endif
157 # elif defined( DLL_IMPORT )
158 # if defined( _MSC_VER ) || defined ( __INTEL_COMPILER )
159 # define VOID_RETURN __declspec( dllimport ) void __stdcall
160 # define INT_RETURN __declspec( dllimport ) int __stdcall
161 # elif defined( __GNUC__ )
162 # define VOID_RETURN __declspec( __dllimport__ ) void
163 # define INT_RETURN __declspec( __dllimport__ ) int
164 # else
165 # error Use of the DLL is only available on the Microsoft, Intel and GCC compilers
166 # endif
167 # elif defined( __WATCOMC__ )
168 # define VOID_RETURN void __cdecl
169 # define INT_RETURN int __cdecl
170 # else
171 # define VOID_RETURN void
172 # define INT_RETURN int
173 # endif
174 #endif
175 
176 /* These defines are used to detect and set the memory alignment of pointers.
177  Note that offsets are in bytes.
178 
179  ALIGN_OFFSET(x,n) return the positive or zero offset of
180  the memory addressed by the pointer 'x'
181  from an address that is aligned on an
182  'n' byte boundary ('n' is a power of 2)
183 
184  ALIGN_FLOOR(x,n) return a pointer that points to memory
185  that is aligned on an 'n' byte boundary
186  and is not higher than the memory address
187  pointed to by 'x' ('n' is a power of 2)
188 
189  ALIGN_CEIL(x,n) return a pointer that points to memory
190  that is aligned on an 'n' byte boundary
191  and is not lower than the memory address
192  pointed to by 'x' ('n' is a power of 2)
193 */
194 
195 #define ALIGN_OFFSET(x,n) (((ptrint_t)(x)) & ((n) - 1))
196 #define ALIGN_FLOOR(x,n) ((uint_8t*)(x) - ( ((ptrint_t)(x)) & ((n) - 1)))
197 #define ALIGN_CEIL(x,n) ((uint_8t*)(x) + (-((ptrint_t)(x)) & ((n) - 1)))
198 
199 /* These defines are used to declare buffers in a way that allows
200  faster operations on longer variables to be used. In all these
201  defines 'size' must be a power of 2 and >= 8. NOTE that the
202  buffer size is in bytes but the type length is in bits
203 
204  UNIT_TYPEDEF(x,size) declares a variable 'x' of length
205  'size' bits
206 
207  BUFR_TYPEDEF(x,size,bsize) declares a buffer 'x' of length 'bsize'
208  bytes defined as an array of variables
209  each of 'size' bits (bsize must be a
210  multiple of size / 8)
211 
212  UNIT_CAST(x,size) casts a variable to a type of
213  length 'size' bits
214 
215  UPTR_CAST(x,size) casts a pointer to a pointer to a
216  varaiable of length 'size' bits
217 */
218 
219 #define UI_TYPE(size) uint_##size##t
220 #define UNIT_TYPEDEF(x,size) typedef UI_TYPE(size) x
221 #define BUFR_TYPEDEF(x,size,bsize) typedef UI_TYPE(size) x[bsize / (size >> 3)]
222 #define UNIT_CAST(x,size) ((UI_TYPE(size) )(x))
223 #define UPTR_CAST(x,size) ((UI_TYPE(size)*)(x))
224 
225 #if defined(__cplusplus)
226 }
227 #endif
228 
229 #endif