Line data Source code
1 : /* 2 : * Copyright (C) 2000-2012 Free Software Foundation, Inc. 3 : * 4 : * Author: Nikos Mavrogiannopoulos 5 : * 6 : * This file is part of GnuTLS. 7 : * 8 : * The GnuTLS is free software; you can redistribute it and/or 9 : * modify it under the terms of the GNU Lesser General Public License 10 : * as published by the Free Software Foundation; either version 2.1 of 11 : * the License, or (at your option) any later version. 12 : * 13 : * This library is distributed in the hope that it will be useful, but 14 : * WITHOUT ANY WARRANTY; without even the implied warranty of 15 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 : * Lesser General Public License for more details. 17 : * 18 : * You should have received a copy of the GNU Lesser General Public License 19 : * along with this program. If not, see <https://www.gnu.org/licenses/> 20 : * 21 : */ 22 : 23 : #ifndef GNUTLS_LIB_NUM_H 24 : #define GNUTLS_LIB_NUM_H 25 : 26 : #include "gnutls_int.h" 27 : 28 : #include <minmax.h> 29 : #include <byteswap.h> 30 : 31 : /* data should be at least 3 bytes */ 32 73285 : inline static uint32_t _gnutls_read_uint24(const uint8_t * data) 33 : { 34 70191 : return (data[0] << 16) | (data[1] << 8) | (data[2]); 35 : } 36 : 37 11023 : inline static uint64_t _gnutls_read_uint64(const uint8_t * data) 38 : { 39 11023 : uint64_t res; 40 : 41 11023 : memcpy(&res, data, sizeof(uint64_t)); 42 : #ifndef WORDS_BIGENDIAN 43 11023 : res = bswap_64(res); 44 : #endif 45 11023 : return res; 46 : } 47 : 48 7885039 : inline static void _gnutls_write_uint64(uint64_t num, uint8_t * data) 49 : { 50 : #ifndef WORDS_BIGENDIAN 51 3161529 : num = bswap_64(num); 52 : #endif 53 3161529 : memcpy(data, &num, 8); 54 10142 : } 55 : 56 144580 : inline static void _gnutls_write_uint24(uint32_t num, uint8_t * data) 57 : { 58 144580 : data[0] = num >> 16; 59 144580 : data[1] = num >> 8; 60 142198 : data[2] = num; 61 2382 : } 62 : 63 845102 : inline static uint32_t _gnutls_read_uint32(const uint8_t * data) 64 : { 65 845102 : uint32_t res; 66 : 67 845102 : memcpy(&res, data, sizeof(uint32_t)); 68 : #ifndef WORDS_BIGENDIAN 69 839164 : res = bswap_32(res); 70 : #endif 71 839164 : return res; 72 : } 73 : 74 1160108 : inline static void _gnutls_write_uint32(uint32_t num, uint8_t * data) 75 : { 76 : 77 : #ifndef WORDS_BIGENDIAN 78 1160108 : num = bswap_32(num); 79 : #endif 80 1154931 : memcpy(data, &num, sizeof(uint32_t)); 81 774222 : } 82 : 83 6107868 : inline static uint16_t _gnutls_read_uint16(const uint8_t * data) 84 : { 85 6107868 : uint16_t res; 86 6107868 : memcpy(&res, data, sizeof(uint16_t)); 87 : #ifndef WORDS_BIGENDIAN 88 4694930 : res = bswap_16(res); 89 : #endif 90 4694930 : return res; 91 : } 92 : 93 5041727 : inline static void _gnutls_write_uint16(uint16_t num, uint8_t * data) 94 : { 95 : 96 : #ifndef WORDS_BIGENDIAN 97 5034050 : num = bswap_16(num); 98 : #endif 99 2519311 : memcpy(data, &num, sizeof(uint16_t)); 100 2614204 : } 101 : 102 : inline static uint32_t _gnutls_conv_uint32(uint32_t data) 103 : { 104 : #ifndef WORDS_BIGENDIAN 105 : return bswap_32(data); 106 : #else 107 : return data; 108 : #endif 109 : } 110 : 111 4731867 : inline static uint16_t _gnutls_conv_uint16(uint16_t data) 112 : { 113 : #ifndef WORDS_BIGENDIAN 114 4731867 : return bswap_16(data); 115 : #else 116 : return data; 117 : #endif 118 : } 119 : 120 : #endif /* GNUTLS_LIB_NUM_H */