-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cc
133 lines (104 loc) · 4 KB
/
main.cc
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
/*
** Copyright 2019 Javier Alvarado
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
#include "libhexer/hexer.h"
#include <assert.h>
#include <string.h>
#include <iostream>
#include <string>
#define CHECKN(a,b,c) do { \
std::string s = b(a); \
printf("INPUT: %s OUTPUT: %s EXPECTED: %s\n", #a, s.c_str(), c); \
assert(s == c); \
} while (0)
// TODO: Make all tests actually test results instead of outputting them.
void TestHexOut(void)
{
/* Test macros that use global HexOut object. */
assert(strcmp(XUINT8(0XBE), "be") == 0);
assert(strcmp(XUINT16(0xaced), "aced") == 0);
assert(strcmp(XUINT24(0xdecade), "decade") == 0);
assert(strcmp(XUINT32(0xBabeFace), "babeface") == 0);
uint8_t buffer[32] = { 0X3F,0X4F,0XB3,0XD9,0X15,0X2A,0XFF,0X94,
0X97,0XBB,0XD7,0X3C,0X3C,0X35,0X14,0XAC,
0X7C,0X7A,0XC3,0X21,0X9D,0X71,0X05,0X56,
0X47,0XE0,0X42,0X19,0XEB,0X48,0X79,0X22 };
printf("SHA-256: %s\n", XTOSTR(buffer, sizeof(buffer)));
/* Modify the global HexOut object used by macros. */
libhexer::XOUT.SetGroupSize(4);
printf("SHA-256: %s\n", XTOSTR(buffer, sizeof(buffer)));
using namespace libhexer;
/* Use custom HexOut object. */
auto xout = HexOut::New().SetGroupSize(1).SetGroupSeparator(':');
CHECKN(0XCAFEBABEFACE, xout.UInt48, "ca:fe:ba:be:fa:ce");
xout.SetGroupSize(2);
CHECKN(0xdecade, xout.UInt24, "de:cade");
xout.SetPartialGroup(HexOut::TRAILING);
CHECKN(0xdecade, xout.UInt24, "deca:de");
}
void TestHexIn(void)
{
using namespace std;
using namespace libhexer;
HexIn xin;
HexOut xout;
uint64_t value = 0;
const char *rest = xin.Parse("DEADBEEFCAFEBABE", (uint8_t *)&value, 8);
cout << hex << value << " + \"" << rest << "\"" << endl;
value = 0;
rest = xin.Parse("DEADBEEF CAFEBABE", (uint8_t *)&value, 8);
cout << hex << value << " + \"" << rest << "\"" << endl;
value = 0;
xin.Ignore(" ");
rest = xin.Parse("DEADBEEF CAFEBABE", (uint8_t *)&value, 8);
cout << hex << value << " + \"" << rest << "\"" << endl;
value = 0;
rest = xin.Parse("Dead Beef\tCafe Babe", (uint8_t *)&value, 8);
cout << hex << value << " + \"" << rest << "\"" << endl;
value = 0;
xin.Ignore(" \t");
rest = xin.Parse("Dead Beef\tCafe Babe", (uint8_t *)&value, 8);
cout << hex << value << " + \"" << rest << "\"" << endl;
value = 0;
xin.Ignore(":");
rest = xin.Parse("00:1e:8c:0e:7d:e6 <-- HWaddr", (uint8_t *)&value, 8);
cout << hex << value << " + \"" << rest << "\"" << endl;
uint8_t *data = NULL;
size_t count = 0;
xin.Ignore(": ");
rest = xin.Parse("020C0600 00000005 : 000029FF F5C618D1 C1B7ED48 395925C2"
"E76D65ED 4AC39588 AA1AD11F F99F1178 A0ADFA44 950154F5"
"DD504AC1 DD36933C DC95E371 14DC3251 EEEF1ECC 81BB6495"
"8F91DA0D 27F888AB 8F2D2614 6FCB9378 05003DB0",
&data, &count);
cout << xout.ToString(data, count) << endl;
xout.SetGroupSize(8).SetLetterCase(HexOut::UPPER);
cout << xout.ToString(data, count) << endl;
delete data;
data = NULL;
xin.Ignore(NULL);
xin.Ignore("");
cout << hex << xin.UInt16("1fe") << endl;
cout << hex << xin.UInt64("4AC39588AA1AD11F") << endl;
}
int main(int, char **)
{
std::cout << "HEX OUT:" << std::endl;
TestHexOut();
std::cout << std::endl;
std::cout << "HEX IN:" << std::endl;
TestHexIn();
return 0;
}