home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: DFÜ und Kommunikation / SOS-DFUE.ISO / programm / dos / utility / pccp076 / oldrtop.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-28  |  37.3 KB  |  1,505 lines

  1. #include<stdio.h>
  2. #include<fcntl.h>
  3. #include<sys\types.h>
  4. #include<sys\stat.h>
  5. #include<setjmp.h>
  6.  
  7. #define MAKEUP 0x1000
  8. #define SEVENOHS 0x1fff
  9.  
  10. unsigned char curbyte, bitmask;
  11.  
  12. int rfxfd;
  13.  
  14. jmp_buf where;
  15.  
  16. int debug;
  17.  
  18. #define FBUFSIZ 4096
  19.  
  20. unsigned char fbuf[FBUFSIZ];
  21. int fbufptr, fbufn;
  22.  
  23. int getbyte()
  24.     {
  25.     if(fbufptr>=fbufn)
  26.         if((fbufn=read(rfxfd, fbuf, FBUFSIZ))<=0)
  27.             return(-1);
  28.         else
  29.             fbufptr=0;
  30.     return(fbuf[fbufptr++]);
  31.     }
  32.  
  33. int getbit()
  34.         {
  35.         int result;
  36.         if(!bitmask)
  37.                 {
  38.                 if((curbyte=getbyte())==-1)
  39.             {
  40.             printf("Premature end of file.\n");
  41.             exit(1);
  42.             }
  43.         if(curbyte==0x10)
  44.             {
  45.             if((curbyte=getbyte())==-1)
  46.                 {
  47.                 printf("Premature end of file.\n");
  48.                 exit(1);
  49.                 }
  50.             if(curbyte==0x03)
  51.                 {
  52.                 longjmp(where, -1);
  53.                 }
  54.             }
  55.                 bitmask=0x80;
  56.         if(debug)
  57.             printf("(%02x)", curbyte);
  58.                 }
  59.         result=curbyte&bitmask;
  60.         bitmask>>=1;
  61.     bitmask&=0x7f;
  62.     if(debug)
  63.         if(result)
  64.             putchar('1');
  65.         else
  66.             putchar('0');
  67.     return(result);
  68.         }
  69.  
  70. int getwhite()
  71.         {
  72.         if(getbit())
  73.          /* 1 */
  74.          if(getbit())
  75.           /* 11 */
  76.           if(getbit())
  77.            /* 111 */
  78.            if(getbit())
  79.             /* 1111 */
  80.             return(7);
  81.            else
  82.             /* 1110 */
  83.             return(6);
  84.           else
  85.            /* 110 */
  86.            if(getbit())
  87.             /* 1101 */
  88.             if(getbit())
  89.              /* 11011 */
  90.              return(MAKEUP|64);
  91.             else
  92.              /* 11010 */
  93.              if(getbit())
  94.               /* 110101 */
  95.               return(15);
  96.              else
  97.               /* 110100 */
  98.               return(14);
  99.            else
  100.             /* 1100 */
  101.             return(5);
  102.          else
  103.           /* 10 */
  104.           if(getbit())
  105.            /* 101 */
  106.            if(getbit())
  107.             /* 1011 */
  108.             return(4);
  109.            else
  110.             /* 1010 */
  111.             if(getbit())
  112.              /* 10101 */
  113.              if(getbit())
  114.               /* 101011 */
  115.               return(17);
  116.              else
  117.               /* 101010 */
  118.               return(16);
  119.             else
  120.              /* 10100 */
  121.              return(9);
  122.           else
  123.            /* 100 */
  124.            if(getbit())
  125.             /* 1001 */
  126.             if(getbit())
  127.              /* 10011 */
  128.              return(8);
  129.             else
  130.              /* 10010 */
  131.              return(MAKEUP|128);
  132.            else
  133.             /* 1000 */
  134.             return(3);
  135.         else
  136.          /* 0 */
  137.          if(getbit())
  138.           /* 01 */
  139.           if(getbit())
  140.            /* 011 */
  141.            if(getbit())
  142.             /* 0111 */
  143.             return(2);
  144.            else
  145.             /* 0110 */
  146.             if(getbit())
  147.              /* 01101 */
  148.              if(getbit())
  149.               /* 011011 */
  150.               if(getbit())
  151.                /* 0110111 */
  152.                return(MAKEUP|256);
  153.               else
  154.                /* 0110110 */
  155.                if(getbit())
  156.                 /* 01101101 */
  157.                 if(getbit())
  158.                  /* 011011011 */
  159.                  return(MAKEUP|1408);
  160.                 else
  161.                  /* 011011010 */
  162.                  return(MAKEUP|1344);
  163.                else
  164.                 /* 01101100 */
  165.                 if(getbit())
  166.                  /* 011011001 */
  167.                  return(MAKEUP|1280);
  168.                 else
  169.                  /* 011011000 */
  170.                  return(MAKEUP|1216);
  171.              else
  172.               /* 011010 */
  173.               if(getbit())
  174.                /* 0110101 */
  175.                if(getbit())
  176.                 /* 01101011 */
  177.                 if(getbit())
  178.                  /* 011010111 */
  179.                  return(MAKEUP|1152);
  180.                 else
  181.                  /* 011010110 */
  182.                  return(MAKEUP|1088);
  183.                else
  184.                 /* 01101010 */
  185.                 if(getbit())
  186.                  /* 011010101 */
  187.                  return(MAKEUP|1024);
  188.                 else
  189.                  /* 011010100 */
  190.                  return(MAKEUP|960);
  191.               else
  192.                /* 0110100 */
  193.                if(getbit())
  194.                 /* 01101001 */
  195.                 if(getbit())
  196.                  /* 011010011 */
  197.                  return(MAKEUP|896);
  198.                 else
  199.                  /* 011010010 */
  200.                  return(MAKEUP|832);
  201.                else
  202.                 /* 01101000 */
  203.                 return(MAKEUP|576);
  204.             else
  205.              /* 01100 */
  206.              if(getbit())
  207.               /* 011001 */
  208.               if(getbit())
  209.                /* 0110011 */
  210.                if(getbit())
  211.                 /* 01100111 */
  212.                 return(MAKEUP|640);
  213.                else
  214.                 /* 01100110 */
  215.                 if(getbit())
  216.                  /* 011001101 */
  217.                  return(MAKEUP|768);
  218.                 else
  219.                  /* 011001100 */
  220.                  return(MAKEUP|704);
  221.               else
  222.                /* 0110010 */
  223.                if(getbit())
  224.                 /* 01100101 */
  225.                 return(MAKEUP|512);
  226.                else
  227.                 /* 01100100 */
  228.                 return(MAKEUP|448);
  229.              else
  230.               /* 011000 */
  231.               return(MAKEUP|1664);
  232.           else
  233.            /* 010 */
  234.            if(getbit())
  235.             /* 0101 */
  236.             if(getbit())
  237.              /* 01011 */
  238.              if(getbit())
  239.               /* 010111 */
  240.               return(MAKEUP|192);
  241.              else
  242.               /* 010110 */
  243.               if(getbit())
  244.                /* 0101101 */
  245.                if(getbit())
  246.                 /* 01011011 */
  247.                 return(58);
  248.                else
  249.                 /* 01011010 */
  250.                 return(57);
  251.               else
  252.                /* 0101100 */
  253.                if(getbit())
  254.                 /* 01011001 */
  255.                 return(56);
  256.                else
  257.                 /* 01011000 */
  258.                 return(55);
  259.             else
  260.              /* 01010 */
  261.              if(getbit())
  262.               /* 010101 */
  263.               if(getbit())
  264.                /* 0101011 */
  265.                return(25);
  266.               else
  267.                /* 0101010 */
  268.                if(getbit())
  269.                 /* 01010101 */
  270.                 return(52);
  271.                else
  272.                 /* 01010100 */
  273.                 return(51);
  274.              else
  275.               /* 010100 */
  276.               if(getbit())
  277.                /* 0101001 */
  278.                if(getbit())
  279.                 /* 01010011 */
  280.                 return(50);
  281.                else
  282.                 /* 01010010 */
  283.                 return(49);
  284.               else
  285.                /* 0101000 */
  286.                return(24);
  287.            else
  288.             /* 0100 */
  289.             if(getbit())
  290.              /* 01001 */
  291.              if(getbit())
  292.               /* 010011 */
  293.               if(getbit())
  294.                /* 0100111 */
  295.                return(18);
  296.               else
  297.                /* 0100110 */
  298.                if(getbit())
  299.                 /* 01001101 */
  300.                 if(getbit())
  301.                  /* 010011011 */
  302.                  return(MAKEUP|1728);
  303.                 else
  304.                  /* 010011010 */
  305.                  return(MAKEUP|1600);
  306.                else
  307.                 /* 01001100 */
  308.                 if(getbit())
  309.                  /* 010011001 */
  310.                  return(MAKEUP|1536);
  311.                 else
  312.                  /* 010011000 */
  313.                  return(MAKEUP|1472);
  314.              else
  315.               /* 010010 */
  316.               if(getbit())
  317.                /* 0100101 */
  318.                if(getbit())
  319.                 /* 01001011 */
  320.                 return(60);
  321.                else
  322.                 /* 01001010 */
  323.                 return(59);
  324.               else
  325.                /* 0100100 */
  326.                return(27);
  327.             else
  328.              /* 01000 */
  329.              return(11);
  330.          else
  331.           /* 00 */
  332.           if(getbit())
  333.            /* 001 */
  334.            if(getbit())
  335.             /* 0011 */
  336.             if(getbit())
  337.              /* 00111 */
  338.              return(10);
  339.             else
  340.              /* 00110 */
  341.              if(getbit())
  342.               /* 001101 */
  343.               if(getbit())
  344.                /* 0011011 */
  345.                if(getbit())
  346.                 /* 00110111 */
  347.                 return(MAKEUP|384);
  348.                else
  349.                 /* 00110110 */
  350.                 return(MAKEUP|320);
  351.               else
  352.                /* 0011010 */
  353.                if(getbit())
  354.                 /* 00110101 */
  355.                 return(0);
  356.                else
  357.                 /* 00110100 */
  358.                 return(63);
  359.              else
  360.               /* 001100 */
  361.               if(getbit())
  362.                /* 0011001 */
  363.                if(getbit())
  364.                 /* 00110011 */
  365.                 return(62);
  366.                else
  367.                 /* 00110010 */
  368.                 return(61);
  369.               else
  370.                /* 0011000 */
  371.                return(28);
  372.            else
  373.             /* 0010 */
  374.             if(getbit())
  375.              /* 00101 */
  376.              if(getbit())
  377.               /* 001011 */
  378.               if(getbit())
  379.                /* 0010111 */
  380.                return(21);
  381.               else
  382.                /* 0010110 */
  383.                if(getbit())
  384.                 /* 00101101 */
  385.                 return(44);
  386.                else
  387.                 /* 00101100 */
  388.                 return(43);
  389.              else
  390.               /* 001010 */
  391.               if(getbit())
  392.                /* 0010101 */
  393.                if(getbit())
  394.                 /* 00101011 */
  395.                 return(42);
  396.                else
  397.                 /* 00101010 */
  398.                 return(41);
  399.               else
  400.                /* 0010100 */
  401.                if(getbit())
  402.                 /* 00101001 */
  403.                 return(40);
  404.                else
  405.                 /* 00101000 */
  406.                 return(39);
  407.             else
  408.              /* 00100 */
  409.              if(getbit())
  410.               /* 001001 */
  411.               if(getbit())
  412.                /* 0010011 */
  413.                return(26);
  414.               else
  415.                /* 0010010 */
  416.                if(getbit())
  417.                 /* 00100101 */
  418.                 return(54);
  419.                else
  420.                 /* 00100100 */
  421.                 return(53);
  422.              else
  423.               /* 001000 */
  424.               return(12);
  425.           else
  426.            /* 000 */
  427.            if(getbit())
  428.             /* 0001 */
  429.             if(getbit())
  430.              /* 00011 */
  431.              if(getbit())
  432.               /* 000111 */
  433.               return(1);
  434.              else
  435.               /* 000110 */
  436.               if(getbit())
  437.                /* 0001101 */
  438.                if(getbit())
  439.                 /* 00011011 */
  440.                 return(32);
  441.                else
  442.                 /* 00011010 */
  443.                 return(31);
  444.               else
  445.                /* 0001100 */
  446.                return(19);
  447.             else
  448.              /* 00010 */
  449.              if(getbit())
  450.               /* 000101 */
  451.               if(getbit())
  452.                /* 0001011 */
  453.                if(getbit())
  454.                 /* 00010111 */
  455.                 return(38);
  456.                else
  457.                 /* 00010110 */
  458.                 return(37);
  459.               else
  460.                /* 0001010 */
  461.                if(getbit())
  462.                 /* 00010101 */
  463.                 return(36);
  464.                else
  465.                 /* 00010100 */
  466.                 return(35);
  467.              else
  468.               /* 000100 */
  469.               if(getbit())
  470.                /* 0001001 */
  471.                if(getbit())
  472.                 /* 00010011 */
  473.                 return(34);
  474.                else
  475.                 /* 00010010 */
  476.                 return(33);
  477.               else
  478.                /* 0001000 */
  479.                return(20);
  480.            else
  481.             /* 0000 */
  482.             if(getbit())
  483.              /* 00001 */
  484.              if(getbit())
  485.               /* 000011 */
  486.               return(13);
  487.              else
  488.               /* 000010 */
  489.               if(getbit())
  490.                /* 0000101 */
  491.                if(getbit())
  492.                 /* 00001011 */
  493.                 return(48);
  494.                else
  495.                 /* 00001010 */
  496.                 return(47);
  497.               else
  498.                /* 0000100 */
  499.                return(23);
  500.             else
  501.              /* 00000 */
  502.              if(getbit())
  503.               /* 000001 */
  504.               if(getbit())
  505.                /* 0000011 */
  506.                return(22);
  507.               else
  508.                /* 0000010 */
  509.                if(getbit())
  510.                 /* 00000101 */
  511.                 return(46);
  512.                else
  513.                 /* 00000100 */
  514.                 return(45);
  515.              else
  516.               /* 000000 */
  517.               if(getbit())
  518.                /* 0000001 */
  519.                if(getbit())
  520.                 /* 00000011 */
  521.                 return(30);
  522.                else
  523.                 /* 00000010 */
  524.                 return(29);
  525.               else
  526.                /* 0000000 */
  527.                return(SEVENOHS);
  528.         }
  529.  
  530. int getblack()
  531.     {
  532.     if(getbit())
  533.          /* 1 */
  534.          if(getbit())
  535.           /* 11 */
  536.           return(2);
  537.          else
  538.           /* 10 */
  539.           return(3);
  540.         else
  541.          /* 0 */
  542.          if(getbit())
  543.           /* 01 */
  544.           if(getbit())
  545.            /* 011 */
  546.            return(4);
  547.           else
  548.            /* 010 */
  549.            return(1);
  550.          else
  551.           /* 00 */
  552.           if(getbit())
  553.            /* 001 */
  554.            if(getbit())
  555.             /* 0011 */
  556.             return(5);
  557.            else
  558.             /* 0010 */
  559.             return(6);
  560.           else
  561.            /* 000 */
  562.            if(getbit())
  563.             /* 0001 */
  564.             if(getbit())
  565.              /* 00011 */
  566.              return(7);
  567.             else
  568.              /* 00010 */
  569.              if(getbit())
  570.               /* 000101 */
  571.               return(8);
  572.              else
  573.               /* 000100 */
  574.               return(9);
  575.            else
  576.             /* 0000 */
  577.             if(getbit())
  578.              /* 00001 */
  579.              if(getbit())
  580.               /* 000011 */
  581.               if(getbit())
  582.                /* 0000111 */
  583.                return(12);
  584.               else
  585.                /* 0000110 */
  586.                if(getbit())
  587.                 /* 00001101 */
  588.                 if(getbit())
  589.                  /* 000011011 */
  590.                  if(getbit())
  591.                   /* 0000110111 */
  592.                   return(0);
  593.                  else
  594.                   /* 0000110110 */
  595.                   if(getbit())
  596.                    /* 00001101101 */
  597.                    if(getbit())
  598.                     /* 000011011011 */
  599.                     return(43);
  600.                    else
  601.                     /* 000011011010 */
  602.                     return(42);
  603.                   else
  604.                    /* 00001101100 */
  605.                    return(21);
  606.                 else
  607.                  /* 000011010 */
  608.                  if(getbit())
  609.                   /* 0000110101 */
  610.                   if(getbit())
  611.                    /* 00001101011 */
  612.                    if(getbit())
  613.                     /* 000011010111 */
  614.                     return(39);
  615.                    else
  616.                     /* 000011010110 */
  617.                     return(38);
  618.                   else
  619.                    /* 00001101010 */
  620.                    if(getbit())
  621.                     /* 000011010101 */
  622.                     return(37);
  623.                    else
  624.                     /* 000011010100 */
  625.                     return(36);
  626.                  else
  627.                   /* 0000110100 */
  628.                   if(getbit())
  629.                    /* 00001101001 */
  630.                    if(getbit())
  631.                     /* 000011010011 */
  632.                     return(35);
  633.                    else
  634.                     /* 000011010010 */
  635.                     return(34);
  636.                   else
  637.                    /* 00001101000 */
  638.                    return(20);
  639.                else
  640.                 /* 00001100 */
  641.                 if(getbit())
  642.                  /* 000011001 */
  643.                  if(getbit())
  644.                   /* 0000110011 */
  645.                   if(getbit())
  646.                    /* 00001100111 */
  647.                    return(19);
  648.                   else
  649.                    /* 00001100110 */
  650.                    if(getbit())
  651.                     /* 000011001101 */
  652.                     return(29);
  653.                    else
  654.                     /* 000011001100 */
  655.                     return(28);
  656.                  else
  657.                   /* 0000110010 */
  658.                   if(getbit())
  659.                    /* 00001100101 */
  660.                    if(getbit())
  661.                     /* 000011001011 */
  662.                     return(27);
  663.                    else
  664.                     /* 000011001010 */
  665.                     return(26);
  666.                   else
  667.                    /* 00001100100 */
  668.                    if(getbit())
  669.                     /* 000011001001 */
  670.                     return(MAKEUP|192);
  671.                    else
  672.                     /* 000011001000 */
  673.                     return(MAKEUP|128);
  674.                 else
  675.                  /* 000011000 */
  676.                  return(15);
  677.              else
  678.               /* 000010 */
  679.               if(getbit())
  680.                /* 0000101 */
  681.                return(11);
  682.               else
  683.                /* 0000100 */
  684.                return(10);
  685.             else
  686.              /* 00000 */
  687.              if(getbit())
  688.               /* 000001 */
  689.               if(getbit())
  690.                /* 0000011 */
  691.                if(getbit())
  692.                 /* 00000111 */
  693.                 return(14);
  694.                else
  695.                 /* 00000110 */
  696.                 if(getbit())
  697.                  /* 000001101 */
  698.                  if(getbit())
  699.                   /* 0000011011 */
  700.                   if(getbit())
  701.                    /* 00000110111 */
  702.                    return(22);
  703.                   else
  704.                    /* 00000110110 */
  705.                    if(getbit())
  706.                     /* 000001101101 */
  707.                     return(41);
  708.                    else
  709.                     /* 000001101100 */
  710.                     return(40);
  711.                  else
  712.                   /* 0000011010 */
  713.                   if(getbit())
  714.                    /* 00000110101 */
  715.                    if(getbit())
  716.                     /* 000001101011 */
  717.                     return(33);
  718.                    else
  719.                     /* 000001101010 */
  720.                     return(32);
  721.                   else
  722.                    /* 00000110100 */
  723.                    if(getbit())
  724.                     /* 000001101001 */
  725.                     return(31);
  726.                    else
  727.                     /* 000001101000 */
  728.                     return(30);
  729.                 else
  730.                  /* 000001100 */
  731.                  if(getbit())
  732.                   /* 0000011001 */
  733.                   if(getbit())
  734.                    /* 00000110011 */
  735.                    if(getbit())
  736.                     /* 000001100111 */
  737.                     return(63);
  738.                    else
  739.                     /* 000001100110 */
  740.                     return(62);
  741.                   else
  742.                    /* 00000110010 */
  743.                    if(getbit())
  744.                     /* 000001100101 */
  745.                     return(49);
  746.                    else
  747.                     /* 000001100100 */
  748.                     return(48);
  749.                  else
  750.                   /* 0000011000 */
  751.                   return(17);
  752.               else
  753.                /* 0000010 */
  754.                if(getbit())
  755.                 /* 00000101 */
  756.                 if(getbit())
  757.                  /* 000001011 */
  758.                  if(getbit())
  759.                   /* 0000010111 */
  760.                   return(16);
  761.                  else
  762.                   /* 0000010110 */
  763.                   if(getbit())
  764.                    /* 00000101101 */
  765.                    if(getbit())
  766.                     /* 000001011011 */
  767.                     return(MAKEUP|256);
  768.                    else
  769.                     /* 000001011010 */
  770.                     return(61);
  771.                   else
  772.                    /* 00000101100 */
  773.                    if(getbit())
  774.                     /* 000001011001 */
  775.                     return(58);
  776.                    else
  777.                     /* 000001011000 */
  778.                     return(57);
  779.                 else
  780.                  /* 000001010 */
  781.                  if(getbit())
  782.                   /* 0000010101 */
  783.                   if(getbit())
  784.                    /* 00000101011 */
  785.                    if(getbit())
  786.                     /* 000001010111 */
  787.                     return(47);
  788.                    else
  789.                     /* 000001010110 */
  790.                     return(46);
  791.                   else
  792.                    /* 00000101010 */
  793.                    if(getbit())
  794.                     /* 000001010101 */
  795.                     return(45);
  796.                    else
  797.                     /* 000001010100 */
  798.                     return(44);
  799.                  else
  800.                   /* 0000010100 */
  801.                   if(getbit())
  802.                    /* 00000101001 */
  803.                    if(getbit())
  804.                     /* 000001010011 */
  805.                     return(51);
  806.                    else
  807.                     /* 000001010010 */
  808.                     return(50);
  809.                   else
  810.                    /* 00000101000 */
  811.                    return(23);
  812.                else
  813.                 /* 00000100 */
  814.                 return(13);
  815.              else
  816.               /* 000000 */
  817.               if(getbit())
  818.                /* 0000001 */
  819.                if(getbit())
  820.                 /* 00000011 */
  821.                 if(getbit())
  822.                  /* 000000111 */
  823.                  if(getbit())
  824.                   /* 0000001111 */
  825.                   return(MAKEUP|64);
  826.                  else
  827.                   /* 0000001110 */
  828.                   if(getbit())
  829.                    /* 00000011101 */
  830.                    if(getbit())
  831.                     /* 000000111011 */
  832.                     if(getbit())
  833.                      /* 0000001110111 */
  834.                      return(MAKEUP|1216);
  835.                     else
  836.                      /* 0000001110110 */
  837.                      return(MAKEUP|1152);
  838.                    else
  839.                     /* 000000111010 */
  840.                     if(getbit())
  841.                      /* 0000001110101 */
  842.                      return(MAKEUP|1088);
  843.                     else
  844.                      /* 0000001110100 */
  845.                      return(MAKEUP|1024);
  846.                   else
  847.                    /* 00000011100 */
  848.                    if(getbit())
  849.                     /* 000000111001 */
  850.                     if(getbit())
  851.                      /* 0000001110011 */
  852.                      return(MAKEUP|960);
  853.                     else
  854.                      /* 0000001110010 */
  855.                      return(MAKEUP|896);
  856.                    else
  857.                     /* 000000111000 */
  858.                     return(54);
  859.                 else
  860.                  /* 000000110 */
  861.                  if(getbit())
  862.                   /* 0000001101 */
  863.                   if(getbit())
  864.                    /* 00000011011 */
  865.                    if(getbit())
  866.                     /* 000000110111 */
  867.                     return(53);
  868.                    else
  869.                     /* 000000110110 */
  870.                     if(getbit())
  871.                      /* 0000001101101 */
  872.                      return(MAKEUP|576);
  873.                     else
  874.                      /* 0000001101100 */
  875.                      return(MAKEUP|512);
  876.                   else
  877.                    /* 00000011010 */
  878.                    if(getbit())
  879.                     /* 000000110101 */
  880.                     return(MAKEUP|448);
  881.                    else
  882.                     /* 000000110100 */
  883.                     return(MAKEUP|384);
  884.                  else
  885.                   /* 0000001100 */
  886.                   if(getbit())
  887.                    /* 00000011001 */
  888.                    if(getbit())
  889.                     /* 000000110011 */
  890.                     return(MAKEUP|320);
  891.                    else
  892.                     /* 000000110010 */
  893.                     if(getbit())
  894.                      /* 0000001100101 */
  895.                      return(MAKEUP|1728);
  896.                     else
  897.                      /* 0000001100100 */
  898.                      return(MAKEUP|1664);
  899.                   else
  900.                    /* 00000011000 */
  901.                    return(25);
  902.                else
  903.                 /* 00000010 */
  904.                 if(getbit())
  905.                  /* 000000101 */
  906.                  if(getbit())
  907.                   /* 0000001011 */
  908.                   if(getbit())
  909.                    /* 00000010111 */
  910.                    return(24);
  911.                   else
  912.                    /* 00000010110 */
  913.                    if(getbit())
  914.                     /* 000000101101 */
  915.                     if(getbit())
  916.                      /* 0000001011011 */
  917.                      return(MAKEUP|1600);
  918.                     else
  919.                      /* 0000001011010 */
  920.                      return(MAKEUP|1536);
  921.                    else
  922.                     /* 000000101100 */
  923.                     return(60);
  924.                  else
  925.                   /* 0000001010 */
  926.                   if(getbit())
  927.                    /* 00000010101 */
  928.                    if(getbit())
  929.                     /* 000000101011 */
  930.                     return(59);
  931.                    else
  932.                     /* 000000101010 */
  933.                     if(getbit())
  934.                      /* 0000001010101 */
  935.                      return(MAKEUP|1472);
  936.                     else
  937.                      /* 0000001010100 */
  938.                      return(MAKEUP|1408);
  939.                   else
  940.                    /* 00000010100 */
  941.                    if(getbit())
  942.                     /* 000000101001 */
  943.                     if(getbit())
  944.                      /* 0000001010011 */
  945.                      return(MAKEUP|1344);
  946.                     else
  947.                      /* 0000001010010 */
  948.                      return(MAKEUP|1280);
  949.                    else
  950.                     /* 000000101000 */
  951.                     return(56);
  952.                 else
  953.                  /* 000000100 */
  954.                  if(getbit())
  955.                   /* 0000001001 */
  956.                   if(getbit())
  957.                    /* 00000010011 */
  958.                    if(getbit())
  959.                     /* 000000100111 */
  960.                     return(55);
  961.                    else
  962.                     /* 000000100110 */
  963.                     if(getbit())
  964.                      /* 0000001001101 */
  965.                      return(MAKEUP|832);
  966.                     else
  967.                      /* 0000001001100 */
  968.                      return(MAKEUP|768);
  969.                   else
  970.                    /* 00000010010 */
  971.                    if(getbit())
  972.                     /* 000000100101 */
  973.                     if(getbit())
  974.                      /* 0000001001011 */
  975.                      return(MAKEUP|704);
  976.                     else
  977.                      /* 0000001001010 */
  978.                      return(MAKEUP|640);
  979.                    else
  980.                     /* 000000100100 */
  981.                     return(52);
  982.                  else
  983.                   /* 0000001000 */
  984.                   return(18);
  985.               else
  986.                /* 0000000 */
  987.                return(SEVENOHS);
  988.        }
  989.  
  990. struct header_s
  991.     {
  992.     unsigned char mfgr;
  993.     unsigned char version;
  994.     unsigned char encoding;
  995.     unsigned char bppix;
  996.     unsigned short xmin;
  997.     unsigned short ymin;
  998.     unsigned short xmax;
  999.     unsigned short ymax;
  1000.     unsigned short hdpi;
  1001.     unsigned short vdpi;
  1002.     unsigned char cmap[48];
  1003.     unsigned char reserved;
  1004.     unsigned char nplanes;
  1005.     unsigned short bypl;
  1006.     unsigned short paltinf;
  1007.     unsigned short hscrnsz;
  1008.     unsigned short vscrnsz;
  1009.     unsigned char filler[54];
  1010.     };
  1011.  
  1012. int pcxfd, pagelines;
  1013. struct header_s header;
  1014.  
  1015. pcxwriteheader()
  1016.     {
  1017.     int i;
  1018.     header.mfgr=10;
  1019.     header.version=45;
  1020.     header.encoding=1;
  1021.     header.bppix=1;
  1022.     header.xmin=0;
  1023.     header.ymin=0;
  1024.     header.xmax=1727;
  1025.     header.ymax=pagelines-1;
  1026.     header.hdpi=200;
  1027.     header.vdpi=192;
  1028.     header.reserved=0;
  1029.     header.nplanes=1;
  1030.     header.bypl=216;
  1031.     header.paltinf=1;
  1032.     header.hscrnsz=0;
  1033.     header.vscrnsz=0;
  1034.     for(i=0;i<54;++i)
  1035.         header.filler[i]=0;
  1036.     lseek(pcxfd, (long)0, SEEK_SET);
  1037.     if(write(pcxfd, &header, 128)!=128)
  1038.         {
  1039.         printf("Error overwriting final pcx file header.\n");
  1040.         exit(9);
  1041.         }
  1042.     }
  1043.  
  1044. unsigned char fcfr[]="+FCFR\r\n\r\n";
  1045. unsigned char fhng[]="+FHNG";
  1046. unsigned char connect[]="CONNECT\r\n";
  1047.  
  1048. int scanfcfr()
  1049.     {
  1050.     int i, j, k;
  1051.     unsigned char c;
  1052.     i=j=k=0;
  1053.     while((c=getbyte())!=-1)
  1054.         {
  1055.         if(fcfr[i]==c)
  1056.             {
  1057.             i++;
  1058.             if(fcfr[i]=='\0')
  1059.                 return(1);
  1060.             }
  1061.         else
  1062.             i=0;
  1063.         if(connect[k]==c)
  1064.             {
  1065.             k++;
  1066.             if(connect[k]=='\0')
  1067.                 return(0);
  1068.             }
  1069.         else
  1070.             k=0;
  1071.         if(fhng[j]==c)
  1072.             {
  1073.             j++;
  1074.             if(fhng[j]=='\0')
  1075.                 {
  1076.                 printf("End of document. Exit code +FHNG");
  1077.                 while(1)
  1078.                     {
  1079.                     if((c=getbyte())==-1)
  1080.                         {
  1081.                         printf("\nPremature end of file while seeking +FHNG code.\n");
  1082.                         exit(6);
  1083.                         }
  1084.                     putchar(c);
  1085.                     if(c=='\n')
  1086.                         break;
  1087.                     }
  1088.                 exit(0);
  1089.                 }
  1090.             }
  1091.         else
  1092.             j=0;
  1093.         }
  1094.     printf("Premature end of file while seeking +FCFR.\n");
  1095.     exit(5);
  1096.     }
  1097.  
  1098. int scanconnect()
  1099.     {
  1100.     int i;
  1101.     unsigned char c;
  1102.     i=0;
  1103.     while((c=getbyte())!=-1)
  1104.         {
  1105.         if(connect[i]==c)
  1106.             {
  1107.             i++;
  1108.             if(connect[i]=='\0')
  1109.                 return(0);
  1110.             }
  1111.         else
  1112.             i=0;
  1113.         }
  1114.     printf("Premature end of file while seeking CONNECT.\n");
  1115.     exit(7);
  1116.     }
  1117.  
  1118. int rfxgets(str)
  1119.     unsigned char *str;
  1120.     {
  1121.     int i;
  1122.     i=0;
  1123.     while((str[i]=getbyte())!=-1)
  1124.         if(str[i]=='\r')
  1125.             {
  1126.             str[i]='\0';
  1127.             return(i);
  1128.             }
  1129.         else
  1130.             i++;
  1131.     return(-1);
  1132.     }
  1133.  
  1134. unsigned char line[216];
  1135. int linenbits;
  1136.  
  1137. putwhite(n)
  1138.     int n;
  1139.     {
  1140.     int bytenum, i;
  1141.     if(n>(1728-linenbits))
  1142.         n=1728-linenbits;
  1143.     bytenum=linenbits/8; /* integer division */
  1144.     for(i=7-(linenbits%8);(i>=0)&&(n>0);i--,n--)
  1145.         {
  1146.         line[bytenum]|=1<<i;
  1147.         linenbits++;
  1148.         }
  1149.     bytenum=linenbits/8;
  1150.     i=n/8;
  1151.     if(n%8)
  1152.         i++;
  1153.     for(;i>0;--i)
  1154.         line[bytenum++]=0xff; /* We limited n above */
  1155.     linenbits+=n;
  1156.     }
  1157.  
  1158. putblack(n)
  1159.     int n;
  1160.     {
  1161.     int bytenum, i;
  1162.     if(n>(1728-linenbits))
  1163.         n=1728-linenbits;
  1164.     bytenum=linenbits/8; /* integer division */
  1165.     for(i=7-(linenbits%8);(i>=0)&&(n>0);i--,n--)
  1166.         {
  1167.         line[bytenum]&=~(1<<i);
  1168.         linenbits++;
  1169.         }
  1170.     bytenum=linenbits/8;
  1171.     i=n/8;
  1172.     if(n%8)
  1173.         i++;
  1174.     for(;i>0;--i)
  1175.         line[bytenum++]=0x00;
  1176.     linenbits+=n;
  1177.     }
  1178.  
  1179. pcxwriteline(times)
  1180.     int times;
  1181.     {
  1182.     unsigned char pcxline[432], hold;
  1183.     int pcxnbytes, i, nreps;
  1184.     if(!linenbits)
  1185.         return(0);
  1186.     if(linenbits<1728)
  1187.         putwhite(1728-linenbits);
  1188.     pcxnbytes=0;
  1189.     /* PCX Encode */
  1190.     hold=line[0];
  1191.     nreps=i=1;
  1192.     while(1)
  1193.         {
  1194.         if((i<216)&&(line[i]==hold)) /* Reading beyond item is OK*/
  1195.             nreps++;
  1196.         else
  1197.             {
  1198.             if((nreps==1)&&((hold&0xc0)!=0xc0))
  1199.                 pcxline[pcxnbytes++]=hold;
  1200.             else
  1201.                 {
  1202.                 while(nreps)
  1203.                     {
  1204.                     if(nreps>63)
  1205.                         {
  1206.                         pcxline[pcxnbytes++]=63|0xc0;
  1207.                         pcxline[pcxnbytes++]=hold;
  1208.                         nreps-=63;
  1209.                         }
  1210.                     else
  1211.                         {
  1212.                         pcxline[pcxnbytes++]=nreps|0xc0;
  1213.                         pcxline[pcxnbytes++]=hold;
  1214.                         nreps=0;
  1215.                         }
  1216.                     }
  1217.                 nreps=1; /* This is a little ahead */
  1218.                 }
  1219.             }
  1220.         if(i<216)
  1221.             hold=line[i++];
  1222.         else
  1223.             break;
  1224.         }
  1225.     /* Write encoded line */
  1226.     for(i=0;i<times;i++)
  1227.         {
  1228.         if(write(pcxfd, pcxline, pcxnbytes)!=pcxnbytes)
  1229.             {
  1230.             printf("Error writing encoded line to file.\n");
  1231.             exit(8);
  1232.             }
  1233.         pagelines++;
  1234.         }
  1235.     }
  1236.  
  1237. abortpage()
  1238.     {
  1239.     unsigned char c;
  1240.     printf("Flushing page...\n");
  1241.     while(1)
  1242.         {
  1243.         if(!read(rfxfd, &c, 1))
  1244.             {
  1245.             printf("Premature end of file while flushing bad page.\n");
  1246.             exit(30);
  1247.             }
  1248.         if(c==0x10)
  1249.             {
  1250.             if(!read(rfxfd, &c, 1))
  1251.                 {
  1252.                 printf("Premature end of file while flushing bad page.\n");
  1253.                 exit(30);
  1254.                 }
  1255.             if(c==0x03)
  1256.                 break;
  1257.             }
  1258.         }
  1259.     }
  1260.  
  1261.  
  1262. main(argc, argv)
  1263.     int argc;
  1264.     char **argv;
  1265.     {
  1266.     int dcsvr, dcswd, dcsdf, page, symbol, bitcount, oflag;
  1267.     int dopage, npelschk, gotfcfr, resyncrpt;
  1268.     long nzb;
  1269.     unsigned char c, str[128];
  1270.     printf("Copyright (C) 1992 Peter Edward Cann\n\n");
  1271.     if((argc!=2)&&(argc!=3))
  1272.         {
  1273.         printf("USAGE: rfx2pcx <rfx filename> [/debug]\n");
  1274.         exit(10);
  1275.         }
  1276.     if(!strcmp(getenv("REMOTE"), "YES"))
  1277.         {
  1278.         printf("You appear to be logged in remotely, judging by the environment\n");
  1279.         printf("variable REMOTE, so this is probably a very bad idea.\n");
  1280.         printf("Are you sure you want to run MASTERM? (y or n) --> ");
  1281.         if(getchar()!='y') /* Note getchar() and not getch()! */
  1282.             {
  1283.             printf("n\nI didn't think so!\n");
  1284.             exit(99);
  1285.             }
  1286.         else
  1287.             printf("y\nOK, you're the boss!\n");
  1288.         }
  1289.     if(argc==3)
  1290.         debug=1;
  1291.     else
  1292.         debug=0;
  1293.     if((rfxfd=open(argv[1], O_RDONLY|O_BINARY))==-1)
  1294.         {
  1295.         printf("Error opening %s for read.\n", argv[1]);
  1296.         exit(11);
  1297.         }
  1298.     oflag=0;
  1299.     fbufptr=fbufn=0;
  1300.     while(1)
  1301.         {
  1302.         if((c=getbyte())==-1)
  1303.             {
  1304.             printf("Premature end of file while seeking initial doublequote.\n");
  1305.             exit(12);
  1306.             }
  1307.         if(c=='"')
  1308.             {
  1309.             oflag=0;
  1310.             break;
  1311.             }
  1312.         if(oflag)
  1313.             if(c=='K')
  1314.                 break;
  1315.             else
  1316.                 oflag=0;
  1317.         if(c=='O')
  1318.             oflag=1;
  1319.         }
  1320.     printf("Sender ID: ");
  1321.     if(oflag)
  1322.         printf("Not specified.");
  1323.     else
  1324.         while(1)
  1325.             {
  1326.             if((c=getbyte())==-1)
  1327.                 {
  1328.                 printf("Premature end of file while seeking final doublequote.\n");
  1329.                 exit(13);
  1330.                 }
  1331.             if(c=='"')
  1332.                 break;
  1333.             putchar(c);
  1334.             }
  1335.     putchar('\n');
  1336.     page=0;
  1337.     gotfcfr=0;
  1338.     while(1)
  1339.         {
  1340.         page++;
  1341.         if(scanfcfr()) /* Normal exit is in this function */
  1342.             {
  1343.             if(rfxgets(str)==-1)
  1344.                 {
  1345.                 printf("Premature end of file while seeking +FDCS.\n");
  1346.                 exit(15);
  1347.                 }
  1348.             if(sscanf(str, " +FDCS: %d,%*d,%d,%*d,%d,%*d,%*d,%*d",
  1349.                 &dcsvr, &dcswd, &dcsdf)!=3)
  1350.                 {
  1351.                 printf("Error scanning +FDCS params.\n");
  1352.                 exit(14);
  1353.                 }
  1354.             else
  1355.                 gotfcfr=1;
  1356.             if(dcsvr)
  1357.                 printf("High vertical resolution.\n");
  1358.             else
  1359.                 printf("Standard vertical resolution.\n");
  1360.             if(dcswd)
  1361.                 {
  1362.                 printf("Non-supported width!\n");
  1363.                 exit(13);
  1364.                 }
  1365.             if(dcsdf)
  1366.                 {
  1367.                 printf("Non-supported coding scheme (must be 1-D)!\n");
  1368.                 exit(14);
  1369.                 }
  1370.             scanconnect();
  1371.             gotfcfr=1;
  1372.             }
  1373.         if(!gotfcfr)
  1374.             {
  1375.             printf("Never got initial +FCFR; aborting.\n");
  1376.             exit(60);
  1377.             }
  1378.         bitmask=0;
  1379.         while(1)
  1380.             {
  1381.             cprintf("Enter filename for Page %d (include .PCX):\r\n--> ", page);
  1382.             gets(str);
  1383.             if((pcxfd=open(str, O_CREAT|O_BINARY|O_TRUNC|O_WRONLY, S_IWRITE))!=-1)
  1384.                 break;
  1385.             else
  1386.                 printf("Error opening file.\n");
  1387.             }
  1388.         /* Save a dummy block for PCX file header */
  1389.         if(write(pcxfd, str, 128)!=128)
  1390.             {
  1391.             printf("Error writing dummy header to file.\n");
  1392.             exit(20);
  1393.             }
  1394.         /* Translate */
  1395.         pagelines=0;
  1396.         while(getbit()); /* flush Panafax 1 bits */
  1397.         while(!getbit()); /* flush initial EOL */
  1398.         if(debug)
  1399.             printf(" = EOL\n");
  1400.         dopage=1;
  1401.         if(!setjmp(where)) /* returns -1 magically on DLE ETX */
  1402.             while(1)
  1403.                 {
  1404.                 /* loop over lines */
  1405.                 linenbits=npelschk=0;
  1406.                 while(1)
  1407.                     {
  1408.                     /* loop over symbols */
  1409.                     symbol=getwhite();
  1410.                     if(debug)
  1411.                         {
  1412.                         printf(" = %d White", symbol&0x0fff);
  1413.                         if(symbol&MAKEUP)
  1414.                             printf(" Makeup\n");
  1415.                         else
  1416.                             printf("\n");
  1417.                         }
  1418.                     bitcount=symbol&0x0fff;
  1419.                     if(symbol&MAKEUP)
  1420.                         {
  1421.                         if(symbol==SEVENOHS)
  1422.                             break;
  1423.                         symbol=getwhite();
  1424.                         if(debug)
  1425.                             {
  1426.                             printf(" = %d White", symbol&0x0fff);
  1427.                             if(symbol&MAKEUP)
  1428.                                 printf(" Makeup\n");
  1429.                             else
  1430.                                 printf("\n");
  1431.                             }
  1432.                         if(symbol&MAKEUP)
  1433.                             {
  1434.                             printf("Error: sequential makeups.\n");
  1435.                             abortpage();
  1436.                             dopage=0;
  1437.                             break;
  1438.                             }
  1439.                         bitcount+=symbol&0x0fff;
  1440.                         }
  1441.                     npelschk+=bitcount;
  1442.                     putwhite(bitcount);
  1443.                     symbol=getblack();
  1444.                     if(debug)
  1445.                         {
  1446.                         printf(" = %d Black", symbol&0x0fff);
  1447.                         if(symbol&MAKEUP)
  1448.                             printf(" Makeup\n");
  1449.                         else
  1450.                             printf("\n");
  1451.                         }
  1452.                     bitcount=symbol&0x0fff;
  1453.                     if(symbol&MAKEUP)
  1454.                         {
  1455.                         if(symbol==SEVENOHS)
  1456.                             break;
  1457.                         symbol=getblack();
  1458.                         if(debug)
  1459.                             {
  1460.                             printf(" = %d Black", symbol&0x0fff);
  1461.                             if(symbol&MAKEUP)
  1462.                                 printf(" Makeup\n");
  1463.                             else
  1464.                                 printf("\n");
  1465.                             }
  1466.                         if(symbol&MAKEUP)
  1467.                             {
  1468.                             printf("Error: sequential makeups.\n");
  1469.                             abortpage();
  1470.                             dopage=0;
  1471.                             break;
  1472.                             }
  1473.                         bitcount+=symbol&0x0fff;
  1474.                         }
  1475.                     npelschk+=bitcount;
  1476.                     putblack(bitcount);
  1477.                     }
  1478.                 if(!dopage)
  1479.                     break;
  1480.                 if(!(((pagelines/(2-dcsvr))&0x0f)||debug))
  1481.                     printf("\rLine %d", pagelines/(2-dcsvr));
  1482.                 if(npelschk&&(npelschk!=1728))
  1483.                     printf("\nLine %d bad; %d pels.\n", pagelines/(2-dcsvr), npelschk);
  1484.                 pcxwriteline(2-dcsvr);
  1485.                 resyncrpt=0;
  1486.                 for(nzb=7;nzb<11;nzb++) /*hang for 11 0-bits*/
  1487.                     if(getbit())
  1488.                         {
  1489.                         resyncrpt=1;
  1490.                         nzb=0;
  1491.                         }
  1492.                 while(!getbit());
  1493.                 if(resyncrpt)
  1494.                     printf("\nHad to resync over junk in decoded line %d\n", pagelines/(2-dcsvr));
  1495.                 if(debug)
  1496.                     printf(" = EOL\n");
  1497.                 }
  1498.         else
  1499.             pcxwriteline(2-dcsvr);
  1500.         pcxwriteheader();
  1501.         close(pcxfd);
  1502.         printf("\nPage translate completed.\n");
  1503.         }
  1504.     }
  1505.