gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

accessor.go (1452B)


      1 package exifundefined
      2 
      3 import (
      4 	"encoding/binary"
      5 
      6 	"github.com/dsoprea/go-logging"
      7 
      8 	"github.com/dsoprea/go-exif/v3/common"
      9 )
     10 
     11 // Encode encodes the given encodeable undefined value to bytes.
     12 func Encode(value EncodeableValue, byteOrder binary.ByteOrder) (encoded []byte, unitCount uint32, err error) {
     13 	defer func() {
     14 		if state := recover(); state != nil {
     15 			err = log.Wrap(state.(error))
     16 		}
     17 	}()
     18 
     19 	encoderName := value.EncoderName()
     20 
     21 	encoder, found := encoders[encoderName]
     22 	if found == false {
     23 		log.Panicf("no encoder registered for type [%s]", encoderName)
     24 	}
     25 
     26 	encoded, unitCount, err = encoder.Encode(value, byteOrder)
     27 	log.PanicIf(err)
     28 
     29 	return encoded, unitCount, nil
     30 }
     31 
     32 // Decode constructs a value from raw encoded bytes
     33 func Decode(valueContext *exifcommon.ValueContext) (value EncodeableValue, err error) {
     34 	defer func() {
     35 		if state := recover(); state != nil {
     36 			err = log.Wrap(state.(error))
     37 		}
     38 	}()
     39 
     40 	uth := UndefinedTagHandle{
     41 		IfdPath: valueContext.IfdPath(),
     42 		TagId:   valueContext.TagId(),
     43 	}
     44 
     45 	decoder, found := decoders[uth]
     46 	if found == false {
     47 		// We have no choice but to return the error. We have no way of knowing how
     48 		// much data there is without already knowing what data-type this tag is.
     49 		return nil, exifcommon.ErrUnhandledUndefinedTypedTag
     50 	}
     51 
     52 	value, err = decoder.Decode(valueContext)
     53 	if err != nil {
     54 		if err == ErrUnparseableValue {
     55 			return nil, err
     56 		}
     57 
     58 		log.Panic(err)
     59 	}
     60 
     61 	return value, nil
     62 }