This repository has been archived by the owner on Dec 29, 2022. It is now read-only.
/
unmarshal.go
828 lines (779 loc) · 24 KB
/
unmarshal.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
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tiff
import (
"encoding/binary"
"fmt"
"log"
"math/big"
"reflect"
"strconv"
"strings"
"time"
)
type keyValPair struct {
Key string
Val string
}
/*
TIFF Struct Tag:
Representation(s):
`tiff:"type"`
`tiff:"type,data"`
Notes:
1. A tiff struct tag represents the general format of a tiff struct tag.
2. The first part is always a simple alpha-numeric only type reference.
3. Current supported types are:
ifd
subifd
field
4. The type is separated by the data portion by a single ',' character.
5. The data portion is just a string that usually contains some structure
referenced by the type.
*/
type tiffStructTag struct {
Type string
Data string
}
func (tst tiffStructTag) String() string {
if tst.Type == "" {
return ""
}
if tst.Data == "" {
return fmt.Sprintf(`tiff:"%s"`, tst.Type)
}
return fmt.Sprintf(`tiff:"%s,%s"`, tst.Type, tst.Data)
}
func ParseTiffStructTag(text string) *tiffStructTag {
if len(text) == 0 {
return nil
}
pair := strings.SplitN(text, ",", 2)
switch len(pair) {
case 1:
return &tiffStructTag{
Type: pair[0],
}
case 2:
return &tiffStructTag{
Type: pair[0],
Data: pair[1],
}
}
return nil
}
/*
IFD Field Struct Tag:
Representation(s):
`tiff:"field,tag=%d,typ=%d,cnt=%d,off=(true|false),def=[%v,%v,%v]"`
Notes:
1. A tiff field struct tag starts with "field" followed by a ',' and then
one or more key value pairs in the form key=value.
2. The key value pairs are separated by commas.
3. There are five key types.
3.1. tag: Represents the IFD Field's numeric Tag ID value.
3.2. typ: Represents the IFD Field's numeric Field Type value.
3.3. cnt: Represents the IFD Field's numeric Count value.
3.4. off: Indicates if the value portion of the underlying entry is
actually an offset to the bytes found in a Field's value.
3.5. def: Represents the value(s) that will be used in the event that an
ifd does not contain a Field that associates with this struct
field.
4. Notes about the key "tag".
4.1. This is the only REQUIRED key.
4.2. The value of the "tag" key MUST be in base10 and MUST fit into a
uint16.
5. Notes about the key "typ".
5.1. This is an OPTIONAL key UNLESS a "def" key exists.
5.2. The value of the "typ" key MUST be in base10 and MUST fit into a
uint16.
6. Notes about the key "cnt".
6.1. This is an OPTIONAL key UNLESS a "def" key exists with multiple
values and the struct field's type is a slice. If the struct field
type is an array, the expected count is taken from the array's
length.
6.2. The value of the "cnt" key MUST be in base10 and MUST fit into a
uint64.
7. Notes about the key "off".
7.1. This is an OPTIONAL key.
7.2. The value of the "off" key MUST be able to be parsed by
strconv.ParseBool (found at
http://golang.org/pkg/strconv/#ParseBool).
8. Notes about the key "def".
8.1. This is an OPTIONAL key.
8.2. A "typ" key is REQUIRED if a "def" key is present. A "cnt" key is
REQUIRED if the struct field's type is a slice.
8.3. The value of "def" is a string representation of a default value
that a tag may have (often indicated in documentation).
8.4. There are a few rules for the structure of the "def" key's value.
If the rules for the are not followed, parsing will silently fail.
9. Rules for the format of the value of the "def" key.
9.1. The key name for the default field is "def" (without quotes).
9.2. A def key SHOULD be placed at the end of the text sequence, but MAY
exist anywhere past the starting "field,".
9.3. All values for def MUST start with [ and end with ]. The
contents in between will be used. A tiff field struct tag has no
other uses for [].
9.4. There are 4 representations supported for parsing of default values.
9.4.1. Byte, Undefined, and Integers: MUST be in base10 (decimal).
1, -23, 456, 7890
9.4.2. Rationals: MUST have the form x/y where x and y are both
base10.
1/2, -3/4, 5/-6, -7/-8
9.4.3. Floats & Doubles: The form "-1234.5678" (without quotes)
9.4.4. ASCII MUST follow within the bounds of a go string literal.
Specifically,
http://golang.org/ref/spec#interpreted_string_lit is what
MUST be used. However, for the case of a literal back quote,
it SHOULD be represented in its hexadecimal form within the
string. The ` character SHOULD be written as \x60 since the
struct tags themselves are raw strings that use ``. The
octal form \140 MAY also be used.
` == \x60 // Hexadecimal form of Back Quote/Back Tick
` == \140 // Octal form of Back Quote/Back Tick
Example:
Given the following struct tag...
`tiff:"field,tag=1,def=[Some quotes \" and ' are easy, but \x60 is more involved.]"`
Running reflect's StructTag.Get("tiff") will return...
"field,tag=1,def=[Some quotes \" and ' are easy, but ` is more involved.]"
And the default value will end up being...
"Some quotes \" and ' are easy, but ` is more involved."
When printed to stdout with fmt.Println...
Some quotes " and ' are easy, but ` is more involved.
If def=[] is present for ascii, an empty string is
assumed. If you do not wish for the string to be set (i.e.
you are using a *string and want it to remain nil), then do
not specify default in the struct tag. For all other types,
using def=[] will not set the zero value.
9.5. For counts > 1 for all types other than ASCII, use a ',' to separate
values.
byte, undefined, integers: def=[1,78,255,0,42]
rationals: def=[1/2,-3/4,5/-6,-7/-8]
floats & doubles: def=[-1.234,56.78,9.0]
9.6. If the struct field is not an array or slice, but you specify
multiple values as though count > 1, only the first value will be
used.
*/
// A fieldStructTag is a data representation of a struct tag used for a struct
// field that directly relates to a tiff ifd field.
type fieldStructTag struct {
Tag *uint16 // key name "tag"
Type *uint16 // key name "typ"
Count *uint64 // key name "cnt"
Offset *bool // key name "off"
Default *string // key name "def"
}
func (fst fieldStructTag) String() string {
var pairs []string
if fst.Tag != nil {
pairs = append(pairs, fmt.Sprintf("tag=%d", *fst.Tag))
}
if fst.Type != nil {
pairs = append(pairs, fmt.Sprintf("typ=%d", *fst.Type))
}
if fst.Count != nil {
pairs = append(pairs, fmt.Sprintf("cnt=%d", *fst.Count))
}
if fst.Offset != nil {
pairs = append(pairs, fmt.Sprintf("off=%v", *fst.Offset))
}
if fst.Default != nil {
quoted := strconv.QuoteToASCII(*fst.Default)
quoted = quoted[1 : len(quoted)-1] // removes the quotes, but keeps the representation
pairs = append(pairs, fmt.Sprintf("def=[%s]", quoted))
}
if len(pairs) > 0 {
return "field," + strings.Join(pairs, ",")
}
return ""
}
func ParseTiffFieldStructTag(text string) (out *fieldStructTag) {
if len(text) < 5 {
return nil
}
pairs := make([]keyValPair, 0, 5)
nextPair := new(keyValPair)
var key *string
var val *string
for i := 0; i < len(text); {
switch {
case key == nil:
if len(text[i:]) < 5 {
return
}
if text[i+3] != '=' {
return
}
nextKey := text[i : i+3]
i += 4
key = &nextKey
case val == nil:
switch *key {
case "def":
if text[i] != '[' {
return
}
to := strings.LastIndex(text, "]")
if to == -1 {
return
}
valText := text[i+1 : to] // Remove the [ ]
val = &valText
i = to + 1
if i < len(text) {
if text[i] == ',' {
i += 1 // pass the comma
}
}
case "tag", "typ", "cnt", "off":
j := strings.Index(text[i:], ",")
if j == -1 { // Assume we must be at the end
valText := text[i:]
val = &valText
i = len(text)
} else {
valText := text[i : i+j]
val = &valText
i += j + 1 // move i to 1 past the ,
}
default:
// Invalid key name
return
}
}
if key != nil && val != nil {
nextPair.Key = *key
nextPair.Val = *val
pairs = append(pairs, *nextPair)
nextPair = new(keyValPair)
key = nil
val = nil
}
}
if len(pairs) == 0 {
return nil
}
var fst fieldStructTag
var hasValue bool
for _, pair := range pairs {
switch pair.Key {
case "tag":
tagInt, err := strconv.ParseUint(pair.Val, 10, 16)
if err != nil {
log.Printf("tiff: structtag key/val conversion failure for key \"tag\": %v\n", err)
continue
}
tagID := uint16(tagInt)
fst.Tag = &tagID
hasValue = true
case "typ":
tInt, err := strconv.ParseUint(pair.Val, 10, 16)
if err != nil {
log.Printf("tiff: structtag key/val conversion failure for key \"typ\": %v\n", err)
continue
}
t := uint16(tInt)
fst.Type = &t
hasValue = true
case "cnt":
cInt, err := strconv.ParseUint(pair.Val, 10, 64)
if err != nil {
log.Printf("tiff: structtag key/val conversion failure for key \"cnt\": %v\n", err)
continue
}
fst.Count = &cInt
hasValue = true
case "off":
o, err := strconv.ParseBool(pair.Val)
if err != nil {
log.Printf("tiff: structtag key/val conversion failure for key \"off\": %v\n", err)
continue
}
fst.Offset = &o
hasValue = true
case "def":
// Make a copy or we risk the value being changed with
// each iteration since pair is reused with the same
// memory location each time through the loop.
defVal := pair.Val
fst.Default = &defVal
hasValue = true
}
}
if !hasValue {
return
}
return &fst
}
/*
IFD Struct Tag:
Representation(s):
`tiff:"ifd"`
`tiff:"ifd,idx=%d"`
Notes:
1. A tiff ifd struct tag starts with "ifd" followed by a ',' and then
zero or one key value pair in the form key=value.
2. There is only one key type.
2.1. idx: Represents the index location of the IFD from TIFF.IFDs().
3. Notes about the key "idx".
3.1. This is an OPTIONAL key.
3.2. The value of the "idx" key MUST be in base10 and and MUST fit into
an int.
3.3. Negative values are ignored. Only values >= 0 are used.
3.4. The absence of the idx key assumes a value of 0 when performing tiff
unmarshaling.
3.5. When performing IFD unmarshaling, if an ifd is being unmarshaled
into a struct that contains either a field or an embedding that is a
struct or pointer to a struct and that field has a tiff ifd struct
tag, the ifd being unmarshaled is used again for the sub struct.
Any idx key and its value will be ignored.
4. The purpose of allowing struct fields nested inside structs (that already
represent ifds) to use tiff ifd struct tags, is for the case where
someone chooses to break up their IFD representation into separate types
each with different fields from the same IFD. For example, one may
choose to have one struct to hold storage based values, another struct
for meta data fields, and another for image representation. This has the
side effect of not preventing the same fields to be copied multiple
times. We do not prevent programmers from doing bad things with this
(i.e. multiple layers of nesting with the same fields), but we hope
anyone working in such a manner knows what they are doing.
*/
type ifdStructTag struct {
Index *int
}
func ParseTiffIFDStructTag(text string) *ifdStructTag {
if len(text) < 5 {
return nil
}
if !strings.HasPrefix(text, "idx=") {
return nil
}
idxValText := text[4:]
idx64, err := strconv.ParseInt(idxValText, 10, 64)
if err != nil {
log.Printf("tiff: structtag key/val conversion failure for key \"idx\": %v\n", err)
return nil
}
idx := int(idx64)
return &ifdStructTag{&idx}
}
/*
Sub-IFD Struct Tag:
Representation:
`tiff:"subifd,tag=330,idx=0"`
`tiff:"subifd,tag=330,idx=1"`
`tiff:"subifd,tag=330,idx=2"`
`tiff:"subifd,tag=34665,tsp=%s"`
`tiff:"subifd,tag=34853,tsp=%s"`
Notes:
...
*/
type subIFDStructTag struct {
Tag *uint16
Index *int
TagSpace *string
}
func ParseTiffSubIFDStructTag(text string) *subIFDStructTag {
pairs := strings.Split(text, ",")
if len(pairs[0]) < 5 {
return nil
}
if pairs[0][:4] != "tag=" {
return nil
}
tagValText := pairs[0][4:]
tag64, err := strconv.ParseUint(tagValText, 10, 16)
if err != nil {
log.Printf("tiff: structtag key/val conversion failure for key \"tag\": %v\n", err)
return nil
}
tagNum := uint16(tag64)
var sist subIFDStructTag
sist.Tag = &tagNum
if len(pairs) > 1 {
for _, p := range pairs[1:] {
if len(p) < 5 {
return nil
}
switch p[:4] {
case "idx=":
idxValText := pairs[1][4:]
idx64, err := strconv.ParseInt(idxValText, 10, 64)
if err != nil {
log.Printf("tiff: structtag key/val conversion failure for key \"idx\": %v\n", err)
return nil
}
idx := int(idx64)
sist.Index = &idx
case "tsp=":
tspText := string(pairs[1][4:])
sist.TagSpace = &tspText
}
}
}
return &sist
}
var bigRatType = reflect.TypeOf((*big.Rat)(nil))
var timeType = reflect.TypeOf(time.Time{})
type ErrUnsuppConversion struct {
From FieldType
To reflect.Type
}
func (e ErrUnsuppConversion) Error() string {
return fmt.Sprintf("tiff: unmarshal: no support for converting field type %q (id: %d) to %q", e.From.Name(), e.From.ID(), e.To)
}
type ErrUnsuppStructField struct {
T reflect.Type
Field int
Problem string
}
func (e ErrUnsuppStructField) Error() string {
var msg string
if e.Problem != "" {
msg = ": " + e.Problem
}
return fmt.Sprintf("tiff: unmarshal: error for field %s of type %s%s", e.T.Field(e.Field).Name, e.T.Name(), msg)
}
func unmarshalVal(data []byte, bo binary.ByteOrder, ft FieldType, v reflect.Value) error {
// Do the quick and simple thing.
if v.Type() == ft.ReflectType() {
v.Set(ft.Valuer()(data, bo))
return nil
}
typ := v.Type()
switch typ.Kind() {
case reflect.Ptr:
switch typ {
case bigRatType:
// If the types were the same, it should have been caught above.
return ErrUnsuppConversion{ft, typ}
default:
newV := reflect.New(typ.Elem())
if err := unmarshalVal(data, bo, ft, newV.Elem()); err != nil {
return err
}
v.Set(newV)
}
case reflect.String:
var s string
switch ft.ReflectType().Kind() {
case reflect.Uint8:
s = string(data)
default:
return ErrUnsuppConversion{ft, typ}
}
v.SetString(s)
case reflect.Uint16:
// We can up convert an uint8/byte to a uint16.
if ft.ReflectType().Kind() == reflect.Uint8 {
v.SetUint(uint64(data[0]))
} else {
return ErrUnsuppConversion{ft, typ}
}
case reflect.Int16:
// We can up convert an int8 to an int16.
if ft.ReflectType().Kind() == reflect.Int8 {
v.SetInt(int64(int8(data[0])))
} else {
return ErrUnsuppConversion{ft, typ}
}
case reflect.Uint32:
var u32 uint32
switch ft.ReflectType().Kind() {
case reflect.Uint16:
// We can up convert an uint16 to an uint32.
u32 = uint32(bo.Uint16(data))
case reflect.Uint8:
// We can up convert an uint8 to an uint32.
u32 = uint32(data[0])
default:
return ErrUnsuppConversion{ft, typ}
}
v.SetUint(uint64(u32))
case reflect.Int32:
var i32 int32
switch ft.ReflectType().Kind() {
case reflect.Int16:
// We can up convert an int16 to an int32.
i32 = int32(int16(bo.Uint16(data)))
case reflect.Int8:
// We can up convert an int8 to an int32.
i32 = int32(int8(data[0]))
default:
return ErrUnsuppConversion{ft, typ}
}
v.SetInt(int64(i32))
case reflect.Uint64:
var u64 uint64
switch ft.ReflectType().Kind() {
case reflect.Uint32:
u64 = uint64(bo.Uint32(data))
case reflect.Uint16:
u64 = uint64(bo.Uint16(data))
case reflect.Uint8:
u64 = uint64(data[0])
default:
return ErrUnsuppConversion{ft, typ}
}
v.SetUint(u64)
case reflect.Int64:
var i64 int64
switch ft.ReflectType().Kind() {
case reflect.Int32:
i64 = int64(int32(bo.Uint32(data)))
case reflect.Int16:
i64 = int64(int16(bo.Uint16(data)))
case reflect.Int8:
i64 = int64(int8(data[0]))
default:
return ErrUnsuppConversion{ft, typ}
}
v.SetInt(i64)
case reflect.Uint8, reflect.Int8, reflect.Float32, reflect.Float64:
// If this was not handled at the top, we do not support
// converting other types to these types.
return ErrUnsuppConversion{ft, typ}
}
return nil
}
func UnmarshalIFD(ifd IFD, out interface{}) error {
if len(ifd.Fields()) == 0 {
return fmt.Errorf("tiff: UnmarshalIFD: ifd has no fields")
}
v := reflect.ValueOf(out).Elem()
structType := v.Type()
for i := 0; i < v.NumField(); i++ {
stField := structType.Field(i)
sTag := ParseTiffStructTag(stField.Tag.Get("tiff"))
if sTag == nil {
continue
}
if sTag.Type != "ifd" && sTag.Type != "field" {
continue
}
vf := v.Field(i)
vft := vf.Type()
vftk := vft.Kind()
switch sTag.Type {
case "ifd":
// Ignore the data contents of the ifd tag, it is used
// when parsing whole tiffs (i.e. where the index value
// is useful).
switch vftk {
case reflect.Ptr:
// We do not support recursive unmarshaling when
// the field points back to the enclosing struct.
if vf.Elem() != v && vft.Elem().Kind() == reflect.Struct {
newStruct := reflect.New(vft.Elem())
if err := UnmarshalIFD(ifd, newStruct.Interface()); err != nil {
return err
}
vf.Set(newStruct)
}
case reflect.Struct:
embStructPtr := v.Field(i).Addr().Interface()
if err := UnmarshalIFD(ifd, embStructPtr); err != nil {
return err
}
default:
log.Printf("tiff: UnmarshalIFD: using a tiff ifd struct tag is only supported for structs (not a %v)\n", vftk)
continue
}
case "field":
fTag := ParseTiffFieldStructTag(sTag.Data)
if fTag == nil {
log.Printf("tiff: UnmarshalIFD: skipping struct field %q due to malformed tiff field struct tag (%q).\n", stField.Name, sTag.Data)
continue
}
if fTag.Tag == nil {
log.Printf("tiff: UnmarshalIFD: skipping struct field %q due to missing \"tag\" key in tiff field struct tag.\n", stField.Name)
continue
}
if !ifd.HasField(*fTag.Tag) {
// TODO(jonathanpittman): Check for default values and use those.
if fTag.Default != nil {
if fTag.Type == nil {
log.Printf("tiff: UnmarshalIFD: skipping default unmarshaling for struct field %q due to missing \"typ\" key.\n", stField.Name)
continue
}
//defVal := *fTag.Default
//ft := DefaultFieldTypeSpace.GetFieldType(*fTag.Type)
}
} else {
// ifd field setup
ifdField := ifd.GetField(*fTag.Tag)
ifdFT := ifdField.Type()
fvBytes := ifdField.Value().Bytes()
fvBo := ifdField.Value().Order()
switch vftk {
case reflect.Array:
l := vft.Len()
buf := fvBytes[:]
for j := 0; j < l; j++ {
data := buf[:ifdFT.Size()]
if err := unmarshalVal(data, fvBo, ifdFT, vf.Index(j)); err != nil {
return err
}
buf = buf[ifdFT.Size():]
}
case reflect.Slice:
newSlice := reflect.MakeSlice(vft, int(ifdField.Count()), int(ifdField.Count()))
l := newSlice.Len()
buf := fvBytes[:]
for j := 0; j < l; j++ {
data := buf[:ifdFT.Size()]
if err := unmarshalVal(data, fvBo, ifdFT, newSlice.Index(j)); err != nil {
return err
}
buf = buf[ifdFT.Size():]
}
vf.Set(newSlice)
default:
if err := unmarshalVal(fvBytes, fvBo, ifdFT, vf); err != nil {
return err
}
}
}
}
}
return nil
}
func UnmarshalSubIFDs(ifd IFD, br BReader, tsp TagSpace, out interface{}) error {
if br == nil {
return fmt.Errorf("tiff: UnmarshalSubIFDs: no BReader available")
}
if len(ifd.Fields()) == 0 {
return fmt.Errorf("tiff: UnmarshalSubIFDs: ifd has no fields")
}
v := reflect.ValueOf(out).Elem()
structType := v.Type()
for i := 0; i < v.NumField(); i++ {
stField := structType.Field(i)
sTag := ParseTiffStructTag(stField.Tag.Get("tiff"))
if sTag == nil || sTag.Type != "subifd" {
continue
}
vf := v.Field(i)
vft := vf.Type()
vftk := vft.Kind()
siTag := ParseTiffSubIFDStructTag(sTag.Data)
if siTag == nil {
log.Printf("tiff: UnmarshalSubIFDs: skipping struct field %q due to malformed tiff subifd struct tag (%q).\n", stField.Name, sTag.Data)
continue
}
if siTag.Tag == nil {
log.Printf("tiff: UnmarshalSubIFDs: skipping struct field %q due to missing \"tag\" key in tiff subifd struct tag.\n", stField.Name)
continue
}
if !ifd.HasField(*siTag.Tag) {
// Default values do not work here. We have to simply skip it.
continue
}
ifdField := ifd.GetField(*siTag.Tag)
ifdFT := ifdField.Type()
fvBytes := ifdField.Value().Bytes()
fvBo := ifdField.Value().Order()
if ifdField.Count() == 0 {
continue
}
if ifdFT.ReflectType().Kind() != reflect.Uint32 {
continue
}
offsets := make([]uint64, ifdField.Count())
for i := range offsets {
offsets[i] = uint64(fvBo.Uint32(fvBytes))
fvBytes = fvBytes[4:]
}
off := offsets[0]
if siTag.Index != nil {
if *siTag.Index >= len(offsets) || *siTag.Index < 0 {
// log a warning
continue
}
off = offsets[*siTag.Index]
}
if tsp == nil {
if siTag.TagSpace != nil {
newSpace := GetTagSpace(*siTag.TagSpace)
if newSpace != nil {
tsp = newSpace
}
}
if tsp == nil {
tsp = DefaultTagSpace
}
}
subIFD, err := ParseIFD(br, uint64(off), tsp, nil)
if err != nil {
return err
}
switch vftk {
case reflect.Ptr:
// We do not support recursive unmarshaling when
// the field points back to the enclosing struct.
if vf.Elem() != v && vft.Elem().Kind() == reflect.Struct {
newStruct := reflect.New(vft.Elem())
if err := UnmarshalIFD(subIFD, newStruct.Interface()); err != nil {
return err
}
vf.Set(newStruct)
}
case reflect.Struct:
embStructPtr := v.Field(i).Addr().Interface()
if err := UnmarshalIFD(subIFD, embStructPtr); err != nil {
return err
}
default:
log.Printf("tiff: UnmarshalSubIFDs: using a tiff SubIFD struct tag is only supported for structs (not a %v)\n", vftk)
continue
}
}
return nil
}
func UnmarshalTIFF(t TIFF, out interface{}) error {
if t == nil {
return fmt.Errorf("tiff: UnmarshalTIFF: nil TIFF value")
}
if len(t.IFDs()) == 0 {
return fmt.Errorf("tiff: UnmarshalTIFF: no IFDs found")
}
v := reflect.ValueOf(out).Elem()
structType := v.Type()
for i := 0; i < v.NumField(); i++ {
stField := structType.Field(i)
sTag := ParseTiffStructTag(stField.Tag.Get("tiff"))
if sTag == nil || sTag.Type != "ifd" {
continue
}
ifdIdx := 0 // Default of 0, unless an index key is present.
iTag := ParseTiffIFDStructTag(sTag.Data)
if iTag != nil && iTag.Index != nil && *iTag.Index > 0 {
ifdIdx = *iTag.Index
}
if ifdIdx >= len(t.IFDs()) {
log.Printf("tiff: UnmarshalTIFF: ifd struct tag index out of range for this tiff: %d > %d\n", ifdIdx, len(t.IFDs()))
continue
}
ifd := t.IFDs()[ifdIdx]
vf := v.Field(i)
vft := vf.Type()
vftk := vft.Kind()
switch vftk {
case reflect.Ptr:
if vf.Elem() != v && vft.Elem().Kind() == reflect.Struct {
newStruct := reflect.New(vft.Elem())
if err := UnmarshalIFD(ifd, newStruct.Interface()); err != nil {
return err
}
vf.Set(newStruct)
}
case reflect.Struct:
embStructPtr := v.Field(i).Addr().Interface()
if err := UnmarshalIFD(ifd, embStructPtr); err != nil {
return err
}
}
}
return nil
}