Binary Storing Data in Itself

Here we discussed clients, mappers, tunnels and their settings.
Posts: 393
Joined: Mon Feb 25, 2008 10:02 pm

Binary Storing Data in Itself

Post by Wobbler »

This is technically a question about C rather than a client-specific question...
If I want to write a programme that stores some data in its own binary rather than an external file, how do I go about it?

Here is an example of a programme, my mume-time offline reference:

Code: Select all

#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>

int oldSysTime, newSysTime, timeDiff;
int tMonth, tDay, tHour, tMin;
FILE *storage;

void error(void)
  printf ("#print (\"Clock error! Try giving HELP as argument.\")\n");
  exit (1);

void mclockHelp(void)
  exit (0);

void mclockGet(void) {
  if ((storage = fopen ("/home/mikaels/mud/powwow-1.2.13/scripts/", "r+")) == NULL) {
    printf ("#print (\"Error: could not open scripts/ for reading!\")\n");
    exit (1);
  fscanf (storage, "%d %d %d %d %d", &oldSysTime, &tMonth, &tDay, &tHour, &tMin);
  fclose (storage);
  timeDiff = newSysTime - oldSysTime;

  tMin += timeDiff;

  while (tMin > 59) {
    tMin -= 60;
  while (tHour > 23) {
    tHour -= 24;
  while (tDay > 30) {
    tDay -= 30;
  if (tMonth > 12) tMonth %= 12;

  printf ("#(@timeMonth=%d, @timeDay=%d, @timeHr=%d, @timeMi=%d);clock;#in clockTimer (800) clockIncrease\n", tMonth, tDay, tHour, tMin);
  exit (0);

void mclockStore(char* arg2, char* arg3, char* arg4, char* arg5) {
  if ((tMonth = atoi (arg2)) > 12) error();
  if ((tDay = atoi (arg3)) > 30) error();
  if ((tHour = atoi (arg4)) > 23) error();
  if ((tMin = atoi (arg5)) > 59) error();

  if ((storage = fopen ("scripts/", "w")) == NULL) {
    printf ("#print (\"Error: could not open scripts/ for writing!\")\n");
    exit (1);
  fprintf (storage, "%d %d %d %d %d", newSysTime, tMonth, tDay, tHour, tMin);
  fclose (storage);
  exit (0);

int main (int argc, char* argv[]) {
  if (argc < 1) error();

  newSysTime = time (NULL);

  if (strcmp (argv[1], "HELP") == 0) mclockHelp();
  if (strcmp (argv[1], "GET") == 0) mclockGet();
  if (strcmp (argv[1], "STORE") == 0) {
    if (argc != 6) error();
    mclockStore(argv[2], argv[3], argv[4], argv[5]);

  return 0;
I would like the resulting binary to store the reference in itself rather than in the external
(Optimisation suggestions for the current time calculation in mclockGet () are also welcome.)
Politicians are wise and benevolent and will gladly sacrifice themselves for the common good. Anyone who implies that there might exist dishonest politicians is obviously a terrorist who should be locked up indefinitely without a trial.
Posts: 478
Joined: Sun Feb 24, 2008 5:02 pm
Location: Italy

Re: Binary Storing Data in Itself

Post by Drugo »

In my experience is not always a good idea to store data in an executable file, unless you have some kind of support from target OS/Compiler.
Under windows you can use resources and under MacOS you can use resources too (write to resource fork of a file)
I don't know if under linux/unix there is support for resources too.

A trivial way could be to check for a your own pattern of bytes in the file. This pattern means 'beginning of stored data'. If the pattern does not exist, you append to executable the pattern and data, if the pattern exist you just update data.
Data formatting is up to you (with resources you have APIs to do so)

Try to create a 'very' unique pattern!
Posts: 34
Joined: Sun Feb 24, 2008 3:36 am
Location: California

Re: Binary Storing Data in Itself

Post by Slippy »

It might be helpful to look into the disassembly of your program as well. And if you ARE running a nix OS you may want to look at some pages linked to from ... ble_Format

Also if this is just for a small toy program, you may want to look into assembly. Even if you don't write your program in it, seeing how it lays out the final binary is really interesting/useful.

EDIT: You can call me the necromancer because I definitely just RESURRECTIFIED this thread!