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* ef, const struct exfat_node* node,
18 memset(stbuf, 0, sizeof(struct stat));
19 if (node->flags & EXFAT_ATTRIB_DIR)
20 stbuf->st_mode = S_IFDIR | (0777 & ~ef->dmask);
22 stbuf->st_mode = S_IFREG | (0777 & ~ef->fmask);
24 stbuf->st_uid = ef->uid;
25 stbuf->st_gid = ef->gid;
26 stbuf->st_size = node->size;
27 stbuf->st_blocks = DIV_ROUND_UP(node->size, CLUSTER_SIZE(*ef->sb)) *
28 CLUSTER_SIZE(*ef->sb) / 512;
29 stbuf->st_mtime = node->mtime;
30 stbuf->st_atime = node->atime;
31 stbuf->st_ctime = 0; /* unapplicable */
34 #define SEC_IN_MIN 60ll
35 #define SEC_IN_HOUR (60 * SEC_IN_MIN)
36 #define SEC_IN_DAY (24 * SEC_IN_HOUR)
37 #define SEC_IN_YEAR (365 * SEC_IN_DAY) /* not leap year */
38 /* Unix epoch started at 0:00:00 UTC 1 January 1970 */
39 #define UNIX_EPOCH_YEAR 1970
40 /* exFAT epoch started at 0:00:00 UTC 1 January 1980 */
41 #define EXFAT_EPOCH_YEAR 1980
42 /* number of years from Unix epoch to exFAT epoch */
43 #define EPOCH_DIFF_YEAR (EXFAT_EPOCH_YEAR - UNIX_EPOCH_YEAR)
44 /* number of days from Unix epoch to exFAT epoch (considering leap days) */
45 #define EPOCH_DIFF_DAYS (EPOCH_DIFF_YEAR * 365 + EPOCH_DIFF_YEAR / 4)
46 /* number of seconds from Unix epoch to exFAT epoch (considering leap days) */
47 #define EPOCH_DIFF_SEC (EPOCH_DIFF_DAYS * SEC_IN_DAY)
48 /* number of leap years passed from exFAT epoch to the specified year
49 (excluding the specified year itself) */
50 #define LEAP_YEARS(year) ((EXFAT_EPOCH_YEAR + (year) - 1) / 4 \
51 - (EXFAT_EPOCH_YEAR - 1) / 4)
52 /* checks whether the specified year is leap */
53 #define IS_LEAP_YEAR(year) ((EXFAT_EPOCH_YEAR + (year)) % 4 == 0)
55 static const time_t days_in_year[] =
57 /* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */
58 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
66 uint16_t day : 5; /* 1-31 */
67 uint16_t month : 4; /* 1-12 */
68 uint16_t year : 7; /* 1-127 (+1980) */
77 uint16_t twosec : 5; /* 0-29 (2 sec granularity) */
78 uint16_t min : 6; /* 0-59 */
79 uint16_t hour : 5; /* 0-23 */
83 time_t exfat_exfat2unix(le16_t date, le16_t time)
85 union exfat_date edate;
86 union exfat_time etime;
87 time_t unix_time = EPOCH_DIFF_SEC;
89 edate.raw = le16_to_cpu(date);
90 etime.raw = le16_to_cpu(time);
92 if (edate.day == 0 || edate.month == 0 || edate.month > 12)
94 exfat_error("bad date %hu-%02hu-%02hu",
95 edate.year + EXFAT_EPOCH_YEAR, edate.month, edate.day);
98 if (etime.hour > 23 || etime.min > 59 || etime.twosec > 29)
100 exfat_error("bad time %hu:%02hu:%02hu",
101 etime.hour, etime.min, etime.twosec * 2);
105 /* every 4th year between 1904 and 2096 is leap */
106 unix_time += edate.year * SEC_IN_YEAR + LEAP_YEARS(edate.year) * SEC_IN_DAY;
107 unix_time += days_in_year[edate.month] * SEC_IN_DAY;
108 /* if it's leap year and February has passed we should add 1 day */
109 if ((EXFAT_EPOCH_YEAR + edate.year) % 4 == 0 && edate.month > 2)
110 unix_time += SEC_IN_DAY;
111 unix_time += (edate.day - 1) * SEC_IN_DAY;
113 unix_time += etime.hour * SEC_IN_HOUR;
114 unix_time += etime.min * SEC_IN_MIN;
115 /* exFAT represents time with 2 sec granularity */
116 unix_time += etime.twosec * 2;
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)
126 union exfat_date edate;
127 union exfat_time etime;
128 time_t shift = EPOCH_DIFF_SEC + timezone;
132 /* time before exFAT epoch cannot be represented */
133 if (unix_time < shift)
138 days = unix_time / SEC_IN_DAY;
139 edate.year = (4 * days) / (4 * 365 + 1);
140 days -= edate.year * 365 + LEAP_YEARS(edate.year);
141 for (i = 1; i <= 12; i++)
143 int leap_day = (IS_LEAP_YEAR(edate.year) && i == 2);
144 int leap_sub = (IS_LEAP_YEAR(edate.year) && i >= 3);
146 if (i == 12 || days - leap_sub < days_in_year[i + 1] + leap_day)
149 days -= days_in_year[i] + leap_sub;
153 edate.day = days + 1;
155 etime.hour = (unix_time % SEC_IN_DAY) / SEC_IN_HOUR;
156 etime.min = (unix_time % SEC_IN_HOUR) / SEC_IN_MIN;
157 etime.twosec = (unix_time % SEC_IN_MIN) / 2;
159 *date = cpu_to_le16(edate.raw);
160 *time = cpu_to_le16(etime.raw);
163 void exfat_get_name(const struct exfat_node* node, char* buffer, size_t n)
165 if (utf16_to_utf8(buffer, node->name, n, EXFAT_NAME_MAX) != 0)
166 exfat_bug("failed to convert name to UTF-8");
169 uint16_t exfat_start_checksum(const struct exfat_file* entry)
174 for (i = 0; i < sizeof(struct exfat_entry); i++)
175 if (i != 2 && i != 3) /* skip checksum field itself */
176 sum = ((sum << 15) | (sum >> 1)) + ((const uint8_t*) entry)[i];
180 uint16_t exfat_add_checksum(const void* entry, uint16_t sum)
184 for (i = 0; i < sizeof(struct exfat_entry); i++)
185 sum = ((sum << 15) | (sum >> 1)) + ((const uint8_t*) entry)[i];
189 le16_t exfat_calc_checksum(const struct exfat_file* meta1,
190 const struct exfat_file_info* meta2, const le16_t* name)
193 const int name_entries = DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX);
196 checksum = exfat_start_checksum(meta1);
197 checksum = exfat_add_checksum(meta2, checksum);
198 for (i = 0; i < name_entries; i++)
200 struct exfat_file_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
201 memcpy(name_entry.name, name + i * EXFAT_ENAME_MAX,
202 EXFAT_ENAME_MAX * sizeof(le16_t));
203 checksum = exfat_add_checksum(&name_entry, checksum);
205 return cpu_to_le16(checksum);
208 le16_t exfat_calc_name_hash(const struct exfat* ef, const le16_t* name)
211 size_t length = utf16_length(name);
214 for (i = 0; i < length; i++)
216 uint16_t c = le16_to_cpu(name[i]);
218 /* convert to upper case */
219 if (c < ef->upcase_chars)
220 c = le16_to_cpu(ef->upcase[c]);
222 hash = ((hash << 15) | (hash >> 1)) + (c & 0xff);
223 hash = ((hash << 15) | (hash >> 1)) + (c >> 8);
225 return cpu_to_le16(hash);