home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Anachriz / programming / crc32.asm.txt < prev    next >
Encoding:
Text File  |  2000-05-25  |  17.6 KB  |  702 lines

  1. ;Copyright (c) 1998,1999 by Anarchriz. All rights reserved.
  2. ; You may use parts of the code or the whole code if you credit me for it.
  3. ; There lies some hard work in it. One of the reasons I released it.
  4. ;Last modified: 09/01/99
  5.  
  6. .model tiny
  7. .stack 100h
  8.  
  9. poly            equ 0edb88320h   ;reflected 0x04c11db7h
  10. STEPSIZE        equ 1024
  11.  
  12. .data
  13.  
  14. ;Messages
  15. beginMsg        db 'Patcher 0.9 - Copyright (c) 1998,1999 by Anarchriz.',10,13,'$'
  16. helpMsg         db ' Help:',10,13
  17.                 db '  usage: patch [offset] [file1] [file2]',10,13
  18.                 db '   offset - the offset where to patch',10,13
  19.                 db '   file1  - the file to patch',10,13
  20.                 db '   file2  - the file to use while patching',10,13
  21.                 db '  when called with no parameters this help is displayed.',10,13,'$'
  22. invalidOffsetMsg db 'ERROR: the offset is no number or to long',10,13,'$'
  23. needFilesMsg    db 'ERROR: I need the filenames',10,13,'$'
  24. errorHelpMsg    db '> For help run the program with no parameters.',10,13,'$'
  25. closeErrorMsg   db 'ERROR: Error on closing ???',10,13,'$'
  26. file1notfoundMsg db 'ERROR: file1 not found',10,13,'$'
  27. file2notfoundMsg db 'ERROR: file2 not found',10,13,'$'
  28. file1OpenErrorMsg db 'ERROR: Error on opening file1',10,13,'$'
  29. file2OpenErrorMsg db 'ERROR: Error on opening file2',10,13,'$'
  30. file1ReadErrorMsg db 'ERROR: Error while reading file1',10,13,'$'
  31. file2ReadErrorMsg db 'ERROR: Error while reading file2',10,13,'$'
  32. filesizeErrorMsg db 'ERROR: Overflow in combination of filesizes and offset',10,13,'$'
  33. moveFileErrorMsg db 'ERROR: Error while moving filepointer',10,13,'$'
  34. writeFileErrorMSg db 'ERROR: Error while writing to file1',10,13,'$'
  35.  
  36. ;Variables
  37. parameterLen    db (?)
  38. parameterPos    dw 81h
  39. filehandle1     dw (0)
  40. filehandle2     dw (0)
  41. implantOffset   dd (?)
  42. crc32           dd 0ffffffffh
  43. filesize1       dd (?)
  44. filesize2       dd (?)
  45. bufferOffset    dw (?)
  46. basecrc         dd (?)
  47. originalEndCrc  dd (?)
  48. newEndCrc       dd (?)
  49. indexValues     dd (?)
  50. xorSequence     db 8 dup (0)
  51. parameterBuffer db 128 dup (?)
  52. crctable        dd 256 dup (?)
  53. readbuffer      db STEPSIZE dup (?)
  54. dta             db 128 dup (?)
  55.  
  56. .code
  57. .386
  58. start:
  59.         mov     ax, @data
  60.         mov     ds, ax
  61.  
  62.         call   InitTable
  63.  
  64.         mov     ah, 1ah
  65.         mov     dx, offset dta
  66.         int     21h             ;set dta address
  67.  
  68.         mov     dx, offset beginMsg
  69.         call   PrintMsg
  70.  
  71.         ;lees offset parameter uit
  72.         mov     al,[es:80h]     ;TO-DO : Check (if to big)
  73.         mov     [parameterLen],al
  74.  
  75.         call   GetNextParameter
  76.         cmp     ax, 0ffffh
  77.         je     printHelp
  78.  
  79.         call   ExtractOffset    ;extract 32 bit offset
  80.         cmp     eax, 0ffffffffh
  81.         je     invalidOffset
  82.         mov     [implantOffset],eax
  83.  
  84.         call   GetNextParameter ;get first filename
  85.         cmp     ax, 0ffffh
  86.         je     noMoreParameters
  87.  
  88.         mov     dx, offset parameterbuffer
  89.         call   FindFile
  90.         cmp     ax, 0ffffh
  91.         je     file1notfound     ;file1 not found
  92.  
  93.         mov     eax, dword ptr[dta+1ah]
  94.         mov     [filesize1], eax
  95.  
  96.         mov     dx, offset parameterbuffer
  97.         call   OpenFile
  98.         cmp     ax, 0ffffh
  99.         je     file1OpenError             ;error on opening file 1
  100.  
  101.         mov     [filehandle1], ax
  102.  
  103.         call   GetNextParameter ;get second filename
  104.         cmp     ax, 0ffffh
  105.         je     noMoreParameters
  106.  
  107.         mov     dx, offset parameterbuffer
  108.         call   FindFile
  109.         cmp     ax, 0ffffh
  110.         je     file2notfound     ;file2 not found
  111.  
  112.         mov     eax, dword ptr[dta+1ah]
  113.         mov     [filesize2], eax
  114.  
  115.         mov     dx, offset parameterbuffer
  116.         call   OpenFile
  117.         cmp     ax, 0ffffh
  118.         je     file2OpenError             ;error on opening file 2
  119.  
  120.         mov     [filehandle2], ax
  121.  
  122.         ;check if parameter info is right
  123.         mov     eax, [filesize2]
  124.         add     eax, 4                  ;correction for 4 xtra 'special' bytes
  125.         mov     ecx, [implantOffset]
  126.         add     eax, ecx
  127.         jc     filesizeError
  128.  
  129.         cmp     eax, [filesize1]
  130.         ja     filesizeError
  131.  
  132.         xor     eax, eax
  133.         xor     bx, bx
  134.         mov     si, offset filehandle1
  135.         mov     dx, offset readbuffer
  136.         ;ecx=offset
  137.         cmp     ecx, 0
  138.         je     noNeedForCompute
  139.         call   ComputeCRC32
  140.         cmp     ax, 0ffffh
  141.         je     readErrorCRC
  142.  
  143.  noNeedForCompute:
  144.         push    dword ptr[crc32]
  145.         pop     dword ptr[basecrc]
  146.  
  147.         mov     eax, [implantOffset]
  148.         mov     si, offset filehandle1   ;si is nog wel goed eigenlijk
  149.         call   MoveFile                 ;set file pointer back to implant offset
  150.         cmp     eax, 0ffffffffh
  151.         je     moveFileErrorMain
  152.  
  153.         mov     ecx, [filesize2]
  154.         add     ecx, 4                  ;correction for 4 xtra 'special' bytes
  155.         mov     si, offset filehandle1  ;si is nog wel goed eigenlijk
  156.         ;dx still pointer to readbuffer
  157.         call   ComputeCRC32            ;**************** move file
  158.         cmp     ax, 0ffffh
  159.         je     readErrorCRC
  160.  
  161.         push    dword ptr[crc32]
  162.         pop     dword ptr[originalEndCrc]
  163.  
  164.         push    dword ptr[basecrc]
  165.         pop     dword ptr[crc32]
  166.  
  167.         mov     ecx, [filesize2]
  168.         mov     si, offset filehandle2
  169.         ;dx still points to readbuffer
  170.         call   ComputeCRC32             ;first time I read in file2
  171.         cmp     ax, 0ffffh
  172.         je     readErrorCRC
  173.  
  174.         push    dword ptr[crc32]
  175.         pop     dword ptr[newEndCrc]
  176.  
  177.  ;*** And now the most important part: The Reverse Algorithm ***
  178.  
  179.         call   ComputeReverse
  180.  
  181.         mov     eax, [implantOffset]
  182.         mov     si, offset filehandle1
  183.         call   MoveFile
  184.         cmp     eax, 0ffffffffh
  185.         je     moveFileErrorMain
  186.  
  187.         xor     eax, eax
  188.         mov     si, offset filehandle2
  189.         call   MoveFile
  190.         cmp     eax, 0ffffffffh
  191.         je     moveFileErrorMain
  192.  
  193.         mov     dx, offset readbuffer
  194.  bigWriteLoop:
  195.         mov     si, offset filehandle2
  196.         call   ReadFile
  197.         cmp     ax,0ffffh
  198.         je     readFileError
  199.  
  200.         mov     si, offset filehandle1
  201.         ;ax is bytes to write
  202.         call   WriteFile
  203.         cmp     ax, 0ffffh
  204.         je     writeFileError
  205.  
  206.         cmp     ax, STEPSIZE
  207.         je     bigWriteLoop
  208.  
  209.         mov     dx, offset xorSequence
  210.         mov     ax, 4
  211.         call   WriteFile
  212.         cmp     ax, 0ffffh
  213.         je     writeFileError
  214.  
  215. closeFiles:
  216.         mov     si, offset filehandle1
  217.         call   CloseFile
  218.         cmp     ax, 0ffffh
  219.         je     closeError
  220.  
  221.         mov     si, offset filehandle2
  222.         call   CloseFile
  223.         cmp     ax, 0ffffh
  224.         je     closeError
  225.  
  226. exit:
  227.         mov     ax, 4c00h
  228.         int     21h
  229.  
  230. printHelp:
  231.         mov     dx, offset helpMsg
  232.         call   PrintMsg
  233.         jmp    exit
  234.  
  235. invalidOffset:
  236.         mov     dx, offset invalidOffsetMsg
  237.         call   PrintMsg
  238.         jmp    errorExit
  239.  
  240. noMoreParameters:
  241.         mov     dx, offset needFilesMsg
  242.         call   PrintMsg
  243.         jmp    closeFiles
  244.  
  245. file1notfound:
  246.         mov     dx, offset file1notfoundMsg
  247.         call   PrintMsg
  248.         jmp    closeFiles
  249.  
  250. file2notfound:
  251.         mov     dx, offset file2notfoundMsg
  252.         call   PrintMsg
  253.         jmp    closeFiles
  254.  
  255. file1OpenError:
  256.         mov     dx, offset file1OpenErrorMsg
  257.         call   PrintMsg
  258.         jmp    closeFiles
  259.  
  260. file2OpenError:
  261.         mov     dx, offset file2OpenErrorMsg
  262.         call   PrintMsg
  263.         jmp    closeFIles
  264.  
  265. filesizeError:
  266.         mov     dx, offset filesizeErrorMsg
  267.         call   PrintMsg
  268.         jmp    closeFiles
  269.  
  270. readErrorCrc:
  271.         mov     dx, offset file1ReadErrorMsg
  272.         call   PrintMsg
  273.         jmp    closeFiles
  274.  
  275. readFileError:
  276.         mov     dx, offset file2ReadErrorMsg
  277.         call   PrintMsg
  278.         jmp    closeFiles
  279.  
  280. moveFileErrorMain:
  281.         mov     dx, offset moveFileErrorMsg
  282.         call   PrintMsg
  283.         jmp    closeFiles
  284.  
  285. writeFileError:
  286.         mov     dx, offset writeFileErrorMsg
  287.         call   PrintMsg
  288.         jmp    closeFiles
  289.  
  290. closeError:
  291.         mov     dx, offset closeErrorMsg
  292.         call   PrintMsg
  293.         jmp    exit
  294.         
  295.  
  296. errorExit:
  297.         mov     dx, offset errorHelpMsg
  298.         call   PrintMsg
  299.         jmp    exit
  300.  
  301.  
  302. ;********************************
  303. ;*** Compute Reverse          ***
  304. ;********************************
  305. ;Parameters:    global originalEndCrc
  306. ;               global newEndCrc
  307. ;Returns:       in d[xorSequence+0] is the transformation message sequence
  308. ComputeReverse proc
  309.         push    eax
  310.         push    bx
  311.         push    di
  312.  
  313.         mov     di, 4
  314.  computeReverseLoop:
  315.         mov     al, byte ptr[originalEndCrc+di-1]
  316.         xor     al, byte ptr[xorSequence+di+3]
  317.         call   GetTableEntry
  318.         xor     dword ptr[xorSequence+di], eax
  319.         mov     byte ptr[indexValues+di-1], bl
  320.         dec     di
  321.         jnz    computeReverseLoop
  322.  
  323.         mov     eax, dword ptr[newEndCrc]
  324.         xor     dword ptr[xorSequence], eax
  325.         mov     eax, dword ptr[indexValues]
  326.         xor     dword ptr[xorSequence], eax
  327.  
  328.         pop     di
  329.         pop     bx
  330.         pop     eax
  331. ComputeReverse endp
  332.  
  333. ;********************************
  334. ;* Extract 32bit Offset         *
  335. ;********************************
  336. ;ax=length para
  337. ;return eax=0ffffffffh if toLong or noNumber ; else eax=para
  338. ExtractOffset proc
  339.         push    edx
  340.         push    bx
  341.         push    cx
  342.  
  343.         cmp     ax, 8
  344.         ja      toLong
  345.  
  346.         mov     bx, ax
  347.         xor     edx, edx
  348.         xor     cx, cx
  349.  ExtractOffsetLus:
  350.         xor     eax, eax
  351.         dec     bx
  352.         mov     al, [parameterBuffer+bx]
  353.         cmp     al, 30h
  354.         jb     noNumber
  355.         cmp     al, 39h
  356.         ja     checkABCDEF
  357.         and     al, 00001111b
  358.         jmp    readyForAdd
  359.  checkABCDEF:
  360.         and     al, 11011111b
  361.         cmp     al, 41h
  362.         jb     noNumber
  363.         cmp     al, 46h
  364.         ja     noNumber
  365.         sub     al, 37h
  366.  readyForAdd:
  367.         shl     eax, cl
  368.         or      edx, eax
  369.         add     cl, 4
  370.         or      bx, bx
  371.         jnz    ExtractOffsetLus
  372.  
  373.         mov     eax, edx
  374.  ExtractOffset_return:
  375.         pop     cx
  376.         pop     bx
  377.         pop     edx
  378.         ret
  379.  toLong:
  380.  noNumber:
  381.         mov     eax, 0ffffffffh
  382.         jmp    ExtractOffset_return
  383. ExtractOffset endp
  384.  
  385. ;********************************
  386. ;* Get next parameter           *
  387. ;********************************
  388. ;return: ax=0ffffh=no parameter found; else ax=length para
  389. GetNextParameter proc
  390.         push    cx
  391.         push    di
  392.         push    si
  393.         push    ds
  394.         push    es
  395.  
  396.         cld
  397.         xor     cx, cx
  398.         mov     cl, [parameterLen]
  399.         mov     al, 20h
  400.         mov     di, [parameterPos]
  401.         repe    scasb                   ;skip spaties
  402.         jcxz   no_parameter
  403.         dec     di
  404.         inc     cx
  405.         mov     si, di
  406.         repne   scasb                   ;zoek naar spatie
  407.         jcxz    no_correction
  408.         dec     di
  409.         inc     cx
  410.  
  411.  no_correction:
  412.         mov     [parameterLen], cl
  413.         mov     [parameterPos], di
  414.         sub     di, si                  ;verschil tussen indices= lengte para
  415.         mov     cx, di
  416.         mov     di, offset parameterBuffer
  417.  
  418.         mov     ax, ds                  ;/*
  419.         push    es
  420.         pop     ds
  421.         push    ax
  422.         pop     es                      ; xchg ds,es */
  423.  
  424.         mov     ax, cx                  ;maak kopietje van lengte para
  425.         rep     movsb                   ;copy from ds:si(psp) to es:di(buffer)
  426.         mov     byte ptr[es:di],0
  427.  
  428.  GetNextParameter_return:
  429.         pop     es
  430.         pop     ds
  431.         pop     si
  432.         pop     di
  433.         pop     cx
  434.         ret
  435. no_parameter:
  436.         mov     ax, 0ffffh
  437.         jmp    GetNextParameter_return
  438. GetNextParameter endp
  439.  
  440. ;*** Get Table Entry       ***
  441. ;Parameters:    al      first byte of Entry to search
  442. ;Returns:       eax     Entry
  443. ;               bx      entry number
  444. GetTableEntry proc
  445.         mov     bx, offset crctable-1
  446.  getTableEntry_loop:
  447.         add     bx, 4                ;points to crctable+k*4-1 (k:1..)
  448.         cmp     [bx], al
  449.         jne     getTableEntry_loop
  450.  
  451.         sub     bx, 3
  452.         mov     eax, [bx]
  453.         sub     bx, offset crctable
  454.         shr     bx, 2
  455.  
  456.         ret
  457. GetTableEntry endp
  458.  
  459. ;*****************************
  460. ;* Init Crc-Table            *
  461. ;*****************************
  462. InitTable proc
  463.         push    eax
  464.         push    ebx
  465.         push    cx
  466.  
  467.  
  468.         xor     ebx, ebx
  469. InitTableLus:
  470.         xor     eax, eax
  471.         mov     al, bl
  472.  
  473.         ;generate entry
  474.         mov     cl, 0
  475.  entrylus:
  476.         test    eax, 1
  477.         jz     no_topbit
  478.         shr     eax, 1
  479.         xor     eax, poly
  480.         jmp    entrygoon
  481.  no_topbit:
  482.         shr     eax, 1
  483.  entrygoon:
  484.         inc     cl
  485.         test    cl, 8
  486.         jz     entrylus
  487.  
  488.         mov     dword ptr[ebx*4 + crctable], eax
  489.         inc     bx
  490.         test    bx, 256
  491.         jz     InitTableLus
  492.  
  493.         pop     cx
  494.         pop     ebx
  495.         pop     eax
  496.         ret
  497. InitTable endp
  498.  
  499. ;*** Compute part of CRC-32 ***
  500. ;ds:dx=pointer buffer waarvan crc32 berekent wordt
  501. ;ax=aantal bytes die moet worden berekent
  502. ComputePartCRC32 proc
  503.         push    eax
  504.         push    ebx
  505.         push    cx
  506.         push    si
  507.  
  508.         mov     cx, ax
  509.         mov     si, dx
  510.         mov     eax, crc32
  511.  computeLus:
  512.         xor     ebx, ebx
  513.         xor     al, [si]
  514.         mov     bl, al
  515.         shr     eax, 8
  516.         xor     eax, dword ptr[4*ebx+crctable]
  517.         inc     si
  518.         loop   computeLus
  519.  
  520.         mov     [crc32], eax
  521.         ;mov     [bufferOffset], si
  522.  
  523.         pop     si
  524.         pop     cx
  525.         pop     ebx
  526.         pop     eax
  527.  
  528.         ret
  529. ComputePartCRC32 endp
  530.  
  531. ;*** Compute CRC-32    ***
  532. ;Parameters:    ds:dx   pointer readbuffer (STEPSIZE big)
  533. ;               ds:si   pointer filehandle
  534. ;               ecx     number of bytes to read
  535. ;Returns:       ax=ffffh on error
  536. ;               ax=0 on no error
  537. ComputeCRC32 proc
  538.         ;****** must implement pointer mover before readfile in main ******
  539.  readmore:
  540.         call   ReadFile
  541.         cmp     ax, 0ffffh
  542.         je     computeCRC_readerror
  543.  
  544.         ;ax holds read bytes
  545.  
  546.         cmp     ecx, STEPSIZE
  547.         jae    computecrc
  548.  
  549. ;        cmp     ax, 0
  550. ;        jne     beginOfBuffer
  551. ;        mov     dx, [bufferOffset]
  552. ; beginOfBuffer:
  553.  
  554.         mov     eax, ecx
  555.  
  556.  computecrc:
  557.         call   ComputePartCRC32
  558.         sub     ecx, eax
  559.         cmp     ecx, 0
  560.         ja     readmore
  561.         jb     computeCRC_readerror     ;unexpected EOF
  562.  
  563.         xor     eax, eax        ;everything is right
  564.         ret
  565.  
  566.  computeCRC_readerror:
  567.         mov     ax, 0ffffh
  568.         ret
  569. ComputeCRC32 endp
  570.  
  571. ;**************************
  572. ;*** File routines      ***
  573. ;**************************
  574. ;eax=offset ; ds:si=pointer filehandle
  575. ;return eax=0ffffffffh on error
  576. MoveFile proc
  577.         push    cx
  578.         push    dx
  579.  
  580.         mov     dx, ax
  581.         shr     eax, 16
  582.         mov     cx, ax
  583.  
  584.         mov     ax, 4200h
  585.         mov     bx, [si]
  586.         int     21h
  587.         jc     moveFileError
  588.  
  589.  moveFileReturn:
  590.         pop     dx
  591.         pop     cx
  592.         ret
  593.  
  594.  moveFileError:
  595.         mov     eax, 0ffffffffh
  596.         jmp     moveFileReturn
  597. MoveFile endp
  598.  
  599. ;ds:dx=pointer filename
  600. ;return: ax<0ffffh=handle.;ax=true=open error.
  601. OpenFile proc
  602.         mov     ax, 3d02h
  603.         int     21h
  604.         jc     open_error
  605.         
  606.         ret
  607.  open_error:
  608.         mov     ax, 0ffffh
  609.         ret
  610. OpenFile endp
  611.  
  612. ;ds:dx=pointer readbuffer ; ds:si=pointer filehandle
  613. ;return ax=ax=read bytes;ax=0ffffh=read error.
  614. ReadFile proc
  615.         push    bx
  616.         push    cx
  617.  
  618.         mov     ah, 3fh
  619.         mov     bx, [si]
  620.         mov     cx, STEPSIZE
  621.         int     21h
  622.         jc     read_error
  623.  
  624.  ReadFile_return:
  625.         pop     cx
  626.         pop     bx
  627.         ret
  628.  read_error:
  629.         mov     ax, 0ffffh
  630.         jmp    ReadFile_return
  631. ReadFile endp
  632.  
  633. ;ds:dx=pointer (write)readbuffer ; ds:si=pointer filehandle ; ax=bytes to write
  634. ;return ax=written bytes;ax=0ffffh=write error.
  635. WriteFile proc
  636.         push    bx
  637.         push    cx
  638.  
  639.         mov     cx, ax
  640.         mov     ah, 40h
  641.         mov     bx, [si]
  642.         int     21h
  643.         jc     write_error
  644.  
  645.  WriteFile_return:
  646.         pop     cx
  647.         pop     bx
  648.         ret
  649.  write_error:
  650.         mov     ax, 0ffffh
  651.         jmp    WriteFile_return
  652. WriteFile endp
  653.  
  654. ;ds:si=pointer filehandle
  655. CloseFile proc
  656.         push    bx
  657.         mov     ah, 3eh
  658.         mov     bx, [si]
  659.         cmp     bx, 0
  660.         je     doNotClose
  661.         int     21h
  662.         jc     close_error
  663.         mov     word ptr[si], 0
  664.  doNotClose:
  665.         xor     ax,ax
  666.  CloseFile_return:
  667.         pop     bx
  668.         ret
  669.  close_error:
  670.         mov     ax, 0ffffh
  671.         jmp    CloseFile_return
  672. CloseFile endp
  673.  
  674. ;ds:dx=point file spec
  675. ;return: ax=ffffh -> file not found els,e ax=n/a
  676. FindFile proc
  677.         push    cx
  678.  
  679.         xor     cx, cx
  680.         mov     ah, 4eh
  681.         int     21h
  682.         jc      not_found
  683.  
  684.  FindFile_return:
  685.         pop     cx
  686.         ret
  687.  
  688.  not_found:
  689.         mov     ax, 0ffffh
  690.         jmp    FindFile_return
  691. FindFile endp
  692.  
  693. PrintMsg proc
  694.         push    ax
  695.         mov     ah,9
  696.         int     21h
  697.         pop     ax
  698.         ret
  699. PrintMsg endp
  700.  
  701. end     start
  702.