2 * Copyright (C) 2017 Cisco Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 // @author Changxue Deng <chadeng@cisco.com>
19 #ifndef __INTEGER_4B_5B_H__
20 #define __INTEGER_4B_5B_H__
27 #define MAX_4B_OFFSET 0xFFFFFFFF
28 #define MAX_5B_OFFSET 0xFFFFFFFFFF
29 #define MAX_6B_OFFSET 0xFFFFFFFFFFFF
31 // write and read 5-byte 6-byte unsigned integer
32 // Note this is based on engianness.
34 inline void Write5BInteger(uint8_t *buffer, size_t offset)
37 if(offset > MAX_5B_OFFSET)
39 std::cerr << "OFFSET " << offset << " TOO LARGE FOR 5 BYTES\n";
44 uint8_t *src = reinterpret_cast<uint8_t*>(&offset);
45 #ifndef __BIG__ENDIAN__
46 memcpy(buffer, src, 5);
48 memcpy(buffer, src+3, 5);
52 inline size_t Get5BInteger(const uint8_t *buffer)
55 uint8_t *target = reinterpret_cast<uint8_t*>(&offset);
56 #ifndef __BIG__ENDIAN__
57 memcpy(target, buffer, 5);
59 memcpy(target+3, buffer, 5);
64 inline void Write6BInteger(uint8_t *buffer, size_t offset)
67 if(offset > MAX_6B_OFFSET)
69 std::cerr << "OFFSET " << offset << "TOO LARGE FOR SIX BYTES\n";
74 uint8_t *src = reinterpret_cast<uint8_t*>(&offset);
75 #ifndef __BIG__ENDIAN__
76 memcpy(buffer, src, 6);
78 memcpy(buffer, src+2, 6);
82 inline size_t Get6BInteger(const uint8_t *buffer)
85 uint8_t *target = reinterpret_cast<uint8_t*>(&offset);
86 #ifndef __BIG__ENDIAN__
87 memcpy(target, buffer, 6);
89 memcpy(target+2, buffer, 6);