-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic-field.c
257 lines (225 loc) · 6.24 KB
/
basic-field.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
* 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 basic-field.c
* \brief Implementation needed for data type definitions.
*/
#include "config.h"
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <epan/wmem/wmem.h>
#include <epan/wmem/wmem_miscutl.h>
#include "debug.h"
#include "basic-field.h"
/*! \brief Converts a string to a decimal.
* \param str String from the parser to be converted to decimal.
* \param value Where the parsed decimal will be stored.
*/
static gboolean string_to_decimal_value (const char* str, FieldValue* value);
/*! \brief Checks if an ascii string is a number.
* \param str String from the parser to be checked.
*/
static gboolean is_number(const char* str);
void init_field_value (FieldValue* value)
{
memset(value, 0, sizeof(FieldValue));
}
void copy_field_value (FieldTypeIdentifier type,
const FieldValue* src,
FieldValue* dest)
{
switch (type) {
case FieldTypeUInt32:
case FieldTypeUInt64:
case FieldTypeInt32:
case FieldTypeInt64:
case FieldTypeDecimal:
memcpy(dest, src, sizeof(FieldValue));
break;
case FieldTypeAsciiString:
case FieldTypeUnicodeString:
case FieldTypeByteVector:
dest->bytevec.nbytes = src->bytevec.nbytes;
dest->bytevec.bytes = (guint8*)wmem_memdup(wmem_epan_scope(),
src->bytevec.bytes,
(1+src->bytevec.nbytes) * sizeof(guint8));
break;
default:
DBG0("Called with bad type.");
break;
}
}
void cleanup_field_value (FieldTypeIdentifier type, FieldValue* value)
{
switch (type) {
case FieldTypeUInt32:
case FieldTypeUInt64:
case FieldTypeInt32:
case FieldTypeInt64:
case FieldTypeDecimal:
/* Nothing. */
break;
case FieldTypeAsciiString:
case FieldTypeUnicodeString:
case FieldTypeByteVector:
value->bytevec.nbytes = 0;
if (value->bytevec.bytes)
value->bytevec.bytes = 0;
break;
default:
DBG0("Called with bad type.");
break;
}
}
static gboolean is_number(const char* str){
int i;
int len;
len=strlen(str);
for(i=0; i<len; i++){
if(!g_ascii_isdigit(str[i]) && str[i]!='-'){
return FALSE;
}
}
return TRUE;
}
gboolean string_to_field_value(const char* str, FieldTypeIdentifier type, FieldValue* value)
{
switch (type) {
int i;
int len;
case FieldTypeUInt32:
if(!is_number(str)){
DBG1("not a number %s", str);
return FALSE;
}
value->u32 = atoi(str);
break;
case FieldTypeInt32:
if(!is_number(str)){
DBG1("not a number %s", str);
return FALSE;
}
value->i32 = atoi(str);
break;
case FieldTypeUInt64:
if(!is_number(str)){
DBG1("not a number %s", str);
return FALSE;
}
value->u64 = g_ascii_strtoull(str, NULL, 10);
break;
case FieldTypeInt64:
if(!is_number(str)){
DBG1("not a number %s", str);
return FALSE;
}
value->i64 = g_ascii_strtoll(str, NULL, 10);
break;
case FieldTypeDecimal:
if(!string_to_decimal_value (str, value)){
return FALSE;
}
break;
case FieldTypeAsciiString:
case FieldTypeUnicodeString:
value->bytevec.nbytes = strlen(str);
value->bytevec.bytes = (guint8*)wmem_strdup(wmem_epan_scope(), str);
break;
case FieldTypeByteVector:
len = strlen(str);
value->bytevec.nbytes = len/2;
value->bytevec.bytes = (guint8*)wmem_alloc(wmem_epan_scope(), 1+value->bytevec.nbytes);
for(i=0; i + 1 < len; i += 2) {
int nibble1;
int nibble2;
nibble1 = g_ascii_xdigit_value(str[i]);
nibble2 = g_ascii_xdigit_value(str[i+1]);
if(-1 != nibble1 && -1 != nibble2) {
value->bytevec.bytes[i/2] = (nibble1 << 4) | nibble2;
}
else {
value->bytevec.bytes[i/2] = 0;
DBG2("invalid character: index %d in string [%s]", i, str);
return FALSE;
}
}
break;
default:
DBG0("Called with bad type.");
return FALSE;
break;
}
return TRUE;
}
gboolean string_to_decimal_value (const char* str, FieldValue* value)
{
unsigned left, right, len;
unsigned dot;
unsigned i;
unsigned start, end;
char* buf;
len = strlen(str);
start = 0;
end = len -1;
left = end;
right = start;
dot = len;
buf = wmem_strdup (wmem_epan_scope(), str);
for (i = 0; i < len; ++i) {
switch (buf[i]) {
case '-':
case '+':
start = i+1;
break;
case '0':
break;
case '.':
if(dot!=len){
return FALSE;
}
dot = i;
break;
default:
if(!g_ascii_isdigit(buf[i])){
return FALSE;
}
if (i < left) left = i;
if (i > right) right = i;
break;
}
}
if (dot <= right) {
/* Scoot that memory over. */
memmove (dot+buf, 1+dot+buf, right-dot);
right -= 1;
}
dot -= 1; /* Now sitting on the 10^0 digit. */
value->decimal.exponent = (gint32) dot - (gint32) right;
end = 1+right;
buf[end] = 0;
value->decimal.mantissa = g_ascii_strtoll(buf, NULL, 10);
if (0 == value->decimal.mantissa) {
/* Our /right/ was never set correctly, could be invalid.
* Thus, the exponent may be strange.
*/
value->decimal.exponent = 0;
/* return FALSE; */
}
return TRUE;
}