Created
March 24, 2013 14:00
-
-
Save steos/5232080 to your computer and use it in GitHub Desktop.
embedding blobs into executable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include <math.h> | |
extern char _binary_resources_tar_start; | |
extern char _binary_resources_tar_end; | |
#define BLOCK_SIZE 512 | |
typedef struct { | |
char name[100]; | |
char mode[8]; | |
char uid[8]; | |
char gid[8]; | |
char size[12]; | |
} header_t; | |
int main(void) { | |
char *offset = &_binary_resources_tar_start; | |
while (*offset != '\0') { | |
header_t *entry = (header_t*)offset; | |
long int size = strtol(entry->size, NULL, 8); | |
printf("%s: %ld bytes\n", entry->name, size); | |
for (int i = 0; i < size; ++i) { | |
fputc(offset[BLOCK_SIZE + i], stdout); | |
} | |
int blocks = size <= 512 ? 1 : ceil(size / 512.0); | |
offset += BLOCK_SIZE * (blocks + 1); | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
foobar | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lorem | |
ipsum | |
dolor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override CFLAGS += -Wall -Wextra -std=c99 -pedantic | |
.PHONY: all | |
all: demo | |
.PHONY: clean | |
clean: | |
rm *.o *.tar demo | |
demo.o: demo.c | |
demo: resources.o -lm | |
resources.o: resources.tar | |
ld -r -b binary -o $@ $^ | |
resources.tar: foobar.txt lorem.txt | |
tar -cf $@ $^ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment