]> git.sven.stormbind.net Git - sven/exfatprogs.git/blob - include/libexfat.h
6a3ecb8a93ce765f8054a9b72c8c4ebdd29749c0
[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_BOUNDARY_ALIGNMENT      (1024*1024)
32
33 #define DEFAULT_SECTOR_SIZE     (512)
34
35 #define VOLUME_LABEL_BUFFER_SIZE        (VOLUME_LABEL_MAX_LEN*MB_LEN_MAX+1)
36
37 /* Upcase tabel macro */
38 #define EXFAT_UPCASE_TABLE_SIZE         (5836)
39
40 enum {
41         BOOT_SEC_IDX = 0,
42         EXBOOT_SEC_IDX,
43         EXBOOT_SEC_NUM = 8,
44         OEM_SEC_IDX,
45         RESERVED_SEC_IDX,
46         CHECKSUM_SEC_IDX,
47         BACKUP_BOOT_SEC_IDX,
48 };
49
50 struct exfat_blk_dev {
51         int dev_fd;
52         unsigned long long size;
53         unsigned int sector_size;
54         unsigned int sector_size_bits;
55         unsigned long long num_sectors;
56         unsigned int num_clusters;
57 };
58
59 struct exfat_user_input {
60         char dev_name[255];
61         bool writeable;
62         unsigned int cluster_size;
63         unsigned int sec_per_clu;
64         unsigned int boundary_align;
65         bool quick;
66         __u16 volume_label[VOLUME_LABEL_MAX_LEN];
67         int volume_label_len;
68 };
69
70 void show_version(void);
71
72 void exfat_set_bit(struct exfat_blk_dev *bd, char *bitmap,
73                 unsigned int clu);
74 void exfat_clear_bit(struct exfat_blk_dev *bd, char *bitmap,
75                 unsigned int clu);
76 wchar_t exfat_bad_char(wchar_t w);
77 void boot_calc_checksum(unsigned char *sector, unsigned short size,
78                 bool is_boot_sec, __le32 *checksum);
79 void init_user_input(struct exfat_user_input *ui);
80 int exfat_get_blk_dev_info(struct exfat_user_input *ui,
81                 struct exfat_blk_dev *bd);
82 ssize_t exfat_read(int fd, void *buf, size_t size, off_t offset);
83 ssize_t exfat_write(int fd, void *buf, size_t size, off_t offset);
84
85 size_t exfat_utf16_len(const __le16 *str, size_t max_size);
86 ssize_t exfat_utf16_enc(const char *in_str, __u16 *out_str, size_t out_size);
87 ssize_t exfat_utf16_dec(const __u16 *in_str, size_t in_len,
88                         char *out_str, size_t out_size);
89
90 /*
91  * Exfat Print
92  */
93
94 extern unsigned int print_level;
95
96 #define EXFAT_ERROR     (1)
97 #define EXFAT_INFO      (2)
98 #define EXFAT_DEBUG     (3)
99
100 #define exfat_msg(level, dir, fmt, ...)                                 \
101         do {                                                            \
102                 if (print_level >= level) {                             \
103                         fprintf(dir, fmt, ##__VA_ARGS__);               \
104                 }                                                       \
105         } while (0)                                                     \
106
107 #define exfat_err(fmt, ...)     exfat_msg(EXFAT_ERROR, stderr,          \
108                                         fmt, ##__VA_ARGS__)
109 #define exfat_info(fmt, ...)    exfat_msg(EXFAT_INFO, stdout,           \
110                                         fmt, ##__VA_ARGS__)
111 #define exfat_debug(fmt, ...)   exfat_msg(EXFAT_DEBUG, stdout,          \
112                                         "[%s:%4d] " fmt, __func__,      \
113                                         __LINE__, ##__VA_ARGS__)
114
115 #endif /* !_LIBEXFAT_H */