]> git.sven.stormbind.net Git - sven/exfat-utils.git/blob - libexfat/utils.c
Merge commit 'upstream/0.9.6'
[sven/exfat-utils.git] / libexfat / utils.c
1 /*
2         utils.c (04.09.09)
3         exFAT file system implementation library.
4
5         Copyright (C) 2009, 2010  Andrew Nayenko
6
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.
11
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.
16
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/>.
19 */
20
21 #include "exfat.h"
22 #include <string.h>
23 #include <stdio.h>
24 #include <inttypes.h>
25 #define _XOPEN_SOURCE /* for timezone in Linux */
26 #include <time.h>
27
28 void exfat_stat(const struct exfat* ef, const struct exfat_node* node,
29                 struct stat* stbuf)
30 {
31         memset(stbuf, 0, sizeof(struct stat));
32         if (node->flags & EXFAT_ATTRIB_DIR)
33                 stbuf->st_mode = S_IFDIR | (0777 & ~ef->dmask);
34         else
35                 stbuf->st_mode = S_IFREG | (0777 & ~ef->fmask);
36         stbuf->st_nlink = 1;
37         stbuf->st_uid = ef->uid;
38         stbuf->st_gid = ef->gid;
39         stbuf->st_size = node->size;
40         stbuf->st_blocks = DIV_ROUND_UP(node->size, CLUSTER_SIZE(*ef->sb)) *
41                 CLUSTER_SIZE(*ef->sb) / 512;
42         stbuf->st_mtime = node->mtime;
43         stbuf->st_atime = node->atime;
44         /* set ctime to mtime to ensure we don't break programs that rely on ctime
45            (e.g. rsync) */
46         stbuf->st_ctime = node->mtime;
47 }
48
49 #define SEC_IN_MIN 60ll
50 #define SEC_IN_HOUR (60 * SEC_IN_MIN)
51 #define SEC_IN_DAY (24 * SEC_IN_HOUR)
52 #define SEC_IN_YEAR (365 * SEC_IN_DAY) /* not leap year */
53 /* Unix epoch started at 0:00:00 UTC 1 January 1970 */
54 #define UNIX_EPOCH_YEAR 1970
55 /* exFAT epoch started at 0:00:00 UTC 1 January 1980 */
56 #define EXFAT_EPOCH_YEAR 1980
57 /* number of years from Unix epoch to exFAT epoch */
58 #define EPOCH_DIFF_YEAR (EXFAT_EPOCH_YEAR - UNIX_EPOCH_YEAR)
59 /* number of days from Unix epoch to exFAT epoch (considering leap days) */
60 #define EPOCH_DIFF_DAYS (EPOCH_DIFF_YEAR * 365 + EPOCH_DIFF_YEAR / 4)
61 /* number of seconds from Unix epoch to exFAT epoch (considering leap days) */
62 #define EPOCH_DIFF_SEC (EPOCH_DIFF_DAYS * SEC_IN_DAY)
63 /* number of leap years passed from exFAT epoch to the specified year
64    (excluding the specified year itself) */
65 #define LEAP_YEARS(year) ((EXFAT_EPOCH_YEAR + (year) - 1) / 4 \
66                 - (EXFAT_EPOCH_YEAR - 1) / 4)
67 /* checks whether the specified year is leap */
68 #define IS_LEAP_YEAR(year) ((EXFAT_EPOCH_YEAR + (year)) % 4 == 0)
69
70 static const time_t days_in_year[] =
71 {
72         /* Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec */
73         0,   0,  31,  59,  90, 120, 151, 181, 212, 243, 273, 304, 334
74 };
75
76 time_t exfat_exfat2unix(le16_t date, le16_t time, uint8_t centisec)
77 {
78         time_t unix_time = EPOCH_DIFF_SEC;
79         uint16_t ndate = le16_to_cpu(date);
80         uint16_t ntime = le16_to_cpu(time);
81
82         uint16_t day    = ndate & 0x1f;     /* 5 bits, 1-31 */
83         uint16_t month  = ndate >> 5 & 0xf; /* 4 bits, 1-12 */
84         uint16_t year   = ndate >> 9;       /* 7 bits, 1-127 (+1980) */
85
86         uint16_t twosec = ntime & 0x1f;     /* 5 bits, 0-29 (2 sec granularity) */
87         uint16_t min    = ntime >> 5 & 0xf; /* 6 bits, 0-59 */
88         uint16_t hour   = ntime >> 11;      /* 5 bits, 0-23 */
89
90         if (day == 0 || month == 0 || month > 12)
91         {
92                 exfat_error("bad date %hu-%02hu-%02hu",
93                                 year + EXFAT_EPOCH_YEAR, month, day);
94                 return 0;
95         }
96         if (hour > 23 || min > 59 || twosec > 29)
97         {
98                 exfat_error("bad time %hu:%02hu:%02hu",
99                                 hour, min, twosec * 2);
100                 return 0;
101         }
102         if (centisec > 199)
103         {
104                 exfat_error("bad centiseconds count %hhu", centisec);
105                 return 0;
106         }
107
108         /* every 4th year between 1904 and 2096 is leap */
109         unix_time += year * SEC_IN_YEAR + LEAP_YEARS(year) * SEC_IN_DAY;
110         unix_time += days_in_year[month] * SEC_IN_DAY;
111         /* if it's leap year and February has passed we should add 1 day */
112         if ((EXFAT_EPOCH_YEAR + year) % 4 == 0 && month > 2)
113                 unix_time += SEC_IN_DAY;
114         unix_time += (day - 1) * SEC_IN_DAY;
115
116         unix_time += hour * SEC_IN_HOUR;
117         unix_time += min * SEC_IN_MIN;
118         /* exFAT represents time with 2 sec granularity */
119         unix_time += twosec * 2;
120         unix_time += centisec / 100;
121
122         /* exFAT stores timestamps in local time, so we correct it to UTC */
123         unix_time += timezone;
124
125         return unix_time;
126 }
127
128 void exfat_unix2exfat(time_t unix_time, le16_t* date, le16_t* time,
129                 uint8_t* centisec)
130 {
131         time_t shift = EPOCH_DIFF_SEC + timezone;
132         uint16_t day, month, year;
133         uint16_t twosec, min, hour;
134         int days;
135         int i;
136
137         /* time before exFAT epoch cannot be represented */
138         if (unix_time < shift)
139                 unix_time = shift;
140
141         unix_time -= shift;
142
143         days = unix_time / SEC_IN_DAY;
144         year = (4 * days) / (4 * 365 + 1);
145         days -= year * 365 + LEAP_YEARS(year);
146         month = 0;
147         for (i = 1; i <= 12; i++)
148         {
149                 int leap_day = (IS_LEAP_YEAR(year) && i == 2);
150                 int leap_sub = (IS_LEAP_YEAR(year) && i >= 3);
151
152                 if (i == 12 || days - leap_sub < days_in_year[i + 1] + leap_day)
153                 {
154                         month = i;
155                         days -= days_in_year[i] + leap_sub;
156                         break;
157                 }
158         }
159         day = days + 1;
160
161         hour = (unix_time % SEC_IN_DAY) / SEC_IN_HOUR;
162         min = (unix_time % SEC_IN_HOUR) / SEC_IN_MIN;
163         twosec = (unix_time % SEC_IN_MIN) / 2;
164
165         *date = cpu_to_le16(day | (month << 5) | (year << 9));
166         *time = cpu_to_le16(twosec | (min << 5) | (hour << 11));
167         if (centisec)
168                 *centisec = (unix_time % 2) * 100;
169 }
170
171 void exfat_get_name(const struct exfat_node* node, char* buffer, size_t n)
172 {
173         if (utf16_to_utf8(buffer, node->name, n, EXFAT_NAME_MAX) != 0)
174                 exfat_bug("failed to convert name to UTF-8");
175 }
176
177 uint16_t exfat_start_checksum(const struct exfat_entry_meta1* entry)
178 {
179         uint16_t sum = 0;
180         int i;
181
182         for (i = 0; i < sizeof(struct exfat_entry); i++)
183                 if (i != 2 && i != 3) /* skip checksum field itself */
184                         sum = ((sum << 15) | (sum >> 1)) + ((const uint8_t*) entry)[i];
185         return sum;
186 }
187
188 uint16_t exfat_add_checksum(const void* entry, uint16_t sum)
189 {
190         int i;
191
192         for (i = 0; i < sizeof(struct exfat_entry); i++)
193                 sum = ((sum << 15) | (sum >> 1)) + ((const uint8_t*) entry)[i];
194         return sum;
195 }
196
197 le16_t exfat_calc_checksum(const struct exfat_entry_meta1* meta1,
198                 const struct exfat_entry_meta2* meta2, const le16_t* name)
199 {
200         uint16_t checksum;
201         const int name_entries = DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX);
202         int i;
203
204         checksum = exfat_start_checksum(meta1);
205         checksum = exfat_add_checksum(meta2, checksum);
206         for (i = 0; i < name_entries; i++)
207         {
208                 struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
209                 memcpy(name_entry.name, name + i * EXFAT_ENAME_MAX,
210                                 EXFAT_ENAME_MAX * sizeof(le16_t));
211                 checksum = exfat_add_checksum(&name_entry, checksum);
212         }
213         return cpu_to_le16(checksum);
214 }
215
216 uint32_t exfat_vbr_start_checksum(const void* sector, size_t size)
217 {
218         size_t i;
219         uint32_t sum = 0;
220
221         for (i = 0; i < size; i++)
222                 /* skip volume_state and allocated_percent fields */
223                 if (i != 0x6a && i != 0x6b && i != 0x70)
224                         sum = ((sum << 31) | (sum >> 1)) + ((const uint8_t*) sector)[i];
225         return sum;
226 }
227
228 uint32_t exfat_vbr_add_checksum(const void* sector, size_t size, uint32_t sum)
229 {
230         size_t i;
231
232         for (i = 0; i < size; i++)
233                 sum = ((sum << 31) | (sum >> 1)) + ((const uint8_t*) sector)[i];
234         return sum;
235 }
236
237
238 le16_t exfat_calc_name_hash(const struct exfat* ef, const le16_t* name)
239 {
240         size_t i;
241         size_t length = utf16_length(name);
242         uint16_t hash = 0;
243
244         for (i = 0; i < length; i++)
245         {
246                 uint16_t c = le16_to_cpu(name[i]);
247
248                 /* convert to upper case */
249                 if (c < ef->upcase_chars)
250                         c = le16_to_cpu(ef->upcase[c]);
251
252                 hash = ((hash << 15) | (hash >> 1)) + (c & 0xff);
253                 hash = ((hash << 15) | (hash >> 1)) + (c >> 8);
254         }
255         return cpu_to_le16(hash);
256 }
257
258 void exfat_humanize_bytes(uint64_t value, struct exfat_human_bytes* hb)
259 {
260         size_t i;
261         const char* units[] = {"bytes", "KB", "MB", "GB", "TB", "PB"};
262         uint64_t divisor = 1;
263         uint64_t temp = 0;
264
265         for (i = 0; i < sizeof(units) / sizeof(units[0]) - 1; i++, divisor *= 1024)
266         {
267                 temp = (value + divisor / 2) / divisor;
268
269                 if (temp == 0)
270                         break;
271                 if (temp / 1024 * 1024 == temp)
272                         continue;
273                 if (temp < 10240)
274                         break;
275         }
276         hb->value = temp;
277         hb->unit = units[i];
278 }
279
280 void exfat_print_info(const struct exfat_super_block* sb,
281                 uint32_t free_clusters)
282 {
283         struct exfat_human_bytes hb;
284         off_t total_space = le64_to_cpu(sb->sector_count) * SECTOR_SIZE(*sb);
285         off_t avail_space = (off_t) free_clusters * CLUSTER_SIZE(*sb);
286
287         printf("File system version           %hhu.%hhu\n",
288                         sb->version.major, sb->version.minor);
289         exfat_humanize_bytes(SECTOR_SIZE(*sb), &hb);
290         printf("Sector size          %10"PRIu64" %s\n", hb.value, hb.unit);
291         exfat_humanize_bytes(CLUSTER_SIZE(*sb), &hb);
292         printf("Cluster size         %10"PRIu64" %s\n", hb.value, hb.unit);
293         exfat_humanize_bytes(total_space, &hb);
294         printf("Volume size          %10"PRIu64" %s\n", hb.value, hb.unit);
295         exfat_humanize_bytes(total_space - avail_space, &hb);
296         printf("Used space           %10"PRIu64" %s\n", hb.value, hb.unit);
297         exfat_humanize_bytes(avail_space, &hb);
298         printf("Available space      %10"PRIu64" %s\n", hb.value, hb.unit);
299 }