+
+uint16_t exfat_start_checksum(const struct exfat_entry_meta1* entry)
+{
+ uint16_t sum = 0;
+ int i;
+
+ for (i = 0; i < sizeof(struct exfat_entry); i++)
+ if (i != 2 && i != 3) /* skip checksum field itself */
+ sum = ((sum << 15) | (sum >> 1)) + ((const uint8_t*) entry)[i];
+ return sum;
+}
+
+uint16_t exfat_add_checksum(const void* entry, uint16_t sum)
+{
+ int i;
+
+ for (i = 0; i < sizeof(struct exfat_entry); i++)
+ sum = ((sum << 15) | (sum >> 1)) + ((const uint8_t*) entry)[i];
+ return sum;
+}
+
+le16_t exfat_calc_checksum(const struct exfat_entry_meta1* meta1,
+ const struct exfat_entry_meta2* meta2, const le16_t* name)
+{
+ uint16_t checksum;
+ const int name_entries = DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX);
+ int i;
+
+ checksum = exfat_start_checksum(meta1);
+ checksum = exfat_add_checksum(meta2, checksum);
+ for (i = 0; i < name_entries; i++)
+ {
+ struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
+ memcpy(name_entry.name, name + i * EXFAT_ENAME_MAX,
+ EXFAT_ENAME_MAX * sizeof(le16_t));
+ checksum = exfat_add_checksum(&name_entry, checksum);
+ }
+ return cpu_to_le16(checksum);
+}
+
+uint32_t exfat_vbr_start_checksum(const void* block, size_t size)
+{
+ size_t i;
+ uint32_t sum = 0;
+
+ for (i = 0; i < size; i++)
+ /* skip volume_state and allocated_percent fields */
+ if (i != 0x6a && i != 0x6b && i != 0x70)
+ sum = ((sum << 31) | (sum >> 1)) + ((const uint8_t*) block)[i];
+ return sum;
+}
+
+uint32_t exfat_vbr_add_checksum(const void* block, size_t size, uint32_t sum)
+{
+ size_t i;
+
+ for (i = 0; i < size; i++)
+ sum = ((sum << 31) | (sum >> 1)) + ((const uint8_t*) block)[i];
+ return sum;
+}
+
+
+le16_t exfat_calc_name_hash(const struct exfat* ef, const le16_t* name)
+{
+ size_t i;
+ size_t length = utf16_length(name);
+ uint16_t hash = 0;
+
+ for (i = 0; i < length; i++)
+ {
+ uint16_t c = le16_to_cpu(name[i]);
+
+ /* convert to upper case */
+ if (c < ef->upcase_chars)
+ c = le16_to_cpu(ef->upcase[c]);
+
+ hash = ((hash << 15) | (hash >> 1)) + (c & 0xff);
+ hash = ((hash << 15) | (hash >> 1)) + (c >> 8);
+ }
+ return cpu_to_le16(hash);
+}
+
+void exfat_humanize_bytes(uint64_t value, struct exfat_human_bytes* hb)
+{
+ size_t i;
+ const char* units[] = {"bytes", "KB", "MB", "GB", "TB", "PB"};
+ uint64_t divisor = 1;
+
+ for (i = 0; i < sizeof(units) / sizeof(units[0]) - 1; i++)
+ {
+ if ((value + divisor / 2) / divisor < 1024)
+ break;
+ divisor *= 1024;
+ }
+ hb->value = (value + divisor / 2) / divisor;
+ hb->unit = units[i];
+}
+
+void exfat_print_info(const struct exfat_super_block* sb,
+ uint32_t free_clusters)
+{
+ struct exfat_human_bytes hb;
+ off_t total_space = (off_t) le64_to_cpu(sb->block_count) * BLOCK_SIZE(*sb);
+ off_t avail_space = (off_t) free_clusters * CLUSTER_SIZE(*sb);
+
+ printf("File system version %hhu.%hhu\n",
+ sb->version.major, sb->version.minor);
+ exfat_humanize_bytes(BLOCK_SIZE(*sb), &hb);
+ printf("Block size %8"PRIu64" %s\n", hb.value, hb.unit);
+ exfat_humanize_bytes(CLUSTER_SIZE(*sb), &hb);
+ printf("Cluster size %8"PRIu64" %s\n", hb.value, hb.unit);
+ exfat_humanize_bytes(total_space, &hb);
+ printf("Volume size %8"PRIu64" %s\n", hb.value, hb.unit);
+ exfat_humanize_bytes(total_space - avail_space, &hb);
+ printf("Used space %8"PRIu64" %s\n", hb.value, hb.unit);
+ exfat_humanize_bytes(avail_space, &hb);
+ printf("Available space %8"PRIu64" %s\n", hb.value, hb.unit);
+}