pre-release commit
[asdcplib.git] / AS_DCP_system.h
1 /*
2 Copyright (c) 2004-2005, 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    AS_DCP_system.h
28     \version $Id$
29     \brief   AS-DCP portable types and byte swapping
30 */
31
32
33 #ifndef _AS_DCP_SYSTEM_H_
34 #define _AS_DCP_SYSTEM_H_
35
36 #ifdef __APPLE__
37 #define ASDCP_BIG_ENDIAN
38 #endif
39
40 // 64 bit types are not used in the public interface
41 #ifdef WIN32
42 #define WIN32_LEAN_AND_MEAN
43 #define VC_EXTRALEAN
44 #include <windows.h>
45 #pragma warning(disable:4786)                   // Ignore "identifer > 255 characters" warning
46
47 #ifndef ASDCP_NO_BASE_TYPES
48 typedef unsigned __int64   ui64_t;
49 typedef __int64            i64_t;
50 #define i64_C(c)  (i64_t)(c)
51 #define ui64_C(c) (ui64_t)(c)
52 #endif
53
54 #else // WIN32
55
56 #ifndef ASDCP_NO_BASE_TYPES
57 typedef unsigned long long ui64_t;
58 typedef long long          i64_t;
59 #define i64_C(c)  c##LL
60 #define ui64_C(c) c##ULL
61 #endif
62
63 #endif // WIN32
64
65 #include <AS_DCP.h>
66 #include <assert.h>
67
68 namespace ASDCP {
69
70   inline ui16_t Swap2(ui16_t i)
71     {
72       return ( (i << 8) | (( i & 0xff00) >> 8) );
73     }
74
75   inline ui32_t Swap4(ui32_t i)
76     {
77       return
78         ( (i & 0x000000ffUL) << 24 ) |
79         ( (i & 0xff000000UL) >> 24 ) |
80         ( (i & 0x0000ff00UL) << 8  ) |
81         ( (i & 0x00ff0000UL) >> 8  );
82     }
83
84   inline ui64_t Swap8(ui64_t i)
85     {
86       return
87         ( (i & ui64_C(0x00000000000000FF)) << 56 ) |
88         ( (i & ui64_C(0xFF00000000000000)) >> 56 ) |
89         ( (i & ui64_C(0x000000000000FF00)) << 40 ) |
90         ( (i & ui64_C(0x00FF000000000000)) >> 40 ) |
91         ( (i & ui64_C(0x0000000000FF0000)) << 24 ) |
92         ( (i & ui64_C(0x0000FF0000000000)) >> 24 ) |
93         ( (i & ui64_C(0x00000000FF000000)) << 8  ) |
94         ( (i & ui64_C(0x000000FF00000000)) >> 8  );
95     }
96
97   //
98   template<class T>
99   inline T xmin(T lhs, T rhs)
100     {
101       return (lhs < rhs) ? lhs : rhs;
102     }
103
104   //
105   template<class T>
106   inline T xmax(T lhs, T rhs)
107     {
108       return (lhs > rhs) ? lhs : rhs;
109     }
110
111   // read an integer from byte-structured storage
112   template<class T>
113   inline T    cp2i(const byte_t* p) { return *(T*)p; }
114
115   // write an integer to byte-structured storage
116   template<class T>
117   inline void i2p(T i, byte_t* p) { *(T*)p = i; }
118
119 #ifdef ASDCP_BIG_ENDIAN
120 #define ASDCP_i16_LE(i)        ASDCP::Swap2(i)
121 #define ASDCP_i32_LE(i)        ASDCP::Swap4(i)
122 #define ASDCP_i64_LE(i)        ASDCP::Swap8(i)
123 #define ASDCP_i16_BE(i)        (i)
124 #define ASDCP_i32_BE(i)        (i)
125 #define ASDCP_i64_BE(i)        (i)
126 #else
127 #define ASDCP_i16_LE(i)        (i)
128 #define ASDCP_i32_LE(i)        (i)
129 #define ASDCP_i64_LE(i)        (i)
130 #define ASDCP_i16_BE(i)        ASDCP::Swap2(i)
131 #define ASDCP_i32_BE(i)        ASDCP::Swap4(i)
132 #define ASDCP_i64_BE(i)        ASDCP::Swap8(i)
133 #endif // ASDCP_BIG_ENDIAN
134
135
136 // 64 bit integer/text conversion
137 //
138   const ui32_t IntBufferLen = 32;
139
140   inline i64_t atoi64(const char *str) {
141 #ifdef WIN32
142     return _atoi64(str);
143 #else
144     return strtoll(str, NULL, 10);
145 #endif
146   }
147
148   inline const char* i64sz(i64_t i, char* buf)
149     { 
150       assert(buf);
151 #ifdef WIN32
152       sprintf(buf, "%I64d", i);
153 #else
154       sprintf(buf, "%lld", i);
155 #endif
156       return buf;
157     }
158
159   inline const char* ui64sz(ui64_t i, char* buf)
160     { 
161       assert(buf);
162 #ifdef WIN32
163       sprintf(buf, "%I64u", i);
164 #else
165       sprintf(buf, "%llu", i);
166 #endif
167       return buf;
168     }
169
170   inline const char* i64szx(i64_t i, ui32_t digits, char* buf)
171     {
172       assert(buf);
173       if ( digits > 30 ) digits = 30;
174 #ifdef WIN32
175       sprintf(buf,"%0*I64x", digits, i);
176 #else
177       sprintf(buf,"%0*llx", digits, i);
178 #endif
179       return buf;
180     }
181
182 } // namespace ASDCP
183
184
185
186 #endif // _AS_DCP_SYSTEM_H_
187
188 //
189 // AS_DCP_system.h
190 //