/src/sleuthkit/ossfuzz/mem_img.h
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 "tsk/tsk_tools_i.h" |
22 | | #include "tsk/img/legacy_cache.h" |
23 | | #include "tsk/img/tsk_img_i.h" |
24 | | |
25 | | typedef struct { |
26 | | IMG_INFO img_info; |
27 | | const uint8_t *data; |
28 | | size_t size; |
29 | | } IMG_MEM_INFO; |
30 | | |
31 | | static ssize_t mem_read(TSK_IMG_INFO *img_info, TSK_OFF_T offset, char *buf, |
32 | 380 | size_t len) { |
33 | 380 | IMG_MEM_INFO *mem_info = reinterpret_cast<IMG_MEM_INFO *>(img_info); |
34 | | // Bounds-checking exists in the real drivers. |
35 | 380 | if ((offset < 0) || ((size_t) offset > mem_info->size)) { |
36 | 0 | return -1; |
37 | 0 | } |
38 | 380 | ssize_t read_len = len; |
39 | 380 | if (len > (mem_info->size - offset)) { |
40 | 0 | read_len = mem_info->size - offset; |
41 | 0 | } |
42 | 380 | if ((read_len > 0) && memcpy(buf, mem_info->data + offset, read_len) == nullptr) { |
43 | 0 | return -1; |
44 | 380 | } else { |
45 | 380 | return read_len; |
46 | 380 | } |
47 | 380 | } |
48 | | |
49 | 46 | static void mem_close(TSK_IMG_INFO *img_info) { |
50 | 46 | IMG_MEM_INFO *mem_info = reinterpret_cast<IMG_MEM_INFO *>(img_info); |
51 | 46 | free(mem_info); |
52 | 46 | } |
53 | | |
54 | 0 | static void mem_imgstat(TSK_IMG_INFO *, FILE *) {} |
55 | | |
56 | 46 | IMG_INFO *mem_open(const uint8_t *data, size_t size) { |
57 | 46 | IMG_MEM_INFO *inmemory_img = |
58 | 46 | reinterpret_cast<IMG_MEM_INFO *>(malloc(sizeof(IMG_MEM_INFO))); |
59 | 46 | TSK_IMG_INFO *img; |
60 | 46 | if (inmemory_img == nullptr) { |
61 | 0 | return nullptr; |
62 | 0 | } |
63 | | |
64 | 46 | inmemory_img->data = data; |
65 | 46 | inmemory_img->size = size; |
66 | | |
67 | 46 | auto base = &(inmemory_img->img_info.img_info); |
68 | 46 | base->itype = TSK_IMG_TYPE_RAW; |
69 | 46 | base->size = size; |
70 | 46 | base->sector_size = 512; |
71 | | |
72 | 46 | inmemory_img->img_info.read = mem_read; |
73 | 46 | inmemory_img->img_info.close = mem_close; |
74 | 46 | inmemory_img->img_info.imgstat = mem_imgstat; |
75 | | |
76 | 46 | inmemory_img->img_info.cache = new LegacyCache(); |
77 | 46 | inmemory_img->img_info.cache_read = tsk_img_read_legacy; |
78 | | |
79 | 46 | return (IMG_INFO*)inmemory_img; |
80 | 46 | } |
81 | | |
82 | | #endif // # MEM_IMG_H |