How to Convert Binary Files to String Securely?

Asked

Viewed 363 times

3

Operating System: Windows 10;

Version of Ruby: 2.2.1;

Server Application: Apache 2 (mod_cgi);

I must create a script that reads a file, convert it to text and after a possible processing, send the result to the user.

The current process works very well for text files (html, Plain, css etc...), but when the subject is binary (pdf, jpg, png etc...) the files always come out "unreadable" or corrupted (in the case of Jpegs)once the browser can mount the image but it is not the original on the server.

The code used so far is as follows::

mime_type = 'image/jpeg'
file = File.open(File.dirname(__FILE__) + 'imagem.jpg', "rb")
head = "Status: 200 OK\nContent-Type: #{mime_type}\nConnection: close\nContent-Length: #{file.size()}\n\n"
body = file.read()
print head + body

Does anyone know a safe way to read binary files on Windows to return to client?

1 answer

0

"puts" adds a CR/LF at the end of each line, as you were not specifying the binary size in the header the other side is accepting these two extra bytes as part of the content, which obviously corrupts it. Use "print" instead of puts.

  • I tested the above solutions. The image is not yet shown. However, now when downloading it, the resulting file has the exact size of the original file. (Code updated in question)

  • Does using "print head" and "STDOUT.write(body)" change anything? I’m guessing "print" and "puts" are translating some character like CRLF or Control-Z.

  • She’s still corrupt, Miss Scarlett. By my analysis the resulting file has the same size in bytes as the original, but has an extra line. (in the case of this specific image) In the case of PDF the file has the same size but thirty lines more.

  • I found out that the nonsense probably happens in file.read(). Because I tried to save what was read to a new file and this new file was corrupted and larger than the original.

  • It may be that File.read() is trying to apply an encoding. It has the IO.binread(name) method that might be worth trying

  • I have tried. The result is the same. I believe that the File.read() uses the IO.binread.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.