Retrieved GIF image from SNMP Device and its not valid, why? (Fix my GIF please!)

I am retrieving the contents of the display panel of a printer in the form of a GIF image.
(More information here )

I am able to retrieve the OID and using GetNext, obtain the remaining chunks of the image in this tree.  I used StreamWriter to directly write the values to a file (each SNMP octet stream is directly written to the filestream)  The first 480 bytes came in the first GET and then an additional 5 bytes in the second GET

But for some reason the image isn't valid.  I looked at the output in a hex editor and there are 4 bytes _before_ the "GIF89a" header.  I tried removing the first 4 bytes of the file but no luck.
The invalid GIF can be found here: << Try saving it locally and looking at it in a hex editor.  

  Does anyone know what's wrong with this GIF?
If you figure out what's wrong with the file, I need to know what portion of the file was changed to make it valid.
Who is Participating?
Closed, 500 points refunded.
Friendly Neighbourhood Community Support Admin
Bob LearnedCommented:
How did you write the file?  Did you wait for both parts before you wrote the file to disk?

masoncooperAuthor Commented:
Here is the code I used to retrieve the values:

Dim selector As String = ""

            Dim response As SnmpResponse = snmp.GetNext(selector, 3)

            Dim sw As StreamWriter

            sw = New StreamWriter("C:\test.gif")

            While response.ErrorStatus = 0
                Dim oid As String = response.VariableBindings(0).OID.ToString

                If selector.Length > oid.Length Or selector <> oid.Substring(0, selector.Length) Then
                    Exit While
                End If


                Console.WriteLine("Bytes Written: " & response.VariableBindings(0).Value.Size)
                response = snmp.GetNext(response.VariableBindings(0).OID.ToString(), 3)

            End While
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

masoncooperAuthor Commented:
Ok, I've made some progress here, contrary to what I said earlier, If I delete the leading 4 bytes from the file, it displays in Internet Explorer, minus a tiny sliver (see )

But the file still isn't valid.  If I try and do an Image.FromFile in VB.Net, it throws an exception "Out of Memory", my guess is that it still isn't a properly formed GIF
Bob LearnedCommented:
Is it my imagination, or does that function return an octet string?  I would try using a BinaryWriter, instead of StreamWriter, and write the bytes to the file.

masoncooperAuthor Commented:
TheLearnedOne, Thanks for the tip, I forgot about that, but yes, the file is still writing the same even after using the binarywriter (my guess is that its because the SNMP object is using the .baseStream object to write the data)

In the meantime, I _did_ come up with a perfectly valid GIF.
If you look at the image test2.gif (mentioned in the comments), and remove the leading 4 bytes, then remove 2 bytes that are 5 bytes from the end, they should be 0x04 0x05 and then the file is valid!  What strikes my attention is that the chunks were 480 bytes and 5 bytes, leading me to believe that the first two bytes of the second chunk were garbage.  As far as I can tell, its not so, because applying that theory to other images obtained from this process do not yield the same result.

Here is another image, this one also came in two chunks but I had it write the streams out as two separate chunks IN ADDITION to the original portion that writes it all as one:
First Chunk:
Second Chunk:
Product of both chunks:
I wanted to be certian that the streams were being written properly and that no additional data was being appended.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.