Debian Codegraph @

src/buffer.cpp

raw
1 /* dvbcut
2 Copyright (c) 2005 Sven Over <[email protected]>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 /* $Id$ */
20
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <sys/mman.h>
24 #include <cerrno>
25 #include <fcntl.h>
26 #include <unistd.h>
27 #include <cstring>
28 #include <cstdlib>
29 #include <cstdio>
30 #include <stdint.h>
31 #include <cassert>
32
33 #include <string>
34 #include <vector>
35
36 #include "port.h"
37 #include "buffer.h"
38
39 #ifndef O_BINARY
40 #define O_BINARY 0
41 #endif /* O_BINARY */
42
43 #ifndef MAP_FAILED
44 #define MAP_FAILED ((void*)-1)
45 #endif
46
47 buffer::buffer(unsigned int _size):size(_size), readpos(0), writepos(0), wrtot(0)
48 {
49 if (size > 0)
50 d = malloc(size);
51 }
52
53 buffer::~buffer()
54 {
55 if (d)
56 free(d);
57 }
58
59 void buffer::relax()
60 {
61 if (readpos == 0)
62 return;
63 if (writepos != readpos)
64 memmove(d, (u_int8_t *) d + readpos, writepos - readpos);
65 writepos -= readpos;
66 readpos = 0;
67 }
68
69 void buffer::resize(unsigned int newsize)
70 {
71 if (newsize == 0 || newsize == size)
72 return;
73 if (newsize < inbytes())
74 writepos = readpos + newsize;
75
76 void *olddata = d;
77
78 d = malloc(newsize);
79 memcpy(d, (u_int8_t *) olddata + readpos, inbytes());
80 free(olddata);
81
82 size = newsize;
83 writepos -= readpos;
84 readpos = 0;
85 }
86
87 unsigned int buffer::putdata(const void *data, unsigned int len, bool autoresize)
88 {
89 if (len > freebytes()) {
90 if (autoresize && len<(1u<<30))
91 // Only resize if len has a reasonable value, to circumvent problems
92 // when (signed int)len<0 (which can only be due to bug)
93 resize(2*(inbytes()+len));
94 else
95 len = freebytes();
96 }
97 if (len == 0)
98 return 0;
99 if (readpos == writepos)
100 readpos = writepos = 0;
101 else if (len > (size - writepos))
102 relax();
103 memcpy((u_int8_t *) d + writepos, data, len);
104 writepos += len;
105 wrtot += len;
106 return len;
107 }
108
109 unsigned int buffer::getdata(void *data, unsigned int len)
110 {
111 if (len > inbytes())
112 len = inbytes();
113 if (len == 0)
114 return 0;
115 memcpy(data, (u_int8_t *) d + readpos, len);
116 readpos += len;
117 if (readpos == writepos)
118 readpos = writepos = 0;
119 return len;
120 }
121
122 int buffer::readdata(int fd)
123 {
124 relax();
125 if (writepos == size)
126 return 0;
127 int r = read(fd, (u_int8_t *) d + writepos, size - writepos);
128
129 if (r > 0) {
130 writepos += r;
131 wrtot += r;
132 }
133 return r;
134 }
135
136 int buffer::writedata(int fd)
137 {
138 if (readpos == writepos) {
139 readpos = writepos = 0;
140 return 0;
141 }
142 int w = write(fd, (u_int8_t *) d + readpos, writepos - readpos);
143
144 if (w > 0)
145 readpos += w;
146 return w;
147 }
148
149
150 // INBUFFER *****************************************************************
151
152 long inbuffer::pagesize=0;
153
154 inbuffer::inbuffer(unsigned int _size, unsigned int _mmapsize) :
155 d(0), size(_size), mmapsize(_mmapsize), readpos(0), writepos(0),
156 eof(false), pos(0), filesize(0), mmapped(false),
157 sequential(false), pipe_mode(false)
158 {
159 if (!pagesize)
160 pagesize=sysconf(_SC_PAGESIZE);
161 }
162
163 inbuffer::~inbuffer() {
164 close();
165 }
166
167 bool
168 inbuffer::open(std::string filename, std::string *errmsg) {
169 int fd;
170
171 fd = ::open(filename.c_str(), O_RDONLY | O_BINARY);
172 if (fd == -1) {
173 if (errmsg)
174 *errmsg = filename + ": open: " + strerror(errno);
175 return false;
176 }
177 if (!open(fd, errmsg, true, filename)) {
178 if (errmsg)
179 *errmsg = filename + ": " + *errmsg;
180 return false;
181 }
182 return true;
183 }
184
185 bool
186 inbuffer::open(int fd, std::string *errmsg, bool closeme, std::string filename) {
187 infile f;
188
189 f.fd = fd;
190 f.name = filename;
191 f.closeme = closeme;
192 if (pipe_mode) {
193 // no more files please!
194 if (errmsg)
195 *errmsg = std::string("open: can't add more input files");
196 if (f.closeme)
197 ::close(f.fd);
198 return false;
199 }
200 #ifdef __WIN32__
201 struct _stati64 st;
202 if (::_fstati64(f.fd, &st) == -1) {
203 #else /* __WIN32__ */
204 struct stat st;
205 if (::fstat(f.fd, &st) == -1) {
206 #endif /* __WIN32__ */
207 if (errmsg)
208 *errmsg = std::string("fstat: ") + strerror(errno);
209 if (f.closeme)
210 ::close(f.fd);
211 return false;
212 }
213 if (S_ISREG(st.st_mode)) {
214 f.off = filesize;
215 f.end = filesize += st.st_size;
216 files.push_back(f);
217 return true;
218 }
219 /*
220 * Input is a (named) pipe or device.
221 * This is only allowed in single-file (aka "pipe") mode.
222 */
223 if (files.empty()) {
224 pipe_mode = true;
225 f.off = 0;
226 f.end = filesize = INT64_MAX;
227 files.push_back(f);
228 return true;
229 }
230 if (errmsg)
231 *errmsg = std::string("not a regular file");
232 if (f.closeme)
233 ::close(f.fd);
234 return false;
235 }
236
237 void
238 inbuffer::close() {
239 // close all files
240 std::vector<infile>::const_iterator i = files.begin();
241 while (i != files.end()) {
242 if (i->closeme)
243 ::close(i->fd);
244 ++i;
245 }
246 files.clear();
247 // free buffer
248 #ifndef _WIN32
249 if (mmapped)
250 ::munmap(d, writepos);
251 else
252 #endif
253 if (d)
254 free(d);
255 mmapped = false;
256 d = 0;
257 pipe_mode = false;
258 }
259
260 void
261 inbuffer::reset() {
262 close();
263 // re-initialize members
264 readpos = 0;
265 writepos = 0;
266 eof = false;
267 pos = 0;
268 filesize = 0;
269 }
270
271 int
272 inbuffer::pipedata(unsigned int amount, long long position) {
273 std::vector<infile>::const_iterator i = files.begin();
274 assert(i != files.end());
275
276 // allocate read buffer
277 if (!d) {
278 d = malloc(size);
279 if (!d) {
280 fprintf(stderr, "inbuffer::pipedata: can't allocate %ld bytes: %s\n",
281 (long)size, strerror(errno));
282 abort();
283 }
284 readpos = 0;
285 writepos = 0;
286 }
287
288 if (amount > size)
289 amount = size;
290
291 // discard unused data
292 readpos = 0;
293 while (pos + writepos <= position) {
294 pos += writepos;
295 writepos = 0;
296 ssize_t n = ::read(i->fd, (char*)d, size);
297 if (n == -1)
298 return -1;
299 if (n == 0) {
300 eof = true;
301 return 0;
302 }
303 writepos = n;
304 }
305 if (pos < position) {
306 size_t pp = position - pos;
307 writepos -= pp;
308 memmove(d, (char*)d + pp, writepos);
309 pos = position;
310 }
311
312 // now read the data we want
313 while (writepos < amount) {
314 size_t len = size - writepos;
315 assert(len > 0);
316 ssize_t n = ::read(i->fd, (char*)d + writepos, len);
317 if (n == -1)
318 return -1;
319 if (n == 0) {
320 eof = true;
321 break;
322 }
323 writepos += n;
324 }
325 return inbytes();
326 }
327
328 int
329 inbuffer::providedata(unsigned int amount, long long position) {
330 if (position < 0 || position >= filesize)
331 return 0;
332
333 if (position + amount > filesize)
334 amount = filesize - position;
335 if (position >= pos && position + amount <= pos + writepos) {
336 readpos = position - pos;
337 return inbytes();
338 }
339
340 if (pipe_mode) {
341 if (position < pos)
342 return -1; // can't go backwards!
343 return pipedata(amount, position);
344 }
345
346 std::vector<infile>::const_iterator i = files.begin();
347 assert(i != files.end()); // otherwise we would have returned already
348 while (position >= i->end) {
349 #ifdef POSIX_FADV_DONTNEED
350 if (sequential) {
351 off_t len = i->end - i->off;
352 posix_fadvise(i->fd, 0, len, POSIX_FADV_DONTNEED);
353 }
354 #endif
355 ++i;
356 assert(i != files.end());
357 }
358 assert(position >= i->off);
359
360 #ifndef _WIN32
361 // remove old mapping, if any
362 if (mmapped) {
363 ::munmap(d, writepos);
364 mmapped = false;
365 d = 0;
366 }
367
368 if (mmapsize > 0 && position + amount <= i->end) {
369 // calculate mmap window
370 dvbcut_off_t newpos = position + amount - mmapsize / 2;
371 if (newpos > position)
372 newpos = position;
373 else if (newpos < i->off)
374 newpos = i->off;
375 // align to pagesize
376 // note: relpos must be aligned, NOT newpos!
377 off_t relpos = newpos - i->off;
378 size_t modulus = relpos % pagesize;
379 relpos -= modulus;
380 newpos -= modulus;
381 #ifdef POSIX_FADV_DONTNEED
382 if (sequential) {
383 // we're done with earlier parts
384 if (relpos)
385 posix_fadvise(i->fd, 0, relpos, POSIX_FADV_DONTNEED);
386 }
387 #endif
388 size_t len = mmapsize;
389 if (newpos + dvbcut_off_t(len) > i->end)
390 len = i->end - newpos;
391 void *ptr = ::mmap(0, len, PROT_READ, MAP_SHARED, i->fd, relpos);
392 if (ptr != MAP_FAILED) {
393 // mmap succeeded
394 if (d)
395 free(d);
396 d = ptr;
397 readpos = position - newpos;
398 writepos = len;
399 pos = newpos;
400 mmapped = true;
401 return inbytes();
402 }
403 }
404 #endif
405
406 // allocate read buffer
407 if (!d) {
408 d = malloc(size);
409 if (!d) {
410 fprintf(stderr, "inbuffer::providedata: can't allocate %ld bytes: %s\n",
411 (long)size, strerror(errno));
412 abort();
413 }
414 readpos = 0;
415 writepos = 0;
416 pos = 0;
417 }
418
419 if (amount > size)
420 amount = size;
421 // reuse existing data if possible
422 if (position >= pos && position < pos + writepos) {
423 unsigned int pp = position - pos;
424 if (pp > 0) {
425 writepos -= pp;
426 memmove(d, (char*)d + pp, writepos);
427 }
428 }
429 else {
430 writepos = 0;
431 }
432 readpos = 0;
433 pos = position;
434 bool needseek = true;
435 while (writepos < amount) {
436 dvbcut_off_t seekpos = pos + writepos;
437 while (seekpos >= i->end) {
438 ++i;
439 assert(i != files.end());
440 needseek = true;
441 }
442 assert(seekpos >= i->off);
443 if (needseek) {
444 #ifdef __WIN32__
445 __int64 relpos = seekpos - i->off;
446 if (::_lseeki64(i->fd, relpos, SEEK_SET) == -1)
447 #else /* __WIN32__ */
448 off_t relpos = seekpos - i->off;
449 if (::lseek(i->fd, relpos, SEEK_SET) == -1)
450 #endif /* __WIN32__ */
451 return -1;
452 needseek = false;
453 }
454 size_t len = size - writepos;
455 if (dvbcut_off_t(len) > i->end - seekpos)
456 len = i->end - seekpos;
457 assert(len > 0);
458 ssize_t n = ::read(i->fd, (char*)d + writepos, len);
459 if (n == -1)
460 return -1;
461 if (n == 0) { // this should NOT happen!
462 eof = true;
463 break;
464 }
465 writepos += n;
466 }
467 return inbytes();
468 }
469
470 int
471 inbuffer::getfilenum(dvbcut_off_t offset, dvbcut_off_t &fileoff) {
472 std::vector<infile>::const_iterator it = files.begin();
473 unsigned int num = 0;
474 while (it != files.end()) {
475 if (offset < it->end) {
476 fileoff = it->off;
477 return num;
478 }
479 ++num;
480 ++it;
481 }
482 return -1;
483 }
484
485 std::string
486 inbuffer::getfilename(int filenum) {
487 return files.at(filenum).name;
488 }
489
490
491 // OUTBUFFER ****************************************************************
492
493 outbuffer::~outbuffer()
494 {
495 if (close && fd>=0)
496 ::close(fd);
497 }
498
499 int outbuffer::open(const char* filename)
500 {
501 close=true;
502 return fd=::open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
503 }
504
505 int outbuffer::putdata(const void *data, unsigned int len, bool autoresize)
506 {
507 if (freebytes()>=len)
508 return putdata(data,len,autoresize);
509
510 int wrn=flush(fd);
511 if (wrn<0)
512 return wrn;
513
514 while (len>0) {
515 int wr=::write(fd,data,len);
516 if (wr<0)
517 return wr;
518 data=(char*)data+wr;
519 len-=wr;
520 wrn+=wr;
521 wrtot+=wr;
522 }
523
524 return wrn;
525 }