UnRAR 5 Module/Wrapper Windows

Share your advanced PureBasic knowledge/code with the community.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

UnRAR 5 Module/Wrapper Windows

Post by ts-soft »

Code: Select all

DeclareModule unrar
  #ERAR_SUCCESS             = 0
  #ERAR_END_ARCHIVE         = 10
  #ERAR_NO_MEMORY           = 11
  #ERAR_BAD_DATA            = 12
  #ERAR_BAD_ARCHIVE         = 13
  #ERAR_UNKNOWN_FORMAT      = 14
  #ERAR_EOPEN               = 15
  #ERAR_ECREATE             = 16
  #ERAR_ECLOSE              = 17
  #ERAR_EREAD               = 18
  #ERAR_EWRITE              = 19
  #ERAR_SMALL_BUF           = 20
  #ERAR_UNKNOWN             = 21
  #ERAR_MISSING_PASSWORD    = 22
  
  #RAR_OM_LIST              = 0
  #RAR_OM_EXTRACT           = 1
  #RAR_OM_LIST_INCSPLIT     = 2
  
  #RAR_SKIP                 = 0
  #RAR_TEST                 = 1
  #RAR_EXTRACT              = 2
  
  #RAR_VOL_ASK              = 0
  #RAR_VOL_NOTIFY           = 1
  
  #RAR_DLL_VERSION          = 6
  
  #RAR_HASH_NONE            = 0
  #RAR_HASH_CRC32           = 1
  #RAR_HASH_BLAKE2          = 2
  
  #RHDF_SPLITBEFORE         = $01
  #RHDF_SPLITAFTER          = $02
  #RHDF_ENCRYPTED           = $04
  #RHDF_SOLID               = $10
  #RHDF_DIRECTORY           = $20

  Enumeration
    #UCM_CHANGEVOLUME
    #UCM_PROCESSDATA
    #UCM_NEEDPASSWORD
    #UCM_CHANGEVOLUMEW
    #UCM_NEEDPASSWORDW
  EndEnumeration
  
  Structure RARHeaderDataEx
    ArcName.b[1024]
    ArcNameW.w[1024]
    FileName.b[1024]
    FileNameW.w[1024]
    Flags.l
    PackSize.q
    UnpSize.q
    HostOS.l
    FileCRC.l
    FileTime.l
    UnpVer.l
    Method.l
    FileAttr.l
    *CmtBuf
    CmtBufSize.l
    CmtSize.l
    CmtState.l
    DictSize.l
    HashType.l
    Hash.b[32]
    Reserved.l[1014]
  EndStructure
  
  Structure RAROpenArchiveDataEx
    *ArcName
    *ArcNameW
    OpenMode.l
    OpenResult.l
    *CmtBuf
    CmtBufSize.l
    CmtSize.l
    CmtState.l
    Flags.l
    *Callback
    UserData.i
    Reserved.l[28]
  EndStructure

  Prototype UNRARCALLBACK(msg, UserData, P1, P2)
  Prototype CHANGEVOLPROC(ArcName.s, Mode)
  Prototype PROCESSDATAPROC(*Addr, Size)
  Prototype RAROpenArchive(*ArchiveData.RAROpenArchiveDataEx)
  Prototype RARCloseArchive(hArcData)
  Prototype RARReadHeader(hArcData, *HeaderData.RARHeaderDataEx)
  Prototype RARProcessFile(hArcData, Operation, DestPath.s, DestName.s)
  Prototype RARSetCallback(hArcData, *Callback.UNRARCALLBACK, UserData)
  Prototype RARSetChangeVolProc(hArcData, *ChangeVolProc.CHANGEVOLPROC)
  Prototype RARSetProcessDataProc(hArcData, *ProcessDataProc.PROCESSDATAPROC)
  Prototype RARSetPassword(hArcData, Password.p-ascii)
  Prototype RARGetDllVersion()
  
  Global RAROpenArchive.RAROpenArchive
  Global RARProcessFile.RARProcessFile
  Global RAROpenArchive.RAROpenArchive
  Global RARProcessFile.RARProcessFile
  Global RARReadHeader.RARReadHeader
  Global RARCloseArchive.RARCloseArchive
  Global RARSetCallback.RARSetCallback
  Global RARSetChangeVolProc.RARSetChangeVolProc
  Global RARSetProcessDataProc.RARSetProcessDataProc
  Global RARSetPassword.RARSetPassword
  Global RARGetDllVersion.RARGetDllVersion
  
  Declare RARUnpackArchiv(FileName.s, DestPath.s = "", Password.s = "", *Callback = 0)
EndDeclareModule

Module unrar
  EnableExplicit

  Define DLL
  
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
    DLL = OpenLibrary(#PB_Any, "unrar64.dll")
  CompilerElse
    DLL = OpenLibrary(#PB_Any, "unrar.dll")
  CompilerEndIf
  
  If DLL
    RAROpenArchive        = GetFunction(DLL, "RAROpenArchiveEx")
    CompilerIf #PB_Compiler_Unicode
    RARProcessFile        = GetFunction(DLL, "RARProcessFileW")
    CompilerElse
    RARProcessFile        = GetFunction(DLL, "RARProcessFile")
    CompilerEndIf
    RARReadHeader         = GetFunction(DLL, "RARReadHeaderEx")
    RARCloseArchive       = GetFunction(DLL, "RARCloseArchive")
    RARSetCallback        = GetFunction(DLL, "RARSetCallback")
    RARSetChangeVolProc   = GetFunction(DLL, "RARSetChangeVolProc")
    RARSetProcessDataProc = GetFunction(DLL, "RARSetProcessDataProc")
    RARSetPassword        = GetFunction(DLL, "RARSetPassword")
    RARGetDllVersion      = GetFunction(DLL, "RARGetDllVersion")
  EndIf
  
  Procedure Callback(msg, UserData, P1, P2)
    Protected PWD.s
    
    If UserData
      PWD = PeekS(UserData)
    EndIf
    Select msg
      Case #UCM_NEEDPASSWORD
        If PWD = ""
          PWD = InputRequester("Password", "Password required:", "", #PB_InputRequester_Password)
        EndIf
        If PWD
          PokeS(P1, PWD, P2, #PB_Ascii)
          ProcedureReturn #True
        EndIf
      Case #UCM_NEEDPASSWORDW
        If PWD = ""
          PWD = InputRequester("Password", "Password required:", "", #PB_InputRequester_Password)
        EndIf
        If PWD
          PokeS(P1, PWD, P2, #PB_Unicode)
          ProcedureReturn #True
        EndIf
    EndSelect
  EndProcedure
  
  Procedure RARUnpackArchiv(FileName.s, DestPath.s = "", Password.s = "", *Callback = 0)
    Protected raropen.RAROpenArchiveDataEx
    Protected rarheader.RARHeaderDataEx
    Protected hRAR, NoError = #True
    
    CompilerIf #PB_Compiler_Unicode
      raropen\ArcNameW = @Filename
    CompilerElse
      raropen\ArcName = @Filename
      If DestPath
        CharToOem_(DestPath, DestPath)
      EndIf
    CompilerEndIf
    raropen\OpenMode = #RAR_OM_EXTRACT
    If Password
      raropen\UserData = @Password
    EndIf
    If *Callback
      raropen\Callback = *Callback
    Else
      raropen\Callback = @Callback()
    EndIf
    
    
    hRAR = RAROpenArchive(raropen)
    
    If hRAR

      While RARReadHeader(hRAR, rarheader) = #ERAR_SUCCESS
        If RARProcessFile(hRAR, #RAR_EXTRACT, DestPath, #NULL$) <> #ERAR_SUCCESS
          NoError = #False
        EndIf
      Wend
      RARCloseArchive(hRAR)
    EndIf
    ProcedureReturn NoError
  EndProcedure
  
EndModule
You can download dlls/docu here: https://dl.dropboxusercontent.com/u/3086026/unrar.zip

Greetings - Thomas
Last edited by ts-soft on Sat Oct 05, 2013 3:31 pm, edited 2 times in total.
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: UnRAR 5 Module/Wrapper Windows

Post by davido »

@ts-soft

Great! Works like a dream!
Thank you very much for sharing.
DE AA EB
User avatar
ar-s
Enthusiast
Enthusiast
Posts: 340
Joined: Sat Oct 06, 2007 11:20 pm
Location: France

Re: UnRAR 5 Module/Wrapper Windows

Post by ar-s »

Fast and furious dude ;) thanks
~Ar-S~
My Image Hoster for PB users
My webSite (french) with PB apps : LDVMULTIMEDIA
PB - 3.x / 5.7x / 6 - W11 x64 - Ryzen 7 3700x / #Rpi4

Code: Select all

r3p347 : 7ry : un71l d0n3 = 1
sec
Enthusiast
Enthusiast
Posts: 790
Joined: Sat Aug 09, 2003 3:13 am
Location: 90-61-92 // EU or ASIA
Contact:

Re: UnRAR 5 Module/Wrapper Windows

Post by sec »

Code: Select all

#RAR_HASH_BLAKE2 = 2

        Debug rarheader\HashType
        
        For i = 0 To 31
          Debug Hex(rarheader\Hash[i],#PB_Byte)
        Next
Nice module, thanks.

RAR5 format is using BLAKE2sp to protect file data integrity (it doesn't use Keccak :shock: )
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: UnRAR 5 Module/Wrapper Windows

Post by ts-soft »

Update:

enhanced RARUnpackArchiv to use a callback (required for encrypted filenames in archiv)
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8433
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: UnRAR 5 Module/Wrapper Windows

Post by netmaestro »

This is great, thanks for it!
BERESHEIT
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: UnRAR 5 Module/Wrapper Windows

Post by rsts »

Your contributions are outstanding.

Thanks for another one.
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4749
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: UnRAR 5 Module/Wrapper Windows

Post by Fangbeast »

Does Unrar.dll support CAB files? I seem to remember reading somewhere that it might have but trying this code, I can see it not doing anything and thought a flag or something might be missing?

I need to be able to extract these.
Amateur Radio, D-STAR/VK3HAF
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: UnRAR 5 Module/Wrapper Windows

Post by ts-soft »

cab <> rar
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4749
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: UnRAR 5 Module/Wrapper Windows

Post by Fangbeast »

ts-soft wrote:cab <> rar
What does that nonsense answer mean? WinRAR supports cab files, I assumed the free unrar.dll might somehow but obviously not.

Thank you for your time.
Amateur Radio, D-STAR/VK3HAF
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: UnRAR 5 Module/Wrapper Windows

Post by ts-soft »

This module supports only unrar, so it is only for RAR-Format, no others formats!

WinRAR is a shareware windows packer/unpacker, that supports some other standard-format,
but this have nothing to do with unrar!

Greetings - Thomas
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
Post Reply