Code Examples
  Home arrow Code Examples arrow Page 4 - Writing Binary Data in WSH
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
CODE EXAMPLES

Writing Binary Data in WSH
By: Nilpo
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2009-03-24

    Table of Contents:
  • Writing Binary Data in WSH
  • ADODB.Stream for writing binary files
  • Fixing the file contents
  • Writing binary files with FSO

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Writing Binary Data in WSH - Writing binary files with FSO


    (Page 4 of 4 )

    I used the ADODB.Stream method of creating binary files for quite some time before stumbling across this method.  It works effectively, but I wasn’t happy about writing every file twice to get the job done.  The ADODB Stream object isn’t particularly inefficient, it just seemed as though I was doing twice as much work as I really should have had to.  Enter the FileSystemObject.

    The FileSystemObject is not designed to work with binary files.  It’s doesn’t provide any binary support at all, as a matter of fact—and only one method of creating a file.  Let’s look at the solution.

    With CreateObject("Scripting.FileSystemObject").OpenTextFile(strPath, 2, True)

       For x = 1 To 44 Step 2

           .Write Chr(Clng("&h" & Mid("504B0506" & String(36, "0"),x,2)))

       Next

       .Close

    End With

    The code really isn’t all that difficult.  The secret is in the use of the OpenTextFile method.  The first and second parameters instruct the FileSystemObject to open a text file For Writing.  The optional third parameter is a Boolean value that tells the FileSystemObject if the file should be created if it doesn’t exist, and that works perfectly for us.

    When the FileSystemObject creates a file, it creates a completely empty file.  We can write our binary data directly to it without any problems.  We just need to determine how to do that.

    I’ve wrapped this code in a For Next loop to simplify it a bit.  The secret is in the functions I’m using inside of that loop.  I’m starting with my binary information in a string.  I move through the string, grabbing those characters two at a time. (Each byte has two hex characters).

    I then add a “&h” in front of those two characters.  That’s VBScript’s way of denoting a hexadecimal value.  Next, we exploit VBScript’s lack of strict data types.  Because this string looks like a hexadecimal number, VBS has no qualms when we instruct it to read the string value as a number with the Clng() function.  The Clng function converts a value into a numeric value of type Long.  This data type is wide enough to house our extremely large decimal values without losing any information.

    We have to convert these hex values into decimal values because the Chr function only accepts decimal numbers.  As you may recall, it takes a decimal number and returns the ASCII character represented by that decimal.  It doesn’t care if the character is actually a non-printable character.  That allows use of the  FileSystemObject object to write the characters directly to the file without any problems.  Closing the File object saves the newly-written binary data exactly the way we intended.

    There you have it.  Two exploits for writing binary data to files with a language that doesn’t support it, in an environment that won’t allow it.  Beware when using file streams, as is it very easy to run into memory issues if you attempt to read or write files that are too large.  It’s best to take them in chunks rather than attempting the file as a whole.  I hope you have fun with this one.  Until next time, keep coding!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    CODE EXAMPLES ARTICLES

    - Bipartite Graphs
    - Connectivity in Graphs
    - The Ford-Fulkerson Algorithm
    - Critical Paths
    - The Bellman-Ford and Roy-Floyd Algorithms
    - Shortest Path Algorithms in Graphs
    - Minimum Spanning Tree
    - Articulation Edges and Vertexes
    - Circles and Connectivity in Graphs
    - Depth-First Search in Graphs
    - Breadth-First Search in Graphs
    - The Prufer Code and the Floyd-Warshall Algor...
    - An Insight into Graphs
    - Coding a Custom Object with WSC
    - Creating a Custom Object with WSC





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek