RFXTOPCX.C
上传用户:wesley
上传日期:2007-01-07
资源大小:266k
文件大小:36k
源码类别:

通讯/手机编程

开发平台:

C/C++

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