blob: 2369b68c60565e3ac7fced8d013b66ef29e52117 [file] [log] [blame]
Alistair Delvabeaee832021-02-24 11:27:23 -08001/* 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 Delvabeaee832021-02-24 11:27:23 -080020#include "llong.h"
21#include "mtools.h"
22
23#if 1
24const mt_off_t max_off_t_31 = MAX_OFF_T_B(31); /* Floppyd */
25static const mt_off_t max_off_t_32 = MAX_OFF_T_B(32); /* Directory */
26const mt_off_t max_off_t_41 = MAX_OFF_T_B(41); /* SCSI */
27const mt_off_t max_off_t_seek = MAX_OFF_T_B(SEEK_BITS); /* SCSI */
28#else
29const mt_off_t max_off_t_31 = MAX_OFF_T_B(10); /* Floppyd */
30const mt_off_t max_off_t_41 = MAX_OFF_T_B(10); /* SCSI */
31const mt_off_t max_off_t_seek = MAX_OFF_T_B(10); /* SCSI */
32#endif
33
34int fileTooBig(mt_off_t off) {
35 return (off & ~max_off_t_32) != 0;
36}
37
Yi Kong39bbd962022-01-09 19:41:38 +080038/* truncMtOffToOff */
Alistair Delvabeaee832021-02-24 11:27:23 -080039off_t truncBytes32(mt_off_t off)
40{
Yi Kong39bbd962022-01-09 19:41:38 +080041 if (fileTooBig(off)) {
42 fprintf(stderr, "Internal error, offset too big\n");
Alistair Delvabeaee832021-02-24 11:27:23 -080043 exit(1);
44 }
45 return (off_t) off;
46}
47
Yi Kong39bbd962022-01-09 19:41:38 +080048uint32_t truncMtOffTo32u(mt_off_t off)
Alistair Delvabeaee832021-02-24 11:27:23 -080049{
Yi Kong39bbd962022-01-09 19:41:38 +080050 if (fileTooBig(off)) {
51 fprintf(stderr, "Internal error, offset too big\n");
52 exit(1);
53 }
54 return (uint32_t) off;
Alistair Delvabeaee832021-02-24 11:27:23 -080055}
56
Yi Kong39bbd962022-01-09 19:41:38 +080057uint32_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
67mt_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 Delvabeaee832021-02-24 11:27:23 -080079#if defined HAVE_LLSEEK
80# ifndef HAVE_LLSEEK_PROTOTYPE
81extern 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
87extern long long lseek64 (int fd, long long offset, int origin);
88# endif
89#endif
90
Alistair Delvabeaee832021-02-24 11:27:23 -080091int 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 Kong39bbd962022-01-09 19:41:38 +0800102 return -1;
Alistair Delvabeaee832021-02-24 11:27:23 -0800103#else
104 if (lseek(fd, (off_t) where, whence) >= 0)
105 return 0;
106 else
107 return 1;
108#endif
109}
110
Yi Kong39bbd962022-01-09 19:41:38 +0800111unsigned int log_2(unsigned int size)
Alistair Delvabeaee832021-02-24 11:27:23 -0800112{
113 unsigned int i;
114
115 for(i=0; i<24; i++) {
Yi Kong39bbd962022-01-09 19:41:38 +0800116 if(1u << i == size)
Alistair Delvabeaee832021-02-24 11:27:23 -0800117 return i;
118 }
119 return 24;
120}