Forum: Ruby Newbie: Better way to check a magic number on a file?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2006-03-22 00:46
All,

I am trying to determine if I am dealing with an Excel file that I
upload to a Web server.

Here is the first test - to verify the magic number of the file that I
have:

#Check the magic number first to see if it's even an Office file

    magic_number = nil
    File.open(path_to_file, "r") do |f|
      magic_number = f.read(6)
    end
    return false unless
magic_number.unpack('H*').to_s().eql?('d0cf11e0a1b1')

This seems a little bit less than optimal.  I have to unpack my string
into an array and then reconvert it back to a string to compare it?

Surely there is a simpler way?

Should magic_number.eql?('d0cf11e0a1b1') work?

Wes
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2006-03-22 00:48
magic_number.eql?('d0cf11e0a1b1') doesn't work which kind of makes
sense.

Anyhow, is this the best way to do it?  It feels very hokey to me :).

Thanks for the patience as well - I rarely inspect magic numbers...

WG

Wes Gamble wrote:
> All,
>
> I am trying to determine if I am dealing with an Excel file that I
> upload to a Web server.
>
> Here is the first test - to verify the magic number of the file that I
> have:
>
> #Check the magic number first to see if it's even an Office file
>
>     magic_number = nil
>     File.open(path_to_file, "r") do |f|
>       magic_number = f.read(6)
>     end
>     return false unless
> magic_number.unpack('H*').to_s().eql?('d0cf11e0a1b1')
>
> This seems a little bit less than optimal.  I have to unpack my string
> into an array and then reconvert it back to a string to compare it?
>
> Surely there is a simpler way?
>
> Should magic_number.eql?('d0cf11e0a1b1') work?
>
> Wes
37a3c73ffbf864e4b28f7f2384ee12ce?d=identicon&s=25 Timothy Hunter (tim-hunter)
on 2006-03-22 00:58
(Received via mailing list)
Wes Gamble wrote:
>     magic_number = nil
>
> Should magic_number.eql?('d0cf11e0a1b1') work?
>
> Wes
>

I'm guessing the magic number is constant? Why not construct a constant
string that is the magic number and then compare the bytes you get from
the file to that string? Simple, fast, and ez-to-read.

MAGIC_STRING = [0xd0,0xcf,0x11,0xe0,0xa1,0xb1].pack('c*').freeze

if magic_number == MAGIC_STRING ...
2ffac40f8a985a2b2749244b8a1c4161?d=identicon&s=25 Mike Stok (Guest)
on 2006-03-22 00:59
(Received via mailing list)
On 21-Mar-06, at 6:46 PM, Wes Gamble wrote:

>     magic_number = nil
>
> Should magic_number.eql?('d0cf11e0a1b1') work?

There are a number of simpler ways, one might be

   EXCEL_FILE_MAGIC_NUMBER = ['d0cf11e0a1b1'].pack('H*')

   # later

   magic_number == EXCEL_FILE_MAGIC_NUMBER # => true or false

or you can use octal or hex escapes to have a string literal to
compare against e.g.

irb(main):006:0> "\320\317\021\340\241\261" == "\xd0\xcf\x11\xe0\xa1
\xb1"
=> true

Maybe there are good bits of the Pickaxe (Programming Ruby) or some
on-line tutorials which can help you see some of the more ruby-ish
ways to do things.

Hope this helps,

Mike

--

Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2006-03-22 01:18
An even better way would be to take advantage of the fact that I'm
uploading through a browser and just look at the MIME type

:) :) :)

Man, I'm dumb :).

But on the other hand, now I now about the IO module, and modules in
general and all kinds of things.

Thanks, everybody.

Wes

Mike Stok wrote:
> On 21-Mar-06, at 6:46 PM, Wes Gamble wrote:
>
>>     magic_number = nil
>>
>> Should magic_number.eql?('d0cf11e0a1b1') work?
>
> There are a number of simpler ways, one might be
>
>    EXCEL_FILE_MAGIC_NUMBER = ['d0cf11e0a1b1'].pack('H*')
>
>    # later
>
>    magic_number == EXCEL_FILE_MAGIC_NUMBER # => true or false
>
> or you can use octal or hex escapes to have a string literal to
> compare against e.g.
>
> irb(main):006:0> "\320\317\021\340\241\261" == "\xd0\xcf\x11\xe0\xa1
> \xb1"
> => true
>
> Maybe there are good bits of the Pickaxe (Programming Ruby) or some
> on-line tutorials which can help you see some of the more ruby-ish
> ways to do things.
>
> Hope this helps,
>
> Mike
>
> --
>
> Mike Stok <mike@stok.ca>
> http://www.stok.ca/~mike/
>
> The "`Stok' disclaimers" apply.
This topic is locked and can not be replied to.