/*******************************************************************
  Simple program to extract data from DHP (DHP Dr. Halo PIC )
  data format.

  rdwrmem.c       - main and only file

  (C) Copyright 2010 by Pavel Pisa
      e-mail:   pisa@cmp.felk.cvut.cz
      homepage: http://cmp.felk.cvut.cz/~pisa
      work:     http://www.pikron.com/
      license:  any combination GPL, LGPL, MPL or BSD licenses

 *******************************************************************/

#define _GNU_SOURCE

#include <sys/mman.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <malloc.h>
#include <string.h>
#include <byteswap.h>
#include <getopt.h>
#include <inttypes.h>
#include <errno.h>

int main(int argc, char *argv[])
{
  int dhp_fd, fd;
  const char *dhp_fname;
  off_t dhp_size;
  off_t obj_start;
  off_t obj_size;
  off_t obj_first=0;
  unsigned int dhp_ver;
  int res;
  unsigned long pagesize;
  const unsigned char *dhp, *p, *r, *s, *o;

  if(argc<2) {
        fprintf(stderr,"%s: the DHP file name required\n",argv[0]);
  }


  dhp_fname=argv[1];

  dhp_fd = open(dhp_fname, O_RDONLY);
  if (fd < 0) {
    fprintf(stderr,"%s: cannot open %s\n",argv[0],dhp_fname);
    return 1;
  }

  dhp_size=lseek(dhp_fd, 0, SEEK_END);
  if(dhp_size==(off_t)-1) {
    fprintf(stderr,"%s: seek failed on file %s\n",argv[0],dhp_fname);
    return 1;
  }
  lseek(dhp_fd, 0, SEEK_SET);

  pagesize=getpagesize();

  dhp = mmap(NULL, dhp_size, PROT_READ, MAP_SHARED, dhp_fd, 0);

  if (dhp == MAP_FAILED) {
    fprintf(stderr,"%s: mmap error\n",argv[0]);
    return 1;
  }

  p = dhp;
  dhp_ver=*(p++);
  dhp_ver|=*(p++)<<8;
  printf("%s: DHP file \"%s\" starts with 0x%02x 0x%02x\n",
         argv[0], dhp_fname, dhp_ver&0xff, dhp_ver>>8);

  while(!obj_first||(p-dhp<obj_first-6)) {
    obj_start = *(p++);
    obj_start |= *(p++) << 8;
    obj_start |= (uint32_t)*(p++) << 16;
    obj_start |= (uint32_t)*(p++) << 24;
    if(!obj_start)
      break;

    r=memchr(p, 0x00, dhp_size-(p-dhp));
    if(r==NULL)
      break;
    s=memrchr(p, '/', r-p);
    if(s==NULL)
      s=memrchr(p, '\\', r-p);
    if(s!=NULL){
      fprintf(stderr,"%s: not local directory path \"%s\"\n",argv[0],p);
    }else{
      s=p;
    }
    p+=13;
    if(p<=r)
      p=r+1;

    o=dhp+obj_start;

    obj_size = *(o++);
    obj_size |= *(o++) << 8;
    obj_size |= (uint32_t)*(o++) << 16;
    obj_size |= (uint32_t)*(o++) << 24;

    printf("opening file \"%s\" for object on offset 0x%ld and size 0x%ld\n",
           s, (long)obj_start, (long)obj_size);

    if(obj_start+obj_size+4>dhp_size){
      fprintf(stderr,"%s: file \"%s\" location exceed container size\n",argv[0],s);
    }

    fd=open((const char*)s,O_CREAT|O_TRUNC|O_WRONLY,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
    if(fd==-1){
      fprintf(stderr,"%s: create file \"%s\" failed %s\n",argv[0],s,strerror(errno));
    }

    res=write(fd,o,obj_size);
    if(res<=0){
      fprintf(stderr,"%s: write to file \"%s\" failed %s\n",argv[0],s,strerror(errno));
    }
    close(fd);

  }

  return 0;
}
