1 /*
2 
3     File: common.h
4 
5     Copyright (C) 1998-2007 Christophe GRENIER <grenier@cgsecurity.org>
6 
7     This software 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 2 of the License, or
10     (at your option) any later version.
11 
12     This program 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 along
18     with this program; if not, write the Free Software Foundation, Inc., 51
19     Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 
21  */
22 #ifndef _COMMON_H
23 #define _COMMON_H
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 typedef struct efi_guid_s efi_guid_t;
29 struct efi_guid_s
30 {
31   uint32_t time_low;
32   uint16_t time_mid;
33   uint16_t time_hi_and_version;
34   uint8_t  clock_seq_hi_and_reserved;
35   uint8_t  clock_seq_low;
36   uint8_t  node[6];
37 } __attribute__ ((gcc_struct, __packed__));
38 
39 #define DEFAULT_SECTOR_SIZE     0x200u
40 
41 #define DISKNAME_MAX	64
42 #define DISKDESCRIPTION_MAX	128
43 /* PARTITION TYPE */
44 #define P_NO_OS         0x00
45 #define P_12FAT         0x01
46 #define P_16FAT         0x04
47 #define P_EXTENDED      0x05
48 #define P_16FATBD       0x06
49 #define P_NTFS          0x07
50 #define P_HPFS          0x07
51 #define P_EXFAT		0x07
52 #define P_OS2MB         0x0A
53 #define P_32FAT         0x0B
54 #define P_32FAT_LBA     0x0C
55 #define P_16FATBD_LBA   0x0E
56 #define P_EXTENDX       0x0F
57 #define P_12FATH        0x11
58 #define P_16FATH        0x14
59 #define P_16FATBDH      0x16
60 #define P_NTFSH         0x17
61 #define P_32FATH        0x1B
62 #define P_32FAT_LBAH    0x1C
63 #define P_16FATBD_LBAH  0x1E
64 #define P_SYSV		0x63
65 #define P_NETWARE	0x65
66 #define P_OLDLINUX      0x81
67 #define P_LINSWAP       0x82
68 #define P_LINUX         0x83
69 #define P_LINUXEXTENDX  0x85
70 #define P_LVM		0x8E
71 #define P_FREEBSD       0xA5
72 #define P_OPENBSD       0xA6
73 #define P_NETBSD        0xA9
74 #define P_HFS		0xAF
75 #define P_HFSP		0xAF
76 #define P_SUN		0xBF
77 #define P_BEOS          0xEB
78 #define P_VMFS		0xFB
79 #define P_RAID		0xFD
80 #define P_UNK		255
81 #define NO_ORDER 	255
82 /* Partition SUN */
83 #define PSUN_BOOT	1
84 #define PSUN_ROOT	2
85 #define PSUN_SWAP	3
86 #define PSUN_USR	4
87 #define PSUN_WHOLE_DISK	5
88 #define PSUN_STAND	6
89 #define PSUN_VAR	7
90 #define PSUN_HOME	8
91 #define PSUN_ALT	9
92 #define PSUN_CACHEFS	10
93 #define PSUN_LINSWAP	P_LINSWAP
94 #define PSUN_LINUX	P_LINUX
95 #define PSUN_LVM	P_LVM
96 #define PSUN_RAID	P_RAID
97 #define PSUN_UNK	255
98 
99 #define PHUMAX_PARTITION 1
100 
101 #define PMAC_DRIVER43 	1
102 #define PMAC_DRIVERATA 	2
103 #define PMAC_DRIVERIO 	3
104 #define PMAC_FREE	4
105 #define PMAC_FWDRIVER	5
106 #define PMAC_SWAP	0x82
107 #define PMAC_LINUX	0x83
108 #define PMAC_HFS	0xAF
109 #define PMAC_MAP	6
110 #define PMAC_PATCHES	7
111 #define PMAC_UNK	8
112 #define PMAC_NewWorld	9
113 #define PMAC_DRIVER	10
114 #define PMAC_MFS	11
115 #define PMAC_PRODOS	12
116 #define PMAC_FAT32      13
117 
118 #define PXBOX_UNK	0
119 #define PXBOX_FATX	1
120 
121 #define	GPT_ENT_TYPE_UNUSED		\
122 	((const efi_guid_t){le32(0x00000000),le16(0x0000),le16(0x0000),0x00,0x00,{0x00,0x00,0x00,0x00,0x00,0x00}})
123 #define	GPT_ENT_TYPE_EFI		\
124 	((const efi_guid_t){le32(0xc12a7328),le16(0xf81f),le16(0x11d2),0xba,0x4b,{0x00,0xa0,0xc9,0x3e,0xc9,0x3b}})
125 /* Extended Boot Partition */
126 #define GPT_ENT_TYPE_EBP		\
127 	((const efi_guid_t){le32(0xbc13c2ff),le16(0x59e6),le16(0x4262),0xa3,0x52,{0xb2,0x75,0xfd,0x6f,0x71,0x72}})
128 #define	GPT_ENT_TYPE_MBR		\
129 	((const efi_guid_t){le32(0x024dee41),le16(0x33e7),le16(0x11d3),0x9d,0x69,{0x00,0x08,0xc7,0x81,0xf3,0x9f}})
130 #define	GPT_ENT_TYPE_FREEBSD		\
131 	((const efi_guid_t){le32(0x516e7cb4),le16(0x6ecf),le16(0x11d6),0x8f,0xf8,{0x00,0x02,0x2d,0x09,0x71,0x2b}})
132 #define	GPT_ENT_TYPE_FREEBSD_SWAP	\
133 	((const efi_guid_t){le32(0x516e7cb5),le16(0x6ecf),le16(0x11d6),0x8f,0xf8,{0x00,0x02,0x2d,0x09,0x71,0x2b}})
134 #define	GPT_ENT_TYPE_FREEBSD_UFS	\
135 	((const efi_guid_t){le32(0x516e7cb6),le16(0x6ecf),le16(0x11d6),0x8f,0xf8,{0x00,0x02,0x2d,0x09,0x71,0x2b}})
136 #define GPT_ENT_TYPE_FREEBSD_ZFS	\
137 	((const efi_guid_t){le32(0x516e7cb),le16(0x6ecf),le16(0x11d6),0x8f,0xf8,{0x00,0x02,0x2d,0x09,0x71,0x2b}})
138 /*
139  * The following is unused but documented here to avoid reuse.
140  *
141  * GPT_ENT_TYPE_FREEBSD_UFS2	\
142  *	((const efi_guid_t){le32(0x516e7cb7),le16(0x6ecf),le16(0x11d6),0x8f,0xf8,{0x00,0x02,0x2d,0x09,0x71,0x2b}})
143  */
144 
145 #define	GPT_ENT_TYPE_FREEBSD_VINUM	\
146 	((const efi_guid_t){le32(0x516e7cb8),le16(0x6ecf),le16(0x11d6),0x8f,0xf8,{0x00,0x02,0x2d,0x09,0x71,0x2b}})
147 
148 
149 #define	GPT_ENT_TYPE_MS_RESERVED	\
150 	((const efi_guid_t){le32(0xe3c9e316),le16(0x0b5c),le16(0x4db8),0x81,0x7d,{0xf9,0x2d,0xf0,0x02,0x15,0xae}})
151 #define	GPT_ENT_TYPE_MS_BASIC_DATA	\
152 	((const efi_guid_t){le32(0xebd0a0a2),le16(0xb9e5),le16(0x4433),0x87,0xc0,{0x68,0xb6,0xb7,0x26,0x99,0xc7}})
153 #define	GPT_ENT_TYPE_MS_LDM_METADATA	\
154 	((const efi_guid_t){le32(0x5808c8aa),le16(0x7e8f),le16(0x42e0),0x85,0xd2,{0xe1,0xe9,0x04,0x34,0xcf,0xb3}})
155 #define	GPT_ENT_TYPE_MS_LDM_DATA	\
156 	((const efi_guid_t){le32(0xaf9b60a0),le16(0x1431),le16(0x4f62),0xbc,0x68,{0x33,0x11,0x71,0x4a,0x69,0xad}})
157 #define	GPT_ENT_TYPE_MS_RECOVERY	\
158 	((const efi_guid_t){le32(0xde94bba4),le16(0x06d1),le16(0x4d40),0xa1,0x6a,{0xbf,0xd5,0x01,0x79,0xd6,0xac}})
159 
160 #define	GPT_ENT_TYPE_LINUX_RAID		\
161 	((const efi_guid_t){le32(0xa19d880f),le16(0x05fc),le16(0x4d3b),0xa0,0x06,{0x74,0x3f,0x0f,0x84,0x91,0x1e}})
162 #define	GPT_ENT_TYPE_LINUX_SWAP		\
163 	((const efi_guid_t){le32(0x0657fd6d),le16(0xa4ab),le16(0x43c4),0x84,0xe5,{0x09,0x33,0xc8,0x4b,0x4f,0x4f}})
164 #define	GPT_ENT_TYPE_LINUX_LVM		\
165 	((const efi_guid_t){le32(0xe6d6d379),le16(0xf507),le16(0x44c2),0xa2,0x3c,{0x23,0x8f,0x2a,0x3d,0xf9,0x28}})
166 #define GPT_ENT_TYPE_LINUX_RESERVED	\
167 	((const efi_guid_t){le32(0x8da63339),le16(0x0007),le16(0x60c0),0xc4,0x36,{0x08,0x3a,0xc8,0x23,0x09,0x08}})
168 #define GPT_ENT_TYPE_LINUX_DATA	\
169 	((const efi_guid_t){le32(0x0fc63daf),le16(0x8483),le16(0x4772),0x8e,0x79,{0x3d,0x69,0xd8,0x47,0x7d,0xe4}})
170 #define GPT_ENT_TYPE_LINUX_HOME \
171 	((const efi_guid_t){le32(0x933ac7e1),le16(0x2eb4),le16(0x4f13),0xb8,0x44,{0x0e,0x14,0xe2,0xae,0xf9,0x15}})
172 #define GPT_ENT_TYPE_LINUX_SRV	\
173 	((const efi_guid_t){le32(0x3b8f8425),le16(0x20e0),le16(0x4f3b),0x90,0x7f,{0x1a,0x25,0xa7,0x6f,0x98,0xe8}})
174 
175 #define GPT_ENT_TYPE_HPUX_DATA	\
176 	((const efi_guid_t){le32(0x75894c1e),le16(0x3aeb),le16(0x11d3),0xb7,0xc1,{0x7b,0x03,0xa0,0x00,0x00,0x00}})
177 #define GPT_ENT_TYPE_HPUX_SERVICE \
178 	((const efi_guid_t){le32(0xe2a1e728),le16(0x32e3),le16(0x11d6),0xa6,0x82,{0x7b,0x03,0xa0,0x00,0x00,0x00}})
179 
180 #define GPT_ENT_TYPE_MAC_AFS		\
181 	((const efi_guid_t){le32(0x7c3457ef),le16(0x0000),le16(0x11aa),0xaa,0x11,{0x00,0x30,0x65,0x43,0xec,0xac}})
182 #define GPT_ENT_TYPE_MAC_HFS		\
183 	((const efi_guid_t){le32(0x48465300),le16(0x0000),le16(0x11aa),0xaa,0x11,{0x00,0x30,0x65,0x43,0xec,0xac}})
184 #define GPT_ENT_TYPE_MAC_UFS		\
185 	((const efi_guid_t){le32(0x55465300),le16(0x0000),le16(0x11aa),0xaa,0x11,{0x00,0x30,0x65,0x43,0xec,0xac}})
186 #define GPT_ENT_TYPE_MAC_RAID	\
187 	((const efi_guid_t){le32(0x52414944),le16(0x0000),le16(0x11aa),0xaa,0x11,{0x00,0x30,0x65,0x43,0xec,0xac}})
188 #define GPT_ENT_TYPE_MAC_RAID_OFFLINE 	\
189 	((const efi_guid_t){le32(0x52414944),le16(0x5f4f),le16(0x11aa),0xaa,0x11,{0x00,0x30,0x65,0x43,0xec,0xac}})
190 #define GPT_ENT_TYPE_MAC_BOOT	\
191 	((const efi_guid_t){le32(0x426f6f74),le16(0x0000),le16(0x11aa),0xaa,0x11,{0x00,0x30,0x65,0x43,0xec,0xac}})
192 #define GPT_ENT_TYPE_MAC_LABEL 	\
193 	((const efi_guid_t){le32(0x4c616265),le16(0x6c00),le16(0x11aa),0xaa,0x11,{0x00,0x30,0x65,0x43,0xec,0xac}})
194 #define GPT_ENT_TYPE_MAC_TV_RECOVERY  	\
195 	((const efi_guid_t){le32(0x5265636f),le16(0x7665),le16(0x11aa),0xaa,0x11,{0x00,0x30,0x65,0x43,0xec,0xac}})
196 
197 #define GPT_ENT_TYPE_SOLARIS_BOOT  	\
198 	((const efi_guid_t){le32(0x6a82cb45),le16(0x1dd2),le16(0x11b2),0x99,0xa6,{0x08,0x00,0x20,0x73,0x66,0x31}})
199 #define GPT_ENT_TYPE_SOLARIS_ROOT  	\
200 	((const efi_guid_t){le32(0x6a85cf4d),le16(0x1dd2),le16(0x11b2),0x99,0xa6,{0x08,0x00,0x20,0x73,0x66,0x31}})
201 #define GPT_ENT_TYPE_SOLARIS_SWAP  	\
202 	((const efi_guid_t){le32(0x6a87c46f),le16(0x1dd2),le16(0x11b2),0x99,0xa6,{0x08,0x00,0x20,0x73,0x66,0x31}})
203 #define GPT_ENT_TYPE_SOLARIS_BACKUP  	\
204 	((const efi_guid_t){le32(0x6a8b642b),le16(0x1dd2),le16(0x11b2),0x99,0xa6,{0x08,0x00,0x20,0x73,0x66,0x31}})
205 #define GPT_ENT_TYPE_SOLARIS_USR  	\
206 	((const efi_guid_t){le32(0x6a898cc3),le16(0x1dd2),le16(0x11b2),0x99,0xa6,{0x08,0x00,0x20,0x73,0x66,0x31}})
207 #define GPT_ENT_TYPE_MAC_ZFS		GPT_ENT_TYPE_SOLARIS_USR
208 #define GPT_ENT_TYPE_SOLARIS_VAR  	\
209 	((const efi_guid_t){le32(0x6a8ef2e9),le16(0x1dd2),le16(0x11b2),0x99,0xa6,{0x08,0x00,0x20,0x73,0x66,0x31}})
210 #define GPT_ENT_TYPE_SOLARIS_HOME  	\
211 	((const efi_guid_t){le32(0x6a90ba39),le16(0x1dd2),le16(0x11b2),0x96,0xa6,{0x08,0x00,0x20,0x73,0x66,0x31}})
212 #define GPT_ENT_TYPE_SOLARIS_EFI_ALTSCTR \
213 	((const efi_guid_t){le32(0x6a9283a5),le16(0x1dd2),le16(0x11b2),0x96,0xa6,{0x08,0x00,0x20,0x73,0x66,0x31}})
214 #define GPT_ENT_TYPE_SOLARIS_RESERVED1	\
215 	((const efi_guid_t){le32(0x6a945a3b),le16(0x1dd2),le16(0x11b2),0x96,0xa6,{0x08,0x00,0x20,0x73,0x66,0x31}})
216 #define GPT_ENT_TYPE_SOLARIS_RESERVED2	\
217 	((const efi_guid_t){le32(0x6a9630d1),le16(0x1dd2),le16(0x11b2),0x96,0xa6,{0x08,0x00,0x20,0x73,0x66,0x31}})
218 #define GPT_ENT_TYPE_SOLARIS_RESERVED3	\
219 	((const efi_guid_t){le32(0x6a980767),le16(0x1dd2),le16(0x11b2),0x96,0xa6,{0x08,0x00,0x20,0x73,0x66,0x31}})
220 #define GPT_ENT_TYPE_SOLARIS_RESERVED4	\
221 	((const efi_guid_t){le32(0x6a96237f),le16(0x1dd2),le16(0x11b2),0x96,0xa6,{0x08,0x00,0x20,0x73,0x66,0x31}})
222 #define GPT_ENT_TYPE_SOLARIS_RESERVED5	\
223 	((const efi_guid_t){le32(0x6a8d2ac7),le16(0x1dd2),le16(0x11b2),0x96,0xa6,{0x08,0x00,0x20,0x73,0x66,0x31}})
224 
225 #define TESTDISK_O_RDONLY     00
226 #define TESTDISK_O_RDWR	      02
227 #define TESTDISK_O_DIRECT 040000
228 #define TESTDISK_O_READAHEAD_8K 04
229 #define TESTDISK_O_READAHEAD_32K 010
230 #define TESTDISK_O_ALL		020
231 
232 enum upart_type {
233   UP_UNK=0,
234   UP_BEOS,
235   UP_BTRFS,
236   UP_CRAMFS,
237   UP_EXFAT,
238   UP_EXT2,
239   UP_EXT3,
240   UP_EXT4,
241   UP_EXTENDED,
242   UP_FAT12,
243   UP_FAT16,
244   UP_FAT32,
245   UP_FATX,
246   UP_FREEBSD,
247   UP_F2FS,
248   UP_GFS2,
249   UP_HFS,
250   UP_HFSP,
251   UP_HFSX,
252   UP_HPFS,
253   UP_ISO,
254   UP_JFS,
255   UP_LINSWAP,
256   UP_LINSWAP2,
257   UP_LINSWAP_8K,
258   UP_LINSWAP2_8K,
259   UP_LINSWAP2_8KBE,
260   UP_LUKS,
261   UP_LVM,
262   UP_LVM2,
263   UP_MD,
264   UP_MD1,
265   UP_NETWARE,
266   UP_NTFS,
267   UP_OPENBSD,
268   UP_OS2MB,
269   UP_ReFS,
270   UP_RFS,
271   UP_RFS2,
272   UP_RFS3,
273   UP_RFS4,
274   UP_SUN,
275   UP_SYSV4,
276   UP_UFS,
277   UP_UFS2,
278   UP_UFS_LE,
279   UP_UFS2_LE,
280   UP_VMFS,
281   UP_WBFS,
282   UP_XFS,
283   UP_XFS2,
284   UP_XFS3,
285   UP_XFS4,
286   UP_XFS5,
287   UP_ZFS};
288 typedef enum upart_type upart_type_t;
289 enum status_type { STATUS_DELETED, STATUS_PRIM, STATUS_PRIM_BOOT, STATUS_LOG, STATUS_EXT, STATUS_EXT_IN_EXT};
290 typedef enum status_type status_type_t;
291 enum errcode_type {BAD_NOERR, BAD_SS, BAD_ES, BAD_SH, BAD_EH, BAD_EBS, BAD_RS, BAD_SC, BAD_EC, BAD_SCOUNT};
292 typedef enum errcode_type errcode_type_t;
293 
294 #define AFF_PART_BASE	0
295 #define AFF_PART_ORDER	1
296 #define AFF_PART_STATUS	2
297 
298 #define UNIT_DEFAULT	0
299 #define UNIT_SECTOR	1
300 #define UNIT_CHS	2
301 
302 typedef struct param_disk_struct disk_t;
303 typedef struct partition_struct partition_t;
304 typedef struct CHS_struct CHS_t;
305 typedef struct
306 {
307   unsigned long int cylinders;
308   unsigned int heads_per_cylinder;
309   unsigned int sectors_per_head;
310   unsigned int bytes_per_sector;	/* WARN: may be uninitialized */
311 } CHSgeometry_t;
312 
313 struct CHS_struct
314 {
315   unsigned long int cylinder;
316   unsigned int head;
317   unsigned int sector;
318 };
319 
320 typedef struct list_part_struct list_part_t;
321 struct list_part_struct
322 {
323   partition_t *part;
324   list_part_t *prev;
325   list_part_t *next;
326   int to_be_removed;
327 };
328 
329 typedef struct list_disk_struct list_disk_t;
330 struct list_disk_struct
331 {
332   disk_t *disk;
333   list_disk_t *prev;
334   list_disk_t *next;
335 };
336 
337 struct systypes {
338   const unsigned int part_type;
339   const char *name;
340 };
341 
342 struct arch_fnct_struct
343 {
344   const char *part_name;
345   const char *part_name_option;
346   const char *msg_part_type;
347   list_part_t *(*read_part)(disk_t *disk, const int verbose,const int saveheader);
348   int (*write_part)(disk_t *disk, const list_part_t *list_part, const int ro, const int verbose);
349   list_part_t *(*init_part_order)(const disk_t *disk, list_part_t *list_part);
350   /* geometry must be initialized to 0,0,0 in get_geometry_from_mbr()*/
351   int (*get_geometry_from_mbr)(const unsigned char *buffer, const int verbose, CHSgeometry_t *geometry);
352   int (*check_part)(disk_t *disk,const int verbose,partition_t *partition, const int saveheader);
353   int (*write_MBR_code)(disk_t *disk);
354   void (*set_prev_status)(const disk_t *disk, partition_t *partition);
355   void (*set_next_status)(const disk_t *disk, partition_t *partition);
356   int (*test_structure)(list_part_t *list_part);
357   unsigned int (*get_part_type)(const partition_t *partition);
358   int (*set_part_type)(partition_t *partition, unsigned int part_type);
359   void (*init_structure)(const disk_t *disk,list_part_t *list_part, const int verbose);
360   int (*erase_list_part)(disk_t *disk);
361   const char *(*get_partition_typename)(const partition_t *partition);
362   int (*is_part_known)(const partition_t *partition);
363 };
364 
365 typedef struct arch_fnct_struct arch_fnct_t;
366 
367 struct param_disk_struct
368 {
369   char description_txt[DISKDESCRIPTION_MAX];
370   char description_short_txt[DISKDESCRIPTION_MAX];
371   CHSgeometry_t	geom;	/* logical CHS */
372   uint64_t disk_size;
373   char *device;
374   char *model;
375   char *serial_no;
376   char *fw_rev;
377   const char *(*description)(disk_t *disk);
378   const char *(*description_short)(disk_t *disk);
379   int (*pread)(disk_t *disk, void *buf, const unsigned int count, const uint64_t offset);
380   int (*pwrite)(disk_t *disk, const void *buf, const unsigned int count, const uint64_t offset);
381   int (*sync)(disk_t *disk);
382   void (*clean)(disk_t *disk);
383   const arch_fnct_t *arch;
384   const arch_fnct_t *arch_autodetected;
385   void *data;
386   uint64_t disk_real_size;
387   uint64_t user_max;
388   uint64_t native_max;
389   uint64_t dco;
390   uint64_t offset;      /* offset to first sector, may be modified in the futur to handle broken raid */
391   void *rbuffer;
392   void *wbuffer;
393   unsigned int rbuffer_size;
394   unsigned int wbuffer_size;
395   int write_used;
396   int autodetect;
397   int access_mode;
398   int unit;
399   unsigned int sector_size;
400 };
401 
402 struct partition_struct
403 {
404   char          fsname[128];
405   char          partname[128];
406   char          info[128];
407   uint64_t      part_offset;
408   uint64_t      part_size;
409   uint64_t      sborg_offset;
410   uint64_t      sb_offset;
411   unsigned int  sb_size;
412   unsigned int  blocksize;
413   efi_guid_t    part_uuid;
414   efi_guid_t    part_type_gpt;
415   unsigned int  part_type_humax;
416   unsigned int  part_type_i386;
417   unsigned int  part_type_mac;
418   unsigned int  part_type_sun;
419   unsigned int  part_type_xbox;
420   upart_type_t  upart_type;
421   status_type_t status;
422   unsigned int  order;
423   errcode_type_t errcode;
424   const arch_fnct_t *arch;
425   /* NTFS => utils_cluster_in_use */
426   /* ext2/ext3/ext4 */
427 #if 0
428   int (*is_allocated)(disk_t *disk, const partition_t *partition, const uint64_t offset);
429   void *free_is_allocated(void);
430 #endif
431 };
432 
433 typedef struct my_data_struct my_data_t;
434 struct my_data_struct
435 {
436   disk_t *disk_car;
437   const partition_t *partition;
438   uint64_t offset;
439 };
440 
441 void *MALLOC(size_t size);
442 unsigned int up2power(const unsigned int number);
443 void set_part_name(partition_t *partition, const char *src, const unsigned int max_size);
444 void set_part_name_chomp(partition_t *partition, const unsigned char *src, const unsigned int max_size);
445 char* strip_dup(char* str);
446 int date_dos2unix(const unsigned short f_time,const unsigned short f_date);
447 void set_secwest(void);
448 time_t td_ntfs2utc (int64_t ntfstime);
449 #ifndef BSD_MAXPARTITIONS
450 #define	BSD_MAXPARTITIONS	8
451 #endif
452 #ifndef OPENBSD_MAXPARTITIONS
453 #define	OPENBSD_MAXPARTITIONS	16
454 #endif
455 
456 #ifdef TESTDISK_LSB
457 #define le16(x)  (x)             /* x as little endian */
458 #define be16(x)  ((((x)&0xff00)>>8)                      | \
459     (((x)&0x00ff)<<8))
460 #define le24(x)  (x)
461 #define le32(x)  (x)
462 #define be32(x)  ((((x)&0xff000000UL)>>24)                | \
463     (((x)&0x00ff0000UL)>>8)                  | \
464     (((x)&0x0000ff00UL)<<8)                  | \
465     (((x)&0x000000ffUL)<<24))
466 #define le64(x)  (x)
467 #define be64(x)  ((((x)&0xff00000000000000ULL)>>56)       | \
468     (((x)&0x00ff000000000000ULL)>>40)        | \
469     (((x)&0x0000ff0000000000ULL)>>24)        | \
470     (((x)&0x000000ff00000000ULL)>>8)         | \
471     (((x)&0x00000000ff000000ULL)<<8)         | \
472     (((x)&0x0000000000ff0000ULL)<<24)        | \
473     (((x)&0x000000000000ff00ULL)<<40)        | \
474     (((x)&0x00000000000000ffULL)<<56))
475 #else /* bigendian */
476 #define le16(x)  ((((x)&0xff00)>>8)                      | \
477     (((x)&0x00ff)<<8))
478 #define be16(x)  (x)
479 #define be24(x)  (x)
480 #define le24(x) ((((x) & 0x000000ffUL) << 16) | \
481     ((x) & 0x0000ff00UL)        | \
482     (((x) & 0x00ff0000UL) >> 16))
483 #define le32(x)  ((((x)&0xff000000UL)>>24)                | \
484     (((x)&0x00ff0000UL)>>8)                  | \
485     (((x)&0x0000ff00UL)<<8)                  | \
486     (((x)&0x000000ffUL)<<24))
487 #define be32(x)  (x)
488 #define le64(x)  ((((x)&0xff00000000000000ULL)>>56)       | \
489     (((x)&0x00ff000000000000ULL)>>40)        | \
490     (((x)&0x0000ff0000000000ULL)>>24)        | \
491     (((x)&0x000000ff00000000ULL)>>8)         | \
492     (((x)&0x00000000ff000000ULL)<<8)         | \
493     (((x)&0x0000000000ff0000ULL)<<24)        | \
494     (((x)&0x000000000000ff00ULL)<<40)        | \
495     (((x)&0x00000000000000ffULL)<<56))
496 #define be64(x)  (x)
497 #endif
498 #ifndef HAVE_SNPRINTF
499 int snprintf(char *str, size_t size, const char *format, ...);
500 #endif
501 #ifndef HAVE_VSNPRINTF
502 #include <stdarg.h>
503 int vsnprintf(char *str, size_t size, const char *format, va_list ap);
504 #endif
505 #ifndef HAVE_STRNCASECMP
506 int strncasecmp(const char * s1, const char * s2, size_t len);
507 #endif
508 #ifndef HAVE_STRCASESTR
509 char * strcasestr (const char *haystack, const char *needle);
510 #endif
511 #if ! defined(HAVE_LOCALTIME_R) && ! defined(__MINGW32__)
512 struct tm *localtime_r(const time_t *timep, struct tm *result);
513 #endif
514 /*
515  * td_min()/td_max() macros that also do
516  * strict type-checking.. See the
517  * "unnecessary" pointer comparison.
518  * Comes from Linux kernel
519  */
520 #define td_min(x,y) ({ \
521         typeof(x) _x = (x);     \
522         typeof(y) _y = (y);     \
523         (void) (&_x == &_y);            \
524         _x < _y ? _x : _y; })
525 
526 #define td_max(x,y) ({ \
527         typeof(x) _x = (x);     \
528         typeof(y) _y = (y);     \
529         (void) (&_x == &_y);            \
530         _x > _y ? _x : _y; })
531 
532 int check_command(char **current_cmd, const char *cmd, size_t n);
533 void skip_comma_in_command(char **current_cmd);
534 uint64_t get_int_from_command(char **current_cmd);
535 #ifdef __cplusplus
536 } /* closing brace for extern "C" */
537 #endif
538 #endif
539