Alistair Delva | beaee83 | 2021-02-24 11:27:23 -0800 | [diff] [blame] | 1 | #ifndef MTOOLS_LLONG_H |
| 2 | #define MTOOLS_LLONG_H |
| 3 | |
Yi Kong | 39bbd96 | 2022-01-09 19:41:38 +0800 | [diff] [blame] | 4 | /* Copyright 1999,2001-2004,2007-2009,2021 Alain Knaff. |
Alistair Delva | beaee83 | 2021-02-24 11:27:23 -0800 | [diff] [blame] | 5 | * This file is part of mtools. |
| 6 | * |
| 7 | * Mtools is free software: you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation, either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * Mtools is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with Mtools. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #if 1 |
| 22 | |
| 23 | |
Yi Kong | 39bbd96 | 2022-01-09 19:41:38 +0800 | [diff] [blame] | 24 | #if SIZEOF_OFF_T >= 8 |
Alistair Delva | beaee83 | 2021-02-24 11:27:23 -0800 | [diff] [blame] | 25 | /* if off_t is already 64 bits, be happy, and don't worry about the |
| 26 | * loff_t and llseek stuff */ |
| 27 | # define MT_OFF_T off_t |
Yi Kong | 39bbd96 | 2022-01-09 19:41:38 +0800 | [diff] [blame] | 28 | # define SIZEOF_MT_OFF_T SIZEOF_OFF_T |
| 29 | #endif |
| 30 | |
| 31 | #ifndef MT_OFF_T |
| 32 | # if defined(HAVE_LSEEK64) && defined (HAVE_OFF64_T) |
| 33 | # define MT_OFF_T off64_t |
| 34 | # define SIZEOF_MT_OFF_T 8 |
Alistair Delva | beaee83 | 2021-02-24 11:27:23 -0800 | [diff] [blame] | 35 | # endif |
| 36 | #endif |
| 37 | |
Yi Kong | 39bbd96 | 2022-01-09 19:41:38 +0800 | [diff] [blame] | 38 | |
Alistair Delva | beaee83 | 2021-02-24 11:27:23 -0800 | [diff] [blame] | 39 | #ifndef MT_OFF_T |
| 40 | # if defined(HAVE_LLSEEK) || defined(HAVE_LSEEK64) |
| 41 | /* we have llseek. Now, what's its type called? loff_t or offset_t ? */ |
| 42 | # ifdef HAVE_LOFF_T |
| 43 | # define MT_OFF_T loff_t |
Yi Kong | 39bbd96 | 2022-01-09 19:41:38 +0800 | [diff] [blame] | 44 | # define SIZEOF_MT_OFF_T 8 |
Alistair Delva | beaee83 | 2021-02-24 11:27:23 -0800 | [diff] [blame] | 45 | /* use the same type for size. Better to get signedness wrong than width */ |
Alistair Delva | beaee83 | 2021-02-24 11:27:23 -0800 | [diff] [blame] | 46 | # else |
| 47 | # ifdef HAVE_OFFSET_T |
| 48 | # define MT_OFF_T offset_t |
Yi Kong | 39bbd96 | 2022-01-09 19:41:38 +0800 | [diff] [blame] | 49 | # define SIZEOF_MT_OFF_T 8 |
Alistair Delva | beaee83 | 2021-02-24 11:27:23 -0800 | [diff] [blame] | 50 | # endif |
| 51 | # endif |
| 52 | # endif |
| 53 | #endif |
| 54 | |
| 55 | #ifndef MT_OFF_T |
| 56 | /* we still don't have a suitable mt_off_t type...*/ |
| 57 | # ifdef HAVE_LONG_LONG |
| 58 | /* ... first try long long ... */ |
| 59 | # define MT_OFF_T long long |
Yi Kong | 39bbd96 | 2022-01-09 19:41:38 +0800 | [diff] [blame] | 60 | # define SIZEOF_MT_OFF_T 8 |
Alistair Delva | beaee83 | 2021-02-24 11:27:23 -0800 | [diff] [blame] | 61 | # else |
Yi Kong | 39bbd96 | 2022-01-09 19:41:38 +0800 | [diff] [blame] | 62 | /* ... and if that fails, fall back on good ole' off_t, even if that |
| 63 | * only has 32 bits */ |
| 64 | # define MT_OFF_T off_t |
| 65 | # define SIZEOF_MT_OFF_T SIZEOF_OFF_T |
Alistair Delva | beaee83 | 2021-02-24 11:27:23 -0800 | [diff] [blame] | 66 | # endif |
| 67 | #endif |
| 68 | |
| 69 | typedef MT_OFF_T mt_off_t; |
Yi Kong | 39bbd96 | 2022-01-09 19:41:38 +0800 | [diff] [blame] | 70 | |
| 71 | /* Define a common supertype of uint32_t and mt_off_t. Usually, |
| 72 | * mt_off_t is bigger, except on 32-bit architectures without large |
| 73 | * file support, where uint32_t can represent larger values. N.B. in |
| 74 | * such a setup, negative values of mt_off_t could not be handled, but |
| 75 | * we don't use any such values anyways in mtools |
| 76 | */ |
| 77 | #if SIZEOF_MT_OFF_T == 4 |
| 78 | |
| 79 | typedef uint32_t smt_off_t; |
| 80 | mt_off_t to_mt_off_t(uint32_t off); |
| 81 | |
| 82 | #else |
| 83 | |
| 84 | typedef mt_off_t smt_off_t; |
| 85 | #define to_mt_off_t(x) (x) |
| 86 | |
| 87 | #endif |
Alistair Delva | beaee83 | 2021-02-24 11:27:23 -0800 | [diff] [blame] | 88 | |
| 89 | #else |
| 90 | /* testing: meant to flag dubious assignments between 32 bit length types |
| 91 | * and 64 bit ones */ |
| 92 | typedef struct { |
| 93 | unsigned int lo; |
| 94 | int high; |
| 95 | } *mt_off_t; |
| 96 | |
Alistair Delva | beaee83 | 2021-02-24 11:27:23 -0800 | [diff] [blame] | 97 | #endif |
| 98 | |
| 99 | #define min(a,b) ((a) < (b) ? (a) : (b)) |
| 100 | #define MAX_OFF_T_B(bits) \ |
| 101 | ((((mt_off_t) 1 << min(bits-1, sizeof(mt_off_t)*8 - 2)) -1) << 1 | 1) |
| 102 | |
| 103 | #if defined(HAVE_LLSEEK) || defined(HAVE_LSEEK64) |
| 104 | # define SEEK_BITS 63 |
| 105 | #else |
| 106 | # define SEEK_BITS (sizeof(off_t) * 8 - 1) |
| 107 | #endif |
| 108 | |
| 109 | extern const mt_off_t max_off_t_31; |
| 110 | extern const mt_off_t max_off_t_41; |
| 111 | extern const mt_off_t max_off_t_seek; |
| 112 | |
Yi Kong | 39bbd96 | 2022-01-09 19:41:38 +0800 | [diff] [blame] | 113 | extern off_t truncBytes32(mt_off_t off); /* truncMtOffToOff */ |
| 114 | extern uint32_t truncMtOffTo32u(mt_off_t off); |
| 115 | extern uint32_t truncSizeTo32u(size_t siz); |
Alistair Delva | beaee83 | 2021-02-24 11:27:23 -0800 | [diff] [blame] | 116 | extern int fileTooBig(mt_off_t off); |
| 117 | |
| 118 | int mt_lseek(int fd, mt_off_t where, int whence); |
| 119 | |
Yi Kong | 39bbd96 | 2022-01-09 19:41:38 +0800 | [diff] [blame] | 120 | unsigned int log_2(unsigned int); |
Alistair Delva | beaee83 | 2021-02-24 11:27:23 -0800 | [diff] [blame] | 121 | |
| 122 | #endif |