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 
mem_read(TSK_IMG_INFO * img_info,TSK_OFF_T offset,char * buf,size_t len)29 static ssize_t mem_read(TSK_IMG_INFO *img_info, TSK_OFF_T offset, char *buf,
30                         size_t len) {
31   IMG_MEM_INFO *mem_info = reinterpret_cast<IMG_MEM_INFO *>(img_info);
32   // Bounds-checking exists in the real drivers.
33   if (offset > mem_info->size) {
34     return -1;
35   }
36   ssize_t read_len = len;
37   if (offset + len > mem_info->size) {
38     read_len = mem_info->size - offset;
39   }
40   if (memcpy(buf, mem_info->data + offset, read_len) == nullptr) {
41     return -1;
42   } else {
43     return read_len;
44   }
45 }
46 
mem_close(TSK_IMG_INFO * img_info)47 static void mem_close(TSK_IMG_INFO *img_info) {
48   IMG_MEM_INFO *mem_info = reinterpret_cast<IMG_MEM_INFO *>(img_info);
49   tsk_deinit_lock(&(img_info->cache_lock));
50   free(mem_info);
51 }
52 
mem_imgstat(TSK_IMG_INFO * img_info,FILE * hFile)53 static void mem_imgstat(TSK_IMG_INFO *img_info, FILE *hFile) {}
54 
mem_open(const uint8_t * data,size_t size)55 TSK_IMG_INFO *mem_open(const uint8_t *data, size_t size) {
56   IMG_MEM_INFO *inmemory_img =
57       reinterpret_cast<IMG_MEM_INFO *>(malloc(sizeof(IMG_MEM_INFO)));
58   TSK_IMG_INFO *img;
59   if (inmemory_img == nullptr) {
60     return nullptr;
61   }
62   img = reinterpret_cast<TSK_IMG_INFO *>(inmemory_img);
63   img->itype = TSK_IMG_TYPE_RAW;
64   img->read = mem_read;
65   img->close = mem_close;
66   img->imgstat = mem_imgstat;
67   img->size = size;
68   img->sector_size = 512;
69   tsk_init_lock(&(img->cache_lock));
70   inmemory_img->data = data;
71   inmemory_img->size = size;
72   return img;
73 }
74 
75 #endif // # MEM_IMG_H
76