OSDN Git Service

Added __packed__ attribute to all on-disk structures definitions.
[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 __attribute__((__packed__));
83
84 union exfat_time
85 {
86         uint16_t raw;
87         struct
88         {
89                 uint16_t twosec : 5; /* 0-29 (2 sec granularity) */
90                 uint16_t min    : 6; /* 0-59 */
91                 uint16_t hour   : 5; /* 0-23 */
92         };
93 }
94 __attribute__((__packed__));
95
96 time_t exfat_exfat2unix(le16_t date, le16_t time)
97 {
98         union exfat_date edate;
99         union exfat_time etime;
100         time_t unix_time = EPOCH_DIFF_SEC;
101
102         edate.raw = le16_to_cpu(date);
103         etime.raw = le16_to_cpu(time);
104
105         if (edate.day == 0 || edate.month == 0 || edate.month > 12)
106         {
107                 exfat_error("bad date %hu-%02hu-%02hu",
108                                 edate.year + EXFAT_EPOCH_YEAR, edate.month, edate.day);
109                 return 0;
110         }
111         if (etime.hour > 23 || etime.min > 59 || etime.twosec > 29)
112         {
113                 exfat_error("bad time %hu:%02hu:%02hu",
114                         etime.hour, etime.min, etime.twosec * 2);
115                 return 0;
116         }
117
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;
125
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;
130
131         /* exFAT stores timestamps in local time, so we correct it to UTC */
132         unix_time += timezone;
133
134         return unix_time;
135 }
136
137 void exfat_unix2exfat(time_t unix_time, le16_t* date, le16_t* time)
138 {
139         union exfat_date edate;
140         union exfat_time etime;
141         time_t shift = EPOCH_DIFF_SEC + timezone;
142         int days;
143         int i;
144
145         /* time before exFAT epoch cannot be represented */
146         if (unix_time < shift)
147                 unix_time = shift;
148
149         unix_time -= shift;
150
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++)
155         {
156                 int leap_day = (IS_LEAP_YEAR(edate.year) && i == 2);
157                 int leap_sub = (IS_LEAP_YEAR(edate.year) && i >= 3);
158
159                 if (i == 12 || days - leap_sub < days_in_year[i + 1] + leap_day)
160                 {
161                         edate.month = i;
162                         days -= days_in_year[i] + leap_sub;
163                         break;
164                 }
165         }
166         edate.day = days + 1;
167
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;
171
172         *date = cpu_to_le16(edate.raw);
173         *time = cpu_to_le16(etime.raw);
174 }
175
176 void exfat_get_name(const struct exfat_node* node, char* buffer, size_t n)
177 {
178         if (utf16_to_utf8(buffer, node->name, n, EXFAT_NAME_MAX) != 0)
179                 exfat_bug("failed to convert name to UTF-8");
180 }
181
182 uint16_t exfat_start_checksum(const struct exfat_entry_meta1* entry)
183 {
184         uint16_t sum = 0;
185         int i;
186
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];
190         return sum;
191 }
192
193 uint16_t exfat_add_checksum(const void* entry, uint16_t sum)
194 {
195         int i;
196
197         for (i = 0; i < sizeof(struct exfat_entry); i++)
198                 sum = ((sum << 15) | (sum >> 1)) + ((const uint8_t*) entry)[i];
199         return sum;
200 }
201
202 le16_t exfat_calc_checksum(const struct exfat_entry_meta1* meta1,
203                 const struct exfat_entry_meta2* meta2, const le16_t* name)
204 {
205         uint16_t checksum;
206         const int name_entries = DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX);
207         int i;
208
209         checksum = exfat_start_checksum(meta1);
210         checksum = exfat_add_checksum(meta2, checksum);
211         for (i = 0; i < name_entries; i++)
212         {
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);
217         }
218         return cpu_to_le16(checksum);
219 }
220
221 le16_t exfat_calc_name_hash(const struct exfat* ef, const le16_t* name)
222 {
223         size_t i;
224         size_t length = utf16_length(name);
225         uint16_t hash = 0;
226
227         for (i = 0; i < length; i++)
228         {
229                 uint16_t c = le16_to_cpu(name[i]);
230
231                 /* convert to upper case */
232                 if (c < ef->upcase_chars)
233                         c = le16_to_cpu(ef->upcase[c]);
234
235                 hash = ((hash << 15) | (hash >> 1)) + (c & 0xff);
236                 hash = ((hash << 15) | (hash >> 1)) + (c >> 8);
237         }
238         return cpu_to_le16(hash);
239 }