| 1 |
|
| /* |
| 2 |
|
| * This program is free software; you can redistribute it and/or modify |
| 3 |
|
| * it under the terms of the GNU General Public License as published by |
| 4 |
|
| * the Free Software Foundation; either version 2 of the License, or |
| 5 |
|
| * (at your option) any later version. |
| 6 |
|
| * |
| 7 |
|
| * This program is distributed in the hope that it will be useful, |
| 8 |
|
| * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 |
|
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 |
|
| * GNU General Public License for more details. |
| 11 |
|
| * |
| 12 |
|
| * You should have received a copy of the GNU General Public License |
| 13 |
|
| * along with this program; if not, write to the Free Software |
| 14 |
|
| * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 15 |
|
| */ |
| 16 |
|
| |
| 17 |
|
| #include "common.h" |
| 18 |
|
| #include "buffer.c" |
| 19 |
|
| |
| 20 |
|
| int |
| 21 |
7289 |
| _check_buf(PerlIO *infile, Buffer *buf, int min_wanted, int max_wanted) |
| 22 |
|
| { |
| 23 |
|
| int ret = 1; |
| 24 |
|
| |
| 25 |
|
| // Do we have enough data? |
| 26 |
7289 |
▶ | if ( buffer_len(buf) < min_wanted ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( buffer_len(buf) < min_wanted ) { |
426 |
6863 |
7289 |
|
| 27 |
|
| // Read more data |
| 28 |
|
| uint32_t read; |
| 29 |
|
| uint32_t actual_wanted; |
| 30 |
|
| unsigned char *tmp; |
| 31 |
|
| |
| 32 |
|
| #ifdef _MSC_VER |
| 33 |
|
| uint32_t pos_check = PerlIO_tell(infile); |
| 34 |
|
| #endif |
| 35 |
|
| |
| 36 |
426 |
| if (min_wanted > max_wanted) { |
| 37 |
|
| max_wanted = min_wanted; |
| 38 |
|
| } |
| 39 |
|
| |
| 40 |
|
| // Adjust actual amount to read by the amount we already have in the buffer |
| 41 |
426 |
| actual_wanted = max_wanted - buffer_len(buf); |
| 42 |
|
| |
| 43 |
426 |
| New(0, tmp, actual_wanted, unsigned char); |
| 44 |
|
| |
| 45 |
|
| DEBUG_TRACE("Buffering from file @ %d (min_wanted %d, max_wanted %d, adjusted to %d)\n", |
| 46 |
|
| (int)PerlIO_tell(infile), min_wanted, max_wanted, actual_wanted |
| 47 |
|
| ); |
| 48 |
|
| |
| 49 |
426 |
▶ | if ( (read = PerlIO_read(infile, tmp, actual_wanted)) <= 0 ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( (read = PerlIO_read(infile, tmp, actual_wanted)) <= 0 ) { |
0 |
426 |
426 |
|
| 50 |
0 |
▶ | if ( PerlIO_error(infile) ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( PerlIO_error(infile) ) { |
0 |
0 |
0 |
|
| 51 |
0 |
| warn("Error reading: %s (wanted %d)\n", strerror(errno), actual_wanted); |
| 52 |
|
| } |
| 53 |
|
| else { |
| 54 |
0 |
| warn("Error: Unable to read at least %d bytes from file.\n", min_wanted); |
| 55 |
|
| } |
| 56 |
|
| |
| 57 |
|
| ret = 0; |
| 58 |
0 |
| goto out; |
| 59 |
|
| } |
| 60 |
|
| |
| 61 |
426 |
| buffer_append(buf, tmp, read); |
| 62 |
|
| |
| 63 |
|
| // Make sure we got enough |
| 64 |
426 |
▶ | if ( buffer_len(buf) < min_wanted ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( buffer_len(buf) < min_wanted ) { |
0 |
426 |
426 |
|
| 65 |
0 |
| warn("Error: Unable to read at least %d bytes from file (only read %d).\n", min_wanted, read); |
| 66 |
|
| ret = 0; |
| 67 |
0 |
| goto out; |
| 68 |
|
| } |
| 69 |
|
| |
| 70 |
|
| #ifdef _MSC_VER |
| 71 |
|
| // Bug 16095, weird off-by-one bug seen only on Win32 and only when reading a filehandle |
| 72 |
|
| if (PerlIO_tell(infile) != pos_check + read) { |
| 73 |
|
| //PerlIO_printf(PerlIO_stderr(), "Win32 bug, pos should be %d, but was %d\n", pos_check + read, PerlIO_tell(infile)); |
| 74 |
|
| PerlIO_seek(infile, pos_check + read, SEEK_SET); |
| 75 |
|
| } |
| 76 |
|
| #endif |
| 77 |
|
| |
| 78 |
|
| DEBUG_TRACE("Buffered %d bytes, new pos %d\n", read, (int)PerlIO_tell(infile)); |
| 79 |
|
| |
| 80 |
426 |
| out: |
| 81 |
426 |
| Safefree(tmp); |
| 82 |
|
| } |
| 83 |
|
| |
| 84 |
7289 |
| return ret; |
| 85 |
|
| } |
| 86 |
|
| |
| 87 |
|
| off_t |
| 88 |
30 |
| _file_size(PerlIO *infile) |
| 89 |
|
| { |
| 90 |
|
| #ifdef _MSC_VER |
| 91 |
|
| // Win32 doesn't work right with fstat |
| 92 |
|
| off_t file_size; |
| 93 |
|
| |
| 94 |
|
| PerlIO_seek(infile, 0, SEEK_END); |
| 95 |
|
| file_size = PerlIO_tell(infile); |
| 96 |
|
| PerlIO_seek(infile, 0, SEEK_SET); |
| 97 |
|
| |
| 98 |
|
| return file_size; |
| 99 |
|
| #else |
| 100 |
|
| struct stat buf; |
| 101 |
|
| |
| 102 |
30 |
▶ | if ( !fstat( PerlIO_fileno(infile), &buf ) ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( !fstat( PerlIO_fileno(infile), &buf ) ) { |
30 |
0 |
30 |
|
| 103 |
30 |
| return buf.st_size; |
| 104 |
|
| } |
| 105 |
|
| |
| 106 |
0 |
| warn("Unable to stat: %s\n", strerror(errno)); |
| 107 |
|
| |
| 108 |
0 |
| return 0; |
| 109 |
|
| #endif |
| 110 |
|
| } |