3 * exFAT file system implementation library.
5 * Created by Andrew Nayenko on 04.09.09.
6 * This software is distributed under the GNU General Public License
7 * version 3 or any later.
12 #define _XOPEN_SOURCE /* for timezone in Linux */
15 void exfat_stat(const struct exfat_node* node, struct stat *stbuf)
17 memset(stbuf, 0, sizeof(struct stat));
18 if (node->flags & EXFAT_ATTRIB_DIR)
19 stbuf->st_mode = S_IFDIR | 0755;
21 stbuf->st_mode = S_IFREG | 0644;
23 stbuf->st_size = node->size;
24 stbuf->st_mtime = node->mtime;
25 stbuf->st_atime = node->atime;
26 stbuf->st_ctime = 0; /* unapplicable */
29 #define SEC_IN_MIN 60ll
30 #define SEC_IN_HOUR (60 * SEC_IN_MIN)
31 #define SEC_IN_DAY (24 * SEC_IN_HOUR)
32 #define SEC_IN_YEAR (365 * SEC_IN_DAY) /* not leap year */
33 /* Unix epoch started at 0:00:00 UTC 1 January 1970 */
34 #define UNIX_EPOCH_YEAR 1970
35 /* exFAT epoch started at 0:00:00 UTC 1 January 1980 */
36 #define EXFAT_EPOCH_YEAR 1980
37 /* number of years from Unix epoch to exFAT epoch */
38 #define EPOCH_DIFF_YEAR (EXFAT_EPOCH_YEAR - UNIX_EPOCH_YEAR)
39 /* number of days from Unix epoch to exFAT epoch (considering leap days) */
40 #define EPOCH_DIFF_DAYS (EPOCH_DIFF_YEAR * 365 + EPOCH_DIFF_YEAR / 4)
41 /* number of seconds from Unix epoch to exFAT epoch (considering leap days) */
42 #define EPOCH_DIFF_SEC (EPOCH_DIFF_DAYS * SEC_IN_DAY)
43 /* number of leap years passed from exFAT epoch to the specified year
44 (excluding the specified year itself) */
45 #define LEAP_YEARS(year) ((EXFAT_EPOCH_YEAR + (year) - 1) / 4 \
46 - (EXFAT_EPOCH_YEAR - 1) / 4)
47 /* checks whether the specified year is leap */
48 #define IS_LEAP_YEAR(year) ((EXFAT_EPOCH_YEAR + (year)) % 4 == 0)
50 static const time_t days_in_year[] =
52 /* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */
53 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
61 uint16_t day : 5; /* 1-31 */
62 uint16_t month : 4; /* 1-12 */
63 uint16_t year : 7; /* 1-127 (+1980) */
72 uint16_t twosec : 5; /* 0-29 (2 sec granularity) */
73 uint16_t min : 6; /* 0-59 */
74 uint16_t hour : 5; /* 0-23 */
78 time_t exfat_exfat2unix(le16_t date, le16_t time)
80 union exfat_date edate;
81 union exfat_time etime;
82 time_t unix_time = EPOCH_DIFF_SEC;
84 edate.raw = le16_to_cpu(date);
85 etime.raw = le16_to_cpu(time);
87 if (edate.day == 0 || edate.month == 0 || edate.month > 12)
89 exfat_error("bad date %hu-%02hu-%02hu",
90 edate.year + EXFAT_EPOCH_YEAR, edate.month, edate.day);
93 if (etime.hour > 23 || etime.min > 59 || etime.twosec > 29)
95 exfat_error("bad time %hu:%02hu:%02hu",
96 etime.hour, etime.min, etime.twosec * 2);
100 /* every 4th year between 1904 and 2096 is leap */
101 unix_time += edate.year * SEC_IN_YEAR + LEAP_YEARS(edate.year) * SEC_IN_DAY;
102 unix_time += days_in_year[edate.month] * SEC_IN_DAY;
103 /* if it's leap year and February has passed we should add 1 day */
104 if ((EXFAT_EPOCH_YEAR + edate.year) % 4 == 0 && edate.month > 2)
105 unix_time += SEC_IN_DAY;
106 unix_time += (edate.day - 1) * SEC_IN_DAY;
108 unix_time += etime.hour * SEC_IN_HOUR;
109 unix_time += etime.min * SEC_IN_MIN;
110 /* exFAT represents time with 2 sec granularity */
111 unix_time += etime.twosec * 2;
113 /* exFAT stores timestamps in local time, so we correct it to UTC */
114 unix_time += timezone;
119 void exfat_unix2exfat(time_t unix_time, le16_t* date, le16_t* time)
121 union exfat_date edate;
122 union exfat_time etime;
123 time_t shift = EPOCH_DIFF_SEC + timezone;
127 /* time before exFAT epoch cannot be represented */
128 if (unix_time < shift)
133 days = unix_time / SEC_IN_DAY;
134 edate.year = (4 * days) / (4 * 365 + 1);
135 days -= edate.year * 365 + LEAP_YEARS(edate.year);
136 for (i = 1; i <= 12; i++)
138 int leap_day = (IS_LEAP_YEAR(edate.year) && i == 2);
139 int leap_sub = (IS_LEAP_YEAR(edate.year) && i >= 3);
141 if (i == 12 || days - leap_sub < days_in_year[i + 1] + leap_day)
144 days -= days_in_year[i] + leap_sub;
148 edate.day = days + 1;
150 etime.hour = (unix_time % SEC_IN_DAY) / SEC_IN_HOUR;
151 etime.min = (unix_time % SEC_IN_HOUR) / SEC_IN_MIN;
152 etime.twosec = (unix_time % SEC_IN_MIN) / 2;
154 *date = cpu_to_le16(edate.raw);
155 *time = cpu_to_le16(etime.raw);
158 void exfat_get_name(const struct exfat_node* node, char* buffer, size_t n)
160 if (utf16_to_utf8(buffer, node->name, n, EXFAT_NAME_MAX) != 0)
161 exfat_bug("failed to convert name to UTF-8");
164 uint16_t exfat_start_checksum(const struct exfat_file* entry)
169 for (i = 0; i < sizeof(struct exfat_entry); i++)
170 if (i != 2 && i != 3) /* skip checksum field itself */
171 sum = ((sum << 15) | (sum >> 1)) + ((const uint8_t*) entry)[i];
175 uint16_t exfat_add_checksum(const void* entry, uint16_t sum)
179 for (i = 0; i < sizeof(struct exfat_entry); i++)
180 sum = ((sum << 15) | (sum >> 1)) + ((const uint8_t*) entry)[i];
184 le16_t exfat_calc_checksum(const struct exfat_file* meta1,
185 const struct exfat_file_info* meta2, const le16_t* name)
188 const int name_entries = DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX);
191 checksum = exfat_start_checksum(meta1);
192 checksum = exfat_add_checksum(meta2, checksum);
193 for (i = 0; i < name_entries; i++)
195 struct exfat_file_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
196 memcpy(name_entry.name, name + i * EXFAT_ENAME_MAX,
197 EXFAT_ENAME_MAX * sizeof(le16_t));
198 checksum = exfat_add_checksum(&name_entry, checksum);
200 return cpu_to_le16(checksum);
203 le16_t exfat_calc_name_hash(const struct exfat* ef, const le16_t* name)
206 size_t length = utf16_length(name);
209 for (i = 0; i < length; i++)
211 uint16_t c = le16_to_cpu(name[i]);
213 /* convert to upper case */
214 if (c < ef->upcase_chars)
215 c = le16_to_cpu(ef->upcase[c]);
217 hash = ((hash << 15) | (hash >> 1)) + (c & 0xff);
218 hash = ((hash << 15) | (hash >> 1)) + (c >> 8);
220 return cpu_to_le16(hash);