OSDN Git Service

Refactor file entry checksum calculation.
[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-2016  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 <string.h>
25 #include <stdio.h>
26 #include <inttypes.h>
27
28 void exfat_stat(const struct exfat* ef, const struct exfat_node* node,
29                 struct stat* stbuf)
30 {
31         memset(stbuf, 0, sizeof(struct stat));
32         if (node->flags & EXFAT_ATTRIB_DIR)
33                 stbuf->st_mode = S_IFDIR | (0777 & ~ef->dmask);
34         else
35                 stbuf->st_mode = S_IFREG | (0777 & ~ef->fmask);
36         stbuf->st_nlink = 1;
37         stbuf->st_uid = ef->uid;
38         stbuf->st_gid = ef->gid;
39         stbuf->st_size = node->size;
40         stbuf->st_blocks = DIV_ROUND_UP(node->size, CLUSTER_SIZE(*ef->sb)) *
41                 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, char* buffer, size_t n)
50 {
51         if (utf16_to_utf8(buffer, node->name, n, EXFAT_NAME_MAX) != 0)
52                 exfat_bug("failed to convert name to UTF-8");
53 }
54
55 static uint16_t add_checksum_byte(uint16_t sum, uint8_t byte)
56 {
57         return ((sum << 15) | (sum >> 1)) + byte;
58 }
59
60 static uint16_t add_checksum_bytes(uint16_t sum, const void* buffer, size_t n)
61 {
62         int i;
63
64         for (i = 0; i < n; i++)
65                 sum = add_checksum_byte(sum, ((const uint8_t*) buffer)[i]);
66         return sum;
67 }
68
69 uint16_t exfat_start_checksum(const struct exfat_entry_meta1* entry)
70 {
71         uint16_t sum = 0;
72         int i;
73
74         for (i = 0; i < sizeof(struct exfat_entry); i++)
75                 if (i != 2 && i != 3) /* skip checksum field itself */
76                         sum = add_checksum_byte(sum, ((const uint8_t*) entry)[i]);
77         return sum;
78 }
79
80 uint16_t exfat_add_checksum(const void* entry, uint16_t sum)
81 {
82         return add_checksum_bytes(sum, entry, sizeof(struct exfat_entry));
83 }
84
85 le16_t exfat_calc_checksum(const struct exfat_entry_meta1* meta1,
86                 const struct exfat_entry_meta2* meta2, const le16_t* name)
87 {
88         uint16_t checksum;
89         const int name_entries = DIV_ROUND_UP(meta2->name_length, EXFAT_ENAME_MAX);
90         int i;
91
92         checksum = exfat_start_checksum(meta1);
93         checksum = exfat_add_checksum(meta2, checksum);
94         for (i = 0; i < name_entries; i++)
95         {
96                 checksum = add_checksum_byte(checksum, EXFAT_ENTRY_FILE_NAME);
97                 checksum = add_checksum_byte(checksum, 0);
98                 checksum = add_checksum_bytes(checksum, name + i * EXFAT_ENAME_MAX,
99                                 EXFAT_ENAME_MAX * sizeof(le16_t));
100         }
101         return cpu_to_le16(checksum);
102 }
103
104 uint32_t exfat_vbr_start_checksum(const void* sector, size_t size)
105 {
106         size_t i;
107         uint32_t sum = 0;
108
109         for (i = 0; i < size; i++)
110                 /* skip volume_state and allocated_percent fields */
111                 if (i != 0x6a && i != 0x6b && i != 0x70)
112                         sum = ((sum << 31) | (sum >> 1)) + ((const uint8_t*) sector)[i];
113         return sum;
114 }
115
116 uint32_t exfat_vbr_add_checksum(const void* sector, size_t size, uint32_t sum)
117 {
118         size_t i;
119
120         for (i = 0; i < size; i++)
121                 sum = ((sum << 31) | (sum >> 1)) + ((const uint8_t*) sector)[i];
122         return sum;
123 }
124
125 le16_t exfat_calc_name_hash(const struct exfat* ef, const le16_t* name)
126 {
127         size_t i;
128         size_t length = utf16_length(name);
129         uint16_t hash = 0;
130
131         for (i = 0; i < length; i++)
132         {
133                 uint16_t c = le16_to_cpu(name[i]);
134
135                 /* convert to upper case */
136                 c = ef->upcase[c];
137
138                 hash = ((hash << 15) | (hash >> 1)) + (c & 0xff);
139                 hash = ((hash << 15) | (hash >> 1)) + (c >> 8);
140         }
141         return cpu_to_le16(hash);
142 }
143
144 void exfat_humanize_bytes(uint64_t value, struct exfat_human_bytes* hb)
145 {
146         size_t i;
147         /* 16 EB (minus 1 byte) is the largest size that can be represented by
148            uint64_t */
149         const char* units[] = {"bytes", "KB", "MB", "GB", "TB", "PB", "EB"};
150         uint64_t divisor = 1;
151         uint64_t temp = 0;
152
153         for (i = 0; ; i++, divisor *= 1024)
154         {
155                 temp = (value + divisor / 2) / divisor;
156
157                 if (temp == 0)
158                         break;
159                 if (temp / 1024 * 1024 == temp)
160                         continue;
161                 if (temp < 10240)
162                         break;
163         }
164         hb->value = temp;
165         hb->unit = units[i];
166 }
167
168 void exfat_print_info(const struct exfat_super_block* sb,
169                 uint32_t free_clusters)
170 {
171         struct exfat_human_bytes hb;
172         off_t total_space = le64_to_cpu(sb->sector_count) * SECTOR_SIZE(*sb);
173         off_t avail_space = (off_t) free_clusters * CLUSTER_SIZE(*sb);
174
175         printf("File system version           %hhu.%hhu\n",
176                         sb->version.major, sb->version.minor);
177         exfat_humanize_bytes(SECTOR_SIZE(*sb), &hb);
178         printf("Sector size          %10"PRIu64" %s\n", hb.value, hb.unit);
179         exfat_humanize_bytes(CLUSTER_SIZE(*sb), &hb);
180         printf("Cluster size         %10"PRIu64" %s\n", hb.value, hb.unit);
181         exfat_humanize_bytes(total_space, &hb);
182         printf("Volume size          %10"PRIu64" %s\n", hb.value, hb.unit);
183         exfat_humanize_bytes(total_space - avail_space, &hb);
184         printf("Used space           %10"PRIu64" %s\n", hb.value, hb.unit);
185         exfat_humanize_bytes(avail_space, &hb);
186         printf("Available space      %10"PRIu64" %s\n", hb.value, hb.unit);
187 }