1 /*
2  * The Sleuth Kit
3  *
4  * Brian Carrier [carrier <at> sleuthkit [dot] org]
5  * Copyright (c) 2019-2020 Brian Carrier.  All Rights reserved
6  * Copyright (c) 2018-2019 BlackBag Technologies.  All Rights reserved
7  *
8  * This software is distributed under the Common Public License 1.0
9  */
10 /** \@file Public C++ API */
11 #pragma once
12 
13 #include <cstdint>
14 #include <utility>
15 #include <vector>
16 
17 #include "../auto/guid.h"
18 #include "tsk_pool.h"
19 
20 class TSKPool {
21  public:
22   using img_t = std::pair<TSK_IMG_INFO *const, const TSK_OFF_T>;
23   using range = struct {
24     uint64_t start_block;
25     uint64_t num_blocks;
26   };
27 
28   // Not default constructible
29   TSKPool() = delete;
30 
31   // Not copyable, due the TSK_IMG_INFO pointers
32   TSKPool(const TSKPool &) = delete;
33   TSKPool &operator=(const TSKPool &) = delete;
34 
35   // Moveable
36   TSKPool(TSKPool &&) = default;
37   TSKPool &operator=(TSKPool &&) = default;
38 
39   virtual ~TSKPool() = default;
40 
41   inline const Guid &uuid() const { return _uuid; }
42 
43   inline uint32_t block_size() const noexcept { return _block_size; }
44   inline uint32_t dev_block_size() const noexcept { return _dev_block_size; }
45   inline uint64_t num_blocks() const noexcept { return _num_blocks; }
46   inline uint64_t first_img_offset() const noexcept {
47       if (_members.size() >= 1) {
48           return _members[0].second;
49       }
50       return 0;
51   }
52   inline int num_vols() const noexcept { return _num_vols; }
53 
54   virtual ssize_t read(uint64_t address, char *buf, size_t buf_size) const
55       noexcept = 0;
56 
57   virtual const std::vector<range> unallocated_ranges() const { return {}; };
58 
59   TSK_IMG_INFO *getTSKImgInfo(unsigned int index) const {
60       if (index < _members.size()) {
61           return _members[index].first;
62       }
63       return NULL;
64   };
65 
66  protected:
67   TSKPool(std::vector<img_t> &&imgs) noexcept : _members{std::move(imgs)} {}
68 
69   std::vector<img_t> _members{};
70   Guid _uuid{};
71   uint64_t _num_blocks;
72   int _num_vols;
73   uint32_t _block_size{};
74   uint32_t _dev_block_size{};
75 };
76 
77 // Helper function to make it easier to set flag bits on enumerations
78 template <typename T, typename = std::enable_if_t<std::is_enum<T>::value>>
79 inline T &operator|=(T &a, T b) {
80   a = static_cast<T>(a | b);
81 
82   return a;
83 }
84