From 080ff7d07d4f41511d04257366c41caee2a54105 Mon Sep 17 00:00:00 2001 From: Dustin Oprea Date: Mon, 28 May 2018 21:14:15 -0400 Subject: [PATCH] Adjusted some string representations. Fixed some hex-encodings. - unknown: We no longer truncate UNDEFINED UserComment values. - This is one less thing that will be different between what we read and how we encode. --- ifd_builder.go | 46 +++++++++++++++++++++++-------------------- ifd_builder_encode.go | 2 +- ifd_tag_entry.go | 4 ++-- tags_unknown.go | 31 ++++++++++++++--------------- 4 files changed, 43 insertions(+), 40 deletions(-) diff --git a/ifd_builder.go b/ifd_builder.go index 4e34bca..eb8adc5 100644 --- a/ifd_builder.go +++ b/ifd_builder.go @@ -29,6 +29,24 @@ type IfdBuilderTagValue struct { 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", valuePhrase, len(ibtv.valueBytes)) + } else if ibtv.IsIb() == true { + return fmt.Sprintf("IfdBuilderTagValue", ibtv.ib) + } else { + log.Panicf("IBTV state undefined") + return "" + } +} + func NewIfdBuilderTagValueFromBytes(valueBytes []byte) *IfdBuilderTagValue { return &IfdBuilderTagValue{ valueBytes: valueBytes, @@ -116,21 +134,7 @@ func (bt builderTag) Value() (value *IfdBuilderTagValue) { } func (bt builderTag) String() string { - valuePhrase := "" - - if bt.value.IsBytes() == true { - valueBytes := bt.value.Bytes() - - if len(valueBytes) <= 8 { - valuePhrase = fmt.Sprintf("%v", valueBytes) - } else { - valuePhrase = fmt.Sprintf("%v...", valueBytes[:8]) - } - } else { - valuePhrase = fmt.Sprintf("%v", bt.value.Ib()) - } - - return fmt.Sprintf("BuilderTag", bt.tagId, bt.ii, valuePhrase) + return fmt.Sprintf("BuilderTag", bt.tagId, bt.ii, bt.value) } // NewStandardBuilderTagFromConfig constructs a `builderTag` instance. The type @@ -330,7 +334,7 @@ func (ib *IfdBuilder) String() string { nextIfdPhrase = ib.nextIb.ii.IfdName } - return fmt.Sprintf("IfdBuilder", ib.ii.ParentIfdName, ib.ii.IfdName, ib.ifdTagId, len(ib.tags), ib.existingOffset, nextIfdPhrase) + return fmt.Sprintf("IfdBuilder", ib.ii.ParentIfdName, ib.ii.IfdName, ib.ifdTagId, len(ib.tags), ib.existingOffset, nextIfdPhrase) } func (ib *IfdBuilder) Tags() (tags []builderTag) { @@ -440,7 +444,7 @@ func (ib *IfdBuilder) printTagTree(levels int) { if isChildIb == true { if tag.value.IsIb() == false { - log.Panicf("tag-ID (0x%02x) is an IFD but the tag value is not an IB instance: %v", tag.tagId, tag) + 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") @@ -479,7 +483,7 @@ func (ib *IfdBuilder) printIfdTree(levels int) { if isChildIb == true { if tag.value.IsIb() == false { - log.Panicf("tag-ID (0x%02x) is an IFD but the tag value is not an IB instance: %v", tag.tagId, tag) + 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() @@ -509,7 +513,7 @@ func (ib *IfdBuilder) dumpToStrings(thisIb *IfdBuilder, prefix string, lines []s childIfdName = tag.value.Ib().ii.IfdName } - line := fmt.Sprintf(" IFD-TAG-ID=(0x%02x) CHILD-IFD=[%s] INDEX=(%d) TAG=[0x%02x]", prefix, thisIb.ii.IfdName, thisIb.ifdTagId, childIfdName, i, tag.tagId) + line := fmt.Sprintf(" IFD-TAG-ID=(0x%04x) CHILD-IFD=[%s] INDEX=(%d) TAG=[0x%04x]", prefix, thisIb.ii.IfdName, thisIb.ifdTagId, childIfdName, i, tag.tagId) linesOutput = append(linesOutput, line) if tag.value.IsIb() == true { @@ -883,10 +887,10 @@ func (ib *IfdBuilder) AddTagsFromExisting(ifd *Ifd, itevr *IfdTagEntryValueResol if childIfd == nil { childTagIds := make([]string, len(ifd.Children)) for j, childIfd := range ifd.Children { - childTagIds[j] = fmt.Sprintf("0x%02x (parent tag-position %d)", childIfd.TagId, childIfd.ParentTagIndex) + childTagIds[j] = fmt.Sprintf("0x%04x (parent tag-position %d)", childIfd.TagId, childIfd.ParentTagIndex) } - log.Panicf("could not find child IFD for child ITE: II=[%s] TAG-ID=(0x%02x) CURRENT-TAG-POSITION=(%d) CHILDREN=%v", ite.Ii, ite.TagId, i, childTagIds) + log.Panicf("could not find child IFD for child ITE: II=[%s] TAG-ID=(0x%04x) CURRENT-TAG-POSITION=(%d) CHILDREN=%v", ite.Ii, ite.TagId, i, childTagIds) } childIb := NewIfdBuilderFromExistingChain(childIfd, itevr) diff --git a/ifd_builder_encode.go b/ifd_builder_encode.go index 7872446..125120d 100644 --- a/ifd_builder_encode.go +++ b/ifd_builder_encode.go @@ -239,7 +239,7 @@ func (ibe *IfdByteEncoder) encodeTagToBytes(ib *IfdBuilder, bt *builderTag, bw * remainder := uint32(len_) % typeSize if remainder > 0 { - log.Panicf("tag (0x%02x) value of (%d) bytes not evenly divisible by type-size (%d)", bt.tagId, len_, typeSize) + log.Panicf("tag (0x%04x) value of (%d) bytes not evenly divisible by type-size (%d)", bt.tagId, len_, typeSize) } } diff --git a/ifd_tag_entry.go b/ifd_tag_entry.go index 26572df..b8e4896 100644 --- a/ifd_tag_entry.go +++ b/ifd_tag_entry.go @@ -29,7 +29,7 @@ type IfdTagEntry struct { } func (ite IfdTagEntry) String() string { - return fmt.Sprintf("IfdTagEntry", ite.ChildIfdName, ite.TagId, TypeNames[ite.TagType], ite.UnitCount) + return fmt.Sprintf("IfdTagEntry", ite.ChildIfdName, ite.TagId, TypeNames[ite.TagType], ite.UnitCount) } // ValueString renders a string from whatever the value in this tag is. @@ -98,7 +98,7 @@ func (ite IfdTagEntry) ValueBytes(addressableData []byte, byteOrder binary.ByteO return valueBytes, nil default: // TODO(dustin): !! Finish translating the rest of the types (make reusable and replace into other similar implementations?) - log.Panicf("can not produce bytes for unknown-type tag (0x%02x): [%s]", ite.TagId, reflect.TypeOf(value)) + log.Panicf("can not produce bytes for unknown-type tag (0x%04x): [%s]", ite.TagId, reflect.TypeOf(value)) } } diff --git a/tags_unknown.go b/tags_unknown.go index 6c27308..dc1fa25 100644 --- a/tags_unknown.go +++ b/tags_unknown.go @@ -93,7 +93,15 @@ type TagUnknownType_9298_UserComment struct { } func (uc TagUnknownType_9298_UserComment) String() string { - return fmt.Sprintf("UserComment", len(uc.EncodingBytes), TagUnknownType_9298_UserComment_Encoding_Names[uc.EncodingType], uc.EncodingBytes) + var valuePhrase string + + if len(uc.EncodingBytes) <= 8 { + valuePhrase = fmt.Sprintf("%v", uc.EncodingBytes) + } else { + valuePhrase = fmt.Sprintf("%v...", uc.EncodingBytes[:8]) + } + + return fmt.Sprintf("UserComment", len(uc.EncodingBytes), TagUnknownType_9298_UserComment_Encoding_Names[uc.EncodingType], valuePhrase, len(uc.EncodingBytes)) } func (uc TagUnknownType_9298_UserComment) ValueBytes() (value []byte, err error) { @@ -103,9 +111,8 @@ func (uc TagUnknownType_9298_UserComment) ValueBytes() (value []byte, err error) } value = make([]byte, len(uc.EncodingBytes) + 8) - copy(value[:8], encodingTypeBytes) -// TODO(dustin): !! With undefined-encoded comments, we always make this empty. However, it comes in with a set of zero bytes. Is there a problem if we send it out with just the encoding bytes? + copy(value[:8], encodingTypeBytes) copy(value[8:], uc.EncodingBytes) return value, nil @@ -245,20 +252,12 @@ func UndefinedValue(ii IfdIdentity, tagId uint16, valueContext ValueContext, byt encoding := valueBytes[:8] for encodingIndex, encodingBytes := range TagUnknownType_9298_UserComment_Encodings { if bytes.Compare(encoding, encodingBytes) == 0 { - // If unknown, return the default rather than what we have - // because there will be a big list of NULs (which aren't - // functional) and this won't equal the default instance - // (above). - if encodingIndex == TagUnknownType_9298_UserComment_Encoding_UNDEFINED { - return unknownUc, nil - } else { - uc := TagUnknownType_9298_UserComment{ - EncodingType: encodingIndex, - EncodingBytes: valueBytes[8:], - } - - return uc, nil + uc := TagUnknownType_9298_UserComment{ + EncodingType: encodingIndex, + EncodingBytes: valueBytes[8:], } + + return uc, nil } }