OSDN Git Service

Add file name hash calculation function.
[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_node* node, struct stat *stbuf)
16 {
17         memset(stbuf, 0, sizeof(struct stat));
18         if (node->flags & EXFAT_ATTRIB_DIR)
19                 stbuf->st_mode = S_IFDIR | 0755;
20         else
21                 stbuf->st_mode = S_IFREG | 0644;
22         stbuf->st_nlink = 1;
23         stbuf->st_size = node->size;
24         stbuf->st_mtime = node->mtime;
25         stbuf->st_atime = node->atime;
26         stbuf->st_ctime = 0; /* unapplicable */
27 }
28
29 #define SEC_IN_MIN 60ll
30 #define SEC_IN_HOUR (60 * SEC_IN_MIN)
31 #define SEC_IN_DAY (24 * SEC_IN_HOUR)
32 #define SEC_IN_YEAR (365 * SEC_IN_DAY) /* not leap year */
33 /* Unix epoch started at 0:00:00 UTC 1 January 1970 */
34 #define UNIX_EPOCH_YEAR 1970
35 /* exFAT epoch started at 0:00:00 UTC 1 January 1980 */
36 #define EXFAT_EPOCH_YEAR 1980
37 /* number of years from Unix epoch to exFAT epoch */
38 #define EPOCH_DIFF_YEAR (EXFAT_EPOCH_YEAR - UNIX_EPOCH_YEAR)
39 /* number of days from Unix epoch to exFAT epoch (considering leap days) */
40 #define EPOCH_DIFF_DAYS (EPOCH_DIFF_YEAR * 365 + EPOCH_DIFF_YEAR / 4)
41 /* number of seconds from Unix epoch to exFAT epoch (considering leap days) */
42 #define EPOCH_DIFF_SEC (EPOCH_DIFF_DAYS * SEC_IN_DAY)
43 /* number of leap years passed from exFAT epoch to the specified year
44    (excluding the specified year itself) */
45 #define LEAP_YEARS(year) ((EXFAT_EPOCH_YEAR + (year) - 1) / 4 \
46                 - (EXFAT_EPOCH_YEAR - 1) / 4)
47 /* checks whether the specified year is leap */
48 #define IS_LEAP_YEAR(year) ((EXFAT_EPOCH_YEAR + (year)) % 4 == 0)
49
50 static const time_t days_in_year[] =
51 {
52         /* Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec */
53         0,   0,  31,  59,  90, 120, 151, 181, 212, 243, 273, 304, 334
54 };
55
56 union exfat_date
57 {
58         uint16_t raw;
59         struct
60         {
61                 uint16_t day   : 5; /* 1-31 */
62                 uint16_t month : 4; /* 1-12 */
63                 uint16_t year  : 7; /* 1-127 (+1980) */
64         };
65 };
66
67 union exfat_time
68 {
69         uint16_t raw;
70         struct
71         {
72                 uint16_t twosec : 5; /* 0-29 (2 sec granularity) */
73                 uint16_t min    : 6; /* 0-59 */
74                 uint16_t hour   : 5; /* 0-23 */
75         };
76 };
77
78 time_t exfat_exfat2unix(le16_t date, le16_t time)
79 {
80         union exfat_date edate;
81         union exfat_time etime;
82         time_t unix_time = EPOCH_DIFF_SEC;
83
84         edate.raw = le16_to_cpu(date);
85         etime.raw = le16_to_cpu(time);
86
87         if (edate.day == 0 || edate.month == 0 || edate.month > 12)
88         {
89                 exfat_error("bad date %hu-%02hu-%02hu",
90                                 edate.year + EXFAT_EPOCH_YEAR, edate.month, edate.day);
91                 return 0;
92         }
93         if (etime.hour > 23 || etime.min > 59 || etime.twosec > 29)
94         {
95                 exfat_error("bad time %hu:%02hu:%02hu",
96                         etime.hour, etime.min, etime.twosec * 2);
97                 return 0;
98         }
99
100         /* every 4th year between 1904 and 2096 is leap */
101         unix_time += edate.year * SEC_IN_YEAR + LEAP_YEARS(edate.year) * SEC_IN_DAY;
102         unix_time += days_in_year[edate.month] * SEC_IN_DAY;
103         /* if it's leap year and February has passed we should add 1 day */
104         if ((EXFAT_EPOCH_YEAR + edate.year) % 4 == 0 && edate.month > 2)
105                 unix_time += SEC_IN_DAY;
106         unix_time += (edate.day - 1) * SEC_IN_DAY;
107
108         unix_time += etime.hour * SEC_IN_HOUR;
109         unix_time += etime.min * SEC_IN_MIN;
110         /* exFAT represents time with 2 sec granularity */
111         unix_time += etime.twosec * 2;
112
113         /* exFAT stores timestamps in local time, so we correct it to UTC */
114         unix_time += timezone;
115
116         return unix_time;
117 }
118
119 void exfat_unix2exfat(time_t unix_time, le16_t* date, le16_t* time)
120 {
121         union exfat_date edate;
122         union exfat_time etime;
123         time_t shift = EPOCH_DIFF_SEC + timezone;
124         int days;
125         int i;
126
127         /* time before exFAT epoch cannot be represented */
128         if (unix_time < shift)
129                 unix_time = shift;
130
131         unix_time -= shift;
132
133         days = unix_time / SEC_IN_DAY;
134         edate.year = (4 * days) / (4 * 365 + 1);
135         days -= edate.year * 365 + LEAP_YEARS(edate.year);
136         for (i = 1; i <= 12; i++)
137         {
138                 int leap_day = (IS_LEAP_YEAR(edate.year) && i == 2);
139                 int leap_sub = (IS_LEAP_YEAR(edate.year) && i >= 3);
140
141                 if (i == 12 || days - leap_sub < days_in_year[i + 1] + leap_day)
142                 {
143                         edate.month = i;
144                         days -= days_in_year[i] + leap_sub;
145                         break;
146                 }
147         }
148         edate.day = days + 1;
149
150         etime.hour = (unix_time % SEC_IN_DAY) / SEC_IN_HOUR;
151         etime.min = (unix_time % SEC_IN_HOUR) / SEC_IN_MIN;
152         etime.twosec = (unix_time % SEC_IN_MIN) / 2;
153
154         *date = cpu_to_le16(edate.raw);
155         *time = cpu_to_le16(etime.raw);
156 }
157
158 void exfat_get_name(const struct exfat_node* node, char* buffer, size_t n)
159 {
160         if (utf16_to_utf8(buffer, node->name, n, EXFAT_NAME_MAX) != 0)
161                 exfat_bug("failed to convert name to UTF-8");
162 }
163
164 uint16_t exfat_start_checksum(const struct exfat_file* entry)
165 {
166         uint16_t sum = 0;
167         int i;
168
169         for (i = 0; i < sizeof(struct exfat_entry); i++)
170                 if (i != 2 && i != 3) /* skip checksum field itself */
171                         sum = ((sum << 15) | (sum >> 1)) + ((const uint8_t*) entry)[i];
172         return sum;
173 }
174
175 uint16_t exfat_add_checksum(const void* entry, uint16_t sum)
176 {
177         int i;
178
179         for (i = 0; i < sizeof(struct exfat_entry); i++)
180                 sum = ((sum << 15) | (sum >> 1)) + ((const uint8_t*) entry)[i];
181         return sum;
182 }
183
184 le16_t exfat_calc_checksum(const struct exfat_file* meta1,
185                 const struct exfat_file_info* meta2, const le16_t* name)
186 {
187         uint16_t checksum;
188         const int name_entries = DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX);
189         int i;
190
191         checksum = exfat_start_checksum(meta1);
192         checksum = exfat_add_checksum(meta2, checksum);
193         for (i = 0; i < name_entries; i++)
194         {
195                 struct exfat_file_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
196                 memcpy(name_entry.name, name + i * EXFAT_ENAME_MAX,
197                                 EXFAT_ENAME_MAX * sizeof(le16_t));
198                 checksum = exfat_add_checksum(&name_entry, checksum);
199         }
200         return cpu_to_le16(checksum);
201 }
202
203 le16_t exfat_calc_name_hash(const struct exfat* ef, const le16_t* name)
204 {
205         size_t i;
206         size_t length = utf16_length(name);
207         uint16_t hash = 0;
208
209         for (i = 0; i < length; i++)
210         {
211                 uint16_t c = le16_to_cpu(name[i]);
212
213                 /* convert to upper case */
214                 if (c < ef->upcase_chars)
215                         c = le16_to_cpu(ef->upcase[c]);
216
217                 hash = ((hash << 15) | (hash >> 1)) + (c & 0xff);
218                 hash = ((hash << 15) | (hash >> 1)) + (c >> 8);
219         }
220         return cpu_to_le16(hash);
221 }