Coverage Report

Created: 2024-04-23 06:05

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