Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2020 Google LLC |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #ifndef MEM_IMG_H |
16 | | #define MEM_IMG_H |
17 | | |
18 | | #include <stddef.h> |
19 | | #include <stdint.h> |
20 | | |
21 | | #include "sleuthkit/tsk/tsk_tools_i.h" |
22 | | |
23 | | typedef struct { |
24 | | TSK_IMG_INFO img_info; |
25 | | const uint8_t *data; |
26 | | size_t size; |
27 | | } IMG_MEM_INFO; |
28 | | |
29 | | static ssize_t mem_read(TSK_IMG_INFO *img_info, TSK_OFF_T offset, char *buf, |
30 | 31 | size_t len) { |
31 | 31 | IMG_MEM_INFO *mem_info = reinterpret_cast<IMG_MEM_INFO *>(img_info); |
32 | | // Bounds-checking exists in the real drivers. |
33 | 31 | if (offset > mem_info->size) { |
34 | 0 | return -1; |
35 | 0 | } |
36 | 31 | ssize_t read_len = len; |
37 | 31 | if (offset + len > mem_info->size) { |
38 | 0 | read_len = mem_info->size - offset; |
39 | 0 | } |
40 | 31 | if (memcpy(buf, mem_info->data + offset, read_len) == nullptr) { |
41 | 0 | return -1; |
42 | 31 | } else { |
43 | 31 | return read_len; |
44 | 31 | } |
45 | 31 | } |
46 | | |
47 | 1.31k | static void mem_close(TSK_IMG_INFO *img_info) { |
48 | 1.31k | IMG_MEM_INFO *mem_info = reinterpret_cast<IMG_MEM_INFO *>(img_info); |
49 | 1.31k | tsk_deinit_lock(&(img_info->cache_lock)); |
50 | 1.31k | free(mem_info); |
51 | 1.31k | } |
52 | | |
53 | 0 | static void mem_imgstat(TSK_IMG_INFO *img_info, FILE *hFile) {} |
54 | | |
55 | 1.31k | TSK_IMG_INFO *mem_open(const uint8_t *data, size_t size) { |
56 | 1.31k | IMG_MEM_INFO *inmemory_img = |
57 | 1.31k | reinterpret_cast<IMG_MEM_INFO *>(malloc(sizeof(IMG_MEM_INFO))); |
58 | 1.31k | TSK_IMG_INFO *img; |
59 | 1.31k | if (inmemory_img == nullptr) { |
60 | 0 | return nullptr; |
61 | 0 | } |
62 | 1.31k | img = reinterpret_cast<TSK_IMG_INFO *>(inmemory_img); |
63 | 1.31k | img->itype = TSK_IMG_TYPE_RAW; |
64 | 1.31k | img->read = mem_read; |
65 | 1.31k | img->close = mem_close; |
66 | 1.31k | img->imgstat = mem_imgstat; |
67 | 1.31k | img->size = size; |
68 | 1.31k | img->sector_size = 512; |
69 | 1.31k | tsk_init_lock(&(img->cache_lock)); |
70 | 1.31k | inmemory_img->data = data; |
71 | 1.31k | inmemory_img->size = size; |
72 | 1.31k | return img; |
73 | 1.31k | } |
74 | | |
75 | | #endif // # MEM_IMG_H |