-
Notifications
You must be signed in to change notification settings - Fork 1
/
tag.go
277 lines (221 loc) · 6.66 KB
/
tag.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
package iptc
import (
"errors"
"fmt"
"io"
"strings"
"unicode"
"encoding/binary"
"github.com/dsoprea/go-logging"
)
var (
// TODO(dustin): We're still not sure if this is the right endianness. No search to IPTC or IIM seems to state one or the other.
// DefaultEncoding is the standard encoding for the IPTC format.
defaultEncoding = binary.BigEndian
)
var (
// ErrInvalidTagMarker indicates that the tag can not be parsed because the
// tag boundary marker is not the expected value.
ErrInvalidTagMarker = errors.New("invalid tag marker")
)
// Tag describes one tag read from the stream.
type Tag struct {
recordNumber uint8
datasetNumber uint8
dataSize uint64
}
// String expresses state as a string.
func (tag *Tag) String() string {
return fmt.Sprintf(
"Tag<DATASET=(%d:%d) DATA-SIZE=(%d)>",
tag.recordNumber, tag.datasetNumber, tag.dataSize)
}
// DecodeTag parses one tag from the stream.
func DecodeTag(r io.Reader) (tag Tag, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
tagMarker := uint8(0)
err = binary.Read(r, defaultEncoding, &tagMarker)
if err != nil {
if err == io.EOF {
return tag, err
}
log.Panic(err)
}
if tagMarker != 0x1c {
return tag, ErrInvalidTagMarker
}
recordNumber := uint8(0)
err = binary.Read(r, defaultEncoding, &recordNumber)
log.PanicIf(err)
datasetNumber := uint8(0)
err = binary.Read(r, defaultEncoding, &datasetNumber)
log.PanicIf(err)
dataSize16Raw := uint16(0)
err = binary.Read(r, defaultEncoding, &dataSize16Raw)
log.PanicIf(err)
var dataSize uint64
if dataSize16Raw < 32768 {
// We only had 16-bits (has the MSB set to (0)).
dataSize = uint64(dataSize16Raw)
} else {
// This field is just the length of the length (has the MSB set to (1)).
// Clear the MSB.
lengthLength := dataSize16Raw & 32767
if lengthLength == 4 {
dataSize32Raw := uint32(0)
err := binary.Read(r, defaultEncoding, &dataSize32Raw)
log.PanicIf(err)
dataSize = uint64(dataSize32Raw)
} else if lengthLength == 8 {
err := binary.Read(r, defaultEncoding, &dataSize)
log.PanicIf(err)
} else {
// No specific sizes or limits are specified in the specification
// so we need to impose our own limits in order to implement.
log.Panicf("extended data-set tag size is not supported: (%d)", lengthLength)
}
}
tag = Tag{
recordNumber: recordNumber,
datasetNumber: datasetNumber,
dataSize: dataSize,
}
return tag, nil
}
// StreamTagKey is a convenience type that lets us key our index with a high-
// level type.
type StreamTagKey struct {
// RecordNumber is the major classification of the dataset.
RecordNumber uint8
// DatasetNumber is the minor classification of the dataset.
DatasetNumber uint8
}
// String returns a descriptive string.
func (stk StreamTagKey) String() string {
return fmt.Sprintf("%d:%d", stk.RecordNumber, stk.DatasetNumber)
}
// TagData is a convenience wrapper around a byte-slice.
type TagData []byte
// IsPrintable returns true if all characters are printable.
func (tg TagData) IsPrintable() bool {
for _, b := range tg {
r := rune(b)
// Newline characters aren't considered printable.
if r == 0x0d || r == 0x0a {
continue
}
if unicode.IsGraphic(r) == false || unicode.IsPrint(r) == false {
return false
}
}
return true
}
// String returns a descriptive string. If the data doesn't include any non-
// printable characters, it will include the value itself.
func (tg TagData) String() string {
if tg.IsPrintable() == true {
return string(tg)
}
return fmt.Sprintf("BINARY<(%d) bytes>", len(tg))
}
// ParsedTags is the complete, unordered set of tags parsed from the stream.
type ParsedTags map[StreamTagKey][]TagData
// ParseStream parses a serial sequence of tags and tag data out of the stream.
func ParseStream(r io.Reader) (tags map[StreamTagKey][]TagData, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
tags = make(ParsedTags)
for {
tag, err := DecodeTag(r)
if err != nil {
if err == io.EOF {
break
}
log.Panic(err)
}
raw := make([]byte, tag.dataSize)
_, err = io.ReadFull(r, raw)
log.PanicIf(err)
data := TagData(raw)
stk := StreamTagKey{
RecordNumber: tag.recordNumber,
DatasetNumber: tag.datasetNumber,
}
if existing, found := tags[stk]; found == true {
tags[stk] = append(existing, data)
} else {
tags[stk] = []TagData{data}
}
}
return tags, nil
}
// GetSimpleDictionaryFromParsedTags returns a dictionary of tag names to tag
// values, where all values are strings and any tag that had a non-printable
// value is omitted. We will also only return the first value, therefore
// dropping any follow-up values for repeatable tags. This will ignore non-
// standard tags. This will trim whitespace from the ends of strings.
//
// This is a convenience function for quickly displaying only the summary IPTC
// metadata that a user might actually be interested in at first glance.
func GetSimpleDictionaryFromParsedTags(pt ParsedTags) (distilled map[string]string) {
distilled = make(map[string]string)
for stk, dataSlice := range pt {
sti, err := GetTagInfo(int(stk.RecordNumber), int(stk.DatasetNumber))
if err != nil {
if err == ErrTagNotStandard {
continue
} else {
log.Panic(err)
}
}
data := dataSlice[0]
if data.IsPrintable() == false {
continue
}
// TODO(dustin): Trim leading whitespace, too.
distilled[sti.Description] = strings.Trim(string(data), "\r\n")
}
return distilled
}
// GetDictionaryFromParsedTags returns all tags. It will keep non-printable
// values, though will not print a placeholder instead. This will keep non-
// standard tags (and print the fully-qualified dataset ID rather than the
// name). It will keep repeated values (with the counter value appended to the
// end).
func GetDictionaryFromParsedTags(pt ParsedTags) (distilled map[string]string) {
distilled = make(map[string]string)
for stk, dataSlice := range pt {
var keyPhrase string
sti, err := GetTagInfo(int(stk.RecordNumber), int(stk.DatasetNumber))
if err != nil {
if err == ErrTagNotStandard {
keyPhrase = fmt.Sprintf("%s (not a standard tag)", stk.String())
} else {
log.Panic(err)
}
} else {
keyPhrase = sti.Description
}
for i, data := range dataSlice {
currentKeyPhrase := keyPhrase
if len(dataSlice) > 1 {
currentKeyPhrase = fmt.Sprintf("%s (%d)", currentKeyPhrase, i+1)
}
var presentable string
if data.IsPrintable() == false {
presentable = fmt.Sprintf("[BINARY] %s", DumpBytesToString(data))
} else {
presentable = string(data)
}
distilled[currentKeyPhrase] = presentable
}
}
return distilled
}