OSDN Git Service

Rename entries structures for consistency.
[android-x86/external-exfat.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 #define _XOPEN_SOURCE /* for timezone in Linux */
24 #include <time.h>
25
26 void exfat_stat(const struct exfat* ef, const struct exfat_node* node,
27                 struct stat* stbuf)
28 {
29         memset(stbuf, 0, sizeof(struct stat));
30         if (node->flags & EXFAT_ATTRIB_DIR)
31                 stbuf->st_mode = S_IFDIR | (0777 & ~ef->dmask);
32         else
33                 stbuf->st_mode = S_IFREG | (0777 & ~ef->fmask);
34         stbuf->st_nlink = 1;
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 */
43 }
44
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)
65
66 static const time_t days_in_year[] =
67 {
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
70 };
71
72 union exfat_date
73 {
74         uint16_t raw;
75         struct
76         {
77                 uint16_t day   : 5; /* 1-31 */
78                 uint16_t month : 4; /* 1-12 */
79                 uint16_t year  : 7; /* 1-127 (+1980) */
80         };
81 };
82
83 union exfat_time
84 {
85         uint16_t raw;
86         struct
87         {
88                 uint16_t twosec : 5; /* 0-29 (2 sec granularity) */
89                 uint16_t min    : 6; /* 0-59 */
90                 uint16_t hour   : 5; /* 0-23 */
91         };
92 };
93
94 time_t exfat_exfat2unix(le16_t date, le16_t time)
95 {
96         union exfat_date edate;
97         union exfat_time etime;
98         time_t unix_time = EPOCH_DIFF_SEC;
99
100         edate.raw = le16_to_cpu(date);
101         etime.raw = le16_to_cpu(time);
102
103         if (edate.day == 0 || edate.month == 0 || edate.month > 12)
104         {
105                 exfat_error("bad date %hu-%02hu-%02hu",
106                                 edate.year + EXFAT_EPOCH_YEAR, edate.month, edate.day);
107                 return 0;
108         }
109         if (etime.hour > 23 || etime.min > 59 || etime.twosec > 29)
110         {
111                 exfat_error("bad time %hu:%02hu:%02hu",
112                         etime.hour, etime.min, etime.twosec * 2);
113                 return 0;
114         }
115
116         /* every 4th year between 1904 and 2096 is leap */
117         unix_time += edate.year * SEC_IN_YEAR + LEAP_YEARS(edate.year) * SEC_IN_DAY;
118         unix_time += days_in_year[edate.month] * SEC_IN_DAY;
119         /* if it's leap year and February has passed we should add 1 day */
120         if ((EXFAT_EPOCH_YEAR + edate.year) % 4 == 0 && edate.month > 2)
121                 unix_time += SEC_IN_DAY;
122         unix_time += (edate.day - 1) * SEC_IN_DAY;
123
124         unix_time += etime.hour * SEC_IN_HOUR;
125         unix_time += etime.min * SEC_IN_MIN;
126         /* exFAT represents time with 2 sec granularity */
127         unix_time += etime.twosec * 2;
128
129         /* exFAT stores timestamps in local time, so we correct it to UTC */
130         unix_time += timezone;
131
132         return unix_time;
133 }
134
135 void exfat_unix2exfat(time_t unix_time, le16_t* date, le16_t* time)
136 {
137         union exfat_date edate;
138         union exfat_time etime;
139         time_t shift = EPOCH_DIFF_SEC + timezone;
140         int days;
141         int i;
142
143         /* time before exFAT epoch cannot be represented */
144         if (unix_time < shift)
145                 unix_time = shift;
146
147         unix_time -= shift;
148
149         days = unix_time / SEC_IN_DAY;
150         edate.year = (4 * days) / (4 * 365 + 1);
151         days -= edate.year * 365 + LEAP_YEARS(edate.year);
152         for (i = 1; i <= 12; i++)
153         {
154                 int leap_day = (IS_LEAP_YEAR(edate.year) && i == 2);
155                 int leap_sub = (IS_LEAP_YEAR(edate.year) && i >= 3);
156
157                 if (i == 12 || days - leap_sub < days_in_year[i + 1] + leap_day)
158                 {
159                         edate.month = i;
160                         days -= days_in_year[i] + leap_sub;
161                         break;
162                 }
163         }
164         edate.day = days + 1;
165
166         etime.hour = (unix_time % SEC_IN_DAY) / SEC_IN_HOUR;
167         etime.min = (unix_time % SEC_IN_HOUR) / SEC_IN_MIN;
168         etime.twosec = (unix_time % SEC_IN_MIN) / 2;
169
170         *date = cpu_to_le16(edate.raw);
171         *time = cpu_to_le16(etime.raw);
172 }
173
174 void exfat_get_name(const struct exfat_node* node, char* buffer, size_t n)
175 {
176         if (utf16_to_utf8(buffer, node->name, n, EXFAT_NAME_MAX) != 0)
177                 exfat_bug("failed to convert name to UTF-8");
178 }
179
180 uint16_t exfat_start_checksum(const struct exfat_entry_meta1* entry)
181 {
182         uint16_t sum = 0;
183         int i;
184
185         for (i = 0; i < sizeof(struct exfat_entry); i++)
186                 if (i != 2 && i != 3) /* skip checksum field itself */
187                         sum = ((sum << 15) | (sum >> 1)) + ((const uint8_t*) entry)[i];
188         return sum;
189 }
190
191 uint16_t exfat_add_checksum(const void* entry, uint16_t sum)
192 {
193         int i;
194
195         for (i = 0; i < sizeof(struct exfat_entry); i++)
196                 sum = ((sum << 15) | (sum >> 1)) + ((const uint8_t*) entry)[i];
197         return sum;
198 }
199
200 le16_t exfat_calc_checksum(const struct exfat_entry_meta1* meta1,
201                 const struct exfat_entry_meta2* meta2, const le16_t* name)
202 {
203         uint16_t checksum;
204         const int name_entries = DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX);
205         int i;
206
207         checksum = exfat_start_checksum(meta1);
208         checksum = exfat_add_checksum(meta2, checksum);
209         for (i = 0; i < name_entries; i++)
210         {
211                 struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
212                 memcpy(name_entry.name, name + i * EXFAT_ENAME_MAX,
213                                 EXFAT_ENAME_MAX * sizeof(le16_t));
214                 checksum = exfat_add_checksum(&name_entry, checksum);
215         }
216         return cpu_to_le16(checksum);
217 }
218
219 le16_t exfat_calc_name_hash(const struct exfat* ef, const le16_t* name)
220 {
221         size_t i;
222         size_t length = utf16_length(name);
223         uint16_t hash = 0;
224
225         for (i = 0; i < length; i++)
226         {
227                 uint16_t c = le16_to_cpu(name[i]);
228
229                 /* convert to upper case */
230                 if (c < ef->upcase_chars)
231                         c = le16_to_cpu(ef->upcase[c]);
232
233                 hash = ((hash << 15) | (hash >> 1)) + (c & 0xff);
234                 hash = ((hash << 15) | (hash >> 1)) + (c >> 8);
235         }
236         return cpu_to_le16(hash);
237 }