-
Notifications
You must be signed in to change notification settings - Fork 1
/
frame.c
35 lines (24 loc) · 876 Bytes
/
frame.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
#include "parse.h"
#include <stdint.h>
#include <stdio.h>
int parse_frame(struct shdr_table *shdr, struct FRAME *frame, FILE *fh, const char *section_name){
frame->frame_index = get_section_index(shdr, section_name);
if(frame->frame_index == (uint8_t)NOT_FOUND) return NOT_FOUND;
frame->frame_size = shdr->shdr_table[frame->frame_index].sh_size;
frame->frame_offset = shdr->shdr_table[frame->frame_index].sh_offset;
frame->buffer = malloc(frame->frame_size);
if(!frame->buffer){
perror("Memory allocation error\n");
return -1;
}
memset(frame->buffer, 0, frame->frame_size);
fseek(fh, frame->frame_offset, SEEK_SET);
if(fread(frame->buffer, 1, frame->frame_size, fh) < frame->frame_size){
perror("error reading file");
return -1;
}
for(int i = 0; i < frame->frame_size; i++){
putchar(frame->buffer[i]);
}
return 0;
}