-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmtf.c
121 lines (97 loc) · 2.61 KB
/
mtf.c
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
#include "mtf.h"
#define ASCIISZ 255
#define BUFFSIZE 8192
#define INDEXBUFF 2
#define MAXNAME 1024
// #define DEBUG_BWT
static byte_t mtf[ASCIISZ];
static byte_t where[ASCIISZ];
void mtf_init()
{
int i;
for(i = 0; i < ASCIISZ; i++) {
mtf[i] = where[i] = i;
}
}
void mtf_encode(char *fname, char *target)
{
int fdread, fdwrite;
int i, j;
int nbytes;
int index;
byte_t buffer[BUFFSIZE];
if((fdread = open(fname, O_RDONLY)) == -1) {
fprintf(stderr, "%s: could not open file\n", fname);
exit(1);
}
if((fdwrite = open(target, O_WRONLY | O_CREAT, 0644)) == -1) {
fprintf(stderr, "%s: could not open file\n", target);
exit(1);
}
mtf_init();
for(;;) {
nbytes = read(fdread, buffer, BUFFSIZE);
if(nbytes <= 0)
break;
index = bwt_encode(buffer, nbytes);
for(i = 0; i < nbytes; i++) {
for(j = 0; j < ASCIISZ; j++) {
where[mtf[j]] = j;
}
write(fdwrite, &where[buffer[i]], 1);
mtf_move(buffer[i], (int)where[buffer[i]]);
}
write(fdwrite, &index, sizeof(int));
}
close(fdwrite);
close(fdread);
}
void mtf_move(byte_t c, int index)
{
for(; index >= 1; index--)
mtf[index] = mtf[index - 1];
mtf[0] = c;
}
void mtf_decode(char *fname, char *target)
{
int fdread, fdwrite;
int i;
int nbytes;
int index;
byte_t *ibuff;
byte_t curr;
byte_t buffer[BUFFSIZE];
if((fdread = open(fname, O_RDONLY)) == -1) {
fprintf(stderr, "%s: could not open file\n", fname);
exit(1);
}
if((fdwrite = open(target, O_WRONLY | O_CREAT, 0644)) == -1) {
fprintf(stderr, "%s: could not open file\n", target);
exit(1);
}
mtf_init();
for(;;) {
nbytes = read(fdread, buffer, BUFFSIZE);
if(nbytes <= 0) {
break;
} if(nbytes == BUFFSIZE) {
read(fdread, &index, sizeof(int));
} else {
ibuff = (byte_t *)malloc(sizeof(int) * sizeof(byte_t));
for(i = sizeof(int) - 1; i >= 0; --i)
ibuff[i] = buffer[--nbytes];
memcpy(&index, ibuff, sizeof(int));
free(ibuff);
}
for(i = 0; i < nbytes; i++) {
// write(fdwrite, &mtf[buffer[i]], 1);
curr = mtf[buffer[i]];
mtf_move(mtf[buffer[i]], (int)buffer[i]);
buffer[i] = curr;
}
bwt_decode(buffer, nbytes, index);
write(fdwrite, buffer, nbytes);
}
close(fdwrite);
close(fdread);
}