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
77 uint16_t day : 5; /* 1-31 */
78 uint16_t month : 4; /* 1-12 */
79 uint16_t year : 7; /* 1-127 (+1980) */
82 __attribute__((__packed__));
89 uint16_t twosec : 5; /* 0-29 (2 sec granularity) */
90 uint16_t min : 6; /* 0-59 */
91 uint16_t hour : 5; /* 0-23 */
94 __attribute__((__packed__));
96 time_t exfat_exfat2unix(le16_t date, le16_t time)
98 union exfat_date edate;
99 union exfat_time etime;
100 time_t unix_time = EPOCH_DIFF_SEC;
102 edate.raw = le16_to_cpu(date);
103 etime.raw = le16_to_cpu(time);
105 if (edate.day == 0 || edate.month == 0 || edate.month > 12)
107 exfat_error("bad date %hu-%02hu-%02hu",
108 edate.year + EXFAT_EPOCH_YEAR, edate.month, edate.day);
111 if (etime.hour > 23 || etime.min > 59 || etime.twosec > 29)
113 exfat_error("bad time %hu:%02hu:%02hu",
114 etime.hour, etime.min, etime.twosec * 2);
118 /* every 4th year between 1904 and 2096 is leap */
119 unix_time += edate.year * SEC_IN_YEAR + LEAP_YEARS(edate.year) * SEC_IN_DAY;
120 unix_time += days_in_year[edate.month] * SEC_IN_DAY;
121 /* if it's leap year and February has passed we should add 1 day */
122 if ((EXFAT_EPOCH_YEAR + edate.year) % 4 == 0 && edate.month > 2)
123 unix_time += SEC_IN_DAY;
124 unix_time += (edate.day - 1) * SEC_IN_DAY;
126 unix_time += etime.hour * SEC_IN_HOUR;
127 unix_time += etime.min * SEC_IN_MIN;
128 /* exFAT represents time with 2 sec granularity */
129 unix_time += etime.twosec * 2;
131 /* exFAT stores timestamps in local time, so we correct it to UTC */
132 unix_time += timezone;
137 void exfat_unix2exfat(time_t unix_time, le16_t* date, le16_t* time)
139 union exfat_date edate;
140 union exfat_time etime;
141 time_t shift = EPOCH_DIFF_SEC + timezone;
145 /* time before exFAT epoch cannot be represented */
146 if (unix_time < shift)
151 days = unix_time / SEC_IN_DAY;
152 edate.year = (4 * days) / (4 * 365 + 1);
153 days -= edate.year * 365 + LEAP_YEARS(edate.year);
154 for (i = 1; i <= 12; i++)
156 int leap_day = (IS_LEAP_YEAR(edate.year) && i == 2);
157 int leap_sub = (IS_LEAP_YEAR(edate.year) && i >= 3);
159 if (i == 12 || days - leap_sub < days_in_year[i + 1] + leap_day)
162 days -= days_in_year[i] + leap_sub;
166 edate.day = days + 1;
168 etime.hour = (unix_time % SEC_IN_DAY) / SEC_IN_HOUR;
169 etime.min = (unix_time % SEC_IN_HOUR) / SEC_IN_MIN;
170 etime.twosec = (unix_time % SEC_IN_MIN) / 2;
172 *date = cpu_to_le16(edate.raw);
173 *time = cpu_to_le16(etime.raw);
176 void exfat_get_name(const struct exfat_node* node, char* buffer, size_t n)
178 if (utf16_to_utf8(buffer, node->name, n, EXFAT_NAME_MAX) != 0)
179 exfat_bug("failed to convert name to UTF-8");
182 uint16_t exfat_start_checksum(const struct exfat_entry_meta1* entry)
187 for (i = 0; i < sizeof(struct exfat_entry); i++)
188 if (i != 2 && i != 3) /* skip checksum field itself */
189 sum = ((sum << 15) | (sum >> 1)) + ((const uint8_t*) entry)[i];
193 uint16_t exfat_add_checksum(const void* entry, uint16_t sum)
197 for (i = 0; i < sizeof(struct exfat_entry); i++)
198 sum = ((sum << 15) | (sum >> 1)) + ((const uint8_t*) entry)[i];
202 le16_t exfat_calc_checksum(const struct exfat_entry_meta1* meta1,
203 const struct exfat_entry_meta2* meta2, const le16_t* name)
206 const int name_entries = DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX);
209 checksum = exfat_start_checksum(meta1);
210 checksum = exfat_add_checksum(meta2, checksum);
211 for (i = 0; i < name_entries; i++)
213 struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
214 memcpy(name_entry.name, name + i * EXFAT_ENAME_MAX,
215 EXFAT_ENAME_MAX * sizeof(le16_t));
216 checksum = exfat_add_checksum(&name_entry, checksum);
218 return cpu_to_le16(checksum);
221 le16_t exfat_calc_name_hash(const struct exfat* ef, const le16_t* name)
224 size_t length = utf16_length(name);
227 for (i = 0; i < length; i++)
229 uint16_t c = le16_to_cpu(name[i]);
231 /* convert to upper case */
232 if (c < ef->upcase_chars)
233 c = le16_to_cpu(ef->upcase[c]);
235 hash = ((hash << 15) | (hash >> 1)) + (c & 0xff);
236 hash = ((hash << 15) | (hash >> 1)) + (c >> 8);
238 return cpu_to_le16(hash);