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
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)
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
70 inline ui16_t Swap2(ui16_t i)
72 return ( (i << 8) | (( i & 0xff00) >> 8) );
75 inline ui32_t Swap4(ui32_t i)
78 ( (i & 0x000000ffUL) << 24 ) |
79 ( (i & 0xff000000UL) >> 24 ) |
80 ( (i & 0x0000ff00UL) << 8 ) |
81 ( (i & 0x00ff0000UL) >> 8 );
84 inline ui64_t Swap8(ui64_t i)
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 );
99 inline T xmin(T lhs, T rhs)
101 return (lhs < rhs) ? lhs : rhs;
106 inline T xmax(T lhs, T rhs)
108 return (lhs > rhs) ? lhs : rhs;
111 // read an integer from byte-structured storage
113 inline T cp2i(const byte_t* p) { return *(T*)p; }
115 // write an integer to byte-structured storage
117 inline void i2p(T i, byte_t* p) { *(T*)p = i; }
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)
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
136 // 64 bit integer/text conversion
138 const ui32_t IntBufferLen = 32;
140 inline i64_t atoi64(const char *str) {
144 return strtoll(str, NULL, 10);
148 inline const char* i64sz(i64_t i, char* buf)
152 sprintf(buf, "%I64d", i);
154 sprintf(buf, "%lld", i);
159 inline const char* ui64sz(ui64_t i, char* buf)
163 sprintf(buf, "%I64u", i);
165 sprintf(buf, "%llu", i);
170 inline const char* i64szx(i64_t i, ui32_t digits, char* buf)
173 if ( digits > 30 ) digits = 30;
175 sprintf(buf,"%0*I64x", digits, i);
177 sprintf(buf,"%0*llx", digits, i);
186 #endif // _AS_DCP_SYSTEM_H_