-
Notifications
You must be signed in to change notification settings - Fork 70
/
ifd_builder.go
1265 lines (989 loc) · 30.1 KB
/
ifd_builder.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package exif
// NOTES:
//
// The thumbnail offset and length tags shouldn't be set directly. Use the
// (*IfdBuilder).SetThumbnail() method instead.
import (
"errors"
"fmt"
"strings"
"encoding/binary"
"github.com/dsoprea/go-logging"
)
var (
ifdBuilderLogger = log.NewLogger("exif.ifd_builder")
)
var (
ErrTagEntryNotFound = errors.New("tag entry not found")
ErrChildIbNotFound = errors.New("child IB not found")
)
type IfdBuilderTagValue struct {
valueBytes []byte
ib *IfdBuilder
}
func (ibtv IfdBuilderTagValue) String() string {
if ibtv.IsBytes() == true {
var valuePhrase string
if len(ibtv.valueBytes) <= 8 {
valuePhrase = fmt.Sprintf("%v", ibtv.valueBytes)
} else {
valuePhrase = fmt.Sprintf("%v...", ibtv.valueBytes[:8])
}
return fmt.Sprintf("IfdBuilderTagValue<BYTES=%v LEN=(%d)>", valuePhrase, len(ibtv.valueBytes))
} else if ibtv.IsIb() == true {
return fmt.Sprintf("IfdBuilderTagValue<IB=%s>", ibtv.ib)
} else {
log.Panicf("IBTV state undefined")
return ""
}
}
func NewIfdBuilderTagValueFromBytes(valueBytes []byte) *IfdBuilderTagValue {
return &IfdBuilderTagValue{
valueBytes: valueBytes,
}
}
func NewIfdBuilderTagValueFromIfdBuilder(ib *IfdBuilder) *IfdBuilderTagValue {
return &IfdBuilderTagValue{
ib: ib,
}
}
// IsBytes returns true if the bytes are populated. This is always the case
// when we're loaded from a tag in an existing IFD.
func (ibtv IfdBuilderTagValue) IsBytes() bool {
return ibtv.valueBytes != nil
}
func (ibtv IfdBuilderTagValue) Bytes() []byte {
if ibtv.IsBytes() == false {
log.Panicf("this tag is not a byte-slice value")
} else if ibtv.IsIb() == true {
log.Panicf("this tag is an IFD-builder value not a byte-slice")
}
return ibtv.valueBytes
}
func (ibtv IfdBuilderTagValue) IsIb() bool {
return ibtv.ib != nil
}
func (ibtv IfdBuilderTagValue) Ib() *IfdBuilder {
if ibtv.IsIb() == false {
log.Panicf("this tag is not an IFD-builder value")
} else if ibtv.IsBytes() == true {
log.Panicf("this tag is a byte-slice, not a IFD-builder")
}
return ibtv.ib
}
type BuilderTag struct {
// ifdPath is the path of the IFD that hosts this tag.
ifdPath string
tagId uint16
typeId TagTypePrimitive
// value is either a value that can be encoded, an IfdBuilder instance (for
// child IFDs), or an IfdTagEntry instance representing an existing,
// previously-stored tag.
value *IfdBuilderTagValue
// byteOrder is the byte order. It's chiefly/originally here to support
// printing the value.
byteOrder binary.ByteOrder
}
func NewBuilderTag(ifdPath string, tagId uint16, typeId TagTypePrimitive, value *IfdBuilderTagValue, byteOrder binary.ByteOrder) *BuilderTag {
return &BuilderTag{
ifdPath: ifdPath,
tagId: tagId,
typeId: typeId,
value: value,
byteOrder: byteOrder,
}
}
func NewChildIfdBuilderTag(ifdPath string, tagId uint16, value *IfdBuilderTagValue) *BuilderTag {
return &BuilderTag{
ifdPath: ifdPath,
tagId: tagId,
typeId: TypeLong,
value: value,
}
}
func (bt *BuilderTag) Value() (value *IfdBuilderTagValue) {
return bt.value
}
func (bt *BuilderTag) String() string {
var valueString string
if bt.value.IsBytes() == true {
var err error
valueString, err = Format(bt.value.Bytes(), bt.typeId, false, bt.byteOrder)
log.PanicIf(err)
} else {
valueString = fmt.Sprintf("%v", bt.value)
}
return fmt.Sprintf("BuilderTag<IFD-PATH=[%s] TAG-ID=(0x%04x) TAG-TYPE=[%s] VALUE=[%s]>", bt.ifdPath, bt.tagId, TypeNames[bt.typeId], valueString)
}
func (bt *BuilderTag) SetValue(byteOrder binary.ByteOrder, value interface{}) (err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
// TODO(dustin): !! Add test.
tt := NewTagType(bt.typeId, byteOrder)
ve := NewValueEncoder(byteOrder)
var ed EncodedData
if bt.typeId == TypeUndefined {
var err error
ed, err = EncodeUndefined(bt.ifdPath, bt.tagId, value)
log.PanicIf(err)
} else {
var err error
ed, err = ve.EncodeWithType(tt, value)
log.PanicIf(err)
}
bt.value = NewIfdBuilderTagValueFromBytes(ed.Encoded)
return nil
}
// NewStandardBuilderTag constructs a `BuilderTag` instance. The type is looked
// up. `ii` is the type of IFD that owns this tag.
func NewStandardBuilderTag(ifdPath string, it *IndexedTag, byteOrder binary.ByteOrder, value interface{}) *BuilderTag {
typeId := it.Type
tt := NewTagType(typeId, byteOrder)
ve := NewValueEncoder(byteOrder)
var ed EncodedData
if it.Type == TypeUndefined {
var err error
ed, err = EncodeUndefined(ifdPath, it.Id, value)
log.PanicIf(err)
} else {
var err error
ed, err = ve.EncodeWithType(tt, value)
log.PanicIf(err)
}
tagValue := NewIfdBuilderTagValueFromBytes(ed.Encoded)
return NewBuilderTag(
ifdPath,
it.Id,
typeId,
tagValue,
byteOrder)
}
type IfdBuilder struct {
// ifdName is the name of the IFD represented by this instance.
name string
// ifdPath is the path of the IFD represented by this instance.
ifdPath string
// fqIfdPath is the fully-qualified path of the IFD represented by this
// instance.
fqIfdPath string
// ifdTagId will be non-zero if we're a child IFD.
ifdTagId uint16
byteOrder binary.ByteOrder
// Includes both normal tags and IFD tags (which point to child IFDs).
// TODO(dustin): Keep a separate list of children like with `Ifd`.
// TODO(dustin): Either rename this or `Entries` in `Ifd` to be the same thing.
tags []*BuilderTag
// existingOffset will be the offset that this IFD is currently found at if
// it represents an IFD that has previously been stored (or 0 if not).
existingOffset uint32
// nextIb represents the next link if we're chaining to another.
nextIb *IfdBuilder
// thumbnailData is populated with thumbnail data if there was thumbnail
// data. Otherwise, it's nil.
thumbnailData []byte
ifdMapping *IfdMapping
tagIndex *TagIndex
}
func NewIfdBuilder(ifdMapping *IfdMapping, tagIndex *TagIndex, fqIfdPath string, byteOrder binary.ByteOrder) (ib *IfdBuilder) {
ifdPath, err := ifdMapping.StripPathPhraseIndices(fqIfdPath)
log.PanicIf(err)
var ifdTagId uint16
mi, err := ifdMapping.GetWithPath(ifdPath)
if err == nil {
ifdTagId = mi.TagId
} else if log.Is(err, ErrChildIfdNotMapped) == false {
log.Panic(err)
}
ib = &IfdBuilder{
// The right-most part of the IFD-path.
name: mi.Name,
// ifdPath describes the current IFD placement within the IFD tree.
ifdPath: ifdPath,
// fqIfdPath describes the current IFD placement within the IFD tree as
// well as being qualified with non-zero indices.
fqIfdPath: fqIfdPath,
// ifdTagId is empty unless it's a child-IFD.
ifdTagId: ifdTagId,
byteOrder: byteOrder,
tags: make([]*BuilderTag, 0),
ifdMapping: ifdMapping,
tagIndex: tagIndex,
}
return ib
}
// NewIfdBuilderWithExistingIfd creates a new IB using the same header type
// information as the given IFD.
func NewIfdBuilderWithExistingIfd(ifd *Ifd) (ib *IfdBuilder) {
name := ifd.Name
ifdPath := ifd.IfdPath
fqIfdPath := ifd.FqIfdPath
var ifdTagId uint16
// There is no tag-ID for the root IFD. It will never be a child IFD.
if ifdPath != IfdPathStandard {
mi, err := ifd.ifdMapping.GetWithPath(ifdPath)
log.PanicIf(err)
ifdTagId = mi.TagId
}
ib = &IfdBuilder{
name: name,
ifdPath: ifdPath,
fqIfdPath: fqIfdPath,
ifdTagId: ifdTagId,
byteOrder: ifd.ByteOrder,
existingOffset: ifd.Offset,
ifdMapping: ifd.ifdMapping,
tagIndex: ifd.tagIndex,
}
return ib
}
// NewIfdBuilderFromExistingChain creates a chain of IB instances from an
// IFD chain generated from real data.
func NewIfdBuilderFromExistingChain(rootIfd *Ifd, itevr *IfdTagEntryValueResolver) (firstIb *IfdBuilder) {
// OBSOLETE(dustin): Support for `itevr` is now obsolete. This parameter will be removed in the future.
var lastIb *IfdBuilder
i := 0
for thisExistingIfd := rootIfd; thisExistingIfd != nil; thisExistingIfd = thisExistingIfd.NextIfd {
newIb := NewIfdBuilder(rootIfd.ifdMapping, rootIfd.tagIndex, rootIfd.FqIfdPath, thisExistingIfd.ByteOrder)
if firstIb == nil {
firstIb = newIb
} else {
lastIb.SetNextIb(newIb)
}
err := newIb.AddTagsFromExisting(thisExistingIfd, nil, nil, nil)
log.PanicIf(err)
lastIb = newIb
i++
}
return firstIb
}
func (ib *IfdBuilder) NextIb() (nextIb *IfdBuilder, err error) {
return ib.nextIb, nil
}
func (ib *IfdBuilder) ChildWithTagId(childIfdTagId uint16) (childIb *IfdBuilder, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
for _, bt := range ib.tags {
if bt.value.IsIb() == false {
continue
}
childIbThis := bt.value.Ib()
if childIbThis.ifdTagId == childIfdTagId {
return childIbThis, nil
}
}
log.Panic(ErrChildIbNotFound)
// Never reached.
return nil, nil
}
func getOrCreateIbFromRootIbInner(rootIb *IfdBuilder, parentIb *IfdBuilder, currentLineage []IfdTagIdAndIndex) (ib *IfdBuilder, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
// TODO(dustin): !! Add test.
thisIb := rootIb
// Since we're calling ourselves recursively with incrementally different
// paths, the FQ IFD-path of the parent that called us needs to be passed
// in, in order for us to know it.
var parentLineage []IfdTagIdAndIndex
if parentIb != nil {
var err error
parentLineage, err = thisIb.ifdMapping.ResolvePath(parentIb.fqIfdPath)
log.PanicIf(err)
}
// Process the current path part.
currentItii := currentLineage[0]
// Make sure the leftmost part of the FQ IFD-path agrees with the IB we
// were given.
expectedFqRootIfdPath := ""
if parentLineage != nil {
expectedLineage := append(parentLineage, currentItii)
expectedFqRootIfdPath = thisIb.ifdMapping.PathPhraseFromLineage(expectedLineage)
} else {
expectedFqRootIfdPath = thisIb.ifdMapping.PathPhraseFromLineage(currentLineage[:1])
}
if expectedFqRootIfdPath != thisIb.fqIfdPath {
log.Panicf("the FQ IFD-path [%s] we were given does not match the builder's FQ IFD-path [%s]", expectedFqRootIfdPath, thisIb.fqIfdPath)
}
// If we actually wanted a sibling (currentItii.Index > 0) then seek to it,
// appending new siblings, as required, until we get there.
for i := 0; i < currentItii.Index; i++ {
if thisIb.nextIb == nil {
// Generate an FQ IFD-path for the sibling. It'll use the same
// non-FQ IFD-path as the current IB.
siblingFqIfdPath := ""
if parentLineage != nil {
siblingFqIfdPath = fmt.Sprintf("%s/%s%d", parentIb.fqIfdPath, currentItii.Name, i+1)
} else {
siblingFqIfdPath = fmt.Sprintf("%s%d", currentItii.Name, i+1)
}
thisIb.nextIb = NewIfdBuilder(thisIb.ifdMapping, thisIb.tagIndex, siblingFqIfdPath, thisIb.byteOrder)
}
thisIb = thisIb.nextIb
}
// There is no child IFD to process. We're done.
if len(currentLineage) == 1 {
return thisIb, nil
}
// Establish the next child to be processed.
childItii := currentLineage[1]
var foundChild *IfdBuilder
for _, bt := range thisIb.tags {
if bt.value.IsIb() == false {
continue
}
childIb := bt.value.Ib()
if childIb.ifdTagId == childItii.TagId {
foundChild = childIb
break
}
}
// If we didn't find the child, add it.
if foundChild == nil {
thisIbLineage, err := thisIb.ifdMapping.ResolvePath(thisIb.fqIfdPath)
log.PanicIf(err)
childLineage := make([]IfdTagIdAndIndex, len(thisIbLineage)+1)
copy(childLineage, thisIbLineage)
childLineage[len(childLineage)-1] = childItii
fqIfdChildPath := thisIb.ifdMapping.FqPathPhraseFromLineage(childLineage)
foundChild = NewIfdBuilder(thisIb.ifdMapping, thisIb.tagIndex, fqIfdChildPath, thisIb.byteOrder)
err = thisIb.AddChildIb(foundChild)
log.PanicIf(err)
}
finalIb, err := getOrCreateIbFromRootIbInner(foundChild, thisIb, currentLineage[1:])
log.PanicIf(err)
return finalIb, nil
}
// GetOrCreateIbFromRootIb returns an IB representing the requested IFD, even if
// an IB doesn't already exist for it. This function may call itself
// recursively.
func GetOrCreateIbFromRootIb(rootIb *IfdBuilder, fqIfdPath string) (ib *IfdBuilder, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
// lineage is a necessity of our recursion process. It doesn't include any
// parent IFDs on its left-side; it starts with the current IB only.
lineage, err := rootIb.ifdMapping.ResolvePath(fqIfdPath)
log.PanicIf(err)
ib, err = getOrCreateIbFromRootIbInner(rootIb, nil, lineage)
log.PanicIf(err)
return ib, nil
}
func (ib *IfdBuilder) String() string {
nextIfdPhrase := ""
if ib.nextIb != nil {
// TODO(dustin): We were setting this to ii.String(), but we were getting hex-data when printing this after building from an existing chain.
nextIfdPhrase = ib.nextIb.ifdPath
}
return fmt.Sprintf("IfdBuilder<PATH=[%s] TAG-ID=(0x%04x) COUNT=(%d) OFF=(0x%04x) NEXT-IFD-PATH=[%s]>", ib.ifdPath, ib.ifdTagId, len(ib.tags), ib.existingOffset, nextIfdPhrase)
}
func (ib *IfdBuilder) Tags() (tags []*BuilderTag) {
return ib.tags
}
// SetThumbnail sets thumbnail data.
//
// NOTES:
//
// - We don't manage any facet of the thumbnail data. This is the
// responsibility of the user/developer.
// - This method will fail unless the thumbnail is set on a the root IFD.
// However, in order to be valid, it must be set on the second one, linked to
// by the first, as per the EXIF/TIFF specification.
// - We set the offset to (0) now but will allocate the data and properly assign
// the offset when the IB is encoded (later).
func (ib *IfdBuilder) SetThumbnail(data []byte) (err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
if ib.ifdPath != IfdPathStandard {
log.Panicf("thumbnails can only go into a root Ifd (and only the second one)")
}
// TODO(dustin): !! Add a test for this function.
if data == nil || len(data) == 0 {
log.Panic("thumbnail is empty")
}
ib.thumbnailData = data
ibtvfb := NewIfdBuilderTagValueFromBytes(ib.thumbnailData)
offsetBt :=
NewBuilderTag(
ib.ifdPath,
ThumbnailOffsetTagId,
TypeLong,
ibtvfb,
ib.byteOrder)
err = ib.Set(offsetBt)
log.PanicIf(err)
thumbnailSizeIt, err := ib.tagIndex.Get(ib.ifdPath, ThumbnailSizeTagId)
log.PanicIf(err)
sizeBt := NewStandardBuilderTag(ib.ifdPath, thumbnailSizeIt, ib.byteOrder, []uint32{uint32(len(ib.thumbnailData))})
err = ib.Set(sizeBt)
log.PanicIf(err)
return nil
}
func (ib *IfdBuilder) Thumbnail() []byte {
return ib.thumbnailData
}
func (ib *IfdBuilder) printTagTree(levels int) {
indent := strings.Repeat(" ", levels*2)
i := 0
for currentIb := ib; currentIb != nil; currentIb = currentIb.nextIb {
prefix := " "
if i > 0 {
prefix = ">"
}
if levels == 0 {
fmt.Printf("%s%sIFD: %s INDEX=(%d)\n", indent, prefix, currentIb, i)
} else {
fmt.Printf("%s%sChild IFD: %s\n", indent, prefix, currentIb)
}
if len(currentIb.tags) > 0 {
fmt.Printf("\n")
for i, tag := range currentIb.tags {
isChildIb := false
_, err := ib.ifdMapping.GetChild(currentIb.ifdPath, tag.tagId)
if err == nil {
isChildIb = true
} else if log.Is(err, ErrChildIfdNotMapped) == false {
log.Panic(err)
}
tagName := ""
// If a normal tag (not a child IFD) get the name.
if isChildIb == true {
tagName = "<Child IFD>"
} else {
it, err := ib.tagIndex.Get(tag.ifdPath, tag.tagId)
if log.Is(err, ErrTagNotFound) == true {
tagName = "<UNKNOWN>"
} else if err != nil {
log.Panic(err)
} else {
tagName = it.Name
}
}
value := tag.Value()
if value.IsIb() == true {
fmt.Printf("%s (%d): [%s] %s\n", indent, i, tagName, value.Ib())
} else {
fmt.Printf("%s (%d): [%s] %s\n", indent, i, tagName, tag)
}
if isChildIb == true {
if tag.value.IsIb() == false {
log.Panicf("tag-ID (0x%04x) is an IFD but the tag value is not an IB instance: %v", tag.tagId, tag)
}
fmt.Printf("\n")
childIb := tag.value.Ib()
childIb.printTagTree(levels + 1)
}
}
fmt.Printf("\n")
}
i++
}
}
func (ib *IfdBuilder) PrintTagTree() {
ib.printTagTree(0)
}
func (ib *IfdBuilder) printIfdTree(levels int) {
indent := strings.Repeat(" ", levels*2)
i := 0
for currentIb := ib; currentIb != nil; currentIb = currentIb.nextIb {
prefix := " "
if i > 0 {
prefix = ">"
}
fmt.Printf("%s%s%s\n", indent, prefix, currentIb)
if len(currentIb.tags) > 0 {
for _, tag := range currentIb.tags {
isChildIb := false
_, err := ib.ifdMapping.GetChild(currentIb.ifdPath, tag.tagId)
if err == nil {
isChildIb = true
} else if log.Is(err, ErrChildIfdNotMapped) == false {
log.Panic(err)
}
if isChildIb == true {
if tag.value.IsIb() == false {
log.Panicf("tag-ID (0x%04x) is an IFD but the tag value is not an IB instance: %v", tag.tagId, tag)
}
childIb := tag.value.Ib()
childIb.printIfdTree(levels + 1)
}
}
}
i++
}
}
func (ib *IfdBuilder) PrintIfdTree() {
ib.printIfdTree(0)
}
func (ib *IfdBuilder) dumpToStrings(thisIb *IfdBuilder, prefix string, tagId uint16, lines []string) (linesOutput []string) {
if lines == nil {
linesOutput = make([]string, 0)
} else {
linesOutput = lines
}
siblingIfdIndex := 0
for ; thisIb != nil; thisIb = thisIb.nextIb {
line := fmt.Sprintf("IFD<PARENTS=[%s] FQ-IFD-PATH=[%s] IFD-INDEX=(%d) IFD-TAG-ID=(0x%04x) TAG=[0x%04x]>", prefix, thisIb.fqIfdPath, siblingIfdIndex, thisIb.ifdTagId, tagId)
linesOutput = append(linesOutput, line)
for i, tag := range thisIb.tags {
var childIb *IfdBuilder
childIfdName := ""
if tag.value.IsIb() == true {
childIb = tag.value.Ib()
childIfdName = childIb.ifdPath
}
line := fmt.Sprintf("TAG<PARENTS=[%s] FQ-IFD-PATH=[%s] IFD-TAG-ID=(0x%04x) CHILD-IFD=[%s] TAG-INDEX=(%d) TAG=[0x%04x]>", prefix, thisIb.fqIfdPath, thisIb.ifdTagId, childIfdName, i, tag.tagId)
linesOutput = append(linesOutput, line)
if childIb == nil {
continue
}
childPrefix := ""
if prefix == "" {
childPrefix = fmt.Sprintf("%s", thisIb.ifdPath)
} else {
childPrefix = fmt.Sprintf("%s->%s", prefix, thisIb.ifdPath)
}
linesOutput = thisIb.dumpToStrings(childIb, childPrefix, tag.tagId, linesOutput)
}
siblingIfdIndex++
}
return linesOutput
}
func (ib *IfdBuilder) DumpToStrings() (lines []string) {
return ib.dumpToStrings(ib, "", 0, lines)
}
func (ib *IfdBuilder) SetNextIb(nextIb *IfdBuilder) (err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
ib.nextIb = nextIb
return nil
}
func (ib *IfdBuilder) DeleteN(tagId uint16, n int) (err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
if n < 1 {
log.Panicf("N must be at least 1: (%d)", n)
}
for n > 0 {
j := -1
for i, bt := range ib.tags {
if bt.tagId == tagId {
j = i
break
}
}
if j == -1 {
log.Panic(ErrTagEntryNotFound)
}
ib.tags = append(ib.tags[:j], ib.tags[j+1:]...)
n--
}
return nil
}
func (ib *IfdBuilder) DeleteFirst(tagId uint16) (err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
err = ib.DeleteN(tagId, 1)
log.PanicIf(err)
return nil
}
func (ib *IfdBuilder) DeleteAll(tagId uint16) (n int, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
for {
err = ib.DeleteN(tagId, 1)
if log.Is(err, ErrTagEntryNotFound) == true {
break
} else if err != nil {
log.Panic(err)
}
n++
}
return n, nil
}
func (ib *IfdBuilder) ReplaceAt(position int, bt *BuilderTag) (err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
if position < 0 {
log.Panicf("replacement position must be 0 or greater")
} else if position >= len(ib.tags) {
log.Panicf("replacement position does not exist")
}
ib.tags[position] = bt
return nil
}
func (ib *IfdBuilder) Replace(tagId uint16, bt *BuilderTag) (err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
position, err := ib.Find(tagId)
log.PanicIf(err)
ib.tags[position] = bt
return nil
}
// Set will add a new entry or update an existing entry.
func (ib *IfdBuilder) Set(bt *BuilderTag) (err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
position, err := ib.Find(bt.tagId)
if err == nil {
ib.tags[position] = bt
} else if log.Is(err, ErrTagEntryNotFound) == true {
err = ib.add(bt)
log.PanicIf(err)
} else {
log.Panic(err)
}
return nil
}
func (ib *IfdBuilder) FindN(tagId uint16, maxFound int) (found []int, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
found = make([]int, 0)
for i, bt := range ib.tags {
if bt.tagId == tagId {
found = append(found, i)
if maxFound == 0 || len(found) >= maxFound {
break
}
}
}
return found, nil
}
func (ib *IfdBuilder) Find(tagId uint16) (position int, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
found, err := ib.FindN(tagId, 1)
log.PanicIf(err)
if len(found) == 0 {
log.Panic(ErrTagEntryNotFound)
}
return found[0], nil
}
func (ib *IfdBuilder) FindTag(tagId uint16) (bt *BuilderTag, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
found, err := ib.FindN(tagId, 1)
log.PanicIf(err)
if len(found) == 0 {
log.Panic(ErrTagEntryNotFound)
}
position := found[0]
return ib.tags[position], nil
}
func (ib *IfdBuilder) FindTagWithName(tagName string) (bt *BuilderTag, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
it, err := ib.tagIndex.GetWithName(ib.ifdPath, tagName)
log.PanicIf(err)
found, err := ib.FindN(it.Id, 1)
log.PanicIf(err)
if len(found) == 0 {
log.Panic(ErrTagEntryNotFound)
}
position := found[0]
return ib.tags[position], nil
}
func (ib *IfdBuilder) add(bt *BuilderTag) (err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
if bt.ifdPath == "" {
log.Panicf("BuilderTag ifdPath is not set: %s", bt)
} else if bt.typeId == 0x0 {
log.Panicf("BuilderTag type-ID is not set: %s", bt)
} else if bt.value == nil {
log.Panicf("BuilderTag value is not set: %s", bt)
}
ib.tags = append(ib.tags, bt)
return nil
}
func (ib *IfdBuilder) Add(bt *BuilderTag) (err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
if bt.value.IsIb() == true {
log.Panicf("child IfdBuilders must be added via AddChildIb() or AddTagsFromExisting(), not Add()")
}
err = ib.add(bt)
log.PanicIf(err)
return nil
}
// AddChildIb adds a tag that branches to a new IFD.
func (ib *IfdBuilder) AddChildIb(childIb *IfdBuilder) (err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
if childIb.ifdTagId == 0 {
log.Panicf("IFD can not be used as a child IFD (not associated with a tag-ID): %v", childIb)
} else if childIb.byteOrder != ib.byteOrder {
log.Panicf("Child IFD does not have the same byte-order: [%s] != [%s]", childIb.byteOrder, ib.byteOrder)
}
// Since no standard IFDs supports occuring more than once, check that a
// tag of this type has not been previously added. Note that we just search
// the current IFD and *not every* IFD.
for _, bt := range childIb.tags {
if bt.tagId == childIb.ifdTagId {
log.Panicf("child-IFD already added: %v", childIb.ifdPath)
}
}
bt := ib.NewBuilderTagFromBuilder(childIb)
ib.tags = append(ib.tags, bt)
return nil