TGC Codebase Backup



UnRAR DLL System by Peace

14th Nov 2004 18:00
Summary

Unrar packed archive with password and comment



Description

For full information about the UnRAR DLL System source you can download the added zip archive UnRar_Example.zip with unrar.dll. The included example will explain how to load an rar packed and encrypted picture, also it will present an included comment in archive if avail.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    Remstart
   ------------------------------------------------------------------
   Author:        Volker Stepprath of Testaware
   Project:       UnRAR DLL System
   Version:       0.00.003 ;D
   Language:      DarkBASIC Professional v1.057
   Description:   UnRAR a packed archive (include password & comment)
   Date:          16. November 2004 / 17:08:25
   ------------------------------------------------------------------
   Hint 1:        I used constants and types for better codestructure
   Hint 2:        Use functions as include in your own sources
   Hint 3:        See unRAR.dll manual for more details
   ------------------------------------------------------------------
Remend

#CONSTANT RAR_OM_LIST          0
#CONSTANT RAR_OM_EXTRACT       1
#CONSTANT RAR_SKIP             0
#CONSTANT RAR_TEST             1
#CONSTANT RAR_EXTRACT          2

#CONSTANT ERAR_END_ARCHIVE    10
#CONSTANT ERAR_NO_MEMORY      11
#CONSTANT ERAR_BAD_DATA       12
#CONSTANT ERAR_BAD_ARCHIVE    13
#CONSTANT ERAR_UNKNOWN_FORMAT 14
#CONSTANT ERAR_EOPEN          15
#CONSTANT ERAR_ECREATE        16
#CONSTANT ERAR_ECLOSE         17
#CONSTANT ERAR_EREAD          18
#CONSTANT ERAR_EWRITE         19
#CONSTANT ERAR_SMALL_BUF      20
#CONSTANT ERAR_UNKNOWN        21

#CONSTANT ERAR_NO_DLL         99 : `* ERAR_NO_DLL = not an official constant

TYPE  struc_MPTR
      RARArchiveName     : Rem * MemBlk to initializing archivename
      RAROpenArchiveData : Rem * MemBlk for unpacked archivinformation
      RARHeaderData      : Rem * MemBlk for headerinformationen
      RARSetPassword     : Rem * Memblk to initializing password
      RARCmtBuf          : Rem * MemBlk to store archive comments
ENDTYPE

MPTR  AS struc_MPTR

`* -----------------------------------------------------------------------

xFileName$="Data.rar"                     : `* Filename of RAR packed archive
xPassWord$="Atomix 2004 v1.03.172"        : `* Password (if archive encrypted)

`* -----------------------------------------------------------------------

xTrap=rar_DLLInit()                        : if xTrap then goto Trap_Skip
xTrap=rar_DLLUnpack(xFileName$,xPassWord$) : if xTrap then goto Trap_Skip

`* -----------------------------------------------------------------------

Trap_Skip:
if xTrap>ERAR_END_ARCHIVE
   print "UnRAR DLL System returned error: ";rar_GetErrorText(xTrap)
else
   wait key
   load bitmap "Atomix2004Screens.bmp",0  : `* Display unpacked mediafile ;D
   delete file "Atomix2004Screens.bmp"    : `* Delete mediafile after display
endif

`* -----------------------------------------------------------------------

rar_DLLClose()                            : `* When finished programm we must close library

wait key
exit prompt "That's all. Feel free to enjoy source for your own ;D","UnRAR DLL System"
END

FUNCTION rar_DLLInit

   `* UnRAR DLL initializing
   `* --------------------------------------------------------------------
   `* Here the unrar.dll will be open and it function can use to work
   `* All global memoryblocks will be reserve
   `* If an error occurr it will be return in param
   `* Remark: I don't like the memblock commands, coz they will increase
   `*         executable filsize larger than 5 MB :(
   `* --------------------------------------------------------------------

   `* Memoryblocks global used by unrar.dll
   MPTR.RARArchiveName     = 1
   MPTR.RAROpenArchiveData = 2
   MPTR.RARHeaderData      = 3
   MPTR.RARSetPassword     = 4
   MPTR.RARCmtBuf          = 5

   if file exist("unrar.dll")                   : `* Test if library avail?
      load dll "unrar.dll",1                    : `* RAR Unpacklibrary open
      make memblock MPTR.RARArchiveName,    260 : `* Mem for archivename
      make memblock MPTR.RAROpenArchiveData, 28 : `* Mem for opened archiveinformation
      make memblock MPTR.RARHeaderData,     600 : `* Mem for packed every packed entry
      make memblock MPTR.RARSetPassword,    260 : `* Mem for password
      make memblock MPTR.RARCmtBuf,       16384 : `* Mem for comments (=16kb max. 64kb)
   else
      xTrap=ERAR_NO_DLL                         : `* Error-Code: library not avail (99)
   endif

ENDFUNCTION xTrap

FUNCTION rar_DLLClose

   `* UnRAR DLL close
   `* --------------------------------------------------------------------
   `* If unrar.dll needed no more, we should close it
   `* All memoryblocks will deleted, too
   `* --------------------------------------------------------------------

   if dll exist(1)                            : `* Was library opened?
      delete dll 1                            : `* Library close
      delete memblock MPTR.RARArchiveName     : `* Clear all memoryblocks
      delete memblock MPTR.RAROpenArchiveData : `* ..
      delete memblock MPTR.RARHeaderData      : `* ..
      delete memblock MPTR.RARSetPassword     : `* ..
      delete memblock MPTR.RARCmtBuf          : `* ..
   endif

ENDFUNCTION

FUNCTION rar_DLLUnpack(a$,b$)

   `* Unpacking RAR-Archiv
   `* --------------------------------------------------------------------
   `* Functioncall: a$=Name of packed archive
   `*               b$=Password (if needed)
   `* --------------------------------------------------------------------
   `* After all variables initialized (s. rar_DLLInit()), we can unrar
   `* the given archive. An optional parameter can set as password. All
   `* included files will be unpacked in actual path.
   `* --------------------------------------------------------------------

   if dll exist(1)

      `* Archivename as *Ptr, terminated with NULL byte
      for i=1 to len(a$)
         write memblock byte MPTR.RARArchiveName,i-1,asc(mid$(a$,i))
      next i
      write memblock byte MPTR.RARArchiveName,len(a$),0

      `* Password as *Ptr, must have if archive could be encrypted
      if b$<>""
         for i=1 to len(b$)
            write memblock byte MPTR.RARSetPassword,i-1,asc(mid$(b$,i))
         next i
         write memblock byte MPTR.RARSetPassword,len(b$),0
      endif

      `* Enter archive (a$), command (RAR_OM_EXTRACT) & commentbuf (CmtBuf) in structure
      write memblock dword MPTR.RAROpenArchiveData, 0,get memblock ptr(MPTR.RARArchiveName)
      write memblock dword MPTR.RAROpenArchiveData, 4,RAR_OM_EXTRACT
      write memblock dword MPTR.RAROpenArchiveData,12,get memblock ptr(MPTR.RARCmtBuf)
      write memblock dword MPTR.RAROpenArchiveData,16,get memblock size(MPTR.RARCmtBuf)

      `* Open packed archve
      hArcData=call dll(1,"RAROpenArchive",get memblock ptr(MPTR.RAROpenArchiveData))

      OpenResult=memblock word(MPTR.RAROpenArchiveData,8)

      `* If no error occurred, all entries will be unpacked
      if OpenResult=0

         `* If given a password, it will be initalize here (void)
         if b$<>"" then call dll 1,"RARSetPassword",hArcData,get memblock ptr(MPTR.RARSetPassword)

         `* If there was comments included, here we will display it
         rar_ArchiveComment()

         `* Repeat if no more entries or error (OutputParameter<>0)
         repeat

            `* Get informationen of packed filedes
            OutputParameter=call dll(1,"RARReadHeader",hArcData,get memblock ptr(MPTR.RARHeaderData))

            `* Show fileinformations (rar_FileInfo()) and unpack it
            if OutputParameter=0
               print rar_GetFileInfo()
               OutputParameter=call dll(1,"RARProcessFile",hArcData,RAR_EXTRACT,0,0)
            endif

         until OutputParameter

         `* If occurred an error we get to notice it
         if OutputParameter<>ERAR_END_ARCHIVE then OpenResult=OutputParameter

         `* Close packed archive (void)
         call dll 1,"RARCloseArchive",hArcData

      endif

   else

      OpenResult=ERAR_NO_DLL : `* UnRAR DLL not avail (s. rar_DLLInit())

   endif

ENDFUNCTION OpenResult

FUNCTION rar_GetFileInfo

   `* Get information of packed entry
   `* --------------------------------------------------------------------
   `* Every packed file has its own infoheader
   `* If interested, you can add perhaps a % ratio information ;D
   `* --------------------------------------------------------------------

   for a=260 to 520
      b=memblock byte(MPTR.RARHeaderData,a)
      if b>0 then a$=a$+chr$(b) else exit
   next a

   a=100-(100.0/memblock dword(MPTR.RARHeaderData,528))*memblock dword(MPTR.RARHeaderData,524)

   a$="File: "+a$
   a$=a$+" - Packed: "+str$(memblock dword(MPTR.RARHeaderData,524))
   a$=a$+" - Unpack: "+str$(memblock dword(MPTR.RARHeaderData,528))
   a$=a$+" - Gain: "  +str$(a)+"%"

ENDFUNCTION a$

FUNCTION rar_GetErrorText(a)

   `* Get full errormsg
   `* --------------------------------------------------------------------
   `* Functioncall: a=OpenResult (s. rar_DLLUnpack())
   `* --------------------------------------------------------------------

   select a
      case ERAR_END_ARCHIVE    : a$="No more entries"              : endcase
      case ERAR_NO_MEMORY      : a$="Not enough memory"            : endcase
      case ERAR_BAD_DATA       : a$="Archive header broken"        : endcase
      case ERAR_BAD_ARCHIVE    : a$="File is not RAR archive"      : endcase
      case ERAR_UNKNOWN_FORMAT : a$="Unknown archive format"       : endcase
      case ERAR_EOPEN          : a$="Volume open error"            : endcase
      case ERAR_ECREATE        : a$="File create error"            : endcase
      case ERAR_ECLOSE         : a$="File close error"             : endcase
      case ERAR_EREAD          : a$="Read error"                   : endcase
      case ERAR_EWRITE         : a$="Write error"                  : endcase
      case ERAR_SMALL_BUF      : a$="Comments not completely read" : endcase
      case ERAR_UNKNOWN        : a$="Unknown error"                : endcase
      case ERAR_NO_DLL         : a$="UnRAR DLL not avail"          : endcase
   endselect

   if a$<>"" then a$="("+str$(a)+") "+a$

ENDFUNCTION a$

FUNCTION rar_ArchiveComment

   `* Get rar included archive comments
   `* --------------------------------------------------------------------
   `* Some packed rar archives included different comments (see on the right)
   `* This is a simple example how we can display it for further informations
   `* --------------------------------------------------------------------

   CmtState=memblock dword(MPTR.RAROpenArchiveData,24)   : `* Testflag if comment include

   if CmtState=1                                         : `* Comments read completely (=1)

      CmtSize=memblock dword(MPTR.RAROpenArchiveData,20) : `* Get the bytesize of comment

      print "Commentsize: ";CmtSize;" bytes"

      for a=0 to CmtSize-1
         b=memblock byte(MPTR.RARCmtBuf,a)
         if b>13
            a$=a$+chr$(b)
         else
            inc a
            print a$
            a$=""
         endif
      next a

   endif

ENDFUNCTION