-
Notifications
You must be signed in to change notification settings - Fork 7
/
mtfscan.c
184 lines (168 loc) · 4.05 KB
/
mtfscan.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
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
#include <sys/ioctl.h>
#ifndef __APPLE__
#include <sys/mtio.h>
#endif
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include "mtf.h"
int mtfscan_init(struct mtf_stream *s, int fd)
{
#ifndef __APPLE__
struct mtget mg;
#endif
s->fd = fd;
s->abspos = 0;
#ifdef __APPLE__
s->blksize = 0; /* no blocks */
errno = 0;
#else
if (ioctl(fd, MTIOCGET, &mg) == -1) {
if (errno == ENOTTY) {
s->blksize = 0; /* no blocks */
errno = 0;
} else {
return 0;
}
} else {
s->blksize = (mg.mt_dsreg & MT_ST_BLKSIZE_MASK) >> MT_ST_BLKSIZE_SHIFT;
}
#endif
s->ready = 0;
s->flbsize = s->flbread = 0;
return 1;
}
int mtfscan_ready(struct mtf_stream *s, unsigned int need)
{
int r;
while (s->ready < need) {
errno = 0;
do {
r = read(s->fd, s->buffer + s->ready, need - s->ready);
} while (r == -1 && errno == EINTR);
if (r < 1) return 0;
s->flbread += r;
s->abspos += r;
s->ready += r;
}
return 1;
}
int mtfscan_readyplus(struct mtf_stream *s, unsigned int more)
{
return mtfscan_ready(s, s->ready + more);
}
int mtfscan_read(struct mtf_stream *s, unsigned char *buf, int chunk)
{
int r;
if (s->flbread + chunk > s->flbsize) {
chunk = s->flbsize - s->flbread;
}
if (chunk < 1) return 0;
errno = 0;
do {
r = read(s->fd, buf, chunk);
} while (r == -1 && errno == EINTR);
if (r < 1) return -1; /* !!! */
s->flbread += r;
s->abspos += r;
return r;
}
int mtfscan_skip(struct mtf_stream *s)
{
unsigned char stump[4096];
int r;
do {
r = mtfscan_read(s, stump, sizeof(stump));
} while (r > 0);
return r;
}
int mtfscan_start(struct mtf_stream *s)
{
s->flbread = 0; /* reset */
if (!mtfscan_ready(s, 48)) return 0;
s->header = s->buffer;
if (mtfdb_tape_type(s)) {
s->flbsize = mtfdb_tape_flbsize(s);
}
s->stringtype = mtfdb_strtype(s);
return 1;
}
int mtfscan_next(struct mtf_stream *s)
{
if (mtfscan_skip(s) < 0) return 0; /* skip to end of logical block */
s->ready = 0;
s->flbread = 0;
return 1;
}
char *mtfscan_string(struct mtf_stream *s, struct mtf_tape_pos q, int sz)
{
unsigned int i;
unsigned int n;
unsigned int cx, cx2;
unsigned char *uc;
char *out, *p;
#ifdef DEBUG
fprintf(stderr, "q.pos=%d, q.size=%d type=%d\n", q.pos, q.size, s->stringtype);
#endif
if (s->stringtype < 0) return 0; /* MTF_NO_STRINGS */
if (!mtfscan_ready(s, q.pos + q.size)) return 0;
if (!(s->stringtype & 1)) {
/* internally in UTF16LE */
/* find utf-8 length */
for (i = n = 0; i < q.size; i += 2) {
uc = (unsigned char *)&s->buffer[q.pos + i];
cx = uc[0] | (((unsigned int)uc[1]) << 8);
if (cx >= 0xd800 && cx <= 0xe000) {
if (i+3 >= q.size) return 0; // pedantic
cx2 = uc[2] | (((unsigned int)uc[3]) << 8);
cx = (cx2 & 0x3ff) + ((cx & 0x3f)<<10) + (((cx & 0x3c0) + 0x40) << 10);
i += 2;
}
if (cx < 0x80) n++;
else if (cx < 0x800) n += 2;
else if (cx < 0x10000) n += 3;
else n += 4;
}
/* convert to UTF8 */
p = out = (char *)malloc(n+1); /* for a terminating null */
if (!p) return 0;
for (i = n = 0; i < q.size; i += 2) {
uc = (unsigned char *)&s->buffer[q.pos + i];
cx = uc[0] | (((unsigned int)uc[1]) << 8);
if (cx == 0 && i+2 < q.size) cx = sz;
if (cx >= 0xd800 && cx <= 0xe000) {
if (i+3 >= q.size) return 0; // pedantic
cx2 = uc[2] | (((unsigned int)uc[3]) << 8);
cx = (cx2 & 0x3ff) + ((cx & 0x3f)<<10) + (((cx & 0x3c0) + 0x40) << 10);
i += 2;
}
if (cx < 0x80) {
*p++ = cx & 255;
} else if (cx < 0x800) {
*p++ = 0xc0 + ((cx >> 6) & 0x1f);
*p++ = 0x80 + (cx & 0x3f);
} else if (cx < 0x10000) {
*p++ = 0xe0 + ((cx>>12) & 0x0f);
*p++ = 0x80 + ((cx>>6) & 0x3f);
*p++ = 0x80 + (cx & 0x3f);
} else {
*p++ = 0xf0 + ((cx >> 18) & 7);
*p++ = 0x80 + ((cx >> 12) & 0x3f);
*p++ = 0x80 + ((cx >> 6) & 0x3f);
*p++ = 0x80 + (cx & 0x3f);
}
}
p[0] = 0; /* nul */
} else {
/* ascii (actually MSDOS CP 646) */
out = (char *)malloc(q.size+1);
if (!out) return 0;
memcpy(out, &s->buffer[q.pos], q.size);
if (sz) {
for (i = 0; i < q.size; i++)
if (!out[i] && (i+1) < q.size) out[i] = sz;
}
out[q.size] = 0;
}
return out;
}