3 exFAT file system implementation library.
5 Copyright (C) 2009, 2010 Andrew Nayenko
7 This program 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 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #define _XOPEN_SOURCE /* for timezone in Linux */
26 void exfat_stat(const struct exfat* ef, const struct exfat_node* node,
29 memset(stbuf, 0, sizeof(struct stat));
30 if (node->flags & EXFAT_ATTRIB_DIR)
31 stbuf->st_mode = S_IFDIR | (0777 & ~ef->dmask);
33 stbuf->st_mode = S_IFREG | (0777 & ~ef->fmask);
35 stbuf->st_uid = ef->uid;
36 stbuf->st_gid = ef->gid;
37 stbuf->st_size = node->size;
38 stbuf->st_blocks = DIV_ROUND_UP(node->size, CLUSTER_SIZE(*ef->sb)) *
39 CLUSTER_SIZE(*ef->sb) / 512;
40 stbuf->st_mtime = node->mtime;
41 stbuf->st_atime = node->atime;
42 stbuf->st_ctime = 0; /* unapplicable */
45 #define SEC_IN_MIN 60ll
46 #define SEC_IN_HOUR (60 * SEC_IN_MIN)
47 #define SEC_IN_DAY (24 * SEC_IN_HOUR)
48 #define SEC_IN_YEAR (365 * SEC_IN_DAY) /* not leap year */
49 /* Unix epoch started at 0:00:00 UTC 1 January 1970 */
50 #define UNIX_EPOCH_YEAR 1970
51 /* exFAT epoch started at 0:00:00 UTC 1 January 1980 */
52 #define EXFAT_EPOCH_YEAR 1980
53 /* number of years from Unix epoch to exFAT epoch */
54 #define EPOCH_DIFF_YEAR (EXFAT_EPOCH_YEAR - UNIX_EPOCH_YEAR)
55 /* number of days from Unix epoch to exFAT epoch (considering leap days) */
56 #define EPOCH_DIFF_DAYS (EPOCH_DIFF_YEAR * 365 + EPOCH_DIFF_YEAR / 4)
57 /* number of seconds from Unix epoch to exFAT epoch (considering leap days) */
58 #define EPOCH_DIFF_SEC (EPOCH_DIFF_DAYS * SEC_IN_DAY)
59 /* number of leap years passed from exFAT epoch to the specified year
60 (excluding the specified year itself) */
61 #define LEAP_YEARS(year) ((EXFAT_EPOCH_YEAR + (year) - 1) / 4 \
62 - (EXFAT_EPOCH_YEAR - 1) / 4)
63 /* checks whether the specified year is leap */
64 #define IS_LEAP_YEAR(year) ((EXFAT_EPOCH_YEAR + (year)) % 4 == 0)
66 static const time_t days_in_year[] =
68 /* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */
69 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
72 time_t exfat_exfat2unix(le16_t date, le16_t time, uint8_t centisec)
74 time_t unix_time = EPOCH_DIFF_SEC;
75 uint16_t ndate = le16_to_cpu(date);
76 uint16_t ntime = le16_to_cpu(time);
78 uint16_t day = ndate & 0x1f; /* 5 bits, 1-31 */
79 uint16_t month = ndate >> 5 & 0xf; /* 4 bits, 1-12 */
80 uint16_t year = ndate >> 9; /* 7 bits, 1-127 (+1980) */
82 uint16_t twosec = ntime & 0x1f; /* 5 bits, 0-29 (2 sec granularity) */
83 uint16_t min = ntime >> 5 & 0xf; /* 6 bits, 0-59 */
84 uint16_t hour = ntime >> 11; /* 5 bits, 0-23 */
86 if (day == 0 || month == 0 || month > 12)
88 exfat_error("bad date %hu-%02hu-%02hu",
89 year + EXFAT_EPOCH_YEAR, month, day);
92 if (hour > 23 || min > 59 || twosec > 29)
94 exfat_error("bad time %hu:%02hu:%02hu",
95 hour, min, twosec * 2);
100 exfat_error("bad centiseconds count %hhu", centisec);
104 /* every 4th year between 1904 and 2096 is leap */
105 unix_time += year * SEC_IN_YEAR + LEAP_YEARS(year) * SEC_IN_DAY;
106 unix_time += days_in_year[month] * SEC_IN_DAY;
107 /* if it's leap year and February has passed we should add 1 day */
108 if ((EXFAT_EPOCH_YEAR + year) % 4 == 0 && month > 2)
109 unix_time += SEC_IN_DAY;
110 unix_time += (day - 1) * SEC_IN_DAY;
112 unix_time += hour * SEC_IN_HOUR;
113 unix_time += min * SEC_IN_MIN;
114 /* exFAT represents time with 2 sec granularity */
115 unix_time += twosec * 2;
116 unix_time += centisec / 100;
118 /* exFAT stores timestamps in local time, so we correct it to UTC */
119 unix_time += timezone;
124 void exfat_unix2exfat(time_t unix_time, le16_t* date, le16_t* time,
127 time_t shift = EPOCH_DIFF_SEC + timezone;
128 uint16_t day, month, year;
129 uint16_t twosec, min, hour;
133 /* time before exFAT epoch cannot be represented */
134 if (unix_time < shift)
139 days = unix_time / SEC_IN_DAY;
140 year = (4 * days) / (4 * 365 + 1);
141 days -= year * 365 + LEAP_YEARS(year);
143 for (i = 1; i <= 12; i++)
145 int leap_day = (IS_LEAP_YEAR(year) && i == 2);
146 int leap_sub = (IS_LEAP_YEAR(year) && i >= 3);
148 if (i == 12 || days - leap_sub < days_in_year[i + 1] + leap_day)
151 days -= days_in_year[i] + leap_sub;
157 hour = (unix_time % SEC_IN_DAY) / SEC_IN_HOUR;
158 min = (unix_time % SEC_IN_HOUR) / SEC_IN_MIN;
159 twosec = (unix_time % SEC_IN_MIN) / 2;
161 *date = cpu_to_le16(day | (month << 5) | (year << 9));
162 *time = cpu_to_le16(twosec | (min << 5) | (hour << 11));
164 *centisec = (unix_time % 2) * 100;
167 void exfat_get_name(const struct exfat_node* node, char* buffer, size_t n)
169 if (utf16_to_utf8(buffer, node->name, n, EXFAT_NAME_MAX) != 0)
170 exfat_bug("failed to convert name to UTF-8");
173 uint16_t exfat_start_checksum(const struct exfat_entry_meta1* entry)
178 for (i = 0; i < sizeof(struct exfat_entry); i++)
179 if (i != 2 && i != 3) /* skip checksum field itself */
180 sum = ((sum << 15) | (sum >> 1)) + ((const uint8_t*) entry)[i];
184 uint16_t exfat_add_checksum(const void* entry, uint16_t sum)
188 for (i = 0; i < sizeof(struct exfat_entry); i++)
189 sum = ((sum << 15) | (sum >> 1)) + ((const uint8_t*) entry)[i];
193 le16_t exfat_calc_checksum(const struct exfat_entry_meta1* meta1,
194 const struct exfat_entry_meta2* meta2, const le16_t* name)
197 const int name_entries = DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX);
200 checksum = exfat_start_checksum(meta1);
201 checksum = exfat_add_checksum(meta2, checksum);
202 for (i = 0; i < name_entries; i++)
204 struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
205 memcpy(name_entry.name, name + i * EXFAT_ENAME_MAX,
206 EXFAT_ENAME_MAX * sizeof(le16_t));
207 checksum = exfat_add_checksum(&name_entry, checksum);
209 return cpu_to_le16(checksum);
212 le16_t exfat_calc_name_hash(const struct exfat* ef, const le16_t* name)
215 size_t length = utf16_length(name);
218 for (i = 0; i < length; i++)
220 uint16_t c = le16_to_cpu(name[i]);
222 /* convert to upper case */
223 if (c < ef->upcase_chars)
224 c = le16_to_cpu(ef->upcase[c]);
226 hash = ((hash << 15) | (hash >> 1)) + (c & 0xff);
227 hash = ((hash << 15) | (hash >> 1)) + (c >> 8);
229 return cpu_to_le16(hash);
232 void exfat_humanize_bytes(uint64_t value, struct exfat_human_bytes* hb)
235 const char* units[] = {"bytes", "KB", "MB", "GB", "TB", "PB"};
236 uint64_t divisor = 1;
238 for (i = 0; i < sizeof(units) / sizeof(units[0]) - 1; i++)
240 if ((value + divisor / 2) / divisor < 1024)
244 hb->value = (value + divisor / 2) / divisor;