-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode.c
148 lines (127 loc) · 3.26 KB
/
decode.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* This file is part of FAST Wireshark.
*
* FAST Wireshark is free software: you can redistribute it and/or modify
* it under the terms of the Lesser GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FAST Wireshark is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Lesser GNU General Public License for more details.
*
* You should have received a copy of the Lesser GNU General Public License
* along with FAST Wireshark. If not, see
* <http://www.gnu.org/licenses/lgpl.txt>.
*/
/*!
* \file decode.c
* \brief Decode simple data types directly.
*/
#include <string.h>
#include "decode.h"
/*!
* \brief Determines which bit is the top bit, the leading bit of the byte (left most)
*/
#define StopByte 0x80
/*!
* \brief Number of bits in a byte
*/
#define NBitsInByte 8
guint count_stop_bit_encoded (guint nbytes, const guint8* bytes)
{
guint i;
for (i = 0; i < nbytes; ++i) {
if (bytes[i] & StopByte) {
return i+1;
}
}
return 0;
}
guint number_decoded_bits (guint nbytes)
{
return nbytes * (NBitsInByte -1);
}
void decode_pmap(guint nbytes, const guint8* bytes, gboolean* pmap_res)
{
guint i;
guint8 j;
size_t pmap_idx = 0;
for (i = 0; i < nbytes; ++i) {
for (j = 1; j < NBitsInByte; ++j) {
pmap_res[pmap_idx++] = bytes[i] & (StopByte >> j);
}
}
}
guint32 decode_uint32 (guint nbytes, const guint8* bytes)
{
guint32 n = 0;
guint i;
if (0 == nbytes) return 0;
for (i = 0; i < nbytes -1; ++i) {
n |= bytes[i]; /* Put in lower bits. */
n <<= NBitsInByte -1; /* Scoot over for next data. */
}
/* (i == nbytes -1) */
n |= bytes[i] & ~StopByte;
return n;
}
guint64 decode_uint64 (guint nbytes, const guint8* bytes)
{
guint64 n = 0;
guint i;
if (0 == nbytes) return 0;
for (i = 0; i < nbytes -1; ++i) {
n |= bytes[i]; /* Put in lower bits. */
n <<= NBitsInByte -1; /* Scoot over for next data. */
}
/* (i == nbytes -1) */
n |= bytes[i] & ~StopByte;
return n;
}
gint32 decode_int32 (guint nbytes, const guint8* bytes)
{
gint32 n;
if (0 == nbytes) return 0;
n = (gint32) decode_uint32 (nbytes, bytes);
/* If we have a sign bit, */
if (bytes[0] & (StopByte >> 1)) {
guint nbits;
nbits = number_decoded_bits (nbytes);
if (nbits < 32) {
/* sign extend /n/. */
n |= ~(gint32)0 << nbits;
}
}
return n;
}
gint64 decode_int64 (guint nbytes, const guint8* bytes)
{
gint64 n;
if (0 == nbytes) return 0;
n = (gint64) decode_uint64 (nbytes, bytes);
/* If we have a sign bit, */
if (bytes[0] & (StopByte >> 1)) {
guint nbits;
nbits = number_decoded_bits (nbytes);
if (nbits < 64) {
/* sign extend /n/. */
n |= ~(gint64)0 << nbits;
}
}
return n;
}
void decode_ascii_string (guint nbytes, const guint8* bytes,
guint8* str)
{
if (nbytes > 0) {
memcpy (str, bytes, nbytes*sizeof(guint8));
str[nbytes-1] &= ~StopByte;
}
}
void decode_byte_vector (guint nbytes, const guint8* bytes,
guint8* vec)
{
memcpy (vec, bytes, nbytes*sizeof(guint8));
}