Fanático de la portabilidad, y muy contento con el método de Shadow para convertir de binario a batch, me puse a buscar algún código que hicera lo mismo que el programa de Shadow, y descubrí uno hecho en pascal (nunca había leído pascal
), y lo compilé y no trabajaba bien, así es que lo arreglé con lo nada que sé de pascal, y mirando los resultados del programa de Shadow. El único problema que tiene es que con archivos grandes, no informa que son grandes, sino que se cae al ejecutarlo. Me comprometo a hacer lo mismo en C en un tiempo más.
Bueno, este es el código (modificado). Lo compilé con Free Pascal.
debugbat.pas
(*************************************************************************
===============================================
Hex-encode binary files in debug-script batches
===============================================
Copyright (c) 1993,1994 by Jos‚ Campione
Ottawa-Orleans Personal Systems Group
Fidonet: 1:163/513.3
This program reads a binary file and creates a hex-encoded
text file. Will use debug.exe or debug.com to
reconstruct the binary file.
**************************************************************************)
{$M 2048,0,0}
program debugbat;
uses crt,dos;
const
maxsize = $FFFF;
type
string2 = string[2];
var
ifile : file of byte;
ofile : text;
n : word;
s : word;
b : byte;
fsize : word;
dir : dirstr;
nam : namestr;
ext : extstr;
filename : string[48];
i : integer;
function b2x(b: byte): string2;
const hexdigit: array[0..15] of char = '0123456789ABCDEF';
begin
b2x:= hexdigit[b shr 4] + hexdigit[b and $0F];
end;
procedure myhalt(e: byte);
begin
gotoxy(1,wherey);
case e of
0 : writeln('done.');
1 : writeln('error in command line.');
2 : writeln('file exceeds the 65K limit.');
else begin
e:= 255;
writeln('Unknown error.');
end;
end;
halt(e);
end;
begin
writeln;
writeln('DEBUGBAT v.1.0 Copyright (c) Feb/93 by J. Campione.');
writeln('Modify by ...');
write('Wait... ');
n := 0;
s := $00;
{$I-}
assign(ifile,paramstr(1));
reset(ifile);
{$I+}
if (paramcount <> 1) or (ioresult <> 0) or (paramstr(1) = '') then myhalt(1);
fsplit(paramstr(1),dir,nam,ext);
for i:= 1 to length(ext) do ext[i]:= upcase(ext[i]);
if ext = '.EXE' then filename:= nam + '.ext'
else filename:= nam + ext;
fsize:= filesize(ifile);
if fsize > maxsize then myhalt(2);
assign(ofile, nam + '.src');
rewrite(ofile);
writeln(ofile,'N ',filename);
while not eof(ifile) do begin
n:= n + 1;
read(ifile,b);
if n mod 16 = 1 then begin
writeln(ofile);
write(ofile,'e ',b2x(hi(s)),b2x(lo(s)));
s := s + 16;
end;
write(ofile,' ',b2x(b));
end;
writeln(ofile);
writeln(ofile,'rcx');
writeln(ofile,b2x(hi(n)),b2x(lo(n)));
writeln(ofile,'w0');
writeln(ofile,'q');
close(ifile);
close(ofile);
myhalt(0);
end.
Se ejecuta así:
debugbat archivo
Luego, solo se toma el archivo resultante .src, y se hace en consola o desde un bat:
debug.exe < archivo.src
o
type archivo.src | debug.exe
Los archivos .exe se crearán como .ext (ya que sino debug arroja error)
Sugerencia obvia: Comprimir antes el ejecutable con upx. Revisar resultados, por ejemplo con fc.exe o comp.exe