-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhexread.c
More file actions
203 lines (189 loc) · 4.81 KB
/
Copy pathhexread.c
File metadata and controls
203 lines (189 loc) · 4.81 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
#include<stdio.h>
#include<stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "util.h"
#include <fcntl.h>
#include <signal.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
int main(int argc, char *argv[]){
struct stat file_stat;
int fd;
//ssize_t file_size;
//ssize_t file_bytes;
ssize_t bin_bytes;
char *file_buffer;
char *binary_data;
ssize_t bytes_written;
char *stdin_buffer;
ssize_t bytes;
int eof = 0;
int is_terminal = isatty(0);
//handle no file provided
if (argc == 1){
int max = 1024;
stdin_buffer = malloc(max);
ssize_t byte_counter = 0;
ssize_t valid_hex_counter = 0;
char temp_buffer[48];
// process running here
while(1){
ssize_t bytes = read(0, stdin_buffer, 48);
if(bytes > 0){
//copy read bytes into byte-counter
for (ssize_t i=0; i < bytes; i++){
if(!isspace(stdin_buffer[i])){
valid_hex_counter++;
}
temp_buffer[byte_counter++] = stdin_buffer[i];
if(valid_hex_counter == 32){
char null_terminated_buffer[byte_counter+1];
memcpy(null_terminated_buffer, temp_buffer, byte_counter);
null_terminated_buffer[byte_counter] = '\0';
binary_data = hex_to_binary(null_terminated_buffer, &bin_bytes); //convert binary to hex
if(binary_data == NULL){
perror("hex_to_binary");
free(stdin_buffer);
free(binary_data);
return 1;
}
//write data to stdout
if((bytes_written = write(1, binary_data, bin_bytes)) == -1){
perror("write");
free(stdin_buffer);
free(binary_data);
return 1;
}
//write(1, "\n", 1);
//free buffers to continuer reading
free(binary_data);
//reset
byte_counter = 0;
valid_hex_counter = 0;
}
}
} else if (bytes == 0){
eof = 1;
break;
} else if (errno == EINTR){
continue;
}else{
perror("read");
free(stdin_buffer);
return 1;
}
}
//flush buffer to stdout
if(byte_counter > 0){
char null_terminated_buffer[byte_counter + 1];
memcpy(null_terminated_buffer, temp_buffer, byte_counter);
null_terminated_buffer[byte_counter] = '\0';
binary_data = hex_to_binary(null_terminated_buffer, &bin_bytes);
if(binary_data == NULL){
perror("binary_to_hex");
free(stdin_buffer);
return 1;
}
if(write(1, binary_data, bin_bytes) == -1){
perror("write");
free(stdin_buffer);
free(binary_data);
return 1;
}
//write(1, "\n", 1);
free(binary_data);
}
// Only write newline if we reached EOF
if (eof && is_terminal) {
write(1, "\n", 1);
}
free(stdin_buffer);
}else{
//read file
if((fd = open(argv[1], O_RDONLY)) == -1){
perror("open");
close(fd);
return 1;
}
if(stat(argv[1], &file_stat) == -1){
perror("stat");
close(fd);
}
int max = 1024;
ssize_t byte_counter = 0;
file_buffer = malloc(max);
char temp_buffer[48]; //if non-hex is included 16 valid hex aligned will never be more than 48 characters
ssize_t valid_hex_counter = 0; //to include any character
//new code start here
while(1){
bytes = read(fd, file_buffer, max);
if(bytes > 0){
//copy read bytes into byte-counter
ssize_t i = 0;
for (; i < bytes; i++){
if(!isspace(file_buffer[i])){
valid_hex_counter++;
}
temp_buffer[byte_counter++] = file_buffer[i];
if(valid_hex_counter == 32){
char null_terminated_buffer[byte_counter+1];
memcpy(null_terminated_buffer, temp_buffer, byte_counter);
null_terminated_buffer[byte_counter] = '\0';
binary_data = hex_to_binary(null_terminated_buffer, &bin_bytes); //convert binary to hex
if(binary_data == NULL){
perror("hex_to_binary");
free(file_buffer);
free(binary_data);
return 1;
}
//write data to stdout
if((bytes_written = write(1, binary_data, bin_bytes)) == -1){
perror("write");
free(file_buffer);
free(binary_data);
return 1;
}
//write(1, "\n", 1);
//free buffers to continuer reading
free(binary_data);
//reset
byte_counter = 0;
valid_hex_counter = 0;
}
}
} else if (bytes == 0){
break;
}else{
perror("read");
free(file_buffer);
return 1;
}
}
//flush buffer to stdout
if(byte_counter > 0){
char null_terminated_buffer[byte_counter + 1];
memcpy(null_terminated_buffer, temp_buffer, byte_counter);
null_terminated_buffer[byte_counter] = '\0';
binary_data = hex_to_binary(null_terminated_buffer, &bin_bytes);
if(binary_data == NULL){
perror("hex_to_binary");
free(file_buffer);
return 1;
}
if(write(1, binary_data, bin_bytes) == -1){
perror("write");
free(file_buffer);
free(binary_data);
return 1;
}
free(binary_data);
}
//write(1, "\n", 1);
free(file_buffer);
close(fd);
}
return 0;
}