]> git.sven.stormbind.net Git - sven/exfatprogs.git/blob - include/libexfat.h
New upstream version 1.0.3
[sven/exfatprogs.git] / include / libexfat.h
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  *  Copyright (C) 2019 Namjae Jeon <linkinjeon@kernel.org>
4  */
5
6 #ifndef _LIBEXFAT_H
7
8 #include <stdbool.h>
9 #include <sys/types.h>
10 #include <wchar.h>
11 #include <limits.h>
12
13 #define KB                      (1024)
14 #define MB                      (1024*1024)
15 #define GB                      (1024UL*1024UL*1024UL)
16
17 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
18 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
19 #define round_down(x, y) ((x) & ~__round_mask(x, y))
20
21 #define MIN(a, b)       ((a) < (b) ? (a) : (b))
22 #define MAX(a, b)       ((a) > (b) ? (a) : (b))
23
24 #define DIV_ROUND_UP(__i, __d)  (((__i) + (__d) - 1) / (__d))
25
26 #define EXFAT_MIN_NUM_SEC_VOL           (2048)
27 #define EXFAT_MAX_NUM_SEC_VOL           ((2 << 64) - 1)
28
29 #define EXFAT_MAX_NUM_CLUSTER           (0xFFFFFFF5)
30
31 #define DEFAULT_SECTOR_SIZE     (512)
32
33 #define VOLUME_LABEL_BUFFER_SIZE        (VOLUME_LABEL_MAX_LEN*MB_LEN_MAX+1)
34
35 /* Upcase tabel macro */
36 #define EXFAT_UPCASE_TABLE_SIZE         (5836)
37
38 enum {
39         BOOT_SEC_IDX = 0,
40         EXBOOT_SEC_IDX,
41         EXBOOT_SEC_NUM = 8,
42         OEM_SEC_IDX,
43         RESERVED_SEC_IDX,
44         CHECKSUM_SEC_IDX,
45         BACKUP_BOOT_SEC_IDX,
46 };
47
48 struct exfat_blk_dev {
49         int dev_fd;
50         unsigned long long size;
51         unsigned int sector_size;
52         unsigned int sector_size_bits;
53         unsigned long long num_sectors;
54         unsigned int num_clusters;
55 };
56
57 struct exfat_user_input {
58         char dev_name[255];
59         bool writeable;
60         unsigned int cluster_size;
61         unsigned int sec_per_clu;
62         bool quick;
63         __u16 volume_label[VOLUME_LABEL_MAX_LEN];
64         int volume_label_len;
65 };
66
67 void show_version(void);
68
69 void exfat_set_bit(struct exfat_blk_dev *bd, char *bitmap,
70                 unsigned int clu);
71 void exfat_clear_bit(struct exfat_blk_dev *bd, char *bitmap,
72                 unsigned int clu);
73 wchar_t exfat_bad_char(wchar_t w);
74 void boot_calc_checksum(unsigned char *sector, unsigned short size,
75                 bool is_boot_sec, __le32 *checksum);
76 void init_user_input(struct exfat_user_input *ui);
77 int exfat_get_blk_dev_info(struct exfat_user_input *ui,
78                 struct exfat_blk_dev *bd);
79 ssize_t exfat_read(int fd, void *buf, size_t size, off_t offset);
80 ssize_t exfat_write(int fd, void *buf, size_t size, off_t offset);
81
82 ssize_t exfat_utf16_enc(const char *in_str, __u16 *out_str, size_t out_size);
83 ssize_t exfat_utf16_dec(const __u16 *in_str, size_t in_len,
84                         char *out_str, size_t out_size);
85
86 /*
87  * Exfat Print
88  */
89
90 extern unsigned int print_level;
91
92 #define EXFAT_ERROR     (1)
93 #define EXFAT_INFO      (2)
94 #define EXFAT_DEBUG     (3)
95
96 #define exfat_msg(level, fmt, ...)                                              \
97         do {                                                                    \
98                 if (print_level >= level) {                                     \
99                         if (print_level == EXFAT_INFO)                          \
100                                 printf(fmt, ##__VA_ARGS__);             \
101                         else                                                    \
102                                 printf("[%s:%4d] " fmt,                         \
103                                         __func__, __LINE__, ##__VA_ARGS__);     \
104                 }                                                               \
105         } while (0)                                                             \
106
107 #define exfat_err(fmt, ...)     exfat_msg(EXFAT_ERROR, fmt, ##__VA_ARGS__)
108 #define exfat_info(fmt, ...)    exfat_msg(EXFAT_INFO, fmt, ##__VA_ARGS__)
109 #define exfat_debug(fmt, ...)   exfat_msg(EXFAT_DEBUG, fmt, ##__VA_ARGS__)
110
111 #endif /* !_LIBEXFAT_H */