home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 113 / EnigmaAmiga113CD.iso / software / sviluppo / xfd_developer / sources / asm / ilscuro.a < prev    next >
Encoding:
Text File  |  2000-06-27  |  10.3 KB  |  453 lines

  1. **********************************************************
  2. **     XFD external decruncher for Il Scuro/Defjam!     **
  3. **       written and © 1998 by Codetapper/Action!       **
  4. **                                                      **
  5. **    This decrunches old games crunched by Il Scuro    **
  6. **    of Defjam. The cruncher used has a bytekiller     **
  7. **      type address cruncher followed by a (lame)      **
  8. **    run length encoded variant. I've called it the    **
  9. **     $B2 RLE decruncher as the RLE flag is a byte     **
  10. **                   encoded as $b2.                    **
  11. **                                                      **
  12. **    Files packed with this often require a little     **
  13. **    disassembling to setup intena/intreq and very     **
  14. **       occasionally a hunk of memory is further       **
  15. **    encrypted. You'll have to disasseble the main     **
  16. **              program to check for this.              **
  17. **                                                      **
  18. **     This was the case with Army Moves and a few      **
  19. **                 other older cracks.                  **
  20. **                                                      **
  21. **      Detection is based on finding all cruncher      **
  22. **    sections (main cruncher, $b2 RLE, jump address    **
  23. **      info and the long 'PROG' before the data).      **
  24. **     Sometimes it will not detect it (Arkanoid 2)     **
  25. **     due to different ordering and the load/jump      **
  26. **   addresses in weird locations.  You'll just have    **
  27. **            to disassemble these yourself!            **
  28. **                                                      **
  29. **          Contact me: codetapper@hotmail.com          **
  30. **      Visit the Action HQ: http://zap.to/action/      **
  31. **                                                      **
  32. **           Greetings to all Action members:           **
  33. **        Tachyon, Elp, Legionary, DJ DeCreator,        **
  34. **                  Hoju, and Shastar!                  **
  35. **********************************************************
  36.  
  37.         INCLUDE    AINCLUDE:IncDirs.i
  38.  
  39. IlScuroMinLen    equ    $1000        ;Just a guess!
  40.  
  41.         SECTION    SWITCH,CODE
  42.         INCLUDE    "libraries/xfdmaster.i"
  43.  
  44. ; xfdForeman structure MUST be first thing in all external decrunchers
  45.  
  46. Forman        moveq    #-1,d0        ;security
  47.         rts
  48.         dc.l    XFDF_ID        ;id
  49.         dc.w    1        ;version
  50.         dc.w    0
  51.         dc.l    0,0        ;private
  52.         dc.l    S_IlScuro    ;first slave
  53.  
  54.         dc.b    "$VER: IlScuro 1.3 (25.07.1999) by Codetapper/Action!",13,10,0
  55.         cnop    0,4
  56.  
  57. ;---------------------------------------------------------
  58. ; Files packed by Il Scuro/Defjam.
  59. ;
  60. ; Decruncher is the same, and "PROG" is located just
  61. ; before the crunched data. This is sometimes moved
  62. ; around or encrypted over itself. Then it is often
  63. ; RLE data so a further decrunch is required!
  64. ;---------------------------------------------------------
  65. ; xfdSlave structure
  66.  
  67. S_IlScuro
  68.     dc.l    0        ;next slave
  69.     dc.w    2        ;version
  70.     dc.w    36        ;master version
  71.     dc.l    N_IlScuro    ;name
  72.     dc.w    XFDPFF_ADDR    ;flags
  73.     dc.w    0
  74.     dc.l    RB_IlScuro    ;recog buffer
  75.     dc.l    DB_IlScuro    ;decrunch buffer
  76.     dc.l    0        ;recog segment
  77.     dc.l    0        ;decrunch segment
  78.     dc.w    0,0        ;slave/replace id
  79.     dc.l    IlScuroMinLen    ;min. file length for header and data
  80.  
  81. N_IlScuro
  82.     dc.b    'Il Scuro/Defjam Compacted',0
  83.     even
  84.  
  85. ;---------------------------------------------------------
  86. ; Recog buffer function: receives buffer + length in a0/d0
  87. ;
  88. ; Returns:
  89. ; d0 = 1 if Il Scuro file, 0 if not!
  90. ; d2 = Offset into file for crunched data
  91. ; d3 = RLE destination address
  92. ; d4 = Jump address
  93. ; d5 = Destination address
  94. ; d6 = Crunched RLE length
  95. ;---------------------------------------------------------
  96.  
  97. RB_IlScuro    MOVE.L    D7,-(A7)
  98.         MOVE.L    D0,D7
  99.         BEQ.B    .SkipSave1
  100.         MOVEM.L    D2-D6,-(A7)
  101. .SkipSave1
  102.     move.l    a0,a1        ;a1 = Start of file
  103. .1    move.l    a0,d1
  104.     sub.l    a1,d1
  105.     cmp.l    #IlScuroMinLen,d1
  106.     bgt    .NoIlScuro    ;Too far into file for a match so quit!
  107.     cmp.w    #$41fa,(a0)+    ;lea PackedData(pc),a0
  108.     bne.b    .1
  109.     move.l    a0,d1
  110.     sub.l    a1,d1
  111.     moveq    #0,d2
  112.     move.w    (a0)+,d2    ;d2 = Offset into file for crunched data
  113.     add.l    d1,d2
  114.     cmp.w    #$d1fc,(a0)+    ;add.l    #$CrunchedLength,a0
  115.     bne.b    .1
  116.     move.l    (a0)+,d1
  117.     add.l    d1,d2        ;This cruncher expects data at the end!
  118.     cmp.w    #$43f9,(a0)+    ;lea    #$DestinationAddress,a1
  119.     bne.b    .1
  120.     move.l    (a0)+,d3    ;d3 = RLE destination address
  121.     cmp.w    #$2460,(a0)+    ;move.l    -(a0),a2
  122.     bne.b    .1
  123.     cmp.w    #$d5c9,(a0)+    ;add.l    a1,a2
  124.     bne.b    .1
  125.     cmp.w    #$4aa0,(a0)+    ;tst.l    -(a0)
  126.     bne.b    .1
  127.     cmp.w    #$2020,(a0)+    ;move.l    -(a0),d0
  128.     bne.b    .1
  129.  
  130. ; Check for the second part of the decruncher, which has some
  131. ; optional screen flashing before it.
  132.  
  133. .2    move.l    a0,d1
  134.     sub.l    a1,d1
  135.     cmp.l    #IlScuroMinLen,d1
  136.     bgt    .NoIlScuro    ;Too far into file for a match so quit!
  137.  
  138.     cmp.w    #$7203,(a0)+    ;moveq    #3,d1
  139.     bne.b    .2
  140.     cmp.w    #$6100,(a0)+    ;bsr    IlDec20
  141.     bne.b    .2
  142.  
  143. ; Now check for the RLE decoder.
  144.  
  145. .3    move.l    a0,d1
  146.     sub.l    a1,d1
  147.     cmp.l    #IlScuroMinLen,d1
  148.     bgt.b    .NoIlScuro    ;Too far into file for a match so quit!
  149.     cmp.w    #$4ef9,(a0)+    ;jmp StartAddress
  150.     bne.b    .3
  151.     move.l    (a0)+,d4    ;d4 = Jump address
  152.     cmp.w    #$45f9,(a0)+    ;lea $RLECrunchedAddress,a2
  153.     bne.b    .3
  154.     addq    #4,a0
  155.     cmp.w    #$47f9,(a0)+    ;lea $DestinationAddress,a3
  156.     bne.b    .3
  157.     move.l    (a0)+,d5    ;d5 = Destination address
  158.     cmp.l    #$c1200b2,(a0)+    ;cmp.b    #$B2,(a2)
  159.     bne.b    .3
  160.     cmp.l    #$67000008,(a0)+    ;beq    B2Found
  161.     bne.b    .3
  162.     cmp.w    #$16da,(a0)+    ;move.b    (a2)+,(a3)+
  163.     bne.b    .3
  164.     cmp.l    #$6000fff4,(a0)+    ;bra    CheckRLE
  165.     bne.b    .3
  166.     cmp.w    #$528a,(a0)+    ;addq.l    #1,a2
  167.     bne.b    .3
  168.     cmp.l    #$c120001,(a0)+    ;cmp.b    #1,(a2)
  169.     bne.b    .3
  170.     cmp.l    #$66000004,(a0)+    ;bne    CheckJustB2
  171.     bne.b    .3
  172.     cmp.w    #$4e75,(a0)+    ;rts
  173.     bne.b    .3
  174.     cmp.l    #$c120000,(a0)+    ;cmp.b    #0,(a2)
  175.     bne.b    .3
  176.  
  177. ; Now for the final check, locate "PROG" in the data.
  178.  
  179. .4    move.l    a0,d1
  180.     sub.l    a1,d1
  181.     cmp.l    #IlScuroMinLen,d1
  182.     bgt.b    .NoIlScuro    ;Too far into file for a match so quit!
  183.     cmp.w    #'PR',(a0)+    ;Search for 'PROG'
  184.     bne.b    .4
  185.     cmp.w    #'OG',(a0)+
  186.     bne.b    .4
  187.  
  188.     moveq    #1,d0        ;We'll quit here, it matches!
  189.     bra.b    .end
  190.  
  191. .NoIlScuro
  192.     moveq    #0,d0        ;No match for this data.
  193. .end        TST.L    D7
  194.         BEQ.B    .SkipSave2
  195.         MOVEM.L    (A7)+,D2-D6
  196. .SkipSave2    MOVE.L    (A7)+,D7
  197.         RTS
  198.  
  199. ;---------------------------------------------------------
  200. ; Decrunch buffer function: receives bufferinfo in a0
  201. ;---------------------------------------------------------
  202. DB_IlScuro
  203.     movem.l    d2-d7/a2-a6,-(a7)
  204.     move.l    a0,a5
  205.     move.l    xfdbi_SourceBuffer(a5),a2
  206.  
  207. ; It's impossible to calculate the correct decrunched 
  208. ; length without actually passing through the file
  209. ; twice. We'll just allocate the full 512k of his old
  210. ; cracks. In about 20 games I've never seen one which
  211. ; was more than 512k so this should be OK.
  212.  
  213.     move.l    #$80000,d0
  214.     move.l    d0,xfdbi_TargetBufSaveLen(a5)
  215.     move.l    d0,xfdbi_TargetBufLen(a5)
  216.  
  217.     move.l    xfdbi_TargetBufMemType(a5),d1
  218.     move.l    4.w,a6
  219.     jsr    -198(a6)
  220.     move.w    #XFDERR_NOMEMORY,xfdbi_Error(a5)
  221.     move.l    d0,xfdbi_TargetBuffer(a5)
  222.     beq.b    .Exit
  223.  
  224. ; Let's fill the buffer!
  225.  
  226.     move.l    xfdbi_SourceBuffer(a5),a0
  227.     CLR.L    D0    * indicate to ignore register saves!
  228.     BSR    RB_IlScuro    ; d2 = Offset into file for crunched data
  229.                 ; d3 = RLE destination address
  230.                 ; d4 = Jump address
  231.                 ; d5 = Destination address
  232.                 ; d6 = Crunched RLE length
  233.     tst.l    d0
  234.     beq.b    .Exit
  235.  
  236.     move.l    d5,xfdbi_DecrAddress(a5)
  237.     move.l    d4,xfdbi_JmpAddress(a5)
  238.  
  239.     move.l    xfdbi_SourceBuffer(a5),a0
  240.     add.l    d2,a0
  241.  
  242.     move.l    xfdbi_TargetBuffer(a5),a1
  243.     add.l    d3,a1
  244.  
  245. ; Unpack the data (from a0 to a1)
  246.  
  247.     bsr.b    D_IlScuro
  248.  
  249. ; Unpack the $B2 RLE data (from a2 to a3)
  250.  
  251.     move.l    xfdbi_TargetBuffer(a5),a3
  252.     move.l    a1,a2
  253.     bsr    B2RLEDecode
  254.     move.l    d0,xfdbi_TargetBufSaveLen(a5)
  255.  
  256. ; Successful exit
  257.  
  258.     moveq    #1,d0
  259. .Exit    movem.l    (a7)+,d2-d7/a2-a6
  260.     rts
  261.  
  262. ;---------------------------------------------------------
  263. ; Actual IlScuro decruncher
  264. ;
  265. ; Inputs:
  266. ; a0 = END of packed data
  267. ; a1 = Destination address
  268. ;
  269. ; Outputs:
  270. ; d0 = Uncrunched length. All other registers preserved.
  271. ;---------------------------------------------------------
  272. D_IlScuro
  273.     movem.l    a0-a6,-(a7)
  274. ;    lea    PackedData(pc),a0    ; Original source
  275. ;    add.l    #$2A0F4,a0        ; a0 = End of source
  276. ;    lea    $3DE24,a1        ; a1 = Destination address
  277.     move.l    -(a0),a2
  278.     add.l    a1,a2
  279.     tst.l    -(a0)
  280.     move.l    -(a0),d0
  281. IlDec1    ;move.w    a2,$DFF180
  282.     moveq    #3,d1
  283.     bsr    IlDec20
  284.     tst.w    d2
  285.     beq.s    IlDec8
  286.     cmp.w    #7,d2
  287.     bne.s    IlDec4
  288.     lsr.l    #1,d0
  289.     bne.s    IlDec2
  290.     bsr    IlDec19
  291. IlDec2    bcc.s    IlDec3
  292.     moveq    #10,d1
  293.     bsr    IlDec20
  294.     tst.w    d2
  295.     bne.s    IlDec4
  296.     moveq    #$12,d1
  297.     bsr    IlDec20
  298.     bra.s    IlDec4
  299.  
  300. IlDec3    moveq    #4,d1
  301.     bsr    IlDec20
  302.     addq.w    #7,d2
  303. IlDec4    subq.w    #1,d2
  304. IlDec5    moveq    #7,d1
  305. IlDec6    lsr.l    #1,d0
  306.     beq.s    IlDec7
  307.     roxl.l    #1,d3
  308.     dbra    d1,IlDec6
  309.  
  310.     move.b    d3,-(a2)
  311.     dbra    d2,IlDec5
  312.  
  313.     bra.s    IlDec8
  314.  
  315. IlDec7    move.l    -(a0),d0
  316.     move    #$10,CCR
  317.     roxr.l    #1,d0
  318.     roxl.l    #1,d3
  319.     dbra    d1,IlDec6
  320.  
  321.     move.b    d3,-(a2)
  322.     dbra    d2,IlDec5
  323.  
  324. IlDec8    cmp.l    a2,a1
  325.     bge.s    IlDec18
  326.     moveq    #2,d1
  327.     bsr    IlDec20
  328.     moveq    #2,d3
  329.     moveq    #8,d1
  330.     tst.w    d2
  331.     beq.s    IlDec16
  332.     moveq    #4,d3
  333.     cmp.w    #2,d2
  334.     beq.s    IlDec13
  335.     moveq    #3,d3
  336.     cmp.w    #1,d2
  337.     beq.s    IlDec11
  338.     moveq    #2,d1
  339.     bsr.s    IlDec20
  340.     cmp.w    #3,d2
  341.     beq.s    IlDec10
  342.     cmp.w    #2,d2
  343.     beq.s    IlDec9
  344.     addq.w    #5,d2
  345.     move.w    d2,d3
  346.     bra.s    IlDec13
  347.  
  348. IlDec9    moveq    #2,d1
  349.     bsr.s    IlDec20
  350.     addq.w    #7,d2
  351.     move.w    d2,d3
  352.     bra.s    IlDec13
  353.  
  354. IlDec10    moveq    #8,d1
  355.     bsr.s    IlDec20
  356.     move.w    d2,d3
  357.     bra.s    IlDec13
  358.  
  359. IlDec11    moveq    #8,d1
  360.     lsr.l    #1,d0
  361.     bne.s    IlDec12
  362.     bsr.s    IlDec19
  363. IlDec12    bcs.s    IlDec16
  364.     moveq    #14,d1
  365.     bra.s    IlDec16
  366.  
  367. IlDec13    moveq    #$10,d1
  368.     lsr.l    #1,d0
  369.     bne.s    IlDec14
  370.     bsr.s    IlDec19
  371. IlDec14    bcc.s    IlDec16
  372.     moveq    #8,d1
  373.     lsr.l    #1,d0
  374.     bne.s    IlDec15
  375.     bsr.s    IlDec19
  376. IlDec15    bcs.s    IlDec16
  377.     moveq    #12,d1
  378. IlDec16    bsr.s    IlDec20
  379.     subq.w    #1,d3
  380. IlDec17    move.b    -1(a2,d2.l),-(a2)
  381.     dbra    d3,IlDec17
  382.  
  383.     cmp.l    a2,a1
  384.     blt    IlDec1
  385. IlDec18    movem.l    (a7)+,a0-a6
  386.     rts
  387.  
  388. IlDec19    move.l    -(a0),d0
  389.     move    #$10,CCR
  390.     roxr.l    #1,d0
  391.     rts
  392.  
  393. IlDec20    subq.w    #1,d1
  394.     clr.l    d2
  395. IlDec21    lsr.l    #1,d0
  396.     beq.s    IlDec22
  397.     roxl.l    #1,d2
  398.     dbra    d1,IlDec21
  399.  
  400.     rts
  401.  
  402. IlDec22    move.l    -(a0),d0
  403.     move    #$10,CCR
  404.     roxr.l    #1,d0
  405.     roxl.l    #1,d2
  406.     dbra    d1,IlDec21
  407.  
  408.     rts
  409.  
  410. ;---------------------------------------------------------
  411. ; $B2 RLE Decoder as used by Il Scuro/Defjam
  412. ;
  413. ; Inputs:
  414. ; a2 = Source address
  415. ; a3 = Destination address
  416. ;
  417. ; Outputs:
  418. ; d0 = Uncrunched length. All other registers preserved.
  419. ;---------------------------------------------------------
  420.  
  421. B2RLEDecode
  422.     movem.l    d7/a2-a3,-(a7)
  423. CheckRLE
  424.     cmp.b    #$b2,(a2)
  425.     beq.b    B2Found
  426.     move.b    (a2)+,(a3)+
  427.     bra.b    CheckRLE
  428.  
  429. B2Found    addq.l    #1,a2
  430.     cmp.b    #1,(a2)
  431.     bne.b    CheckJustB2
  432.  
  433.     move.l    a3,d0        ;a3 = End of unpacked data
  434.     movem.l    (a7)+,d7/a2-a3
  435.     sub.l    a3,d0
  436. ;    subq    #1,d0        ;d0 = Uncrunched length
  437.     rts
  438.  
  439. CheckJustB2    tst.b    (a2)
  440.     bne.b    CopyXBytes
  441.     move.b    #$b2,(a3)+
  442.     addq.l    #1,a2
  443.     bra.b    CheckRLE
  444.  
  445. CopyXBytes    move.b    (a2)+,d7
  446. CopyLoop    move.b    (a2),(a3)+
  447.     subq.b    #1,d7
  448.     bne.b    CopyLoop
  449.     addq.l    #1,a2
  450.     bra.b    CheckRLE
  451.  
  452.     END
  453.