1 : /*-----------------------------------------------------------------------------
2 : Copyright (C) 2011 SIL International
3 : Responsibility: Tim Eves
4 :
5 : This library is free software; you can redistribute it and/or modify
6 : it under the terms of the GNU Lesser General Public License as published
7 : by the Free Software Foundation; either version 2.1 of License, or
8 : (at your option) any later version.
9 :
10 : This program is distributed in the hope that it will be useful,
11 : but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 : Lesser General Public License for more details.
14 :
15 : You should also have received a copy of the GNU Lesser General Public
16 : License along with this library in the file named "LICENSE".
17 : If not, write to the Free Software Foundation, 51 Franklin Street,
18 : Suite 500, Boston, MA 02110-1335, USA or visit their web page on the
19 : internet at http://www.fsf.org/licenses/lgpl.html.
20 :
21 : Alternatively, the contents of this file may be used under the terms of the
22 : Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public
23 : License, as published by the Free Software Foundation, either version 2
24 : of the License or (at your option) any later version.
25 :
26 : Description:
27 : A set of fast template based decoders for decoding values of any C integer
28 : type up to long int size laid out with most significant byte first or least
29 : significant byte first (aka big endian or little endian). These are CPU
30 : byte order agnostic and will function the same regardless of the CPUs native
31 : byte order.
32 : Being template based means if the either le or be class is not used then
33 : template code of unused functions will not be instantiated by the compiler
34 : and thus shouldn't cause any overhead.
35 : -----------------------------------------------------------------------------*/
36 :
37 : class be
38 : {
39 : template<int S>
40 0 : inline static unsigned long int _peek(const unsigned char * p) {
41 0 : return _peek<S/2>(p) << (S/2)*8 | _peek<S/2>(p+S/2);
42 : }
43 : public:
44 : template<typename T>
45 0 : inline static T peek(const void * p) {
46 0 : return T(_peek<sizeof(T)>(static_cast<const unsigned char *>(p)));
47 : }
48 :
49 : template<typename T>
50 0 : inline static T read(const unsigned char * &p) {
51 0 : const T r = T(_peek<sizeof(T)>(p));
52 0 : p += sizeof r;
53 0 : return r;
54 : }
55 :
56 : template<typename T>
57 0 : inline static T swap(const T x) {
58 0 : return T(_peek<sizeof(T)>(reinterpret_cast<const unsigned char *>(&x)));
59 : }
60 :
61 : template<typename T>
62 0 : inline static void skip(const unsigned char * &p, size_t n=1) {
63 0 : p += sizeof(T)*n;
64 0 : }
65 : };
66 :
67 : template<>
68 0 : inline unsigned long int be::_peek<1>(const unsigned char * p) { return *p; }
69 :
70 :
71 : class le
72 : {
73 : template<int S>
74 : inline static unsigned long int _peek(const unsigned char * p) {
75 : return _peek<S/2>(p) | _peek<S/2>(p+S/2) << (S/2)*8;
76 : }
77 : public:
78 : template<typename T>
79 : inline static T peek(const void * p) {
80 : return T(_peek<sizeof(T)>(static_cast<const unsigned char *>(p)));
81 : }
82 :
83 : template<typename T>
84 : inline static T read(const unsigned char * &p) {
85 : const T r = T(_peek<sizeof(T)>(p));
86 : p += sizeof r;
87 : return r;
88 : }
89 :
90 : template<typename T>
91 : inline static T swap(const T x) {
92 : return T(_peek<sizeof(T)>(reinterpret_cast<const unsigned char *>(&x)));
93 : }
94 :
95 : template<typename T>
96 : inline static void skip(const unsigned char * &p, size_t n=1) {
97 : p += sizeof(T)*n;
98 : }
99 : };
100 :
101 : template<>
102 : inline unsigned long int le::_peek<1>(const unsigned char * p) { return *p; }
103 :
|