/src/is_blockdetection.h

http://ext3grep.googlecode.com/ · C Header · 108 lines · 60 code · 16 blank · 32 comment · 3 complexity · 298833ef82f908e74557f62687857cc6 MD5 · raw file

  1. // ext3grep -- An ext3 file system investigation and undelete tool
  2. //
  3. //! @file is_blockdetection.h Various is_* test functions.
  4. //
  5. // Copyright (C) 2008, by
  6. //
  7. // Carlo Wood, Run on IRC <carlo@alinoe.com>
  8. // RSA-1024 0x624ACAD5 1997-01-26 Sign & Encrypt
  9. // Fingerprint16 = 32 EC A7 B6 AC DB 65 A6 F6 F6 55 DD 1C DC FF 61
  10. //
  11. // This program is free software: you can redistribute it and/or modify
  12. // it under the terms of the GNU General Public License as published by
  13. // the Free Software Foundation, either version 2 of the License, or
  14. // (at your option) any later version.
  15. //
  16. // This program is distributed in the hope that it will be useful,
  17. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. // GNU General Public License for more details.
  20. //
  21. // You should have received a copy of the GNU General Public License
  22. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. #ifndef IS_BLOCKDETECTION_H
  24. #define IS_BLOCKDETECTION_H
  25. #ifndef USE_PCH
  26. #include <stdint.h> // Needed for uint32_t
  27. #include <iosfwd> // Needed for std::ostream
  28. #endif
  29. #include "inode.h" // Needed for InodePointer
  30. // Return type of is_directory.
  31. enum is_directory_type {
  32. isdir_no = 0, // Block is not a directory.
  33. isdir_start, // Block is a directory containing "." and "..".
  34. isdir_extended // Block is a directory not containing "." and "..".
  35. };
  36. class DirectoryBlockStats {
  37. private:
  38. int M_number_of_entries; // Number of entries in chain to the end.
  39. __u8 M_unlikely_character_count[256]; // Character count of filenames.
  40. public:
  41. DirectoryBlockStats(void) { std::memset(this, 0, sizeof(DirectoryBlockStats)); }
  42. int number_of_entries(void) const { return M_number_of_entries; }
  43. void increment_number_of_entries(void) { ++M_number_of_entries; }
  44. void increment_unlikely_character_count(__u8 c) { ++M_unlikely_character_count[c]; }
  45. };
  46. // Return true if this inode is a directory.
  47. inline bool is_directory(Inode const& inode)
  48. {
  49. return (inode.mode() & 0xf000) == 0x4000;
  50. }
  51. // Same for an InodePointer.
  52. inline bool is_directory(InodePointer const& inoderef)
  53. {
  54. // We can dereference inoderef here because it is known that is_directory does not keep a pointer or reference to the inode.
  55. return is_directory(*inoderef);
  56. }
  57. // Return true if this inode is a symlink.
  58. inline bool is_symlink(Inode const& inode)
  59. {
  60. return (inode.mode() & 0xf000) == 0xa000;
  61. }
  62. // Same for an InodePointer.
  63. inline bool is_symlink(InodePointer const& inoderef)
  64. {
  65. // We can dereference inoderef here because it is known that is_symlink does not keep a pointer or reference to the inode.
  66. return is_symlink(*inoderef);
  67. }
  68. // Return true if this inode is a regular file.
  69. inline bool is_regular_file(Inode const& inode)
  70. {
  71. return (inode.mode() & 0xf000) == 0x8000;
  72. }
  73. // Same for an InodePointer.
  74. inline bool is_regular_file(InodePointer const& inoderef)
  75. {
  76. // We can dereference inoderef here because it is known that is_regular_file does not keep a pointer or reference to the inode.
  77. return is_regular_file(*inoderef);
  78. }
  79. inline bool is_block_number(uint32_t block_number)
  80. {
  81. return block_number < block_count_;
  82. }
  83. inline bool is_data_block_number(uint32_t block_number)
  84. {
  85. return block_number < block_count_; // FIXME: not all blocks contain data (ie, skip at least the inode tables).
  86. }
  87. int block_to_inode(int block);
  88. bool is_inode(int block);
  89. bool is_allocated(int inode);
  90. int inode_to_block(ext3_super_block const& super_block, int inode);
  91. void print_buf_to(std::ostream& os, char const* buf, int len);
  92. #endif // IS_BLOCKDETECTION_H