1 // Copyright 2001,2007 Alan Donovan. All rights reserved.
2 //
3 // Author: Alan Donovan <adonovan@google.com>
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //    http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 // common.h -- common definitions.
18 //
19 
20 #ifndef INCLUDED_DEVTOOLS_IJAR_COMMON_H
21 #define INCLUDED_DEVTOOLS_IJAR_COMMON_H
22 
23 #include <stddef.h>
24 #include <stdint.h>
25 #include <string.h>
26 
27 namespace devtools_ijar {
28 
29 typedef unsigned long long u8;
30 typedef uint32_t u4;
31 typedef uint16_t u2;
32 typedef uint8_t  u1;
33 
34 // be = big endian, le = little endian
35 
get_u1(const u1 * & p)36 inline u1 get_u1(const u1 *&p) {
37     return *p++;
38 }
39 
get_u2be(const u1 * & p)40 inline u2 get_u2be(const u1 *&p) {
41     u4 x = (p[0] << 8) | p[1];
42     p += 2;
43     return x;
44 }
45 
get_u2le(const u1 * & p)46 inline u2 get_u2le(const u1 *&p) {
47     u4 x = (p[1] << 8) | p[0];
48     p += 2;
49     return x;
50 }
51 
get_u4be(const u1 * & p)52 inline u4 get_u4be(const u1 *&p) {
53     u4 x = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
54     p += 4;
55     return x;
56 }
57 
get_u4le(const u1 * & p)58 inline u4 get_u4le(const u1 *&p) {
59     u4 x = (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0];
60     p += 4;
61     return x;
62 }
63 
put_u1(u1 * & p,u1 x)64 inline void put_u1(u1 *&p, u1 x) {
65     *p++ = x;
66 }
67 
put_u2be(u1 * & p,u2 x)68 inline void put_u2be(u1 *&p, u2 x) {
69     *p++ = x >> 8;
70     *p++ = x & 0xff;
71 }
72 
put_u2le(u1 * & p,u2 x)73 inline void put_u2le(u1 *&p, u2 x) {
74     *p++ = x & 0xff;
75     *p++ = x >> 8;;
76 }
77 
put_u4be(u1 * & p,u4 x)78 inline void put_u4be(u1 *&p, u4 x) {
79     *p++ = x >> 24;
80     *p++ = (x >> 16) & 0xff;
81     *p++ = (x >> 8) & 0xff;
82     *p++ = x & 0xff;
83 }
84 
put_u4le(u1 * & p,u4 x)85 inline void put_u4le(u1 *&p, u4 x) {
86     *p++ = x & 0xff;
87     *p++ = (x >> 8) & 0xff;
88     *p++ = (x >> 16) & 0xff;
89     *p++ = x >> 24;
90 }
91 
92 // Copy n bytes from src to p, and advance p.
put_n(u1 * & p,const u1 * src,size_t n)93 inline void put_n(u1 *&p, const u1 *src, size_t n) {
94   memcpy(p, src, n);
95   p += n;
96 }
97 
98 extern bool verbose;
99 
100 }  // namespace devtools_ijar
101 
102 #endif // INCLUDED_DEVTOOLS_IJAR_COMMON_H
103