2 Copyright (c) 2004-2005, John Hurst
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
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.
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.
27 /*! \file AS_DCP_system.h
29 \brief AS-DCP portable types and byte swapping
33 #ifndef _AS_DCP_SYSTEM_H_
34 #define _AS_DCP_SYSTEM_H_
37 #define ASDCP_BIG_ENDIAN
40 // 64 bit types are not used in the public interface
42 #define WIN32_LEAN_AND_MEAN
45 #pragma warning(disable:4786) // Ignore "identifer > 255 characters" warning
46 #define snprintf _snprintf
48 #ifndef ASDCP_NO_BASE_TYPES
49 typedef unsigned __int64 ui64_t;
50 typedef __int64 i64_t;
51 #define i64_C(c) (i64_t)(c)
52 #define ui64_C(c) (ui64_t)(c)
57 #ifndef ASDCP_NO_BASE_TYPES
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
71 inline ui16_t Swap2(ui16_t i)
73 return ( (i << 8) | (( i & 0xff00) >> 8) );
76 inline ui32_t Swap4(ui32_t i)
79 ( (i & 0x000000ffUL) << 24 ) |
80 ( (i & 0xff000000UL) >> 24 ) |
81 ( (i & 0x0000ff00UL) << 8 ) |
82 ( (i & 0x00ff0000UL) >> 8 );
85 inline ui64_t Swap8(ui64_t i)
88 ( (i & ui64_C(0x00000000000000FF)) << 56 ) |
89 ( (i & ui64_C(0xFF00000000000000)) >> 56 ) |
90 ( (i & ui64_C(0x000000000000FF00)) << 40 ) |
91 ( (i & ui64_C(0x00FF000000000000)) >> 40 ) |
92 ( (i & ui64_C(0x0000000000FF0000)) << 24 ) |
93 ( (i & ui64_C(0x0000FF0000000000)) >> 24 ) |
94 ( (i & ui64_C(0x00000000FF000000)) << 8 ) |
95 ( (i & ui64_C(0x000000FF00000000)) >> 8 );
100 inline T xmin(T lhs, T rhs)
102 return (lhs < rhs) ? lhs : rhs;
107 inline T xmax(T lhs, T rhs)
109 return (lhs > rhs) ? lhs : rhs;
112 // read an integer from byte-structured storage
114 inline T cp2i(const byte_t* p) { return *(T*)p; }
116 // write an integer to byte-structured storage
118 inline void i2p(T i, byte_t* p) { *(T*)p = i; }
120 #ifdef ASDCP_BIG_ENDIAN
121 #define ASDCP_i16_LE(i) ASDCP::Swap2(i)
122 #define ASDCP_i32_LE(i) ASDCP::Swap4(i)
123 #define ASDCP_i64_LE(i) ASDCP::Swap8(i)
124 #define ASDCP_i16_BE(i) (i)
125 #define ASDCP_i32_BE(i) (i)
126 #define ASDCP_i64_BE(i) (i)
128 #define ASDCP_i16_LE(i) (i)
129 #define ASDCP_i32_LE(i) (i)
130 #define ASDCP_i64_LE(i) (i)
131 #define ASDCP_i16_BE(i) ASDCP::Swap2(i)
132 #define ASDCP_i32_BE(i) ASDCP::Swap4(i)
133 #define ASDCP_i64_BE(i) ASDCP::Swap8(i)
134 #endif // ASDCP_BIG_ENDIAN
137 // 64 bit integer/text conversion
139 const ui32_t IntBufferLen = 32;
141 inline i64_t atoi64(const char *str) {
145 return strtoll(str, NULL, 10);
149 inline const char* i64sz(i64_t i, char* buf)
153 snprintf(buf, IntBufferLen, "%I64d", i);
155 snprintf(buf, IntBufferLen, "%lld", i);
160 inline const char* ui64sz(ui64_t i, char* buf)
164 snprintf(buf, IntBufferLen, "%I64u", i);
166 snprintf(buf, IntBufferLen, "%llu", i);
171 inline const char* i64szx(i64_t i, ui32_t digits, char* buf)
174 if ( digits > 30 ) digits = 30;
176 snprintf(buf, IntBufferLen, "%0*I64x", digits, i);
178 snprintf(buf, IntBufferLen, "%0*llx", digits, i);
187 #endif // _AS_DCP_SYSTEM_H_