Program Decoder;

Const
     BufSize  =  32768;

Var
    SF, DF : File;
NumR, NumW : Word;
         C : Word;
      R, W : Byte;
      Buf  : Array  [ 1..BufSize ] of byte;

Begin
  WriteLn;
  WriteLn('KOI-8 to ASCII decoder');

  If (ParamStr(1)='') or (ParamStr(2)='') then
                      begin
                       WriteLn('Usage: koi2asc FileName1 FileName2');
                       WriteLn('FileName1 - Source ( Win-1251 text file )');
                       WriteLn('FileName2 - Destination ( ASCII text file )');
                       Halt(1);
                      end;
  If ParamStr(1) = ParamStr(2) then
                 begin
                    WriteLn ('Error! Can''t decode file to itself!');
                    Halt(2);
                 end;
  Assign  ( SF, ParamStr(1) );
  Assign  ( DF, ParamStr(2) );
  Reset   ( SF, 1 );
  ReWrite ( DF, 1 );

    Repeat
     BlockRead ( SF, Buf, SizeOf(Buf), NumR );
        For C:= 1 to BufSize do
         begin
          R :=  Buf[C];
          If R in [219..255] then Dec(R,32);
          W := R;
          If R in [ 186 .. 218 ] then
          case R of
          186: W := 135;
          187: W := 152;
          189: W := 153;
          190: W := 151;
          192: W := 158;
          193: W := 128;
          194: W := 129;
          195: W := 150;
          196: W := 132;
          197: W := 133;
          198: W := 148;
          199: W := 131;
          200: W := 149;
          201: W := 136;
          202: W := 137;
          203: W := 138;
          204: W := 139;
          205: W := 140;
          206: W := 141;
          207: W := 142;
          208: W := 143;
          209: W := 159;
          210: W := 144;
          211: W := 145;
          212: W := 146;
          213: W := 147;
          214: W := 134;
          215: W := 130;
          216: W := 156;
          217: W := 155;
          218: W := 135;
          219: W := 135;


          end;
          {If R in [ 240 .. 255 ] then W := R - 16;}
          {If R = 150 then W := 45;
          If R = 151 then W := 45;
	  If R = 171 then W := 34;
	  If R = 187 then W := 34;
          If R = 168 then W := 240;
          If R = 184 then W := 241;
          If R = 133 then W := 46;}
          Buf[C] := W;
        end;
     BlockWrite ( DF, Buf, NumR, NumW );
    Until ( NumR =  0 ) or ( NumR <> NumW );

  Close   ( SF );
  Close   ( DF );
End.