sixarm_ruby_magic_number_type 1.0.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (31) hide show
  1. data.tar.gz.sig +1 -2
  2. data/.gemtest +0 -0
  3. data/README.md +104 -0
  4. data/Rakefile +8 -0
  5. data/VERSION +1 -0
  6. data/lib/sixarm_ruby_magic_number_type.rb +4 -140
  7. data/lib/sixarm_ruby_magic_number_type/file.rb +24 -0
  8. data/lib/sixarm_ruby_magic_number_type/io.rb +33 -0
  9. data/lib/sixarm_ruby_magic_number_type/string.rb +136 -0
  10. data/test/sixarm_ruby_magic_number_type_test.rb +9 -0
  11. data/test/sixarm_ruby_magic_number_type_test/file_test.rb +35 -0
  12. data/test/sixarm_ruby_magic_number_type_test/io_test.rb +13 -0
  13. data/test/sixarm_ruby_magic_number_type_test/samples/sample.bz2 +0 -0
  14. data/test/{sixarm_ruby_magic_number_type → sixarm_ruby_magic_number_type_test/samples}/sample.fit +0 -0
  15. data/test/{sixarm_ruby_magic_number_type → sixarm_ruby_magic_number_type_test/samples}/sample.gif +0 -0
  16. data/test/sixarm_ruby_magic_number_type_test/samples/sample.gz +0 -0
  17. data/test/sixarm_ruby_magic_number_type_test/samples/sample.html +1 -0
  18. data/test/{sixarm_ruby_magic_number_type → sixarm_ruby_magic_number_type_test/samples}/sample.jpg +0 -0
  19. data/test/{sixarm_ruby_magic_number_type → sixarm_ruby_magic_number_type_test/samples}/sample.png +0 -0
  20. data/test/{sixarm_ruby_magic_number_type → sixarm_ruby_magic_number_type_test/samples}/sample.ps +0 -0
  21. data/test/{sixarm_ruby_magic_number_type → sixarm_ruby_magic_number_type_test/samples}/sample.ras +0 -0
  22. data/test/{sixarm_ruby_magic_number_type → sixarm_ruby_magic_number_type_test/samples}/sample.sgi +0 -0
  23. data/test/{sixarm_ruby_magic_number_type → sixarm_ruby_magic_number_type_test/samples}/sample.tiff +0 -0
  24. data/test/sixarm_ruby_magic_number_type_test/samples/sample.xml +1 -0
  25. data/test/sixarm_ruby_magic_number_type_test/string_test.rb +94 -0
  26. metadata +88 -64
  27. metadata.gz.sig +0 -0
  28. data/LICENSE.txt +0 -12
  29. data/README.rdoc +0 -30
  30. data/test/sixarm_ruby_magic_number_type/sample.xcf.bz2 +0 -0
  31. data/test/sixarm_ruby_magic_number_type/sample.xcf.gz +0 -0
data.tar.gz.sig CHANGED
@@ -1,2 +1 @@
1
- A��|\[�$���9e
2
- µ�4L-.�>϶�A��ҡ��~.�
1
+ 7vŏ���D��&�Fҧ���H:�ZD!��v\!����>!v;�$1����nT���D��d9O���V�PYS}�M�F�U'�< s�r�N��u��,�O��t�sH��t=�M�E(%� z�o�
data/.gemtest ADDED
File without changes
data/README.md ADDED
@@ -0,0 +1,104 @@
1
+ # SixArm.com » Ruby » <br> MagicNumberType infers mime types from leading bits
2
+
3
+ * Doc: <http://sixarm.com/sixarm_ruby_magic_number_type/doc>
4
+ * Gem: <http://rubygems.org/gems/sixarm_ruby_magic_number_type>
5
+ * Repo: <http://github.com/sixarm/sixarm_ruby_magic_number_type>
6
+ * Email: Joel Parker Henderson, <joel@sixarm.com>
7
+
8
+
9
+ ## Introduction
10
+
11
+
12
+ Magic numbers are the first bits of a file or data stream
13
+ which uniquely identify the type of file or data stream.
14
+
15
+ For example when the first bits are "BM",
16
+ this identifies the file as a bitmap image file.
17
+
18
+ For docs go to <http://sixarm.com/sixarm_ruby_magic_number_type/doc>
19
+
20
+ Want to help? We're happy to get pull requests.
21
+
22
+
23
+ ## Install quickstart
24
+
25
+ Install:
26
+
27
+ gem install sixarm_ruby_magic_number_type
28
+
29
+ Gemfile:
30
+
31
+ gem "sixarm_ruby_magic_number_type", "~>2.0.0"
32
+
33
+ Require:
34
+
35
+ require "sixarm_ruby_magic_number_type"
36
+
37
+
38
+ ## Install with security (optional)
39
+
40
+ To enable high security for all our gems:
41
+
42
+ wget http://sixarm.com/sixarm.pem
43
+ gem cert --add sixarm.pem
44
+ gem sources --add http://sixarm.com
45
+
46
+ To install with high security:
47
+
48
+ gem install sixarm_ruby_magic_number_type --test --trust-policy HighSecurity
49
+
50
+
51
+ ## Details
52
+
53
+ This gem infers based on widespread programming conventions for data file formats.
54
+
55
+ These magic numbers are by convention and we are using this guide:
56
+ http://www.astro.keele.ac.uk/oldusers/rno/Computing/File_magic.html
57
+
58
+ Typical uses of magic numbers:
59
+
60
+ * to quickly identify a file's data type
61
+ * to check if data matches the file's MIME type or extension
62
+ * to check if a web form file upload matches its HTTP content type
63
+
64
+ Compare:
65
+
66
+ * MIME::Types ruby library
67
+ * Unix magic() command for testing files on disk
68
+ * http://shared-mime.rubyforge.org/
69
+
70
+
71
+ ## Changes
72
+
73
+ * 2012-09-08 2.0.0 Upgrade with more formats, samples, classes, and hex packing
74
+ * 2012-04-20 1.2.0 Upgrade to much faster algorithm
75
+ * 2012-03-14 1.1.0 Update docs, tests
76
+
77
+
78
+ ## License
79
+
80
+ You may choose any of these open source licenses:
81
+
82
+ * Apache License
83
+ * BSD License
84
+ * CreativeCommons License, Non-commercial Share Alike
85
+ * GNU General Public License Version 2 (GPL 2)
86
+ * GNU Lesser General Public License (LGPL)
87
+ * MIT License
88
+ * Perl Artistic License
89
+ * Ruby License
90
+
91
+ The software is provided "as is", without warranty of any kind,
92
+ express or implied, including but not limited to the warranties of
93
+ merchantability, fitness for a particular purpose and noninfringement.
94
+
95
+ In no event shall the authors or copyright holders be liable for any
96
+ claim, damages or other liability, whether in an action of contract,
97
+ tort or otherwise, arising from, out of or in connection with the
98
+ software or the use or other dealings in the software.
99
+
100
+ This license is for the included software that is created by SixArm;
101
+ some of the included software may have its own licenses, copyrights,
102
+ authors, etc. and these do take precedence over the SixArm license.
103
+
104
+ Copyright (c) 2005-2013 Joel Parker Henderson
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new(:test) do |t|
6
+ t.libs << 'lib' << 'test'
7
+ t.pattern = 'test/*.rb'
8
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 2.0.0
@@ -1,144 +1,8 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  =begin rdoc
3
-
4
- = SixArm.com » Ruby » MagicNumberType
5
-
6
- Author:: Joel Parker Henderson, joelparkerhenderson@gmail.com
7
- Copyright:: Copyright (c) 2010 Joel Parker Henderson
8
- License:: See LICENSE.txt file
9
-
10
- MagicNumberType infers a data type from the data's leading bytes.
11
-
12
- It infers based on widespread programming conventions for data file formats.
13
-
14
- Magic numbers are the first bits of a file or data stream
15
- which uniquely identify the type of file or data stream.
16
-
17
- For example when the first bits are "BM",
18
- this identifies the file as a bitmap image file.
19
-
20
- These magic numbers are by convention and we are using this guide:
21
- http://www.astro.keele.ac.uk/oldusers/rno/Computing/File_magic.html
22
-
23
- Typical uses of magic numbers:
24
- - to quickly identify a file's data type
25
- - to check if data matches the file's MIME type or extension
26
- - to check if a web form file upload matches its HTTP content type
27
-
28
- Compare:
29
- - MIME::Types ruby library
30
- - Unix magic() command for testing files on disk
31
- - http://shared-mime.rubyforge.org/
32
-
3
+ Please see README
33
4
  =end
34
5
 
35
- require 'pp'
36
-
37
- class IO
38
-
39
- # We implement magic by using a lookup hash.
40
- # The key is a string that encodes the first bits.
41
- # The value is a symbol that indicates the magic type.
42
- #
43
- # Examples:
44
- # IO::MagicNumberType("BM") => :bitmap
45
- # IO::MagicNumberType("GIF8") => :gif
46
- # IO::MagicNumberType("\xa6\x00") => :pgp_encrypted_data
47
- #
48
- # Quirks:
49
- # - JPEG adjustment:
50
- # - Some cameras put JPEG Exif data in bytes 3 & 4,
51
- # so we only check the first two bytes of a JPEG.
52
- # - TIFF has two possible matches:
53
- # - MM** is Motorola big endian
54
- # - II** is Intel little ending
55
- #
56
- # See:
57
- # - IO#magic_number_type
58
- # - File.magic_number_type
59
-
60
- MagicNumberTypeHash = {
61
- "BC" => :bitcode,
62
- "BM" => :bitmap,
63
- "BZ" => :bzip,
64
- "MZ" => :exe,
65
- "SIMPLE"=> :fits,
66
- "GIF8" => :gif,
67
- "GKSM" => :gks,
68
- [0x01,0xDA].pack('c*') => :iris_rgb,
69
- [0xF1,0x00,0x40,0xBB].pack('c*') => :itc,
70
- [0xFF,0xD8].pack('c*') => :jpeg,
71
- "IIN1" => :niff,
72
- "MThd" => :midi,
73
- "%PDF" => :pdf,
74
- "VIEW" => :pm,
75
- [0x89].pack('c*') + "PNG" => :png,
76
- "%!" => :postscript,
77
- "Y" + [0xA6].pack('c*') + "j" + [0x95].pack('c*') => :sun_rasterfile,
78
- "MM*" + [0x00].pack('c*') => :tiff,
79
- "II*" + [0x00].pack('c*') => :tiff,
80
- "gimp xcf" => :gimp_xcf,
81
- "#FIG" => :xfig,
82
- "/* XPM */" => :xpm,
83
- [0x23,0x21].pack('c*') => :shebang,
84
- [0x1F,0x9D].pack('c*') => :compress,
85
- [0x1F,0x8B].pack('c*') => :gzip,
86
- "PK" + [0x03,0x04].pack('c*') => :pkzip,
87
- "MZ" => :dos_os2_windows_executable,
88
- ".ELF" => :unix_elf,
89
- [0x99,0x00].pack('c*') => :pgp_public_ring,
90
- [0x95,0x01].pack('c*') => :pgp_security_ring,
91
- [0x95,0x00].pack('c*') => :pgp_security_ring,
92
- [0xA6,0x00].pack('c*') => :pgp_encrypted_data,
93
- [0xD0,0xCF,0x11,0xE0].pack('c*') => :docfile
94
- }
95
-
96
- # Detect the data type by checking various "magic number" conventions
97
- # for the introductory bytes of a data stream
98
- #
99
- # Return the "magic number" as a symbol:
100
- # - :bitmap = Bitmap image file, typical extension ".bmp"
101
- # - :gzip = Unix GZIP compressed data, typical extension ".gz"
102
- # - :postscript = Postscript pages, typical extension ".ps"
103
- #
104
- # Return nil if there's no match for any known magic number.
105
- #
106
- # Example:
107
- # f = File.open("test.ps","rb")
108
- # put f.magic_number(s)
109
- # => :postscript
110
- #
111
- # See:
112
- # - IO::MagicNumberTypeHash
113
- # - File.magic_number_type
114
-
115
- def magic_number_type(bytes=self.read(9))
116
- MagicNumberTypeHash.each_pair do |byte_string,type_symbol|
117
- return type_symbol if byte_string==bytes[0,byte_string.length]
118
- end
119
- return nil
120
- end
121
-
122
- end
123
-
124
-
125
- class File
126
-
127
- # Detect the file's data type by opening the file then
128
- # using IO#magic_number_type to read the first bits.
129
- #
130
- # Return a magic number type symbol, e.g. :bitmap, :jpg, etc.
131
- #
132
- # Example:
133
- # puts File.magic_number_type("test.ps") => :postscript
134
- #
135
- # See
136
- # - IO#MagicNumberTypeHash
137
- # - IO#magic_number_type
138
-
139
- def self.magic_number_type(file_name)
140
- File.open(file_name,"rb"){|f| f.magic_number_type }
141
- end
142
-
143
- end
144
-
6
+ ['file','io','string'].map{|x|
7
+ require File.dirname(__FILE__) + "/sixarm_ruby_magic_number_type/#{x}.rb"
8
+ }
@@ -0,0 +1,24 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ class File
4
+
5
+ # Detect the file's data type by opening the file then
6
+ # using IO#magic_number_type to read the first bits.
7
+ #
8
+ # Return a magic number type symbol, e.g. :bitmap, :jpg, etc.
9
+ #
10
+ # Example:
11
+ # puts File.magic_number_type("test.ps") => :postscript
12
+ #
13
+ # See
14
+ # - IO#MagicNumberTypeHash
15
+ # - IO#magic_number_type
16
+ #
17
+ def self.magic_number_type(file_name)
18
+ f = File.open(file_name,"rb")
19
+ type = f.magic_number_type
20
+ f.close
21
+ return type
22
+ end
23
+
24
+ end
@@ -0,0 +1,33 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ class IO
4
+
5
+ # Detect the data type by checking various "magic number" conventions
6
+ # for the introductory bytes of a data stream
7
+ #
8
+ # Return the "magic number" as a symbol:
9
+ # - :bitmap = Bitmap image file, typical extension ".bmp"
10
+ # - :gzip = Unix GZIP compressed data, typical extension ".gz"
11
+ # - :postscript = Postscript pages, typical extension ".ps"
12
+ #
13
+ # Return nil if there's no match for any known magic number.
14
+ #
15
+ # Example:
16
+ # IO.f = File.open("test.ps","rb")
17
+ # put f.magic_number(s)
18
+ # => :postscript
19
+ #
20
+ # See:
21
+ # - IO::MagicNumberTypeHash
22
+ # - File.magic_number_type
23
+ #
24
+ # TODO change from hash implementation to binary tree
25
+
26
+ def magic_number_type(bytes=self.read(String::MagicNumberTypeMaxLength))
27
+ String::MagicNumberTypeHash.each_pair do |byte_string,type_symbol|
28
+ return type_symbol if byte_string==bytes[0,byte_string.length]
29
+ end
30
+ return nil
31
+ end
32
+
33
+ end
@@ -0,0 +1,136 @@
1
+ # -*- coding: utf-8 -*-
2
+ =begin rdoc
3
+ Please see README
4
+ =end
5
+
6
+ class String
7
+
8
+ # We implement magic number type by using a lookup hash.
9
+ # This is fast enough for our needs; it could be optimized.
10
+ #
11
+ # The key is a string that encodes the first bits.
12
+ # The value is a symbol that indicates the magic type.
13
+ #
14
+ # See:
15
+ # - IO#magic_number_type
16
+ # - File.magic_number_type
17
+ #
18
+ # Quirks:
19
+ # - JPEG adjustment:
20
+ # - Some cameras put JPEG Exif data in bytes 3 & 4,
21
+ # so we only check the first two bytes of a JPEG.
22
+ # - TIFF has two possible matches:
23
+ # - II.. Intel little ending ("II2A00")
24
+ # - MM.. Motorola big endian ("MM002A")
25
+ #
26
+ # TODO change from hash implementation to binary tree
27
+ #
28
+ MagicNumberTypeHash = {
29
+ "<!doctyp" => :html,
30
+ "<html" => :html,
31
+ "<?xml" => :xml,
32
+ "<MakerFile" => :adobe_framemaker,
33
+ "BC" => :bitcode,
34
+ "BM" => :bitmap,
35
+ "BZ" => :bzip,
36
+ "MZ" => :exe,
37
+ "SIMPLE"=> :fits,
38
+ "GIF8" => :gif,
39
+ "GKSM" => :gks,
40
+ ["01DA"].pack('H*') => :iris_rgb,
41
+ ["F10040BB"].pack('H*') => :itc,
42
+ ["FFD8"].pack('H*') => :jpeg,
43
+ "IIN1" => :niff,
44
+ "MThd" => :midi,
45
+ "%PDF" => :pdf,
46
+ "VIEW" => :pm,
47
+ ["89504E470D0A1A0A"].pack('H*') => :png,
48
+ "Y" + ["A6"].pack('H*') + "j" + ["95"].pack('H*') => :sun_rasterfile,
49
+ "II" + ["2A00"].pack('H*') => :tiff, # II means Intel format, then 42 little-endian
50
+ "MM" + ["002A"].pack('H*') => :tiff, # MM means Motorola format, then 42 big-endian
51
+ "gimp xcf" => :gimp_xcf,
52
+ "#FIG" => :xfig,
53
+ "/* XPM */" => :xpm,
54
+ ["1F9D"].pack('H*') => :compress,
55
+ ["1F8B"].pack('H*') => :gzip,
56
+ "\x1F\xA0" => :tar_file_using_lzh_compression,
57
+ "PK\x03\x04" => :pkzip,
58
+ "7Z\xBC\xAF\x27\x1C" => :seven_zip,
59
+ "MZ" => :dos_os2_windows_executable,
60
+ ".ELF" => :unix_elf,
61
+ "\x99\x00" => :pgp_public_ring,
62
+ "\x95\x01" => :pgp_security_ring,
63
+ "\x95\x00" => :pgp_security_ring,
64
+ "\xA6\x00" => :pgp_encrypted_data,
65
+ "\xD0\xCF\x11\xE0" => :docfile,
66
+ #TODO matroska seems to have multiple magic file types-- diagnose and fix
67
+ #"\x1a\x45\xdf\xa3\xa3\x42\x86\x81\x01\x42\xf7\x81\x01\x42\xf2\x81\x04\x42\xf3\x81\x08\x42\x82\x88matroska" => :matroska_stream,
68
+ ".RTS COMPRESSED IMAGE" => :runtime_software_disk_image,
69
+ "WS" => :wordstar_document,
70
+ "!BDN" => :microsoft_outlook_personal_folder_file,
71
+ "# Disk Descripto" => :vmware_disk_description,
72
+ "# Microsoft Developer Studio" => :microsoft_developer_studio,
73
+ "#!AMR" => :adaptive_multi_rate,
74
+ "#?RADIANCE." => :radiance_high_dynamic_range_image_file,
75
+ "%!PS-Adobe-3.0 EPSF-3.0" => :encapsulated_postscript,
76
+ "8BPS" => :adobe_photoshop,
77
+ "\x00\xBF" => :adobe_flash_shared_object, # Typically a flash cookie
78
+ "\x00\x00\x00\x14ftypisom" => :iso_base_media, # ISO Base Media file (MPEG-4) v1
79
+ "\x00\x00\x00\x14ftypqt" => :quicktime_movie,
80
+ "\x00\x00\x00\x14fty3gp5" => :mpeg4_video,
81
+ "\x00\x00\x00\x14ftymp42" => :mpeg4_video_quicktime,
82
+ "\x00\x00\x00\x14ftyM4A" => :app_lossless_audio_codec,
83
+ "\x00\x01\x00\x00MYISAM DATABASE" => :msisam_database, # e.g. Microsoft Money
84
+ "\x00\x01\x00\x00Standard ACE DB" => :microsoft_access_ace_db,
85
+ "\x00\x01\x00\x00Standard Jet DB" => :microsoft_access_jet_db,
86
+ "\x00\x01BA" => :palm_address_book,
87
+ "\x00\x01BD" => :palm_date_book,
88
+ "\x01\x0F\x00\x00" => :microsoft_sql_server_2000,
89
+ "(This file must be converted with BinHex" => :binhex,
90
+ "*** Installation Started" => :symantec_wise_installer_log_file,
91
+ ".REC" => :realplayer_video,
92
+ ".RMF" => :realplayer_media,
93
+ "01ORDNANCE SURVEY" => :national_transfer_format_map,
94
+ "\x31\xBE" => :microsoft_write,
95
+ "\x32\xBE" => :microsoft_write,
96
+ "4" + ["CDB2A1"].pack('H*') => :tcpdump,
97
+ ":VERSION" => :surfplan_kite_project,
98
+ "AC10" => :autocad,
99
+ ".snd" => :sun_microsystems_law_audio_file,
100
+ # Short ones below that would mask longer ones above
101
+ "\x23\x21" => :shebang,
102
+ "%!" => :postscript
103
+ }
104
+
105
+ MagicNumberTypeMaxLength = 64 # Longest key
106
+
107
+
108
+ # Detect the data type by checking various "magic number" conventions
109
+ # for the introductory bytes of a data stream
110
+ #
111
+ # Return the "magic number" as a symbol:
112
+ # - :bitmap = Bitmap image file, typical extension ".bmp"
113
+ # - :gzip = Unix GZIP compressed data, typical extension ".gz"
114
+ # - :postscript = Postscript pages, typical extension ".ps"
115
+ #
116
+ # Return nil if there's no match for any known magic number.
117
+ #
118
+ # Examples:
119
+ # "BM".magic_number_type => :bitmap
120
+ # "GIF8".magic_numer_type => :gif
121
+ # "\xa6\x00".magic_number_type => :pgp_encrypted_data
122
+ #
123
+ # TODO change from hash implementation to binary tree
124
+ #
125
+ def magic_number_type
126
+ String::MagicNumberTypeHash.each_pair do |byte_string,type_symbol|
127
+ return type_symbol if byte_string==self.byteslice(0,byte_string.length)
128
+ end
129
+ return nil
130
+ end
131
+
132
+ protected
133
+
134
+ end
135
+
136
+
@@ -0,0 +1,9 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'minitest/autorun'
3
+ require 'simplecov'
4
+ SimpleCov.start
5
+
6
+ ['file','io','string'].map{|x|
7
+ require "sixarm_ruby_magic_number_type_test/#{x}_test.rb"
8
+ }
9
+
@@ -0,0 +1,35 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'minitest/autorun'
3
+ require 'simplecov'
4
+ SimpleCov.start
5
+ require 'sixarm_ruby_magic_number_type'
6
+
7
+ describe File do
8
+
9
+ before do
10
+ SAMPLE_FILE_NAME_TO_MAGIC_NUMBER_TYPE ||= {
11
+ 'sample.fit' => :fits,
12
+ 'sample.gif' => :gif,
13
+ 'sample.html' => :html,
14
+ 'sample.xml' => :xml,
15
+ 'sample.jpg' => :jpeg,
16
+ 'sample.png' => :png,
17
+ 'sample.ps' => :postscript,
18
+ 'sample.ras' => :sun_rasterfile,
19
+ 'sample.sgi' => :iris_rgb,
20
+ 'sample.tiff' => :tiff,
21
+ 'sample.bz2' => :bzip,
22
+ 'sample.gz' => :gzip,
23
+ }
24
+ end
25
+
26
+ it "solves the magic number type" do
27
+ SAMPLE_FILE_NAME_TO_MAGIC_NUMBER_TYPE.each_pair{|file_name, type|
28
+ path = File.join("test","sixarm_ruby_magic_number_type_test","samples",file_name)
29
+ File.exist?(path).must_equal true
30
+ File.magic_number_type(path).must_equal type, "Trying #{file_name}"
31
+ }
32
+ end
33
+
34
+ end
35
+
@@ -0,0 +1,13 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'minitest/autorun'
3
+ require 'simplecov'
4
+ SimpleCov.start
5
+ require 'sixarm_ruby_magic_number_type'
6
+
7
+ describe IO do
8
+
9
+ # So far, all our needs go through File.
10
+ # If you want to add tests here, please do.
11
+
12
+ end
13
+
@@ -0,0 +1,94 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'minitest/autorun'
3
+ require 'simplecov'
4
+ SimpleCov.start
5
+ require 'sixarm_ruby_magic_number_type'
6
+
7
+
8
+ describe String do
9
+
10
+ before do
11
+ SAMPLE_STRING_TO_MAGIC_NUMBER_TYPE ||= {
12
+ "<!doctyp" => :html,
13
+ "<html" => :html,
14
+ "<?xml" => :xml,
15
+ "<MakerFile" => :adobe_framemaker,
16
+ "BC" => :bitcode,
17
+ "BM" => :bitmap,
18
+ "BZ" => :bzip,
19
+ "MZ" => :exe,
20
+ "SIMPLE"=> :fits,
21
+ "GIF8" => :gif,
22
+ "GKSM" => :gks,
23
+ ["01DA"].pack('H*') => :iris_rgb,
24
+ ["F10040BB"].pack('H*') => :itc,
25
+ ["FFD8"].pack('H*') => :jpeg,
26
+ "IIN1" => :niff,
27
+ "MThd" => :midi,
28
+ "%PDF" => :pdf,
29
+ "VIEW" => :pm,
30
+ ["89504E470D0A1A0A"].pack('H*') => :png,
31
+ "Y" + ["A6"].pack('H*') + "j" + ["95"].pack('H*') => :sun_rasterfile,
32
+ "II" + ["2A00"].pack('H*') => :tiff, # II means Intel format, then 42 little-endian
33
+ "MM" + ["002A"].pack('H*') => :tiff, # MM means Motorola format, then 42 big-endian
34
+ "gimp xcf" => :gimp_xcf,
35
+ "#FIG" => :xfig,
36
+ "/* XPM */" => :xpm,
37
+ ["1F9D"].pack('H*') => :compress,
38
+ ["1F8B"].pack('H*') => :gzip,
39
+ "\x1F\xA0" => :tar_file_using_lzh_compression,
40
+ "PK\x03\x04" => :pkzip,
41
+ "7Z\xBC\xAF\x27\x1C" => :seven_zip,
42
+ "MZ" => :dos_os2_windows_executable,
43
+ ".ELF" => :unix_elf,
44
+ "\x99\x00" => :pgp_public_ring,
45
+ "\x95\x01" => :pgp_security_ring,
46
+ "\x95\x00" => :pgp_security_ring,
47
+ "\xA6\x00" => :pgp_encrypted_data,
48
+ "\xD0\xCF\x11\xE0" => :docfile,
49
+ #TODO matroska seems to have multiple magic file types-- diagnose and fix
50
+ #"\x1a\x45\xdf\xa3\xa3\x42\x86\x81\x01\x42\xf7\x81\x01\x42\xf2\x81\x04\x42\xf3\x81\x08\x42\x82\x88matroska" => :matroska_stream,
51
+ ".RTS COMPRESSED IMAGE" => :runtime_software_disk_image,
52
+ "WS" => :wordstar_document,
53
+ "!BDN" => :microsoft_outlook_personal_folder_file,
54
+ "# Disk Descripto" => :vmware_disk_description,
55
+ "# Microsoft Developer Studio" => :microsoft_developer_studio,
56
+ "#!AMR" => :adaptive_multi_rate,
57
+ "#?RADIANCE." => :radiance_high_dynamic_range_image_file,
58
+ "%!PS-Adobe-3.0 EPSF-3.0" => :encapsulated_postscript,
59
+ "8BPS" => :adobe_photoshop,
60
+ "\x00\xBF" => :adobe_flash_shared_object, # Typically a flash cookie
61
+ "\x00\x00\x00\x14ftypisom" => :iso_base_media, # ISO Base Media file (MPEG-4) v1
62
+ "\x00\x00\x00\x14ftypqt" => :quicktime_movie,
63
+ "\x00\x00\x00\x14fty3gp5" => :mpeg4_video,
64
+ "\x00\x00\x00\x14ftymp42" => :mpeg4_video_quicktime,
65
+ "\x00\x00\x00\x14ftyM4A" => :app_lossless_audio_codec,
66
+ "\x00\x01\x00\x00MYISAM DATABASE" => :msisam_database, # e.g. Microsoft Money
67
+ "\x00\x01\x00\x00Standard ACE DB" => :microsoft_access_ace_db,
68
+ "\x00\x01\x00\x00Standard Jet DB" => :microsoft_access_jet_db,
69
+ "\x00\x01BA" => :palm_address_book,
70
+ "\x00\x01BD" => :palm_date_book,
71
+ "\x01\x0F\x00\x00" => :microsoft_sql_server_2000,
72
+ "(This file must be converted with BinHex" => :binhex,
73
+ "*** Installation Started" => :symantec_wise_installer_log_file,
74
+ ".REC" => :realplayer_video,
75
+ ".RMF" => :realplayer_media,
76
+ "01ORDNANCE SURVEY" => :national_transfer_format_map,
77
+ "\x31\xBE" => :microsoft_write,
78
+ "\x32\xBE" => :microsoft_write,
79
+ "4" + ['CDB2A1'].pack('H*') => :tcpdump,
80
+ ":VERSION" => :surfplan_kite_project,
81
+ "AC10" => :autocad,
82
+ ".snd" => :sun_microsystems_law_audio_file,
83
+ "\x23\x21" => :shebang,
84
+ "%!" => :postscript
85
+ }
86
+ end
87
+
88
+ it "solves the magic number type" do
89
+ SAMPLE_STRING_TO_MAGIC_NUMBER_TYPE.each_pair{|string, type|
90
+ string.magic_number_type.must_equal type, "Trying #{string} => #{type}"
91
+ }
92
+ end
93
+
94
+ end
metadata CHANGED
@@ -1,88 +1,112 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: sixarm_ruby_magic_number_type
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.0
4
5
  prerelease:
5
- version: 1.0.0
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - SixArm
9
9
  autorequire:
10
10
  bindir: bin
11
- cert_chain:
12
- - |
13
- -----BEGIN CERTIFICATE-----
14
- MIIDBDCCAm2gAwIBAgIJAKPwEETU5bHoMA0GCSqGSIb3DQEBBQUAMGAxCzAJBgNV
15
- BAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlhMRYwFAYDVQQHEw1TYW4gRnJhbmNp
16
- c2NvMQ8wDQYDVQQKEwZTaXhBcm0xEzARBgNVBAMTCnNpeGFybS5jb20wHhcNMTAx
17
- MjEzMjMyNzEzWhcNMTMwOTA4MjMyNzEzWjBgMQswCQYDVQQGEwJVUzETMBEGA1UE
18
- CBMKQ2FsaWZvcm5pYTEWMBQGA1UEBxMNU2FuIEZyYW5jaXNjbzEPMA0GA1UEChMG
19
- U2l4QXJtMRMwEQYDVQQDEwpzaXhhcm0uY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GN
20
- ADCBiQKBgQC94mD9JDwBsunsOI0VR3CXXbOWg9cWaWciwFyJNFiM7A9I8KPLfXUw
21
- QC4czUe5ZuG4WHvinrWhkrCK+1dWBqoEClxdF/FoKO5a+tonGCjjmfy81JmFjjyx
22
- eTsjsHyvw+Qik9kpf9aj6+pnkNrVswgNHVea2o9yabbEiS6VSeJWoQIDAQABo4HF
23
- MIHCMB0GA1UdDgQWBBQzPJtqmSgc53eDN7aSzDQwr9TALDCBkgYDVR0jBIGKMIGH
24
- gBQzPJtqmSgc53eDN7aSzDQwr9TALKFkpGIwYDELMAkGA1UEBhMCVVMxEzARBgNV
25
- BAgTCkNhbGlmb3JuaWExFjAUBgNVBAcTDVNhbiBGcmFuY2lzY28xDzANBgNVBAoT
26
- BlNpeEFybTETMBEGA1UEAxMKc2l4YXJtLmNvbYIJAKPwEETU5bHoMAwGA1UdEwQF
27
- MAMBAf8wDQYJKoZIhvcNAQEFBQADgYEAooEexP/oPam1TP71SyuhxMb+uTrZbSQe
28
- jVB+ExRwWadGwaNPUA56d39qwavwP+iu+3JpeonNMVvbWXF5naCX/dNFIeREHzER
29
- ZDRQYMqru9TEMna6HD9zpcstF7vwThGovlOQ+3Y6plQ4nMzipXcZ9THqs65PIL0q
30
- eabwpCbAopo=
31
- -----END CERTIFICATE-----
32
-
33
- date: 2011-02-07 00:00:00 -08:00
34
- default_executable:
11
+ cert_chain:
12
+ - !binary |-
13
+ LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURCRENDQW0yZ0F3SUJB
14
+ Z0lKQUtQd0VFVFU1YkhvTUEwR0NTcUdTSWIzRFFFQkJRVUFNR0F4Q3pBSkJn
15
+ TlYKQkFZVEFsVlRNUk13RVFZRFZRUUlFd3BEWVd4cFptOXlibWxoTVJZd0ZB
16
+ WURWUVFIRXcxVFlXNGdSbkpoYm1OcApjMk52TVE4d0RRWURWUVFLRXdaVGFY
17
+ aEJjbTB4RXpBUkJnTlZCQU1UQ25OcGVHRnliUzVqYjIwd0hoY05NVEF4Ck1q
18
+ RXpNak15TnpFeldoY05NVE13T1RBNE1qTXlOekV6V2pCZ01Rc3dDUVlEVlFR
19
+ R0V3SlZVekVUTUJFR0ExVUUKQ0JNS1EyRnNhV1p2Y201cFlURVdNQlFHQTFV
20
+ RUJ4TU5VMkZ1SUVaeVlXNWphWE5qYnpFUE1BMEdBMVVFQ2hNRwpVMmw0UVhK
21
+ dE1STXdFUVlEVlFRREV3cHphWGhoY20wdVkyOXRNSUdmTUEwR0NTcUdTSWIz
22
+ RFFFQkFRVUFBNEdOCkFEQ0JpUUtCZ1FDOTRtRDlKRHdCc3Vuc09JMFZSM0NY
23
+ WGJPV2c5Y1dhV2Npd0Z5Sk5GaU03QTlJOEtQTGZYVXcKUUM0Y3pVZTVadUc0
24
+ V0h2aW5yV2hrckNLKzFkV0Jxb0VDbHhkRi9Gb0tPNWErdG9uR0Nqam1meTgx
25
+ Sm1Gamp5eAplVHNqc0h5dncrUWlrOWtwZjlhajYrcG5rTnJWc3dnTkhWZWEy
26
+ bzl5YWJiRWlTNlZTZUpXb1FJREFRQUJvNEhGCk1JSENNQjBHQTFVZERnUVdC
27
+ QlF6UEp0cW1TZ2M1M2VETjdhU3pEUXdyOVRBTERDQmtnWURWUjBqQklHS01J
28
+ R0gKZ0JRelBKdHFtU2djNTNlRE43YVN6RFF3cjlUQUxLRmtwR0l3WURFTE1B
29
+ a0dBMVVFQmhNQ1ZWTXhFekFSQmdOVgpCQWdUQ2tOaGJHbG1iM0p1YVdFeEZq
30
+ QVVCZ05WQkFjVERWTmhiaUJHY21GdVkybHpZMjh4RHpBTkJnTlZCQW9UCkJs
31
+ TnBlRUZ5YlRFVE1CRUdBMVVFQXhNS2MybDRZWEp0TG1OdmJZSUpBS1B3RUVU
32
+ VTViSG9NQXdHQTFVZEV3UUYKTUFNQkFmOHdEUVlKS29aSWh2Y05BUUVGQlFB
33
+ RGdZRUFvb0VleFAvb1BhbTFUUDcxU3l1aHhNYit1VHJaYlNRZQpqVkIrRXhS
34
+ d1dhZEd3YU5QVUE1NmQzOXF3YXZ3UCtpdSszSnBlb25OTVZ2YldYRjVuYUNY
35
+ L2RORkllUkVIekVSClpEUlFZTXFydTlURU1uYTZIRDl6cGNzdEY3dndUaEdv
36
+ dmxPUSszWTZwbFE0bk16aXBYY1o5VEhxczY1UElMMHEKZWFid3BDYkFvcG89
37
+ Ci0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0K
38
+ date: 2012-09-07 00:00:00.000000000 Z
35
39
  dependencies: []
36
-
37
40
  description:
38
41
  email: sixarm@sixarm.com
39
42
  executables: []
40
-
41
43
  extensions: []
42
-
43
44
  extra_rdoc_files: []
44
-
45
- files:
46
- - README.rdoc
47
- - LICENSE.txt
45
+ files:
46
+ - .gemtest
47
+ - Rakefile
48
+ - README.md
49
+ - VERSION
48
50
  - lib/sixarm_ruby_magic_number_type.rb
49
- - test/sixarm_ruby_magic_number_type/sample.fit
50
- - test/sixarm_ruby_magic_number_type/sample.gif
51
- - test/sixarm_ruby_magic_number_type/sample.jpg
52
- - test/sixarm_ruby_magic_number_type/sample.png
53
- - test/sixarm_ruby_magic_number_type/sample.ps
54
- - test/sixarm_ruby_magic_number_type/sample.ras
55
- - test/sixarm_ruby_magic_number_type/sample.sgi
56
- - test/sixarm_ruby_magic_number_type/sample.tiff
57
- - test/sixarm_ruby_magic_number_type/sample.xcf.bz2
58
- - test/sixarm_ruby_magic_number_type/sample.xcf.gz
59
- has_rdoc: true
51
+ - lib/sixarm_ruby_magic_number_type/file.rb
52
+ - lib/sixarm_ruby_magic_number_type/io.rb
53
+ - lib/sixarm_ruby_magic_number_type/string.rb
54
+ - test/sixarm_ruby_magic_number_type_test.rb
55
+ - test/sixarm_ruby_magic_number_type_test/file_test.rb
56
+ - test/sixarm_ruby_magic_number_type_test/io_test.rb
57
+ - test/sixarm_ruby_magic_number_type_test/string_test.rb
58
+ - test/sixarm_ruby_magic_number_type_test/samples/sample.html
59
+ - test/sixarm_ruby_magic_number_type_test/samples/sample.xml
60
+ - test/sixarm_ruby_magic_number_type_test/samples/sample.fit
61
+ - test/sixarm_ruby_magic_number_type_test/samples/sample.gif
62
+ - test/sixarm_ruby_magic_number_type_test/samples/sample.jpg
63
+ - test/sixarm_ruby_magic_number_type_test/samples/sample.png
64
+ - test/sixarm_ruby_magic_number_type_test/samples/sample.ps
65
+ - test/sixarm_ruby_magic_number_type_test/samples/sample.ras
66
+ - test/sixarm_ruby_magic_number_type_test/samples/sample.sgi
67
+ - test/sixarm_ruby_magic_number_type_test/samples/sample.tiff
68
+ - test/sixarm_ruby_magic_number_type_test/samples/sample.bz2
69
+ - test/sixarm_ruby_magic_number_type_test/samples/sample.gz
60
70
  homepage: http://sixarm.com/
61
71
  licenses: []
62
-
63
72
  post_install_message:
64
73
  rdoc_options: []
65
-
66
- require_paths:
74
+ require_paths:
67
75
  - lib
68
- required_ruby_version: !ruby/object:Gem::Requirement
76
+ required_ruby_version: !ruby/object:Gem::Requirement
69
77
  none: false
70
- requirements:
71
- - - ">="
72
- - !ruby/object:Gem::Version
73
- version: "0"
74
- required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
83
  none: false
76
- requirements:
77
- - - ">="
78
- - !ruby/object:Gem::Version
79
- version: "0"
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
80
88
  requirements: []
81
-
82
89
  rubyforge_project:
83
- rubygems_version: 1.5.0
90
+ rubygems_version: 1.8.23
84
91
  signing_key:
85
92
  specification_version: 3
86
- summary: "SixArm.com \xC2\xBB Ruby \xC2\xBB Magic number type inspects bytes to decide on a mime type"
87
- test_files: []
88
-
93
+ summary: SixArm.com » Ruby » Magic number type inspects bytes to decide on a mime
94
+ type
95
+ test_files:
96
+ - test/sixarm_ruby_magic_number_type_test.rb
97
+ - test/sixarm_ruby_magic_number_type_test/file_test.rb
98
+ - test/sixarm_ruby_magic_number_type_test/io_test.rb
99
+ - test/sixarm_ruby_magic_number_type_test/string_test.rb
100
+ - test/sixarm_ruby_magic_number_type_test/samples/sample.html
101
+ - test/sixarm_ruby_magic_number_type_test/samples/sample.xml
102
+ - test/sixarm_ruby_magic_number_type_test/samples/sample.fit
103
+ - test/sixarm_ruby_magic_number_type_test/samples/sample.gif
104
+ - test/sixarm_ruby_magic_number_type_test/samples/sample.jpg
105
+ - test/sixarm_ruby_magic_number_type_test/samples/sample.png
106
+ - test/sixarm_ruby_magic_number_type_test/samples/sample.ps
107
+ - test/sixarm_ruby_magic_number_type_test/samples/sample.ras
108
+ - test/sixarm_ruby_magic_number_type_test/samples/sample.sgi
109
+ - test/sixarm_ruby_magic_number_type_test/samples/sample.tiff
110
+ - test/sixarm_ruby_magic_number_type_test/samples/sample.bz2
111
+ - test/sixarm_ruby_magic_number_type_test/samples/sample.gz
112
+ has_rdoc: true
metadata.gz.sig CHANGED
Binary file
data/LICENSE.txt DELETED
@@ -1,12 +0,0 @@
1
- LICENSE
2
-
3
- You may choose any of these licenses:
4
-
5
- - CreativeCommons License, Non-commercial Share Alike
6
- - LGPL, GNU Lesser General Public License
7
- - MIT License
8
- - Ruby License
9
-
10
- THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
11
- IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
12
- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
data/README.rdoc DELETED
@@ -1,30 +0,0 @@
1
-
2
- = SixArm.com » Ruby » MagicNumberType
3
-
4
- Author:: Joel Parker Henderson, joelparkerhenderson@gmail.com
5
- Copyright:: Copyright (c) 2010 Joel Parker Henderson
6
- License:: See LICENSE.txt file
7
-
8
- MagicNumberType infers the mime type of data from the leading bits.
9
-
10
- It infers based on widespread programming conventions for data file formats.
11
-
12
- Magic numbers are the first bits of a file or data stream
13
- which uniquely identify the type of file or data stream.
14
-
15
- For example when the first bits are "BM",
16
- this identifies the file as a bitmap image file.
17
-
18
- These magic numbers are by convention and we are using this guide:
19
- http://www.astro.keele.ac.uk/oldusers/rno/Computing/File_magic.html
20
-
21
- Typical uses of magic numbers:
22
- - to quickly identify a file's data type
23
- - to check if data matches the file's MIME type or extension
24
- - to check if a web form file upload matches its HTTP content type
25
-
26
- Compare:
27
- - MIME::Types ruby library
28
- - Unix magic() command for testing files on disk
29
- - http://shared-mime.rubyforge.org/
30
-