-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils.cpp
76 lines (67 loc) · 1.99 KB
/
utils.cpp
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
#include "utils.h"
// Print bytes in hexadecimal format
void print_hex(unsigned char *bytes, size_t length) {
for (size_t i = 0; i < length; ++i) {
printf("%02x", bytes[i]);
}
printf("\n");
}
// Function to read key or IV from a file.
void read_key_or_iv(unsigned char *data, size_t size, const char *filename) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
fprintf(stderr, "Cannot open file: %s\n", filename);
exit(1);
}
for (size_t i = 0; i < size; i++) {
char buffer[3];
if (fread(buffer, 1, 2, file) != 2) {
fprintf(stderr, "Cannot read value from file: %s\n", filename);
exit(1);
}
buffer[2] = '\0'; // Null-terminate the buffer
data[i] = (unsigned char)strtol(
buffer, NULL, 16); // Convert the buffer to a hexadecimal value
}
fclose(file);
}
// Function to read binary from a file.
void read_file_as_binary(unsigned char **data, size_t *size,
const char *filename) {
FILE *file = fopen(filename, "rb");
if (file == NULL) {
fprintf(stderr, "Cannot open file: %s\n", filename);
exit(1);
}
// Determine the file size
fseek(file, 0, SEEK_END);
*size = ftell(file);
fseek(file, 0, SEEK_SET);
// Allocate the buffer
*data = new unsigned char[*size];
size_t bytesRead = fread(*data, 1, *size, file);
if (bytesRead != *size) {
fprintf(stderr, "Failed to read the entire file: %s\n", filename);
exit(1);
}
fclose(file);
}
// Function to write ciphertext to a file
void write_encrypted(const unsigned char *ciphertext, size_t size,
const char *filename) {
FILE *file = fopen(filename, "wb");
if (file == NULL) {
fprintf(stderr, "Cannot open file: %s\n", filename);
exit(1);
}
if (fwrite(ciphertext, 1, size, file) != size) {
fprintf(stderr, "Error writing to file: %s\n", filename);
exit(1);
}
fclose(file);
}
double getTimeStampMs() {
struct timeval tv;
gettimeofday(&tv, NULL);
return (double)tv.tv_usec / 1000 + tv.tv_sec * 1000;
}