-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathsegment_list.go
395 lines (312 loc) · 8.26 KB
/
segment_list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package jpegstructure
import (
"bytes"
"fmt"
"io"
"crypto/sha1"
"encoding/binary"
"github.com/dsoprea/go-exif/v2"
"github.com/dsoprea/go-iptc"
"github.com/dsoprea/go-logging"
)
// SegmentList contains a slice of segments.
type SegmentList struct {
segments []*Segment
}
// NewSegmentList returns a new SegmentList struct.
func NewSegmentList(segments []*Segment) (sl *SegmentList) {
if segments == nil {
segments = make([]*Segment, 0)
}
return &SegmentList{
segments: segments,
}
}
// OffsetsEqual returns true is all segments have the same marker-IDs and were
// found at the same offsets.
func (sl *SegmentList) OffsetsEqual(o *SegmentList) bool {
if len(o.segments) != len(sl.segments) {
return false
}
for i, s := range o.segments {
if s.MarkerId != sl.segments[i].MarkerId || s.Offset != sl.segments[i].Offset {
return false
}
}
return true
}
// Segments returns the underlying slice of segments.
func (sl *SegmentList) Segments() []*Segment {
return sl.segments
}
// Add adds another segment.
func (sl *SegmentList) Add(s *Segment) {
sl.segments = append(sl.segments, s)
}
// Print prints segment info.
func (sl *SegmentList) Print() {
if len(sl.segments) == 0 {
fmt.Printf("No segments.\n")
} else {
exifIndex, _, err := sl.FindExif()
if err != nil {
if err == exif.ErrNoExif {
exifIndex = -1
} else {
log.Panic(err)
}
}
xmpIndex, _, err := sl.FindXmp()
if err != nil {
if err == ErrNoXmp {
xmpIndex = -1
} else {
log.Panic(err)
}
}
iptcIndex, _, err := sl.FindIptc()
if err != nil {
if err == ErrNoIptc {
iptcIndex = -1
} else {
log.Panic(err)
}
}
for i, s := range sl.segments {
fmt.Printf("%2d: %s", i, s.EmbeddedString())
if i == exifIndex {
fmt.Printf(" [EXIF]")
} else if i == xmpIndex {
fmt.Printf(" [XMP]")
} else if i == iptcIndex {
fmt.Printf(" [IPTC]")
}
fmt.Printf("\n")
}
}
}
// Validate checks that all of the markers are actually located at all of the
// recorded offsets.
func (sl *SegmentList) Validate(data []byte) (err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
if len(sl.segments) < 2 {
log.Panicf("minimum segments not found")
}
if sl.segments[0].MarkerId != MARKER_SOI {
log.Panicf("first segment not SOI")
} else if sl.segments[len(sl.segments)-1].MarkerId != MARKER_EOI {
log.Panicf("last segment not EOI")
}
lastOffset := 0
for i, s := range sl.segments {
if lastOffset != 0 && s.Offset <= lastOffset {
log.Panicf("segment offset not greater than the last: SEGMENT=(%d) (0x%08x) <= (0x%08x)", i, s.Offset, lastOffset)
}
// The scan-data doesn't start with a marker.
if s.MarkerId == 0x0 {
continue
}
o := s.Offset
if bytes.Compare(data[o:o+2], []byte{0xff, s.MarkerId}) != 0 {
log.Panicf("segment offset does not point to the start of a segment: SEGMENT=(%d) (0x%08x)", i, s.Offset)
}
lastOffset = o
}
return nil
}
// FindExif returns the the segment that hosts the EXIF data (if present).
func (sl *SegmentList) FindExif() (index int, segment *Segment, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
for i, s := range sl.segments {
if s.IsExif() == true {
return i, s, nil
}
}
return -1, nil, exif.ErrNoExif
}
// FindXmp returns the the segment that hosts the XMP data (if present).
func (sl *SegmentList) FindXmp() (index int, segment *Segment, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
for i, s := range sl.segments {
if s.IsXmp() == true {
return i, s, nil
}
}
return -1, nil, ErrNoXmp
}
// FindIptc returns the the segment that hosts the IPTC data (if present).
func (sl *SegmentList) FindIptc() (index int, segment *Segment, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
for i, s := range sl.segments {
if s.IsIptc() == true {
return i, s, nil
}
}
return -1, nil, ErrNoIptc
}
// Exif returns an `exif.Ifd` instance for the EXIF data we currently have.
func (sl *SegmentList) Exif() (rootIfd *exif.Ifd, rawExif []byte, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
_, s, err := sl.FindExif()
log.PanicIf(err)
rootIfd, rawExif, err = s.Exif()
log.PanicIf(err)
return rootIfd, rawExif, nil
}
// Iptc returns embedded IPTC data if present.
func (sl *SegmentList) Iptc() (tags map[iptc.StreamTagKey][]iptc.TagData, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
// TODO(dustin): Add comment and return data.
_, s, err := sl.FindIptc()
log.PanicIf(err)
tags, err = s.Iptc()
log.PanicIf(err)
return tags, nil
}
// ConstructExifBuilder returns an `exif.IfdBuilder` instance (needed for
// modifying) preloaded with all existing tags.
func (sl *SegmentList) ConstructExifBuilder() (rootIb *exif.IfdBuilder, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
rootIfd, _, err := sl.Exif()
log.PanicIf(err)
ib := exif.NewIfdBuilderFromExistingChain(rootIfd)
return ib, nil
}
// DumpExif returns an unstructured list of tags (useful when just reviewing).
func (sl *SegmentList) DumpExif() (segmentIndex int, segment *Segment, exifTags []exif.ExifTag, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
segmentIndex, s, err := sl.FindExif()
if err != nil {
if err == exif.ErrNoExif {
return 0, nil, nil, err
}
log.Panic(err)
}
exifTags, err = s.FlatExif()
log.PanicIf(err)
return segmentIndex, s, exifTags, nil
}
func makeEmptyExifSegment() (s *Segment) {
// TODO(dustin): Add test
return &Segment{
MarkerId: MARKER_APP1,
}
}
// SetExif encodes and sets EXIF data into the given segment. If `index` is -1,
// append a new segment.
func (sl *SegmentList) SetExif(ib *exif.IfdBuilder) (err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
_, s, err := sl.FindExif()
if err != nil {
if log.Is(err, exif.ErrNoExif) == false {
log.Panic(err)
}
s = makeEmptyExifSegment()
prefix := sl.segments[:1]
// Install it near the beginning where we know it's safe. We can't
// insert it after the EOI segment, and there might be more than one
// depending on implementation and/or lax adherence to the standard.
tail := append([]*Segment{s}, sl.segments[1:]...)
sl.segments = append(prefix, tail...)
}
err = s.SetExif(ib)
log.PanicIf(err)
return nil
}
// DropExif will drop the EXIF data if present.
func (sl *SegmentList) DropExif() (wasDropped bool, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
// TODO(dustin): Add test
i, _, err := sl.FindExif()
if err == nil {
// Found.
sl.segments = append(sl.segments[:i], sl.segments[i+1:]...)
return true, nil
} else if log.Is(err, exif.ErrNoExif) == false {
log.Panic(err)
}
// Not found.
return false, nil
}
// Write writes the segment data to the given `io.Writer`.
func (sl *SegmentList) Write(w io.Writer) (err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
offset := 0
for i, s := range sl.segments {
h := sha1.New()
h.Write(s.Data)
// The scan-data will have a marker-ID of (0) because it doesn't have a
// marker-ID or length.
if s.MarkerId != 0 {
_, err := w.Write([]byte{0xff})
log.PanicIf(err)
offset++
_, err = w.Write([]byte{s.MarkerId})
log.PanicIf(err)
offset++
sizeLen, found := markerLen[s.MarkerId]
if found == false || sizeLen == 2 {
sizeLen = 2
l := uint16(len(s.Data) + sizeLen)
err = binary.Write(w, binary.BigEndian, &l)
log.PanicIf(err)
offset += 2
} else if sizeLen == 4 {
l := uint32(len(s.Data) + sizeLen)
err = binary.Write(w, binary.BigEndian, &l)
log.PanicIf(err)
offset += 4
} else if sizeLen != 0 {
log.Panicf("not a supported marker-size: SEGMENT-INDEX=(%d) MARKER-ID=(0x%02x) MARKER-SIZE-LEN=(%d)", i, s.MarkerId, sizeLen)
}
}
_, err := w.Write(s.Data)
log.PanicIf(err)
offset += len(s.Data)
}
return nil
}