OSDN Git Service

Implemented uid and gid mount options.
[android-x86/external-exfat.git] / libexfat / utils.c
1 /*
2  *  utils.c
3  *  exFAT file system implementation library.
4  *
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.
8  */
9
10 #include "exfat.h"
11 #include <string.h>
12 #define _XOPEN_SOURCE /* for timezone in Linux */
13 #include <time.h>
14
15 void exfat_stat(const struct exfat* ef, const struct exfat_node* node,
16                 struct stat* stbuf)
17 {
18         memset(stbuf, 0, sizeof(struct stat));
19         if (node->flags & EXFAT_ATTRIB_DIR)
20                 stbuf->st_mode = S_IFDIR | (0777 & ~ef->dmask);
21         else
22                 stbuf->st_mode = S_IFREG | (0777 & ~ef->fmask);
23         stbuf->st_nlink = 1;
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 */
32 }
33
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)
54
55 static const time_t days_in_year[] =
56 {
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
59 };
60
61 union exfat_date
62 {
63         uint16_t raw;
64         struct
65         {
66                 uint16_t day   : 5; /* 1-31 */
67                 uint16_t month : 4; /* 1-12 */
68                 uint16_t year  : 7; /* 1-127 (+1980) */
69         };
70 };
71
72 union exfat_time
73 {
74         uint16_t raw;
75         struct
76         {
77                 uint16_t twosec : 5; /* 0-29 (2 sec granularity) */
78                 uint16_t min    : 6; /* 0-59 */
79                 uint16_t hour   : 5; /* 0-23 */
80         };
81 };
82
83 time_t exfat_exfat2unix(le16_t date, le16_t time)
84 {
85         union exfat_date edate;
86         union exfat_time etime;
87         time_t unix_time = EPOCH_DIFF_SEC;
88
89         edate.raw = le16_to_cpu(date);
90         etime.raw = le16_to_cpu(time);
91
92         if (edate.day == 0 || edate.month == 0 || edate.month > 12)
93         {
94                 exfat_error("bad date %hu-%02hu-%02hu",
95                                 edate.year + EXFAT_EPOCH_YEAR, edate.month, edate.day);
96                 return 0;
97         }
98         if (etime.hour > 23 || etime.min > 59 || etime.twosec > 29)
99         {
100                 exfat_error("bad time %hu:%02hu:%02hu",
101                         etime.hour, etime.min, etime.twosec * 2);
102                 return 0;
103         }
104
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;
112
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;
117
118         /* exFAT stores timestamps in local time, so we correct it to UTC */
119         unix_time += timezone;
120
121         return unix_time;
122 }
123
124 void exfat_unix2exfat(time_t unix_time, le16_t* date, le16_t* time)
125 {
126         union exfat_date edate;
127         union exfat_time etime;
128         time_t shift = EPOCH_DIFF_SEC + timezone;
129         int days;
130         int i;
131
132         /* time before exFAT epoch cannot be represented */
133         if (unix_time < shift)
134                 unix_time = shift;
135
136         unix_time -= shift;
137
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++)
142         {
143                 int leap_day = (IS_LEAP_YEAR(edate.year) && i == 2);
144                 int leap_sub = (IS_LEAP_YEAR(edate.year) && i >= 3);
145
146                 if (i == 12 || days - leap_sub < days_in_year[i + 1] + leap_day)
147                 {
148                         edate.month = i;
149                         days -= days_in_year[i] + leap_sub;
150                         break;
151                 }
152         }
153         edate.day = days + 1;
154
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;
158
159         *date = cpu_to_le16(edate.raw);
160         *time = cpu_to_le16(etime.raw);
161 }
162
163 void exfat_get_name(const struct exfat_node* node, char* buffer, size_t n)
164 {
165         if (utf16_to_utf8(buffer, node->name, n, EXFAT_NAME_MAX) != 0)
166                 exfat_bug("failed to convert name to UTF-8");
167 }
168
169 uint16_t exfat_start_checksum(const struct exfat_file* entry)
170 {
171         uint16_t sum = 0;
172         int i;
173
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];
177         return sum;
178 }
179
180 uint16_t exfat_add_checksum(const void* entry, uint16_t sum)
181 {
182         int i;
183
184         for (i = 0; i < sizeof(struct exfat_entry); i++)
185                 sum = ((sum << 15) | (sum >> 1)) + ((const uint8_t*) entry)[i];
186         return sum;
187 }
188
189 le16_t exfat_calc_checksum(const struct exfat_file* meta1,
190                 const struct exfat_file_info* meta2, const le16_t* name)
191 {
192         uint16_t checksum;
193         const int name_entries = DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX);
194         int i;
195
196         checksum = exfat_start_checksum(meta1);
197         checksum = exfat_add_checksum(meta2, checksum);
198         for (i = 0; i < name_entries; i++)
199         {
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);
204         }
205         return cpu_to_le16(checksum);
206 }
207
208 le16_t exfat_calc_name_hash(const struct exfat* ef, const le16_t* name)
209 {
210         size_t i;
211         size_t length = utf16_length(name);
212         uint16_t hash = 0;
213
214         for (i = 0; i < length; i++)
215         {
216                 uint16_t c = le16_to_cpu(name[i]);
217
218                 /* convert to upper case */
219                 if (c < ef->upcase_chars)
220                         c = le16_to_cpu(ef->upcase[c]);
221
222                 hash = ((hash << 15) | (hash >> 1)) + (c & 0xff);
223                 hash = ((hash << 15) | (hash >> 1)) + (c >> 8);
224         }
225         return cpu_to_le16(hash);
226 }