Skip to content

Instantly share code, notes, and snippets.

@steos
Created March 24, 2013 14:00
Show Gist options
  • Save steos/5232080 to your computer and use it in GitHub Desktop.
Save steos/5232080 to your computer and use it in GitHub Desktop.
embedding blobs into executable
#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;
}
lorem
ipsum
dolor
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