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