home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 4_2005-2006.ISO / data / Zips / Crash_Encr196996252006.psc / CrashEncryption2 / FileHandle.bas < prev   
BASIC Source File  |  2006-01-31  |  7KB  |  216 lines

  1. Attribute VB_Name = "BitReadWrite"
  2. '**************************************
  3. ' Name: Bit IO
  4. ' Description:This module allows you to
  5. '     view a file as a collection of bits rath
  6. '     er than as a collection of bytes. It all
  7. '     ows you to read/write a single bit at a
  8. '     time or read/write up to 32 bits at once
  9. '     .
  10. ' By: Derek Haas
  11. '
  12. ' Inputs:It's all explained in the code
  13. '
  14. ' Returns:Same as above
  15. '
  16. ' Side Effects:Don't try writing to a fi
  17. '     le opened for reading, and don't try rea
  18. '     ding from a file opened for writing - th
  19. '     ere is no error checking for that and th
  20. '     e results are unpredictable.
  21.  
  22. 'Don 't try To read or write more than 32 bits at a time With the InputBits and OutputBits functions.
  23. '    If you try To write a value With less bits than that value requires, the correct value will Not be written. For example, don't try to write the value 32 into a file using only 4 bits.
  24. '    After every call To inputbits and inputbit, you should check For eof on the input file using this code:
  25.     'inputbit/inputbits call here
  26.  
  27.     'If EOF(BitFile.FileNum) = True Then 'replace bitfile With the name of the variable
  28.         'put code to exit loop or leave function
  29.         '     here
  30.     'End If
  31.  
  32. '
  33. 'This code is copyrighted and has' limited warranties.Please see http://w
  34. '     ww.Planet-Source-Code.com/vb/scripts/Sho
  35. '     wCode.asp?txtCodeId=2903&lngWId=1'for details.'**************************************
  36.  
  37.  
  38. Type BitFile
  39.     FileNum As Integer 'File handle
  40.     holder As Byte 'holds a byte from file
  41.     mask As Byte 'used To read bits
  42.     End Type
  43.  
  44. Public Function OpenOBitFile(FileName As String) As BitFile
  45.  
  46.     'Parameters - Filename
  47.     'Returns - Bitfile
  48.     'What it does - Opens a file for output
  49.     '     a single bit at a time
  50.     'Example -dim OutputFile as bitfile
  51.     'OutputFile = OpenOBitFile("C:\test.bit"
  52.     '     )
  53.     
  54.     Dim bitfilename As BitFile
  55.     FileNum = FreeFile 'get lowest available file handle
  56.     Open FileName For Binary As FileNum 'open it
  57.     bitfilename.FileNum = FileNum 'assign file number To structure
  58.     bitfilename.holder = 0 'bit holder = 0
  59.     bitfilename.mask = 128 'used To read individual bits
  60.     OpenOBitFile = bitfilename
  61. End Function
  62.  
  63.  
  64. Public Function OpenIBitFile(FileName As String) As BitFile
  65.  
  66.     'Parameters - Filename
  67.     'Returns - Bitfile
  68.     'What it does - Opens a file for input a
  69.     '     single bit at a time
  70.     'Example -dim InputFile as bitfile
  71.     'InputFile = OpenIBitFile("C:\command.co
  72.     '     m")
  73.     Dim bitfilename As BitFile
  74.     FileNum = FreeFile 'get lowest available file handle
  75.     Open FileName For Binary As FileNum 'open it
  76.     bitfilename.FileNum = FileNum 'assign file number To structure
  77.     bitfilename.holder = 0 'bit holder = 0
  78.     bitfilename.mask = 128 'used To read individual bits
  79.     OpenIBitFile = bitfilename
  80. End Function
  81.  
  82.  
  83. Public Sub CloseIBitFile(bitfilename As BitFile)
  84.  
  85.     'Parameters - bitfile
  86.     'Returns - Nothing
  87.     'What it does - Closes the file associat
  88.     '     ed with a bitfile
  89.     'Example - CloseIBitFile(InputFile)
  90.     Close bitfilename.FileNum 'Close the file associated With the bitfile
  91. End Sub
  92.  
  93.  
  94. Public Sub CloseOBitFile(bitfilename As BitFile)
  95.  
  96.     'Parameters - bitfile
  97.     'Returns - Nothing
  98.     'What it does - Closes the file associat
  99.     '     ed with a bitfile
  100.     'Example - CloseOBitFile(OutputFile)
  101.  
  102.     If bitfilename.mask <> 128 Then 'If there is unwritten data...
  103.         Put bitfilename.FileNum, , bitfilename.holder 'Write it now
  104.     End If
  105.  
  106.     Close bitfilename.FileNum 'Close the file
  107. End Sub
  108.  
  109.  
  110. Public Sub OutputBit(ByRef bitfilename As BitFile, bit As Byte)
  111.  
  112.     'Parameters - bitfile, bit to write
  113.     'Returns - nothing
  114.     'What it does - Writes the specified bit
  115.     '     to the file
  116.     'Example - OutputBit(OutputFile, 1)
  117.  
  118.     If bit <> 0 Then
  119.         bitfilename.holder = bitfilename.holder Or bitfilename.mask
  120.         'the holder stores up written bits until
  121.         '     there are 8
  122.         'At that point vb's normal file handling
  123.         '     facilities can write it
  124.     End If
  125.  
  126.     bitfilename.mask = bitfilename.mask \ 2 'decrease mask by power of 2
  127.  
  128.  
  129.     If bitfilename.mask = 0 Then 'if mask is empty
  130.         Put bitfilename.FileNum, , bitfilename.holder 'write the Byte
  131.         bitfilename.holder = 0 'reset holder and mask
  132.         bitfilename.mask = 128
  133.     End If
  134.  
  135.     
  136. End Sub
  137.  
  138.  
  139. Public Sub OutputBits(ByRef bitfilename As BitFile, ByVal code As Long, ByVal count As Integer)
  140.  
  141.     'Parameters - bitfile, data to write, nu
  142.     '     mber of bits to use
  143.     'Returns - nothing
  144.     'What it does - Writes the specified inf
  145.     '     o using the specified number of bits
  146.     'Example - OutputBits(OutputFile, 28, 7)
  147.     '
  148.     Dim mask As Long
  149.     mask = 2 ^ (count - 1)
  150.  
  151.  
  152.     Do While mask <> 0
  153.  
  154.  
  155.         If (mask And code) <> 0 Then 'if the bits match up...
  156.             bitfilename.holder = bitfilename.holder Or bitfilename.mask 'put the bit In the holder
  157.         End If
  158.  
  159.         bitfilename.mask = bitfilename.mask \ 2
  160.         mask = mask \ 2
  161.  
  162.  
  163.         If bitfilename.mask = 0 Then 'when there are 8 bits, write the holder To the file
  164.             Put bitfilename.FileNum, , bitfilename.holder
  165.             bitfilename.holder = 0 'and reset the holder and mask
  166.             bitfilename.mask = 128
  167.         End If
  168.  
  169.     Loop
  170.  
  171. End Sub
  172.  
  173.  
  174. Public Function InputBit(ByRef bitfilename As BitFile) As Byte
  175.  
  176.     'Parameters - bitfile
  177.     'returns - the next bit from the file
  178.     'Example: bit = InputBit(InputBitFile)
  179.     Dim value As Byte
  180.  
  181.     If bitfilename.mask = 128 Then 'if at End of previous Byte
  182.         
  183.         Get bitfilename.FileNum, , bitfilename.holder 'get a new Byte from file
  184.     End If
  185.  
  186.     value = bitfilename.holder And bitfilename.mask 'get the bit
  187.     bitfilename.mask = bitfilename.mask \ 2 'move the mask bit down one
  188.  
  189.  
  190.     If bitfilename.mask = 0 Then
  191.         bitfilename.mask = 128
  192.     End If
  193.  
  194.  
  195.     If value <> 0 Then 'return 0 or 1 desedThen slIIf valtreturn get the buWd Sub
  196.  
  197.  
  198. PubloWd Sub
  199.  
  200.  
  201. Pubsignt > 0 ThenubloWd Subl tIoWd Sub-b    '     mbeeitfilena  mbeitfilen  nt > 0
  202. Pubsignt > 0 ThenubloWd Subl tIoWd Sub-b   iB,oeorbloWd Su   iB,oeorboebsigntgc    
  203. Pubsignt > 0 Thenb
  204.  
  205.  
  206. Pubsigntrame.wc2igntrame.w'ub
  207. itFila as LonghiiPubsila a- Ou0 Th,im valve mask bl 128 Thiue.mask 0 That the buWd p bit lbl 'rame.wc2igntrame.w'gstIe'if at7s128 Thiu=f
  208.     '    oame.w'gstitf 4gl Subl trIf f la 
  209.    f 4gl tst 0 Tbname.mask tsl8mbeelPlsila a- Ou0 Th,ie.wbl 'ifimask bsign mask \ 2
  210. g'  sk bsign m ename.mask = bh)d If
  211. isnits match up...
  212.              match up...a'gstI 0 ThenubloWd Subl ss.
  213. 'me.ma'r of bit> 0 Thsk = bh)d If
  214. isnits match up...
  215.              mat tIoWd Sub "W eie.wbsch up...
  216.              mat tIoWd Sub "W eie.wbschIoWd Sub is - the nes 'i  'Pche holhat iF .wbsch up..fask bl 128ds 'i :n tR elP