home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / pascal / cx201 / vbcxsub.bas < prev    next >
Encoding:
BASIC Source File  |  1994-03-01  |  8.6 KB  |  307 lines

  1. '  CXSUB functions.
  2. '  Copyright (c) 1990-1994 Eugene Nelson, Four Lakes Computing.
  3. '
  4. '  This file contains useful subroutines that may be used with Cx.
  5. '  See files CXSUB.DOC and BINIO.DOC for interface information.
  6.  
  7. Const CXSUB_ERR_OPENS = 1
  8. Const CXSUB_ERR_OPEND = 2
  9. Const CXSUB_ERR_NOMEM = 3
  10. Const CXSUB_ERR_READ = 4
  11. Const CXSUB_ERR_WRITE = 5
  12. Const CXSUB_ERR_CLOSE = 6
  13. Const CXSUB_ERR_INVALID = 7
  14.  
  15. Function cx_compress_file (dst$, src$, method As Integer, bsize As Long, tsize As Long, continue As Integer, bytes As Long)
  16.     Dim ifile As Integer
  17.     Dim ofile As Integer
  18.     Dim k As Integer
  19.     Dim j As Integer
  20.  
  21.     ifile = binio_open(src$, BINIO_OPEN_READ)
  22.     If ifile = -1 Then
  23.         cx_compress_file = CXSUB_ERR_OPENS
  24.         Exit Function
  25.     End If
  26.     
  27.     ofile = binio_open(dst$, BINIO_OPEN_CREATE)
  28.     If ofile = -1 Then
  29.         k = binio_close(ifile)
  30.         cx_compress_file = CXSUB_ERR_OPEND
  31.         Exit Function
  32.     End If
  33.  
  34.     k = cx_compress_ofile(ofile, ifile, method, bsize, tsize, continue, bytes)
  35.  
  36.     j = binio_close(ifile)
  37.     j = binio_close(ofile)
  38.     If j <> 0 Then j = CXSUB_ERR_CLOSE
  39.  
  40.     If k = 0 Then cx_compress_file = j Else cx_compress_file = k
  41. End Function
  42.  
  43. Function cx_compress_ofile (ofile As Integer, ifile As Integer, method As Integer, bsize As Long, tsize As Long, continue As Integer, bytes As Long)
  44.     ReDim ibuff((bsize + 1) / 2) As Integer
  45.     ReDim obuff((bsize + 1) / 2) As Integer
  46.     ReDim tbuff((tsize + 1) / 2) As Integer
  47.     Dim bsizei As Integer
  48.     Dim tsizei As Integer
  49.     Dim l As Long
  50.     Dim lj As Long
  51.     Dim lk As Long
  52.     Dim k As Integer
  53.     Dim j As Integer
  54.     Dim crc As Integer
  55.  
  56.     bsizei = ltoi(bsize)
  57.     tsizei = ltoi(tsize)
  58.  
  59.     While True
  60.         DoEvents
  61.         If Not continue Then
  62.             cx_compress_ofile = 0
  63.             Exit Function
  64.         End If
  65.  
  66.         lj = binio_read(ifile, ibuff(0), bsize)
  67.         j = ltoi(lj)
  68.         If lj = -1 Then
  69.             cx_compress_ofile = CXSUB_ERR_READ
  70.             Exit Function
  71.         End If
  72.  
  73.         bytes = bytes + lj
  74.     
  75.         l = binio_write(ofile, j, CXINTSIZE)
  76.         If l <> CXINTSIZE Then
  77.             cx_compress_ofile = CXSUB_ERR_WRITE
  78.             Exit Function
  79.         End If
  80.  
  81.         If j = 0 Then
  82.             cx_compress_ofile = 0
  83.             Exit Function
  84.         End If
  85.  
  86.         k = CX_COMPRESS(method, obuff(0), bsizei, ibuff(0), j, tbuff(0), tsizei)
  87.         lk = itol(k)
  88.  
  89.         If (lk > lj) Then
  90.             cx_compress_ofile = k
  91.             Exit Function
  92.         End If
  93.  
  94.         l = binio_write(ofile, k, CXINTSIZE)
  95.         If l <> CXINTSIZE Then
  96.             cx_compress_ofile = CXSUB_ERR_WRITE
  97.             Exit Function
  98.         End If
  99.  
  100.         If k = j Then
  101.             crc = CX_CRC(ibuff(0), k)
  102.             l = binio_write(ofile, crc, CXINTSIZE)
  103.             If l <> CXINTSIZE Then
  104.                 cx_compress_ofile = CXSUB_ERR_WRITE
  105.                 Exit Function
  106.             End If
  107.  
  108.             l = binio_write(ofile, ibuff(0), lk)
  109.             If l <> lk Then
  110.                 cx_compress_ofile = CXSUB_ERR_WRITE
  111.                 Exit Function
  112.             End If
  113.         Else
  114.             crc = CX_CRC(obuff(0), k)
  115.             l = binio_write(ofile, crc, CXINTSIZE)
  116.             If l <> CXINTSIZE Then
  117.                 cx_compress_ofile = CXSUB_ERR_WRITE
  118.                 Exit Function
  119.             End If
  120.  
  121.             l = binio_write(ofile, obuff(0), lk)
  122.             If l <> lk Then
  123.                 cx_compress_ofile = CXSUB_ERR_WRITE
  124.                 Exit Function
  125.             End If
  126.         End If
  127.     Wend
  128. End Function
  129.  
  130. Function cx_decompress_file (dst$, src$, continue As Integer, bytes As Long)
  131.     Dim ifile As Integer
  132.     Dim ofile As Integer
  133.     Dim k As Integer
  134.  
  135.     ifile = binio_open(src$, BINIO_OPEN_READ)
  136.     If ifile = -1 Then
  137.         cx_decompress_file = CXSUB_ERR_OPENS
  138.         Exit Function
  139.     End If
  140.     
  141.     If dst$ <> "" Then
  142.         ofile = binio_open(dst$, BINIO_OPEN_CREATE)
  143.         If ofile = -1 Then
  144.             k = binio_close(ifile)
  145.             cx_decompress_file = CXSUB_ERR_OPEND
  146.             Exit Function
  147.         End If
  148.     Else
  149.         ofile = -1
  150.     End If
  151.  
  152.     k = cx_decompress_ofile(ofile, ifile, continue, bytes)
  153.  
  154.     j = binio_close(ifile)
  155.     If ofile = -1 Then j = 0 Else j = binio_close(ofile)
  156.     If j <> 0 Then j = CXSUB_ERR_CLOSE
  157.  
  158.     If k = 0 Then cx_decompress_file = j Else cx_decompress_file = k
  159. End Function
  160.  
  161. Function cx_decompress_ofile (ofile As Integer, ifile As Integer, continue As Integer, bytes As Long)
  162.     Dim bsize As Long
  163.     Dim tsize As Long
  164.     Dim bsizei As Integer
  165.     Dim tsizei As Integer
  166.     Dim l As Long
  167.     Dim lj As Long
  168.     Dim lk As Long
  169.     Dim k As Integer
  170.     Dim j As Integer
  171.     Dim crc As Integer
  172.  
  173.     bsize = 0
  174.     tsize = CX_D_MINTEMP
  175.     tsizei = ltoi(tsize)
  176.     ReDim tbuff((tsize + 1) / 2) As Integer
  177.  
  178.     While True
  179.         DoEvents
  180.         If Not continue Then
  181.             cx_decompress_ofile = 0
  182.             Exit Function
  183.         End If
  184.  
  185.         l = binio_read(ifile, j, CXINTSIZE)
  186.         If l <> CXINTSIZE Then
  187.             cx_decompress_ofile = CXSUB_ERR_READ
  188.             Exit Function
  189.         End If
  190.  
  191.         If j = 0 Then
  192.             cx_decompress_ofile = 0
  193.             Exit Function
  194.         End If
  195.  
  196.         lj = itol(j)
  197.         If bsize < lj Then
  198.             bsize = lj
  199.             bsizei = ltoi(bsize)
  200.             ReDim ibuff((bsize + 1) / 2) As Integer
  201.             ReDim obuff((bsize + 1) / 2) As Integer
  202.         End If
  203.  
  204.         bytes = bytes + lj
  205.  
  206.         l = binio_read(ifile, k, CXINTSIZE)
  207.         If l <> CXINTSIZE Then
  208.             cx_decompress_ofile = CXSUB_ERR_READ
  209.             Exit Function
  210.         End If
  211.         lk = itol(k)
  212.  
  213.         l = binio_read(ifile, crc, CXINTSIZE)
  214.         If l <> CXINTSIZE Then
  215.             cx_decompress_ofile = CXSUB_ERR_READ
  216.             Exit Function
  217.         End If
  218.  
  219.         If (lk > lj) Or (lk > bsize) Or (lj > bsize) Then
  220.             cx_decompress_ofile = CXSUB_ERR_INVALID
  221.             Exit Function
  222.         End If
  223.  
  224.         l = binio_read(ifile, ibuff(0), lk)
  225.         If l <> lk Then
  226.             cx_decompress_ofile = CXSUB_ERR_READ
  227.             Exit Function
  228.         End If
  229.  
  230.         If CX_CRC(ibuff(0), k) <> crc Then
  231.             cx_decompress_ofile = CXSUB_ERR_INVALID
  232.             Exit Function
  233.         End If
  234.  
  235.         If j = k Then
  236.             If ofile <> -1 Then
  237.                 l = binio_write(ofile, ibuff(0), lk)
  238.                 If l <> lk Then
  239.                     cx_decompress_ofile = CXSUB_ERR_WRITE
  240.                     Exit Function
  241.                 End If
  242.             End If
  243.         Else
  244.             k = CX_DECOMPRESS(obuff(0), bsizei, ibuff(0), k, tbuff(0), tsizei)
  245.             lk = itol(k)
  246.  
  247.             If lk > CX_MAX_BUFFER Then
  248.                 cx_decompress_ofile = k
  249.                 Exit Function
  250.             End If
  251.                   
  252.             If j <> k Then
  253.                 cx_decompress_ofile = CXSUB_ERR_INVALID
  254.                 Exit Function
  255.             End If
  256.  
  257.             If ofile <> -1 Then
  258.                 l = binio_write(ofile, obuff(0), lk)
  259.                 If l <> lk Then
  260.                     cx_decompress_ofile = CXSUB_ERR_WRITE
  261.                     Exit Function
  262.                 End If
  263.             End If
  264.         End If
  265.     Wend
  266. End Function
  267.  
  268. Function cx_error_message (errnum As Long) As String
  269.     Select Case errnum
  270.         Case CX_ERR_INVALID
  271.             s$ = "data could not be decompressed"
  272.         Case CX_ERR_METHOD
  273.             s$ = "invalid compression method"
  274.         Case CX_ERR_BUFFSIZE
  275.             s$ = "invalid buffer size"
  276.         Case CX_ERR_TEMPSIZE
  277.             s$ = "invalid temp buffer size"
  278.         Case CXSUB_ERR_OPENS
  279.             s$ = "could not open source"
  280.         Case CXSUB_ERR_OPEND
  281.             s$ = "could not open destination"
  282.         Case CXSUB_ERR_NOMEM
  283.             s$ = "insufficient memory"
  284.         Case CXSUB_ERR_READ
  285.             s$ = "could not read from source"
  286.         Case CXSUB_ERR_WRITE
  287.             s$ = "could not write to destination"
  288.         Case CXSUB_ERR_CLOSE
  289.             s$ = "could not close destination"
  290.         Case CXSUB_ERR_INVALID
  291.             s$ = "source file is invalid or corrupt"
  292.         Case Else
  293.             s$ = "unknown"
  294.     End Select
  295.  
  296.     cx_error_message = s$
  297. End Function
  298.  
  299. Function itol (i As Integer) As Long
  300.     If i < 0 Then itol = 65536 + i Else itol = i
  301. End Function
  302.  
  303. Function ltoi (l As Long) As Integer
  304.     If l > 32767 Then ltoi = l - 65536 Else ltoi = l
  305. End Function
  306.  
  307.