48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
# File 'lib/fileshunter/Decoders/JPEG.rb', line 48
def decode(offset)
ending_offset = nil
cursor = offset + 2
nbr_segments = 0
quantisation_tables_id = []
huffman_ac_tables_id = []
huffman_dc_tables_id = []
found_sos = false
found_sof = false
while (ending_offset == nil)
log_debug "@#{cursor} Decoding next offset: #{@data[cursor..cursor+1].inspect}"
invalid_data("@#{cursor} - Did not get a valid marker definition: #{@data[cursor..cursor+1].inspect}") if (@data[cursor] != MARKER_PREFIX)
c_1 = @data[cursor+1]
invalid_data("@#{cursor} - Invalid marker: #{c_1.ord}") if (c_1.ord < 192)
if (MARKERS_WITHOUT_PAYLOAD.include?(c_1))
log_debug "=== No payload"
cursor += 2
ending_offset = cursor if (c_1 == END_MARKER)
else
size = BinData::Uint16be.read(@data[cursor+2..cursor+3])
log_debug "=== Payload of size #{size}"
case c_1
when MARKER_APP0
log_debug "@#{cursor} - Found APP0 marker"
case @data[cursor+4..cursor+8]
when JFIF_HEADER
invalid_data("@#{cursor} - Invalid size for JFIF marker: #{size}") if (size < 16)
version_major = @data[cursor+9].ord
version_minor = @data[cursor+10].ord
units = @data[cursor+11].ord
invalid_data("@#{cursor} - Invalid units: #{units}") if (units > 2)
width = BinData::Uint16be.read(@data[cursor+12..cursor+13])
invalid_data("@#{cursor} - Invalid width: #{width}") if (width == 0)
height = BinData::Uint16be.read(@data[cursor+14..cursor+15])
invalid_data("@#{cursor} - Invalid height: #{height}") if (height == 0)
jfif_metadata = {
:version_major => version_major,
:version_minor => version_minor,
:units => units,
:width => width,
:height => height
}
if (size > 16)
width_thumb = BinData::Uint16be.read(@data[cursor+16..cursor+17])
height_thumb = BinData::Uint16be.read(@data[cursor+18..cursor+19])
jfif_metadata.merge!(
:width_thumb => width_thumb,
:height_thumb => height_thumb
)
end
metadata( :jfif_metadata => jfif_metadata )
when JFXX_HEADER
extension_code = @data[cursor+9].ord
invalid_data("@#{cursor} - Invalid extension code: #{extension_code}") if (!VALID_EXTENSION_CODES.include?(extension_code))
metadata( :jfxx_metadata => { :extension_code => extension_code } )
end
when MARKER_APP1
log_debug "@#{cursor} - Found APP1 marker"
case @data[cursor+4..cursor+9]
when EXIF_HEADER
require 'fileshunter/Decoders/TIFF'
invalid_data("@#{cursor} - Invalid TIFF header") if (@data[cursor+10..cursor+13].index(FilesHunter::Decoders::TIFF::BEGIN_PATTERN_TIFF) != 0)
tiff_decoder = FilesHunter::Decoders::TIFF.new
tiff_decoder.setup(FilesHunter::get_segments_analyzer, @data, cursor+10, cursor+2+size)
tiff_decoder.accept_no_image_data
begin
tiff_decoder.find_segments
rescue InvalidDataError, TruncatedDataError, AccessAfterDataError
invalid_data("@#{cursor} - Invalid TIFF data: #{$!}")
end
segments = tiff_decoder.segments_found
invalid_data("@#{cursor} - No valid TIFF segment found for Exif") if segments.empty?
invalid_data("@#{cursor} - Not a valid TIFF segment found for Exif. Found #{segments[0].extensions.inspect}.") if (!segments[0].extensions.include?(:tif))
invalid_data("@#{cursor} - Truncated TIFF segment found for Exif.") if (segments[0].truncated)
invalid_data("@#{cursor} - TIFF segment (@#{segments[0].begin_offset}) not found at the beginning of Exif (#{cursor+10}).") if (segments[0].begin_offset != cursor+10)
metadata( :exif_metadata => segments[0].metadata )
found_relevant_data([:jpg, :thm])
end
when MARKER_SOF0..MARKER_SOF3
log_debug "@#{cursor} - Found SOF marker"
invalid_data("@#{cursor} - Found several SOF markers") if found_sof
invalid_data("@#{cursor} - Found a SOF marker after the SOS marker") if found_sos
found_sof = true
sample_precision = @data[cursor+4].ord
invalid_data("@#{cursor} - Invalid sample precision: #{sample_precision}") if ((sample_precision != 8) and (sample_precision != 12))
image_height = BinData::Uint16be.read(@data[cursor+5..cursor+6])
image_width = BinData::Uint16be.read(@data[cursor+7..cursor+8])
metadata(
:image_height => image_height,
:image_width => image_width
)
nbr_components = @data[cursor+9].ord
invalid_data("@#{cursor} - Invalid number of components: #{nbr_components}") if (nbr_components == 0)
nbr_components.times do |idx_component|
sampling = @data[cursor+11+idx_component*3].ord
horizontal_sampling = ((sampling & 0b11110000) >> 4)
vertical_sampling = (sampling & 0b00001111)
invalid_data("@#{cursor} - Invalid horizontal sampling: #{horizontal_sampling}") if (horizontal_sampling == 0)
invalid_data("@#{cursor} - Invalid vertical sampling: #{vertical_sampling}") if (vertical_sampling == 0)
dqt_id = @data[cursor+12+idx_component*3].ord
invalid_data("@#{cursor} - Missing quantisation table ID #{dqt_id}") if (!quantisation_tables_id.include?(dqt_id))
end
when MARKER_DHT
log_debug "@#{cursor} - Found DHT marker"
end_cursor = cursor + 2 + size
dht_cursor = cursor + 4
while (dht_cursor < end_cursor)
= @data[dht_cursor].ord
huffman_type = (( & 0b11110000) >> 4)
invalid_data("@#{cursor} - Unknown Huffman table type: #{huffman_type}") if (huffman_type > 1)
if (huffman_type == 0)
huffman_dc_table_id = ( & 0b00001111)
huffman_dc_tables_id << huffman_dc_table_id
log_debug "@#{cursor} - Found Huffman DC table: #{huffman_dc_table_id}"
else
huffman_ac_table_id = ( & 0b00001111)
huffman_ac_tables_id << huffman_ac_table_id
log_debug "@#{cursor} - Found Huffman AC table: #{huffman_ac_table_id}"
end
nbr_elements = 0
@data[dht_cursor+1..dht_cursor+16].bytes.each do |nbr_element_for_depth|
nbr_elements += nbr_element_for_depth
end
dht_cursor += 17 + nbr_elements
invalid_data("@#{dqt_cursor} - End of Huffman table was supposed to be @#{end_cursor}.") if (dht_cursor > end_cursor)
end
when MARKER_SOS
log_debug "@#{cursor} - Found SOS marker"
invalid_data("@#{cursor} - SOS marker begins whereas no quantisation table has been defined.") if (quantisation_tables_id.empty?)
invalid_data("@#{cursor} - SOS marker begins whereas no SOF marker has been encountered.") if (!found_sof)
found_sos = true
nbr_components = @data[cursor+4].ord
invalid_data("@#{cursor} - Invalid number of components: #{nbr_components}") if (nbr_components == 0)
nbr_components.times do |idx_component|
huffman_table_ids = @data[cursor+6+2*idx_component].ord
huffman_dc_table_id = ((huffman_table_ids & 0b11110000) >> 4)
huffman_ac_table_id = (huffman_table_ids & 0b00001111)
end
when MARKER_DQT
log_debug "@#{cursor} - Found DQT marker"
end_cursor = cursor + 2 + size
dqt_cursor = cursor + 4
while (dqt_cursor < end_cursor)
= @data[dqt_cursor].ord
precision = (( & 0b11110000) >> 4)
quantisation_table_id = ( & 0b00001111)
invalid_data("@#{cursor} - Quantisation table id #{quantisation_table_id} already defined.") if (quantisation_tables_id.include?(quantisation_table_id))
quantisation_tables_id << quantisation_table_id
log_debug "@#{cursor} - Found quantisation table: #{quantisation_table_id}"
dqt_cursor += 1 + 64*((precision == 0) ? 1 : 2)
invalid_data("@#{dqt_cursor} - End of quantisation table was supposed to be @#{end_cursor}.") if (dqt_cursor > end_cursor)
end
else
log_debug "@#{cursor} - Found ignored marker: #{c_1.inspect}"
end
if (c_1 == MARKER_WITH_ENTROPY_DATA)
found_relevant_data([:jpg, :thm])
cursor = @data.index(MARKERS_IGNORED_IN_ENTROPY_DATA_REGEXP, cursor + 2 + size, 2)
log_debug "=== Entropy data gets to cursor #{cursor.inspect}"
truncated_data("@#{cursor} - Truncated entropy data segment", @end_offset) if (cursor == nil)
else
cursor += 2 + size
end
end
nbr_segments += 1
progress(cursor)
end
metadata( :nbr_segments => nbr_segments )
return ending_offset
end
|