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 | ca829c4 | 2013-01-28 08:14:25 -0500 | [diff] [blame] | 5 | Copyright (C) 2010-2013 Andrew Nayenko |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 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 | #include <stdio.h> |
| 24 | #include <inttypes.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 | /* set ctime to mtime to ensure we don't break programs that rely on ctime |
| 43 | (e.g. rsync) */ |
| 44 | stbuf->st_ctime = node->mtime; |
| 45 | } |
| 46 | |
| 47 | void exfat_get_name(const struct exfat_node* node, char* buffer, size_t n) |
| 48 | { |
| 49 | if (utf16_to_utf8(buffer, node->name, n, EXFAT_NAME_MAX) != 0) |
| 50 | exfat_bug("failed to convert name to UTF-8"); |
| 51 | } |
| 52 | |
| 53 | uint16_t exfat_start_checksum(const struct exfat_entry_meta1* entry) |
| 54 | { |
| 55 | uint16_t sum = 0; |
| 56 | int i; |
| 57 | |
| 58 | for (i = 0; i < sizeof(struct exfat_entry); i++) |
| 59 | if (i != 2 && i != 3) /* skip checksum field itself */ |
| 60 | sum = ((sum << 15) | (sum >> 1)) + ((const uint8_t*) entry)[i]; |
| 61 | return sum; |
| 62 | } |
| 63 | |
| 64 | uint16_t exfat_add_checksum(const void* entry, uint16_t sum) |
| 65 | { |
| 66 | int i; |
| 67 | |
| 68 | for (i = 0; i < sizeof(struct exfat_entry); i++) |
| 69 | sum = ((sum << 15) | (sum >> 1)) + ((const uint8_t*) entry)[i]; |
| 70 | return sum; |
| 71 | } |
| 72 | |
| 73 | le16_t exfat_calc_checksum(const struct exfat_entry_meta1* meta1, |
| 74 | const struct exfat_entry_meta2* meta2, const le16_t* name) |
| 75 | { |
| 76 | uint16_t checksum; |
| 77 | const int name_entries = DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX); |
| 78 | int i; |
| 79 | |
| 80 | checksum = exfat_start_checksum(meta1); |
| 81 | checksum = exfat_add_checksum(meta2, checksum); |
| 82 | for (i = 0; i < name_entries; i++) |
| 83 | { |
| 84 | struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0}; |
| 85 | memcpy(name_entry.name, name + i * EXFAT_ENAME_MAX, |
| 86 | EXFAT_ENAME_MAX * sizeof(le16_t)); |
| 87 | checksum = exfat_add_checksum(&name_entry, checksum); |
| 88 | } |
| 89 | return cpu_to_le16(checksum); |
| 90 | } |
| 91 | |
| 92 | uint32_t exfat_vbr_start_checksum(const void* sector, size_t size) |
| 93 | { |
| 94 | size_t i; |
| 95 | uint32_t sum = 0; |
| 96 | |
| 97 | for (i = 0; i < size; i++) |
| 98 | /* skip volume_state and allocated_percent fields */ |
| 99 | if (i != 0x6a && i != 0x6b && i != 0x70) |
| 100 | sum = ((sum << 31) | (sum >> 1)) + ((const uint8_t*) sector)[i]; |
| 101 | return sum; |
| 102 | } |
| 103 | |
| 104 | uint32_t exfat_vbr_add_checksum(const void* sector, size_t size, uint32_t sum) |
| 105 | { |
| 106 | size_t i; |
| 107 | |
| 108 | for (i = 0; i < size; i++) |
| 109 | sum = ((sum << 31) | (sum >> 1)) + ((const uint8_t*) sector)[i]; |
| 110 | return sum; |
| 111 | } |
| 112 | |
| 113 | le16_t exfat_calc_name_hash(const struct exfat* ef, const le16_t* name) |
| 114 | { |
| 115 | size_t i; |
| 116 | size_t length = utf16_length(name); |
| 117 | uint16_t hash = 0; |
| 118 | |
| 119 | for (i = 0; i < length; i++) |
| 120 | { |
| 121 | uint16_t c = le16_to_cpu(name[i]); |
| 122 | |
| 123 | /* convert to upper case */ |
| 124 | if (c < ef->upcase_chars) |
| 125 | c = le16_to_cpu(ef->upcase[c]); |
| 126 | |
| 127 | hash = ((hash << 15) | (hash >> 1)) + (c & 0xff); |
| 128 | hash = ((hash << 15) | (hash >> 1)) + (c >> 8); |
| 129 | } |
| 130 | return cpu_to_le16(hash); |
| 131 | } |
| 132 | |
| 133 | void exfat_humanize_bytes(uint64_t value, struct exfat_human_bytes* hb) |
| 134 | { |
| 135 | size_t i; |
| 136 | /* 16 EB (minus 1 byte) is the largest size that can be represented by |
| 137 | uint64_t */ |
| 138 | const char* units[] = {"bytes", "KB", "MB", "GB", "TB", "PB", "EB"}; |
| 139 | uint64_t divisor = 1; |
| 140 | uint64_t temp = 0; |
| 141 | |
| 142 | for (i = 0; ; i++, divisor *= 1024) |
| 143 | { |
| 144 | temp = (value + divisor / 2) / divisor; |
| 145 | |
| 146 | if (temp == 0) |
| 147 | break; |
| 148 | if (temp / 1024 * 1024 == temp) |
| 149 | continue; |
| 150 | if (temp < 10240) |
| 151 | break; |
| 152 | } |
| 153 | hb->value = temp; |
| 154 | hb->unit = units[i]; |
| 155 | } |
| 156 | |
| 157 | void exfat_print_info(const struct exfat_super_block* sb, |
| 158 | uint32_t free_clusters) |
| 159 | { |
| 160 | struct exfat_human_bytes hb; |
| 161 | off64_t total_space = le64_to_cpu(sb->sector_count) * SECTOR_SIZE(*sb); |
| 162 | off64_t avail_space = (off64_t) free_clusters * CLUSTER_SIZE(*sb); |
| 163 | |
| 164 | printf("File system version %hhu.%hhu\n", |
| 165 | sb->version.major, sb->version.minor); |
| 166 | exfat_humanize_bytes(SECTOR_SIZE(*sb), &hb); |
| 167 | printf("Sector size %10"PRIu64" %s\n", hb.value, hb.unit); |
| 168 | exfat_humanize_bytes(CLUSTER_SIZE(*sb), &hb); |
| 169 | printf("Cluster size %10"PRIu64" %s\n", hb.value, hb.unit); |
| 170 | exfat_humanize_bytes(total_space, &hb); |
| 171 | printf("Volume size %10"PRIu64" %s\n", hb.value, hb.unit); |
| 172 | exfat_humanize_bytes(total_space - avail_space, &hb); |
| 173 | printf("Used space %10"PRIu64" %s\n", hb.value, hb.unit); |
| 174 | exfat_humanize_bytes(avail_space, &hb); |
| 175 | printf("Available space %10"PRIu64" %s\n", hb.value, hb.unit); |
| 176 | } |