home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / enterprs / cpm / utils / a / dbdecode.lbr / DECODE.CZ / DECODE.C
Encoding:
C/C++ Source or Header  |  1991-10-19  |  3.5 KB  |  126 lines

  1. /* DECODE.C by Ross Presser
  2.    Decrypts a psuedo-compiled dBASE II .CMD file.
  3.    The decrypted command file will be pretty printed as follows:
  4.  
  5. IF A=B
  6.    statement
  7. ELSE
  8.    statement
  9. ENDIF
  10. DO WHILE A=B
  11.    statement
  12. ENDDO
  13. DO cmdfile
  14. DO CASE
  15.    CASE A=B
  16.       statement
  17.    CASE C=D
  18.       statement
  19.    CASE e=F
  20.       statement
  21.    OTHERWISE
  22.       statement
  23. ENDCASE
  24.  
  25. all indentations are three spaces; all verbs are in capitals
  26.  
  27. --------------
  28.  
  29.    Usage: DECODE <infile.cmd >outfile.CMD
  30.  
  31. --------------
  32.    Construction with BDS C:
  33.  
  34.    cc decode
  35.    l2 decode dio
  36.  
  37. */
  38.  
  39.  
  40. #include <bdscio.h>
  41. #include <dio.h>
  42.  
  43. #define NCMDS 0x42        /* Number of dBASE command verbs */
  44.  
  45. char *cmds[NCMDS];        /* table of command verbs */
  46. int c,indent,i;
  47.  
  48. main(argc,argv) int argc; char *argv[];
  49. {
  50. /* Initialize dio (i/o redirection) and the table of cmd verb strings */
  51.     dioinit(&argc,argv);
  52.     cmdinit(); indent=0;
  53.  
  54. /* main processing loop
  55.    If the first char is > 128, decrypt the line, otherwise echo it
  56. */
  57.     while((c=getchar())!=EOF && c!=26) {
  58.  
  59.  
  60. /* if < 128, don't attempt to decrypt */
  61.         if (c<0x80) {
  62.             putchar(c);
  63.         } else {
  64.  
  65. /* This switch stmt & the following one cause the file to be pretty-printed */
  66.             switch (c-0x80) {
  67.                 case 7: indent -= 3;
  68.                 case 1: case 2: case 4: 
  69.                  case 5: case 6: indent -= 3;
  70.                 }
  71.             for(i=indent;i--;putchar(' ')) ;
  72.             switch(c-0x80) {
  73.                 case 9: indent +=3;
  74.                 case 0: case 1: case 3: case 5: 
  75.                 case 6: case 8: case 9: indent += 3;
  76.                  }
  77.  
  78. /* print the cmd verb */
  79.             puts(cmds[c-0x80]);
  80.             putchar(' ');
  81.  
  82.             }
  83. /* decrypt (or just print out) the rest of the line */
  84.         while((c=getchar())!='\n')
  85.             putchar(c>127 ? 255-c : c);
  86.         putchar('\n');
  87.         }
  88.  
  89. /* flush output files before exiting */
  90.     dioflush();
  91. }
  92.  
  93.  
  94. /* This command table was discovered by tracing dBASE as it executed
  95.    an encoded file.  The indices (0 thru 0x42) point into a jump table
  96.    which is located in DBASEOVR.COM.  When the interpreter discovers a
  97.    line beginning with a char > 128, it subtracts 128 and points into
  98.    this jump table. */
  99.  
  100. cmdinit()
  101. {
  102. cmds[0x00]="IF";    cmds[0x01]="ELSE";    cmds[0x02]="ENDIF";
  103. cmds[0x03]="DO";    cmds[0x04]="ENDDO";    cmds[0x05]="CASE";
  104. cmds[0x06]="OTHERWISE";    cmds[0x07]="ENDCASE";    cmds[0x08]="DO WHILE";
  105. cmds[0x09]="DO CASE";    cmds[0x0a]="STORE";    cmds[0x0b]="?";
  106. cmds[0x0c]="RELEASE";    cmds[0x0d]="RETURN";    cmds[0x0e]="SELECT";    
  107. cmds[0x0f]="@";        cmds[0x10]="ACCEPT";    cmds[0x11]="APPEND";
  108. cmds[0x12]="BROWSE";    cmds[0x13]="CALL";    cmds[0x14]="CANCEL";
  109. cmds[0x15]="CHANGE";    cmds[0x16]="CLEAR";    cmds[0x17]="COPY";
  110. cmds[0x18]="COUNT";    cmds[0x19]="CREATE";    cmds[0x1a]="DELETE";
  111. cmds[0x1b]="DISPLAY";    cmds[0x1c]="CONTINUE";    cmds[0x1d]="EDIT";
  112. cmds[0x1e]="EJECT";    cmds[0x1f]="ERASE";    cmds[0x20]="GO";
  113. cmds[0x21]="FIND";    cmds[0x22]="HELP";    cmds[0x23]="INDEX";
  114. cmds[0x24]="INPUT";    cmds[0x25]="INSERT";    cmds[0x26]="JOIN";
  115. cmds[0x27]="LIST";    cmds[0x28]="LOAD";    cmds[0x29]="LOCATE";
  116. cmds[0x2a]="LOOP";    cmds[0x2b]="MODIFY";    cmds[0x2c]="PACK";
  117. cmds[0x2d]="POKE";    cmds[0x2e]="QUIT";    cmds[0x2f]="READ";
  118. cmds[0x30]="RECALL";    cmds[0x31]="REINDEX";    cmds[0x32]="REMARK";
  119. cmds[0x33]="RENAME";    cmds[0x34]="REPLACE";    cmds[0x35]="REPORT";
  120. cmds[0x36]="RESET";    cmds[0x37]="RESTORE";    cmds[0x38]="SAVE";
  121. cmds[0x39]="SET";    cmds[0x3a]="SKIP";    cmds[0x3b]="SORT";
  122. cmds[0x3c]="SUM";    cmds[0x3d]="TEXT";    cmds[0x3e]="TOTAL";
  123. cmds[0x3f]="UNLOCK";    cmds[0x40]="UPDATE";    cmds[0x41]="USE";
  124. cmds[0x42]="WAIT";
  125. }
  126.