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