Recent

Author Topic: [SOLVED]A case of blindness, probably. Old hands, plz help.  (Read 2727 times)

Thaddy

  • Hero Member
  • *****
  • Posts: 19235
  • Glad to be alive.
[SOLVED]A case of blindness, probably. Old hands, plz help.
« on: September 04, 2024, 09:40:16 am »
I found some old code by me, myself and I, but it is not correct and I can not see where it fails.
DON'T comment on the programming style - that is sound, but old -, but find the error, please... ;D
Code: Pascal  [Select][+][-]
  1. program hexdump;
  2. { Simple Hexdump, unix like clone.
  3.   Has error! Where?
  4.   This is an adapted version of my original.
  5.   (c)1991-2024, Thaddy de Koning.
  6.   Use as you like, no license }
  7. {$mode objfpc}{$I-}{$H-}
  8. const
  9.   maxchar = 15;
  10. type
  11.   TByteOrChar  = packed record
  12.   case boolean of
  13.     false:(asByte : packed array[0..maxchar] of byte);
  14.     true :(asChar : packed array[0..maxchar] of AnsiChar);
  15.   end;
  16.  
  17.   function ByteToHex(const value:byte):string;inline;
  18.   const
  19.     HexDigits = '0123456789ABCDEF';
  20.   begin
  21.     ByteToHex := HexDigits[hi(value)+1]+HexDigits[lo(value)+1];
  22.   end;
  23.  
  24. var
  25.   f: file of TByteOrChar;
  26.   bor: TByteOrChar;
  27.   i,r: integer;
  28. begin
  29.   if ParamCount > 0 then
  30.   begin
  31.     Assign(f,paramstr(1));
  32.     filemode := 0;
  33.     reset(f);
  34.     r:= IOResult;
  35.     if r = 0 then
  36.     begin
  37.       while not eof(f) do
  38.       begin
  39.         write(HexStr(FilePos(f) * 16,8):10);
  40.         read(f,bor);
  41.         for i := 0 to maxchar do
  42.         begin
  43.           write(ByteToHex(bor.asbyte[i]):3);
  44.           if (bor.asbyte[i] < 32 ) or  (bor.asbyte[i] > 127 ) then bor.aschar[i]:='.';
  45.         end;
  46.         writeln('|':2,bor.asChar,'|');
  47.       end;
  48.       close(f);
  49.     end
  50.     else
  51.       case r of
  52.       2:writeln('File not found');
  53.       5:writeln('Access denied');
  54.       else
  55.         writeln('A less common error occurred:',r:4);
  56.     end;
  57.  end
  58.  else
  59.    writeln('Use: hexdump <filename>');
  60. end.
Problem at hand is that I am missing the last line, compared to e.g. the standard hexdump on linux.
I can't see it? must be easy for old hands... 8-)
The error is the last line of the output missing.
   
« Last Edit: September 04, 2024, 10:14:06 am by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8572
Re: A case of blindness, probably. Old hands, plz help.
« Reply #1 on: September 04, 2024, 09:52:54 am »
Nothing springs out. I suspect that the issue is that the unix variant of the code first outputs an address and then looks to see how much (if any) data is to follow it, then repeats.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Zvoni

  • Hero Member
  • *****
  • Posts: 3396
Re: A case of blindness, probably. Old hands, plz help.
« Reply #2 on: September 04, 2024, 09:58:17 am »
Copy line 38 between line 46 and 47
The writing of the FilePos (L38) happens within the loop, so the moment Line39 returns EOF....

EDIT: Modified to reflect Mark's answer
Code: Pascal  [Select][+][-]
  1. program hexdump;
  2. { Simple Hexdump, unix like clone.
  3.   Has error! Where?
  4.   (c)1991-2024, Thaddy de Koning.
  5.   Use as you like, no license }
  6. {$mode objfpc}{$I-}{$H-}
  7. const
  8.   maxchar = 15;
  9. type
  10.   TByteOrChar  = packed record
  11.   case boolean of
  12.     false:(asByte : packed array[0..maxchar] of byte);
  13.     true :(asChar : packed array[0..maxchar] of AnsiChar);
  14.   end;
  15.  
  16.   function ByteToHex(const value:byte):string;inline;
  17.   const
  18.     HexDigits = '0123456789ABCDEF';
  19.   begin
  20.     ByteToHex := HexDigits[hi(value)+1]+HexDigits[lo(value)+1];
  21.   end;
  22.  
  23. var
  24.   f: file of TByteOrChar;
  25.   bor: TByteOrChar;
  26.   i,r: integer;
  27. begin
  28.   if ParamCount > 0 then
  29.   begin
  30.     Assign(f,paramstr(1));
  31.     filemode := 0;
  32.     reset(f);
  33.     r:= IOResult;
  34.     if r = 0 then
  35.     begin
  36.       write(HexStr(FilePos(f) * 16,8):10);   //Added/Moved
  37.       while not eof(f) do
  38.       begin
  39.         //write(HexStr(FilePos(f) * 16,8):10);
  40.         read(f,bor);
  41.         for i := 0 to maxchar do
  42.         begin
  43.           write(ByteToHex(bor.asbyte[i]):3);
  44.           if (bor.asbyte[i] < 32 ) or  (bor.asbyte[i] > 127 ) then bor.aschar[i]:='.';
  45.         end;
  46.         writeln('|':2,bor.asChar,'|');    
  47.         write(HexStr(FilePos(f) * 16,8):10);   //Added
  48.       end;
  49.       close(f);
  50.     end
  51.     else
  52.       case r of
  53.       2:writeln('File not found');
  54.       5:writeln('Access denied');
  55.       else
  56.         writeln('A less common error occurred:',r:4);
  57.     end;
  58.  end
  59.  else
  60.    writeln('Use: hexdump <filename>');
  61. end.
« Last Edit: September 04, 2024, 10:03:23 am by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Thaddy

  • Hero Member
  • *****
  • Posts: 19235
  • Glad to be alive.
Re: A case of blindness, probably. Old hands, plz help.
« Reply #3 on: September 04, 2024, 10:13:24 am »
TNX Zvoni and Mark! Identical output. (apart from the space, but that does not matter).
Sometimes you need 4 extra eyes... Again, thanks.
Code compiles in all modes that I use and is cross platform.
« Last Edit: September 04, 2024, 10:16:37 am by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

Zvoni

  • Hero Member
  • *****
  • Posts: 3396
Re: A case of blindness, probably. Old hands, plz help.
« Reply #4 on: September 04, 2024, 10:23:55 am »
Identical output. (apart from the space, but that does not matter).
Line 36 / 47 in my modified code: Remove the ":10" (or change to ":8")
In Line 43 add two leading spaces to the Write
*shrug*
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Thaddy

  • Hero Member
  • *****
  • Posts: 19235
  • Glad to be alive.
Re: [SOLVED]A case of blindness, probably. Old hands, plz help.
« Reply #5 on: September 04, 2024, 10:37:12 am »
Hey, Zvoni, it was a 33 year old bug... :P :-X I did only know about minix at the time...
Code compiles in the real TP7, btw. just remove inline.
« Last Edit: September 04, 2024, 10:44:35 am by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8572
Re: A case of blindness, probably. Old hands, plz help.
« Reply #6 on: September 04, 2024, 10:43:27 am »
EDIT: Modified to reflect Mark's answer

Sorry, I was getting ready to fire another radio test run on the hour, and really didn't want to get too deep into it.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Zvoni

  • Hero Member
  • *****
  • Posts: 3396
Re: [SOLVED]A case of blindness, probably. Old hands, plz help.
« Reply #7 on: September 04, 2024, 10:46:09 am »
Hey, Zvoni, it was a 33 year old bug... :P :-X I did only know about minix at the time...
Code compiles in the real TP7, btw. just remove inline.
Well then.... "Full steam ahead, and damn the torpedos".... or however that quote goes...  :D
EDIT: Modified to reflect Mark's answer

Sorry, I was getting ready to fire another radio test run on the hour, and really didn't want to get too deep into it.

MarkMLl

Nothing to be sorry about....
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Thaddy

  • Hero Member
  • *****
  • Posts: 19235
  • Glad to be alive.
Re: [SOLVED]A case of blindness, probably. Old hands, plz help.
« Reply #8 on: September 04, 2024, 10:46:43 am »
Well, we now have a small, old school, hexdump tool that is very useful. I wonder why I remembered that code and why? maybe somebody asked a question recently? I can't find anything or think of anything. Must be getting old.
« Last Edit: September 04, 2024, 10:49:06 am by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8572
Re: [SOLVED]A case of blindness, probably. Old hands, plz help.
« Reply #9 on: September 04, 2024, 10:57:40 am »
Well, we now have a small, old school, hexdump tool that is very useful. I wonder why I remembered that code and why? maybe somebody asked a question recently? I can't find anything or think of anything. Must be getting old.

Something I found /very/ useful, somewhat more than 33 years ago when there was significantly less prepackaged software around, was an undump program which allowed a hexdump to be edited as text and then converted back to a binary. I can't remember, but I might even have done that as an elementary patch program, i.e. it could seek into the binary file and change only the hex digits given (the others being dots as placeholders).

Since then there's been at least one commercial program to patch/relocate executables rather than sending out a whole binary, and I believe that Oracle now owns the rights to a way to patch the Linux kernel without having to reboot.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Thaddy

  • Hero Member
  • *****
  • Posts: 19235
  • Glad to be alive.
Re: [SOLVED]A case of blindness, probably. Old hands, plz help.
« Reply #10 on: September 04, 2024, 11:01:02 am »
Mark, I also wrote - and found - the reverse, but that is much more complicated to get to work.
That is basically a hex editor, based on one I wrote for my C64. One could not really work without hex editors in these days. (I lost a lot of code since 1982, not everything made it to DOS)
« Last Edit: September 04, 2024, 11:03:06 am by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

BrunoK

  • Hero Member
  • *****
  • Posts: 766
  • Retired programmer
Re: A case of blindness, probably. Old hands, plz help.
« Reply #11 on: September 04, 2024, 11:01:16 am »
That program seems to still not dump the end of the file.

Win10

Modifiying the comment to
Code: Pascal  [Select][+][-]
  1. { Simple Hexdump, unix like clone.
  2.   Has error! Where?
  3.   (c)1991-2024, Thaddy de Koning.
  4.   Use as you like, no license.
  5.   ~bk dumping itself still dump en of file }
   
and dumping the program itself gives an end of program
Quote
000005F0 20 20 77 72 69 74 65 6C 6E 28 27 55 73 65 3A 20 |  writeln('Use: |
00000600 68 65 78 64 75 6D 70 20 3C 66 69 6C 65 6E 61 6D |hexdump <filenam|
00000610

Thaddy

  • Hero Member
  • *****
  • Posts: 19235
  • Glad to be alive.
Re: [SOLVED]A case of blindness, probably. Old hands, plz help.
« Reply #12 on: September 04, 2024, 11:04:46 am »
What do you mean? That is not clear to me.I get the same output as linux hexdump after Zvoni's fix.
objects are fine constructs. You can even initialize them with constructors.

BrunoK

  • Hero Member
  • *****
  • Posts: 766
  • Retired programmer
Re: [SOLVED]A case of blindness, probably. Old hands, plz help.
« Reply #13 on: September 04, 2024, 11:13:40 am »
What do you mean? That is not clear to me.I get the same output as linux hexdump after Zvoni's fix.
I mean that dumping the program itself should also display the end of the source that is
Code: Pascal  [Select][+][-]
  1. >');
  2. end.

Zvoni

  • Hero Member
  • *****
  • Posts: 3396
Re: [SOLVED]A case of blindness, probably. Old hands, plz help.
« Reply #14 on: September 04, 2024, 01:10:26 pm »
It's true. There is something fishy going on.
As if EOF(f) becomes true before reaching the end of the file
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

 

TinyPortal © 2005-2018