|
Packit |
aea12f |
/*
|
|
Packit |
aea12f |
* Copyright (C) 2000-2012 Free Software Foundation, Inc.
|
|
Packit |
aea12f |
*
|
|
Packit |
aea12f |
* Author: Nikos Mavrogiannopoulos
|
|
Packit |
aea12f |
*
|
|
Packit |
aea12f |
* This file is part of GnuTLS.
|
|
Packit |
aea12f |
*
|
|
Packit |
aea12f |
* The GnuTLS is free software; you can redistribute it and/or
|
|
Packit |
aea12f |
* modify it under the terms of the GNU Lesser General Public License
|
|
Packit |
aea12f |
* as published by the Free Software Foundation; either version 2.1 of
|
|
Packit |
aea12f |
* the License, or (at your option) any later version.
|
|
Packit |
aea12f |
*
|
|
Packit |
aea12f |
* This library is distributed in the hope that it will be useful, but
|
|
Packit |
aea12f |
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
aea12f |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
aea12f |
* Lesser General Public License for more details.
|
|
Packit |
aea12f |
*
|
|
Packit |
aea12f |
* You should have received a copy of the GNU Lesser General Public License
|
|
Packit |
aea12f |
* along with this program. If not, see <https://www.gnu.org/licenses/>
|
|
Packit |
aea12f |
*
|
|
Packit |
aea12f |
*/
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#ifndef GNUTLS_LIB_NUM_H
|
|
Packit |
aea12f |
#define GNUTLS_LIB_NUM_H
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#include "gnutls_int.h"
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#include <minmax.h>
|
|
Packit |
aea12f |
#include <byteswap.h>
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
/* data should be at least 3 bytes */
|
|
Packit |
aea12f |
inline static uint32_t _gnutls_read_uint24(const uint8_t * data)
|
|
Packit |
aea12f |
{
|
|
Packit Service |
991b93 |
return (data[0] << 16) | (data[1] << 8) | (data[2]);
|
|
Packit Service |
991b93 |
}
|
|
Packit |
aea12f |
|
|
Packit Service |
991b93 |
inline static uint64_t _gnutls_read_uint64(const uint8_t * data)
|
|
Packit Service |
991b93 |
{
|
|
Packit Service |
991b93 |
uint64_t res;
|
|
Packit |
aea12f |
|
|
Packit Service |
991b93 |
memcpy(&res, data, sizeof(uint64_t));
|
|
Packit |
aea12f |
#ifndef WORDS_BIGENDIAN
|
|
Packit Service |
991b93 |
res = bswap_64(res);
|
|
Packit |
aea12f |
#endif
|
|
Packit |
aea12f |
return res;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
inline static void _gnutls_write_uint64(uint64_t num, uint8_t * data)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
#ifndef WORDS_BIGENDIAN
|
|
Packit |
aea12f |
num = bswap_64(num);
|
|
Packit |
aea12f |
#endif
|
|
Packit |
aea12f |
memcpy(data, &num, 8);
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
inline static void _gnutls_write_uint24(uint32_t num, uint8_t * data)
|
|
Packit |
aea12f |
{
|
|
Packit Service |
991b93 |
data[0] = num >> 16;
|
|
Packit Service |
991b93 |
data[1] = num >> 8;
|
|
Packit Service |
991b93 |
data[2] = num;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
inline static uint32_t _gnutls_read_uint32(const uint8_t * data)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
uint32_t res;
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
memcpy(&res, data, sizeof(uint32_t));
|
|
Packit |
aea12f |
#ifndef WORDS_BIGENDIAN
|
|
Packit |
aea12f |
res = bswap_32(res);
|
|
Packit |
aea12f |
#endif
|
|
Packit |
aea12f |
return res;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
inline static void _gnutls_write_uint32(uint32_t num, uint8_t * data)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#ifndef WORDS_BIGENDIAN
|
|
Packit |
aea12f |
num = bswap_32(num);
|
|
Packit |
aea12f |
#endif
|
|
Packit |
aea12f |
memcpy(data, &num, sizeof(uint32_t));
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
inline static uint16_t _gnutls_read_uint16(const uint8_t * data)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
uint16_t res;
|
|
Packit |
aea12f |
memcpy(&res, data, sizeof(uint16_t));
|
|
Packit |
aea12f |
#ifndef WORDS_BIGENDIAN
|
|
Packit |
aea12f |
res = bswap_16(res);
|
|
Packit |
aea12f |
#endif
|
|
Packit |
aea12f |
return res;
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
inline static void _gnutls_write_uint16(uint16_t num, uint8_t * data)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#ifndef WORDS_BIGENDIAN
|
|
Packit |
aea12f |
num = bswap_16(num);
|
|
Packit |
aea12f |
#endif
|
|
Packit |
aea12f |
memcpy(data, &num, sizeof(uint16_t));
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
inline static uint32_t _gnutls_conv_uint32(uint32_t data)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
#ifndef WORDS_BIGENDIAN
|
|
Packit |
aea12f |
return bswap_32(data);
|
|
Packit |
aea12f |
#else
|
|
Packit |
aea12f |
return data;
|
|
Packit |
aea12f |
#endif
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
inline static uint16_t _gnutls_conv_uint16(uint16_t data)
|
|
Packit |
aea12f |
{
|
|
Packit |
aea12f |
#ifndef WORDS_BIGENDIAN
|
|
Packit |
aea12f |
return bswap_16(data);
|
|
Packit |
aea12f |
#else
|
|
Packit |
aea12f |
return data;
|
|
Packit |
aea12f |
#endif
|
|
Packit |
aea12f |
}
|
|
Packit |
aea12f |
|
|
Packit |
aea12f |
#endif /* GNUTLS_LIB_NUM_H */
|