Rewrite unimg to compile with GCC (-60% of size).

git-svn-id: svn://kolibrios.org@7868 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
Magomed Kostoev (mkostoevr) 2020-05-05 18:38:41 +00:00
parent e4b948b955
commit fa23477ae5
3 changed files with 45 additions and 35 deletions

View File

@ -3,7 +3,6 @@
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <conio.h>
typedef struct { typedef struct {
size_t length; size_t length;
@ -54,8 +53,8 @@ static void mkdir(const char *name) {
int fn; int fn;
int unused[4]; int unused[4];
char b; char b;
const char *path __attribute__((packed)); const char *path;
} info; } __attribute__((packed)) info;
memset(&info, 0, sizeof(info)); memset(&info, 0, sizeof(info));
info.fn = 9; info.fn = 9;
info.b = 0; info.b = 0;
@ -329,16 +328,16 @@ static int fat12__open(Fat12 *this, const char *img) {
this->rootDirectory = this->firstFat + this->numberOfFats this->rootDirectory = this->firstFat + this->numberOfFats
* this->sectorsPerFat * this->bytesPerSector; * this->sectorsPerFat * this->bytesPerSector;
this->dataRegion = this->rootDirectory + this->maxRootEntries * 32; this->dataRegion = this->rootDirectory + this->maxRootEntries * 32;
con_printf("\nBytes per sector: %d\n", this->bytesPerSector); printf("\nBytes per sector: %d\n", this->bytesPerSector);
con_printf("Sectors per claster: %d\n", this->sectorsPerClaster); printf("Sectors per claster: %d\n", this->sectorsPerClaster);
con_printf("Reserver sector count: %d\n", this->reservedSectorCount); printf("Reserver sector count: %d\n", this->reservedSectorCount);
con_printf("Number of FATs: %d\n", this->numberOfFats); printf("Number of FATs: %d\n", this->numberOfFats);
con_printf("Max root entries: %d\n", this->maxRootEntries); printf("Max root entries: %d\n", this->maxRootEntries);
con_printf("Total sectors: %d\n", this->totalSectors); printf("Total sectors: %d\n", this->totalSectors);
con_printf("Sectors per FAT: %d\n", this->sectorsPerFat); printf("Sectors per FAT: %d\n", this->sectorsPerFat);
con_printf("First FAT: %d\n", this->firstFat); printf("First FAT: %d\n", this->firstFat);
con_printf("Root directory: %d\n", this->rootDirectory); printf("Root directory: %d\n", this->rootDirectory);
con_printf("Data region: %d\n\n", this->dataRegion); printf("Data region: %d\n\n", this->dataRegion);
return 1; return 1;
} }
@ -348,13 +347,34 @@ static int fat12__error(Fat12 *this, const char *errorMessage) {
} }
static int handleError(const Fat12 *fat12) { static int handleError(const Fat12 *fat12) {
con_printf("Error in Fat12: %s\n", fat12->errorMessage); printf("Error in Fat12: %s\n", fat12->errorMessage);
con_exit(0);
return -1; return -1;
} }
void writeFile(const char *fileName, int size, const uint8_t *data) {
// FILE *fp = NULL;
// if (!(fp = fopen(fileName, "wb"))) { perror(NULL); }
// fwrite(data, 1, size, fp);
// fclose(fp);
struct Info {
int number;
int reserved0;
int reserved1;
int dataSize;
const void *data;
char zero;
const char *name;
} __attribute__((packed)) *info = calloc(sizeof(struct Info), 1);
info->number = 2; // create/overwrite file
info->dataSize = size;
info->data = data;
info->zero = 0;
info->name = fileName;
asm volatile ("int $0x40" :: "a"(70), "b"(info));
}
static int callback(const char *name, size_t size, const uint8_t *data, void *param) { static int callback(const char *name, size_t size, const uint8_t *data, void *param) {
FILE *fp = NULL;
String *outputPath = param; String *outputPath = param;
while (outputPath->capacity < outputPath->length + strlen(name) + 1 + 1) { while (outputPath->capacity < outputPath->length + strlen(name) + 1 + 1) {
@ -373,10 +393,8 @@ static int callback(const char *name, size_t size, const uint8_t *data, void *pa
*fileNameDelim = '/'; *fileNameDelim = '/';
} }
} }
con_printf("Extracting %s\n", outputPath->data); printf("Extracting %s\n", outputPath->data);
if (!(fp = fopen(outputPath->data, "wb"))) { perror(NULL); } writeFile(outputPath->data, size, data);
fwrite(data, 1, size, fp);
fclose(fp);
outputPath->data[outputPath->length] = '\0'; outputPath->data[outputPath->length] = '\0';
return 0; return 0;
} }
@ -387,20 +405,14 @@ int main(int argc, char **argv) {
Fat12 fat12 = { 0 }; Fat12 fat12 = { 0 };
char *imageFile = NULL; char *imageFile = NULL;
String outputFolder = { 0 }; String outputFolder = { 0 };
int exit = 0;
char app_title[] = "UnImg - kolibri.img file unpacker";
if (con_init_console_dll_param(-1, -1, -1, 350, app_title)) return -1;
if (argc < 2) { if (argc < 2) {
con_write_asciiz(" Usage:\n"); puts(" Usage:\n");
con_write_asciiz(" unimg \"/path/to/kolibri.img\" \"/optional/extract/path\" [-e]\n"); puts(" unimg \"/path/to/kolibri.img\" \"/optional/extract/path\"\n");
con_write_asciiz(" where optional key [-e] is exit on success");
con_exit(0);
return -1; return -1;
} else { } else {
imageFile = argv[1]; imageFile = argv[1];
con_printf("File: %s\n", imageFile); printf("File: %s\n", imageFile);
} }
@ -416,9 +428,6 @@ int main(int argc, char **argv) {
outputFolder.length = strlen(outputFolder.data); outputFolder.length = strlen(outputFolder.data);
// handle -e parameter - exit on success
if (argc >= 3 && !strcmp(argv[argc - 1], "-e")) { exit = 1; }
if (!fat12__open(&fat12, imageFile)) { if (!fat12__open(&fat12, imageFile)) {
return handleError(&fat12); return handleError(&fat12);
} }
@ -427,6 +436,5 @@ int main(int argc, char **argv) {
return handleError(&fat12); return handleError(&fat12);
} }
con_write_asciiz("\nDONE!\n\n"); puts("\nDONE!\n");
con_exit(exit);
} }

View File

@ -1,2 +1,4 @@
kos32-tcc fat12.c -lck -o unimg gcc -m32 -c -fomit-frame-pointer -IC:\Users\Boppan\Documents\KolibriOS\contrib/sdk/sources/newlib/libc/include fat12.c -o unimg.o -Wall -Wextra
kos32-ld unimg.o -o unimg -static -S -nostdlib -T C:\Users\Boppan\Documents\KolibriOS\contrib/sdk/sources/newlib/app.lds --image-base 0 -L "C:\Program Files (x86)\kos32-msys-5.4.0\win32\lib" -lgcc -lapp -lc.dll
kos32-objcopy unimg -O binary
@pause @pause

Binary file not shown.