OSDN Git Service

Added facilities for printing bytes values in human-readable format (i.e. with SI...
[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 time_t exfat_exfat2unix(le16_t date, le16_t time, uint8_t centisec)
73 {
74         time_t unix_time = EPOCH_DIFF_SEC;
75         uint16_t ndate = le16_to_cpu(date);
76         uint16_t ntime = le16_to_cpu(time);
77
78         uint16_t day    = ndate & 0x1f;     /* 5 bits, 1-31 */
79         uint16_t month  = ndate >> 5 & 0xf; /* 4 bits, 1-12 */
80         uint16_t year   = ndate >> 9;       /* 7 bits, 1-127 (+1980) */
81
82         uint16_t twosec = ntime & 0x1f;     /* 5 bits, 0-29 (2 sec granularity) */
83         uint16_t min    = ntime >> 5 & 0xf; /* 6 bits, 0-59 */
84         uint16_t hour   = ntime >> 11;      /* 5 bits, 0-23 */
85
86         if (day == 0 || month == 0 || month > 12)
87         {
88                 exfat_error("bad date %hu-%02hu-%02hu",
89                                 year + EXFAT_EPOCH_YEAR, month, day);
90                 return 0;
91         }
92         if (hour > 23 || min > 59 || twosec > 29)
93         {
94                 exfat_error("bad time %hu:%02hu:%02hu",
95                                 hour, min, twosec * 2);
96                 return 0;
97         }
98         if (centisec > 199)
99         {
100                 exfat_error("bad centiseconds count %hhu", centisec);
101                 return 0;
102         }
103
104         /* every 4th year between 1904 and 2096 is leap */
105         unix_time += year * SEC_IN_YEAR + LEAP_YEARS(year) * SEC_IN_DAY;
106         unix_time += days_in_year[month] * SEC_IN_DAY;
107         /* if it's leap year and February has passed we should add 1 day */
108         if ((EXFAT_EPOCH_YEAR + year) % 4 == 0 && month > 2)
109                 unix_time += SEC_IN_DAY;
110         unix_time += (day - 1) * SEC_IN_DAY;
111
112         unix_time += hour * SEC_IN_HOUR;
113         unix_time += min * SEC_IN_MIN;
114         /* exFAT represents time with 2 sec granularity */
115         unix_time += twosec * 2;
116         unix_time += centisec / 100;
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                 uint8_t* centisec)
126 {
127         time_t shift = EPOCH_DIFF_SEC + timezone;
128         uint16_t day, month, year;
129         uint16_t twosec, min, hour;
130         int days;
131         int i;
132
133         /* time before exFAT epoch cannot be represented */
134         if (unix_time < shift)
135                 unix_time = shift;
136
137         unix_time -= shift;
138
139         days = unix_time / SEC_IN_DAY;
140         year = (4 * days) / (4 * 365 + 1);
141         days -= year * 365 + LEAP_YEARS(year);
142         month = 0;
143         for (i = 1; i <= 12; i++)
144         {
145                 int leap_day = (IS_LEAP_YEAR(year) && i == 2);
146                 int leap_sub = (IS_LEAP_YEAR(year) && i >= 3);
147
148                 if (i == 12 || days - leap_sub < days_in_year[i + 1] + leap_day)
149                 {
150                         month = i;
151                         days -= days_in_year[i] + leap_sub;
152                         break;
153                 }
154         }
155         day = days + 1;
156
157         hour = (unix_time % SEC_IN_DAY) / SEC_IN_HOUR;
158         min = (unix_time % SEC_IN_HOUR) / SEC_IN_MIN;
159         twosec = (unix_time % SEC_IN_MIN) / 2;
160
161         *date = cpu_to_le16(day | (month << 5) | (year << 9));
162         *time = cpu_to_le16(twosec | (min << 5) | (hour << 11));
163         if (centisec)
164                 *centisec = (unix_time % 2) * 100;
165 }
166
167 void exfat_get_name(const struct exfat_node* node, char* buffer, size_t n)
168 {
169         if (utf16_to_utf8(buffer, node->name, n, EXFAT_NAME_MAX) != 0)
170                 exfat_bug("failed to convert name to UTF-8");
171 }
172
173 uint16_t exfat_start_checksum(const struct exfat_entry_meta1* entry)
174 {
175         uint16_t sum = 0;
176         int i;
177
178         for (i = 0; i < sizeof(struct exfat_entry); i++)
179                 if (i != 2 && i != 3) /* skip checksum field itself */
180                         sum = ((sum << 15) | (sum >> 1)) + ((const uint8_t*) entry)[i];
181         return sum;
182 }
183
184 uint16_t exfat_add_checksum(const void* entry, uint16_t sum)
185 {
186         int i;
187
188         for (i = 0; i < sizeof(struct exfat_entry); i++)
189                 sum = ((sum << 15) | (sum >> 1)) + ((const uint8_t*) entry)[i];
190         return sum;
191 }
192
193 le16_t exfat_calc_checksum(const struct exfat_entry_meta1* meta1,
194                 const struct exfat_entry_meta2* meta2, const le16_t* name)
195 {
196         uint16_t checksum;
197         const int name_entries = DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX);
198         int i;
199
200         checksum = exfat_start_checksum(meta1);
201         checksum = exfat_add_checksum(meta2, checksum);
202         for (i = 0; i < name_entries; i++)
203         {
204                 struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
205                 memcpy(name_entry.name, name + i * EXFAT_ENAME_MAX,
206                                 EXFAT_ENAME_MAX * sizeof(le16_t));
207                 checksum = exfat_add_checksum(&name_entry, checksum);
208         }
209         return cpu_to_le16(checksum);
210 }
211
212 le16_t exfat_calc_name_hash(const struct exfat* ef, const le16_t* name)
213 {
214         size_t i;
215         size_t length = utf16_length(name);
216         uint16_t hash = 0;
217
218         for (i = 0; i < length; i++)
219         {
220                 uint16_t c = le16_to_cpu(name[i]);
221
222                 /* convert to upper case */
223                 if (c < ef->upcase_chars)
224                         c = le16_to_cpu(ef->upcase[c]);
225
226                 hash = ((hash << 15) | (hash >> 1)) + (c & 0xff);
227                 hash = ((hash << 15) | (hash >> 1)) + (c >> 8);
228         }
229         return cpu_to_le16(hash);
230 }
231
232 void exfat_humanize_bytes(uint64_t value, struct exfat_human_bytes* hb)
233 {
234         size_t i;
235         const char* units[] = {"bytes", "KB", "MB", "GB", "TB", "PB"};
236         uint64_t divisor = 1;
237
238         for (i = 0; i < sizeof(units) / sizeof(units[0]) - 1; i++)
239         {
240                 if ((value + divisor / 2) / divisor < 1024)
241                         break;
242                 divisor *= 1024;
243         }
244         hb->value = (value + divisor / 2) / divisor;
245         hb->unit = units[i];
246 }