-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhex2binary-cmd.cc
More file actions
89 lines (78 loc) · 1.9 KB
/
Copy pathhex2binary-cmd.cc
File metadata and controls
89 lines (78 loc) · 1.9 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
#include <fstream>
#include <iostream>
#include <stdexcept>
/**
* Converts a hex file into a binary file.
* Oppositeof hex-dump which dumps a binary file
* as a hexa-decimal representation of the contents.
*/
extern "C" {
#include "hex2binary.c"
}
class Buffer {
private:
char *buf;
size_t size;
static const size_t init_size;
public:
Buffer();
Buffer(size_t size);
~Buffer();
Buffer &operator=(const Buffer &b) = delete;
void resizeBuf(size_t newSize);
void ensureBufSize(size_t size);
inline char *getBuf() { return buf; }
inline size_t getSize() { return size; }
};
const size_t Buffer::init_size = 32;
Buffer::Buffer() : Buffer(init_size) {}
Buffer::Buffer(size_t size) : buf(nullptr), size(size) {
buf = (char *)malloc(size);
if (buf == nullptr) {
throw std::bad_alloc();
}
}
Buffer::~Buffer() {
free(buf);
size = 0;
}
void Buffer::resizeBuf(size_t newSize) {
char *buf2 = (char *)malloc(newSize);
if (buf2 == nullptr) throw std::bad_alloc();
free(buf);
buf = buf2;
size = newSize;
}
void Buffer::ensureBufSize(size_t size) {
if (size <= this->size) return;
resizeBuf(size);
}
int main(int argc, char *argv[]) {
if (argc < 2) {
std::cerr << "File name is needed\n";
return 0;
}
std::ifstream ifs(argv[1], std::ifstream::in);
if (!ifs) {
std::cerr << "Error opening file " << argv[1] << '\n';
return EINVAL;
}
int error;
Buffer buf;
std::string line;
std::getline(ifs, line);
while (ifs) {
buf.ensureBufSize(line.size());
size_t s = decode_hex_to_binary(line.c_str(), line.size(), buf.getBuf(),
buf.getSize(), &error);
std::cerr << "decoded bytes s = " << s << "\n";
if (error == -1) {
std::cerr << "Error decoding hex string: " << line << '\n';
}
for (size_t i = 0; i < s; ++i) {
std::cout << buf.getBuf()[i];
}
std::getline(ifs, line);
}
return 0;
}