OSDN Git Service

Merge branch 'master' of git://github.com/relan/exfat into marshmallow-x86
[android-x86/external-exfat.git] / libexfat / utils.c
1 /*
2         utils.c (04.09.09)
3         exFAT file system implementation library.
4
5         Free exFAT implementation.
6         Copyright (C) 2010-2017  Andrew Nayenko
7
8         This program is free software; you can redistribute it and/or modify
9         it under the terms of the GNU General Public License as published by
10         the Free Software Foundation, either version 2 of the License, or
11         (at your option) any later version.
12
13         This program is distributed in the hope that it will be useful,
14         but WITHOUT ANY WARRANTY; without even the implied warranty of
15         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16         GNU General Public License for more details.
17
18         You should have received a copy of the GNU General Public License along
19         with this program; if not, write to the Free Software Foundation, Inc.,
20         51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 #include "exfat.h"
24 #include "utf.h"
25 #include <string.h>
26 #include <stdio.h>
27 #include <inttypes.h>
28
29 void exfat_stat(const struct exfat* ef, const struct exfat_node* node,
30                 struct stat* stbuf)
31 {
32         memset(stbuf, 0, sizeof(struct stat));
33         if (node->attrib & EXFAT_ATTRIB_DIR)
34                 stbuf->st_mode = S_IFDIR | (0777 & ~ef->dmask);
35         else
36                 stbuf->st_mode = S_IFREG | (0777 & ~ef->fmask);
37         stbuf->st_nlink = 1;
38         stbuf->st_uid = ef->uid;
39         stbuf->st_gid = ef->gid;
40         stbuf->st_size = node->size;
41         stbuf->st_blocks = ROUND_UP(node->size, CLUSTER_SIZE(*ef->sb)) / 512;
42         stbuf->st_mtime = node->mtime;
43         stbuf->st_atime = node->atime;
44         /* set ctime to mtime to ensure we don't break programs that rely on ctime
45            (e.g. rsync) */
46         stbuf->st_ctime = node->mtime;
47 }
48
49 void exfat_get_name(const struct exfat_node* node,
50                 char buffer[EXFAT_UTF8_NAME_BUFFER_MAX])
51 {
52         if (utf16_to_utf8(buffer, node->name, EXFAT_UTF8_NAME_BUFFER_MAX,
53                                 EXFAT_NAME_MAX) != 0)
54                 exfat_bug("failed to convert name to UTF-8");
55 }
56
57 static uint16_t add_checksum_byte(uint16_t sum, uint8_t byte)
58 {
59         return ((sum << 15) | (sum >> 1)) + byte;
60 }
61
62 static uint16_t add_checksum_bytes(uint16_t sum, const void* buffer, size_t n)
63 {
64         int i;
65
66         for (i = 0; i < n; i++)
67                 sum = add_checksum_byte(sum, ((const uint8_t*) buffer)[i]);
68         return sum;
69 }
70
71 uint16_t exfat_start_checksum(const struct exfat_entry_meta1* entry)
72 {
73         uint16_t sum = 0;
74         int i;
75
76         for (i = 0; i < sizeof(struct exfat_entry); i++)
77                 if (i != 2 && i != 3) /* skip checksum field itself */
78                         sum = add_checksum_byte(sum, ((const uint8_t*) entry)[i]);
79         return sum;
80 }
81
82 uint16_t exfat_add_checksum(const void* entry, uint16_t sum)
83 {
84         return add_checksum_bytes(sum, entry, sizeof(struct exfat_entry));
85 }
86
87 le16_t exfat_calc_checksum(const struct exfat_entry* entries, int n)
88 {
89         uint16_t checksum;
90         int i;
91
92         checksum = exfat_start_checksum((const struct exfat_entry_meta1*) entries);
93         for (i = 1; i < n; i++)
94                 checksum = exfat_add_checksum(entries + i, checksum);
95         return cpu_to_le16(checksum);
96 }
97
98 uint32_t exfat_vbr_start_checksum(const void* sector, size_t size)
99 {
100         size_t i;
101         uint32_t sum = 0;
102
103         for (i = 0; i < size; i++)
104                 /* skip volume_state and allocated_percent fields */
105                 if (i != 0x6a && i != 0x6b && i != 0x70)
106                         sum = ((sum << 31) | (sum >> 1)) + ((const uint8_t*) sector)[i];
107         return sum;
108 }
109
110 uint32_t exfat_vbr_add_checksum(const void* sector, size_t size, uint32_t sum)
111 {
112         size_t i;
113
114         for (i = 0; i < size; i++)
115                 sum = ((sum << 31) | (sum >> 1)) + ((const uint8_t*) sector)[i];
116         return sum;
117 }
118
119 le16_t exfat_calc_name_hash(const struct exfat* ef, const le16_t* name,
120                 size_t length)
121 {
122         size_t i;
123         uint16_t hash = 0;
124
125         for (i = 0; i < length; i++)
126         {
127                 uint16_t c = le16_to_cpu(name[i]);
128
129                 /* convert to upper case */
130                 c = ef->upcase[c];
131
132                 hash = ((hash << 15) | (hash >> 1)) + (c & 0xff);
133                 hash = ((hash << 15) | (hash >> 1)) + (c >> 8);
134         }
135         return cpu_to_le16(hash);
136 }
137
138 void exfat_humanize_bytes(uint64_t value, struct exfat_human_bytes* hb)
139 {
140         size_t i;
141         /* 16 EB (minus 1 byte) is the largest size that can be represented by
142            uint64_t */
143         const char* units[] = {"bytes", "KB", "MB", "GB", "TB", "PB", "EB"};
144         uint64_t divisor = 1;
145         uint64_t temp = 0;
146
147         for (i = 0; ; i++, divisor *= 1024)
148         {
149                 temp = (value + divisor / 2) / divisor;
150
151                 if (temp == 0)
152                         break;
153                 if (temp / 1024 * 1024 == temp)
154                         continue;
155                 if (temp < 10240)
156                         break;
157         }
158         hb->value = temp;
159         hb->unit = units[i];
160 }
161
162 void exfat_print_info(const struct exfat_super_block* sb,
163                 uint32_t free_clusters)
164 {
165         struct exfat_human_bytes hb;
166         off_t total_space = le64_to_cpu(sb->sector_count) * SECTOR_SIZE(*sb);
167         off_t avail_space = (off_t) free_clusters * CLUSTER_SIZE(*sb);
168
169         printf("File system version           %hhu.%hhu\n",
170                         sb->version.major, sb->version.minor);
171         exfat_humanize_bytes(SECTOR_SIZE(*sb), &hb);
172         printf("Sector size          %10"PRIu64" %s\n", hb.value, hb.unit);
173         exfat_humanize_bytes(CLUSTER_SIZE(*sb), &hb);
174         printf("Cluster size         %10"PRIu64" %s\n", hb.value, hb.unit);
175         exfat_humanize_bytes(total_space, &hb);
176         printf("Volume size          %10"PRIu64" %s\n", hb.value, hb.unit);
177         exfat_humanize_bytes(total_space - avail_space, &hb);
178         printf("Used space           %10"PRIu64" %s\n", hb.value, hb.unit);
179         exfat_humanize_bytes(avail_space, &hb);
180         printf("Available space      %10"PRIu64" %s\n", hb.value, hb.unit);
181 }