Results 1 to 14 of 14
  1. #1
    PJo is offline Novice
    Windows 10 Office 365
    Join Date
    Apr 2021
    Location
    Province du Québec
    Posts
    9

    Unworking code to compress string.

    Hi everyone,



    I'm working with security coding. I found code for RC4 encryption here, which come from this site. Short AES code here (sorry, it's in French, my mother language). Short Base64 code here. Bytes to Hex and Hex to Bytes here. String to Hex and Hex to String here. Hash MD5, SHA1, SHA128, SHA384, SHA512 and their HMAC version here.

    Now, I'm trying to compress string. I found some "ready" code for VBA here. There are 2 errors in the VB7 functions compress and Decompress using Access x64. pUncompressedData and pCompressedDataBuffer must be As LongPtr because StrPtr() «returns a LongPtr on a 64 bit version». Here's how I wrote the code:
    Code:
    Option Compare Database
    Option Explicit
    
    ' https://www.codegrepper.com/code-examples/vb/excel+vba+compress+string
    Private Declare PtrSafe Function compress Lib "cabinet" Alias "Compress" (ByVal hCompressor As Long, ByVal pUncompressedData As LongPtr, ByVal sizeUncompressedData As Long, ByVal pCompressedDataBuffer As LongPtr, ByVal sizeCompressedBuffer As Long, ByVal bytesOut As Long) As Long
    Private Declare PtrSafe Function Decompress Lib "cabinet" (ByVal hCompressor As Long, ByVal pCompressedData As LongPtr, ByVal sizeCompressedData As Long, ByVal pUncompressedDataBuffer As LongPtr, ByVal sizeOfUncompressedBuffer As Long, ByVal bytesOut As Long) As Long
    Private Declare PtrSafe Function CreateCompressor Lib "cabinet" (ByVal CompressAlgorithm As Long, ByVal pAllocationRoutines As Long, hCompressor As Long) As Long
    Private Declare PtrSafe Function CreateDecompressor Lib "cabinet" (ByVal CompressAlgorithm As Long, ByVal pAllocationRoutines As Long, hDecompressor As Long) As Long
    Private Declare PtrSafe Function CloseCompressor Lib "cabinet" (ByVal hCompressor As Long) As Long
    Private Declare PtrSafe Function CloseDecompressor Lib "cabinet" (ByVal hDecompressor As Long) As Long
    
    Function CompressString$(s$, Optional algorithm& = 5)
        Dim h As Long, max As Long, bytesOut As Long, b As String
        If Len(s) Then
            If CreateCompressor(algorithm, 0&, h) Then
                max = LenB(s): b = Space$(max)
                If compress(h, StrPtr(s), max, StrPtr(b), max, bytesOut) Then
                    If bytesOut Then CompressString = Left$(b, bytesOut \ 2)
                End If
                CloseCompressor h
            End If
        End If
    End Function
    
    Function DecompressString$(s$, Optional algorithm& = 5)
        Dim h As Long, bytesOut As Long, b As String
        If Len(s) Then
            If CreateDecompressor(algorithm, 0&, h) Then
                b = Space$(LenB(s) * 50)
                If Decompress(h, StrPtr(s), LenB(s), StrPtr(b), LenB(b), bytesOut) Then
                    If bytesOut Then DecompressString = Left$(b, bytesOut \ 2)
                End If
                CloseDecompressor h
            End If
        End If
    End Function
    
    Sub TestCompressString()
        Dim GreenEggs$, Tiny$, Roundtrip$
    
        GreenEggs = "I do not like them in a box. I do not like them with a fox. I will not eat them in a house. I do not like them with a mouse. I do not like them here or there. I do not like them ANYWHERE!"
        Tiny = CompressString(GreenEggs)
        Roundtrip = DecompressString(Tiny)
    
        Debug.Print Len(GreenEggs) & ": " & GreenEggs      '<--displays:  187
        Debug.Print Len(Tiny) & ": " & Tiny                '<--displays:   73
        Debug.Print Len(Roundtrip) & ": " & Roundtrip      '<--displays:  187       '<--displays Dr. Seus's breakfast problem
     End Sub
    When I try TestCompressString, Access crashes at the line If compress(h, StrPtr(s), max, StrPtr(b), max, bytesOut) Then. Maybe Access will also crash with If Decompress(h, StrPtr(s), LenB(s), StrPtr(b), LenB(b), bytesOut) Then. Can someone help me with that? Thanks!

    Jonathan

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,716
    This link from Phil Stiefel may help.

    Note: I used the code from
    ...Now, I'm trying to compress string. I found some "ready" code for VBA here.
    And it worked fine. I have win 10 64bit and Office 365 32 bit. I don't have Office 365 64 bit.

    I included all of the code including conditional compilation and this small sub.

    Code:
    Sub testit()
    Dim GreenEggs$, Tiny$, Roundtrip$
                                  
    GreenEggs = "I do not like them in a box. I do not like them with a fox. I will not eat them in a house. I do not like them with a mouse. I do not like them here or there. I do not like them ANYWHERE!"
    Tiny = CompressString(GreenEggs)
    Roundtrip = DecompressString(Tiny)
    
                                  
    MsgBox Len(GreenEggs)       '<--displays:  187
    MsgBox Len(Tiny)            '<--displays:   73
    MsgBox Len(Roundtrip)       '<--displays:  187
    
    'added debug prints
    Debug.Print "Len(GreenEggs) " & Len(GreenEggs) & vbCrLf _
                     & "Len(Tiny)  " & Len(Tiny) & vbCrLf _
                     & "Len(Roundtrip) " & Len(Roundtrip)
                     
    Debug.Print Roundtrip       '<--displays Dr. Seus's breakfast problem
                                  
    'Note: Tiny (the compressed string) can be written to disk and decompressed
    '      at a later date.
                                  
    'Note: These functions use the Win32 Compression API, which is bundled with
    'Windows since Windows 8. These function will not work on Windows 7 and earlier.
                                  
    'Note: These functons default to using Algorithm #5: LZMS. The functions can
    '      optionally be directed to use the other supported MS API
    '      compression algorithms:
    '                              MSZIP:       2
    '                              XPRESS:      3
    '                              XPRESS_HUFF: 4
    '                              LZMS:        5
    
    'Note: There is no Algorithm #1 included in the API.
                                  
    'Note: The default LZMS compression algorithm seems to compress the best.
                                  
                                  
    'Reference:
    '    https://docs.microsoft.com/en-us/windows/win32/api/_cmpapi/
                                  
    End Sub
    Result:

    Len(GreenEggs) 187
    Len(Tiny) 73
    Len(Roundtrip) 187
    I do not like them in a box. I do not like them with a fox. I will not eat them in a house. I do not like them with a mouse. I do not like them here or there. I do not like them ANYWHERE!



    Also, I used your sub
    Code:
    Sub TestCompressString()
        Dim GreenEggs$, Tiny$, Roundtrip$
    
        GreenEggs = "I do not like them in a box. I do not like them with a fox. I will not eat them in a house. I do not like them with a mouse. I do not like them here or there. I do not like them ANYWHERE!"
        Tiny = CompressString(GreenEggs)
        Roundtrip = DecompressString(Tiny)
    
        Debug.Print Len(GreenEggs) & ": " & GreenEggs      '<--displays:  187
        Debug.Print Len(Tiny) & ": " & Tiny                '<--displays:   73
        Debug.Print Len(Roundtrip) & ": " & Roundtrip      '<--displays:  187       '<--displays Dr. Seus's breakfast problem
     End Sub
    with the following result:

    187: I do not like them in a box. I do not like them with a fox. I will not eat them in a house. I do not like them with a mouse. I do not like them here or there. I do not like them ANYWHERE!
    73: ???Y Y v ???L?????? ????????????????????????????????????????????? ?
    187: I do not like them in a box. I do not like them with a fox. I will not eat them in a house. I do not like them with a mouse. I do not like them here or there. I do not like them ANYWHERE!

  3. #3
    PJo is offline Novice
    Windows 10 Office 365
    Join Date
    Apr 2021
    Location
    Province du Québec
    Posts
    9
    Hi Orange,

    Thanks for your answer and the link! In the last few months, I played a few with converting code from 32 bits to 64 bits. I'm currently running Access 2019 x64 French.

    Glad the code worked as intended with you. It's not working with me. I tried to simply copy/paste the original code with your sub and run it. And I get this error message:
    Click image for larger version. 

Name:	Error message.png 
Views:	46 
Size:	2.5 KB 
ID:	45098

    I already explained it's because of StrPtr() which returns 64 bits (LongLong) value with 64 bits version of Office. If I change pUncompressedData and pCompressedDataBuffer to be As LongPtr, Access craches. I think it's because the compress and decompress functions of the library cabinet don't accept 64 bits long data for pUncompressedData and pCompressedDataBuffer. If I'm right, nothing can be done to make the code works with Access 64 bits because of the behavior of StrPtr.

    Here's some info about StrPtr which explain how it works.

    So, can someone help to resolve my issue or make a workaround? Thanks!

    Jonathan

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    Post 3 was moderated, I'm posting to trigger email notifications.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    PJo is offline Novice
    Windows 10 Office 365
    Join Date
    Apr 2021
    Location
    Province du Québec
    Posts
    9

    Unworking code to compress string with Access 64 bits.

    Hi everyone,

    I tried to edit my post to add the missing image and ended up by causing an error and deleting it...

    So, Orange, thanks for your answer and the link. In the last months, I played a few with converting code from 32 bits to 64 bits. I'm currently running Access 2019 64 bits French.

    Glad that the code worked well with you. But for me, even with pasting your sub with the original code, it's not working. I get this error:
    Click image for larger version. 

Name:	Error message.png 
Views:	45 
Size:	2.5 KB 
ID:	45099

    I already explained it's because that StrPtr returns 64 bits Long (LongLong) with Access 64 bits. And if I try to change pUncompressedData and pCompressedDataBuffer As LongPtr in the declarations, Access craches. I think it's because the function compress and decompress of cabinet don't accept 64 bits long with pUncompressedData and pCompressedDataBuffer, even if Windows is 64 bits and have almost everything converted into 64 bits. So, if I'm right, nothing can be done to resolve this issue because of the behavior of StrPtr.

    Can someone help to resolve this issue or make a wordaround? Thanks!

    Here's some explanation of how StrPtr works.

    Jonathan

  6. #6
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,716
    Jonathan,
    You could try changing all of the occurrences of

    ByVal hCompressor As Long to
    ByVal hCompressor As LongPtr

    It's more a guess than a solution, but as I read different things
    Particularly for pointers, Microsoft introduced an all new and very clever data type. The LongPtr data type. The really clever thing about the LongPtr type is, it is a 32-bit Integer if the code runs in 32-bit VBA and it becomes a 64-bit Integer if the code runs in 64-bit VBA. LongPtr is the perfect type for any pointer or handle in your Declare Statement. You can use this data type in both environments, and it will always be appropriately sized to handle the pointer size of your environment.

    Also: see this info.

    As I said, it's more of trial and error - you can try, and remove/revert if it doesn't work.
    Unfortunately I don't have 64 bit Office to test.

    @anyone with 64 bit Office.
    Can you test the code that Jonathan posted in post #1?

  7. #7
    PJo is offline Novice
    Windows 10 Office 365
    Join Date
    Apr 2021
    Location
    Province du Québec
    Posts
    9
    Hi Orange,

    Thanks again for your reply. I tried and it changed nothing because hCompressor receives value from h which is a Long and not a LongPtr.

    So, someone with Access 64 bits!

    Jonathan

  8. #8
    Minty is online now VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,001
    I'll have a 64 bit bite

    It crashes Access without an generating error in it's original form.
    I'm going to have a mess about with it.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  9. #9
    PJo is offline Novice
    Windows 10 Office 365
    Join Date
    Apr 2021
    Location
    Province du Québec
    Posts
    9
    Hi everyone,

    So, the code is working well with Access 32 bits, but not with Access 64 bits. Do someone has an alternative solution? Maybe with Windows internal zip? There are plenty of exemples in the internet for files. But I found nothing to compress strings or don't know how to convert from zipping files to zip string. Thanks!

    Jonathan

  10. #10
    PJo is offline Novice
    Windows 10 Office 365
    Join Date
    Apr 2021
    Location
    Province du Québec
    Posts
    9
    Hi everyone,

    The best answer I have is to stick with Access 32 bits. Here you can see some good explanations why it is better to use 32-bit Access instead of 64-bit. Even Microsoft recommmand to use Office 32 bits (also applies to Office 2016, 2019 and O365):
    We recommend the 32-bit version of Office for most users, because it's more compatible with most other applications, especially third-party add-ins. This is why the 32-bit version of Office 2013 is installed by default, even on 64-bit Windows operating systems. On these systems, the 32-bit Office client is supported as a Windows-32-on-Windows-64 (WOW64) installation. WOW64 is the x86 emulator that enables 32-bit Windows-based applications to run seamlessly on 64-bit Windows systems. This lets users continue to use existing Microsoft ActiveX Controls and COM add-ins with 32-bit Office.
    Somehow my problem is solved...

    Thanks everyone!

    Jonathan

  11. #11
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,954
    Hi
    I haven't tested your code but wanted to point out that the first RC4 link in post #1 was an incorrect link to my site
    It should have been http://www.mendipdatasystems.co.uk/password-login/4594469149 and NOT https....
    Ironically, my site isn't currently a 'secure' site due to circumstances beyond my control

    There is one important difference in the RC4 function that I use compared to that in the second link of your post.
    The original can cause error 9 - subscript out of range
    I published my version here http://www.mendipdatasystems.co.uk/e...ngs/4595188968

    EDIT
    For info, the 7th link in post #1 is also incorrect. I believe it should be https://en.m.wikibooks.org/wiki/Visu...Hashing_in_VBA
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  12. #12
    PJo is offline Novice
    Windows 10 Office 365
    Join Date
    Apr 2021
    Location
    Province du Québec
    Posts
    9
    Hi Isladogs,

    Thanks for reporting errors in the links. My bad! I want to edit my first message, but I can't find the Edit button...

    Thanks also for the correction of RC4 code and the link provided.

    Jonathan

  13. #13
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,954
    Members are only able to edit links for a limited amount of time so it may be too late for you to go back and fix the links.
    I didn't read this thread when you first posted last month so hadn't noticed the errors earlier.
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  14. #14
    PJo is offline Novice
    Windows 10 Office 365
    Join Date
    Apr 2021
    Location
    Province du Québec
    Posts
    9
    Yeah. Before writing this post, I could edit my last post, but not yesterday's...

Please reply to this thread with any new information or opinions.

Similar Threads

  1. How to compress picture when exporting to Excel
    By Jo.. in forum Import/Export Data
    Replies: 1
    Last Post: 07-17-2015, 12:00 AM
  2. Replies: 2
    Last Post: 04-05-2015, 06:06 PM
  3. Compress On Exit
    By Derrick T. Davidson in forum Access
    Replies: 5
    Last Post: 07-31-2014, 09:40 PM
  4. Replies: 7
    Last Post: 05-28-2013, 09:11 AM
  5. any way to schedule a compress/repaor task?
    By Coolpapabell in forum Access
    Replies: 7
    Last Post: 10-07-2009, 02:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums