...

Source file src/github.com/dsoprea/go-exif/v3/undefined/exif_9286_user_comment.go

Documentation: github.com/dsoprea/go-exif/v3/undefined

     1  package exifundefined
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  
     7  	"encoding/binary"
     8  
     9  	"github.com/dsoprea/go-logging"
    10  
    11  	"github.com/dsoprea/go-exif/v3/common"
    12  )
    13  
    14  var (
    15  	exif9286Logger = log.NewLogger("exifundefined.exif_9286_user_comment")
    16  )
    17  
    18  const (
    19  	TagUndefinedType_9286_UserComment_Encoding_ASCII     = iota
    20  	TagUndefinedType_9286_UserComment_Encoding_JIS       = iota
    21  	TagUndefinedType_9286_UserComment_Encoding_UNICODE   = iota
    22  	TagUndefinedType_9286_UserComment_Encoding_UNDEFINED = iota
    23  )
    24  
    25  var (
    26  	TagUndefinedType_9286_UserComment_Encoding_Names = map[int]string{
    27  		TagUndefinedType_9286_UserComment_Encoding_ASCII:     "ASCII",
    28  		TagUndefinedType_9286_UserComment_Encoding_JIS:       "JIS",
    29  		TagUndefinedType_9286_UserComment_Encoding_UNICODE:   "UNICODE",
    30  		TagUndefinedType_9286_UserComment_Encoding_UNDEFINED: "UNDEFINED",
    31  	}
    32  
    33  	TagUndefinedType_9286_UserComment_Encodings = map[int][]byte{
    34  		TagUndefinedType_9286_UserComment_Encoding_ASCII:     {'A', 'S', 'C', 'I', 'I', 0, 0, 0},
    35  		TagUndefinedType_9286_UserComment_Encoding_JIS:       {'J', 'I', 'S', 0, 0, 0, 0, 0},
    36  		TagUndefinedType_9286_UserComment_Encoding_UNICODE:   {'U', 'n', 'i', 'c', 'o', 'd', 'e', 0},
    37  		TagUndefinedType_9286_UserComment_Encoding_UNDEFINED: {0, 0, 0, 0, 0, 0, 0, 0},
    38  	}
    39  )
    40  
    41  type Tag9286UserComment struct {
    42  	EncodingType  int
    43  	EncodingBytes []byte
    44  }
    45  
    46  func (Tag9286UserComment) EncoderName() string {
    47  	return "Codec9286UserComment"
    48  }
    49  
    50  func (uc Tag9286UserComment) String() string {
    51  	var valuePhrase string
    52  
    53  	if uc.EncodingType == TagUndefinedType_9286_UserComment_Encoding_ASCII {
    54  		return fmt.Sprintf("[ASCII] %s", string(uc.EncodingBytes))
    55  	} else {
    56  		if len(uc.EncodingBytes) <= 8 {
    57  			valuePhrase = fmt.Sprintf("%v", uc.EncodingBytes)
    58  		} else {
    59  			valuePhrase = fmt.Sprintf("%v...", uc.EncodingBytes[:8])
    60  		}
    61  	}
    62  
    63  	return fmt.Sprintf("UserComment<SIZE=(%d) ENCODING=[%s] V=%v LEN=(%d)>", len(uc.EncodingBytes), TagUndefinedType_9286_UserComment_Encoding_Names[uc.EncodingType], valuePhrase, len(uc.EncodingBytes))
    64  }
    65  
    66  type Codec9286UserComment struct {
    67  }
    68  
    69  func (Codec9286UserComment) Encode(value interface{}, byteOrder binary.ByteOrder) (encoded []byte, unitCount uint32, err error) {
    70  	defer func() {
    71  		if state := recover(); state != nil {
    72  			err = log.Wrap(state.(error))
    73  		}
    74  	}()
    75  
    76  	uc, ok := value.(Tag9286UserComment)
    77  	if ok == false {
    78  		log.Panicf("can only encode a Tag9286UserComment")
    79  	}
    80  
    81  	encodingTypeBytes, found := TagUndefinedType_9286_UserComment_Encodings[uc.EncodingType]
    82  	if found == false {
    83  		log.Panicf("encoding-type not valid for unknown-type tag 9286 (UserComment): (%d)", uc.EncodingType)
    84  	}
    85  
    86  	encoded = make([]byte, len(uc.EncodingBytes)+8)
    87  
    88  	copy(encoded[:8], encodingTypeBytes)
    89  	copy(encoded[8:], uc.EncodingBytes)
    90  
    91  	// TODO(dustin): Confirm this size against the specification.
    92  
    93  	return encoded, uint32(len(encoded)), nil
    94  }
    95  
    96  func (Codec9286UserComment) Decode(valueContext *exifcommon.ValueContext) (value EncodeableValue, err error) {
    97  	defer func() {
    98  		if state := recover(); state != nil {
    99  			err = log.Wrap(state.(error))
   100  		}
   101  	}()
   102  
   103  	valueContext.SetUndefinedValueType(exifcommon.TypeByte)
   104  
   105  	valueBytes, err := valueContext.ReadBytes()
   106  	log.PanicIf(err)
   107  
   108  	if len(valueBytes) < 8 {
   109  		return nil, ErrUnparseableValue
   110  	}
   111  
   112  	unknownUc := Tag9286UserComment{
   113  		EncodingType:  TagUndefinedType_9286_UserComment_Encoding_UNDEFINED,
   114  		EncodingBytes: []byte{},
   115  	}
   116  
   117  	encoding := valueBytes[:8]
   118  	for encodingIndex, encodingBytes := range TagUndefinedType_9286_UserComment_Encodings {
   119  		if bytes.Compare(encoding, encodingBytes) == 0 {
   120  			uc := Tag9286UserComment{
   121  				EncodingType:  encodingIndex,
   122  				EncodingBytes: valueBytes[8:],
   123  			}
   124  
   125  			return uc, nil
   126  		}
   127  	}
   128  
   129  	exif9286Logger.Warningf(nil, "User-comment encoding not valid. Returning 'unknown' type (the default).")
   130  	return unknownUc, nil
   131  }
   132  
   133  func init() {
   134  	registerEncoder(
   135  		Tag9286UserComment{},
   136  		Codec9286UserComment{})
   137  
   138  	registerDecoder(
   139  		exifcommon.IfdExifStandardIfdIdentity.UnindexedString(),
   140  		0x9286,
   141  		Codec9286UserComment{})
   142  }
   143  

View as plain text