PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
bin2c.c
1/*
2# _____ ___ ____ ___ ____
3# ____| | ____| | | |____|
4# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5#-----------------------------------------------------------------------
6# Copyright 2001-2004, ps2dev - http://www.ps2dev.org
7# Licenced under Academic Free License version 2.0
8# Review ps2sdk README & LICENSE files for further details.
9*/
10
11#include <sys/types.h>
12#include <sys/stat.h>
13#include <unistd.h>
14#include <fcntl.h>
15#include <stdlib.h>
16#include <stdio.h>
17
18unsigned char *buffer;
19
20int main(int argc, char *argv[])
21{
22 int fd_size;
23 FILE *source,*dest;
24 int i;
25
26 if(argc != 4) {
27 printf("bin2c - from bin2s By Sjeep\n"
28 "Usage: bin2c infile outfile label\n\n");
29 return 1;
30 }
31
32 if((source=fopen( argv[1], "rb")) == NULL) {
33 printf("Error opening %s for reading.\n",argv[1]);
34 return 1;
35 }
36
37 fseek(source,0,SEEK_END);
38 fd_size = ftell(source);
39 fseek(source,0,SEEK_SET);
40
41 buffer = malloc(fd_size);
42 if(buffer == NULL) {
43 printf("Failed to allocate memory.\n");
44 fclose(source);
45 return 1;
46 }
47
48 if(fread(buffer,1,fd_size,source) != fd_size) {
49 printf("Failed to read file.\n");
50 fclose(source);
51 return 1;
52 }
53 fclose(source);
54
55 if((dest = fopen(argv[2],"w+")) == NULL) {
56 printf("Failed to open/create %s.\n",argv[2]);
57 return 1;
58 }
59
60 fprintf(dest, "#ifndef __%s__\n", argv[3]);
61 fprintf(dest, "#define __%s__\n\n", argv[3]);
62 fprintf(dest, "unsigned int size_%s = %d;\n", argv[3], fd_size);
63 fprintf(dest, "unsigned char %s[] __attribute__((aligned(16))) = {", argv[3]);
64
65 for(i=0;i<fd_size;i+=1) {
66 if((i % 16) == 0) fprintf(dest, "\n\t");
67 fprintf(dest, "0x%02x, ", buffer[i]);
68 }
69
70 fprintf(dest, "\n};\n\n#endif\n");
71
72 fclose(dest);
73
74 return 0;
75}