LisaList2

General Category => LisaList2 => Lisa Workshop => Topic started by: sigma7 on July 26, 2025, 04:22:32 am

Title: Hex Dump Exec Script
Post by: sigma7 on July 26, 2025, 04:22:32 am
A script to dump a few bytes from a file in hex.

Excruciatingly slowly prints some bytes from a file in hexadecimal. I cobbled this trying to figure out what was happening when uploading text files that have characters with the high bit set. I don't think it is particularly useful, but it does serve as a warning about trying something so ambitious.

WARNINGS:
Do not casually ask for more than say 256 bytes, or to dump at an offset more than that. If you do, you will be resetting the machine or waiting...
As written, the script cannot be interrupted (other than dropping into the debugger, but I don't know what you would do next).

To use this:
examples:
r<HD(n68k.err)                 dump 16 bytes starting at the first one to the console
r<HD.TEXT(diff.obj,,32)     dump 32 bytes starting at the first one to the console
r<hd(n68k.err,8,2)            dump 2 bytes starting at the eighth one to the console
r<hd(n68k.err,,99999,N68k.err.hexdump.text)            dump many bytes to the file "N68k.err.hexdump.text"

Code: [Select]
EXEC (filename,start,count,outfile)
  IF filename = '?' THEN
    WRITELN 'Parameters: (filename,start[1],count[16],outfile[-console])'
    WRITELN 'Dump "count" bytes from "filename" to "outfile" in hex beginning at byte number "start"'
    HALT
  ENDIF
  DEFAULT start to 1
  DEFAULT count to 16
  DEFAULT outfile to '-console'
  WRITELN 'Hex dump of ',count,' bytes from ',filename,', to ',outfile,', starting at byte #',start
  RESET fref,filename
  REWRITE outref,outfile
  IF IORESULT <> '' THEN
    HALT IORESULT
  ENDIF
  SET %3 TO 1
  WHILE %3 LT start DO
    READCH (fref) %4
    if %4 = 'EOF' THEN
      WRITELN 'EOF reached prematurely at ',%3,' bytes'
      HALT
    ELSEIF IORESULT <> '' THEN
      HALT IORESULT
    ENDIF
    SET %3 to EVAL(%3 + 1)
  ENDWHILE
  SET %3 TO 0
  SET %9 to count
  WHILE %3 LT %9 DO
    READCH (fref) %4
    if %4 = 'EOF' THEN
      WRITELN 'EOF reached after ',%3,' bytes were dumped'
      HALT
    ELSEIF IORESULT <> '' THEN
      HALT IORESULT
    ENDIF
    SET %5 TO ORD (%4)
    SET %6 TO EVAL (%5 MOD 16 + 1)
    SET %7 TO EVAL (%5 / 16 + 1)
    SET %8 TO CONCAT ( COPY ('0123456789ABCDEF',%7,1), COPY ('0123456789ABCDEF',%6,1))
    WRITE (outref) %8,' '
    IF IORESULT <> '' THEN
      HALT IORESULT
    ENDIF
    SET %3 to EVAL(%3 + 1)
    SET %5 to EVAL (%3 MOD 16)
    IF %5 EQ 0 THEN
      WRITELN (outref) ' '
    ENDIF
    IF %3 GE %9 THEN
      IF outfile = '-console' THEN
        WRITE ' -- Enter space to dump ',count,' more, '
        REQUEST %1 WITH 'anything else to cancel...'
        IF %1 = ' ' THEN
          SET %9 TO EVAL(count + %9)
        ELSE
          CLOSE fref
          HALT 'Done'
        ENDIF
      ELSE
        CLOSE fref
        CLOSE outref
        HALT 'Done'     
      ENDIF
    ENDIF
  ENDWHILE
ENDEXEC

BUGS etc.:

edit: fixed EOF, implemented output to a file