-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer.cpp
144 lines (109 loc) · 3.27 KB
/
buffer.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
#include "buffer.h"
bool read_buffer_safe(FILE *source_file, char* buffer, int *buf_i) {
bool result = true;
if (*buf_i != -1) {
if (*(buffer + *buf_i) == '\0' || *(buffer + *buf_i - 1) == '\n') {
result = false;
}
}
else
if (fgets(buffer, MAX_BUFFER_LENGTH, source_file)) {
*buf_i = 0;
}
else
result = false;
return result;
}
bool set_pointers(const char *buffer, int *begin, int *buf_i, int *end, int offset) {
int length = offset;
bool result = true;
while (*(buffer + *buf_i) != '\0' && length > 0) {
(*buf_i)++;
length--;
}
while (*(buffer + *end) != '\0' && (*end - *buf_i) < MAX_SEARCH_BUFFER_LENGTH) {
(*end)++;
}
if (*buf_i < MAX_WINDOW_LENGTH) {
*begin = 0;
}
else {
while ((*buf_i - *begin) > MAX_WINDOW_LENGTH) {
(*begin)++;
}
}
if (*buf_i == *end) {
*begin = 0;
*end = 0;
result = false;
}
return result;
}
void write_string(const char *src, char *dst, int length) {
int i = length;
while (i > 0) {
*(dst + i - 1) = *(src + i - 1);
i--;
}
}
void write_buffer_safe(FILE *compressed_file, char *buffer, int *buf_i, const char *word, int word_length) {
if (word && word_length) {
if ((*buf_i + word_length) >= MAX_BUFFER_LENGTH) {
*(buffer + *buf_i) = '\0';
fprintf(compressed_file, "%s", buffer);
*buf_i = 0;
}
write_string(word, buffer + *buf_i, word_length);
*buf_i += word_length;
}
else
{
*(buffer + *buf_i) = '\0';
fprintf(compressed_file, "%s", buffer);
}
}
bool read_buffer_secure(FILE *compressed_file, char *read_buffer, int *rbuf_i) {
bool result = true;
if (*rbuf_i == -1) {
fgets(read_buffer, MAX_WINDOW_LENGTH, compressed_file);
*rbuf_i = 0;
}
else {
if (*(read_buffer + *rbuf_i) == '\0') {
if (!feof(compressed_file)) {
fgets(read_buffer, MAX_WINDOW_LENGTH, compressed_file);
*rbuf_i = 0;
}
else
result = false;
}
}
return result;
}
void write_buffer_secure(FILE *decompressed_file, char *write_buffer, char* copy_buffer, int *wbuf_i, int *cbuf_last, const char *word, int word_length) {
if (word && word_length) {
if ((*wbuf_i + word_length) >= MAX_WINDOW_LENGTH) {
*(write_buffer + *wbuf_i) = '\0';
strcpy(copy_buffer, (const char*)write_buffer);
*cbuf_last = strlen(write_buffer);
fprintf(decompressed_file, "%s", write_buffer);
*wbuf_i = 0;
}
write_string(word, write_buffer + *wbuf_i, word_length);
write_string(word, (copy_buffer + *cbuf_last + *wbuf_i), word_length);
*wbuf_i += word_length;
}
else
{
*(write_buffer + *wbuf_i) = '\0';
fprintf(decompressed_file, "%s", write_buffer);
}
}
void write_character_to_buffer(FILE *decoded_file, char *buffer, int *buf_i, char c) {
if (*buf_i == MAX_BUFFER_LENGTH) {
fwrite(buffer, sizeof(char), *buf_i, decoded_file);
*buf_i = 0;
}
*(buffer + *buf_i) = c;
(*buf_i)++;
}