2 * @brief Read/write unaligned 1, 2, 4 byte integers.
4 /* Copyright 1999,2000,2001 BrightStation PLC
5 * Copyright 2002,2004,2008,2011 Olly Betts
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
23 #ifndef XAPIAN_INCLUDED_UNALIGNED_H
24 #define XAPIAN_INCLUDED_UNALIGNED_H
28 // FIXME: 65536 in Asserts below is the max chert block size. We should
29 // abstract this out, and use the current block_size to catch overruns better.
31 getint1(const unsigned char *p
, int c
)
34 AssertRel(c
, <, 65536);
39 setint1(unsigned char *p
, int c
, int x
)
42 AssertRel(c
, <, 65536);
43 p
[c
] = static_cast<unsigned char>(x
);
47 getint2(const unsigned char *p
, int c
)
50 AssertRel(c
, <, 65536 - 1);
51 return p
[c
] << 8 | p
[c
+ 1];
55 setint2(unsigned char *p
, int c
, int x
)
58 AssertRel(c
, <, 65536 - 1);
59 p
[c
] = static_cast<unsigned char>(x
>> 8);
60 p
[c
+ 1] = static_cast<unsigned char>(x
);
64 getint4(const unsigned char *p
, int c
)
67 AssertRel(c
, <, 65536 - 3);
68 return p
[c
] << 24 | p
[c
+ 1] << 16 | p
[c
+ 2] << 8 | p
[c
+ 3];
72 setint4(unsigned char *p
, int c
, int x
)
75 AssertRel(c
, <, 65536 - 3);
76 p
[c
] = static_cast<unsigned char>(x
>> 24);
77 p
[c
+ 1] = static_cast<unsigned char>(x
>> 16);
78 p
[c
+ 2] = static_cast<unsigned char>(x
>> 8);
79 p
[c
+ 3] = static_cast<unsigned char>(x
);
82 #endif // XAPIAN_INCLUDED_UNALIGNED_H