home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / palmos / pilot-frotz-src-1.00r4.tar.gz / pilot-frotz-src-1.00r4.tar / pilot-frotz-1.00r4 / Z2PDB.c < prev    next >
C/C++ Source or Header  |  1998-10-26  |  8KB  |  217 lines

  1.  
  2. //**********
  3. //*  Serial Number: 981026-163958
  4. //* Version Number:   0001.77
  5. //**********
  6.  
  7. #define SERIAL_NUM   "981026-163958"
  8. #define VERSION_NUM  "0001.77"
  9.  
  10. // Check the endian of your machine as this is set for LSB-MSB order
  11. // to change the edian just comment out this define
  12. #define ENDIAN_LSB_MSB
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17.  
  18. typedef unsigned char ubyte;
  19. typedef signed char sbyte;
  20. typedef unsigned short uword;
  21. typedef short word;
  22. typedef unsigned long udword;
  23. typedef long dword;
  24.  
  25. //------------------------------------------------------------------------------------------------------------
  26.  
  27. #ifndef __GNUC__
  28. #pragma pack(2)
  29. #endif
  30.  
  31. #define PDBHEADER struct pdbheader_struct
  32. struct pdbheader_struct{
  33.     ubyte name[32];                // String - This is the name of the database on the PalmPilot device.
  34.                                 //  It need not match the name of the PDB file in the environment in which it is created.
  35.     uword fileAttributes;        // Attributes of the pdb file.
  36.                                 //  0x0002 Read-Only
  37.                                 //  0x0004 Dirty AppInfoArea
  38.                                 //  0x0008 Backup this database (i.e. no conduit exists)
  39.                                 //  0x0010 (16 decimal) Okay to install newer over existing copy, if present on PalmPilot
  40.                                 //  0x0020 (32 decimal) Force the PalmPilot to reset after this database is installed
  41.     uword version;                // Defined by the application.
  42.     udword creationDate;        // Expressed as the number of seconds since January 1, 1904.
  43.                                 //  The database will not install if this value is zero. (PalmOS 1.0.6)
  44.     udword modificationDate;    // Expressed as the number of seconds since January 1, 1904.
  45.                                 //  The database will not install if this value is zero. (PalmOS 1.0.6)
  46.     udword lastBackupDate;        // Expressed as the number of seconds since January 1, 1904.
  47.                                 //  This can be left at zero and the database will install.
  48.     udword modificationNumber;    // Set to zero.
  49.     udword appInfoArea;            // The byte number in the PDB file (counting from zero) at which the AppInfoArea is located.
  50.                                 //  This must be the first entry in the Data portion of the PDB file.
  51.                                 //  If this database does not have an AppInfoArea, set this value to zero.
  52.     udword sortInfoArea;        // The byte number in the PDB file (counting from zero) at which the SortInfoArea is
  53.                                 //  located. This must be placed immediately after the AppInfoArea, if one exists,
  54.                                 //  within the Data portion of the PDB file. If this database does not have a SortInfoArea,
  55.                                 //  set this value to zero. Do not use this. See Note C below for further details.
  56.     ubyte databaseType[4];        // String - Set this to the desired value. Generally it should match the Database Type
  57.                                 //  used by the corresponding application This is 4 characters long and does not have a
  58.                                 //  terminating null.
  59.     ubyte creatorID[4];            // String - Set this to the desired value. Generally it should match the Creator ID used
  60.                                 //  by the corresponding application. In all cases, you should always register your
  61.                                 //  Creator ID before using it. This is 4 characters long and does not have a terminating
  62.                                 //  null.
  63.     udword uniqueIDSeed;        // This is used to generate the Unique ID number of subsequent records. Generally,
  64.                                 //  this should be set to zero.
  65.     udword nextRecordListID;    // Set this to zero. (This does not appear to be used, but that has not been verified by
  66.                                 //  a third party.)
  67.     uword numberOfRecords;        // This contains the number of records
  68. }
  69. #ifdef __GNUC__
  70. __attribute__ ((packed))
  71. #endif
  72. ;
  73.  
  74. //------------------------------------------------------------------------------------------------------------
  75.  
  76. #define RECHEADER struct recheader_struct
  77. struct recheader_struct{
  78.     udword recordDataOffset;    // The byte number in the PDB file (counting from zero) at which the record is located.
  79.     ubyte recordAttributes;        // The records attributes.
  80.                                  //  0x10 (16 decimal) Secret record bit.
  81.                                 //  0x20 (32 decimal) Record in use (busy bit).
  82.                                 //  0x40 (64 decimal) Dirty record bit.
  83.                                 //  0x80 (128, unsigned decimal) Delete record on next HotSync.
  84.                                 //  The least significant four bits are used to represent the category values.
  85.     ubyte uniqueID[3];            // Set this to zero and do not try to second-guess what PalmOS will do with this value.
  86. }
  87. #ifdef __GNUC__
  88. __attribute__ ((packed))
  89. #endif
  90. ;
  91.  
  92. //------------------------------------------------------------------------------------------------------------
  93.  
  94. void WriteWord(void* addr,uword data){
  95.  
  96.     ubyte* to = (ubyte*)addr;
  97.     ubyte* from = (ubyte*)&data;
  98.  
  99. #ifdef ENDIAN_LSB_MSB
  100.     to[0] = from[1];
  101.     to[1] = from[0];
  102. #else
  103.     to[0] = from[0];
  104.     to[1] = from[1];
  105. #endif
  106. }
  107.  
  108. //------------------------------------------------------------------------------------------------------------
  109.  
  110. void WriteDWord(void* addr,udword data){
  111.  
  112.     ubyte* to = (ubyte*)addr;
  113.     ubyte* from = (ubyte*)&data;
  114.  
  115. #ifdef ENDIAN_LSB_MSB
  116.     to[0] = from[3];
  117.     to[1] = from[2];
  118.     to[2] = from[1];
  119.     to[3] = from[0];
  120. #else
  121.     to[0] = from[0];
  122.     to[1] = from[1];
  123.     to[2] = from[2];
  124.     to[3] = from[3];
  125. #endif
  126. }
  127.  
  128. //------------------------------------------------------------------------------------------------------------
  129.  
  130. void main(int argc,char *argv[]){
  131.  
  132.     FILE* fpo;
  133.     FILE* fpi;
  134.     dword c;
  135.     PDBHEADER pdbHeader;
  136.     RECHEADER recHeader;
  137.     dword save;
  138.     dword length;
  139.     ubyte* zip;
  140.     uword records;
  141.  
  142.     printf("Z2PDB: V%s. Builds a FROTZ database for the PalmIII/Pilot.\n",VERSION_NUM);
  143.     printf(" By P.D.Margrave (C) King of the Jungle Ltd. SN:%s\n\n",SERIAL_NUM);
  144.     printf("  USAGE: Z2PDB <FROTZ PDB File> <Zip File> <Title>\n");
  145.     printf("\n");
  146.  
  147.     // do command line parsing
  148.  
  149.     if(argc < 4){
  150.         printf("ERROR: Not enough parameters\n");
  151.         exit(1);
  152.     }
  153.  
  154.     if(fpo = fopen(argv[1],"wb")){
  155.         printf("Writing PDB file \"%s\" - %s\n",argv[3],argv[1]);
  156.  
  157.         if(fpi = fopen(argv[2],"rb")){
  158.             printf("Inserting ZIP file - %s\n",argv[2]);
  159.             save = ftell(fpi);
  160.             fseek(fpi,0,SEEK_END);
  161.             length = ftell(fpi);
  162.             fseek(fpi,save,SEEK_SET);
  163.  
  164.             length = (length + 4095) & 0xfffff000;
  165.  
  166.             zip = malloc(length);
  167.             fread(zip,length,1,fpi);
  168.             records = (length / 4096);
  169.  
  170.             // make header
  171.             memset(&pdbHeader,0,sizeof(PDBHEADER));
  172.             if(strlen(argv[3]) > 31){
  173.                 *(argv[3] + 31) = 0;        // fix title width
  174.             }
  175.             strcpy(pdbHeader.name,argv[3]);
  176.             WriteWord(&(pdbHeader.fileAttributes),0x0000);
  177.             WriteWord(&(pdbHeader.version),0x0001);
  178.             WriteDWord(&(pdbHeader.creationDate),0x34ac829f);
  179.             WriteDWord(&(pdbHeader.modificationDate),0x34ac829f);
  180.             WriteDWord(&(pdbHeader.lastBackupDate),0x00000000);
  181.             WriteDWord(&(pdbHeader.modificationNumber),0x00000000);
  182.             WriteDWord(&(pdbHeader.appInfoArea),0x00000000);
  183.             WriteDWord(&(pdbHeader.sortInfoArea),0x00000000);
  184.             pdbHeader.databaseType[0] = 'Z';
  185.             pdbHeader.databaseType[1] = 'C';
  186.             pdbHeader.databaseType[2] = 'O';
  187.             pdbHeader.databaseType[3] = 'D';
  188.             pdbHeader.creatorID[0] = 'F';
  189.             pdbHeader.creatorID[1] = 'o';
  190.             pdbHeader.creatorID[2] = 't';
  191.             pdbHeader.creatorID[3] = 'z';
  192.             WriteDWord(&(pdbHeader.uniqueIDSeed),0x00000000);
  193.             WriteDWord(&(pdbHeader.nextRecordListID),0x00000000);
  194.             WriteWord(&(pdbHeader.numberOfRecords),records);
  195.             fwrite(&pdbHeader,sizeof(PDBHEADER),1,fpo);
  196.  
  197.             for(c = 0;c < records;c++){
  198.                 WriteDWord(&(recHeader.recordDataOffset),(c * 4096) + sizeof(PDBHEADER) + records * sizeof(RECHEADER));
  199.                 recHeader.recordAttributes = 0x60;
  200.                 recHeader.uniqueID[0] = 0x00;
  201.                 recHeader.uniqueID[1] = 0x00;
  202.                 recHeader.uniqueID[2] = 0x00;
  203.                 fwrite(&recHeader,sizeof(RECHEADER),1,fpo);
  204.             }
  205.  
  206.             fwrite(zip,length,1,fpo);
  207.             free(zip);
  208.  
  209.             fclose(fpi);
  210.  
  211.         }
  212.         fclose(fpo);
  213.     }
  214. }
  215.  
  216. //------------------------------------------------------------------------------------------------------------
  217.