home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / mskermit / msbmkb.asm < prev    next >
Assembly Source File  |  2020-01-01  |  9KB  |  429 lines

  1.     TITLE    MSBMKB MS-DOS System EXE to BOO Utility Program
  2.  
  3. ; (c) 1988 W.C. Parke, WGCP0086@GWUVM
  4. ;
  5. ; This program converts an EXE to a BOO file 
  6. ;
  7. ;  BOO files are ASCII files constructed from binary files for the purpose
  8. ;  of transferring the files over networks or tape which do not accept binary.
  9. ;  The translation is by the following algorithm (by Bill Catchings): 
  10. ;    1. The name of the file is placed on the first line, in plain text,
  11. ;       followed by CR,LF.
  12. ;    2. If a series of nulls occurs, a '~' (7EH) is used followed by
  13. ;       an byte number for the null count from 2 to 78 plus '0' bias.
  14. ;    3. Three byte sequences are coded as 4 bytes, taking lower 6 bits from
  15. ;       each byte and adding a '0' bias.
  16. ;    4. Carriage return/linefeeds are inserted to make lines breaks
  17. ;       at column 76 unless a split of 4 byte code would occur.  If so,
  18. ;       line ends at column 74.
  19. ;
  20. ; MSBMKB compiled with MASM 5.10.  Use LINK and EXE2BIN to make COM file.
  21. ;
  22. ; May be used with any file on any MS-DOS path.
  23. ; BOO file is created in current directory.
  24. ;
  25. ; MSBMKB uses the file name on the first line of the BOO file
  26. ; for its output file.
  27.  
  28. LF    EQU    0AH
  29. CR    EQU    0DH
  30. BUFSIZ    EQU    6000H
  31.  
  32. CODE    SEGMENT
  33.     ASSUME    CS:CODE,DS:CODE,ES:CODE,SS:CODE
  34.  
  35.     ORG    100H
  36.  
  37. START:    JMP    BOO
  38. FHAND1    DW    0
  39.     DW    0
  40. FNAM1    DB    80 DUP(0)
  41. FHAND2    DW    0
  42.     DW    0
  43. FNAM2    DB    16 DUP(0)
  44.     DW    0
  45. FNAMT    DB    16 DUP(0)
  46. INVOC    DB    CR,LF,'MSMKB Version 1.1'
  47.     DB    CR,LF,'EXE to BOO file converter.'
  48.     DB    CR,LF,'(c) 1988 W.C. Parke',CR,LF,CR,LF,'$'
  49. DHPERR    DB    'Syntax:',CR,LF,LF
  50.     DB    '  MSBMKB filename',CR,LF,CR,LF,'$'
  51. DCONV    DB    'Converting $'
  52. DTO    DB    ' to $'
  53. DDOT    DB    ' ... $'
  54. DEXIST    DB    ' already exists. Overwrite it? $'
  55. DERROR    DB    CR,LF,'Error in file $'
  56. DOPERR    DB    'open$'
  57. DCRERR    DB    'creation$'
  58. DCLERR    DB    'close$'
  59. DRDERR    DB    'read$'
  60. DWRERR    DB    'write$'
  61. DERRF    DB    '.',CR,LF,'$'
  62. FBOO    DB    '.BOO',0,0
  63. DONE    DB    CR,LF,'Done. ',CR,LF,'$'
  64. FEOF    DB    0
  65. FEND    DW    0
  66. FEND2    DW    0
  67.  
  68. BOO:    MOV    AH,9
  69.     MOV    DX,OFFSET INVOC
  70.     INT    21H            ; give our invocation
  71.     MOV    SI,80H
  72.     LODSB
  73.     XOR    AH,AH
  74.     AND    AX,AX
  75.     JNZ    BOF            ; got command line tail
  76.     JMP    ABORTHP            ; give help screen
  77. BOF:    MOV    DI,OFFSET FNAM1
  78.     INC    AX
  79.     MOV    CX,AX
  80. BO0:    LODSB                ; get byte of command line tail
  81.     CMP    AL,CR            ; check for cr termination
  82.     JZ    BO1
  83.     CMP    AL,20H
  84.     JZ    BOL            ; check for spaces
  85.     CMP    AL,'a'
  86.     JC    BOLL
  87.     AND    AL,5FH
  88. BOLL:    STOSB                ; save in file name buffer
  89. BOL:    LOOP    BO0            ; continue
  90. BO1:    XOR    AL,AL
  91.     STOSB                ; make asciiz string
  92.     DEC    DI            ; point to 0
  93.     MOV    AX,OFFSET FNAM1
  94.     MOV    CX,DI
  95.     SUB    CX,AX
  96.     MOV    FEND,CX            ; size of file and path name to 0
  97.     INC    CX
  98.     INC    CX
  99.     STD
  100.     MOV    AL,'\'
  101.     REPNZ    SCASB
  102.     CLD
  103.     INC    DI
  104.     INC    DI        ; pointer to fnam1 without path
  105.     MOV    SI,DI
  106.     MOV    DI,OFFSET FNAM2
  107.     XOR    CX,CX
  108. BON:    LODSB
  109.     INC    CX
  110.     AND    AL,AL
  111.     STOSB            ; move copy to fnam2
  112.     JZ    BOM
  113.     JMP    SHORT BON
  114. BOM:    DEC    CX
  115.     MOV    FEND2,CX    ; size of fnam2
  116.     MOV    SI,OFFSET FNAM2
  117.     MOV    DI,OFFSET FNAMT
  118.     MOV    CX,8
  119.     REP    MOVSW        ; copy file name for header of BOO file
  120.     MOV    DX,OFFSET FNAM1
  121.     MOV    AH,3DH        ; open file 1
  122.     INT    21H
  123.     JNC    BO2
  124.     JMP    ABORTOP        ; give error on open message
  125. BO2:    MOV    FHAND1,AX    ; save handle
  126.     MOV    DI,OFFSET FNAM2
  127.     MOV    CX,FEND2
  128.     ADD    DI,CX
  129.     PUSH    DI        ; save ptr to 0
  130.     STD
  131.     INC    CX
  132.     MOV    AL,'.'
  133.     REPNZ    SCASB        ; look for '.' in string from rear
  134.     CLD
  135.     JCXZ    BO3        ; not found
  136.     INC    DI        ; point to '.'
  137.     POP    AX        ; drop old 0 pointer
  138.     PUSH    DI        ; use new pointer
  139. BO3:    POP    SI    ; pointer to extension in FNAM1
  140.     MOV    DI,SI
  141.     MOV    SI,OFFSET FBOO
  142.     MOV    CX,3
  143.     REP    MOVSW        ; add BOO extension to fnam2
  144.     MOV    DX,OFFSET FNAM2
  145.     XOR    CX,CX
  146.     MOV    AH,4EH
  147.     INT    21H        ; search file
  148.     JC    BOZ
  149.     MOV    SI,OFFSET FNAM2
  150.     CALL    SHOW
  151.     MOV    AH,9
  152.     MOV    DX,OFFSET DEXIST    ; already exists
  153.     INT    21H
  154.     MOV    AX,0C01H
  155.     INT    21H            ; look for user response
  156.     PUSH    AX
  157.     MOV    AH,2
  158.     MOV    DL,CR
  159.     INT    21H
  160.     MOV    DL,LF
  161.     INT    21H
  162.     POP    AX
  163.     AND    AL,5FH
  164.     CMP    AL,'Y'
  165.     JZ    BOZ            ; got overwrite permission
  166.     JMP    EXIT
  167. BOZ:    MOV    DX,OFFSET FNAM2
  168.     MOV    AH,3CH
  169.     XOR    CX,CX
  170.     INT    21H        ; create output file
  171.     JC    ABORTCR
  172.     MOV    FHAND2,AX    ; save handle
  173.     MOV    AH,9
  174.     MOV    DX,OFFSET DCONV
  175.     INT    21H        ; say converting
  176.     MOV    SI,OFFSET FNAM1
  177.     CALL    SHOW
  178.     MOV    AH,9
  179.     MOV    DX,OFFSET DTO
  180.     INT    21H
  181.     MOV    SI,OFFSET FNAM2
  182.     CALL    SHOW
  183.     MOV    AH,9
  184.     MOV    DX,OFFSET DDOT
  185.     INT    21H
  186.     MOV    SI,OFFSET FNAMT
  187.     XOR    AX,AX
  188.     MOV    BP,AX            ; line column count
  189.     MOV    CNT1,AX
  190.     MOV    CNT2,AX            ; zero buffer counts
  191.     MOV    DI,OFFSET FBUF2
  192.     MOV    CX,16            ; do no more than 16
  193. BO4:    LODSB
  194.     AND    AL,AL            ; end of asciiz string
  195.     JZ    BO5
  196.     SUB    AL,'0'            ; anticipate bias
  197.     CALL    OUTB            ; send to file2 buffer
  198.     LOOP    BO4
  199. BO5:    CALL    OUTCR            ; send lf,cr
  200.     MOV    SI,OFFSET FBUF1
  201.     JMP    NEXT            ; start exe file conversion
  202. ABORTOP:
  203.     MOV    DX,OFFSET DOPERR    ; file open error
  204.     MOV    AL,2
  205.     JMP    ABORT
  206. ABORTCR:
  207.     MOV    DX,OFFSET DCRERR    ; file creation error
  208.     MOV    AL,3
  209.     JMP    ABORT
  210. ABORTCL:
  211.     MOV    DX,OFFSET DCLERR    ; file close error
  212.     MOV    AL,7
  213.     JMP    ABORT
  214. PUBLIC NULLS
  215. NUL1:    MOV    BL,AL            ; put byte-2 after null
  216.     XOR    BH,BH            ; put null for byte-1
  217.     JMP    SHORT NEX3    ; process next byte for byte-3
  218. NULLS:    CALL    GETB            ; process multiple nulls
  219.     AND    AL,AL
  220.     JNZ    NUL1            ; only one null
  221.     CMP    BP,75        ; columns count - 2
  222.     JC    NULG
  223.     CALL    OUTCR
  224. NULG:    MOV    AL,'~'-'0'        ; ascii null flag
  225.     CALL    OUTB
  226.     MOV    CX,77            ; lowest count is 2
  227.     MOV    BX,78            ; largest count allowed
  228. NULSN:    CALL    GETB
  229.     AND    AL,AL
  230.     LOOPZ    NULSN
  231. NULF:    PUSH    AX            ; save non-null for byte-1
  232.     SUB    BX,CX            ; find null count
  233.     MOV    AX,BX
  234.     CALL    OUTB
  235.     POP    AX
  236.     JMP    SHORT NEX1
  237. PUBLIC NEXT
  238. NEXT:    CALL    GETB        ; get byte-1 xxxxxxxx
  239. NEX1:    AND    AL,AL
  240.     JZ    NULLS        ; got a null
  241.     MOV    AH,AL
  242.     CALL    GETB        ; get byte-2 yyyyyyyy
  243.     MOV    BX,AX
  244. NEX3:    CALL    GETB        ; get byte-3 zzzzzzzz
  245.     MOV    CH,AL
  246.     MOV    AX,BX
  247.     SHR    AX,1
  248.     SHR    AX,1
  249.     XCHG    AH,AL
  250.     CMP    BP,73    ; columns count - 4
  251.     JC    NEXG
  252.     CALL    OUTCR
  253. NEXG:    CALL    OUTB    ; send 00xxxxxx
  254.     XCHG    AL,AH
  255.     SHR    AL,1
  256.     SHR    AL,1
  257.     CALL    OUTB    ; send 00xxyyyy
  258.     MOV    AH,BL    
  259.     AND    AH,0FH
  260.     MOV    AL,CH
  261.     SHL    AX,1
  262.     SHL    AX,1
  263.     XCHG    AH,AL
  264.     CALL    OUTB    ; send 00yyyyzz
  265.     MOV    AL,AH
  266.     SHR    AL,1
  267.     SHR    AL,1
  268.     CALL    OUTB    ; send 00zzzzzz, fourth of quarted
  269.     JMP    NEXT
  270.  
  271. PUBLIC GETB
  272. CNT1    DW    0        ; byte count in buffer 1
  273. EXTRA    DB    3        ; extra byte count to finish ascii quartet
  274. GETB:    CMP    WORD PTR CNT1,0
  275.     JZ    GETBF        ; get more from file
  276. GETBG:    LODSB
  277.     DEC    WORD PTR CNT1    ; on less in buffer
  278.     RET
  279. GETBF:    CALL    GETMF        ; file get
  280.     JNC    GETBG        ; got more file bytes
  281.     TEST    BYTE PTR FEOF,0FFH
  282.     JZ    GETBEC        ; error if not end of file
  283.     DEC    EXTRA
  284.     CMP    EXTRA,0        ; any extra bytes to add?
  285.     JZ    FIN        ; no
  286.     MOV    AL,90H        ; add extra 'nop' to file
  287.     RET
  288. GETBEC:    POP    AX        ; drop return address
  289.     JMP    ABORTWR
  290. PUBLIC FIN
  291. FIN:    POP    AX        ; drop return address
  292.     CALL    OUTLST            ; save last of converted file
  293.     JNC    EXIT
  294.     JMP    ABORTCL        ; close error
  295. EXIT:    MOV    AH,9
  296.     MOV    DX,OFFSET DONE
  297.     INT    21H
  298.     MOV    AX,4C00H
  299.     INT    21H            ; good exit
  300.  
  301. GETMFF:    STC            ; got eof
  302.     RET
  303. GETMF:    TEST    BYTE PTR FEOF,0FFH
  304.     JNZ    GETMFF
  305.     PUSH    AX
  306.     PUSH    BX
  307.     PUSH    CX
  308.     PUSH    DX
  309.     MOV    BX,FHAND1
  310.     MOV    DX,OFFSET FBUF1
  311.     MOV    AH,3FH
  312.     MOV    CX,BUFSIZ    ; file buffer size
  313.     INT    21H        ; get bytes
  314.     MOV    SI,OFFSET FBUF1
  315.     POP    DX
  316.     POP    CX
  317.     POP    BX
  318.     JC    GETBC        ; read error
  319.     MOV    CNT1,AX        ; new byte count
  320.     AND    AX,AX
  321.     JZ    GETBE        ; no more bytes
  322. GETBC:    POP    AX
  323.     RET
  324. GETBE:    INC    BYTE PTR FEOF    ; set eof
  325.     POP    AX
  326.     STC
  327.     RET
  328.  
  329. OUTCR:    XOR    BP,BP        ; new column
  330.     PUSH    AX
  331.     MOV    AL,0DDH    ; cr-'0'
  332.     CALL    OUTB        ; send cr
  333.     MOV    AL,0DAH    ; lf-'0'
  334.     CALL    OUTB        ; send lf
  335.     POP    AX
  336.     XOR    BP,BP        ; reset column position
  337.     RET
  338.  
  339. PUBLIC OUTB
  340. CNT2    DW    0        ; buffer 2 character count
  341. OUTBB:    CALL    OUTBF        ; write buffer contents
  342.     JC    OUTBCA        ; write error
  343.     JMP    SHORT OUTBS    ; start filling new buffer
  344. OUTB:    CMP    CNT2,BUFSIZ    ; any space in buffer?
  345.     JNC    OUTBB        ; no, write buffer
  346. OUTBS:    ADD    AL,'0'        ; add bias to byte 
  347.     STOSB            ; save character
  348.     INC    BP        ; increment column position
  349.     INC    WORD PTR CNT2    ; and characters in buffer
  350.     CLC
  351.     RET
  352. OUTBCA:    POP    AX
  353.     JMP    ABORTWR        ; write error
  354.  
  355. OUTBF:    PUSH    AX
  356.     PUSH    BX
  357.     PUSH    CX
  358.     PUSH    DX
  359.     MOV    BX,WORD PTR FHAND2
  360.     MOV    AH,40H
  361.     MOV    DX,OFFSET FBUF2
  362.     MOV    CX,BUFSIZ
  363.     INT    21H        ; write file 2 buffer
  364.     POP    DX
  365.     POP    CX
  366.     POP    BX
  367.     JC    OUTBC        ; write error
  368.     CMP    AX,BUFSIZ
  369.     JNZ    OUTBC        ; carry set if incomplete write
  370.     MOV    WORD PTR CNT2,0    ; reset character count
  371.     MOV    DI,OFFSET FBUF2    ; and buffer pointer
  372. OUTBC:    POP    AX
  373.     RET
  374. OUTLST:    CALL    OUTCR        ; send final cr,lf
  375.     MOV    BX,WORD PTR FHAND2
  376.     MOV    AH,40H
  377.     MOV    DX,OFFSET FBUF2
  378.     MOV    CX,CNT2
  379.     INT    21H        ; write
  380.     MOV    AH,3EH
  381.     INT    21H        ; and close
  382.     RET
  383. ABORTHP:
  384.     MOV    DX,OFFSET DHPERR
  385.     MOV    AH,9
  386.     INT    21H        ; show help screen
  387.     MOV    AX,4C01H
  388.     INT    21H
  389. ABORTRD:
  390.     MOV    DX,OFFSET DRDERR    ; read error
  391.     MOV    AL,4
  392.     JMP    SHORT ABORT
  393. ABORTWR:
  394.     MOV    DX,OFFSET DWRERR    ; write error
  395.     MOV    AL,6
  396. ABORT:    CALL    ABERR            ; error in file ...
  397.     PUSH    AX
  398.     MOV    AH,9
  399.     INT    21H            ; ...(error type)
  400.     MOV    DX,OFFSET DERRF        ; period
  401.     INT    21H
  402.     POP    AX            ; errorlevel in AL
  403.     MOV    AH,4CH
  404.     INT    21H
  405. ABERR:    PUSH    AX
  406.     PUSH    DX
  407.     MOV    DX,OFFSET DERROR
  408.     MOV    AH,9
  409.     INT    21H            ; show error in file
  410.     POP    DX
  411.     POP    AX
  412.     RET
  413.     EVEN
  414. SHOW:    MOV    AH,2            ; show asciiz string
  415. SHOM:    LODSB
  416.     AND    AL,AL
  417.     JZ    SHOE
  418.     MOV    DL,AL
  419.     INT    21H
  420.     JMP    SHORT SHOM
  421. SHOE:    RET    
  422.  
  423. FBUF1    EQU    $
  424. FBUF2    EQU    $+BUFSIZ
  425.  
  426. CODE    ENDS
  427.  
  428.     END    START
  429.