xref: /aosp_15_r20/external/mtools/llong.h (revision d5c9a868b113e0ec0db2f27bc2ce8a253e77c4b0)
1 #ifndef MTOOLS_LLONG_H
2 #define MTOOLS_LLONG_H
3 
4 /*  Copyright 1999,2001-2004,2007-2009,2021 Alain Knaff.
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 
24 #if SIZEOF_OFF_T >= 8
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
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
35 # endif
36 #endif
37 
38 
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
44 #   define SIZEOF_MT_OFF_T 8
45 /* use the same type for size. Better to get signedness wrong than width */
46 #  else
47 #   ifdef HAVE_OFFSET_T
48 #    define MT_OFF_T offset_t
49 #    define SIZEOF_MT_OFF_T 8
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
60 #  define SIZEOF_MT_OFF_T 8
61 # else
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
66 # endif
67 #endif
68 
69 typedef MT_OFF_T mt_off_t;
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
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 
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 
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);
116 extern int fileTooBig(mt_off_t off);
117 
118 int mt_lseek(int fd, mt_off_t where, int whence);
119 
120 unsigned int log_2(unsigned int);
121 
122 #endif
123