external/zlib-1.2.11/old/visual-basic.txt
author Sam Lantinga <slouken@libsdl.org>
Fri, 26 Oct 2018 14:58:01 -0700
changeset 617 d64228a395fc
parent 521 9ed2f7d4a251
permissions -rw-r--r--
Fixed webp library detection when cross-compiling for Windows
slouken@521
     1
See below some functions declarations for Visual Basic.
slouken@521
     2
slouken@521
     3
Frequently Asked Question:
slouken@521
     4
slouken@521
     5
Q: Each time I use the compress function I get the -5 error (not enough
slouken@521
     6
   room in the output buffer).
slouken@521
     7
slouken@521
     8
A: Make sure that the length of the compressed buffer is passed by
slouken@521
     9
   reference ("as any"), not by value ("as long"). Also check that
slouken@521
    10
   before the call of compress this length is equal to the total size of
slouken@521
    11
   the compressed buffer and not zero.
slouken@521
    12
slouken@521
    13
slouken@521
    14
From: "Jon Caruana" <jon-net@usa.net>
slouken@521
    15
Subject: Re: How to port zlib declares to vb?
slouken@521
    16
Date: Mon, 28 Oct 1996 18:33:03 -0600
slouken@521
    17
slouken@521
    18
Got the answer! (I haven't had time to check this but it's what I got, and
slouken@521
    19
looks correct):
slouken@521
    20
slouken@521
    21
He has the following routines working:
slouken@521
    22
        compress
slouken@521
    23
        uncompress
slouken@521
    24
        gzopen
slouken@521
    25
        gzwrite
slouken@521
    26
        gzread
slouken@521
    27
        gzclose
slouken@521
    28
slouken@521
    29
Declares follow: (Quoted from Carlos Rios <c_rios@sonda.cl>, in Vb4 form)
slouken@521
    30
slouken@521
    31
#If Win16 Then   'Use Win16 calls.
slouken@521
    32
Declare Function compress Lib "ZLIB.DLL" (ByVal compr As
slouken@521
    33
        String, comprLen As Any, ByVal buf As String, ByVal buflen
slouken@521
    34
        As Long) As Integer
slouken@521
    35
Declare Function uncompress Lib "ZLIB.DLL" (ByVal uncompr
slouken@521
    36
        As String, uncomprLen As Any, ByVal compr As String, ByVal
slouken@521
    37
        lcompr As Long) As Integer
slouken@521
    38
Declare Function gzopen Lib "ZLIB.DLL" (ByVal filePath As
slouken@521
    39
        String, ByVal mode As String) As Long
slouken@521
    40
Declare Function gzread Lib "ZLIB.DLL" (ByVal file As
slouken@521
    41
        Long, ByVal uncompr As String, ByVal uncomprLen As Integer)
slouken@521
    42
        As Integer
slouken@521
    43
Declare Function gzwrite Lib "ZLIB.DLL" (ByVal file As
slouken@521
    44
        Long, ByVal uncompr As String, ByVal uncomprLen As Integer)
slouken@521
    45
        As Integer
slouken@521
    46
Declare Function gzclose Lib "ZLIB.DLL" (ByVal file As
slouken@521
    47
        Long) As Integer
slouken@521
    48
#Else
slouken@521
    49
Declare Function compress Lib "ZLIB32.DLL"
slouken@521
    50
        (ByVal compr As String, comprLen As Any, ByVal buf As
slouken@521
    51
        String, ByVal buflen As Long) As Integer
slouken@521
    52
Declare Function uncompress Lib "ZLIB32.DLL"
slouken@521
    53
        (ByVal uncompr As String, uncomprLen As Any, ByVal compr As
slouken@521
    54
        String, ByVal lcompr As Long) As Long
slouken@521
    55
Declare Function gzopen Lib "ZLIB32.DLL"
slouken@521
    56
        (ByVal file As String, ByVal mode As String) As Long
slouken@521
    57
Declare Function gzread Lib "ZLIB32.DLL"
slouken@521
    58
        (ByVal file As Long, ByVal uncompr As String, ByVal
slouken@521
    59
        uncomprLen As Long) As Long
slouken@521
    60
Declare Function gzwrite Lib "ZLIB32.DLL"
slouken@521
    61
        (ByVal file As Long, ByVal uncompr As String, ByVal
slouken@521
    62
        uncomprLen As Long) As Long
slouken@521
    63
Declare Function gzclose Lib "ZLIB32.DLL"
slouken@521
    64
        (ByVal file As Long) As Long
slouken@521
    65
#End If
slouken@521
    66
slouken@521
    67
-Jon Caruana
slouken@521
    68
jon-net@usa.net
slouken@521
    69
Microsoft Sitebuilder Network Level 1 Member - HTML Writer's Guild Member
slouken@521
    70
slouken@521
    71
slouken@521
    72
Here is another example from Michael <michael_borgsys@hotmail.com> that he
slouken@521
    73
says conforms to the VB guidelines, and that solves the problem of not
slouken@521
    74
knowing the uncompressed size by storing it at the end of the file:
slouken@521
    75
slouken@521
    76
'Calling the functions:
slouken@521
    77
'bracket meaning: <parameter> [optional] {Range of possible values}
slouken@521
    78
'Call subCompressFile(<path with filename to compress> [, <path with
slouken@521
    79
filename to write to>, [level of compression {1..9}]])
slouken@521
    80
'Call subUncompressFile(<path with filename to compress>)
slouken@521
    81
slouken@521
    82
Option Explicit
slouken@521
    83
Private lngpvtPcnSml As Long 'Stores value for 'lngPercentSmaller'
slouken@521
    84
Private Const SUCCESS As Long = 0
slouken@521
    85
Private Const strFilExt As String = ".cpr"
slouken@521
    86
Private Declare Function lngfncCpr Lib "zlib.dll" Alias "compress2" (ByRef
slouken@521
    87
dest As Any, ByRef destLen As Any, ByRef src As Any, ByVal srcLen As Long,
slouken@521
    88
ByVal level As Integer) As Long
slouken@521
    89
Private Declare Function lngfncUcp Lib "zlib.dll" Alias "uncompress" (ByRef
slouken@521
    90
dest As Any, ByRef destLen As Any, ByRef src As Any, ByVal srcLen As Long)
slouken@521
    91
As Long
slouken@521
    92
slouken@521
    93
Public Sub subCompressFile(ByVal strargOriFilPth As String, Optional ByVal
slouken@521
    94
strargCprFilPth As String, Optional ByVal intLvl As Integer = 9)
slouken@521
    95
    Dim strCprPth As String
slouken@521
    96
    Dim lngOriSiz As Long
slouken@521
    97
    Dim lngCprSiz As Long
slouken@521
    98
    Dim bytaryOri() As Byte
slouken@521
    99
    Dim bytaryCpr() As Byte
slouken@521
   100
    lngOriSiz = FileLen(strargOriFilPth)
slouken@521
   101
    ReDim bytaryOri(lngOriSiz - 1)
slouken@521
   102
    Open strargOriFilPth For Binary Access Read As #1
slouken@521
   103
        Get #1, , bytaryOri()
slouken@521
   104
    Close #1
slouken@521
   105
    strCprPth = IIf(strargCprFilPth = "", strargOriFilPth, strargCprFilPth)
slouken@521
   106
'Select file path and name
slouken@521
   107
    strCprPth = strCprPth & IIf(Right(strCprPth, Len(strFilExt)) =
slouken@521
   108
strFilExt, "", strFilExt) 'Add file extension if not exists
slouken@521
   109
    lngCprSiz = (lngOriSiz * 1.01) + 12 'Compression needs temporary a bit
slouken@521
   110
more space then original file size
slouken@521
   111
    ReDim bytaryCpr(lngCprSiz - 1)
slouken@521
   112
    If lngfncCpr(bytaryCpr(0), lngCprSiz, bytaryOri(0), lngOriSiz, intLvl) =
slouken@521
   113
SUCCESS Then
slouken@521
   114
        lngpvtPcnSml = (1# - (lngCprSiz / lngOriSiz)) * 100
slouken@521
   115
        ReDim Preserve bytaryCpr(lngCprSiz - 1)
slouken@521
   116
        Open strCprPth For Binary Access Write As #1
slouken@521
   117
            Put #1, , bytaryCpr()
slouken@521
   118
            Put #1, , lngOriSiz 'Add the the original size value to the end
slouken@521
   119
(last 4 bytes)
slouken@521
   120
        Close #1
slouken@521
   121
    Else
slouken@521
   122
        MsgBox "Compression error"
slouken@521
   123
    End If
slouken@521
   124
    Erase bytaryCpr
slouken@521
   125
    Erase bytaryOri
slouken@521
   126
End Sub
slouken@521
   127
slouken@521
   128
Public Sub subUncompressFile(ByVal strargFilPth As String)
slouken@521
   129
    Dim bytaryCpr() As Byte
slouken@521
   130
    Dim bytaryOri() As Byte
slouken@521
   131
    Dim lngOriSiz As Long
slouken@521
   132
    Dim lngCprSiz As Long
slouken@521
   133
    Dim strOriPth As String
slouken@521
   134
    lngCprSiz = FileLen(strargFilPth)
slouken@521
   135
    ReDim bytaryCpr(lngCprSiz - 1)
slouken@521
   136
    Open strargFilPth For Binary Access Read As #1
slouken@521
   137
        Get #1, , bytaryCpr()
slouken@521
   138
    Close #1
slouken@521
   139
    'Read the original file size value:
slouken@521
   140
    lngOriSiz = bytaryCpr(lngCprSiz - 1) * (2 ^ 24) _
slouken@521
   141
              + bytaryCpr(lngCprSiz - 2) * (2 ^ 16) _
slouken@521
   142
              + bytaryCpr(lngCprSiz - 3) * (2 ^ 8) _
slouken@521
   143
              + bytaryCpr(lngCprSiz - 4)
slouken@521
   144
    ReDim Preserve bytaryCpr(lngCprSiz - 5) 'Cut of the original size value
slouken@521
   145
    ReDim bytaryOri(lngOriSiz - 1)
slouken@521
   146
    If lngfncUcp(bytaryOri(0), lngOriSiz, bytaryCpr(0), lngCprSiz) = SUCCESS
slouken@521
   147
Then
slouken@521
   148
        strOriPth = Left(strargFilPth, Len(strargFilPth) - Len(strFilExt))
slouken@521
   149
        Open strOriPth For Binary Access Write As #1
slouken@521
   150
            Put #1, , bytaryOri()
slouken@521
   151
        Close #1
slouken@521
   152
    Else
slouken@521
   153
        MsgBox "Uncompression error"
slouken@521
   154
    End If
slouken@521
   155
    Erase bytaryCpr
slouken@521
   156
    Erase bytaryOri
slouken@521
   157
End Sub
slouken@521
   158
Public Property Get lngPercentSmaller() As Long
slouken@521
   159
    lngPercentSmaller = lngpvtPcnSml
slouken@521
   160
End Property