Alistair Delva | beaee83 | 2021-02-24 11:27:23 -0800 | [diff] [blame] | 1 | /* Copyright 1999-2003,2006,2008,2009 Alain Knaff. |
| 2 | * This file is part of mtools. |
| 3 | * |
| 4 | * Mtools 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 3 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * Mtools 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 Mtools. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | #include "sysincludes.h" |
| 19 | #include "stream.h" |
Alistair Delva | beaee83 | 2021-02-24 11:27:23 -0800 | [diff] [blame] | 20 | #include "llong.h" |
| 21 | #include "mtools.h" |
| 22 | |
| 23 | #if 1 |
| 24 | const mt_off_t max_off_t_31 = MAX_OFF_T_B(31); /* Floppyd */ |
| 25 | static const mt_off_t max_off_t_32 = MAX_OFF_T_B(32); /* Directory */ |
| 26 | const mt_off_t max_off_t_41 = MAX_OFF_T_B(41); /* SCSI */ |
| 27 | const mt_off_t max_off_t_seek = MAX_OFF_T_B(SEEK_BITS); /* SCSI */ |
| 28 | #else |
| 29 | const mt_off_t max_off_t_31 = MAX_OFF_T_B(10); /* Floppyd */ |
| 30 | const mt_off_t max_off_t_41 = MAX_OFF_T_B(10); /* SCSI */ |
| 31 | const mt_off_t max_off_t_seek = MAX_OFF_T_B(10); /* SCSI */ |
| 32 | #endif |
| 33 | |
| 34 | int fileTooBig(mt_off_t off) { |
| 35 | return (off & ~max_off_t_32) != 0; |
| 36 | } |
| 37 | |
Yi Kong | 39bbd96 | 2022-01-09 19:41:38 +0800 | [diff] [blame] | 38 | /* truncMtOffToOff */ |
Alistair Delva | beaee83 | 2021-02-24 11:27:23 -0800 | [diff] [blame] | 39 | off_t truncBytes32(mt_off_t off) |
| 40 | { |
Yi Kong | 39bbd96 | 2022-01-09 19:41:38 +0800 | [diff] [blame] | 41 | if (fileTooBig(off)) { |
| 42 | fprintf(stderr, "Internal error, offset too big\n"); |
Alistair Delva | beaee83 | 2021-02-24 11:27:23 -0800 | [diff] [blame] | 43 | exit(1); |
| 44 | } |
| 45 | return (off_t) off; |
| 46 | } |
| 47 | |
Yi Kong | 39bbd96 | 2022-01-09 19:41:38 +0800 | [diff] [blame] | 48 | uint32_t truncMtOffTo32u(mt_off_t off) |
Alistair Delva | beaee83 | 2021-02-24 11:27:23 -0800 | [diff] [blame] | 49 | { |
Yi Kong | 39bbd96 | 2022-01-09 19:41:38 +0800 | [diff] [blame] | 50 | if (fileTooBig(off)) { |
| 51 | fprintf(stderr, "Internal error, offset too big\n"); |
| 52 | exit(1); |
| 53 | } |
| 54 | return (uint32_t) off; |
Alistair Delva | beaee83 | 2021-02-24 11:27:23 -0800 | [diff] [blame] | 55 | } |
| 56 | |
Yi Kong | 39bbd96 | 2022-01-09 19:41:38 +0800 | [diff] [blame] | 57 | uint32_t truncSizeTo32u(size_t siz) |
| 58 | { |
| 59 | if (siz > UINT32_MAX) { |
| 60 | fprintf(stderr, "Internal error, size too big\n"); |
| 61 | exit(1); |
| 62 | } |
| 63 | return (uint32_t) siz; |
| 64 | } |
| 65 | |
| 66 | #if SIZEOF_MT_OFF_T == 4 |
| 67 | mt_off_t to_mt_off_t(uint32_t off) |
| 68 | { |
| 69 | if(off > UINT32_MAX >> 1) { |
| 70 | fprintf(stderr, "File size/pos %d too big for this platform\n", |
| 71 | off); |
| 72 | exit(1); |
| 73 | } |
| 74 | return (mt_off_t) off; |
| 75 | } |
| 76 | #endif |
| 77 | |
| 78 | |
Alistair Delva | beaee83 | 2021-02-24 11:27:23 -0800 | [diff] [blame] | 79 | #if defined HAVE_LLSEEK |
| 80 | # ifndef HAVE_LLSEEK_PROTOTYPE |
| 81 | extern long long llseek (int fd, long long offset, int origin); |
| 82 | # endif |
| 83 | #endif |
| 84 | |
| 85 | #if defined HAVE_LSEEK64 |
| 86 | # ifndef HAVE_LSEEK64_PROTOTYPE |
| 87 | extern long long lseek64 (int fd, long long offset, int origin); |
| 88 | # endif |
| 89 | #endif |
| 90 | |
Alistair Delva | beaee83 | 2021-02-24 11:27:23 -0800 | [diff] [blame] | 91 | int mt_lseek(int fd, mt_off_t where, int whence) |
| 92 | { |
| 93 | #if defined HAVE_LSEEK64 |
| 94 | if(lseek64(fd, where, whence) >= 0) |
| 95 | return 0; |
| 96 | else |
| 97 | return -1; |
| 98 | #elif defined HAVE_LLSEEK |
| 99 | if(llseek(fd, where, whence) >= 0) |
| 100 | return 0; |
| 101 | else |
Yi Kong | 39bbd96 | 2022-01-09 19:41:38 +0800 | [diff] [blame] | 102 | return -1; |
Alistair Delva | beaee83 | 2021-02-24 11:27:23 -0800 | [diff] [blame] | 103 | #else |
| 104 | if (lseek(fd, (off_t) where, whence) >= 0) |
| 105 | return 0; |
| 106 | else |
| 107 | return 1; |
| 108 | #endif |
| 109 | } |
| 110 | |
Yi Kong | 39bbd96 | 2022-01-09 19:41:38 +0800 | [diff] [blame] | 111 | unsigned int log_2(unsigned int size) |
Alistair Delva | beaee83 | 2021-02-24 11:27:23 -0800 | [diff] [blame] | 112 | { |
| 113 | unsigned int i; |
| 114 | |
| 115 | for(i=0; i<24; i++) { |
Yi Kong | 39bbd96 | 2022-01-09 19:41:38 +0800 | [diff] [blame] | 116 | if(1u << i == size) |
Alistair Delva | beaee83 | 2021-02-24 11:27:23 -0800 | [diff] [blame] | 117 | return i; |
| 118 | } |
| 119 | return 24; |
| 120 | } |