home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Boxer / PalmBoxer / unbox.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  2.7 KB  |  139 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <fcntl.h>
  5. #include <time.h>
  6.  
  7. #ifndef O_BINARY
  8. #define O_BINARY 0
  9. #endif
  10.  
  11. #ifndef __GNUC__
  12. unsigned long htonl(unsigned long x)
  13. {
  14.   unsigned char *cp, *dp;
  15.   unsigned long y;
  16.   dp = &y;
  17.   cp = &x;
  18.   dp += 3;
  19.   *dp-- = *cp++;
  20.   *dp-- = *cp++;
  21.   *dp-- = *cp++;
  22.   *dp-- = *cp++;
  23.   return y;
  24. }
  25.  
  26. unsigned short htons(unsigned short x)
  27. {
  28.   unsigned char *cp, *dp;
  29.   unsigned short y;
  30.   dp = &y;
  31.   cp = &x;
  32.   dp += 1;
  33.   *dp-- = *cp++;
  34.   *dp-- = *cp++;
  35.   return y;
  36. }
  37. #else
  38. #include <unistd.h>
  39. #include <sys/types.h>
  40. #include <netinet/in.h>
  41. #endif
  42.  
  43.  
  44. #pragma pack(2)
  45.  
  46. struct prchead {
  47.   char name[32];                //0-32
  48.   short int attr;               //32-33
  49.   short int vers;               //34-35
  50.   long int cr, md, bkt;         //times 36-47
  51.   long int mn, app, sort;       // zero 48-59 - zero for prcs.
  52.   long int type, crea;          //60-67
  53.   long int uidseed, nxrec;      //68-75 - uidseed rand, nxrec zero;
  54.   short int nrecs;              //76-78
  55. } head;
  56.  
  57. int main(int argc, char *argv[])
  58. {
  59.  
  60.   unsigned long cofst, xlong, xlong2;
  61.   unsigned char *bbuf;
  62.   long bbuflen;
  63.   int outfd, infd, i, maxsect, argp;
  64.   char title[128];
  65.  
  66.   if (argc < 2) {
  67.     fprintf(stderr, "Usage: %s prcfile\n", argv[0]);
  68.     exit(1);
  69.   }
  70.  
  71.   for (argp = 1; argp < argc; argp++) {
  72.  
  73.     if ((infd = open(argv[argp], O_BINARY | O_RDONLY)) < 1) {
  74.       fprintf(stderr, "Can't open file %s\n", argv[argp]);
  75.       exit(-2);
  76.     }
  77.  
  78.     if (!(bbuf = malloc(8400)))
  79.       exit(-3);
  80.  
  81.     read(infd, &head, sizeof(head));
  82.     read(infd, &xlong, 4);
  83.  
  84.     maxsect = htons(head.nrecs);
  85.  
  86.     cofst = htonl(xlong);
  87.  
  88.     strcpy(title, argv[1]);
  89.  
  90.     if (!strcmp(&title[strlen(title) - 4], ".pdb"))
  91.       title[strlen(title) - 4] = 0;
  92.     else
  93.       strcat(title, ".out");
  94.  
  95.     fprintf(stderr, "%s -> %s\n", argv[argp], title);
  96.  
  97.     outfd = open(title, O_BINARY | O_RDWR | O_CREAT, 0644);
  98.  
  99.     for (i = 0; i < maxsect; i++) {
  100.  
  101.       read(infd, title, 4);
  102.  
  103.       if (i + 1 != maxsect) {
  104.  
  105.         read(infd, &xlong2, 4);
  106.  
  107.         bbuflen = htonl(xlong2);
  108.         bbuflen = bbuflen - cofst;
  109.       } else
  110.         bbuflen = 4096 + 8;
  111.  
  112.       if (bbuflen > 8400 || bbuflen < 8)
  113.         exit(-3);
  114.  
  115.       xlong = lseek(infd, 0, SEEK_CUR);
  116.       lseek(infd, cofst, SEEK_SET);
  117.  
  118.       read(infd, bbuf, 4);
  119.       if (strncmp(bbuf, "DBLK", 4))
  120.         exit(-2);
  121.       read(infd, &cofst, 4);
  122.       bbuflen -= 8;
  123.       bbuflen = read(infd, bbuf, bbuflen);
  124.  
  125.       if (i != maxsect - 1 && htonl(cofst) != bbuflen)
  126.         fprintf(stderr, "Length mismatch %d v.s. %ld\n", htonl(cofst), bbuflen);
  127.  
  128.       lseek(infd, xlong, SEEK_SET);
  129.       cofst = htonl(xlong2);
  130.       write(outfd, bbuf, bbuflen);
  131.  
  132.     }
  133.  
  134.     close(outfd);
  135.   }
  136.  
  137.   return 0;
  138. }
  139.