-
Notifications
You must be signed in to change notification settings - Fork 16
/
img2.c
154 lines (123 loc) · 3.76 KB
/
img2.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <zlib.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"
#include "abstractfile.h"
#include "img2.h"
void flipImg2Header(Img2Header* header) {
FLIPENDIANLE(header->signature);
FLIPENDIANLE(header->imageType);
FLIPENDIANLE(header->unknown1);
FLIPENDIANLE(header->security_epoch);
FLIPENDIANLE(header->flags1);
FLIPENDIANLE(header->dataLen);
FLIPENDIANLE(header->dataLenPadded);
FLIPENDIANLE(header->unknown3);
FLIPENDIANLE(header->flags2);
FLIPENDIANLE(header->unknown4);
FLIPENDIANLE(header->header_checksum);
FLIPENDIANLE(header->checksum2);
}
size_t readImg2(AbstractFile* file, void* data, size_t len) {
InfoImg2* info = (InfoImg2*) (file->data);
memcpy(data, (void*)((uint8_t*)info->buffer + (uint32_t)info->offset), len);
info->offset += (size_t)len;
return len;
}
size_t writeImg2(AbstractFile* file, const void* data, size_t len) {
InfoImg2* info = (InfoImg2*) (file->data);
while((info->offset + (size_t)len) > info->header.dataLen) {
info->header.dataLen = info->offset + (size_t)len;
info->buffer = realloc(info->buffer, info->header.dataLen);
}
memcpy((void*)((uint8_t*)info->buffer + (uint32_t)info->offset), data, len);
info->offset += (size_t)len;
info->dirty = TRUE;
return len;
}
int seekImg2(AbstractFile* file, off_t offset) {
InfoImg2* info = (InfoImg2*) (file->data);
info->offset = (size_t)offset;
return 0;
}
off_t tellImg2(AbstractFile* file) {
InfoImg2* info = (InfoImg2*) (file->data);
return (off_t)info->offset;
}
off_t getLengthImg2(AbstractFile* file) {
InfoImg2* info = (InfoImg2*) (file->data);
return info->header.dataLen;
}
void closeImg2(AbstractFile* file) {
InfoImg2* info = (InfoImg2*) (file->data);
uint32_t cksum;
if(info->dirty) {
info->file->seek(info->file, sizeof(info->header));
info->file->write(info->file, info->buffer, info->header.dataLen);
info->header.dataLenPadded = info->header.dataLen;
flipImg2Header(&(info->header));
cksum = crc32(0, (unsigned char *)&(info->header), 0x64);
FLIPENDIANLE(cksum);
info->header.header_checksum = cksum;
info->file->seek(info->file, 0);
info->file->write(info->file, &(info->header), sizeof(info->header));
}
free(info->buffer);
info->file->close(info->file);
free(info);
free(file);
}
AbstractFile* createAbstractFileFromImg2(AbstractFile* file) {
InfoImg2* info;
AbstractFile* toReturn;
if(!file) {
return NULL;
}
info = (InfoImg2*) malloc(sizeof(InfoImg2));
info->file = file;
file->seek(file, 0);
file->read(file, &(info->header), sizeof(info->header));
flipImg2Header(&(info->header));
if(info->header.signature != IMG2_SIGNATURE) {
free(info);
return NULL;
}
info->buffer = malloc(info->header.dataLen);
file->read(file, info->buffer, info->header.dataLen);
info->dirty = FALSE;
info->offset = 0;
toReturn = (AbstractFile*) malloc(sizeof(AbstractFile));
toReturn->data = info;
toReturn->read = readImg2;
toReturn->write = writeImg2;
toReturn->seek = seekImg2;
toReturn->tell = tellImg2;
toReturn->getLength = getLengthImg2;
toReturn->close = closeImg2;
toReturn->type = AbstractFileTypeImg2;
return toReturn;
}
AbstractFile* duplicateImg2File(AbstractFile* file, AbstractFile* backing) {
InfoImg2* info;
AbstractFile* toReturn;
if(!file) {
return NULL;
}
info = (InfoImg2*) malloc(sizeof(InfoImg2));
memcpy(info, file->data, sizeof(InfoImg2));
info->file = backing;
info->buffer = malloc(1);
info->header.dataLen = 0;
info->dirty = TRUE;
info->offset = 0;
toReturn = (AbstractFile*) malloc(sizeof(AbstractFile));
toReturn->data = info;
toReturn->read = readImg2;
toReturn->write = writeImg2;
toReturn->seek = seekImg2;
toReturn->tell = tellImg2;
toReturn->getLength = getLengthImg2;
toReturn->close = closeImg2;
toReturn->type = AbstractFileTypeImg2;
return toReturn;
}