2
* Copyright (C) Jan 2013 Mellanox Technologies Ltd. All rights reserved.
4
* This software is available to you under a choice of one of two
5
* licenses. You may choose to be licensed under the terms of the GNU
6
* General Public License (GPL) Version 2, available from the file
7
* COPYING in the main directory of this source tree, or the
8
* OpenIB.org BSD license below:
10
* Redistribution and use in source and binary forms, with or
11
* without modification, are permitted provided that the following
14
* - Redistributions of source code must retain the above
15
* copyright notice, this list of conditions and the following
18
* - Redistributions in binary form must reproduce the above
19
* copyright notice, this list of conditions and the following
20
* disclaimer in the documentation and/or other materials
21
* provided with the distribution.
23
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34
#include "packets_common.h"
38
#include <sys/types.h>
40
#include <Winsock2.h> // for htonl
44
/************************************/
45
void push_to_buff_64(u_int8_t *buff, u_int32_t bit_offset, u_int64_t field_value)
48
u_int32_t *buffer = PTR_32_OF_BUFF(buff, (bit_offset / 8));
49
u_int64_t value = CPU_TO_BE64(field_value);
50
memcpy(buffer, &value, sizeof(value));
52
u_int64_t *buffer = PTR_64_OF_BUFF(buff, (bit_offset / 8));
53
memcpy(buffer, &field_value, sizeof(field_value));
54
*buffer = CPU_TO_BE64(*buffer);
59
/************************************/
60
void push_to_buff_32(u_int8_t *buff, u_int32_t bit_offset, u_int32_t field_value)
62
u_int32_t *buffer = PTR_32_OF_BUFF(buff, (bit_offset / 8));
63
memcpy(buffer, &field_value, sizeof(field_value));
64
*buffer = CPU_TO_BE32(*buffer);
68
/************************************/
69
//the next function will push the field into the buffer by inserting it's MSB bits first
70
//and therefore by doing it we save the CPU_TO_BE operation
71
void push_to_buff(u_int8_t *buff, u_int32_t bit_offset, u_int32_t field_size, u_int32_t field_value)
74
u_int32_t byte_n = bit_offset / 8;
75
u_int32_t byte_n_offset = bit_offset % 8;
78
//going over all bits in field
79
while (i < field_size) {
80
to_push = PCK_MIN(8 - byte_n_offset, field_size - i);
82
//printf("Inserting %u bits(%u) to byte %u to bit %u\n", to_push, EXTRACT8(field_value, field_size - i, to_push), byte_n, 8 - to_push - byte_n_offset);
83
INSERTF_8(BYTE_N(buff, byte_n), 8 - to_push - byte_n_offset, field_value, field_size - i, to_push);
84
byte_n_offset = 0; //(byte_n_offset + to_push) % 8;
90
/************************************/
91
u_int64_t pop_from_buff_64(u_int8_t *buff, u_int32_t bit_offset)
95
memcpy(&value, PTR_32_OF_BUFF(buff, (bit_offset / 8)), sizeof(u_int64_t));
96
return (BE64_TO_CPU(value));
98
return (BE64_TO_CPU(FIELD_64_OF_BUFF(buff, (bit_offset / 8))));
103
/************************************/
104
u_int32_t pop_from_buff_32(u_int8_t *buff, u_int32_t bit_offset)
106
return (BE32_TO_CPU(FIELD_32_OF_BUFF(buff, (bit_offset / 8))));
110
/************************************/
111
//the next function will pop the field into the buffer by removing it's MSB bits first
112
//and therefore by doing it we save the BE_TO_CPU operation
113
u_int32_t pop_from_buff(u_int8_t *buff, u_int32_t bit_offset, u_int32_t field_size)
116
u_int32_t byte_n = bit_offset / 8;
117
u_int32_t byte_n_offset = bit_offset % 8;
118
u_int32_t field_32 = 0;
121
//going over all bits in field
122
while (i < field_size) {
123
to_pop = PCK_MIN(8 - byte_n_offset, field_size - i);
125
//printf("Removing %u bits(%u) from byte %u to bit %u\n", to_pop, EXTRACT8(BYTE_N(buff, byte_n), 8 - to_pop - byte_n_offset, to_pop), byte_n, field_size - i);
126
INSERTF_8(field_32, field_size - i, BYTE_N(buff, byte_n), 8 - to_pop - byte_n_offset, to_pop);
127
byte_n_offset = 0; //(byte_n_offset + to_pop) % 8;