forked from THUBear-wjy/LogReducer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
elastic.cpp
267 lines (247 loc) · 7.88 KB
/
elastic.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
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
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
using namespace std;
#define BUFFERSIZE 1000000
typedef unsigned char byte;
const int MAXSIZE = 4; //int equals 4 bytes
const int BYTE_BITS = 8;
const int INT_BIT_SIZE = sizeof (int) * BYTE_BITS;
int iBuffer[BUFFERSIZE];
byte bBuffer[4 * BUFFERSIZE];
const char* bytes_to_binary_str(byte* v, int byte_count, char* out, int out_size){
int idx = 0;
for(int i = 0; i < byte_count; i++){
if (i > 0) out[idx++] = '_';
for(int j = 0; j < BYTE_BITS && i * BYTE_BITS + j < out_size; j++){
out[idx++] = '0' + ((v[i] >> (BYTE_BITS - j - 1)) & 1);
}
}
out[idx] = 0;
return out;
}
const char* int_to_binary_str(int v, char* out, int out_size){
int idx = 0;
for(int i = 0; i < INT_BIT_SIZE && i < out_size; i++){
if (i > 0 && i % BYTE_BITS == 0){
out[idx++] = '_';
}
out[idx++] = '0' + ((v >> (INT_BIT_SIZE - i - 1)) & 1);
}
out[idx] = 0;
return out;
}
int int_to_zigzag(int n){
return (n << 1) ^ (n >> 31);
}
int zigzag_to_int(int n){
return (((unsigned int)n) >> 1) ^ -(n & 1);
}
int write_to_buffer(int zz, byte* buf, int size){
int ret = 0;
for(int i = 0 ; i < size; i++){
if ((zz & (~0x7f)) == 0){
buf[i] = (byte)zz;
ret = i + 1;
break;
}else{
buf[i] = (byte)((zz & 0x7f) | 0x80);
zz = ((unsigned int )zz) >> 7;
}
}
return ret;
}
int write_to_file(int num, FILE* file, bool debug=false){
byte tBuffer[5];
memset(tBuffer, 0, sizeof(byte)*5);
char str[INT_BIT_SIZE + INT_BIT_SIZE / BYTE_BITS + 1];
int_to_binary_str(num, str, sizeof(str));
int ret = write_to_buffer(num, tBuffer, 5);
char str_write[ret * BYTE_BITS + ret + 1];
bytes_to_binary_str(tBuffer, ret, str_write, sizeof(str_write));
if (debug){
printf("ret: %d, %11d [%s] ==to-buf==> %s\n", ret, num,
str, str_write
);
}
fwrite(tBuffer, sizeof(byte), ret, file);
return ret;
}
unsigned int read_from_buffer(byte* buf, long& start, int max_size){
unsigned int ret = 0;
int offset = 0;
int i = start;
for(; i - start < max_size; i++, offset += 7){
byte n = buf[i];
if ((n & 0x80) != 0x80){
ret |= (n << offset);
i++;
break;
}else{
ret |= ((n & 0x7f) << offset);
}
}
start = i;
return ret;
}
int main(int argc, char** argv){
int idx = 0;
bool zigzag = false;
if (argc > 4 && strncmp(argv[4], "O", 1) == 0){
zigzag = true;
}
if (zigzag){
//printf("Zigzag mode!\n");
if (strncmp(argv[1], "e",1) == 0){ //Encoder
FILE* in;
FILE* out;
if ((in = fopen(argv[2], "r")) == NULL){
printf("Fail to open input file");
exit(0);
}
if ((out = fopen(argv[3], "wb")) == NULL){
printf("Fail to open output file");
exit(0);
}
int temp = 0;
while(fscanf(in, "%d", &temp) == 1){
iBuffer[idx++] = temp;
}
fclose(in);
write_to_file(idx, out);
for (int i = 0; i < idx;i++){
int zigzag = int_to_zigzag(iBuffer[i]);
write_to_file(zigzag, out);
}
fclose(out);
}else{
FILE* in;
FILE* out;
if ((in = fopen(argv[2], "rb")) == NULL){
printf("Fail to open input file");
exit(0);
}
if ((out = fopen(argv[3], "w")) == NULL){
printf("Fail to open output file");
exit(0);
}
fseek(in, 0, SEEK_END);
long lSize = ftell(in);
rewind(in);
long result = fread(bBuffer, 1, lSize, in);
if (result != lSize){
printf("Reading entair file fail");
exit(0);
}
fclose(in);
long start = 0;
int tot = read_from_buffer(bBuffer, start, 5);
int count = 0;
while(count < tot){
//printf("start: %d, result: %d\n", start, (int)result);
int ret = read_from_buffer(bBuffer, start, 5);
fprintf(out, "%d\n", zigzag_to_int(ret));
count++;
}
fclose(out);
}
return 0;
}
if (strncmp(argv[1], "e",1) == 0){ //Encoder
FILE* in;
FILE* out;
if ((in = fopen(argv[2], "r")) == NULL){
printf("Fail to open input file");
exit(0);
}
if ((out = fopen(argv[3], "wb")) == NULL){
printf("Fail to open output file");
exit(0);
}
int temp = 0;
while(fscanf(in, "%d", &temp) == 1){
iBuffer[idx++] = temp;
}
fclose(in);
int minus = 0;
unsigned int* minus_value = (unsigned int *) malloc(idx * sizeof(unsigned int));
int* minus_loc = (int *)malloc(idx * sizeof(int));
memset(minus_value, 0, sizeof(unsigned int)*idx);
memset(minus_loc, 0, sizeof(int) * idx);
for (int i = 0; i < idx; i++){
if (iBuffer[i] < 0){
minus_value[minus] = -iBuffer[i];
minus_loc[minus] = i;
minus++;
}
}
write_to_file(idx, out);
write_to_file(minus, out);
for (int i = 0; i < minus; i++){
write_to_file(minus_loc[i], out);
write_to_file(minus_value[i], out);
//printf("Write Minus Loc: %d, Minux Value: %d\n", minus_loc[i], minus_value[i]);
}
free(minus_value);
free(minus_loc);
for (int i = 0; i < idx;i++){
if (iBuffer[i] < 0) continue;
write_to_file(iBuffer[i], out);
}
fclose(out);
}else{
FILE* in;
FILE* out;
if ((in = fopen(argv[2], "rb")) == NULL){
printf("Fail to open input file");
exit(0);
}
if ((out = fopen(argv[3], "w")) == NULL){
printf("Fail to open output file");
exit(0);
}
fseek(in, 0, SEEK_END);
long lSize = ftell(in);
rewind(in);
long result = fread(bBuffer, 1, lSize, in);
if (result != lSize){
printf("Reading entair file fail");
exit(0);
}
fclose(in);
long start = 0;
int tot = read_from_buffer(bBuffer, start, 5);
int minus = read_from_buffer(bBuffer, start, 5);
cout << minus << endl;
//printf("Read minus: %d", minus);
unsigned int * minus_value = (unsigned int*)malloc(minus * sizeof(unsigned int));
int * minus_loc = (int*)malloc(minus * sizeof(int));
memset(minus_value, 0, sizeof(unsigned int)*minus);
memset(minus_loc, 0, sizeof(int) * minus);
for (int i = 0; i < minus; i++){
minus_loc[i] = read_from_buffer(bBuffer, start, 5);
minus_value[i] = read_from_buffer(bBuffer, start, 5);
//printf("Minus Loc: %d, Minux Value: %d\n", minus_loc[i], minus_value[i]);
}
int count = 0;
int now_minus = 0;
printf("start: %ld, result: %ld", start, result);
while(count < tot){
//printf("start: %d, result: %d\n", start, (int)result);
if (now_minus < minus && count == minus_loc[now_minus]){
fprintf(out, "-%u\n", minus_value[now_minus]);
now_minus++;
count++;
continue;
}
int ret = read_from_buffer(bBuffer, start, 5);
fprintf(out, "%d\n", ret);
count++;
}
fclose(out);
free(minus_value);
free(minus_loc);
}
}