forked from ScaryTea/STM32F103
-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.c
61 lines (54 loc) · 1.52 KB
/
format.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
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
static uint8_t shifts[] = { 1, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7 };
static uint32_t fsize = 0;
static unsigned char* readFile(char* path);
int main(int argc, char *argv[]) {
/* unused info, probably will need this in future
uint32_t width = input[18];
uint32_t height = input[22];
*/
unsigned char* input = readFile(argv[1]);
uint32_t offset = input[10];
printf("offset: (%u)\n fsize = %d\n",offset,fsize);
unsigned char arr[6][84];
for(int i = 0; i < 6; i++)
for(int j = 0; j < 84; j++)
arr[i][j] = 0;
unsigned char a;
int i=0, j=83;
// v - vertical iterating
// k - iterating over bytes in 'strings' of 12 bytes
// bitpos - iterating over bits in byte
for(int v = 0; v < 48; v++, j=83, i=(int)(v/8)) {
for(int k = 1; k < 12; k++) {
a = input[fsize-1 - v*12 - k];
for(int bitpos = (k==1)?4:0; bitpos < 8; bitpos++, j--) {
if(j < 0) { printf("Smth broken\n"); break; }
arr[i][j] |= (!!(a & shifts[bitpos])) << v%8;
}
}
}
FILE* f = fopen("result", "w+");
for(i = 0; i < 6; i++)
for(j = 0; j < 84; j++) {
fwrite(&arr[i][j], sizeof(unsigned char), 1, f);
}
}
static unsigned char* readFile(char* path) {
FILE* f = fopen(path, "r");
if(f == NULL)
{
printf("Error openning the file\n");
return NULL;
}
fseek(f, 0, SEEK_END);
fsize = ftell(f);
rewind(f);
unsigned char* res = (unsigned char*) malloc(sizeof(unsigned char)*fsize);
fread(res, sizeof(unsigned char), fsize, f);
fclose(f);
return res;
}