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