utils.c 5.1 KB

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