-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinbuf.c
241 lines (185 loc) · 4.64 KB
/
inbuf.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
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
/*
inbuf.c -- random access to a stretch of a file
Copyright (C) 2000 Dieter Baron
This file is part of malint, an MPEG Audio stream validator.
The author can be contacted at <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdlib.h>
#include <string.h>
#include "inbuf.h"
#define BUFSIZE (1024*1024)
#define ALLOCADD 4096
struct inbuf *
inbuf_new(FILE *f, long length)
{
struct inbuf *inb;
if ((inb=(struct inbuf *)malloc(sizeof(struct inbuf))) == NULL)
return NULL;
inb->bsize = BUFSIZE;
inb->allocsize = inb->bsize + ALLOCADD;
if ((inb->b=(unsigned char *)malloc(inb->allocsize)) == NULL) {
free(inb);
return NULL;
}
inb->f = f;
inb->first = inb->last = 0;
inb->keep = -1;
inb->nkeep = 0;
inb->length = length;
inb->eof = 0;
return inb;
}
void
inbuf_free(struct inbuf *inb)
{
free(inb->b);
free(inb);
}
int
inbuf_fgetc(long pos, struct inbuf *inb)
{
int nskip, nread, first;
int n, k, off, n2;
if (pos < inb->first)
return -2; /* data no longer available */
else if (pos < inb->last)
return inb->b[pos % inb->bsize];
if (inb->eof)
return EOF;
if (inb->length != -1 && pos >= inb->length)
return EOF; /* logical EOF */
inb->first = ((inb->keep != -1 && inb->keep < pos)
? inb->keep : pos);
if (inb->first + inb->bsize < pos)
return -3; /* buffer overflow */
if (inb->first < inb->last) {
first = inb->last;
nskip = 0;
nread = inb->first+inb->bsize - first;
}
else {
first = inb->first;
nskip = inb->first-inb->last;
nread = inb->bsize;
}
if (inb->length != -1 && first + nread > inb->length)
nread = inb->length - first;
while (nskip > inb->bsize) {
for (n=0; ((k=fread(inb->b, 1, inb->bsize, inb->f)) > 0
&& (n+=k) < inb->bsize);)
;
if (k == 0) { /* physical EOF */
inb->eof = 1;
return EOF;
}
nskip -= inb->bsize;
}
first -= nskip;
nread += nskip;
off = first % inb->bsize;
n = inb->bsize - off;
if (n > nread)
n = nread;
for (n2=0; ((k=fread(inb->b+off+n2, 1, n-n2, inb->f)) > 0
&& (n2+=k) < n);)
;
if (k == 0) { /* physical EOF */
inb->eof = 1;
nread = n = n2;
}
inb->last = first + n2;
if (nread-n > 0) {
n = nread-n;
for (n2=0; ((k=fread(inb->b+n2, 1, n-n2, inb->f)) > 0
&& (n2+=k) < n);)
;
if (k == 0) /* physical EOF */
inb->eof = 1;
inb->last += n2;
}
if (pos > inb->last)
return EOF;
return inb->b[pos % inb->bsize];
}
int
inbuf_keep(long pos, struct inbuf *inb)
{
if (inb->nkeep >= INBUF_MAX_KEEP)
return -1;
inb->okeep[inb->nkeep++] = inb->keep;
if (inb->keep == -1 || pos < inb->keep)
inb->keep = pos;
return 0;
}
int
inbuf_unkeep(struct inbuf *inb)
{
if (inb->nkeep == 0)
inb->keep = -1;
else
inb->keep = inb->okeep[--inb->nkeep];
return 0;
}
int
inbuf_getlong(unsigned long *lp, long pos, struct inbuf *inb)
{
int c;
unsigned long l;
if ((c = inbuf_getc(pos, inb)) < 0)
return c;
l = (c & 0xff) << 24;
if ((c = inbuf_getc(pos+1, inb)) < 0)
return c;
l |= (c & 0xff) << 16;
if ((c = inbuf_getc(pos+2, inb)) < 0)
return c;
l |= (c & 0xff) << 8;
if ((c = inbuf_getc(pos+3, inb)) < 0)
return c;
l |= (c & 0xff);
*lp = l;
return 0;
}
int
inbuf_copy(unsigned char **b, long pos, long len, struct inbuf *inb)
{
int n;
if (pos < inb->first)
return -2;
inbuf_keep(pos, inb);
if (inbuf_getc(pos+len-1, inb) < 0) {
len = inbuf_length(inb) - pos;
inbuf_getc(pos+len-1, inb);
}
if ((pos / inb->bsize) != ((pos+len-1) / inb->bsize)) {
n = (pos+len) % inb->bsize;
if (inb->allocsize < inb->bsize+n) {
inb->allocsize = inb->bsize+n;
if ((inb->b=(unsigned char *)realloc(inb->b, inb->allocsize)) == NULL)
return -1;
}
memcpy(inb->b+inb->bsize, inb->b, n);
}
*b = inb->b + (pos%inb->bsize);
inbuf_unkeep(inb);
return len;
}
int
inbuf_length(struct inbuf *inb)
{
if (inb->eof)
return inb->last;
else
return inb->length;
}