hmac pad fix
[asdcplib.git] / src / KM_platform.h
1 /*
2 Copyright (c) 2004-2009, John Hurst
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 1. Redistributions of source code must retain the above copyright
9    notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11    notice, this list of conditions and the following disclaimer in the
12    documentation and/or other materials provided with the distribution.
13 3. The name of the author may not be used to endorse or promote products
14    derived from this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27   /*! \file    KM_platform.h
28     \version $Id$
29     \brief   platform portability
30   */
31
32 #ifndef _KM_PLATFORM_H_
33 # define _KM_PLATFORM_H_
34
35 # ifdef __APPLE__
36 #  ifdef __BIG_ENDIAN__
37 #   define KM_BIG_ENDIAN
38 #  endif
39 # endif
40
41 # ifdef KM_WIN32
42 #  define WIN32_LEAN_AND_MEAN
43 #  define VC_EXTRALEAN
44 #  include <windows.h>
45 #  include <stdlib.h>
46 #  include <stdio.h>
47 #  include <stdarg.h>
48 #  pragma warning(disable:4786)                 // Ignore "identifer > 255 characters" warning
49
50 typedef unsigned __int64   ui64_t;
51 typedef __int64            i64_t;
52 #  define i64_C(c)  (i64_t)(c)
53 #  define ui64_C(c) (ui64_t)(c)
54 #  define snprintf _snprintf
55 #  define vsnprintf _vsnprintf
56
57 # else // KM_WIN32
58 typedef unsigned long long ui64_t;
59 typedef long long          i64_t;
60 #  define i64_C(c)  c##LL
61 #  define ui64_C(c) c##ULL
62
63 # endif // KM_WIN32
64
65 # include <stdio.h>
66 # include <assert.h>
67 # include <stdlib.h>
68 # include <limits.h>
69
70 typedef unsigned char  byte_t;
71 typedef char           i8_t;
72 typedef unsigned char  ui8_t;
73 typedef short          i16_t;
74 typedef unsigned short ui16_t;
75 typedef int            i32_t;
76 typedef unsigned int   ui32_t;
77
78
79 namespace Kumu
80 {
81   inline ui16_t Swap2(ui16_t i)
82     {
83       return ( (i << 8) | (( i & 0xff00) >> 8) );
84     }
85
86   inline ui32_t Swap4(ui32_t i)
87     {
88       return
89         ( (i & 0x000000ffUL) << 24 ) |
90         ( (i & 0xff000000UL) >> 24 ) |
91         ( (i & 0x0000ff00UL) << 8  ) |
92         ( (i & 0x00ff0000UL) >> 8  );
93     }
94
95   inline ui64_t Swap8(ui64_t i)
96     {
97       return
98         ( (i & ui64_C(0x00000000000000FF)) << 56 ) |
99         ( (i & ui64_C(0xFF00000000000000)) >> 56 ) |
100         ( (i & ui64_C(0x000000000000FF00)) << 40 ) |
101         ( (i & ui64_C(0x00FF000000000000)) >> 40 ) |
102         ( (i & ui64_C(0x0000000000FF0000)) << 24 ) |
103         ( (i & ui64_C(0x0000FF0000000000)) >> 24 ) |
104         ( (i & ui64_C(0x00000000FF000000)) << 8  ) |
105         ( (i & ui64_C(0x000000FF00000000)) >> 8  );
106     }
107
108   //
109   template<class T>
110     inline T xmin(T lhs, T rhs) {
111     return (lhs < rhs) ? lhs : rhs;
112   }
113
114   //
115   template<class T>
116     inline T xmax(T lhs, T rhs) {
117     return (lhs > rhs) ? lhs : rhs;
118   }
119
120   //
121   template<class T>
122     inline T xclamp(T v, T l, T h) {
123     if ( v < l ) return l;
124     if ( v > h ) return h;
125     return v;
126   }
127
128
129   // read an integer from byte-structured storage
130   template<class T>
131   inline T    cp2i(const byte_t* p) { return *(T*)p; }
132
133   // write an integer to byte-structured storage
134   template<class T>
135   inline void i2p(T i, byte_t* p) { *(T*)p = i; }
136
137 # ifdef KM_BIG_ENDIAN
138 #  define KM_i16_LE(i)        Kumu::Swap2(i)
139 #  define KM_i32_LE(i)        Kumu::Swap4(i)
140 #  define KM_i64_LE(i)        Kumu::Swap8(i)
141 #  define KM_i16_BE(i)        (i)
142 #  define KM_i32_BE(i)        (i)
143 #  define KM_i64_BE(i)        (i)
144 # else
145 #  define KM_i16_LE(i)        (i)
146 #  define KM_i32_LE(i)        (i)
147 #  define KM_i64_LE(i)        (i)
148 #  define KM_i16_BE(i)        Kumu::Swap2(i)
149 #  define KM_i32_BE(i)        Kumu::Swap4(i)
150 #  define KM_i64_BE(i)        Kumu::Swap8(i)
151 # endif // KM_BIG_ENDIAN
152
153   // A non-reference counting, auto-delete container for internal
154   // member object pointers.
155   template <class T>
156     class mem_ptr
157     {
158       mem_ptr(T&);
159
160     protected:
161       T* m_p; // the thing we point to
162
163     public:
164       mem_ptr() : m_p(0) {}
165       mem_ptr(T* p) : m_p(p) {}
166       ~mem_ptr() { delete m_p; }
167
168       inline T&   operator*()  const { return *m_p; }
169       inline T*   operator->() const { return m_p; }
170       inline      operator T*()const { return m_p; }
171       inline const mem_ptr<T>& operator=(T* p) { this->set(p); return *this; }
172       inline T*   set(T* p)          { delete m_p; m_p = p; return m_p; }
173       inline T*   get()        const { return m_p; }
174       inline void release()          { m_p = 0; }
175       inline bool empty()      const { return m_p == 0; }
176     };
177
178 } // namespace Kumu
179
180 // Produces copy constructor boilerplate. Allows convenient private
181 // declatarion of copy constructors to prevent the compiler from
182 // silently manufacturing default methods.
183 # define KM_NO_COPY_CONSTRUCT(T)   \
184           T(const T&); \
185           T& operator=(const T&)
186
187 #endif // _KM_PLATFORM_H_
188
189 //
190 // KM_platform.h
191 //