-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalue.zig
More file actions
363 lines (319 loc) · 12.8 KB
/
Copy pathvalue.zig
File metadata and controls
363 lines (319 loc) · 12.8 KB
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
//! Core numeric value and user-input parsing helpers.
// Copyright (C) 2026 brkzlr <brksys@icloud.com>
//
// This file is part of Libmemscan.
//
// Libmemscan is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program 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
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const Allocator = std.mem.Allocator;
const whitespace = " \t\n\r\x0b\x0c";
pub const ParseError = error{
InvalidByteArray,
InvalidInteger,
InvalidFloat,
InvalidNumber,
OutOfMemory,
};
/// Flags that represent the potential type of a match
pub const MatchFlags = packed struct(u16) {
u8b: bool = false,
s8b: bool = false,
u16b: bool = false,
s16b: bool = false,
u32b: bool = false,
s32b: bool = false,
u64b: bool = false,
s64b: bool = false,
f32b: bool = false,
f64b: bool = false,
_reserved: u6 = 0,
pub const i8b: MatchFlags = .{ .u8b = true, .s8b = true };
pub const i16b: MatchFlags = .{ .u16b = true, .s16b = true };
pub const i32b: MatchFlags = .{ .u32b = true, .s32b = true };
pub const i64b: MatchFlags = .{ .u64b = true, .s64b = true };
pub const integer: MatchFlags = .{
.u8b = true,
.s8b = true,
.u16b = true,
.s16b = true,
.u32b = true,
.s32b = true,
.u64b = true,
.s64b = true,
};
pub const float: MatchFlags = .{ .f32b = true, .f64b = true };
pub const all: MatchFlags = .{
.u8b = true,
.s8b = true,
.u16b = true,
.s16b = true,
.u32b = true,
.s32b = true,
.u64b = true,
.s64b = true,
.f32b = true,
.f64b = true,
};
pub fn bits(self: MatchFlags) u16 {
return @bitCast(self);
}
};
pub const ValueData = extern union {
int8_value: i8,
uint8_value: u8,
int16_value: i16,
uint16_value: u16,
int32_value: i32,
uint32_value: u32,
int64_value: i64,
uint64_value: u64,
float32_value: f32,
float64_value: f64,
bytes: [@sizeOf(u64)]u8,
chars: [@sizeOf(u64)]u8,
};
/// Describes matched values
pub const Value = extern struct {
data: ValueData = .{ .uint64_value = 0 },
flags: MatchFlags = .{},
};
pub const Wildcard = enum(u8) {
FIXED = 0xFF,
WILDCARD = 0x00,
};
/// Describes values provided by users
pub const UserValue = struct {
int8_value: i8 = 0,
uint8_value: u8 = 0,
int16_value: i16 = 0,
uint16_value: u16 = 0,
int32_value: i32 = 0,
uint32_value: u32 = 0,
int64_value: i64 = 0,
uint64_value: u64 = 0,
float32_value: f32 = 0,
float64_value: f64 = 0,
bytearray_value: ?[]const u8 = null,
wildcard_value: ?[]const Wildcard = null,
string_value: ?[]const u8 = null,
flags: MatchFlags = .{},
pub fn deinit(self: *UserValue, allocator: Allocator) void {
if (self.bytearray_value) |bytes| allocator.free(bytes);
if (self.wildcard_value) |wildcards| allocator.free(wildcards);
self.bytearray_value = null;
self.wildcard_value = null;
}
pub fn parseByteArray(allocator: Allocator, tokens: []const []const u8) ParseError!UserValue {
var result = UserValue{};
errdefer result.deinit(allocator);
const bytes = allocator.alloc(u8, tokens.len) catch return ParseError.OutOfMemory;
errdefer allocator.free(bytes);
const wildcards = allocator.alloc(Wildcard, tokens.len) catch return ParseError.OutOfMemory;
errdefer allocator.free(wildcards);
for (tokens, 0..) |token, index| {
if (token.len != 2) {
return ParseError.InvalidByteArray;
}
if (std.mem.eql(u8, token, "??")) {
bytes[index] = 0;
wildcards[index] = .WILDCARD;
} else {
bytes[index] = std.fmt.parseUnsigned(u8, token, 16) catch return ParseError.InvalidByteArray;
wildcards[index] = .FIXED;
}
}
result.bytearray_value = bytes;
result.wildcard_value = wildcards;
return result;
}
pub fn parseByteArrayText(allocator: Allocator, text: []const u8) ParseError!UserValue {
var token_count: usize = 0;
var counter = std.mem.tokenizeAny(u8, text, whitespace);
while (counter.next() != null) {
token_count += 1;
}
if (token_count == 0) return ParseError.InvalidByteArray;
const tokens = allocator.alloc([]const u8, token_count) catch return ParseError.OutOfMemory;
defer allocator.free(tokens);
var it = std.mem.tokenizeAny(u8, text, whitespace);
var index: usize = 0;
while (it.next()) |token| : (index += 1) {
tokens[index] = token;
}
return parseByteArray(allocator, tokens);
}
pub fn parseNumber(text: []const u8) ParseError!UserValue {
if (UserValue.parseInt(text)) |result| {
var number = result;
number.flags.f32b = true;
number.flags.f64b = true;
if (number.flags.s64b) {
number.float32_value = @floatFromInt(number.int64_value);
number.float64_value = @floatFromInt(number.int64_value);
} else {
number.float32_value = @floatFromInt(number.uint64_value);
number.float64_value = @floatFromInt(number.uint64_value);
}
return number;
} else |int_err| switch (int_err) {
ParseError.InvalidInteger => return UserValue.parseFloatAndBackfillInts(text) catch ParseError.InvalidNumber,
ParseError.OutOfMemory => unreachable,
else => return ParseError.InvalidNumber,
}
}
pub fn parseInt(text: []const u8) ParseError!UserValue {
const trimmed = std.mem.trimStart(u8, text, whitespace);
var result = UserValue{};
const signed_value = std.fmt.parseInt(i64, trimmed, 0) catch null;
const unsigned_value = if (trimmed.len > 0 and trimmed[0] != '-')
std.fmt.parseInt(u64, trimmed, 0) catch null
else
null;
if (signed_value == null and unsigned_value == null) {
return ParseError.InvalidInteger;
}
if (unsigned_value) |value| {
if (value <= std.math.maxInt(u8)) {
result.flags.u8b = true;
result.uint8_value = @intCast(value);
}
if (value <= std.math.maxInt(u16)) {
result.flags.u16b = true;
result.uint16_value = @intCast(value);
}
if (value <= std.math.maxInt(u32)) {
result.flags.u32b = true;
result.uint32_value = @intCast(value);
}
result.flags.u64b = true;
result.uint64_value = value;
}
if (signed_value) |value| {
if (value >= std.math.minInt(i8) and value <= std.math.maxInt(i8)) {
result.flags.s8b = true;
result.int8_value = @intCast(value);
}
if (value >= std.math.minInt(i16) and value <= std.math.maxInt(i16)) {
result.flags.s16b = true;
result.int16_value = @intCast(value);
}
if (value >= std.math.minInt(i32) and value <= std.math.maxInt(i32)) {
result.flags.s32b = true;
result.int32_value = @intCast(value);
}
result.flags.s64b = true;
result.int64_value = value;
}
return result;
}
pub fn parseFloat(text: []const u8) ParseError!UserValue {
const trimmed = std.mem.trimStart(u8, text, whitespace);
const number = std.fmt.parseFloat(f64, trimmed) catch return ParseError.InvalidFloat;
return .{
.float32_value = @floatCast(number),
.float64_value = number,
.flags = MatchFlags.float,
};
}
fn parseFloatAndBackfillInts(text: []const u8) ParseError!UserValue {
var result = try UserValue.parseFloat(text);
const number = result.float64_value;
if (number >= 0 and number <= std.math.maxInt(u8)) {
result.flags.u8b = true;
result.uint8_value = @intFromFloat(number);
}
if (number >= std.math.minInt(i8) and number <= std.math.maxInt(i8)) {
result.flags.s8b = true;
result.int8_value = @intFromFloat(number);
}
if (number >= 0 and number <= std.math.maxInt(u16)) {
result.flags.u16b = true;
result.uint16_value = @intFromFloat(number);
}
if (number >= std.math.minInt(i16) and number <= std.math.maxInt(i16)) {
result.flags.s16b = true;
result.int16_value = @intFromFloat(number);
}
if (number >= 0 and number <= std.math.maxInt(u32)) {
result.flags.u32b = true;
result.uint32_value = @intFromFloat(number);
}
if (number >= std.math.minInt(i32) and number <= std.math.maxInt(i32)) {
result.flags.s32b = true;
result.int32_value = @intFromFloat(number);
}
if (number >= 0 and number < @as(f64, @floatFromInt(std.math.maxInt(u64)))) {
result.flags.u64b = true;
result.uint64_value = @intFromFloat(number);
}
if (number >= std.math.minInt(i64) and number < @as(f64, @floatFromInt(std.math.maxInt(i64)))) {
result.flags.s64b = true;
result.int64_value = @intFromFloat(number);
}
return result;
}
};
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
test "parseInt: preserves matching signed and unsigned ranges" {
const value = try UserValue.parseInt("255");
try std.testing.expectEqual((MatchFlags{ .u8b = true, .u16b = true, .u32b = true, .u64b = true, .s16b = true, .s32b = true, .s64b = true }).bits(), value.flags.bits());
try std.testing.expect(!value.flags.s8b);
try std.testing.expectEqual(255, value.uint8_value);
try std.testing.expectEqual(255, value.uint16_value);
try std.testing.expectEqual(255, value.uint32_value);
try std.testing.expectEqual(255, value.uint64_value);
try std.testing.expectEqual(255, value.int16_value);
try std.testing.expectEqual(255, value.int32_value);
try std.testing.expectEqual(255, value.int64_value);
}
test "parseInt: does not enable float flags" {
const value = try UserValue.parseInt("42");
try std.testing.expect(!value.flags.f32b);
try std.testing.expect(!value.flags.f64b);
}
test "parseNumber: from integer also enables float flags" {
const value = try UserValue.parseNumber("42");
try std.testing.expectEqual(MatchFlags.all.bits(), value.flags.bits());
try std.testing.expectEqual(42, value.uint8_value);
try std.testing.expectEqual(42, value.int8_value);
try std.testing.expectEqual(42, value.float32_value);
try std.testing.expectEqual(42, value.float64_value);
}
test "parseNumber: from float backfills integer candidates" {
const value = try UserValue.parseNumber("12.75");
try std.testing.expectEqual(MatchFlags.all.bits(), value.flags.bits());
try std.testing.expectEqual(12, value.uint8_value);
try std.testing.expectEqual(12, value.int8_value);
try std.testing.expectEqual(12.75, value.float32_value);
try std.testing.expectEqual(12.75, value.float64_value);
}
test "parseByteArray: allocates bytes and wildcards" {
const allocator = std.testing.allocator;
const tokens = [_][]const u8{ "4F", "??", "A0" };
var value = try UserValue.parseByteArray(allocator, &tokens);
defer value.deinit(allocator);
try std.testing.expectEqualSlices(u8, &[_]u8{ 0x4F, 0x00, 0xA0 }, value.bytearray_value.?);
try std.testing.expectEqual(Wildcard.FIXED, value.wildcard_value.?[0]);
try std.testing.expectEqual(Wildcard.WILDCARD, value.wildcard_value.?[1]);
try std.testing.expectEqual(Wildcard.FIXED, value.wildcard_value.?[2]);
}
test "parseByteArrayText: tokenizes whitespace-delimited input" {
const allocator = std.testing.allocator;
var value = try UserValue.parseByteArrayText(allocator, "4F ?? A0");
defer value.deinit(allocator);
try std.testing.expectEqualSlices(u8, &[_]u8{ 0x4F, 0x00, 0xA0 }, value.bytearray_value.?);
try std.testing.expectEqualSlices(Wildcard, &[_]Wildcard{ .FIXED, .WILDCARD, .FIXED }, value.wildcard_value.?);
}