-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.c
51 lines (45 loc) · 1.17 KB
/
common.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//
// Created by lauwsj on 3/4/23.
//
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <unistd.h>
#include "common.h"
void get_disk_image_mmap(const char *diskimg_path, off_t *size,
uint8_t **image) {
int fd = open(diskimg_path, O_RDONLY);
if (fd < 0) {
perror("open");
exit(EXIT_FAILURE);
}
// get file length
(*size) = lseek(fd, 0, SEEK_END);
if ((*size) == -1) {
perror("lseek");
exit(EXIT_FAILURE);
}
(*image) = mmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0);
if ((*image) == (void *)-1) {
close(fd);
perror("mmap");
exit(EXIT_FAILURE);
}
close(fd);
}
uint32_t convert_sector_to_byte_offset(const struct BPB *hdr,
uint32_t sector_number) {
return sector_number * hdr->BPB_BytsPerSec;
}
void hexdump(const void *data, size_t size) {
#warning "You must remove this function before submitting."
FILE *proc;
proc = popen("hexdump -C", "w");
if (proc == NULL) {
perror("popen");
exit(EXIT_FAILURE);
}
fwrite(data, 1, size, proc);
}