bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1 | /* |
| 2 | utils.c (04.09.09) |
| 3 | exFAT file system implementation library. |
| 4 | |
bigbiff bigbiff | 61cdc02 | 2013-08-08 08:35:06 -0400 | [diff] [blame] | 5 | Free exFAT implementation. |
bigbiff bigbiff | ca829c4 | 2013-01-28 08:14:25 -0500 | [diff] [blame] | 6 | Copyright (C) 2010-2013 Andrew Nayenko |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 7 | |
bigbiff bigbiff | 61cdc02 | 2013-08-08 08:35:06 -0400 | [diff] [blame] | 8 | This program is free software; you can redistribute it and/or modify |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 9 | it under the terms of the GNU General Public License as published by |
bigbiff bigbiff | 61cdc02 | 2013-08-08 08:35:06 -0400 | [diff] [blame] | 10 | the Free Software Foundation, either version 2 of the License, or |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 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 | |
bigbiff bigbiff | 61cdc02 | 2013-08-08 08:35:06 -0400 | [diff] [blame] | 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. |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 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 | uint16_t exfat_start_checksum(const struct exfat_entry_meta1* entry) |
| 56 | { |
| 57 | uint16_t sum = 0; |
| 58 | int i; |
| 59 | |
| 60 | for (i = 0; i < sizeof(struct exfat_entry); i++) |
| 61 | if (i != 2 && i != 3) /* skip checksum field itself */ |
| 62 | sum = ((sum << 15) | (sum >> 1)) + ((const uint8_t*) entry)[i]; |
| 63 | return sum; |
| 64 | } |
| 65 | |
| 66 | uint16_t exfat_add_checksum(const void* entry, uint16_t sum) |
| 67 | { |
| 68 | int i; |
| 69 | |
| 70 | for (i = 0; i < sizeof(struct exfat_entry); i++) |
| 71 | sum = ((sum << 15) | (sum >> 1)) + ((const uint8_t*) entry)[i]; |
| 72 | return sum; |
| 73 | } |
| 74 | |
| 75 | le16_t exfat_calc_checksum(const struct exfat_entry_meta1* meta1, |
| 76 | const struct exfat_entry_meta2* meta2, const le16_t* name) |
| 77 | { |
| 78 | uint16_t checksum; |
| 79 | const int name_entries = DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX); |
| 80 | int i; |
| 81 | |
| 82 | checksum = exfat_start_checksum(meta1); |
| 83 | checksum = exfat_add_checksum(meta2, checksum); |
| 84 | for (i = 0; i < name_entries; i++) |
| 85 | { |
| 86 | struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0}; |
| 87 | memcpy(name_entry.name, name + i * EXFAT_ENAME_MAX, |
bigbiff bigbiff | 61cdc02 | 2013-08-08 08:35:06 -0400 | [diff] [blame] | 88 | MIN(EXFAT_ENAME_MAX, EXFAT_NAME_MAX - i * EXFAT_ENAME_MAX) * |
| 89 | sizeof(le16_t)); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 90 | checksum = exfat_add_checksum(&name_entry, checksum); |
| 91 | } |
| 92 | return cpu_to_le16(checksum); |
| 93 | } |
| 94 | |
| 95 | uint32_t exfat_vbr_start_checksum(const void* sector, size_t size) |
| 96 | { |
| 97 | size_t i; |
| 98 | uint32_t sum = 0; |
| 99 | |
| 100 | for (i = 0; i < size; i++) |
| 101 | /* skip volume_state and allocated_percent fields */ |
| 102 | if (i != 0x6a && i != 0x6b && i != 0x70) |
| 103 | sum = ((sum << 31) | (sum >> 1)) + ((const uint8_t*) sector)[i]; |
| 104 | return sum; |
| 105 | } |
| 106 | |
| 107 | uint32_t exfat_vbr_add_checksum(const void* sector, size_t size, uint32_t sum) |
| 108 | { |
| 109 | size_t i; |
| 110 | |
| 111 | for (i = 0; i < size; i++) |
| 112 | sum = ((sum << 31) | (sum >> 1)) + ((const uint8_t*) sector)[i]; |
| 113 | return sum; |
| 114 | } |
| 115 | |
| 116 | le16_t exfat_calc_name_hash(const struct exfat* ef, const le16_t* name) |
| 117 | { |
| 118 | size_t i; |
| 119 | size_t length = utf16_length(name); |
| 120 | uint16_t hash = 0; |
| 121 | |
| 122 | for (i = 0; i < length; i++) |
| 123 | { |
| 124 | uint16_t c = le16_to_cpu(name[i]); |
| 125 | |
| 126 | /* convert to upper case */ |
| 127 | if (c < ef->upcase_chars) |
| 128 | c = le16_to_cpu(ef->upcase[c]); |
| 129 | |
| 130 | hash = ((hash << 15) | (hash >> 1)) + (c & 0xff); |
| 131 | hash = ((hash << 15) | (hash >> 1)) + (c >> 8); |
| 132 | } |
| 133 | return cpu_to_le16(hash); |
| 134 | } |
| 135 | |
| 136 | void exfat_humanize_bytes(uint64_t value, struct exfat_human_bytes* hb) |
| 137 | { |
| 138 | size_t i; |
| 139 | /* 16 EB (minus 1 byte) is the largest size that can be represented by |
| 140 | uint64_t */ |
| 141 | const char* units[] = {"bytes", "KB", "MB", "GB", "TB", "PB", "EB"}; |
| 142 | uint64_t divisor = 1; |
| 143 | uint64_t temp = 0; |
| 144 | |
| 145 | for (i = 0; ; i++, divisor *= 1024) |
| 146 | { |
| 147 | temp = (value + divisor / 2) / divisor; |
| 148 | |
| 149 | if (temp == 0) |
| 150 | break; |
| 151 | if (temp / 1024 * 1024 == temp) |
| 152 | continue; |
| 153 | if (temp < 10240) |
| 154 | break; |
| 155 | } |
| 156 | hb->value = temp; |
| 157 | hb->unit = units[i]; |
| 158 | } |
| 159 | |
| 160 | void exfat_print_info(const struct exfat_super_block* sb, |
| 161 | uint32_t free_clusters) |
| 162 | { |
| 163 | struct exfat_human_bytes hb; |
| 164 | off64_t total_space = le64_to_cpu(sb->sector_count) * SECTOR_SIZE(*sb); |
| 165 | off64_t avail_space = (off64_t) free_clusters * CLUSTER_SIZE(*sb); |
| 166 | |
| 167 | printf("File system version %hhu.%hhu\n", |
| 168 | sb->version.major, sb->version.minor); |
| 169 | exfat_humanize_bytes(SECTOR_SIZE(*sb), &hb); |
| 170 | printf("Sector size %10"PRIu64" %s\n", hb.value, hb.unit); |
| 171 | exfat_humanize_bytes(CLUSTER_SIZE(*sb), &hb); |
| 172 | printf("Cluster size %10"PRIu64" %s\n", hb.value, hb.unit); |
| 173 | exfat_humanize_bytes(total_space, &hb); |
| 174 | printf("Volume size %10"PRIu64" %s\n", hb.value, hb.unit); |
| 175 | exfat_humanize_bytes(total_space - avail_space, &hb); |
| 176 | printf("Used space %10"PRIu64" %s\n", hb.value, hb.unit); |
| 177 | exfat_humanize_bytes(avail_space, &hb); |
| 178 | printf("Available space %10"PRIu64" %s\n", hb.value, hb.unit); |
| 179 | } |