[feature] support processing of (many) more media types (#3090)

* initial work replacing our media decoding / encoding pipeline with ffprobe + ffmpeg

* specify the video codec to use when generating static image from emoji

* update go-storage library (fixes incompatibility after updating go-iotools)

* maintain image aspect ratio when generating a thumbnail for it

* update readme to show go-ffmpreg

* fix a bunch of media tests, move filesize checking to callers of media manager for more flexibility

* remove extra debug from error message

* fix up incorrect function signatures

* update PutFile to just use regular file copy, as changes are file is on separate partition

* fix remaining tests, remove some unneeded tests now we're working with ffmpeg/ffprobe

* update more tests, add more code comments

* add utilities to generate processed emoji / media outputs

* fix remaining tests

* add test for opus media file, add license header to utility cmds

* limit the number of concurrently available ffmpeg / ffprobe instances

* reduce number of instances

* further reduce number of instances

* fix envparsing test with configuration variables

* update docs and configuration with new media-{local,remote}-max-size variables
This commit is contained in:
kim
2024-07-12 09:39:47 +00:00
committed by GitHub
parent 5bc567196b
commit cde2fb6244
376 changed files with 8026 additions and 54091 deletions

View File

@ -1 +0,0 @@
vendor

View File

@ -1,21 +0,0 @@
MIT License
Copyright (c) 2020 AbemaTV
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -1,159 +0,0 @@
go-mp4
------
[![Go Reference](https://pkg.go.dev/badge/github.com/abema/go-mp4.svg)](https://pkg.go.dev/github.com/abema/go-mp4)
![Test](https://github.com/abema/go-mp4/actions/workflows/test.yml/badge.svg)
[![Coverage Status](https://coveralls.io/repos/github/abema/go-mp4/badge.svg)](https://coveralls.io/github/abema/go-mp4)
[![Go Report Card](https://goreportcard.com/badge/github.com/abema/go-mp4)](https://goreportcard.com/report/github.com/abema/go-mp4)
go-mp4 is Go library which provides low-level I/O interfaces of MP4.
This library supports you to parse or build any MP4 boxes(atoms) directly.
go-mp4 provides very flexible interfaces for reading boxes.
If you want to read only specific parts of MP4 file, this library extracts those boxes via io.ReadSeeker interface.
On the other hand, this library is not suitable for complex data conversions.
## Integration with your Go application
### Reading
You can parse MP4 file as follows:
```go
// expand all boxes
_, err := mp4.ReadBoxStructure(file, func(h *mp4.ReadHandle) (interface{}, error) {
fmt.Println("depth", len(h.Path))
// Box Type (e.g. "mdhd", "tfdt", "mdat")
fmt.Println("type", h.BoxInfo.Type.String())
// Box Size
fmt.Println("size", h.BoxInfo.Size)
if h.BoxInfo.IsSupportedType() {
// Payload
box, _, err := h.ReadPayload()
if err != nil {
return nil, err
}
str, err := mp4.Stringify(box, h.BoxInfo.Context)
if err != nil {
return nil, err
}
fmt.Println("payload", str)
// Expands children
return h.Expand()
}
return nil, nil
})
```
```go
// extract specific boxes
boxes, err := mp4.ExtractBoxWithPayload(file, nil, mp4.BoxPath{mp4.BoxTypeMoov(), mp4.BoxTypeTrak(), mp4.BoxTypeTkhd()})
if err != nil {
:
}
for _, box := range boxes {
tkhd := box.Payload.(*mp4.Tkhd)
fmt.Println("track ID:", tkhd.TrackID)
}
```
```go
// get basic informations
info, err := mp4.Probe(bufseekio.NewReadSeeker(file, 1024, 4))
if err != nil {
:
}
fmt.Println("track num:", len(info.Tracks))
```
### Writing
Writer helps you to write box tree.
The following sample code edits emsg box and writes to another file.
```go
r := bufseekio.NewReadSeeker(inputFile, 128*1024, 4)
w := mp4.NewWriter(outputFile)
_, err = mp4.ReadBoxStructure(r, func(h *mp4.ReadHandle) (interface{}, error) {
switch h.BoxInfo.Type {
case mp4.BoxTypeEmsg():
// write box size and box type
_, err := w.StartBox(&h.BoxInfo)
if err != nil {
return nil, err
}
// read payload
box, _, err := h.ReadPayload()
if err != nil {
return nil, err
}
// update MessageData
emsg := box.(*mp4.Emsg)
emsg.MessageData = []byte("hello world")
// write box playload
if _, err := mp4.Marshal(w, emsg, h.BoxInfo.Context); err != nil {
return nil, err
}
// rewrite box size
_, err = w.EndBox()
return nil, err
default:
// copy all
return nil, w.CopyBox(r, &h.BoxInfo)
}
})
```
### User-defined Boxes
You can create additional box definition as follows:
```go
func BoxTypeXxxx() BoxType { return mp4.StrToBoxType("xxxx") }
func init() {
mp4.AddBoxDef(&Xxxx{}, 0)
}
type Xxxx struct {
FullBox `mp4:"0,extend"`
UI32 uint32 `mp4:"1,size=32"`
ByteArray []byte `mp4:"2,size=8,len=dynamic"`
}
func (*Xxxx) GetType() BoxType {
return BoxTypeXxxx()
}
```
### Buffering
go-mp4 has no buffering feature for I/O.
If you should reduce Read function calls, you can wrap the io.ReadSeeker by [bufseekio](https://github.com/sunfish-shogi/bufseekio).
## Command Line Tool
Install mp4tool as follows:
```sh
go install github.com/abema/go-mp4/cmd/mp4tool@latest
mp4tool -help
```
For example, `mp4tool dump MP4_FILE_NAME` command prints MP4 box tree as follows:
```
[moof] Size=504
[mfhd] Size=16 Version=0 Flags=0x000000 SequenceNumber=1
[traf] Size=480
[tfhd] Size=28 Version=0 Flags=0x020038 TrackID=1 DefaultSampleDuration=9000 DefaultSampleSize=33550 DefaultSampleFlags=0x1010000
[tfdt] Size=20 Version=1 Flags=0x000000 BaseMediaDecodeTimeV1=0
[trun] Size=424 ... (use -a option to show all)
[mdat] Size=44569 Data=[...] (use -mdat option to expand)
```

View File

@ -1,19 +0,0 @@
package mp4
type IAnyType interface {
IBox
SetType(BoxType)
}
type AnyTypeBox struct {
Box
Type BoxType
}
func (e *AnyTypeBox) GetType() BoxType {
return e.Type
}
func (e *AnyTypeBox) SetType(boxType BoxType) {
e.Type = boxType
}

188
vendor/github.com/abema/go-mp4/box.go generated vendored
View File

@ -1,188 +0,0 @@
package mp4
import (
"errors"
"io"
"math"
"github.com/abema/go-mp4/internal/bitio"
)
const LengthUnlimited = math.MaxUint32
type ICustomFieldObject interface {
// GetFieldSize returns size of dynamic field
GetFieldSize(name string, ctx Context) uint
// GetFieldLength returns length of dynamic field
GetFieldLength(name string, ctx Context) uint
// IsOptFieldEnabled check whether if the optional field is enabled
IsOptFieldEnabled(name string, ctx Context) bool
// StringifyField returns field value as string
StringifyField(name string, indent string, depth int, ctx Context) (string, bool)
IsPString(name string, bytes []byte, remainingSize uint64, ctx Context) bool
BeforeUnmarshal(r io.ReadSeeker, size uint64, ctx Context) (n uint64, override bool, err error)
OnReadField(name string, r bitio.ReadSeeker, leftBits uint64, ctx Context) (rbits uint64, override bool, err error)
OnWriteField(name string, w bitio.Writer, ctx Context) (wbits uint64, override bool, err error)
}
type BaseCustomFieldObject struct {
}
// GetFieldSize returns size of dynamic field
func (box *BaseCustomFieldObject) GetFieldSize(string, Context) uint {
panic(errors.New("GetFieldSize not implemented"))
}
// GetFieldLength returns length of dynamic field
func (box *BaseCustomFieldObject) GetFieldLength(string, Context) uint {
panic(errors.New("GetFieldLength not implemented"))
}
// IsOptFieldEnabled check whether if the optional field is enabled
func (box *BaseCustomFieldObject) IsOptFieldEnabled(string, Context) bool {
return false
}
// StringifyField returns field value as string
func (box *BaseCustomFieldObject) StringifyField(string, string, int, Context) (string, bool) {
return "", false
}
func (*BaseCustomFieldObject) IsPString(name string, bytes []byte, remainingSize uint64, ctx Context) bool {
return true
}
func (*BaseCustomFieldObject) BeforeUnmarshal(io.ReadSeeker, uint64, Context) (uint64, bool, error) {
return 0, false, nil
}
func (*BaseCustomFieldObject) OnReadField(string, bitio.ReadSeeker, uint64, Context) (uint64, bool, error) {
return 0, false, nil
}
func (*BaseCustomFieldObject) OnWriteField(string, bitio.Writer, Context) (uint64, bool, error) {
return 0, false, nil
}
// IImmutableBox is common interface of box
type IImmutableBox interface {
ICustomFieldObject
// GetVersion returns the box version
GetVersion() uint8
// GetFlags returns the flags
GetFlags() uint32
// CheckFlag checks the flag status
CheckFlag(uint32) bool
// GetType returns the BoxType
GetType() BoxType
}
// IBox is common interface of box
type IBox interface {
IImmutableBox
// SetVersion sets the box version
SetVersion(uint8)
// SetFlags sets the flags
SetFlags(uint32)
// AddFlag adds the flag
AddFlag(uint32)
// RemoveFlag removes the flag
RemoveFlag(uint32)
}
type Box struct {
BaseCustomFieldObject
}
// GetVersion returns the box version
func (box *Box) GetVersion() uint8 {
return 0
}
// SetVersion sets the box version
func (box *Box) SetVersion(uint8) {
}
// GetFlags returns the flags
func (box *Box) GetFlags() uint32 {
return 0x000000
}
// CheckFlag checks the flag status
func (box *Box) CheckFlag(flag uint32) bool {
return true
}
// SetFlags sets the flags
func (box *Box) SetFlags(uint32) {
}
// AddFlag adds the flag
func (box *Box) AddFlag(flag uint32) {
}
// RemoveFlag removes the flag
func (box *Box) RemoveFlag(flag uint32) {
}
// FullBox is ISOBMFF FullBox
type FullBox struct {
BaseCustomFieldObject
Version uint8 `mp4:"0,size=8"`
Flags [3]byte `mp4:"1,size=8"`
}
// GetVersion returns the box version
func (box *FullBox) GetVersion() uint8 {
return box.Version
}
// SetVersion sets the box version
func (box *FullBox) SetVersion(version uint8) {
box.Version = version
}
// GetFlags returns the flags
func (box *FullBox) GetFlags() uint32 {
flag := uint32(box.Flags[0]) << 16
flag ^= uint32(box.Flags[1]) << 8
flag ^= uint32(box.Flags[2])
return flag
}
// CheckFlag checks the flag status
func (box *FullBox) CheckFlag(flag uint32) bool {
return box.GetFlags()&flag != 0
}
// SetFlags sets the flags
func (box *FullBox) SetFlags(flags uint32) {
box.Flags[0] = byte(flags >> 16)
box.Flags[1] = byte(flags >> 8)
box.Flags[2] = byte(flags)
}
// AddFlag adds the flag
func (box *FullBox) AddFlag(flag uint32) {
box.SetFlags(box.GetFlags() | flag)
}
// RemoveFlag removes the flag
func (box *FullBox) RemoveFlag(flag uint32) {
box.SetFlags(box.GetFlags() & (^flag))
}

View File

@ -1,162 +0,0 @@
package mp4
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"math"
)
type Context struct {
// IsQuickTimeCompatible represents whether ftyp.compatible_brands contains "qt ".
IsQuickTimeCompatible bool
// QuickTimeKeysMetaEntryCount the expected number of items under the ilst box as observed from the keys box
QuickTimeKeysMetaEntryCount int
// UnderWave represents whether current box is under the wave box.
UnderWave bool
// UnderIlst represents whether current box is under the ilst box.
UnderIlst bool
// UnderIlstMeta represents whether current box is under the metadata box under the ilst box.
UnderIlstMeta bool
// UnderIlstFreeMeta represents whether current box is under "----" box.
UnderIlstFreeMeta bool
// UnderUdta represents whether current box is under the udta box.
UnderUdta bool
}
// BoxInfo has common infomations of box
type BoxInfo struct {
// Offset specifies an offset of the box in a file.
Offset uint64
// Size specifies size(bytes) of box.
Size uint64
// HeaderSize specifies size(bytes) of common fields which are defined as "Box" class member at ISO/IEC 14496-12.
HeaderSize uint64
// Type specifies box type which is represented by 4 characters.
Type BoxType
// ExtendToEOF is set true when Box.size is zero. It means that end of box equals to end of file.
ExtendToEOF bool
// Context would be set by ReadBoxStructure, not ReadBoxInfo.
Context
}
func (bi *BoxInfo) IsSupportedType() bool {
return bi.Type.IsSupported(bi.Context)
}
const (
SmallHeaderSize = 8
LargeHeaderSize = 16
)
// WriteBoxInfo writes common fields which are defined as "Box" class member at ISO/IEC 14496-12.
// This function ignores bi.Offset and returns BoxInfo which contains real Offset and recalculated Size/HeaderSize.
func WriteBoxInfo(w io.WriteSeeker, bi *BoxInfo) (*BoxInfo, error) {
offset, err := w.Seek(0, io.SeekCurrent)
if err != nil {
return nil, err
}
var data []byte
if bi.ExtendToEOF {
data = make([]byte, SmallHeaderSize)
} else if bi.Size <= math.MaxUint32 && bi.HeaderSize != LargeHeaderSize {
data = make([]byte, SmallHeaderSize)
binary.BigEndian.PutUint32(data, uint32(bi.Size))
} else {
data = make([]byte, LargeHeaderSize)
binary.BigEndian.PutUint32(data, 1)
binary.BigEndian.PutUint64(data[SmallHeaderSize:], bi.Size)
}
data[4] = bi.Type[0]
data[5] = bi.Type[1]
data[6] = bi.Type[2]
data[7] = bi.Type[3]
if _, err := w.Write(data); err != nil {
return nil, err
}
return &BoxInfo{
Offset: uint64(offset),
Size: bi.Size - bi.HeaderSize + uint64(len(data)),
HeaderSize: uint64(len(data)),
Type: bi.Type,
ExtendToEOF: bi.ExtendToEOF,
}, nil
}
// ReadBoxInfo reads common fields which are defined as "Box" class member at ISO/IEC 14496-12.
func ReadBoxInfo(r io.ReadSeeker) (*BoxInfo, error) {
offset, err := r.Seek(0, io.SeekCurrent)
if err != nil {
return nil, err
}
bi := &BoxInfo{
Offset: uint64(offset),
}
// read 8 bytes
buf := bytes.NewBuffer(make([]byte, 0, SmallHeaderSize))
if _, err := io.CopyN(buf, r, SmallHeaderSize); err != nil {
return nil, err
}
bi.HeaderSize += SmallHeaderSize
// pick size and type
data := buf.Bytes()
bi.Size = uint64(binary.BigEndian.Uint32(data))
bi.Type = BoxType{data[4], data[5], data[6], data[7]}
if bi.Size == 0 {
// box extends to end of file
offsetEOF, err := r.Seek(0, io.SeekEnd)
if err != nil {
return nil, err
}
bi.Size = uint64(offsetEOF) - bi.Offset
bi.ExtendToEOF = true
if _, err := bi.SeekToPayload(r); err != nil {
return nil, err
}
} else if bi.Size == 1 {
// read more 8 bytes
buf.Reset()
if _, err := io.CopyN(buf, r, LargeHeaderSize-SmallHeaderSize); err != nil {
return nil, err
}
bi.HeaderSize += LargeHeaderSize - SmallHeaderSize
bi.Size = binary.BigEndian.Uint64(buf.Bytes())
}
if bi.Size == 0 {
return nil, fmt.Errorf("invalid size")
}
return bi, nil
}
func (bi *BoxInfo) SeekToStart(s io.Seeker) (int64, error) {
return s.Seek(int64(bi.Offset), io.SeekStart)
}
func (bi *BoxInfo) SeekToPayload(s io.Seeker) (int64, error) {
return s.Seek(int64(bi.Offset+bi.HeaderSize), io.SeekStart)
}
func (bi *BoxInfo) SeekToEnd(s io.Seeker) (int64, error) {
return s.Seek(int64(bi.Offset+bi.Size), io.SeekStart)
}

View File

@ -1,24 +0,0 @@
package mp4
var udta3GppMetaBoxTypes = []BoxType{
StrToBoxType("titl"),
StrToBoxType("dscp"),
StrToBoxType("cprt"),
StrToBoxType("perf"),
StrToBoxType("auth"),
StrToBoxType("gnre"),
}
func init() {
for _, bt := range udta3GppMetaBoxTypes {
AddAnyTypeBoxDefEx(&Udta3GppString{}, bt, isUnderUdta, 0)
}
}
type Udta3GppString struct {
AnyTypeBox
FullBox `mp4:"0,extend"`
Pad bool `mp4:"1,size=1,hidden"`
Language [3]byte `mp4:"2,size=5,iso639-2"` // ISO-639-2/T language code
Data []byte `mp4:"3,size=8,string"`
}

View File

@ -1,44 +0,0 @@
package mp4
/*************************** av01 ****************************/
// https://aomediacodec.github.io/av1-isobmff
func BoxTypeAv01() BoxType { return StrToBoxType("av01") }
func init() {
AddAnyTypeBoxDef(&VisualSampleEntry{}, BoxTypeAv01())
}
/*************************** av1C ****************************/
// https://aomediacodec.github.io/av1-isobmff
func BoxTypeAv1C() BoxType { return StrToBoxType("av1C") }
func init() {
AddBoxDef(&Av1C{})
}
type Av1C struct {
Box
Marker uint8 `mp4:"0,size=1,const=1"`
Version uint8 `mp4:"1,size=7,const=1"`
SeqProfile uint8 `mp4:"2,size=3"`
SeqLevelIdx0 uint8 `mp4:"3,size=5"`
SeqTier0 uint8 `mp4:"4,size=1"`
HighBitdepth uint8 `mp4:"5,size=1"`
TwelveBit uint8 `mp4:"6,size=1"`
Monochrome uint8 `mp4:"7,size=1"`
ChromaSubsamplingX uint8 `mp4:"8,size=1"`
ChromaSubsamplingY uint8 `mp4:"9,size=1"`
ChromaSamplePosition uint8 `mp4:"10,size=2"`
Reserved uint8 `mp4:"11,size=3,const=0"`
InitialPresentationDelayPresent uint8 `mp4:"12,size=1"`
InitialPresentationDelayMinusOne uint8 `mp4:"13,size=4"`
ConfigOBUs []uint8 `mp4:"14,size=8"`
}
func (Av1C) GetType() BoxType {
return BoxTypeAv1C()
}

View File

@ -1,36 +0,0 @@
package mp4
/*************************** ac-3 ****************************/
// https://www.etsi.org/deliver/etsi_ts/102300_102399/102366/01.04.01_60/ts_102366v010401p.pdf
func BoxTypeAC3() BoxType { return StrToBoxType("ac-3") }
func init() {
AddAnyTypeBoxDef(&AudioSampleEntry{}, BoxTypeAC3())
}
/*************************** dac3 ****************************/
// https://www.etsi.org/deliver/etsi_ts/102300_102399/102366/01.04.01_60/ts_102366v010401p.pdf
func BoxTypeDAC3() BoxType { return StrToBoxType("dac3") }
func init() {
AddBoxDef(&Dac3{})
}
type Dac3 struct {
Box
Fscod uint8 `mp4:"0,size=2"`
Bsid uint8 `mp4:"1,size=5"`
Bsmod uint8 `mp4:"2,size=3"`
Acmod uint8 `mp4:"3,size=3"`
LfeOn uint8 `mp4:"4,size=1"`
BitRateCode uint8 `mp4:"5,size=5"`
Reserved uint8 `mp4:"6,size=5,const=0"`
}
func (Dac3) GetType() BoxType {
return BoxTypeDAC3()
}

File diff suppressed because it is too large Load Diff

View File

@ -1,126 +0,0 @@
package mp4
import "fmt"
/*************************** esds ****************************/
// https://developer.apple.com/library/content/documentation/QuickTime/QTFF/QTFFChap3/qtff3.html
func BoxTypeEsds() BoxType { return StrToBoxType("esds") }
func init() {
AddBoxDef(&Esds{}, 0)
}
const (
ESDescrTag = 0x03
DecoderConfigDescrTag = 0x04
DecSpecificInfoTag = 0x05
SLConfigDescrTag = 0x06
)
// Esds is ES descripter box
type Esds struct {
FullBox `mp4:"0,extend"`
Descriptors []Descriptor `mp4:"1,array"`
}
// GetType returns the BoxType
func (*Esds) GetType() BoxType {
return BoxTypeEsds()
}
type Descriptor struct {
BaseCustomFieldObject
Tag int8 `mp4:"0,size=8"` // must be 0x03
Size uint32 `mp4:"1,varint"`
ESDescriptor *ESDescriptor `mp4:"2,extend,opt=dynamic"`
DecoderConfigDescriptor *DecoderConfigDescriptor `mp4:"3,extend,opt=dynamic"`
Data []byte `mp4:"4,size=8,opt=dynamic,len=dynamic"`
}
// GetFieldLength returns length of dynamic field
func (ds *Descriptor) GetFieldLength(name string, ctx Context) uint {
switch name {
case "Data":
return uint(ds.Size)
}
panic(fmt.Errorf("invalid name of dynamic-length field: boxType=esds fieldName=%s", name))
}
func (ds *Descriptor) IsOptFieldEnabled(name string, ctx Context) bool {
switch ds.Tag {
case ESDescrTag:
return name == "ESDescriptor"
case DecoderConfigDescrTag:
return name == "DecoderConfigDescriptor"
default:
return name == "Data"
}
}
// StringifyField returns field value as string
func (ds *Descriptor) StringifyField(name string, indent string, depth int, ctx Context) (string, bool) {
switch name {
case "Tag":
switch ds.Tag {
case ESDescrTag:
return "ESDescr", true
case DecoderConfigDescrTag:
return "DecoderConfigDescr", true
case DecSpecificInfoTag:
return "DecSpecificInfo", true
case SLConfigDescrTag:
return "SLConfigDescr", true
default:
return "", false
}
default:
return "", false
}
}
type ESDescriptor struct {
BaseCustomFieldObject
ESID uint16 `mp4:"0,size=16"`
StreamDependenceFlag bool `mp4:"1,size=1"`
UrlFlag bool `mp4:"2,size=1"`
OcrStreamFlag bool `mp4:"3,size=1"`
StreamPriority int8 `mp4:"4,size=5"`
DependsOnESID uint16 `mp4:"5,size=16,opt=dynamic"`
URLLength uint8 `mp4:"6,size=8,opt=dynamic"`
URLString []byte `mp4:"7,size=8,len=dynamic,opt=dynamic,string"`
OCRESID uint16 `mp4:"8,size=16,opt=dynamic"`
}
func (esds *ESDescriptor) GetFieldLength(name string, ctx Context) uint {
switch name {
case "URLString":
return uint(esds.URLLength)
}
panic(fmt.Errorf("invalid name of dynamic-length field: boxType=ESDescriptor fieldName=%s", name))
}
func (esds *ESDescriptor) IsOptFieldEnabled(name string, ctx Context) bool {
switch name {
case "DependsOnESID":
return esds.StreamDependenceFlag
case "URLLength", "URLString":
return esds.UrlFlag
case "OCRESID":
return esds.OcrStreamFlag
default:
return false
}
}
type DecoderConfigDescriptor struct {
BaseCustomFieldObject
ObjectTypeIndication byte `mp4:"0,size=8"`
StreamType int8 `mp4:"1,size=6"`
UpStream bool `mp4:"2,size=1"`
Reserved bool `mp4:"3,size=1"`
BufferSizeDB uint32 `mp4:"4,size=24"`
MaxBitrate uint32 `mp4:"5,size=32"`
AvgBitrate uint32 `mp4:"6,size=32"`
}

View File

@ -1,35 +0,0 @@
package mp4
/*************************** ipcm ****************************/
func BoxTypeIpcm() BoxType { return StrToBoxType("ipcm") }
func init() {
AddAnyTypeBoxDef(&AudioSampleEntry{}, BoxTypeIpcm())
}
/*************************** fpcm ****************************/
func BoxTypeFpcm() BoxType { return StrToBoxType("fpcm") }
func init() {
AddAnyTypeBoxDef(&AudioSampleEntry{}, BoxTypeFpcm())
}
/*************************** pcmC ****************************/
func BoxTypePcmC() BoxType { return StrToBoxType("pcmC") }
func init() {
AddBoxDef(&PcmC{}, 0, 1)
}
type PcmC struct {
FullBox `mp4:"0,extend"`
FormatFlags uint8 `mp4:"1,size=8"`
PCMSampleSize uint8 `mp4:"1,size=8"`
}
func (PcmC) GetType() BoxType {
return BoxTypePcmC()
}

View File

@ -1,108 +0,0 @@
package mp4
import (
"bytes"
"fmt"
"github.com/google/uuid"
)
/*************************** pssh ****************************/
func BoxTypePssh() BoxType { return StrToBoxType("pssh") }
func init() {
AddBoxDef(&Pssh{}, 0, 1)
}
// Pssh is ISOBMFF pssh box type
type Pssh struct {
FullBox `mp4:"0,extend"`
SystemID [16]byte `mp4:"1,size=8,uuid"`
KIDCount uint32 `mp4:"2,size=32,nver=0"`
KIDs []PsshKID `mp4:"3,nver=0,len=dynamic,size=128"`
DataSize int32 `mp4:"4,size=32"`
Data []byte `mp4:"5,size=8,len=dynamic"`
}
type PsshKID struct {
KID [16]byte `mp4:"0,size=8,uuid"`
}
// GetFieldLength returns length of dynamic field
func (pssh *Pssh) GetFieldLength(name string, ctx Context) uint {
switch name {
case "KIDs":
return uint(pssh.KIDCount)
case "Data":
return uint(pssh.DataSize)
}
panic(fmt.Errorf("invalid name of dynamic-length field: boxType=pssh fieldName=%s", name))
}
// StringifyField returns field value as string
func (pssh *Pssh) StringifyField(name string, indent string, depth int, ctx Context) (string, bool) {
switch name {
case "KIDs":
buf := bytes.NewBuffer(nil)
buf.WriteString("[")
for i, e := range pssh.KIDs {
if i != 0 {
buf.WriteString(", ")
}
buf.WriteString(uuid.UUID(e.KID).String())
}
buf.WriteString("]")
return buf.String(), true
default:
return "", false
}
}
// GetType returns the BoxType
func (*Pssh) GetType() BoxType {
return BoxTypePssh()
}
/*************************** tenc ****************************/
func BoxTypeTenc() BoxType { return StrToBoxType("tenc") }
func init() {
AddBoxDef(&Tenc{}, 0, 1)
}
// Tenc is ISOBMFF tenc box type
type Tenc struct {
FullBox `mp4:"0,extend"`
Reserved uint8 `mp4:"1,size=8,dec"`
DefaultCryptByteBlock uint8 `mp4:"2,size=4,dec"` // always 0 on version 0
DefaultSkipByteBlock uint8 `mp4:"3,size=4,dec"` // always 0 on version 0
DefaultIsProtected uint8 `mp4:"4,size=8,dec"`
DefaultPerSampleIVSize uint8 `mp4:"5,size=8,dec"`
DefaultKID [16]byte `mp4:"6,size=8,uuid"`
DefaultConstantIVSize uint8 `mp4:"7,size=8,opt=dynamic,dec"`
DefaultConstantIV []byte `mp4:"8,size=8,opt=dynamic,len=dynamic"`
}
func (tenc *Tenc) IsOptFieldEnabled(name string, ctx Context) bool {
switch name {
case "DefaultConstantIVSize", "DefaultConstantIV":
return tenc.DefaultIsProtected == 1 && tenc.DefaultPerSampleIVSize == 0
}
return false
}
func (tenc *Tenc) GetFieldLength(name string, ctx Context) uint {
switch name {
case "DefaultConstantIV":
return uint(tenc.DefaultConstantIVSize)
}
panic(fmt.Errorf("invalid name of dynamic-length field: boxType=tenc fieldName=%s", name))
}
// GetType returns the BoxType
func (*Tenc) GetType() BoxType {
return BoxTypeTenc()
}

View File

@ -1,257 +0,0 @@
package mp4
import (
"fmt"
"github.com/abema/go-mp4/internal/util"
)
/*************************** ilst ****************************/
func BoxTypeIlst() BoxType { return StrToBoxType("ilst") }
func BoxTypeData() BoxType { return StrToBoxType("data") }
var ilstMetaBoxTypes = []BoxType{
StrToBoxType("----"),
StrToBoxType("aART"),
StrToBoxType("akID"),
StrToBoxType("apID"),
StrToBoxType("atID"),
StrToBoxType("cmID"),
StrToBoxType("cnID"),
StrToBoxType("covr"),
StrToBoxType("cpil"),
StrToBoxType("cprt"),
StrToBoxType("desc"),
StrToBoxType("disk"),
StrToBoxType("egid"),
StrToBoxType("geID"),
StrToBoxType("gnre"),
StrToBoxType("pcst"),
StrToBoxType("pgap"),
StrToBoxType("plID"),
StrToBoxType("purd"),
StrToBoxType("purl"),
StrToBoxType("rtng"),
StrToBoxType("sfID"),
StrToBoxType("soaa"),
StrToBoxType("soal"),
StrToBoxType("soar"),
StrToBoxType("soco"),
StrToBoxType("sonm"),
StrToBoxType("sosn"),
StrToBoxType("stik"),
StrToBoxType("tmpo"),
StrToBoxType("trkn"),
StrToBoxType("tven"),
StrToBoxType("tves"),
StrToBoxType("tvnn"),
StrToBoxType("tvsh"),
StrToBoxType("tvsn"),
{0xA9, 'A', 'R', 'T'},
{0xA9, 'a', 'l', 'b'},
{0xA9, 'c', 'm', 't'},
{0xA9, 'c', 'o', 'm'},
{0xA9, 'd', 'a', 'y'},
{0xA9, 'g', 'e', 'n'},
{0xA9, 'g', 'r', 'p'},
{0xA9, 'n', 'a', 'm'},
{0xA9, 't', 'o', 'o'},
{0xA9, 'w', 'r', 't'},
}
func IsIlstMetaBoxType(boxType BoxType) bool {
for _, bt := range ilstMetaBoxTypes {
if boxType == bt {
return true
}
}
return false
}
func init() {
AddBoxDef(&Ilst{})
AddBoxDefEx(&Data{}, isUnderIlstMeta)
for _, bt := range ilstMetaBoxTypes {
AddAnyTypeBoxDefEx(&IlstMetaContainer{}, bt, isIlstMetaContainer)
}
AddAnyTypeBoxDefEx(&StringData{}, StrToBoxType("mean"), isUnderIlstFreeFormat)
AddAnyTypeBoxDefEx(&StringData{}, StrToBoxType("name"), isUnderIlstFreeFormat)
}
type Ilst struct {
Box
}
// GetType returns the BoxType
func (*Ilst) GetType() BoxType {
return BoxTypeIlst()
}
type IlstMetaContainer struct {
AnyTypeBox
}
func isIlstMetaContainer(ctx Context) bool {
return ctx.UnderIlst && !ctx.UnderIlstMeta
}
const (
DataTypeBinary = 0
DataTypeStringUTF8 = 1
DataTypeStringUTF16 = 2
DataTypeStringMac = 3
DataTypeStringJPEG = 14
DataTypeSignedIntBigEndian = 21
DataTypeFloat32BigEndian = 22
DataTypeFloat64BigEndian = 23
)
// Data is a Value BoxType
// https://developer.apple.com/documentation/quicktime-file-format/value_atom
type Data struct {
Box
DataType uint32 `mp4:"0,size=32"`
DataLang uint32 `mp4:"1,size=32"`
Data []byte `mp4:"2,size=8"`
}
// GetType returns the BoxType
func (*Data) GetType() BoxType {
return BoxTypeData()
}
func isUnderIlstMeta(ctx Context) bool {
return ctx.UnderIlstMeta
}
// StringifyField returns field value as string
func (data *Data) StringifyField(name string, indent string, depth int, ctx Context) (string, bool) {
switch name {
case "DataType":
switch data.DataType {
case DataTypeBinary:
return "BINARY", true
case DataTypeStringUTF8:
return "UTF8", true
case DataTypeStringUTF16:
return "UTF16", true
case DataTypeStringMac:
return "MAC_STR", true
case DataTypeStringJPEG:
return "JPEG", true
case DataTypeSignedIntBigEndian:
return "INT", true
case DataTypeFloat32BigEndian:
return "FLOAT32", true
case DataTypeFloat64BigEndian:
return "FLOAT64", true
}
case "Data":
switch data.DataType {
case DataTypeStringUTF8:
return fmt.Sprintf("\"%s\"", util.EscapeUnprintables(string(data.Data))), true
}
}
return "", false
}
type StringData struct {
AnyTypeBox
Data []byte `mp4:"0,size=8"`
}
// StringifyField returns field value as string
func (sd *StringData) StringifyField(name string, indent string, depth int, ctx Context) (string, bool) {
if name == "Data" {
return fmt.Sprintf("\"%s\"", util.EscapeUnprintables(string(sd.Data))), true
}
return "", false
}
/*************************** numbered items ****************************/
// Item is a numbered item under an item list atom
// https://developer.apple.com/documentation/quicktime-file-format/metadata_item_list_atom/item_list
type Item struct {
AnyTypeBox
Version uint8 `mp4:"0,size=8"`
Flags [3]byte `mp4:"1,size=8"`
ItemName []byte `mp4:"2,size=8,len=4"`
Data Data `mp4:"3"`
}
// StringifyField returns field value as string
func (i *Item) StringifyField(name string, indent string, depth int, ctx Context) (string, bool) {
switch name {
case "ItemName":
return fmt.Sprintf("\"%s\"", util.EscapeUnprintables(string(i.ItemName))), true
}
return "", false
}
func isUnderIlstFreeFormat(ctx Context) bool {
return ctx.UnderIlstFreeMeta
}
func BoxTypeKeys() BoxType { return StrToBoxType("keys") }
func init() {
AddBoxDef(&Keys{})
}
/*************************** keys ****************************/
// Keys is the Keys BoxType
// https://developer.apple.com/documentation/quicktime-file-format/metadata_item_keys_atom
type Keys struct {
FullBox `mp4:"0,extend"`
EntryCount int32 `mp4:"1,size=32"`
Entries []Key `mp4:"2,len=dynamic"`
}
// GetType implements the IBox interface and returns the BoxType
func (*Keys) GetType() BoxType {
return BoxTypeKeys()
}
// GetFieldLength implements the ICustomFieldObject interface and returns the length of dynamic fields
func (k *Keys) GetFieldLength(name string, ctx Context) uint {
switch name {
case "Entries":
return uint(k.EntryCount)
}
panic(fmt.Errorf("invalid name of dynamic-length field: boxType=keys fieldName=%s", name))
}
/*************************** key ****************************/
// Key is a key value field in the Keys BoxType
// https://developer.apple.com/documentation/quicktime-file-format/metadata_item_keys_atom/key_value_key_size-8
type Key struct {
BaseCustomFieldObject
KeySize int32 `mp4:"0,size=32"`
KeyNamespace []byte `mp4:"1,size=8,len=4"`
KeyValue []byte `mp4:"2,size=8,len=dynamic"`
}
// GetFieldLength implements the ICustomFieldObject interface and returns the length of dynamic fields
func (k *Key) GetFieldLength(name string, ctx Context) uint {
switch name {
case "KeyValue":
// sizeOf(KeySize)+sizeOf(KeyNamespace) = 8 bytes
return uint(k.KeySize) - 8
}
panic(fmt.Errorf("invalid name of dynamic-length field: boxType=key fieldName=%s", name))
}
// StringifyField returns field value as string
func (k *Key) StringifyField(name string, indent string, depth int, ctx Context) (string, bool) {
switch name {
case "KeyNamespace":
return fmt.Sprintf("\"%s\"", util.EscapeUnprintables(string(k.KeyNamespace))), true
case "KeyValue":
return fmt.Sprintf("\"%s\"", util.EscapeUnprintables(string(k.KeyValue))), true
}
return "", false
}

View File

@ -1,54 +0,0 @@
package mp4
/*************************** Opus ****************************/
// https://opus-codec.org/docs/opus_in_isobmff.html
func BoxTypeOpus() BoxType { return StrToBoxType("Opus") }
func init() {
AddAnyTypeBoxDef(&AudioSampleEntry{}, BoxTypeOpus())
}
/*************************** dOps ****************************/
// https://opus-codec.org/docs/opus_in_isobmff.html
func BoxTypeDOps() BoxType { return StrToBoxType("dOps") }
func init() {
AddBoxDef(&DOps{})
}
type DOps struct {
Box
Version uint8 `mp4:"0,size=8"`
OutputChannelCount uint8 `mp4:"1,size=8"`
PreSkip uint16 `mp4:"2,size=16"`
InputSampleRate uint32 `mp4:"3,size=32"`
OutputGain int16 `mp4:"4,size=16"`
ChannelMappingFamily uint8 `mp4:"5,size=8"`
StreamCount uint8 `mp4:"6,opt=dynamic,size=8"`
CoupledCount uint8 `mp4:"7,opt=dynamic,size=8"`
ChannelMapping []uint8 `mp4:"8,opt=dynamic,size=8,len=dynamic"`
}
func (DOps) GetType() BoxType {
return BoxTypeDOps()
}
func (dops DOps) IsOptFieldEnabled(name string, ctx Context) bool {
switch name {
case "StreamCount", "CoupledCount", "ChannelMapping":
return dops.ChannelMappingFamily != 0
}
return false
}
func (ops DOps) GetFieldLength(name string, ctx Context) uint {
switch name {
case "ChannelMapping":
return uint(ops.OutputChannelCount)
}
return 0
}

View File

@ -1,53 +0,0 @@
package mp4
// https://www.webmproject.org/vp9/mp4/
/*************************** vp08 ****************************/
func BoxTypeVp08() BoxType { return StrToBoxType("vp08") }
func init() {
AddAnyTypeBoxDef(&VisualSampleEntry{}, BoxTypeVp08())
}
/*************************** vp09 ****************************/
func BoxTypeVp09() BoxType { return StrToBoxType("vp09") }
func init() {
AddAnyTypeBoxDef(&VisualSampleEntry{}, BoxTypeVp09())
}
/*************************** VpcC ****************************/
func BoxTypeVpcC() BoxType { return StrToBoxType("vpcC") }
func init() {
AddBoxDef(&VpcC{})
}
type VpcC struct {
FullBox `mp4:"0,extend"`
Profile uint8 `mp4:"1,size=8"`
Level uint8 `mp4:"2,size=8"`
BitDepth uint8 `mp4:"3,size=4"`
ChromaSubsampling uint8 `mp4:"4,size=3"`
VideoFullRangeFlag uint8 `mp4:"5,size=1"`
ColourPrimaries uint8 `mp4:"6,size=8"`
TransferCharacteristics uint8 `mp4:"7,size=8"`
MatrixCoefficients uint8 `mp4:"8,size=8"`
CodecInitializationDataSize uint16 `mp4:"9,size=16"`
CodecInitializationData []uint8 `mp4:"10,size=8,len=dynamic"`
}
func (VpcC) GetType() BoxType {
return BoxTypeVpcC()
}
func (vpcc VpcC) GetFieldLength(name string, ctx Context) uint {
switch name {
case "CodecInitializationData":
return uint(vpcc.CodecInitializationDataSize)
}
return 0
}

View File

@ -1,98 +0,0 @@
package mp4
import (
"errors"
"io"
)
type BoxInfoWithPayload struct {
Info BoxInfo
Payload IBox
}
func ExtractBoxWithPayload(r io.ReadSeeker, parent *BoxInfo, path BoxPath) ([]*BoxInfoWithPayload, error) {
return ExtractBoxesWithPayload(r, parent, []BoxPath{path})
}
func ExtractBoxesWithPayload(r io.ReadSeeker, parent *BoxInfo, paths []BoxPath) ([]*BoxInfoWithPayload, error) {
bis, err := ExtractBoxes(r, parent, paths)
if err != nil {
return nil, err
}
bs := make([]*BoxInfoWithPayload, 0, len(bis))
for _, bi := range bis {
if _, err := bi.SeekToPayload(r); err != nil {
return nil, err
}
var ctx Context
if parent != nil {
ctx = parent.Context
}
box, _, err := UnmarshalAny(r, bi.Type, bi.Size-bi.HeaderSize, ctx)
if err != nil {
return nil, err
}
bs = append(bs, &BoxInfoWithPayload{
Info: *bi,
Payload: box,
})
}
return bs, nil
}
func ExtractBox(r io.ReadSeeker, parent *BoxInfo, path BoxPath) ([]*BoxInfo, error) {
return ExtractBoxes(r, parent, []BoxPath{path})
}
func ExtractBoxes(r io.ReadSeeker, parent *BoxInfo, paths []BoxPath) ([]*BoxInfo, error) {
if len(paths) == 0 {
return nil, nil
}
for i := range paths {
if len(paths[i]) == 0 {
return nil, errors.New("box path must not be empty")
}
}
boxes := make([]*BoxInfo, 0, 8)
handler := func(handle *ReadHandle) (interface{}, error) {
path := handle.Path
if parent != nil {
path = path[1:]
}
if handle.BoxInfo.Type == BoxTypeAny() {
return nil, nil
}
fm, m := matchPath(paths, path)
if m {
boxes = append(boxes, &handle.BoxInfo)
}
if fm {
if _, err := handle.Expand(); err != nil {
return nil, err
}
}
return nil, nil
}
if parent != nil {
_, err := ReadBoxStructureFromInternal(r, parent, handler)
return boxes, err
}
_, err := ReadBoxStructure(r, handler)
return boxes, err
}
func matchPath(paths []BoxPath, path BoxPath) (forwardMatch bool, match bool) {
for i := range paths {
fm, m := path.compareWith(paths[i])
forwardMatch = forwardMatch || fm
match = match || m
}
return
}

View File

@ -1,290 +0,0 @@
package mp4
import (
"fmt"
"os"
"reflect"
"sort"
"strconv"
"strings"
)
type (
stringType uint8
fieldFlag uint16
)
const (
stringType_C stringType = iota
stringType_C_P
fieldString fieldFlag = 1 << iota // 0
fieldExtend // 1
fieldDec // 2
fieldHex // 3
fieldISO639_2 // 4
fieldUUID // 5
fieldHidden // 6
fieldOptDynamic // 7
fieldVarint // 8
fieldSizeDynamic // 9
fieldLengthDynamic // 10
)
type field struct {
children []*field
name string
cnst string
order int
optFlag uint32
nOptFlag uint32
size uint
length uint
flags fieldFlag
strType stringType
version uint8
nVersion uint8
}
func (f *field) set(flag fieldFlag) {
f.flags |= flag
}
func (f *field) is(flag fieldFlag) bool {
return f.flags&flag != 0
}
func buildFields(box IImmutableBox) []*field {
t := reflect.TypeOf(box).Elem()
return buildFieldsStruct(t)
}
func buildFieldsStruct(t reflect.Type) []*field {
fs := make([]*field, 0, 8)
for i := 0; i < t.NumField(); i++ {
ft := t.Field(i).Type
tag, ok := t.Field(i).Tag.Lookup("mp4")
if !ok {
continue
}
f := buildField(t.Field(i).Name, tag)
f.children = buildFieldsAny(ft)
fs = append(fs, f)
}
sort.SliceStable(fs, func(i, j int) bool {
return fs[i].order < fs[j].order
})
return fs
}
func buildFieldsAny(t reflect.Type) []*field {
switch t.Kind() {
case reflect.Struct:
return buildFieldsStruct(t)
case reflect.Ptr, reflect.Array, reflect.Slice:
return buildFieldsAny(t.Elem())
default:
return nil
}
}
func buildField(fieldName string, tag string) *field {
f := &field{
name: fieldName,
}
tagMap := parseFieldTag(tag)
for key, val := range tagMap {
if val != "" {
continue
}
if order, err := strconv.Atoi(key); err == nil {
f.order = order
break
}
}
if val, contained := tagMap["string"]; contained {
f.set(fieldString)
if val == "c_p" {
f.strType = stringType_C_P
fmt.Fprint(os.Stderr, "go-mp4: string=c_p tag is deprecated!! See https://github.com/abema/go-mp4/issues/76\n")
}
}
if _, contained := tagMap["varint"]; contained {
f.set(fieldVarint)
}
if val, contained := tagMap["opt"]; contained {
if val == "dynamic" {
f.set(fieldOptDynamic)
} else {
base := 10
if strings.HasPrefix(val, "0x") {
val = val[2:]
base = 16
}
opt, err := strconv.ParseUint(val, base, 32)
if err != nil {
panic(err)
}
f.optFlag = uint32(opt)
}
}
if val, contained := tagMap["nopt"]; contained {
base := 10
if strings.HasPrefix(val, "0x") {
val = val[2:]
base = 16
}
nopt, err := strconv.ParseUint(val, base, 32)
if err != nil {
panic(err)
}
f.nOptFlag = uint32(nopt)
}
if _, contained := tagMap["extend"]; contained {
f.set(fieldExtend)
}
if _, contained := tagMap["dec"]; contained {
f.set(fieldDec)
}
if _, contained := tagMap["hex"]; contained {
f.set(fieldHex)
}
if _, contained := tagMap["iso639-2"]; contained {
f.set(fieldISO639_2)
}
if _, contained := tagMap["uuid"]; contained {
f.set(fieldUUID)
}
if _, contained := tagMap["hidden"]; contained {
f.set(fieldHidden)
}
if val, contained := tagMap["const"]; contained {
f.cnst = val
}
f.version = anyVersion
if val, contained := tagMap["ver"]; contained {
ver, err := strconv.Atoi(val)
if err != nil {
panic(err)
}
f.version = uint8(ver)
}
f.nVersion = anyVersion
if val, contained := tagMap["nver"]; contained {
ver, err := strconv.Atoi(val)
if err != nil {
panic(err)
}
f.nVersion = uint8(ver)
}
if val, contained := tagMap["size"]; contained {
if val == "dynamic" {
f.set(fieldSizeDynamic)
} else {
size, err := strconv.ParseUint(val, 10, 32)
if err != nil {
panic(err)
}
f.size = uint(size)
}
}
f.length = LengthUnlimited
if val, contained := tagMap["len"]; contained {
if val == "dynamic" {
f.set(fieldLengthDynamic)
} else {
l, err := strconv.ParseUint(val, 10, 32)
if err != nil {
panic(err)
}
f.length = uint(l)
}
}
return f
}
func parseFieldTag(str string) map[string]string {
tag := make(map[string]string, 8)
list := strings.Split(str, ",")
for _, e := range list {
kv := strings.SplitN(e, "=", 2)
if len(kv) == 2 {
tag[strings.Trim(kv[0], " ")] = strings.Trim(kv[1], " ")
} else {
tag[strings.Trim(kv[0], " ")] = ""
}
}
return tag
}
type fieldInstance struct {
field
cfo ICustomFieldObject
}
func resolveFieldInstance(f *field, box IImmutableBox, parent reflect.Value, ctx Context) *fieldInstance {
fi := fieldInstance{
field: *f,
}
cfo, ok := parent.Addr().Interface().(ICustomFieldObject)
if ok {
fi.cfo = cfo
} else {
fi.cfo = box
}
if fi.is(fieldSizeDynamic) {
fi.size = fi.cfo.GetFieldSize(f.name, ctx)
}
if fi.is(fieldLengthDynamic) {
fi.length = fi.cfo.GetFieldLength(f.name, ctx)
}
return &fi
}
func isTargetField(box IImmutableBox, fi *fieldInstance, ctx Context) bool {
if box.GetVersion() != anyVersion {
if fi.version != anyVersion && box.GetVersion() != fi.version {
return false
}
if fi.nVersion != anyVersion && box.GetVersion() == fi.nVersion {
return false
}
}
if fi.optFlag != 0 && box.GetFlags()&fi.optFlag == 0 {
return false
}
if fi.nOptFlag != 0 && box.GetFlags()&fi.nOptFlag != 0 {
return false
}
if fi.is(fieldOptDynamic) && !fi.cfo.IsOptFieldEnabled(fi.name, ctx) {
return false
}
return true
}

View File

@ -1,8 +0,0 @@
package bitio
import "errors"
var (
ErrInvalidAlignment = errors.New("invalid alignment")
ErrDiscouragedReader = errors.New("discouraged reader implementation")
)

View File

@ -1,97 +0,0 @@
package bitio
import "io"
type Reader interface {
io.Reader
// alignment:
// |-1-byte-block-|--------------|--------------|--------------|
// |<-offset->|<-------------------width---------------------->|
ReadBits(width uint) (data []byte, err error)
ReadBit() (bit bool, err error)
}
type ReadSeeker interface {
Reader
io.Seeker
}
type reader struct {
reader io.Reader
octet byte
width uint
}
func NewReader(r io.Reader) Reader {
return &reader{reader: r}
}
func (r *reader) Read(p []byte) (n int, err error) {
if r.width != 0 {
return 0, ErrInvalidAlignment
}
return r.reader.Read(p)
}
func (r *reader) ReadBits(size uint) ([]byte, error) {
bytes := (size + 7) / 8
data := make([]byte, bytes)
offset := (bytes * 8) - (size)
for i := uint(0); i < size; i++ {
bit, err := r.ReadBit()
if err != nil {
return nil, err
}
byteIdx := (offset + i) / 8
bitIdx := 7 - (offset+i)%8
if bit {
data[byteIdx] |= 0x1 << bitIdx
}
}
return data, nil
}
func (r *reader) ReadBit() (bool, error) {
if r.width == 0 {
buf := make([]byte, 1)
if n, err := r.reader.Read(buf); err != nil {
return false, err
} else if n != 1 {
return false, ErrDiscouragedReader
}
r.octet = buf[0]
r.width = 8
}
r.width--
return (r.octet>>r.width)&0x01 != 0, nil
}
type readSeeker struct {
reader
seeker io.Seeker
}
func NewReadSeeker(r io.ReadSeeker) ReadSeeker {
return &readSeeker{
reader: reader{reader: r},
seeker: r,
}
}
func (r *readSeeker) Seek(offset int64, whence int) (int64, error) {
if whence == io.SeekCurrent && r.reader.width != 0 {
return 0, ErrInvalidAlignment
}
n, err := r.seeker.Seek(offset, whence)
if err != nil {
return n, err
}
r.reader.width = 0
return n, nil
}

View File

@ -1,61 +0,0 @@
package bitio
import (
"io"
)
type Writer interface {
io.Writer
// alignment:
// |-1-byte-block-|--------------|--------------|--------------|
// |<-offset->|<-------------------width---------------------->|
WriteBits(data []byte, width uint) error
WriteBit(bit bool) error
}
type writer struct {
writer io.Writer
octet byte
width uint
}
func NewWriter(w io.Writer) Writer {
return &writer{writer: w}
}
func (w *writer) Write(p []byte) (n int, err error) {
if w.width != 0 {
return 0, ErrInvalidAlignment
}
return w.writer.Write(p)
}
func (w *writer) WriteBits(data []byte, width uint) error {
length := uint(len(data)) * 8
offset := length - width
for i := offset; i < length; i++ {
oi := i / 8
if err := w.WriteBit((data[oi]>>(7-i%8))&0x01 != 0); err != nil {
return err
}
}
return nil
}
func (w *writer) WriteBit(bit bool) error {
if bit {
w.octet |= 0x1 << (7 - w.width)
}
w.width++
if w.width == 8 {
if _, err := w.writer.Write([]byte{w.octet}); err != nil {
return err
}
w.octet = 0x00
w.width = 0
}
return nil
}

View File

@ -1,30 +0,0 @@
package util
import (
"bytes"
"io"
)
func ReadString(r io.Reader) (string, error) {
b := make([]byte, 1)
buf := bytes.NewBuffer(nil)
for {
if _, err := r.Read(b); err != nil {
return "", err
}
if b[0] == 0 {
return buf.String(), nil
}
buf.Write(b)
}
}
func WriteString(w io.Writer, s string) error {
if _, err := w.Write([]byte(s)); err != nil {
return err
}
if _, err := w.Write([]byte{0}); err != nil {
return err
}
return nil
}

View File

@ -1,42 +0,0 @@
package util
import (
"strconv"
"strings"
"unicode"
)
func FormatSignedFixedFloat1616(val int32) string {
if val&0xffff == 0 {
return strconv.Itoa(int(val >> 16))
} else {
return strconv.FormatFloat(float64(val)/(1<<16), 'f', 5, 64)
}
}
func FormatUnsignedFixedFloat1616(val uint32) string {
if val&0xffff == 0 {
return strconv.Itoa(int(val >> 16))
} else {
return strconv.FormatFloat(float64(val)/(1<<16), 'f', 5, 64)
}
}
func FormatSignedFixedFloat88(val int16) string {
if val&0xff == 0 {
return strconv.Itoa(int(val >> 8))
} else {
return strconv.FormatFloat(float64(val)/(1<<8), 'f', 3, 32)
}
}
func EscapeUnprintable(r rune) rune {
if unicode.IsGraphic(r) {
return r
}
return rune('.')
}
func EscapeUnprintables(src string) string {
return strings.Map(EscapeUnprintable, src)
}

View File

@ -1,663 +0,0 @@
package mp4
import (
"bytes"
"errors"
"fmt"
"io"
"math"
"reflect"
"github.com/abema/go-mp4/internal/bitio"
)
const (
anyVersion = math.MaxUint8
)
var ErrUnsupportedBoxVersion = errors.New("unsupported box version")
func readerHasSize(reader bitio.ReadSeeker, size uint64) bool {
pre, err := reader.Seek(0, io.SeekCurrent)
if err != nil {
return false
}
end, err := reader.Seek(0, io.SeekEnd)
if err != nil {
return false
}
if uint64(end-pre) < size {
return false
}
_, err = reader.Seek(pre, io.SeekStart)
if err != nil {
return false
}
return true
}
type marshaller struct {
writer bitio.Writer
wbits uint64
src IImmutableBox
ctx Context
}
func Marshal(w io.Writer, src IImmutableBox, ctx Context) (n uint64, err error) {
boxDef := src.GetType().getBoxDef(ctx)
if boxDef == nil {
return 0, ErrBoxInfoNotFound
}
v := reflect.ValueOf(src).Elem()
m := &marshaller{
writer: bitio.NewWriter(w),
src: src,
ctx: ctx,
}
if err := m.marshalStruct(v, boxDef.fields); err != nil {
return 0, err
}
if m.wbits%8 != 0 {
return 0, fmt.Errorf("box size is not multiple of 8 bits: type=%s, bits=%d", src.GetType().String(), m.wbits)
}
return m.wbits / 8, nil
}
func (m *marshaller) marshal(v reflect.Value, fi *fieldInstance) error {
switch v.Type().Kind() {
case reflect.Ptr:
return m.marshalPtr(v, fi)
case reflect.Struct:
return m.marshalStruct(v, fi.children)
case reflect.Array:
return m.marshalArray(v, fi)
case reflect.Slice:
return m.marshalSlice(v, fi)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return m.marshalInt(v, fi)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return m.marshalUint(v, fi)
case reflect.Bool:
return m.marshalBool(v, fi)
case reflect.String:
return m.marshalString(v)
default:
return fmt.Errorf("unsupported type: %s", v.Type().Kind())
}
}
func (m *marshaller) marshalPtr(v reflect.Value, fi *fieldInstance) error {
return m.marshal(v.Elem(), fi)
}
func (m *marshaller) marshalStruct(v reflect.Value, fs []*field) error {
for _, f := range fs {
fi := resolveFieldInstance(f, m.src, v, m.ctx)
if !isTargetField(m.src, fi, m.ctx) {
continue
}
wbits, override, err := fi.cfo.OnWriteField(f.name, m.writer, m.ctx)
if err != nil {
return err
}
m.wbits += wbits
if override {
continue
}
err = m.marshal(v.FieldByName(f.name), fi)
if err != nil {
return err
}
}
return nil
}
func (m *marshaller) marshalArray(v reflect.Value, fi *fieldInstance) error {
size := v.Type().Size()
for i := 0; i < int(size)/int(v.Type().Elem().Size()); i++ {
var err error
err = m.marshal(v.Index(i), fi)
if err != nil {
return err
}
}
return nil
}
func (m *marshaller) marshalSlice(v reflect.Value, fi *fieldInstance) error {
length := uint64(v.Len())
if fi.length != LengthUnlimited {
if length < uint64(fi.length) {
return fmt.Errorf("the slice has too few elements: required=%d actual=%d", fi.length, length)
}
length = uint64(fi.length)
}
elemType := v.Type().Elem()
if elemType.Kind() == reflect.Uint8 && fi.size == 8 && m.wbits%8 == 0 {
if _, err := io.CopyN(m.writer, bytes.NewBuffer(v.Bytes()), int64(length)); err != nil {
return err
}
m.wbits += length * 8
return nil
}
for i := 0; i < int(length); i++ {
m.marshal(v.Index(i), fi)
}
return nil
}
func (m *marshaller) marshalInt(v reflect.Value, fi *fieldInstance) error {
signed := v.Int()
if fi.is(fieldVarint) {
return errors.New("signed varint is unsupported")
}
signBit := signed < 0
val := uint64(signed)
for i := uint(0); i < fi.size; i += 8 {
v := val
size := uint(8)
if fi.size > i+8 {
v = v >> (fi.size - (i + 8))
} else if fi.size < i+8 {
size = fi.size - i
}
// set sign bit
if i == 0 {
if signBit {
v |= 0x1 << (size - 1)
} else {
v &= 0x1<<(size-1) - 1
}
}
if err := m.writer.WriteBits([]byte{byte(v)}, size); err != nil {
return err
}
m.wbits += uint64(size)
}
return nil
}
func (m *marshaller) marshalUint(v reflect.Value, fi *fieldInstance) error {
val := v.Uint()
if fi.is(fieldVarint) {
m.writeUvarint(val)
return nil
}
for i := uint(0); i < fi.size; i += 8 {
v := val
size := uint(8)
if fi.size > i+8 {
v = v >> (fi.size - (i + 8))
} else if fi.size < i+8 {
size = fi.size - i
}
if err := m.writer.WriteBits([]byte{byte(v)}, size); err != nil {
return err
}
m.wbits += uint64(size)
}
return nil
}
func (m *marshaller) marshalBool(v reflect.Value, fi *fieldInstance) error {
var val byte
if v.Bool() {
val = 0xff
} else {
val = 0x00
}
if err := m.writer.WriteBits([]byte{val}, fi.size); err != nil {
return err
}
m.wbits += uint64(fi.size)
return nil
}
func (m *marshaller) marshalString(v reflect.Value) error {
data := []byte(v.String())
for _, b := range data {
if err := m.writer.WriteBits([]byte{b}, 8); err != nil {
return err
}
m.wbits += 8
}
// null character
if err := m.writer.WriteBits([]byte{0x00}, 8); err != nil {
return err
}
m.wbits += 8
return nil
}
func (m *marshaller) writeUvarint(u uint64) error {
for i := 21; i > 0; i -= 7 {
if err := m.writer.WriteBits([]byte{(byte(u >> uint(i))) | 0x80}, 8); err != nil {
return err
}
m.wbits += 8
}
if err := m.writer.WriteBits([]byte{byte(u) & 0x7f}, 8); err != nil {
return err
}
m.wbits += 8
return nil
}
type unmarshaller struct {
reader bitio.ReadSeeker
dst IBox
size uint64
rbits uint64
ctx Context
}
func UnmarshalAny(r io.ReadSeeker, boxType BoxType, payloadSize uint64, ctx Context) (box IBox, n uint64, err error) {
dst, err := boxType.New(ctx)
if err != nil {
return nil, 0, err
}
n, err = Unmarshal(r, payloadSize, dst, ctx)
return dst, n, err
}
func Unmarshal(r io.ReadSeeker, payloadSize uint64, dst IBox, ctx Context) (n uint64, err error) {
boxDef := dst.GetType().getBoxDef(ctx)
if boxDef == nil {
return 0, ErrBoxInfoNotFound
}
v := reflect.ValueOf(dst).Elem()
dst.SetVersion(anyVersion)
u := &unmarshaller{
reader: bitio.NewReadSeeker(r),
dst: dst,
size: payloadSize,
ctx: ctx,
}
if n, override, err := dst.BeforeUnmarshal(r, payloadSize, u.ctx); err != nil {
return 0, err
} else if override {
return n, nil
} else {
u.rbits = n * 8
}
sn, err := r.Seek(0, io.SeekCurrent)
if err != nil {
return 0, err
}
if err := u.unmarshalStruct(v, boxDef.fields); err != nil {
if err == ErrUnsupportedBoxVersion {
r.Seek(sn, io.SeekStart)
}
return 0, err
}
if u.rbits%8 != 0 {
return 0, fmt.Errorf("box size is not multiple of 8 bits: type=%s, size=%d, bits=%d", dst.GetType().String(), u.size, u.rbits)
}
if u.rbits > u.size*8 {
return 0, fmt.Errorf("overrun error: type=%s, size=%d, bits=%d", dst.GetType().String(), u.size, u.rbits)
}
return u.rbits / 8, nil
}
func (u *unmarshaller) unmarshal(v reflect.Value, fi *fieldInstance) error {
var err error
switch v.Type().Kind() {
case reflect.Ptr:
err = u.unmarshalPtr(v, fi)
case reflect.Struct:
err = u.unmarshalStructInternal(v, fi)
case reflect.Array:
err = u.unmarshalArray(v, fi)
case reflect.Slice:
err = u.unmarshalSlice(v, fi)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
err = u.unmarshalInt(v, fi)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
err = u.unmarshalUint(v, fi)
case reflect.Bool:
err = u.unmarshalBool(v, fi)
case reflect.String:
err = u.unmarshalString(v, fi)
default:
return fmt.Errorf("unsupported type: %s", v.Type().Kind())
}
return err
}
func (u *unmarshaller) unmarshalPtr(v reflect.Value, fi *fieldInstance) error {
v.Set(reflect.New(v.Type().Elem()))
return u.unmarshal(v.Elem(), fi)
}
func (u *unmarshaller) unmarshalStructInternal(v reflect.Value, fi *fieldInstance) error {
if fi.size != 0 && fi.size%8 == 0 {
u2 := *u
u2.size = uint64(fi.size / 8)
u2.rbits = 0
if err := u2.unmarshalStruct(v, fi.children); err != nil {
return err
}
u.rbits += u2.rbits
if u2.rbits != uint64(fi.size) {
return errors.New("invalid alignment")
}
return nil
}
return u.unmarshalStruct(v, fi.children)
}
func (u *unmarshaller) unmarshalStruct(v reflect.Value, fs []*field) error {
for _, f := range fs {
fi := resolveFieldInstance(f, u.dst, v, u.ctx)
if !isTargetField(u.dst, fi, u.ctx) {
continue
}
rbits, override, err := fi.cfo.OnReadField(f.name, u.reader, u.size*8-u.rbits, u.ctx)
if err != nil {
return err
}
u.rbits += rbits
if override {
continue
}
err = u.unmarshal(v.FieldByName(f.name), fi)
if err != nil {
return err
}
if v.FieldByName(f.name).Type() == reflect.TypeOf(FullBox{}) && !u.dst.GetType().IsSupportedVersion(u.dst.GetVersion(), u.ctx) {
return ErrUnsupportedBoxVersion
}
}
return nil
}
func (u *unmarshaller) unmarshalArray(v reflect.Value, fi *fieldInstance) error {
size := v.Type().Size()
for i := 0; i < int(size)/int(v.Type().Elem().Size()); i++ {
var err error
err = u.unmarshal(v.Index(i), fi)
if err != nil {
return err
}
}
return nil
}
func (u *unmarshaller) unmarshalSlice(v reflect.Value, fi *fieldInstance) error {
var slice reflect.Value
elemType := v.Type().Elem()
length := uint64(fi.length)
if fi.length == LengthUnlimited {
if fi.size != 0 {
left := (u.size)*8 - u.rbits
if left%uint64(fi.size) != 0 {
return errors.New("invalid alignment")
}
length = left / uint64(fi.size)
} else {
length = 0
}
}
if u.rbits%8 == 0 && elemType.Kind() == reflect.Uint8 && fi.size == 8 {
totalSize := length * uint64(fi.size) / 8
if !readerHasSize(u.reader, totalSize) {
return fmt.Errorf("not enough bits")
}
buf := bytes.NewBuffer(make([]byte, 0, totalSize))
if _, err := io.CopyN(buf, u.reader, int64(totalSize)); err != nil {
return err
}
slice = reflect.ValueOf(buf.Bytes())
u.rbits += uint64(totalSize) * 8
} else {
slice = reflect.MakeSlice(v.Type(), 0, 0)
for i := 0; ; i++ {
if fi.length != LengthUnlimited && uint(i) >= fi.length {
break
}
if fi.length == LengthUnlimited && u.rbits >= u.size*8 {
break
}
slice = reflect.Append(slice, reflect.Zero(elemType))
if err := u.unmarshal(slice.Index(i), fi); err != nil {
return err
}
if u.rbits > u.size*8 {
return fmt.Errorf("failed to read array completely: fieldName=\"%s\"", fi.name)
}
}
}
v.Set(slice)
return nil
}
func (u *unmarshaller) unmarshalInt(v reflect.Value, fi *fieldInstance) error {
if fi.is(fieldVarint) {
return errors.New("signed varint is unsupported")
}
if fi.size == 0 {
return fmt.Errorf("size must not be zero: %s", fi.name)
}
data, err := u.reader.ReadBits(fi.size)
if err != nil {
return err
}
u.rbits += uint64(fi.size)
signBit := false
if len(data) > 0 {
signMask := byte(0x01) << ((fi.size - 1) % 8)
signBit = data[0]&signMask != 0
if signBit {
data[0] |= ^(signMask - 1)
}
}
var val uint64
if signBit {
val = ^uint64(0)
}
for i := range data {
val <<= 8
val |= uint64(data[i])
}
v.SetInt(int64(val))
return nil
}
func (u *unmarshaller) unmarshalUint(v reflect.Value, fi *fieldInstance) error {
if fi.is(fieldVarint) {
val, err := u.readUvarint()
if err != nil {
return err
}
v.SetUint(val)
return nil
}
if fi.size == 0 {
return fmt.Errorf("size must not be zero: %s", fi.name)
}
data, err := u.reader.ReadBits(fi.size)
if err != nil {
return err
}
u.rbits += uint64(fi.size)
val := uint64(0)
for i := range data {
val <<= 8
val |= uint64(data[i])
}
v.SetUint(val)
return nil
}
func (u *unmarshaller) unmarshalBool(v reflect.Value, fi *fieldInstance) error {
if fi.size == 0 {
return fmt.Errorf("size must not be zero: %s", fi.name)
}
data, err := u.reader.ReadBits(fi.size)
if err != nil {
return err
}
u.rbits += uint64(fi.size)
val := false
for _, b := range data {
val = val || (b != byte(0))
}
v.SetBool(val)
return nil
}
func (u *unmarshaller) unmarshalString(v reflect.Value, fi *fieldInstance) error {
switch fi.strType {
case stringType_C:
return u.unmarshalStringC(v)
case stringType_C_P:
return u.unmarshalStringCP(v, fi)
default:
return fmt.Errorf("unknown string type: %d", fi.strType)
}
}
func (u *unmarshaller) unmarshalStringC(v reflect.Value) error {
data := make([]byte, 0, 16)
for {
if u.rbits >= u.size*8 {
break
}
c, err := u.reader.ReadBits(8)
if err != nil {
return err
}
u.rbits += 8
if c[0] == 0 {
break // null character
}
data = append(data, c[0])
}
v.SetString(string(data))
return nil
}
func (u *unmarshaller) unmarshalStringCP(v reflect.Value, fi *fieldInstance) error {
if ok, err := u.tryReadPString(v, fi); err != nil {
return err
} else if ok {
return nil
}
return u.unmarshalStringC(v)
}
func (u *unmarshaller) tryReadPString(v reflect.Value, fi *fieldInstance) (ok bool, err error) {
remainingSize := (u.size*8 - u.rbits) / 8
if remainingSize < 2 {
return false, nil
}
offset, err := u.reader.Seek(0, io.SeekCurrent)
if err != nil {
return false, err
}
defer func() {
if err == nil && !ok {
_, err = u.reader.Seek(offset, io.SeekStart)
}
}()
buf0 := make([]byte, 1)
if _, err := io.ReadFull(u.reader, buf0); err != nil {
return false, err
}
remainingSize--
plen := buf0[0]
if uint64(plen) > remainingSize {
return false, nil
}
buf := make([]byte, int(plen))
if _, err := io.ReadFull(u.reader, buf); err != nil {
return false, err
}
remainingSize -= uint64(plen)
if fi.cfo.IsPString(fi.name, buf, remainingSize, u.ctx) {
u.rbits += uint64(len(buf)+1) * 8
v.SetString(string(buf))
return true, nil
}
return false, nil
}
func (u *unmarshaller) readUvarint() (uint64, error) {
var val uint64
for {
octet, err := u.reader.ReadBits(8)
if err != nil {
return 0, err
}
u.rbits += 8
val = (val << 7) + uint64(octet[0]&0x7f)
if octet[0]&0x80 == 0 {
return val, nil
}
}
}

171
vendor/github.com/abema/go-mp4/mp4.go generated vendored
View File

@ -1,171 +0,0 @@
package mp4
import (
"encoding/binary"
"errors"
"fmt"
"reflect"
"strings"
)
var ErrBoxInfoNotFound = errors.New("box info not found")
// BoxType is mpeg box type
type BoxType [4]byte
func StrToBoxType(code string) BoxType {
if len(code) != 4 {
panic(fmt.Errorf("invalid box type id length: [%s]", code))
}
return BoxType{code[0], code[1], code[2], code[3]}
}
// Uint32ToBoxType returns a new BoxType from the provied uint32
func Uint32ToBoxType(i uint32) BoxType {
b := make([]byte, 4)
binary.BigEndian.PutUint32(b, i)
return BoxType{b[0], b[1], b[2], b[3]}
}
func (boxType BoxType) String() string {
if isPrintable(boxType[0]) && isPrintable(boxType[1]) && isPrintable(boxType[2]) && isPrintable(boxType[3]) {
s := string([]byte{boxType[0], boxType[1], boxType[2], boxType[3]})
s = strings.ReplaceAll(s, string([]byte{0xa9}), "(c)")
return s
}
return fmt.Sprintf("0x%02x%02x%02x%02x", boxType[0], boxType[1], boxType[2], boxType[3])
}
func isASCII(c byte) bool {
return c >= 0x20 && c <= 0x7e
}
func isPrintable(c byte) bool {
return isASCII(c) || c == 0xa9
}
func (lhs BoxType) MatchWith(rhs BoxType) bool {
if lhs == boxTypeAny || rhs == boxTypeAny {
return true
}
return lhs == rhs
}
var boxTypeAny = BoxType{0x00, 0x00, 0x00, 0x00}
func BoxTypeAny() BoxType {
return boxTypeAny
}
type boxDef struct {
dataType reflect.Type
versions []uint8
isTarget func(Context) bool
fields []*field
}
var boxMap = make(map[BoxType][]boxDef, 64)
func AddBoxDef(payload IBox, versions ...uint8) {
boxMap[payload.GetType()] = append(boxMap[payload.GetType()], boxDef{
dataType: reflect.TypeOf(payload).Elem(),
versions: versions,
fields: buildFields(payload),
})
}
func AddBoxDefEx(payload IBox, isTarget func(Context) bool, versions ...uint8) {
boxMap[payload.GetType()] = append(boxMap[payload.GetType()], boxDef{
dataType: reflect.TypeOf(payload).Elem(),
versions: versions,
isTarget: isTarget,
fields: buildFields(payload),
})
}
func AddAnyTypeBoxDef(payload IAnyType, boxType BoxType, versions ...uint8) {
boxMap[boxType] = append(boxMap[boxType], boxDef{
dataType: reflect.TypeOf(payload).Elem(),
versions: versions,
fields: buildFields(payload),
})
}
func AddAnyTypeBoxDefEx(payload IAnyType, boxType BoxType, isTarget func(Context) bool, versions ...uint8) {
boxMap[boxType] = append(boxMap[boxType], boxDef{
dataType: reflect.TypeOf(payload).Elem(),
versions: versions,
isTarget: isTarget,
fields: buildFields(payload),
})
}
var itemBoxFields = buildFields(&Item{})
func (boxType BoxType) getBoxDef(ctx Context) *boxDef {
boxDefs := boxMap[boxType]
for i := len(boxDefs) - 1; i >= 0; i-- {
boxDef := &boxDefs[i]
if boxDef.isTarget == nil || boxDef.isTarget(ctx) {
return boxDef
}
}
if ctx.UnderIlst {
typeID := int(binary.BigEndian.Uint32(boxType[:]))
if typeID >= 1 && typeID <= ctx.QuickTimeKeysMetaEntryCount {
return &boxDef{
dataType: reflect.TypeOf(Item{}),
isTarget: isIlstMetaContainer,
fields: itemBoxFields,
}
}
}
return nil
}
func (boxType BoxType) IsSupported(ctx Context) bool {
return boxType.getBoxDef(ctx) != nil
}
func (boxType BoxType) New(ctx Context) (IBox, error) {
boxDef := boxType.getBoxDef(ctx)
if boxDef == nil {
return nil, ErrBoxInfoNotFound
}
box, ok := reflect.New(boxDef.dataType).Interface().(IBox)
if !ok {
return nil, fmt.Errorf("box type not implements IBox interface: %s", boxType.String())
}
anyTypeBox, ok := box.(IAnyType)
if ok {
anyTypeBox.SetType(boxType)
}
return box, nil
}
func (boxType BoxType) GetSupportedVersions(ctx Context) ([]uint8, error) {
boxDef := boxType.getBoxDef(ctx)
if boxDef == nil {
return nil, ErrBoxInfoNotFound
}
return boxDef.versions, nil
}
func (boxType BoxType) IsSupportedVersion(ver uint8, ctx Context) bool {
boxDef := boxType.getBoxDef(ctx)
if boxDef == nil {
return false
}
if len(boxDef.versions) == 0 {
return true
}
for _, sver := range boxDef.versions {
if ver == sver {
return true
}
}
return false
}

View File

@ -1,684 +0,0 @@
package mp4
import (
"bytes"
"errors"
"io"
"github.com/abema/go-mp4/internal/bitio"
)
type ProbeInfo struct {
MajorBrand [4]byte
MinorVersion uint32
CompatibleBrands [][4]byte
FastStart bool
Timescale uint32
Duration uint64
Tracks Tracks
Segments Segments
}
// Deprecated: replace with ProbeInfo
type FraProbeInfo = ProbeInfo
type Tracks []*Track
// Deprecated: replace with Track
type TrackInfo = Track
type Track struct {
TrackID uint32
Timescale uint32
Duration uint64
Codec Codec
Encrypted bool
EditList EditList
Samples Samples
Chunks Chunks
AVC *AVCDecConfigInfo
MP4A *MP4AInfo
}
type Codec int
const (
CodecUnknown Codec = iota
CodecAVC1
CodecMP4A
)
type EditList []*EditListEntry
type EditListEntry struct {
MediaTime int64
SegmentDuration uint64
}
type Samples []*Sample
type Sample struct {
Size uint32
TimeDelta uint32
CompositionTimeOffset int64
}
type Chunks []*Chunk
type Chunk struct {
DataOffset uint64
SamplesPerChunk uint32
}
type AVCDecConfigInfo struct {
ConfigurationVersion uint8
Profile uint8
ProfileCompatibility uint8
Level uint8
LengthSize uint16
Width uint16
Height uint16
}
type MP4AInfo struct {
OTI uint8
AudOTI uint8
ChannelCount uint16
}
type Segments []*Segment
// Deprecated: replace with Segment
type SegmentInfo = Segment
type Segment struct {
TrackID uint32
MoofOffset uint64
BaseMediaDecodeTime uint64
DefaultSampleDuration uint32
SampleCount uint32
Duration uint32
CompositionTimeOffset int32
Size uint32
}
// Probe probes MP4 file
func Probe(r io.ReadSeeker) (*ProbeInfo, error) {
probeInfo := &ProbeInfo{
Tracks: make([]*Track, 0, 8),
Segments: make([]*Segment, 0, 8),
}
bis, err := ExtractBoxes(r, nil, []BoxPath{
{BoxTypeFtyp()},
{BoxTypeMoov()},
{BoxTypeMoov(), BoxTypeMvhd()},
{BoxTypeMoov(), BoxTypeTrak()},
{BoxTypeMoof()},
{BoxTypeMdat()},
})
if err != nil {
return nil, err
}
var mdatAppeared bool
for _, bi := range bis {
switch bi.Type {
case BoxTypeFtyp():
var ftyp Ftyp
if _, err := bi.SeekToPayload(r); err != nil {
return nil, err
}
if _, err := Unmarshal(r, bi.Size-bi.HeaderSize, &ftyp, bi.Context); err != nil {
return nil, err
}
probeInfo.MajorBrand = ftyp.MajorBrand
probeInfo.MinorVersion = ftyp.MinorVersion
probeInfo.CompatibleBrands = make([][4]byte, 0, len(ftyp.CompatibleBrands))
for _, entry := range ftyp.CompatibleBrands {
probeInfo.CompatibleBrands = append(probeInfo.CompatibleBrands, entry.CompatibleBrand)
}
case BoxTypeMoov():
probeInfo.FastStart = !mdatAppeared
case BoxTypeMvhd():
var mvhd Mvhd
if _, err := bi.SeekToPayload(r); err != nil {
return nil, err
}
if _, err := Unmarshal(r, bi.Size-bi.HeaderSize, &mvhd, bi.Context); err != nil {
return nil, err
}
probeInfo.Timescale = mvhd.Timescale
if mvhd.GetVersion() == 0 {
probeInfo.Duration = uint64(mvhd.DurationV0)
} else {
probeInfo.Duration = mvhd.DurationV1
}
case BoxTypeTrak():
track, err := probeTrak(r, bi)
if err != nil {
return nil, err
}
probeInfo.Tracks = append(probeInfo.Tracks, track)
case BoxTypeMoof():
segment, err := probeMoof(r, bi)
if err != nil {
return nil, err
}
probeInfo.Segments = append(probeInfo.Segments, segment)
case BoxTypeMdat():
mdatAppeared = true
}
}
return probeInfo, nil
}
// ProbeFra probes fragmented MP4 file
// Deprecated: replace with Probe
func ProbeFra(r io.ReadSeeker) (*FraProbeInfo, error) {
probeInfo, err := Probe(r)
return (*FraProbeInfo)(probeInfo), err
}
func probeTrak(r io.ReadSeeker, bi *BoxInfo) (*Track, error) {
track := new(Track)
bips, err := ExtractBoxesWithPayload(r, bi, []BoxPath{
{BoxTypeTkhd()},
{BoxTypeEdts(), BoxTypeElst()},
{BoxTypeMdia(), BoxTypeMdhd()},
{BoxTypeMdia(), BoxTypeMinf(), BoxTypeStbl(), BoxTypeStsd(), BoxTypeAvc1()},
{BoxTypeMdia(), BoxTypeMinf(), BoxTypeStbl(), BoxTypeStsd(), BoxTypeAvc1(), BoxTypeAvcC()},
{BoxTypeMdia(), BoxTypeMinf(), BoxTypeStbl(), BoxTypeStsd(), BoxTypeEncv()},
{BoxTypeMdia(), BoxTypeMinf(), BoxTypeStbl(), BoxTypeStsd(), BoxTypeEncv(), BoxTypeAvcC()},
{BoxTypeMdia(), BoxTypeMinf(), BoxTypeStbl(), BoxTypeStsd(), BoxTypeMp4a()},
{BoxTypeMdia(), BoxTypeMinf(), BoxTypeStbl(), BoxTypeStsd(), BoxTypeMp4a(), BoxTypeEsds()},
{BoxTypeMdia(), BoxTypeMinf(), BoxTypeStbl(), BoxTypeStsd(), BoxTypeMp4a(), BoxTypeWave(), BoxTypeEsds()},
{BoxTypeMdia(), BoxTypeMinf(), BoxTypeStbl(), BoxTypeStsd(), BoxTypeEnca()},
{BoxTypeMdia(), BoxTypeMinf(), BoxTypeStbl(), BoxTypeStsd(), BoxTypeEnca(), BoxTypeEsds()},
{BoxTypeMdia(), BoxTypeMinf(), BoxTypeStbl(), BoxTypeStco()},
{BoxTypeMdia(), BoxTypeMinf(), BoxTypeStbl(), BoxTypeCo64()},
{BoxTypeMdia(), BoxTypeMinf(), BoxTypeStbl(), BoxTypeStts()},
{BoxTypeMdia(), BoxTypeMinf(), BoxTypeStbl(), BoxTypeCtts()},
{BoxTypeMdia(), BoxTypeMinf(), BoxTypeStbl(), BoxTypeStsc()},
{BoxTypeMdia(), BoxTypeMinf(), BoxTypeStbl(), BoxTypeStsz()},
})
if err != nil {
return nil, err
}
var tkhd *Tkhd
var elst *Elst
var mdhd *Mdhd
var avc1 *VisualSampleEntry
var avcC *AVCDecoderConfiguration
var audioSampleEntry *AudioSampleEntry
var esds *Esds
var stco *Stco
var stts *Stts
var stsc *Stsc
var ctts *Ctts
var stsz *Stsz
var co64 *Co64
for _, bip := range bips {
switch bip.Info.Type {
case BoxTypeTkhd():
tkhd = bip.Payload.(*Tkhd)
case BoxTypeElst():
elst = bip.Payload.(*Elst)
case BoxTypeMdhd():
mdhd = bip.Payload.(*Mdhd)
case BoxTypeAvc1():
track.Codec = CodecAVC1
avc1 = bip.Payload.(*VisualSampleEntry)
case BoxTypeAvcC():
avcC = bip.Payload.(*AVCDecoderConfiguration)
case BoxTypeEncv():
track.Codec = CodecAVC1
track.Encrypted = true
case BoxTypeMp4a():
track.Codec = CodecMP4A
audioSampleEntry = bip.Payload.(*AudioSampleEntry)
case BoxTypeEnca():
track.Codec = CodecMP4A
track.Encrypted = true
audioSampleEntry = bip.Payload.(*AudioSampleEntry)
case BoxTypeEsds():
esds = bip.Payload.(*Esds)
case BoxTypeStco():
stco = bip.Payload.(*Stco)
case BoxTypeStts():
stts = bip.Payload.(*Stts)
case BoxTypeStsc():
stsc = bip.Payload.(*Stsc)
case BoxTypeCtts():
ctts = bip.Payload.(*Ctts)
case BoxTypeStsz():
stsz = bip.Payload.(*Stsz)
case BoxTypeCo64():
co64 = bip.Payload.(*Co64)
}
}
if tkhd == nil {
return nil, errors.New("tkhd box not found")
}
track.TrackID = tkhd.TrackID
if elst != nil {
editList := make([]*EditListEntry, 0, len(elst.Entries))
for i := range elst.Entries {
editList = append(editList, &EditListEntry{
MediaTime: elst.GetMediaTime(i),
SegmentDuration: elst.GetSegmentDuration(i),
})
}
track.EditList = editList
}
if mdhd == nil {
return nil, errors.New("mdhd box not found")
}
track.Timescale = mdhd.Timescale
track.Duration = mdhd.GetDuration()
if avc1 != nil && avcC != nil {
track.AVC = &AVCDecConfigInfo{
ConfigurationVersion: avcC.ConfigurationVersion,
Profile: avcC.Profile,
ProfileCompatibility: avcC.ProfileCompatibility,
Level: avcC.Level,
LengthSize: uint16(avcC.LengthSizeMinusOne) + 1,
Width: avc1.Width,
Height: avc1.Height,
}
}
if audioSampleEntry != nil && esds != nil {
oti, audOTI, err := detectAACProfile(esds)
if err != nil {
return nil, err
}
track.MP4A = &MP4AInfo{
OTI: oti,
AudOTI: audOTI,
ChannelCount: audioSampleEntry.ChannelCount,
}
}
track.Chunks = make([]*Chunk, 0)
if stco != nil {
for _, offset := range stco.ChunkOffset {
track.Chunks = append(track.Chunks, &Chunk{
DataOffset: uint64(offset),
})
}
} else if co64 != nil {
for _, offset := range co64.ChunkOffset {
track.Chunks = append(track.Chunks, &Chunk{
DataOffset: offset,
})
}
} else {
return nil, errors.New("stco/co64 box not found")
}
if stts == nil {
return nil, errors.New("stts box not found")
}
track.Samples = make([]*Sample, 0)
for _, entry := range stts.Entries {
for i := uint32(0); i < entry.SampleCount; i++ {
track.Samples = append(track.Samples, &Sample{
TimeDelta: entry.SampleDelta,
})
}
}
if stsc == nil {
return nil, errors.New("stsc box not found")
}
for si, entry := range stsc.Entries {
end := uint32(len(track.Chunks))
if si != len(stsc.Entries)-1 && stsc.Entries[si+1].FirstChunk-1 < end {
end = stsc.Entries[si+1].FirstChunk - 1
}
for ci := entry.FirstChunk - 1; ci < end; ci++ {
track.Chunks[ci].SamplesPerChunk = entry.SamplesPerChunk
}
}
if ctts != nil {
var si uint32
for ci, entry := range ctts.Entries {
for i := uint32(0); i < entry.SampleCount; i++ {
if si >= uint32(len(track.Samples)) {
break
}
track.Samples[si].CompositionTimeOffset = ctts.GetSampleOffset(ci)
si++
}
}
}
if stsz != nil {
for i := 0; i < len(stsz.EntrySize) && i < len(track.Samples); i++ {
track.Samples[i].Size = stsz.EntrySize[i]
}
}
return track, nil
}
func detectAACProfile(esds *Esds) (oti, audOTI uint8, err error) {
configDscr := findDescriptorByTag(esds.Descriptors, DecoderConfigDescrTag)
if configDscr == nil || configDscr.DecoderConfigDescriptor == nil {
return 0, 0, nil
}
if configDscr.DecoderConfigDescriptor.ObjectTypeIndication != 0x40 {
return configDscr.DecoderConfigDescriptor.ObjectTypeIndication, 0, nil
}
specificDscr := findDescriptorByTag(esds.Descriptors, DecSpecificInfoTag)
if specificDscr == nil {
return 0, 0, errors.New("DecoderSpecificationInfoDescriptor not found")
}
r := bitio.NewReader(bytes.NewReader(specificDscr.Data))
remaining := len(specificDscr.Data) * 8
// audio object type
audioObjectType, read, err := getAudioObjectType(r)
if err != nil {
return 0, 0, err
}
remaining -= read
// sampling frequency index
samplingFrequencyIndex, err := r.ReadBits(4)
if err != nil {
return 0, 0, err
}
remaining -= 4
if samplingFrequencyIndex[0] == 0x0f {
if _, err = r.ReadBits(24); err != nil {
return 0, 0, err
}
remaining -= 24
}
if audioObjectType == 2 && remaining >= 20 {
if _, err = r.ReadBits(4); err != nil {
return 0, 0, err
}
remaining -= 4
syncExtensionType, err := r.ReadBits(11)
if err != nil {
return 0, 0, err
}
remaining -= 11
if syncExtensionType[0] == 0x2 && syncExtensionType[1] == 0xb7 {
extAudioObjectType, _, err := getAudioObjectType(r)
if err != nil {
return 0, 0, err
}
if extAudioObjectType == 5 || extAudioObjectType == 22 {
sbr, err := r.ReadBits(1)
if err != nil {
return 0, 0, err
}
remaining--
if sbr[0] != 0 {
if extAudioObjectType == 5 {
sfi, err := r.ReadBits(4)
if err != nil {
return 0, 0, err
}
remaining -= 4
if sfi[0] == 0xf {
if _, err := r.ReadBits(24); err != nil {
return 0, 0, err
}
remaining -= 24
}
if remaining >= 12 {
syncExtensionType, err := r.ReadBits(11)
if err != nil {
return 0, 0, err
}
if syncExtensionType[0] == 0x5 && syncExtensionType[1] == 0x48 {
ps, err := r.ReadBits(1)
if err != nil {
return 0, 0, err
}
if ps[0] != 0 {
return 0x40, 29, nil
}
}
}
}
return 0x40, 5, nil
}
}
}
}
return 0x40, audioObjectType, nil
}
func findDescriptorByTag(dscrs []Descriptor, tag int8) *Descriptor {
for _, dscr := range dscrs {
if dscr.Tag == tag {
return &dscr
}
}
return nil
}
func getAudioObjectType(r bitio.Reader) (byte, int, error) {
audioObjectType, err := r.ReadBits(5)
if err != nil {
return 0, 0, err
}
if audioObjectType[0] != 0x1f {
return audioObjectType[0], 5, nil
}
audioObjectType, err = r.ReadBits(6)
if err != nil {
return 0, 0, err
}
return audioObjectType[0] + 32, 11, nil
}
func probeMoof(r io.ReadSeeker, bi *BoxInfo) (*Segment, error) {
bips, err := ExtractBoxesWithPayload(r, bi, []BoxPath{
{BoxTypeTraf(), BoxTypeTfhd()},
{BoxTypeTraf(), BoxTypeTfdt()},
{BoxTypeTraf(), BoxTypeTrun()},
})
if err != nil {
return nil, err
}
var tfhd *Tfhd
var tfdt *Tfdt
var trun *Trun
segment := &Segment{
MoofOffset: bi.Offset,
}
for _, bip := range bips {
switch bip.Info.Type {
case BoxTypeTfhd():
tfhd = bip.Payload.(*Tfhd)
case BoxTypeTfdt():
tfdt = bip.Payload.(*Tfdt)
case BoxTypeTrun():
trun = bip.Payload.(*Trun)
}
}
if tfhd == nil {
return nil, errors.New("tfhd not found")
}
segment.TrackID = tfhd.TrackID
segment.DefaultSampleDuration = tfhd.DefaultSampleDuration
if tfdt != nil {
if tfdt.Version == 0 {
segment.BaseMediaDecodeTime = uint64(tfdt.BaseMediaDecodeTimeV0)
} else {
segment.BaseMediaDecodeTime = tfdt.BaseMediaDecodeTimeV1
}
}
if trun != nil {
segment.SampleCount = trun.SampleCount
if trun.CheckFlag(0x000100) {
segment.Duration = 0
for ei := range trun.Entries {
segment.Duration += trun.Entries[ei].SampleDuration
}
} else {
segment.Duration = tfhd.DefaultSampleDuration * segment.SampleCount
}
if trun.CheckFlag(0x000200) {
segment.Size = 0
for ei := range trun.Entries {
segment.Size += trun.Entries[ei].SampleSize
}
} else {
segment.Size = tfhd.DefaultSampleSize * segment.SampleCount
}
var duration uint32
for ei := range trun.Entries {
offset := int32(duration) + int32(trun.GetSampleCompositionTimeOffset(ei))
if ei == 0 || offset < segment.CompositionTimeOffset {
segment.CompositionTimeOffset = offset
}
if trun.CheckFlag(0x000100) {
duration += trun.Entries[ei].SampleDuration
} else {
duration += tfhd.DefaultSampleDuration
}
}
}
return segment, nil
}
func FindIDRFrames(r io.ReadSeeker, trackInfo *TrackInfo) ([]int, error) {
if trackInfo.AVC == nil {
return nil, nil
}
lengthSize := uint32(trackInfo.AVC.LengthSize)
var si int
idxs := make([]int, 0, 8)
for _, chunk := range trackInfo.Chunks {
end := si + int(chunk.SamplesPerChunk)
dataOffset := chunk.DataOffset
for ; si < end && si < len(trackInfo.Samples); si++ {
sample := trackInfo.Samples[si]
if sample.Size == 0 {
continue
}
for nalOffset := uint32(0); nalOffset+lengthSize+1 <= sample.Size; {
if _, err := r.Seek(int64(dataOffset+uint64(nalOffset)), io.SeekStart); err != nil {
return nil, err
}
data := make([]byte, lengthSize+1)
if _, err := io.ReadFull(r, data); err != nil {
return nil, err
}
var length uint32
for i := 0; i < int(lengthSize); i++ {
length = (length << 8) + uint32(data[i])
}
nalHeader := data[lengthSize]
nalType := nalHeader & 0x1f
if nalType == 5 {
idxs = append(idxs, si)
break
}
nalOffset += lengthSize + length
}
dataOffset += uint64(sample.Size)
}
}
return idxs, nil
}
func (samples Samples) GetBitrate(timescale uint32) uint64 {
var totalSize uint64
var totalDuration uint64
for _, sample := range samples {
totalSize += uint64(sample.Size)
totalDuration += uint64(sample.TimeDelta)
}
if totalDuration == 0 {
return 0
}
return 8 * totalSize * uint64(timescale) / totalDuration
}
func (samples Samples) GetMaxBitrate(timescale uint32, timeDelta uint64) uint64 {
if timeDelta == 0 {
return 0
}
var maxBitrate uint64
var size uint64
var duration uint64
var begin int
var end int
for end < len(samples) {
for {
size += uint64(samples[end].Size)
duration += uint64(samples[end].TimeDelta)
end++
if duration >= timeDelta || end == len(samples) {
break
}
}
bitrate := 8 * size * uint64(timescale) / duration
if bitrate > maxBitrate {
maxBitrate = bitrate
}
for {
size -= uint64(samples[begin].Size)
duration -= uint64(samples[begin].TimeDelta)
begin++
if duration < timeDelta {
break
}
}
}
return maxBitrate
}
func (segments Segments) GetBitrate(trackID uint32, timescale uint32) uint64 {
var totalSize uint64
var totalDuration uint64
for _, segment := range segments {
if segment.TrackID == trackID {
totalSize += uint64(segment.Size)
totalDuration += uint64(segment.Duration)
}
}
if totalDuration == 0 {
return 0
}
return 8 * totalSize * uint64(timescale) / totalDuration
}
func (segments Segments) GetMaxBitrate(trackID uint32, timescale uint32) uint64 {
var maxBitrate uint64
for _, segment := range segments {
if segment.TrackID == trackID && segment.Duration != 0 {
bitrate := 8 * uint64(segment.Size) * uint64(timescale) / uint64(segment.Duration)
if bitrate > maxBitrate {
maxBitrate = bitrate
}
}
}
return maxBitrate
}

View File

@ -1,199 +0,0 @@
package mp4
import (
"errors"
"fmt"
"io"
)
type BoxPath []BoxType
func (lhs BoxPath) compareWith(rhs BoxPath) (forwardMatch bool, match bool) {
if len(lhs) > len(rhs) {
return false, false
}
for i := 0; i < len(lhs); i++ {
if !lhs[i].MatchWith(rhs[i]) {
return false, false
}
}
if len(lhs) < len(rhs) {
return true, false
}
return false, true
}
type ReadHandle struct {
Params []interface{}
BoxInfo BoxInfo
Path BoxPath
ReadPayload func() (box IBox, n uint64, err error)
ReadData func(io.Writer) (n uint64, err error)
Expand func(params ...interface{}) (vals []interface{}, err error)
}
type ReadHandler func(handle *ReadHandle) (val interface{}, err error)
func ReadBoxStructure(r io.ReadSeeker, handler ReadHandler, params ...interface{}) ([]interface{}, error) {
if _, err := r.Seek(0, io.SeekStart); err != nil {
return nil, err
}
return readBoxStructure(r, 0, true, nil, Context{}, handler, params)
}
func ReadBoxStructureFromInternal(r io.ReadSeeker, bi *BoxInfo, handler ReadHandler, params ...interface{}) (interface{}, error) {
return readBoxStructureFromInternal(r, bi, nil, handler, params)
}
func readBoxStructureFromInternal(r io.ReadSeeker, bi *BoxInfo, path BoxPath, handler ReadHandler, params []interface{}) (interface{}, error) {
if _, err := bi.SeekToPayload(r); err != nil {
return nil, err
}
// check comatible-brands
if len(path) == 0 && bi.Type == BoxTypeFtyp() {
var ftyp Ftyp
if _, err := Unmarshal(r, bi.Size-bi.HeaderSize, &ftyp, bi.Context); err != nil {
return nil, err
}
if ftyp.HasCompatibleBrand(BrandQT()) {
bi.IsQuickTimeCompatible = true
}
if _, err := bi.SeekToPayload(r); err != nil {
return nil, err
}
}
// parse numbered ilst items after keys box by saving EntryCount field to context
if bi.Type == BoxTypeKeys() {
var keys Keys
if _, err := Unmarshal(r, bi.Size-bi.HeaderSize, &keys, bi.Context); err != nil {
return nil, err
}
bi.QuickTimeKeysMetaEntryCount = int(keys.EntryCount)
if _, err := bi.SeekToPayload(r); err != nil {
return nil, err
}
}
ctx := bi.Context
if bi.Type == BoxTypeWave() {
ctx.UnderWave = true
} else if bi.Type == BoxTypeIlst() {
ctx.UnderIlst = true
} else if bi.UnderIlst && !bi.UnderIlstMeta && IsIlstMetaBoxType(bi.Type) {
ctx.UnderIlstMeta = true
if bi.Type == StrToBoxType("----") {
ctx.UnderIlstFreeMeta = true
}
} else if bi.Type == BoxTypeUdta() {
ctx.UnderUdta = true
}
newPath := make(BoxPath, len(path)+1)
copy(newPath, path)
newPath[len(path)] = bi.Type
h := &ReadHandle{
Params: params,
BoxInfo: *bi,
Path: newPath,
}
var childrenOffset uint64
h.ReadPayload = func() (IBox, uint64, error) {
if _, err := bi.SeekToPayload(r); err != nil {
return nil, 0, err
}
box, n, err := UnmarshalAny(r, bi.Type, bi.Size-bi.HeaderSize, bi.Context)
if err != nil {
return nil, 0, err
}
childrenOffset = bi.Offset + bi.HeaderSize + n
return box, n, nil
}
h.ReadData = func(w io.Writer) (uint64, error) {
if _, err := bi.SeekToPayload(r); err != nil {
return 0, err
}
size := bi.Size - bi.HeaderSize
if _, err := io.CopyN(w, r, int64(size)); err != nil {
return 0, err
}
return size, nil
}
h.Expand = func(params ...interface{}) ([]interface{}, error) {
if childrenOffset == 0 {
if _, err := bi.SeekToPayload(r); err != nil {
return nil, err
}
_, n, err := UnmarshalAny(r, bi.Type, bi.Size-bi.HeaderSize, bi.Context)
if err != nil {
return nil, err
}
childrenOffset = bi.Offset + bi.HeaderSize + n
} else {
if _, err := r.Seek(int64(childrenOffset), io.SeekStart); err != nil {
return nil, err
}
}
childrenSize := bi.Offset + bi.Size - childrenOffset
return readBoxStructure(r, childrenSize, false, newPath, ctx, handler, params)
}
if val, err := handler(h); err != nil {
return nil, err
} else if _, err := bi.SeekToEnd(r); err != nil {
return nil, err
} else {
return val, nil
}
}
func readBoxStructure(r io.ReadSeeker, totalSize uint64, isRoot bool, path BoxPath, ctx Context, handler ReadHandler, params []interface{}) ([]interface{}, error) {
vals := make([]interface{}, 0, 8)
for isRoot || totalSize >= SmallHeaderSize {
bi, err := ReadBoxInfo(r)
if isRoot && err == io.EOF {
return vals, nil
} else if err != nil {
return nil, err
}
if !isRoot && bi.Size > totalSize {
return nil, fmt.Errorf("too large box size: type=%s, size=%d, actualBufSize=%d", bi.Type.String(), bi.Size, totalSize)
}
totalSize -= bi.Size
bi.Context = ctx
val, err := readBoxStructureFromInternal(r, bi, path, handler, params)
if err != nil {
return nil, err
}
vals = append(vals, val)
if bi.IsQuickTimeCompatible {
ctx.IsQuickTimeCompatible = true
}
// preserve keys entry count on context for subsequent ilst number item box
if bi.Type == BoxTypeKeys() {
ctx.QuickTimeKeysMetaEntryCount = bi.QuickTimeKeysMetaEntryCount
}
}
if totalSize != 0 && !ctx.IsQuickTimeCompatible {
return nil, errors.New("Unexpected EOF")
}
return vals, nil
}

View File

@ -1,261 +0,0 @@
package mp4
import (
"bytes"
"fmt"
"io"
"reflect"
"strconv"
"github.com/abema/go-mp4/internal/util"
)
type stringifier struct {
buf *bytes.Buffer
src IImmutableBox
indent string
ctx Context
}
func Stringify(src IImmutableBox, ctx Context) (string, error) {
return StringifyWithIndent(src, "", ctx)
}
func StringifyWithIndent(src IImmutableBox, indent string, ctx Context) (string, error) {
boxDef := src.GetType().getBoxDef(ctx)
if boxDef == nil {
return "", ErrBoxInfoNotFound
}
v := reflect.ValueOf(src).Elem()
m := &stringifier{
buf: bytes.NewBuffer(nil),
src: src,
indent: indent,
ctx: ctx,
}
err := m.stringifyStruct(v, boxDef.fields, 0, true)
if err != nil {
return "", err
}
return m.buf.String(), nil
}
func (m *stringifier) stringify(v reflect.Value, fi *fieldInstance, depth int) error {
switch v.Type().Kind() {
case reflect.Ptr:
return m.stringifyPtr(v, fi, depth)
case reflect.Struct:
return m.stringifyStruct(v, fi.children, depth, fi.is(fieldExtend))
case reflect.Array:
return m.stringifyArray(v, fi, depth)
case reflect.Slice:
return m.stringifySlice(v, fi, depth)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return m.stringifyInt(v, fi, depth)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return m.stringifyUint(v, fi, depth)
case reflect.Bool:
return m.stringifyBool(v, depth)
case reflect.String:
return m.stringifyString(v, depth)
default:
return fmt.Errorf("unsupported type: %s", v.Type().Kind())
}
}
func (m *stringifier) stringifyPtr(v reflect.Value, fi *fieldInstance, depth int) error {
return m.stringify(v.Elem(), fi, depth)
}
func (m *stringifier) stringifyStruct(v reflect.Value, fs []*field, depth int, extended bool) error {
if !extended {
m.buf.WriteString("{")
if m.indent != "" {
m.buf.WriteString("\n")
}
depth++
}
for _, f := range fs {
fi := resolveFieldInstance(f, m.src, v, m.ctx)
if !isTargetField(m.src, fi, m.ctx) {
continue
}
if f.cnst != "" || f.is(fieldHidden) {
continue
}
if !f.is(fieldExtend) {
if m.indent != "" {
writeIndent(m.buf, m.indent, depth+1)
} else if m.buf.Len() != 0 && m.buf.Bytes()[m.buf.Len()-1] != '{' {
m.buf.WriteString(" ")
}
m.buf.WriteString(f.name)
m.buf.WriteString("=")
}
str, ok := fi.cfo.StringifyField(f.name, m.indent, depth+1, m.ctx)
if ok {
m.buf.WriteString(str)
if !f.is(fieldExtend) && m.indent != "" {
m.buf.WriteString("\n")
}
continue
}
if f.name == "Version" {
m.buf.WriteString(strconv.Itoa(int(m.src.GetVersion())))
} else if f.name == "Flags" {
fmt.Fprintf(m.buf, "0x%06x", m.src.GetFlags())
} else {
err := m.stringify(v.FieldByName(f.name), fi, depth)
if err != nil {
return err
}
}
if !f.is(fieldExtend) && m.indent != "" {
m.buf.WriteString("\n")
}
}
if !extended {
if m.indent != "" {
writeIndent(m.buf, m.indent, depth)
}
m.buf.WriteString("}")
}
return nil
}
func (m *stringifier) stringifyArray(v reflect.Value, fi *fieldInstance, depth int) error {
begin, sep, end := "[", ", ", "]"
if fi.is(fieldString) || fi.is(fieldISO639_2) {
begin, sep, end = "\"", "", "\""
} else if fi.is(fieldUUID) {
begin, sep, end = "", "", ""
}
m.buf.WriteString(begin)
m2 := *m
if fi.is(fieldString) {
m2.buf = bytes.NewBuffer(nil)
}
size := v.Type().Size()
for i := 0; i < int(size)/int(v.Type().Elem().Size()); i++ {
if i != 0 {
m2.buf.WriteString(sep)
}
if err := m2.stringify(v.Index(i), fi, depth+1); err != nil {
return err
}
if fi.is(fieldUUID) && (i == 3 || i == 5 || i == 7 || i == 9) {
m.buf.WriteString("-")
}
}
if fi.is(fieldString) {
m.buf.WriteString(util.EscapeUnprintables(m2.buf.String()))
}
m.buf.WriteString(end)
return nil
}
func (m *stringifier) stringifySlice(v reflect.Value, fi *fieldInstance, depth int) error {
begin, sep, end := "[", ", ", "]"
if fi.is(fieldString) || fi.is(fieldISO639_2) {
begin, sep, end = "\"", "", "\""
}
m.buf.WriteString(begin)
m2 := *m
if fi.is(fieldString) {
m2.buf = bytes.NewBuffer(nil)
}
for i := 0; i < v.Len(); i++ {
if fi.length != LengthUnlimited && uint(i) >= fi.length {
break
}
if i != 0 {
m2.buf.WriteString(sep)
}
if err := m2.stringify(v.Index(i), fi, depth+1); err != nil {
return err
}
}
if fi.is(fieldString) {
m.buf.WriteString(util.EscapeUnprintables(m2.buf.String()))
}
m.buf.WriteString(end)
return nil
}
func (m *stringifier) stringifyInt(v reflect.Value, fi *fieldInstance, depth int) error {
if fi.is(fieldHex) {
val := v.Int()
if val >= 0 {
m.buf.WriteString("0x")
m.buf.WriteString(strconv.FormatInt(val, 16))
} else {
m.buf.WriteString("-0x")
m.buf.WriteString(strconv.FormatInt(-val, 16))
}
} else {
m.buf.WriteString(strconv.FormatInt(v.Int(), 10))
}
return nil
}
func (m *stringifier) stringifyUint(v reflect.Value, fi *fieldInstance, depth int) error {
if fi.is(fieldISO639_2) {
m.buf.WriteString(string([]byte{byte(v.Uint() + 0x60)}))
} else if fi.is(fieldUUID) {
fmt.Fprintf(m.buf, "%02x", v.Uint())
} else if fi.is(fieldString) {
m.buf.WriteString(string([]byte{byte(v.Uint())}))
} else if fi.is(fieldHex) || (!fi.is(fieldDec) && v.Type().Kind() == reflect.Uint8) || v.Type().Kind() == reflect.Uintptr {
m.buf.WriteString("0x")
m.buf.WriteString(strconv.FormatUint(v.Uint(), 16))
} else {
m.buf.WriteString(strconv.FormatUint(v.Uint(), 10))
}
return nil
}
func (m *stringifier) stringifyBool(v reflect.Value, depth int) error {
m.buf.WriteString(strconv.FormatBool(v.Bool()))
return nil
}
func (m *stringifier) stringifyString(v reflect.Value, depth int) error {
m.buf.WriteString("\"")
m.buf.WriteString(util.EscapeUnprintables(v.String()))
m.buf.WriteString("\"")
return nil
}
func writeIndent(w io.Writer, indent string, depth int) {
for i := 0; i < depth; i++ {
io.WriteString(w, indent)
}
}

View File

@ -1,68 +0,0 @@
package mp4
import (
"errors"
"io"
)
type Writer struct {
writer io.WriteSeeker
biStack []*BoxInfo
}
func NewWriter(w io.WriteSeeker) *Writer {
return &Writer{
writer: w,
}
}
func (w *Writer) Write(p []byte) (int, error) {
return w.writer.Write(p)
}
func (w *Writer) Seek(offset int64, whence int) (int64, error) {
return w.writer.Seek(offset, whence)
}
func (w *Writer) StartBox(bi *BoxInfo) (*BoxInfo, error) {
bi, err := WriteBoxInfo(w.writer, bi)
if err != nil {
return nil, err
}
w.biStack = append(w.biStack, bi)
return bi, nil
}
func (w *Writer) EndBox() (*BoxInfo, error) {
bi := w.biStack[len(w.biStack)-1]
w.biStack = w.biStack[:len(w.biStack)-1]
end, err := w.writer.Seek(0, io.SeekCurrent)
if err != nil {
return nil, err
}
bi.Size = uint64(end) - bi.Offset
if _, err = bi.SeekToStart(w.writer); err != nil {
return nil, err
}
if bi2, err := WriteBoxInfo(w.writer, bi); err != nil {
return nil, err
} else if bi.HeaderSize != bi2.HeaderSize {
return nil, errors.New("header size changed")
}
if _, err := w.writer.Seek(end, io.SeekStart); err != nil {
return nil, err
}
return bi, nil
}
func (w *Writer) CopyBox(r io.ReadSeeker, bi *BoxInfo) error {
if _, err := bi.SeekToStart(r); err != nil {
return err
}
if n, err := io.CopyN(w, r, int64(bi.Size)); err != nil {
return err
} else if n != int64(bi.Size) {
return errors.New("failed to copy box")
}
return nil
}

View File

View File

@ -1,9 +0,0 @@
MIT LICENSE
Copyright 2019 Dustin Oprea
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -1,651 +0,0 @@
package exifcommon
import (
"errors"
"fmt"
"strings"
"github.com/dsoprea/go-logging"
)
var (
ifdLogger = log.NewLogger("exifcommon.ifd")
)
var (
ErrChildIfdNotMapped = errors.New("no child-IFD for that tag-ID under parent")
)
// MappedIfd is one node in the IFD-mapping.
type MappedIfd struct {
ParentTagId uint16
Placement []uint16
Path []string
Name string
TagId uint16
Children map[uint16]*MappedIfd
}
// String returns a descriptive string.
func (mi *MappedIfd) String() string {
pathPhrase := mi.PathPhrase()
return fmt.Sprintf("MappedIfd<(0x%04X) [%s] PATH=[%s]>", mi.TagId, mi.Name, pathPhrase)
}
// PathPhrase returns a non-fully-qualified IFD path.
func (mi *MappedIfd) PathPhrase() string {
return strings.Join(mi.Path, "/")
}
// TODO(dustin): Refactor this to use IfdIdentity structs.
// IfdMapping describes all of the IFDs that we currently recognize.
type IfdMapping struct {
rootNode *MappedIfd
}
// NewIfdMapping returns a new IfdMapping struct.
func NewIfdMapping() (ifdMapping *IfdMapping) {
rootNode := &MappedIfd{
Path: make([]string, 0),
Children: make(map[uint16]*MappedIfd),
}
return &IfdMapping{
rootNode: rootNode,
}
}
// NewIfdMappingWithStandard retruns a new IfdMapping struct preloaded with the
// standard IFDs.
func NewIfdMappingWithStandard() (ifdMapping *IfdMapping, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
im := NewIfdMapping()
err = LoadStandardIfds(im)
log.PanicIf(err)
return im, nil
}
// Get returns the node given the path slice.
func (im *IfdMapping) Get(parentPlacement []uint16) (childIfd *MappedIfd, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
ptr := im.rootNode
for _, tagId := range parentPlacement {
if descendantPtr, found := ptr.Children[tagId]; found == false {
log.Panicf("ifd child with tag-ID (%04x) not registered: [%s]", tagId, ptr.PathPhrase())
} else {
ptr = descendantPtr
}
}
return ptr, nil
}
// GetWithPath returns the node given the path string.
func (im *IfdMapping) GetWithPath(pathPhrase string) (mi *MappedIfd, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
if pathPhrase == "" {
log.Panicf("path-phrase is empty")
}
path := strings.Split(pathPhrase, "/")
ptr := im.rootNode
for _, name := range path {
var hit *MappedIfd
for _, mi := range ptr.Children {
if mi.Name == name {
hit = mi
break
}
}
if hit == nil {
log.Panicf("ifd child with name [%s] not registered: [%s]", name, ptr.PathPhrase())
}
ptr = hit
}
return ptr, nil
}
// GetChild is a convenience function to get the child path for a given parent
// placement and child tag-ID.
func (im *IfdMapping) GetChild(parentPathPhrase string, tagId uint16) (mi *MappedIfd, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
mi, err = im.GetWithPath(parentPathPhrase)
log.PanicIf(err)
for _, childMi := range mi.Children {
if childMi.TagId == tagId {
return childMi, nil
}
}
// Whether or not an IFD is defined in data, such an IFD is not registered
// and would be unknown.
log.Panic(ErrChildIfdNotMapped)
return nil, nil
}
// IfdTagIdAndIndex represents a specific part of the IFD path.
//
// This is a legacy type.
type IfdTagIdAndIndex struct {
Name string
TagId uint16
Index int
}
// String returns a descriptive string.
func (itii IfdTagIdAndIndex) String() string {
return fmt.Sprintf("IfdTagIdAndIndex<NAME=[%s] ID=(%04x) INDEX=(%d)>", itii.Name, itii.TagId, itii.Index)
}
// ResolvePath takes a list of names, which can also be suffixed with indices
// (to identify the second, third, etc.. sibling IFD) and returns a list of
// tag-IDs and those indices.
//
// Example:
//
// - IFD/Exif/Iop
// - IFD0/Exif/Iop
//
// This is the only call that supports adding the numeric indices.
func (im *IfdMapping) ResolvePath(pathPhrase string) (lineage []IfdTagIdAndIndex, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
pathPhrase = strings.TrimSpace(pathPhrase)
if pathPhrase == "" {
log.Panicf("can not resolve empty path-phrase")
}
path := strings.Split(pathPhrase, "/")
lineage = make([]IfdTagIdAndIndex, len(path))
ptr := im.rootNode
empty := IfdTagIdAndIndex{}
for i, name := range path {
indexByte := name[len(name)-1]
index := 0
if indexByte >= '0' && indexByte <= '9' {
index = int(indexByte - '0')
name = name[:len(name)-1]
}
itii := IfdTagIdAndIndex{}
for _, mi := range ptr.Children {
if mi.Name != name {
continue
}
itii.Name = name
itii.TagId = mi.TagId
itii.Index = index
ptr = mi
break
}
if itii == empty {
log.Panicf("ifd child with name [%s] not registered: [%s]", name, pathPhrase)
}
lineage[i] = itii
}
return lineage, nil
}
// FqPathPhraseFromLineage returns the fully-qualified IFD path from the slice.
func (im *IfdMapping) FqPathPhraseFromLineage(lineage []IfdTagIdAndIndex) (fqPathPhrase string) {
fqPathParts := make([]string, len(lineage))
for i, itii := range lineage {
if itii.Index > 0 {
fqPathParts[i] = fmt.Sprintf("%s%d", itii.Name, itii.Index)
} else {
fqPathParts[i] = itii.Name
}
}
return strings.Join(fqPathParts, "/")
}
// PathPhraseFromLineage returns the non-fully-qualified IFD path from the
// slice.
func (im *IfdMapping) PathPhraseFromLineage(lineage []IfdTagIdAndIndex) (pathPhrase string) {
pathParts := make([]string, len(lineage))
for i, itii := range lineage {
pathParts[i] = itii.Name
}
return strings.Join(pathParts, "/")
}
// StripPathPhraseIndices returns a non-fully-qualified path-phrase (no
// indices).
func (im *IfdMapping) StripPathPhraseIndices(pathPhrase string) (strippedPathPhrase string, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
lineage, err := im.ResolvePath(pathPhrase)
log.PanicIf(err)
strippedPathPhrase = im.PathPhraseFromLineage(lineage)
return strippedPathPhrase, nil
}
// Add puts the given IFD at the given position of the tree. The position of the
// tree is referred to as the placement and is represented by a set of tag-IDs,
// where the leftmost is the root tag and the tags going to the right are
// progressive descendants.
func (im *IfdMapping) Add(parentPlacement []uint16, tagId uint16, name string) (err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
// TODO(dustin): !! It would be nicer to provide a list of names in the placement rather than tag-IDs.
ptr, err := im.Get(parentPlacement)
log.PanicIf(err)
path := make([]string, len(parentPlacement)+1)
if len(parentPlacement) > 0 {
copy(path, ptr.Path)
}
path[len(path)-1] = name
placement := make([]uint16, len(parentPlacement)+1)
if len(placement) > 0 {
copy(placement, ptr.Placement)
}
placement[len(placement)-1] = tagId
childIfd := &MappedIfd{
ParentTagId: ptr.TagId,
Path: path,
Placement: placement,
Name: name,
TagId: tagId,
Children: make(map[uint16]*MappedIfd),
}
if _, found := ptr.Children[tagId]; found == true {
log.Panicf("child IFD with tag-ID (%04x) already registered under IFD [%s] with tag-ID (%04x)", tagId, ptr.Name, ptr.TagId)
}
ptr.Children[tagId] = childIfd
return nil
}
func (im *IfdMapping) dumpLineages(stack []*MappedIfd, input []string) (output []string, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
currentIfd := stack[len(stack)-1]
output = input
for _, childIfd := range currentIfd.Children {
stackCopy := make([]*MappedIfd, len(stack)+1)
copy(stackCopy, stack)
stackCopy[len(stack)] = childIfd
// Add to output, but don't include the obligatory root node.
parts := make([]string, len(stackCopy)-1)
for i, mi := range stackCopy[1:] {
parts[i] = mi.Name
}
output = append(output, strings.Join(parts, "/"))
output, err = im.dumpLineages(stackCopy, output)
log.PanicIf(err)
}
return output, nil
}
// DumpLineages returns a slice of strings representing all mappings.
func (im *IfdMapping) DumpLineages() (output []string, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
stack := []*MappedIfd{im.rootNode}
output = make([]string, 0)
output, err = im.dumpLineages(stack, output)
log.PanicIf(err)
return output, nil
}
// LoadStandardIfds loads the standard IFDs into the mapping.
func LoadStandardIfds(im *IfdMapping) (err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
err = im.Add(
[]uint16{},
IfdStandardIfdIdentity.TagId(), IfdStandardIfdIdentity.Name())
log.PanicIf(err)
err = im.Add(
[]uint16{IfdStandardIfdIdentity.TagId()},
IfdExifStandardIfdIdentity.TagId(), IfdExifStandardIfdIdentity.Name())
log.PanicIf(err)
err = im.Add(
[]uint16{IfdStandardIfdIdentity.TagId(), IfdExifStandardIfdIdentity.TagId()},
IfdExifIopStandardIfdIdentity.TagId(), IfdExifIopStandardIfdIdentity.Name())
log.PanicIf(err)
err = im.Add(
[]uint16{IfdStandardIfdIdentity.TagId()},
IfdGpsInfoStandardIfdIdentity.TagId(), IfdGpsInfoStandardIfdIdentity.Name())
log.PanicIf(err)
return nil
}
// IfdTag describes a single IFD tag and its parent (if any).
type IfdTag struct {
parentIfdTag *IfdTag
tagId uint16
name string
}
func NewIfdTag(parentIfdTag *IfdTag, tagId uint16, name string) IfdTag {
return IfdTag{
parentIfdTag: parentIfdTag,
tagId: tagId,
name: name,
}
}
// ParentIfd returns the IfdTag of this IFD's parent.
func (it IfdTag) ParentIfd() *IfdTag {
return it.parentIfdTag
}
// TagId returns the tag-ID of this IFD.
func (it IfdTag) TagId() uint16 {
return it.tagId
}
// Name returns the simple name of this IFD.
func (it IfdTag) Name() string {
return it.name
}
// String returns a descriptive string.
func (it IfdTag) String() string {
parentIfdPhrase := ""
if it.parentIfdTag != nil {
parentIfdPhrase = fmt.Sprintf(" PARENT=(0x%04x)[%s]", it.parentIfdTag.tagId, it.parentIfdTag.name)
}
return fmt.Sprintf("IfdTag<TAG-ID=(0x%04x) NAME=[%s]%s>", it.tagId, it.name, parentIfdPhrase)
}
var (
// rootStandardIfd is the standard root IFD.
rootStandardIfd = NewIfdTag(nil, 0x0000, "IFD") // IFD
// exifStandardIfd is the standard "Exif" IFD.
exifStandardIfd = NewIfdTag(&rootStandardIfd, 0x8769, "Exif") // IFD/Exif
// iopStandardIfd is the standard "Iop" IFD.
iopStandardIfd = NewIfdTag(&exifStandardIfd, 0xA005, "Iop") // IFD/Exif/Iop
// gpsInfoStandardIfd is the standard "GPS" IFD.
gpsInfoStandardIfd = NewIfdTag(&rootStandardIfd, 0x8825, "GPSInfo") // IFD/GPSInfo
)
// IfdIdentityPart represents one component in an IFD path.
type IfdIdentityPart struct {
Name string
Index int
}
// String returns a fully-qualified IFD path.
func (iip IfdIdentityPart) String() string {
if iip.Index > 0 {
return fmt.Sprintf("%s%d", iip.Name, iip.Index)
} else {
return iip.Name
}
}
// UnindexedString returned a non-fully-qualified IFD path.
func (iip IfdIdentityPart) UnindexedString() string {
return iip.Name
}
// IfdIdentity represents a single IFD path and provides access to various
// information and representations.
//
// Only global instances can be used for equality checks.
type IfdIdentity struct {
ifdTag IfdTag
parts []IfdIdentityPart
ifdPath string
fqIfdPath string
}
// NewIfdIdentity returns a new IfdIdentity struct.
func NewIfdIdentity(ifdTag IfdTag, parts ...IfdIdentityPart) (ii *IfdIdentity) {
ii = &IfdIdentity{
ifdTag: ifdTag,
parts: parts,
}
ii.ifdPath = ii.getIfdPath()
ii.fqIfdPath = ii.getFqIfdPath()
return ii
}
// NewIfdIdentityFromString parses a string like "IFD/Exif" or "IFD1" or
// something more exotic with custom IFDs ("SomeIFD4/SomeChildIFD6"). Note that
// this will valid the unindexed IFD structure (because the standard tags from
// the specification are unindexed), but not, obviously, any indices (e.g.
// the numbers in "IFD0", "IFD1", "SomeIFD4/SomeChildIFD6"). It is
// required for the caller to check whether these specific instances
// were actually parsed out of the stream.
func NewIfdIdentityFromString(im *IfdMapping, fqIfdPath string) (ii *IfdIdentity, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
lineage, err := im.ResolvePath(fqIfdPath)
log.PanicIf(err)
var lastIt *IfdTag
identityParts := make([]IfdIdentityPart, len(lineage))
for i, itii := range lineage {
// Build out the tag that will eventually point to the IFD represented
// by the right-most part in the IFD path.
it := &IfdTag{
parentIfdTag: lastIt,
tagId: itii.TagId,
name: itii.Name,
}
lastIt = it
// Create the next IfdIdentity part.
iip := IfdIdentityPart{
Name: itii.Name,
Index: itii.Index,
}
identityParts[i] = iip
}
ii = NewIfdIdentity(*lastIt, identityParts...)
return ii, nil
}
func (ii *IfdIdentity) getFqIfdPath() string {
partPhrases := make([]string, len(ii.parts))
for i, iip := range ii.parts {
partPhrases[i] = iip.String()
}
return strings.Join(partPhrases, "/")
}
func (ii *IfdIdentity) getIfdPath() string {
partPhrases := make([]string, len(ii.parts))
for i, iip := range ii.parts {
partPhrases[i] = iip.UnindexedString()
}
return strings.Join(partPhrases, "/")
}
// String returns a fully-qualified IFD path.
func (ii *IfdIdentity) String() string {
return ii.fqIfdPath
}
// UnindexedString returns a non-fully-qualified IFD path.
func (ii *IfdIdentity) UnindexedString() string {
return ii.ifdPath
}
// IfdTag returns the tag struct behind this IFD.
func (ii *IfdIdentity) IfdTag() IfdTag {
return ii.ifdTag
}
// TagId returns the tag-ID of the IFD.
func (ii *IfdIdentity) TagId() uint16 {
return ii.ifdTag.TagId()
}
// LeafPathPart returns the last right-most path-part, which represents the
// current IFD.
func (ii *IfdIdentity) LeafPathPart() IfdIdentityPart {
return ii.parts[len(ii.parts)-1]
}
// Name returns the simple name of this IFD.
func (ii *IfdIdentity) Name() string {
return ii.LeafPathPart().Name
}
// Index returns the index of this IFD (more then one IFD under a parent IFD
// will be numbered [0..n]).
func (ii *IfdIdentity) Index() int {
return ii.LeafPathPart().Index
}
// Equals returns true if the two IfdIdentity instances are effectively
// identical.
//
// Since there's no way to get a specific fully-qualified IFD path without a
// certain slice of parts and all other fields are also derived from this,
// checking that the fully-qualified IFD path is equals is sufficient.
func (ii *IfdIdentity) Equals(ii2 *IfdIdentity) bool {
return ii.String() == ii2.String()
}
// NewChild creates an IfdIdentity for an IFD that is a child of the current
// IFD.
func (ii *IfdIdentity) NewChild(childIfdTag IfdTag, index int) (iiChild *IfdIdentity) {
if *childIfdTag.parentIfdTag != ii.ifdTag {
log.Panicf("can not add child; we are not the parent:\nUS=%v\nCHILD=%v", ii.ifdTag, childIfdTag)
}
childPart := IfdIdentityPart{childIfdTag.name, index}
childParts := append(ii.parts, childPart)
iiChild = NewIfdIdentity(childIfdTag, childParts...)
return iiChild
}
// NewSibling creates an IfdIdentity for an IFD that is a sibling to the current
// one.
func (ii *IfdIdentity) NewSibling(index int) (iiSibling *IfdIdentity) {
parts := make([]IfdIdentityPart, len(ii.parts))
copy(parts, ii.parts)
parts[len(parts)-1].Index = index
iiSibling = NewIfdIdentity(ii.ifdTag, parts...)
return iiSibling
}
var (
// IfdStandardIfdIdentity represents the IFD path for IFD0.
IfdStandardIfdIdentity = NewIfdIdentity(rootStandardIfd, IfdIdentityPart{"IFD", 0})
// IfdExifStandardIfdIdentity represents the IFD path for IFD0/Exif0.
IfdExifStandardIfdIdentity = IfdStandardIfdIdentity.NewChild(exifStandardIfd, 0)
// IfdExifIopStandardIfdIdentity represents the IFD path for IFD0/Exif0/Iop0.
IfdExifIopStandardIfdIdentity = IfdExifStandardIfdIdentity.NewChild(iopStandardIfd, 0)
// IfdGPSInfoStandardIfdIdentity represents the IFD path for IFD0/GPSInfo0.
IfdGpsInfoStandardIfdIdentity = IfdStandardIfdIdentity.NewChild(gpsInfoStandardIfd, 0)
// Ifd1StandardIfdIdentity represents the IFD path for IFD1.
Ifd1StandardIfdIdentity = NewIfdIdentity(rootStandardIfd, IfdIdentityPart{"IFD", 1})
)

View File

@ -1,280 +0,0 @@
package exifcommon
import (
"bytes"
"errors"
"math"
"encoding/binary"
"github.com/dsoprea/go-logging"
)
var (
parserLogger = log.NewLogger("exifcommon.parser")
)
var (
ErrParseFail = errors.New("parse failure")
)
// Parser knows how to parse all well-defined, encoded EXIF types.
type Parser struct {
}
// ParseBytesknows how to parse a byte-type value.
func (p *Parser) ParseBytes(data []byte, unitCount uint32) (value []uint8, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
// TODO(dustin): Add test
count := int(unitCount)
if len(data) < (TypeByte.Size() * count) {
log.Panic(ErrNotEnoughData)
}
value = []uint8(data[:count])
return value, nil
}
// ParseAscii returns a string and auto-strips the trailing NUL character that
// should be at the end of the encoding.
func (p *Parser) ParseAscii(data []byte, unitCount uint32) (value string, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
// TODO(dustin): Add test
count := int(unitCount)
if len(data) < (TypeAscii.Size() * count) {
log.Panic(ErrNotEnoughData)
}
if len(data) == 0 || data[count-1] != 0 {
s := string(data[:count])
parserLogger.Warningf(nil, "ASCII not terminated with NUL as expected: [%v]", s)
for i, c := range s {
if c > 127 {
// Binary
t := s[:i]
parserLogger.Warningf(nil, "ASCII also had binary characters. Truncating: [%v]->[%s]", s, t)
return t, nil
}
}
return s, nil
}
// Auto-strip the NUL from the end. It serves no purpose outside of
// encoding semantics.
return string(data[:count-1]), nil
}
// ParseAsciiNoNul returns a string without any consideration for a trailing NUL
// character.
func (p *Parser) ParseAsciiNoNul(data []byte, unitCount uint32) (value string, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
// TODO(dustin): Add test
count := int(unitCount)
if len(data) < (TypeAscii.Size() * count) {
log.Panic(ErrNotEnoughData)
}
return string(data[:count]), nil
}
// ParseShorts knows how to parse an encoded list of shorts.
func (p *Parser) ParseShorts(data []byte, unitCount uint32, byteOrder binary.ByteOrder) (value []uint16, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
// TODO(dustin): Add test
count := int(unitCount)
if len(data) < (TypeShort.Size() * count) {
log.Panic(ErrNotEnoughData)
}
value = make([]uint16, count)
for i := 0; i < count; i++ {
value[i] = byteOrder.Uint16(data[i*2:])
}
return value, nil
}
// ParseLongs knows how to encode an encoded list of unsigned longs.
func (p *Parser) ParseLongs(data []byte, unitCount uint32, byteOrder binary.ByteOrder) (value []uint32, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
// TODO(dustin): Add test
count := int(unitCount)
if len(data) < (TypeLong.Size() * count) {
log.Panic(ErrNotEnoughData)
}
value = make([]uint32, count)
for i := 0; i < count; i++ {
value[i] = byteOrder.Uint32(data[i*4:])
}
return value, nil
}
// ParseFloats knows how to encode an encoded list of floats.
func (p *Parser) ParseFloats(data []byte, unitCount uint32, byteOrder binary.ByteOrder) (value []float32, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
count := int(unitCount)
if len(data) != (TypeFloat.Size() * count) {
log.Panic(ErrNotEnoughData)
}
value = make([]float32, count)
for i := 0; i < count; i++ {
value[i] = math.Float32frombits(byteOrder.Uint32(data[i*4 : (i+1)*4]))
}
return value, nil
}
// ParseDoubles knows how to encode an encoded list of doubles.
func (p *Parser) ParseDoubles(data []byte, unitCount uint32, byteOrder binary.ByteOrder) (value []float64, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
count := int(unitCount)
if len(data) != (TypeDouble.Size() * count) {
log.Panic(ErrNotEnoughData)
}
value = make([]float64, count)
for i := 0; i < count; i++ {
value[i] = math.Float64frombits(byteOrder.Uint64(data[i*8 : (i+1)*8]))
}
return value, nil
}
// ParseRationals knows how to parse an encoded list of unsigned rationals.
func (p *Parser) ParseRationals(data []byte, unitCount uint32, byteOrder binary.ByteOrder) (value []Rational, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
// TODO(dustin): Add test
count := int(unitCount)
if len(data) < (TypeRational.Size() * count) {
log.Panic(ErrNotEnoughData)
}
value = make([]Rational, count)
for i := 0; i < count; i++ {
value[i].Numerator = byteOrder.Uint32(data[i*8:])
value[i].Denominator = byteOrder.Uint32(data[i*8+4:])
}
return value, nil
}
// ParseSignedLongs knows how to parse an encoded list of signed longs.
func (p *Parser) ParseSignedLongs(data []byte, unitCount uint32, byteOrder binary.ByteOrder) (value []int32, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
// TODO(dustin): Add test
count := int(unitCount)
if len(data) < (TypeSignedLong.Size() * count) {
log.Panic(ErrNotEnoughData)
}
b := bytes.NewBuffer(data)
value = make([]int32, count)
for i := 0; i < count; i++ {
err := binary.Read(b, byteOrder, &value[i])
log.PanicIf(err)
}
return value, nil
}
// ParseSignedRationals knows how to parse an encoded list of signed
// rationals.
func (p *Parser) ParseSignedRationals(data []byte, unitCount uint32, byteOrder binary.ByteOrder) (value []SignedRational, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
// TODO(dustin): Add test
count := int(unitCount)
if len(data) < (TypeSignedRational.Size() * count) {
log.Panic(ErrNotEnoughData)
}
b := bytes.NewBuffer(data)
value = make([]SignedRational, count)
for i := 0; i < count; i++ {
err = binary.Read(b, byteOrder, &value[i].Numerator)
log.PanicIf(err)
err = binary.Read(b, byteOrder, &value[i].Denominator)
log.PanicIf(err)
}
return value, nil
}

View File

@ -1,88 +0,0 @@
package exifcommon
import (
"os"
"path"
"encoding/binary"
"io/ioutil"
"github.com/dsoprea/go-logging"
)
var (
moduleRootPath = ""
testExifData []byte = nil
// EncodeDefaultByteOrder is the default byte-order for encoding operations.
EncodeDefaultByteOrder = binary.BigEndian
// Default byte order for tests.
TestDefaultByteOrder = binary.BigEndian
)
func GetModuleRootPath() string {
if moduleRootPath == "" {
moduleRootPath = os.Getenv("EXIF_MODULE_ROOT_PATH")
if moduleRootPath != "" {
return moduleRootPath
}
currentWd, err := os.Getwd()
log.PanicIf(err)
currentPath := currentWd
visited := make([]string, 0)
for {
tryStampFilepath := path.Join(currentPath, ".MODULE_ROOT")
_, err := os.Stat(tryStampFilepath)
if err != nil && os.IsNotExist(err) != true {
log.Panic(err)
} else if err == nil {
break
}
visited = append(visited, tryStampFilepath)
currentPath = path.Dir(currentPath)
if currentPath == "/" {
log.Panicf("could not find module-root: %v", visited)
}
}
moduleRootPath = currentPath
}
return moduleRootPath
}
func GetTestAssetsPath() string {
moduleRootPath := GetModuleRootPath()
assetsPath := path.Join(moduleRootPath, "assets")
return assetsPath
}
func getTestImageFilepath() string {
assetsPath := GetTestAssetsPath()
testImageFilepath := path.Join(assetsPath, "NDM_8901.jpg")
return testImageFilepath
}
func getTestExifData() []byte {
if testExifData == nil {
assetsPath := GetTestAssetsPath()
filepath := path.Join(assetsPath, "NDM_8901.jpg.exif")
var err error
testExifData, err = ioutil.ReadFile(filepath)
log.PanicIf(err)
}
return testExifData
}

View File

@ -1,482 +0,0 @@
package exifcommon
import (
"errors"
"fmt"
"reflect"
"strconv"
"strings"
"unicode"
"encoding/binary"
"github.com/dsoprea/go-logging"
)
var (
typeLogger = log.NewLogger("exif.type")
)
var (
// ErrNotEnoughData is used when there isn't enough data to accommodate what
// we're trying to parse (sizeof(type) * unit_count).
ErrNotEnoughData = errors.New("not enough data for type")
// ErrWrongType is used when we try to parse anything other than the
// current type.
ErrWrongType = errors.New("wrong type, can not parse")
// ErrUnhandledUndefinedTypedTag is used when we try to parse a tag that's
// recorded as an "unknown" type but not a documented tag (therefore
// leaving us not knowning how to read it).
ErrUnhandledUndefinedTypedTag = errors.New("not a standard unknown-typed tag")
)
// TagTypePrimitive is a type-alias that let's us easily lookup type properties.
type TagTypePrimitive uint16
const (
// TypeByte describes an encoded list of bytes.
TypeByte TagTypePrimitive = 1
// TypeAscii describes an encoded list of characters that is terminated
// with a NUL in its encoded form.
TypeAscii TagTypePrimitive = 2
// TypeShort describes an encoded list of shorts.
TypeShort TagTypePrimitive = 3
// TypeLong describes an encoded list of longs.
TypeLong TagTypePrimitive = 4
// TypeRational describes an encoded list of rationals.
TypeRational TagTypePrimitive = 5
// TypeUndefined describes an encoded value that has a complex/non-clearcut
// interpretation.
TypeUndefined TagTypePrimitive = 7
// We've seen type-8, but have no documentation on it.
// TypeSignedLong describes an encoded list of signed longs.
TypeSignedLong TagTypePrimitive = 9
// TypeSignedRational describes an encoded list of signed rationals.
TypeSignedRational TagTypePrimitive = 10
// TypeFloat describes an encoded list of floats
TypeFloat TagTypePrimitive = 11
// TypeDouble describes an encoded list of doubles.
TypeDouble TagTypePrimitive = 12
// TypeAsciiNoNul is just a pseudo-type, for our own purposes.
TypeAsciiNoNul TagTypePrimitive = 0xf0
)
// String returns the name of the type
func (typeType TagTypePrimitive) String() string {
return TypeNames[typeType]
}
// Size returns the size of one atomic unit of the type.
func (tagType TagTypePrimitive) Size() int {
switch tagType {
case TypeByte, TypeAscii, TypeAsciiNoNul:
return 1
case TypeShort:
return 2
case TypeLong, TypeSignedLong, TypeFloat:
return 4
case TypeRational, TypeSignedRational, TypeDouble:
return 8
default:
log.Panicf("can not determine tag-value size for type (%d): [%s]",
tagType,
TypeNames[tagType])
// Never called.
return 0
}
}
// IsValid returns true if tagType is a valid type.
func (tagType TagTypePrimitive) IsValid() bool {
// TODO(dustin): Add test
return tagType == TypeByte ||
tagType == TypeAscii ||
tagType == TypeAsciiNoNul ||
tagType == TypeShort ||
tagType == TypeLong ||
tagType == TypeRational ||
tagType == TypeSignedLong ||
tagType == TypeSignedRational ||
tagType == TypeFloat ||
tagType == TypeDouble ||
tagType == TypeUndefined
}
var (
// TODO(dustin): Rename TypeNames() to typeNames() and add getter.
TypeNames = map[TagTypePrimitive]string{
TypeByte: "BYTE",
TypeAscii: "ASCII",
TypeShort: "SHORT",
TypeLong: "LONG",
TypeRational: "RATIONAL",
TypeUndefined: "UNDEFINED",
TypeSignedLong: "SLONG",
TypeSignedRational: "SRATIONAL",
TypeFloat: "FLOAT",
TypeDouble: "DOUBLE",
TypeAsciiNoNul: "_ASCII_NO_NUL",
}
typeNamesR = map[string]TagTypePrimitive{}
)
// Rational describes an unsigned rational value.
type Rational struct {
// Numerator is the numerator of the rational value.
Numerator uint32
// Denominator is the numerator of the rational value.
Denominator uint32
}
// SignedRational describes a signed rational value.
type SignedRational struct {
// Numerator is the numerator of the rational value.
Numerator int32
// Denominator is the numerator of the rational value.
Denominator int32
}
func isPrintableText(s string) bool {
for _, c := range s {
// unicode.IsPrint() returns false for newline characters.
if c == 0x0d || c == 0x0a {
continue
} else if unicode.IsPrint(rune(c)) == false {
return false
}
}
return true
}
// Format returns a stringified value for the given encoding. Automatically
// parses. Automatically calculates count based on type size. This function
// also supports undefined-type values (the ones that we support, anyway) by
// way of the String() method that they all require. We can't be more specific
// because we're a base package and we can't refer to it.
func FormatFromType(value interface{}, justFirst bool) (phrase string, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
// TODO(dustin): !! Add test
switch t := value.(type) {
case []byte:
return DumpBytesToString(t), nil
case string:
for i, c := range t {
if c == 0 {
t = t[:i]
break
}
}
if isPrintableText(t) == false {
phrase = fmt.Sprintf("string with binary data (%d bytes)", len(t))
return phrase, nil
}
return t, nil
case []uint16, []uint32, []int32, []float64, []float32:
val := reflect.ValueOf(t)
if val.Len() == 0 {
return "", nil
}
if justFirst == true {
var valueSuffix string
if val.Len() > 1 {
valueSuffix = "..."
}
return fmt.Sprintf("%v%s", val.Index(0), valueSuffix), nil
}
return fmt.Sprintf("%v", val), nil
case []Rational:
if len(t) == 0 {
return "", nil
}
parts := make([]string, len(t))
for i, r := range t {
parts[i] = fmt.Sprintf("%d/%d", r.Numerator, r.Denominator)
if justFirst == true {
break
}
}
if justFirst == true {
var valueSuffix string
if len(t) > 1 {
valueSuffix = "..."
}
return fmt.Sprintf("%v%s", parts[0], valueSuffix), nil
}
return fmt.Sprintf("%v", parts), nil
case []SignedRational:
if len(t) == 0 {
return "", nil
}
parts := make([]string, len(t))
for i, r := range t {
parts[i] = fmt.Sprintf("%d/%d", r.Numerator, r.Denominator)
if justFirst == true {
break
}
}
if justFirst == true {
var valueSuffix string
if len(t) > 1 {
valueSuffix = "..."
}
return fmt.Sprintf("%v%s", parts[0], valueSuffix), nil
}
return fmt.Sprintf("%v", parts), nil
case fmt.Stringer:
s := t.String()
if isPrintableText(s) == false {
phrase = fmt.Sprintf("stringable with binary data (%d bytes)", len(s))
return phrase, nil
}
// An undefined value that is documented (or that we otherwise support).
return s, nil
default:
// Affects only "unknown" values, in general.
log.Panicf("type can not be formatted into string: %v", reflect.TypeOf(value).Name())
// Never called.
return "", nil
}
}
// Format returns a stringified value for the given encoding. Automatically
// parses. Automatically calculates count based on type size.
func FormatFromBytes(rawBytes []byte, tagType TagTypePrimitive, justFirst bool, byteOrder binary.ByteOrder) (phrase string, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
// TODO(dustin): !! Add test
typeSize := tagType.Size()
if len(rawBytes)%typeSize != 0 {
log.Panicf("byte-count (%d) does not align for [%s] type with a size of (%d) bytes", len(rawBytes), TypeNames[tagType], typeSize)
}
// unitCount is the calculated unit-count. This should equal the original
// value from the tag (pre-resolution).
unitCount := uint32(len(rawBytes) / typeSize)
// Truncate the items if it's not bytes or a string and we just want the first.
var value interface{}
switch tagType {
case TypeByte:
var err error
value, err = parser.ParseBytes(rawBytes, unitCount)
log.PanicIf(err)
case TypeAscii:
var err error
value, err = parser.ParseAscii(rawBytes, unitCount)
log.PanicIf(err)
case TypeAsciiNoNul:
var err error
value, err = parser.ParseAsciiNoNul(rawBytes, unitCount)
log.PanicIf(err)
case TypeShort:
var err error
value, err = parser.ParseShorts(rawBytes, unitCount, byteOrder)
log.PanicIf(err)
case TypeLong:
var err error
value, err = parser.ParseLongs(rawBytes, unitCount, byteOrder)
log.PanicIf(err)
case TypeFloat:
var err error
value, err = parser.ParseFloats(rawBytes, unitCount, byteOrder)
log.PanicIf(err)
case TypeDouble:
var err error
value, err = parser.ParseDoubles(rawBytes, unitCount, byteOrder)
log.PanicIf(err)
case TypeRational:
var err error
value, err = parser.ParseRationals(rawBytes, unitCount, byteOrder)
log.PanicIf(err)
case TypeSignedLong:
var err error
value, err = parser.ParseSignedLongs(rawBytes, unitCount, byteOrder)
log.PanicIf(err)
case TypeSignedRational:
var err error
value, err = parser.ParseSignedRationals(rawBytes, unitCount, byteOrder)
log.PanicIf(err)
default:
// Affects only "unknown" values, in general.
log.Panicf("value of type [%s] can not be formatted into string", tagType.String())
// Never called.
return "", nil
}
phrase, err = FormatFromType(value, justFirst)
log.PanicIf(err)
return phrase, nil
}
// TranslateStringToType converts user-provided strings to properly-typed
// values. If a string, returns a string. Else, assumes that it's a single
// number. If a list needs to be processed, it is the caller's responsibility to
// split it (according to whichever convention has been established).
func TranslateStringToType(tagType TagTypePrimitive, valueString string) (value interface{}, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
if tagType == TypeUndefined {
// The caller should just call String() on the decoded type.
log.Panicf("undefined-type values are not supported")
}
if tagType == TypeByte {
wide, err := strconv.ParseInt(valueString, 16, 8)
log.PanicIf(err)
return byte(wide), nil
} else if tagType == TypeAscii || tagType == TypeAsciiNoNul {
// Whether or not we're putting an NUL on the end is only relevant for
// byte-level encoding. This function really just supports a user
// interface.
return valueString, nil
} else if tagType == TypeShort {
n, err := strconv.ParseUint(valueString, 10, 16)
log.PanicIf(err)
return uint16(n), nil
} else if tagType == TypeLong {
n, err := strconv.ParseUint(valueString, 10, 32)
log.PanicIf(err)
return uint32(n), nil
} else if tagType == TypeRational {
parts := strings.SplitN(valueString, "/", 2)
numerator, err := strconv.ParseUint(parts[0], 10, 32)
log.PanicIf(err)
denominator, err := strconv.ParseUint(parts[1], 10, 32)
log.PanicIf(err)
return Rational{
Numerator: uint32(numerator),
Denominator: uint32(denominator),
}, nil
} else if tagType == TypeSignedLong {
n, err := strconv.ParseInt(valueString, 10, 32)
log.PanicIf(err)
return int32(n), nil
} else if tagType == TypeFloat {
n, err := strconv.ParseFloat(valueString, 32)
log.PanicIf(err)
return float32(n), nil
} else if tagType == TypeDouble {
n, err := strconv.ParseFloat(valueString, 64)
log.PanicIf(err)
return float64(n), nil
} else if tagType == TypeSignedRational {
parts := strings.SplitN(valueString, "/", 2)
numerator, err := strconv.ParseInt(parts[0], 10, 32)
log.PanicIf(err)
denominator, err := strconv.ParseInt(parts[1], 10, 32)
log.PanicIf(err)
return SignedRational{
Numerator: int32(numerator),
Denominator: int32(denominator),
}, nil
}
log.Panicf("from-string encoding for type not supported; this shouldn't happen: [%s]", tagType.String())
return nil, nil
}
// GetTypeByName returns the `TagTypePrimitive` for the given type name.
// Returns (0) if not valid.
func GetTypeByName(typeName string) (tagType TagTypePrimitive, found bool) {
tagType, found = typeNamesR[typeName]
return tagType, found
}
// BasicTag describes a single tag for any purpose.
type BasicTag struct {
// FqIfdPath is the fully-qualified IFD-path.
FqIfdPath string
// IfdPath is the unindexed IFD-path.
IfdPath string
// TagId is the tag-ID.
TagId uint16
}
func init() {
for typeId, typeName := range TypeNames {
typeNamesR[typeName] = typeId
}
}

View File

@ -1,148 +0,0 @@
package exifcommon
import (
"bytes"
"fmt"
"reflect"
"strconv"
"strings"
"time"
"github.com/dsoprea/go-logging"
)
var (
timeType = reflect.TypeOf(time.Time{})
)
// DumpBytes prints a list of hex-encoded bytes.
func DumpBytes(data []byte) {
fmt.Printf("DUMP: ")
for _, x := range data {
fmt.Printf("%02x ", x)
}
fmt.Printf("\n")
}
// DumpBytesClause prints a list like DumpBytes(), but encapsulated in
// "[]byte { ... }".
func DumpBytesClause(data []byte) {
fmt.Printf("DUMP: ")
fmt.Printf("[]byte { ")
for i, x := range data {
fmt.Printf("0x%02x", x)
if i < len(data)-1 {
fmt.Printf(", ")
}
}
fmt.Printf(" }\n")
}
// DumpBytesToString returns a stringified list of hex-encoded bytes.
func DumpBytesToString(data []byte) string {
b := new(bytes.Buffer)
for i, x := range data {
_, err := b.WriteString(fmt.Sprintf("%02x", x))
log.PanicIf(err)
if i < len(data)-1 {
_, err := b.WriteRune(' ')
log.PanicIf(err)
}
}
return b.String()
}
// DumpBytesClauseToString returns a comma-separated list of hex-encoded bytes.
func DumpBytesClauseToString(data []byte) string {
b := new(bytes.Buffer)
for i, x := range data {
_, err := b.WriteString(fmt.Sprintf("0x%02x", x))
log.PanicIf(err)
if i < len(data)-1 {
_, err := b.WriteString(", ")
log.PanicIf(err)
}
}
return b.String()
}
// ExifFullTimestampString produces a string like "2018:11:30 13:01:49" from a
// `time.Time` struct. It will attempt to convert to UTC first.
func ExifFullTimestampString(t time.Time) (fullTimestampPhrase string) {
t = t.UTC()
return fmt.Sprintf("%04d:%02d:%02d %02d:%02d:%02d", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())
}
// ParseExifFullTimestamp parses dates like "2018:11:30 13:01:49" into a UTC
// `time.Time` struct.
func ParseExifFullTimestamp(fullTimestampPhrase string) (timestamp time.Time, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
parts := strings.Split(fullTimestampPhrase, " ")
datestampValue, timestampValue := parts[0], parts[1]
// Normalize the separators.
datestampValue = strings.ReplaceAll(datestampValue, "-", ":")
timestampValue = strings.ReplaceAll(timestampValue, "-", ":")
dateParts := strings.Split(datestampValue, ":")
year, err := strconv.ParseUint(dateParts[0], 10, 16)
if err != nil {
log.Panicf("could not parse year")
}
month, err := strconv.ParseUint(dateParts[1], 10, 8)
if err != nil {
log.Panicf("could not parse month")
}
day, err := strconv.ParseUint(dateParts[2], 10, 8)
if err != nil {
log.Panicf("could not parse day")
}
timeParts := strings.Split(timestampValue, ":")
hour, err := strconv.ParseUint(timeParts[0], 10, 8)
if err != nil {
log.Panicf("could not parse hour")
}
minute, err := strconv.ParseUint(timeParts[1], 10, 8)
if err != nil {
log.Panicf("could not parse minute")
}
second, err := strconv.ParseUint(timeParts[2], 10, 8)
if err != nil {
log.Panicf("could not parse second")
}
timestamp = time.Date(int(year), time.Month(month), int(day), int(hour), int(minute), int(second), 0, time.UTC)
return timestamp, nil
}
// IsTime returns true if the value is a `time.Time`.
func IsTime(v interface{}) bool {
// TODO(dustin): Add test
return reflect.TypeOf(v) == timeType
}

View File

@ -1,464 +0,0 @@
package exifcommon
import (
"errors"
"io"
"encoding/binary"
"github.com/dsoprea/go-logging"
)
var (
parser *Parser
)
var (
// ErrNotFarValue indicates that an offset-based lookup was attempted for a
// non-offset-based (embedded) value.
ErrNotFarValue = errors.New("not a far value")
)
// ValueContext embeds all of the parameters required to find and extract the
// actual tag value.
type ValueContext struct {
unitCount uint32
valueOffset uint32
rawValueOffset []byte
rs io.ReadSeeker
tagType TagTypePrimitive
byteOrder binary.ByteOrder
// undefinedValueTagType is the effective type to use if this is an
// "undefined" value.
undefinedValueTagType TagTypePrimitive
ifdPath string
tagId uint16
}
// TODO(dustin): We can update newValueContext() to derive `valueOffset` itself (from `rawValueOffset`).
// NewValueContext returns a new ValueContext struct.
func NewValueContext(ifdPath string, tagId uint16, unitCount, valueOffset uint32, rawValueOffset []byte, rs io.ReadSeeker, tagType TagTypePrimitive, byteOrder binary.ByteOrder) *ValueContext {
return &ValueContext{
unitCount: unitCount,
valueOffset: valueOffset,
rawValueOffset: rawValueOffset,
rs: rs,
tagType: tagType,
byteOrder: byteOrder,
ifdPath: ifdPath,
tagId: tagId,
}
}
// SetUndefinedValueType sets the effective type if this is an unknown-type tag.
func (vc *ValueContext) SetUndefinedValueType(tagType TagTypePrimitive) {
if vc.tagType != TypeUndefined {
log.Panicf("can not set effective type for unknown-type tag because this is *not* an unknown-type tag")
}
vc.undefinedValueTagType = tagType
}
// UnitCount returns the embedded unit-count.
func (vc *ValueContext) UnitCount() uint32 {
return vc.unitCount
}
// ValueOffset returns the value-offset decoded as a `uint32`.
func (vc *ValueContext) ValueOffset() uint32 {
return vc.valueOffset
}
// RawValueOffset returns the uninterpreted value-offset. This is used for
// embedded values (values small enough to fit within the offset bytes rather
// than needing to be stored elsewhere and referred to by an actual offset).
func (vc *ValueContext) RawValueOffset() []byte {
return vc.rawValueOffset
}
// AddressableData returns the block of data that we can dereference into.
func (vc *ValueContext) AddressableData() io.ReadSeeker {
// RELEASE)dustin): Rename from AddressableData() to ReadSeeker()
return vc.rs
}
// ByteOrder returns the byte-order of numbers.
func (vc *ValueContext) ByteOrder() binary.ByteOrder {
return vc.byteOrder
}
// IfdPath returns the path of the IFD containing this tag.
func (vc *ValueContext) IfdPath() string {
return vc.ifdPath
}
// TagId returns the ID of the tag that we represent.
func (vc *ValueContext) TagId() uint16 {
return vc.tagId
}
// isEmbedded returns whether the value is embedded or a reference. This can't
// be precalculated since the size is not defined for all types (namely the
// "undefined" types).
func (vc *ValueContext) isEmbedded() bool {
tagType := vc.effectiveValueType()
return (tagType.Size() * int(vc.unitCount)) <= 4
}
// SizeInBytes returns the number of bytes that this value requires. The
// underlying call will panic if the type is UNDEFINED. It is the
// responsibility of the caller to preemptively check that.
func (vc *ValueContext) SizeInBytes() int {
tagType := vc.effectiveValueType()
return tagType.Size() * int(vc.unitCount)
}
// effectiveValueType returns the effective type of the unknown-type tag or, if
// not unknown, the actual type.
func (vc *ValueContext) effectiveValueType() (tagType TagTypePrimitive) {
if vc.tagType == TypeUndefined {
tagType = vc.undefinedValueTagType
if tagType == 0 {
log.Panicf("undefined-value type not set")
}
} else {
tagType = vc.tagType
}
return tagType
}
// readRawEncoded returns the encoded bytes for the value that we represent.
func (vc *ValueContext) readRawEncoded() (rawBytes []byte, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
tagType := vc.effectiveValueType()
unitSizeRaw := uint32(tagType.Size())
if vc.isEmbedded() == true {
byteLength := unitSizeRaw * vc.unitCount
return vc.rawValueOffset[:byteLength], nil
}
_, err = vc.rs.Seek(int64(vc.valueOffset), io.SeekStart)
log.PanicIf(err)
rawBytes = make([]byte, vc.unitCount*unitSizeRaw)
_, err = io.ReadFull(vc.rs, rawBytes)
log.PanicIf(err)
return rawBytes, nil
}
// GetFarOffset returns the offset if the value is not embedded [within the
// pointer itself] or an error if an embedded value.
func (vc *ValueContext) GetFarOffset() (offset uint32, err error) {
if vc.isEmbedded() == true {
return 0, ErrNotFarValue
}
return vc.valueOffset, nil
}
// ReadRawEncoded returns the encoded bytes for the value that we represent.
func (vc *ValueContext) ReadRawEncoded() (rawBytes []byte, err error) {
// TODO(dustin): Remove this method and rename readRawEncoded in its place.
return vc.readRawEncoded()
}
// Format returns a string representation for the value.
//
// Where the type is not ASCII, `justFirst` indicates whether to just stringify
// the first item in the slice (or return an empty string if the slice is
// empty).
//
// Since this method lacks the information to process undefined-type tags (e.g.
// byte-order, tag-ID, IFD type), it will return an error if attempted. See
// `Undefined()`.
func (vc *ValueContext) Format() (value string, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
rawBytes, err := vc.readRawEncoded()
log.PanicIf(err)
phrase, err := FormatFromBytes(rawBytes, vc.effectiveValueType(), false, vc.byteOrder)
log.PanicIf(err)
return phrase, nil
}
// FormatFirst is similar to `Format` but only gets and stringifies the first
// item.
func (vc *ValueContext) FormatFirst() (value string, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
rawBytes, err := vc.readRawEncoded()
log.PanicIf(err)
phrase, err := FormatFromBytes(rawBytes, vc.tagType, true, vc.byteOrder)
log.PanicIf(err)
return phrase, nil
}
// ReadBytes parses the encoded byte-array from the value-context.
func (vc *ValueContext) ReadBytes() (value []byte, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
rawValue, err := vc.readRawEncoded()
log.PanicIf(err)
value, err = parser.ParseBytes(rawValue, vc.unitCount)
log.PanicIf(err)
return value, nil
}
// ReadAscii parses the encoded NUL-terminated ASCII string from the value-
// context.
func (vc *ValueContext) ReadAscii() (value string, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
rawValue, err := vc.readRawEncoded()
log.PanicIf(err)
value, err = parser.ParseAscii(rawValue, vc.unitCount)
log.PanicIf(err)
return value, nil
}
// ReadAsciiNoNul parses the non-NUL-terminated encoded ASCII string from the
// value-context.
func (vc *ValueContext) ReadAsciiNoNul() (value string, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
rawValue, err := vc.readRawEncoded()
log.PanicIf(err)
value, err = parser.ParseAsciiNoNul(rawValue, vc.unitCount)
log.PanicIf(err)
return value, nil
}
// ReadShorts parses the list of encoded shorts from the value-context.
func (vc *ValueContext) ReadShorts() (value []uint16, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
rawValue, err := vc.readRawEncoded()
log.PanicIf(err)
value, err = parser.ParseShorts(rawValue, vc.unitCount, vc.byteOrder)
log.PanicIf(err)
return value, nil
}
// ReadLongs parses the list of encoded, unsigned longs from the value-context.
func (vc *ValueContext) ReadLongs() (value []uint32, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
rawValue, err := vc.readRawEncoded()
log.PanicIf(err)
value, err = parser.ParseLongs(rawValue, vc.unitCount, vc.byteOrder)
log.PanicIf(err)
return value, nil
}
// ReadFloats parses the list of encoded, floats from the value-context.
func (vc *ValueContext) ReadFloats() (value []float32, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
rawValue, err := vc.readRawEncoded()
log.PanicIf(err)
value, err = parser.ParseFloats(rawValue, vc.unitCount, vc.byteOrder)
log.PanicIf(err)
return value, nil
}
// ReadDoubles parses the list of encoded, doubles from the value-context.
func (vc *ValueContext) ReadDoubles() (value []float64, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
rawValue, err := vc.readRawEncoded()
log.PanicIf(err)
value, err = parser.ParseDoubles(rawValue, vc.unitCount, vc.byteOrder)
log.PanicIf(err)
return value, nil
}
// ReadRationals parses the list of encoded, unsigned rationals from the value-
// context.
func (vc *ValueContext) ReadRationals() (value []Rational, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
rawValue, err := vc.readRawEncoded()
log.PanicIf(err)
value, err = parser.ParseRationals(rawValue, vc.unitCount, vc.byteOrder)
log.PanicIf(err)
return value, nil
}
// ReadSignedLongs parses the list of encoded, signed longs from the value-context.
func (vc *ValueContext) ReadSignedLongs() (value []int32, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
rawValue, err := vc.readRawEncoded()
log.PanicIf(err)
value, err = parser.ParseSignedLongs(rawValue, vc.unitCount, vc.byteOrder)
log.PanicIf(err)
return value, nil
}
// ReadSignedRationals parses the list of encoded, signed rationals from the
// value-context.
func (vc *ValueContext) ReadSignedRationals() (value []SignedRational, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
rawValue, err := vc.readRawEncoded()
log.PanicIf(err)
value, err = parser.ParseSignedRationals(rawValue, vc.unitCount, vc.byteOrder)
log.PanicIf(err)
return value, nil
}
// Values knows how to resolve the given value. This value is always a list
// (undefined-values aside), so we're named accordingly.
//
// Since this method lacks the information to process unknown-type tags (e.g.
// byte-order, tag-ID, IFD type), it will return an error if attempted. See
// `Undefined()`.
func (vc *ValueContext) Values() (values interface{}, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
if vc.tagType == TypeByte {
values, err = vc.ReadBytes()
log.PanicIf(err)
} else if vc.tagType == TypeAscii {
values, err = vc.ReadAscii()
log.PanicIf(err)
} else if vc.tagType == TypeAsciiNoNul {
values, err = vc.ReadAsciiNoNul()
log.PanicIf(err)
} else if vc.tagType == TypeShort {
values, err = vc.ReadShorts()
log.PanicIf(err)
} else if vc.tagType == TypeLong {
values, err = vc.ReadLongs()
log.PanicIf(err)
} else if vc.tagType == TypeRational {
values, err = vc.ReadRationals()
log.PanicIf(err)
} else if vc.tagType == TypeSignedLong {
values, err = vc.ReadSignedLongs()
log.PanicIf(err)
} else if vc.tagType == TypeSignedRational {
values, err = vc.ReadSignedRationals()
log.PanicIf(err)
} else if vc.tagType == TypeFloat {
values, err = vc.ReadFloats()
log.PanicIf(err)
} else if vc.tagType == TypeDouble {
values, err = vc.ReadDoubles()
log.PanicIf(err)
} else if vc.tagType == TypeUndefined {
log.Panicf("will not parse undefined-type value")
// Never called.
return nil, nil
} else {
log.Panicf("value of type [%s] is unparseable", vc.tagType)
// Never called.
return nil, nil
}
return values, nil
}
func init() {
parser = new(Parser)
}

View File

@ -1,273 +0,0 @@
package exifcommon
import (
"bytes"
"math"
"reflect"
"time"
"encoding/binary"
"github.com/dsoprea/go-logging"
)
var (
typeEncodeLogger = log.NewLogger("exif.type_encode")
)
// EncodedData encapsulates the compound output of an encoding operation.
type EncodedData struct {
Type TagTypePrimitive
Encoded []byte
// TODO(dustin): Is this really necessary? We might have this just to correlate to the incoming stream format (raw bytes and a unit-count both for incoming and outgoing).
UnitCount uint32
}
// ValueEncoder knows how to encode values of every type to bytes.
type ValueEncoder struct {
byteOrder binary.ByteOrder
}
// NewValueEncoder returns a new ValueEncoder.
func NewValueEncoder(byteOrder binary.ByteOrder) *ValueEncoder {
return &ValueEncoder{
byteOrder: byteOrder,
}
}
func (ve *ValueEncoder) encodeBytes(value []uint8) (ed EncodedData, err error) {
ed.Type = TypeByte
ed.Encoded = []byte(value)
ed.UnitCount = uint32(len(value))
return ed, nil
}
func (ve *ValueEncoder) encodeAscii(value string) (ed EncodedData, err error) {
ed.Type = TypeAscii
ed.Encoded = []byte(value)
ed.Encoded = append(ed.Encoded, 0)
ed.UnitCount = uint32(len(ed.Encoded))
return ed, nil
}
// encodeAsciiNoNul returns a string encoded as a byte-string without a trailing
// NUL byte.
//
// Note that:
//
// 1. This type can not be automatically encoded using `Encode()`. The default
// mode is to encode *with* a trailing NUL byte using `encodeAscii`. Only
// certain undefined-type tags using an unterminated ASCII string and these
// are exceptional in nature.
//
// 2. The presence of this method allows us to completely test the complimentary
// no-nul parser.
//
func (ve *ValueEncoder) encodeAsciiNoNul(value string) (ed EncodedData, err error) {
ed.Type = TypeAsciiNoNul
ed.Encoded = []byte(value)
ed.UnitCount = uint32(len(ed.Encoded))
return ed, nil
}
func (ve *ValueEncoder) encodeShorts(value []uint16) (ed EncodedData, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
ed.UnitCount = uint32(len(value))
ed.Encoded = make([]byte, ed.UnitCount*2)
for i := uint32(0); i < ed.UnitCount; i++ {
ve.byteOrder.PutUint16(ed.Encoded[i*2:(i+1)*2], value[i])
}
ed.Type = TypeShort
return ed, nil
}
func (ve *ValueEncoder) encodeLongs(value []uint32) (ed EncodedData, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
ed.UnitCount = uint32(len(value))
ed.Encoded = make([]byte, ed.UnitCount*4)
for i := uint32(0); i < ed.UnitCount; i++ {
ve.byteOrder.PutUint32(ed.Encoded[i*4:(i+1)*4], value[i])
}
ed.Type = TypeLong
return ed, nil
}
func (ve *ValueEncoder) encodeFloats(value []float32) (ed EncodedData, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
ed.UnitCount = uint32(len(value))
ed.Encoded = make([]byte, ed.UnitCount*4)
for i := uint32(0); i < ed.UnitCount; i++ {
ve.byteOrder.PutUint32(ed.Encoded[i*4:(i+1)*4], math.Float32bits(value[i]))
}
ed.Type = TypeFloat
return ed, nil
}
func (ve *ValueEncoder) encodeDoubles(value []float64) (ed EncodedData, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
ed.UnitCount = uint32(len(value))
ed.Encoded = make([]byte, ed.UnitCount*8)
for i := uint32(0); i < ed.UnitCount; i++ {
ve.byteOrder.PutUint64(ed.Encoded[i*8:(i+1)*8], math.Float64bits(value[i]))
}
ed.Type = TypeDouble
return ed, nil
}
func (ve *ValueEncoder) encodeRationals(value []Rational) (ed EncodedData, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
ed.UnitCount = uint32(len(value))
ed.Encoded = make([]byte, ed.UnitCount*8)
for i := uint32(0); i < ed.UnitCount; i++ {
ve.byteOrder.PutUint32(ed.Encoded[i*8+0:i*8+4], value[i].Numerator)
ve.byteOrder.PutUint32(ed.Encoded[i*8+4:i*8+8], value[i].Denominator)
}
ed.Type = TypeRational
return ed, nil
}
func (ve *ValueEncoder) encodeSignedLongs(value []int32) (ed EncodedData, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
ed.UnitCount = uint32(len(value))
b := bytes.NewBuffer(make([]byte, 0, 8*ed.UnitCount))
for i := uint32(0); i < ed.UnitCount; i++ {
err := binary.Write(b, ve.byteOrder, value[i])
log.PanicIf(err)
}
ed.Type = TypeSignedLong
ed.Encoded = b.Bytes()
return ed, nil
}
func (ve *ValueEncoder) encodeSignedRationals(value []SignedRational) (ed EncodedData, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
ed.UnitCount = uint32(len(value))
b := bytes.NewBuffer(make([]byte, 0, 8*ed.UnitCount))
for i := uint32(0); i < ed.UnitCount; i++ {
err := binary.Write(b, ve.byteOrder, value[i].Numerator)
log.PanicIf(err)
err = binary.Write(b, ve.byteOrder, value[i].Denominator)
log.PanicIf(err)
}
ed.Type = TypeSignedRational
ed.Encoded = b.Bytes()
return ed, nil
}
// Encode returns bytes for the given value, infering type from the actual
// value. This does not support `TypeAsciiNoNull` (all strings are encoded as
// `TypeAscii`).
func (ve *ValueEncoder) Encode(value interface{}) (ed EncodedData, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
switch t := value.(type) {
case []byte:
ed, err = ve.encodeBytes(t)
log.PanicIf(err)
case string:
ed, err = ve.encodeAscii(t)
log.PanicIf(err)
case []uint16:
ed, err = ve.encodeShorts(t)
log.PanicIf(err)
case []uint32:
ed, err = ve.encodeLongs(t)
log.PanicIf(err)
case []float32:
ed, err = ve.encodeFloats(t)
log.PanicIf(err)
case []float64:
ed, err = ve.encodeDoubles(t)
log.PanicIf(err)
case []Rational:
ed, err = ve.encodeRationals(t)
log.PanicIf(err)
case []int32:
ed, err = ve.encodeSignedLongs(t)
log.PanicIf(err)
case []SignedRational:
ed, err = ve.encodeSignedRationals(t)
log.PanicIf(err)
case time.Time:
// For convenience, if the user doesn't want to deal with translation
// semantics with timestamps.
s := ExifFullTimestampString(t)
ed, err = ve.encodeAscii(s)
log.PanicIf(err)
default:
log.Panicf("value not encodable: [%s] [%v]", reflect.TypeOf(value), value)
}
return ed, nil
}

View File

@ -1,50 +0,0 @@
package exif
import (
"io"
"github.com/dsoprea/go-logging"
"github.com/dsoprea/go-utility/v2/filesystem"
)
type ExifBlobSeeker interface {
GetReadSeeker(initialOffset int64) (rs io.ReadSeeker, err error)
}
// ExifReadSeeker knows how to retrieve data from the EXIF blob relative to the
// beginning of the blob (so, absolute position (0) is the first byte of the
// EXIF data).
type ExifReadSeeker struct {
rs io.ReadSeeker
}
func NewExifReadSeeker(rs io.ReadSeeker) *ExifReadSeeker {
return &ExifReadSeeker{
rs: rs,
}
}
func NewExifReadSeekerWithBytes(exifData []byte) *ExifReadSeeker {
sb := rifs.NewSeekableBufferWithBytes(exifData)
edbs := NewExifReadSeeker(sb)
return edbs
}
// Fork creates a new ReadSeeker instead that wraps a BouncebackReader to
// maintain its own position in the stream.
func (edbs *ExifReadSeeker) GetReadSeeker(initialOffset int64) (rs io.ReadSeeker, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
br, err := rifs.NewBouncebackReader(edbs.rs)
log.PanicIf(err)
_, err = br.Seek(initialOffset, io.SeekStart)
log.PanicIf(err)
return br, nil
}

View File

@ -1,14 +0,0 @@
package exif
import (
"errors"
)
var (
// ErrTagNotFound indicates that the tag was not found.
ErrTagNotFound = errors.New("tag not found")
// ErrTagNotKnown indicates that the tag is not registered with us as a
// known tag.
ErrTagNotKnown = errors.New("tag is not known")
)

View File

@ -1,333 +0,0 @@
package exif
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"os"
"encoding/binary"
"io/ioutil"
"github.com/dsoprea/go-logging"
"github.com/dsoprea/go-exif/v3/common"
)
const (
// ExifAddressableAreaStart is the absolute offset in the file that all
// offsets are relative to.
ExifAddressableAreaStart = uint32(0x0)
// ExifDefaultFirstIfdOffset is essentially the number of bytes in addition
// to `ExifAddressableAreaStart` that you have to move in order to escape
// the rest of the header and get to the earliest point where we can put
// stuff (which has to be the first IFD). This is the size of the header
// sequence containing the two-character byte-order, two-character fixed-
// bytes, and the four bytes describing the first-IFD offset.
ExifDefaultFirstIfdOffset = uint32(2 + 2 + 4)
)
const (
// ExifSignatureLength is the number of bytes in the EXIF signature (which
// customarily includes the first IFD offset).
ExifSignatureLength = 8
)
var (
exifLogger = log.NewLogger("exif.exif")
ExifBigEndianSignature = [4]byte{'M', 'M', 0x00, 0x2a}
ExifLittleEndianSignature = [4]byte{'I', 'I', 0x2a, 0x00}
)
var (
ErrNoExif = errors.New("no exif data")
ErrExifHeaderError = errors.New("exif header error")
)
// SearchAndExtractExif searches for an EXIF blob in the byte-slice.
func SearchAndExtractExif(data []byte) (rawExif []byte, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
b := bytes.NewBuffer(data)
rawExif, err = SearchAndExtractExifWithReader(b)
if err != nil {
if err == ErrNoExif {
return nil, err
}
log.Panic(err)
}
return rawExif, nil
}
// SearchAndExtractExifN searches for an EXIF blob in the byte-slice, but skips
// the given number of EXIF blocks first. This is a forensics tool that helps
// identify multiple EXIF blocks in a file.
func SearchAndExtractExifN(data []byte, n int) (rawExif []byte, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
skips := 0
totalDiscarded := 0
for {
b := bytes.NewBuffer(data)
var discarded int
rawExif, discarded, err = searchAndExtractExifWithReaderWithDiscarded(b)
if err != nil {
if err == ErrNoExif {
return nil, err
}
log.Panic(err)
}
exifLogger.Debugf(nil, "Read EXIF block (%d).", skips)
totalDiscarded += discarded
if skips >= n {
exifLogger.Debugf(nil, "Reached requested EXIF block (%d).", n)
break
}
nextOffset := discarded + 1
exifLogger.Debugf(nil, "Skipping EXIF block (%d) by seeking to position (%d).", skips, nextOffset)
data = data[nextOffset:]
skips++
}
exifLogger.Debugf(nil, "Found EXIF blob (%d) bytes from initial position.", totalDiscarded)
return rawExif, nil
}
// searchAndExtractExifWithReaderWithDiscarded searches for an EXIF blob using
// an `io.Reader`. We can't know how much long the EXIF data is without parsing
// it, so this will likely grab up a lot of the image-data, too.
//
// This function returned the count of preceding bytes.
func searchAndExtractExifWithReaderWithDiscarded(r io.Reader) (rawExif []byte, discarded int, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
// Search for the beginning of the EXIF information. The EXIF is near the
// beginning of most JPEGs, so this likely doesn't have a high cost (at
// least, again, with JPEGs).
br := bufio.NewReader(r)
for {
window, err := br.Peek(ExifSignatureLength)
if err != nil {
if err == io.EOF {
return nil, 0, ErrNoExif
}
log.Panic(err)
}
_, err = ParseExifHeader(window)
if err != nil {
if log.Is(err, ErrNoExif) == true {
// No EXIF. Move forward by one byte.
_, err := br.Discard(1)
log.PanicIf(err)
discarded++
continue
}
// Some other error.
log.Panic(err)
}
break
}
exifLogger.Debugf(nil, "Found EXIF blob (%d) bytes from initial position.", discarded)
rawExif, err = ioutil.ReadAll(br)
log.PanicIf(err)
return rawExif, discarded, nil
}
// RELEASE(dustin): We should replace the implementation of SearchAndExtractExifWithReader with searchAndExtractExifWithReaderWithDiscarded and drop the latter.
// SearchAndExtractExifWithReader searches for an EXIF blob using an
// `io.Reader`. We can't know how much long the EXIF data is without parsing it,
// so this will likely grab up a lot of the image-data, too.
func SearchAndExtractExifWithReader(r io.Reader) (rawExif []byte, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
rawExif, _, err = searchAndExtractExifWithReaderWithDiscarded(r)
if err != nil {
if err == ErrNoExif {
return nil, err
}
log.Panic(err)
}
return rawExif, nil
}
// SearchFileAndExtractExif returns a slice from the beginning of the EXIF data
// to the end of the file (it's not practical to try and calculate where the
// data actually ends).
func SearchFileAndExtractExif(filepath string) (rawExif []byte, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
// Open the file.
f, err := os.Open(filepath)
log.PanicIf(err)
defer f.Close()
rawExif, err = SearchAndExtractExifWithReader(f)
log.PanicIf(err)
return rawExif, nil
}
type ExifHeader struct {
ByteOrder binary.ByteOrder
FirstIfdOffset uint32
}
func (eh ExifHeader) String() string {
return fmt.Sprintf("ExifHeader<BYTE-ORDER=[%v] FIRST-IFD-OFFSET=(0x%02x)>", eh.ByteOrder, eh.FirstIfdOffset)
}
// ParseExifHeader parses the bytes at the very top of the header.
//
// This will panic with ErrNoExif on any data errors so that we can double as
// an EXIF-detection routine.
func ParseExifHeader(data []byte) (eh ExifHeader, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
// Good reference:
//
// CIPA DC-008-2016; JEITA CP-3451D
// -> http://www.cipa.jp/std/documents/e/DC-008-Translation-2016-E.pdf
if len(data) < ExifSignatureLength {
exifLogger.Warningf(nil, "Not enough data for EXIF header: (%d)", len(data))
return eh, ErrNoExif
}
if bytes.Equal(data[:4], ExifBigEndianSignature[:]) == true {
exifLogger.Debugf(nil, "Byte-order is big-endian.")
eh.ByteOrder = binary.BigEndian
} else if bytes.Equal(data[:4], ExifLittleEndianSignature[:]) == true {
eh.ByteOrder = binary.LittleEndian
exifLogger.Debugf(nil, "Byte-order is little-endian.")
} else {
return eh, ErrNoExif
}
eh.FirstIfdOffset = eh.ByteOrder.Uint32(data[4:8])
return eh, nil
}
// Visit recursively invokes a callback for every tag.
func Visit(rootIfdIdentity *exifcommon.IfdIdentity, ifdMapping *exifcommon.IfdMapping, tagIndex *TagIndex, exifData []byte, visitor TagVisitorFn, so *ScanOptions) (eh ExifHeader, furthestOffset uint32, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
eh, err = ParseExifHeader(exifData)
log.PanicIf(err)
ebs := NewExifReadSeekerWithBytes(exifData)
ie := NewIfdEnumerate(ifdMapping, tagIndex, ebs, eh.ByteOrder)
_, err = ie.Scan(rootIfdIdentity, eh.FirstIfdOffset, visitor, so)
log.PanicIf(err)
furthestOffset = ie.FurthestOffset()
return eh, furthestOffset, nil
}
// Collect recursively builds a static structure of all IFDs and tags.
func Collect(ifdMapping *exifcommon.IfdMapping, tagIndex *TagIndex, exifData []byte) (eh ExifHeader, index IfdIndex, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
eh, err = ParseExifHeader(exifData)
log.PanicIf(err)
ebs := NewExifReadSeekerWithBytes(exifData)
ie := NewIfdEnumerate(ifdMapping, tagIndex, ebs, eh.ByteOrder)
index, err = ie.Collect(eh.FirstIfdOffset)
log.PanicIf(err)
return eh, index, nil
}
// BuildExifHeader constructs the bytes that go at the front of the stream.
func BuildExifHeader(byteOrder binary.ByteOrder, firstIfdOffset uint32) (headerBytes []byte, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
b := new(bytes.Buffer)
var signatureBytes []byte
if byteOrder == binary.BigEndian {
signatureBytes = ExifBigEndianSignature[:]
} else {
signatureBytes = ExifLittleEndianSignature[:]
}
_, err = b.Write(signatureBytes)
log.PanicIf(err)
err = binary.Write(b, byteOrder, firstIfdOffset)
log.PanicIf(err)
return b.Bytes(), nil
}

View File

@ -1,117 +0,0 @@
package exif
import (
"errors"
"fmt"
"time"
"github.com/dsoprea/go-logging"
"github.com/golang/geo/s2"
"github.com/dsoprea/go-exif/v3/common"
)
var (
// ErrGpsCoordinatesNotValid means that some part of the geographic data was
// unparseable.
ErrGpsCoordinatesNotValid = errors.New("GPS coordinates not valid")
)
// GpsDegrees is a high-level struct representing geographic data.
type GpsDegrees struct {
// Orientation describes the N/E/S/W direction that this position is
// relative to.
Orientation byte
// Degrees is a simple float representing the underlying rational degrees
// amount.
Degrees float64
// Minutes is a simple float representing the underlying rational minutes
// amount.
Minutes float64
// Seconds is a simple float representing the underlying ration seconds
// amount.
Seconds float64
}
// NewGpsDegreesFromRationals returns a GpsDegrees struct given the EXIF-encoded
// information. The refValue is the N/E/S/W direction that this position is
// relative to.
func NewGpsDegreesFromRationals(refValue string, rawCoordinate []exifcommon.Rational) (gd GpsDegrees, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
if len(rawCoordinate) != 3 {
log.Panicf("new GpsDegrees struct requires a raw-coordinate with exactly three rationals")
}
gd = GpsDegrees{
Orientation: refValue[0],
Degrees: float64(rawCoordinate[0].Numerator) / float64(rawCoordinate[0].Denominator),
Minutes: float64(rawCoordinate[1].Numerator) / float64(rawCoordinate[1].Denominator),
Seconds: float64(rawCoordinate[2].Numerator) / float64(rawCoordinate[2].Denominator),
}
return gd, nil
}
// String provides returns a descriptive string.
func (d GpsDegrees) String() string {
return fmt.Sprintf("Degrees<O=[%s] D=(%g) M=(%g) S=(%g)>", string([]byte{d.Orientation}), d.Degrees, d.Minutes, d.Seconds)
}
// Decimal calculates and returns the simplified float representation of the
// component degrees.
func (d GpsDegrees) Decimal() float64 {
decimal := float64(d.Degrees) + float64(d.Minutes)/60.0 + float64(d.Seconds)/3600.0
if d.Orientation == 'S' || d.Orientation == 'W' {
return -decimal
}
return decimal
}
// Raw returns a Rational struct that can be used to *write* coordinates. In
// practice, the denominator are typically (1) in the original EXIF data, and,
// that being the case, this will best preserve precision.
func (d GpsDegrees) Raw() []exifcommon.Rational {
return []exifcommon.Rational{
{Numerator: uint32(d.Degrees), Denominator: 1},
{Numerator: uint32(d.Minutes), Denominator: 1},
{Numerator: uint32(d.Seconds), Denominator: 1},
}
}
// GpsInfo encapsulates all of the geographic information in one place.
type GpsInfo struct {
Latitude, Longitude GpsDegrees
Altitude int
Timestamp time.Time
}
// String returns a descriptive string.
func (gi *GpsInfo) String() string {
return fmt.Sprintf("GpsInfo<LAT=(%.05f) LON=(%.05f) ALT=(%d) TIME=[%s]>",
gi.Latitude.Decimal(), gi.Longitude.Decimal(), gi.Altitude, gi.Timestamp)
}
// S2CellId returns the cell-ID of the geographic location on the earth.
func (gi *GpsInfo) S2CellId() s2.CellID {
latitude := gi.Latitude.Decimal()
longitude := gi.Longitude.Decimal()
ll := s2.LatLngFromDegrees(latitude, longitude)
cellId := s2.CellIDFromLatLng(ll)
if cellId.IsValid() == false {
panic(ErrGpsCoordinatesNotValid)
}
return cellId
}

File diff suppressed because it is too large Load Diff

View File

@ -1,532 +0,0 @@
package exif
import (
"bytes"
"fmt"
"strings"
"encoding/binary"
"github.com/dsoprea/go-logging"
"github.com/dsoprea/go-exif/v3/common"
)
const (
// Tag-ID + Tag-Type + Unit-Count + Value/Offset.
IfdTagEntrySize = uint32(2 + 2 + 4 + 4)
)
type ByteWriter struct {
b *bytes.Buffer
byteOrder binary.ByteOrder
}
func NewByteWriter(b *bytes.Buffer, byteOrder binary.ByteOrder) (bw *ByteWriter) {
return &ByteWriter{
b: b,
byteOrder: byteOrder,
}
}
func (bw ByteWriter) writeAsBytes(value interface{}) (err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
err = binary.Write(bw.b, bw.byteOrder, value)
log.PanicIf(err)
return nil
}
func (bw ByteWriter) WriteUint32(value uint32) (err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
err = bw.writeAsBytes(value)
log.PanicIf(err)
return nil
}
func (bw ByteWriter) WriteUint16(value uint16) (err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
err = bw.writeAsBytes(value)
log.PanicIf(err)
return nil
}
func (bw ByteWriter) WriteFourBytes(value []byte) (err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
len_ := len(value)
if len_ != 4 {
log.Panicf("value is not four-bytes: (%d)", len_)
}
_, err = bw.b.Write(value)
log.PanicIf(err)
return nil
}
// ifdOffsetIterator keeps track of where the next IFD should be written by
// keeping track of where the offsets start, the data that has been added, and
// bumping the offset *when* the data is added.
type ifdDataAllocator struct {
offset uint32
b bytes.Buffer
}
func newIfdDataAllocator(ifdDataAddressableOffset uint32) *ifdDataAllocator {
return &ifdDataAllocator{
offset: ifdDataAddressableOffset,
}
}
func (ida *ifdDataAllocator) Allocate(value []byte) (offset uint32, err error) {
_, err = ida.b.Write(value)
log.PanicIf(err)
offset = ida.offset
ida.offset += uint32(len(value))
return offset, nil
}
func (ida *ifdDataAllocator) NextOffset() uint32 {
return ida.offset
}
func (ida *ifdDataAllocator) Bytes() []byte {
return ida.b.Bytes()
}
// IfdByteEncoder converts an IB to raw bytes (for writing) while also figuring
// out all of the allocations and indirection that is required for extended
// data.
type IfdByteEncoder struct {
// journal holds a list of actions taken while encoding.
journal [][3]string
}
func NewIfdByteEncoder() (ibe *IfdByteEncoder) {
return &IfdByteEncoder{
journal: make([][3]string, 0),
}
}
func (ibe *IfdByteEncoder) Journal() [][3]string {
return ibe.journal
}
func (ibe *IfdByteEncoder) TableSize(entryCount int) uint32 {
// Tag-Count + (Entry-Size * Entry-Count) + Next-IFD-Offset.
return uint32(2) + (IfdTagEntrySize * uint32(entryCount)) + uint32(4)
}
func (ibe *IfdByteEncoder) pushToJournal(where, direction, format string, args ...interface{}) {
event := [3]string{
direction,
where,
fmt.Sprintf(format, args...),
}
ibe.journal = append(ibe.journal, event)
}
// PrintJournal prints a hierarchical representation of the steps taken during
// encoding.
func (ibe *IfdByteEncoder) PrintJournal() {
maxWhereLength := 0
for _, event := range ibe.journal {
where := event[1]
len_ := len(where)
if len_ > maxWhereLength {
maxWhereLength = len_
}
}
level := 0
for i, event := range ibe.journal {
direction := event[0]
where := event[1]
message := event[2]
if direction != ">" && direction != "<" && direction != "-" {
log.Panicf("journal operation not valid: [%s]", direction)
}
if direction == "<" {
if level <= 0 {
log.Panicf("journal operations unbalanced (too many closes)")
}
level--
}
indent := strings.Repeat(" ", level)
fmt.Printf("%3d %s%s %s: %s\n", i, indent, direction, where, message)
if direction == ">" {
level++
}
}
if level != 0 {
log.Panicf("journal operations unbalanced (too many opens)")
}
}
// encodeTagToBytes encodes the given tag to a byte stream. If
// `nextIfdOffsetToWrite` is more than (0), recurse into child IFDs
// (`nextIfdOffsetToWrite` is required in order for them to know where the its
// IFD data will be written, in order for them to know the offset of where
// their allocated-data block will start, which follows right behind).
func (ibe *IfdByteEncoder) encodeTagToBytes(ib *IfdBuilder, bt *BuilderTag, bw *ByteWriter, ida *ifdDataAllocator, nextIfdOffsetToWrite uint32) (childIfdBlock []byte, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
// Write tag-ID.
err = bw.WriteUint16(bt.tagId)
log.PanicIf(err)
// Works for both values and child IFDs (which have an official size of
// LONG).
err = bw.WriteUint16(uint16(bt.typeId))
log.PanicIf(err)
// Write unit-count.
if bt.value.IsBytes() == true {
effectiveType := bt.typeId
if bt.typeId == exifcommon.TypeUndefined {
effectiveType = exifcommon.TypeByte
}
// It's a non-unknown value.Calculate the count of values of
// the type that we're writing and the raw bytes for the whole list.
typeSize := uint32(effectiveType.Size())
valueBytes := bt.value.Bytes()
len_ := len(valueBytes)
unitCount := uint32(len_) / typeSize
if _, found := tagsWithoutAlignment[bt.tagId]; found == false {
remainder := uint32(len_) % typeSize
if remainder > 0 {
log.Panicf("tag (0x%04x) value of (%d) bytes not evenly divisible by type-size (%d)", bt.tagId, len_, typeSize)
}
}
err = bw.WriteUint32(unitCount)
log.PanicIf(err)
// Write four-byte value/offset.
if len_ > 4 {
offset, err := ida.Allocate(valueBytes)
log.PanicIf(err)
err = bw.WriteUint32(offset)
log.PanicIf(err)
} else {
fourBytes := make([]byte, 4)
copy(fourBytes, valueBytes)
err = bw.WriteFourBytes(fourBytes)
log.PanicIf(err)
}
} else {
if bt.value.IsIb() == false {
log.Panicf("tag value is not a byte-slice but also not a child IB: %v", bt)
}
// Write unit-count (one LONG representing one offset).
err = bw.WriteUint32(1)
log.PanicIf(err)
if nextIfdOffsetToWrite > 0 {
var err error
ibe.pushToJournal("encodeTagToBytes", ">", "[%s]->[%s]", ib.IfdIdentity().UnindexedString(), bt.value.Ib().IfdIdentity().UnindexedString())
// Create the block of IFD data and everything it requires.
childIfdBlock, err = ibe.encodeAndAttachIfd(bt.value.Ib(), nextIfdOffsetToWrite)
log.PanicIf(err)
ibe.pushToJournal("encodeTagToBytes", "<", "[%s]->[%s]", bt.value.Ib().IfdIdentity().UnindexedString(), ib.IfdIdentity().UnindexedString())
// Use the next-IFD offset for it. The IFD will actually get
// attached after we return.
err = bw.WriteUint32(nextIfdOffsetToWrite)
log.PanicIf(err)
} else {
// No child-IFDs are to be allocated. Finish the entry with a NULL
// pointer.
ibe.pushToJournal("encodeTagToBytes", "-", "*Not* descending to child: [%s]", bt.value.Ib().IfdIdentity().UnindexedString())
err = bw.WriteUint32(0)
log.PanicIf(err)
}
}
return childIfdBlock, nil
}
// encodeIfdToBytes encodes the given IB to a byte-slice. We are given the
// offset at which this IFD will be written. This method is used called both to
// pre-determine how big the table is going to be (so that we can calculate the
// address to allocate data at) as well as to write the final table.
//
// It is necessary to fully realize the table in order to predetermine its size
// because it is not enough to know the size of the table: If there are child
// IFDs, we will not be able to allocate them without first knowing how much
// data we need to allocate for the current IFD.
func (ibe *IfdByteEncoder) encodeIfdToBytes(ib *IfdBuilder, ifdAddressableOffset uint32, nextIfdOffsetToWrite uint32, setNextIb bool) (data []byte, tableSize uint32, dataSize uint32, childIfdSizes []uint32, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
ibe.pushToJournal("encodeIfdToBytes", ">", "%s", ib)
tableSize = ibe.TableSize(len(ib.tags))
b := new(bytes.Buffer)
bw := NewByteWriter(b, ib.byteOrder)
// Write tag count.
err = bw.WriteUint16(uint16(len(ib.tags)))
log.PanicIf(err)
ida := newIfdDataAllocator(ifdAddressableOffset)
childIfdBlocks := make([][]byte, 0)
// Write raw bytes for each tag entry. Allocate larger data to be referred
// to in the follow-up data-block as required. Any "unknown"-byte tags that
// we can't parse will not be present here (using AddTagsFromExisting(), at
// least).
for _, bt := range ib.tags {
childIfdBlock, err := ibe.encodeTagToBytes(ib, bt, bw, ida, nextIfdOffsetToWrite)
log.PanicIf(err)
if childIfdBlock != nil {
// We aren't allowed to have non-nil child IFDs if we're just
// sizing things up.
if nextIfdOffsetToWrite == 0 {
log.Panicf("no IFD offset provided for child-IFDs; no new child-IFDs permitted")
}
nextIfdOffsetToWrite += uint32(len(childIfdBlock))
childIfdBlocks = append(childIfdBlocks, childIfdBlock)
}
}
dataBytes := ida.Bytes()
dataSize = uint32(len(dataBytes))
childIfdSizes = make([]uint32, len(childIfdBlocks))
childIfdsTotalSize := uint32(0)
for i, childIfdBlock := range childIfdBlocks {
len_ := uint32(len(childIfdBlock))
childIfdSizes[i] = len_
childIfdsTotalSize += len_
}
// N the link from this IFD to the next IFD that will be written in the
// next cycle.
if setNextIb == true {
// Write address of next IFD in chain. This will be the original
// allocation offset plus the size of everything we have allocated for
// this IFD and its child-IFDs.
//
// It is critical that this number is stepped properly. We experienced
// an issue whereby it first looked like we were duplicating the IFD and
// then that we were duplicating the tags in the wrong IFD, and then
// finally we determined that the next-IFD offset for the first IFD was
// accidentally pointing back to the EXIF IFD, so we were visiting it
// twice when visiting through the tags after decoding. It was an
// expensive bug to find.
ibe.pushToJournal("encodeIfdToBytes", "-", "Setting 'next' IFD to (0x%08x).", nextIfdOffsetToWrite)
err := bw.WriteUint32(nextIfdOffsetToWrite)
log.PanicIf(err)
} else {
err := bw.WriteUint32(0)
log.PanicIf(err)
}
_, err = b.Write(dataBytes)
log.PanicIf(err)
// Append any child IFD blocks after our table and data blocks. These IFDs
// were equipped with the appropriate offset information so it's expected
// that all offsets referred to by these will be correct.
//
// Note that child-IFDs are append after the current IFD and before the
// next IFD, as opposed to the root IFDs, which are chained together but
// will be interrupted by these child-IFDs (which is expected, per the
// standard).
for _, childIfdBlock := range childIfdBlocks {
_, err = b.Write(childIfdBlock)
log.PanicIf(err)
}
ibe.pushToJournal("encodeIfdToBytes", "<", "%s", ib)
return b.Bytes(), tableSize, dataSize, childIfdSizes, nil
}
// encodeAndAttachIfd is a reentrant function that processes the IFD chain.
func (ibe *IfdByteEncoder) encodeAndAttachIfd(ib *IfdBuilder, ifdAddressableOffset uint32) (data []byte, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
ibe.pushToJournal("encodeAndAttachIfd", ">", "%s", ib)
b := new(bytes.Buffer)
i := 0
for thisIb := ib; thisIb != nil; thisIb = thisIb.nextIb {
// Do a dry-run in order to pre-determine its size requirement.
ibe.pushToJournal("encodeAndAttachIfd", ">", "Beginning encoding process: (%d) [%s]", i, thisIb.IfdIdentity().UnindexedString())
ibe.pushToJournal("encodeAndAttachIfd", ">", "Calculating size: (%d) [%s]", i, thisIb.IfdIdentity().UnindexedString())
_, tableSize, allocatedDataSize, _, err := ibe.encodeIfdToBytes(thisIb, ifdAddressableOffset, 0, false)
log.PanicIf(err)
ibe.pushToJournal("encodeAndAttachIfd", "<", "Finished calculating size: (%d) [%s]", i, thisIb.IfdIdentity().UnindexedString())
ifdAddressableOffset += tableSize
nextIfdOffsetToWrite := ifdAddressableOffset + allocatedDataSize
ibe.pushToJournal("encodeAndAttachIfd", ">", "Next IFD will be written at offset (0x%08x)", nextIfdOffsetToWrite)
// Write our IFD as well as any child-IFDs (now that we know the offset
// where new IFDs and their data will be allocated).
setNextIb := thisIb.nextIb != nil
ibe.pushToJournal("encodeAndAttachIfd", ">", "Encoding starting: (%d) [%s] NEXT-IFD-OFFSET-TO-WRITE=(0x%08x)", i, thisIb.IfdIdentity().UnindexedString(), nextIfdOffsetToWrite)
tableAndAllocated, effectiveTableSize, effectiveAllocatedDataSize, childIfdSizes, err :=
ibe.encodeIfdToBytes(thisIb, ifdAddressableOffset, nextIfdOffsetToWrite, setNextIb)
log.PanicIf(err)
if effectiveTableSize != tableSize {
log.Panicf("written table size does not match the pre-calculated table size: (%d) != (%d) %s", effectiveTableSize, tableSize, ib)
} else if effectiveAllocatedDataSize != allocatedDataSize {
log.Panicf("written allocated-data size does not match the pre-calculated allocated-data size: (%d) != (%d) %s", effectiveAllocatedDataSize, allocatedDataSize, ib)
}
ibe.pushToJournal("encodeAndAttachIfd", "<", "Encoding done: (%d) [%s]", i, thisIb.IfdIdentity().UnindexedString())
totalChildIfdSize := uint32(0)
for _, childIfdSize := range childIfdSizes {
totalChildIfdSize += childIfdSize
}
if len(tableAndAllocated) != int(tableSize+allocatedDataSize+totalChildIfdSize) {
log.Panicf("IFD table and data is not a consistent size: (%d) != (%d)", len(tableAndAllocated), tableSize+allocatedDataSize+totalChildIfdSize)
}
// TODO(dustin): We might want to verify the original tableAndAllocated length, too.
_, err = b.Write(tableAndAllocated)
log.PanicIf(err)
// Advance past what we've allocated, thus far.
ifdAddressableOffset += allocatedDataSize + totalChildIfdSize
ibe.pushToJournal("encodeAndAttachIfd", "<", "Finishing encoding process: (%d) [%s] [FINAL:] NEXT-IFD-OFFSET-TO-WRITE=(0x%08x)", i, ib.IfdIdentity().UnindexedString(), nextIfdOffsetToWrite)
i++
}
ibe.pushToJournal("encodeAndAttachIfd", "<", "%s", ib)
return b.Bytes(), nil
}
// EncodeToExifPayload is the base encoding step that transcribes the entire IB
// structure to its on-disk layout.
func (ibe *IfdByteEncoder) EncodeToExifPayload(ib *IfdBuilder) (data []byte, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
data, err = ibe.encodeAndAttachIfd(ib, ExifDefaultFirstIfdOffset)
log.PanicIf(err)
return data, nil
}
// EncodeToExif calls EncodeToExifPayload and then packages the result into a
// complete EXIF block.
func (ibe *IfdByteEncoder) EncodeToExif(ib *IfdBuilder) (data []byte, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
encodedIfds, err := ibe.EncodeToExifPayload(ib)
log.PanicIf(err)
// Wrap the IFD in a formal EXIF block.
b := new(bytes.Buffer)
headerBytes, err := BuildExifHeader(ib.byteOrder, ExifDefaultFirstIfdOffset)
log.PanicIf(err)
_, err = b.Write(headerBytes)
log.PanicIf(err)
_, err = b.Write(encodedIfds)
log.PanicIf(err)
return b.Bytes(), nil
}

File diff suppressed because it is too large Load Diff

View File

@ -1,298 +0,0 @@
package exif
import (
"fmt"
"io"
"encoding/binary"
"github.com/dsoprea/go-logging"
"github.com/dsoprea/go-exif/v3/common"
"github.com/dsoprea/go-exif/v3/undefined"
)
var (
iteLogger = log.NewLogger("exif.ifd_tag_entry")
)
// IfdTagEntry refers to a tag in the loaded EXIF block.
type IfdTagEntry struct {
tagId uint16
tagIndex int
tagType exifcommon.TagTypePrimitive
unitCount uint32
valueOffset uint32
rawValueOffset []byte
// childIfdName is the right most atom in the IFD-path. We need this to
// construct the fully-qualified IFD-path.
childIfdName string
// childIfdPath is the IFD-path of the child if this tag represents a child
// IFD.
childIfdPath string
// childFqIfdPath is the IFD-path of the child if this tag represents a
// child IFD. Includes indices.
childFqIfdPath string
// TODO(dustin): !! IB's host the child-IBs directly in the tag, but that's not the case here. Refactor to accommodate it for a consistent experience.
ifdIdentity *exifcommon.IfdIdentity
isUnhandledUnknown bool
rs io.ReadSeeker
byteOrder binary.ByteOrder
tagName string
}
func newIfdTagEntry(ii *exifcommon.IfdIdentity, tagId uint16, tagIndex int, tagType exifcommon.TagTypePrimitive, unitCount uint32, valueOffset uint32, rawValueOffset []byte, rs io.ReadSeeker, byteOrder binary.ByteOrder) *IfdTagEntry {
return &IfdTagEntry{
ifdIdentity: ii,
tagId: tagId,
tagIndex: tagIndex,
tagType: tagType,
unitCount: unitCount,
valueOffset: valueOffset,
rawValueOffset: rawValueOffset,
rs: rs,
byteOrder: byteOrder,
}
}
// String returns a stringified representation of the struct.
func (ite *IfdTagEntry) String() string {
return fmt.Sprintf("IfdTagEntry<TAG-IFD-PATH=[%s] TAG-ID=(0x%04x) TAG-TYPE=[%s] UNIT-COUNT=(%d)>", ite.ifdIdentity.String(), ite.tagId, ite.tagType.String(), ite.unitCount)
}
// TagName returns the name of the tag. This is determined else and set after
// the parse (since it's not actually stored in the stream). If it's empty, it
// is because it is an unknown tag (nonstandard or otherwise unavailable in the
// tag-index).
func (ite *IfdTagEntry) TagName() string {
return ite.tagName
}
// setTagName sets the tag-name. This provides the name for convenience and
// efficiency by determining it when most efficient while we're parsing rather
// than delegating it to the caller (or, worse, the user).
func (ite *IfdTagEntry) setTagName(tagName string) {
ite.tagName = tagName
}
// IfdPath returns the fully-qualified path of the IFD that owns this tag.
func (ite *IfdTagEntry) IfdPath() string {
return ite.ifdIdentity.String()
}
// TagId returns the ID of the tag that we represent. The combination of
// (IfdPath(), TagId()) is unique.
func (ite *IfdTagEntry) TagId() uint16 {
return ite.tagId
}
// IsThumbnailOffset returns true if the tag has the IFD and tag-ID of a
// thumbnail offset.
func (ite *IfdTagEntry) IsThumbnailOffset() bool {
return ite.tagId == ThumbnailOffsetTagId && ite.ifdIdentity.String() == ThumbnailFqIfdPath
}
// IsThumbnailSize returns true if the tag has the IFD and tag-ID of a thumbnail
// size.
func (ite *IfdTagEntry) IsThumbnailSize() bool {
return ite.tagId == ThumbnailSizeTagId && ite.ifdIdentity.String() == ThumbnailFqIfdPath
}
// TagType is the type of value for this tag.
func (ite *IfdTagEntry) TagType() exifcommon.TagTypePrimitive {
return ite.tagType
}
// updateTagType sets an alternatively interpreted tag-type.
func (ite *IfdTagEntry) updateTagType(tagType exifcommon.TagTypePrimitive) {
ite.tagType = tagType
}
// UnitCount returns the unit-count of the tag's value.
func (ite *IfdTagEntry) UnitCount() uint32 {
return ite.unitCount
}
// updateUnitCount sets an alternatively interpreted unit-count.
func (ite *IfdTagEntry) updateUnitCount(unitCount uint32) {
ite.unitCount = unitCount
}
// getValueOffset is the four-byte offset converted to an integer to point to
// the location of its value in the EXIF block. The "get" parameter is obviously
// used in order to differentiate the naming of the method from the field.
func (ite *IfdTagEntry) getValueOffset() uint32 {
return ite.valueOffset
}
// GetRawBytes renders a specific list of bytes from the value in this tag.
func (ite *IfdTagEntry) GetRawBytes() (rawBytes []byte, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
valueContext := ite.getValueContext()
if ite.tagType == exifcommon.TypeUndefined {
value, err := exifundefined.Decode(valueContext)
if err != nil {
if err == exifcommon.ErrUnhandledUndefinedTypedTag {
ite.setIsUnhandledUnknown(true)
return nil, exifundefined.ErrUnparseableValue
} else if err == exifundefined.ErrUnparseableValue {
return nil, err
} else {
log.Panic(err)
}
}
// Encode it back, in order to get the raw bytes. This is the best,
// general way to do it with an undefined tag.
rawBytes, _, err := exifundefined.Encode(value, ite.byteOrder)
log.PanicIf(err)
return rawBytes, nil
}
rawBytes, err = valueContext.ReadRawEncoded()
log.PanicIf(err)
return rawBytes, nil
}
// Value returns the specific, parsed, typed value from the tag.
func (ite *IfdTagEntry) Value() (value interface{}, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
valueContext := ite.getValueContext()
if ite.tagType == exifcommon.TypeUndefined {
var err error
value, err = exifundefined.Decode(valueContext)
if err != nil {
if err == exifcommon.ErrUnhandledUndefinedTypedTag || err == exifundefined.ErrUnparseableValue {
return nil, err
}
log.Panic(err)
}
} else {
var err error
value, err = valueContext.Values()
log.PanicIf(err)
}
return value, nil
}
// Format returns the tag's value as a string.
func (ite *IfdTagEntry) Format() (phrase string, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
value, err := ite.Value()
if err != nil {
if err == exifcommon.ErrUnhandledUndefinedTypedTag {
return exifundefined.UnparseableUnknownTagValuePlaceholder, nil
} else if err == exifundefined.ErrUnparseableValue {
return exifundefined.UnparseableHandledTagValuePlaceholder, nil
}
log.Panic(err)
}
phrase, err = exifcommon.FormatFromType(value, false)
log.PanicIf(err)
return phrase, nil
}
// FormatFirst returns the same as Format() but only the first item.
func (ite *IfdTagEntry) FormatFirst() (phrase string, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
// TODO(dustin): We should add a convenience type "timestamp", to simplify translating to and from the physical ASCII and provide validation.
value, err := ite.Value()
if err != nil {
if err == exifcommon.ErrUnhandledUndefinedTypedTag {
return exifundefined.UnparseableUnknownTagValuePlaceholder, nil
}
log.Panic(err)
}
phrase, err = exifcommon.FormatFromType(value, true)
log.PanicIf(err)
return phrase, nil
}
func (ite *IfdTagEntry) setIsUnhandledUnknown(isUnhandledUnknown bool) {
ite.isUnhandledUnknown = isUnhandledUnknown
}
// SetChildIfd sets child-IFD information (if we represent a child IFD).
func (ite *IfdTagEntry) SetChildIfd(ii *exifcommon.IfdIdentity) {
ite.childFqIfdPath = ii.String()
ite.childIfdPath = ii.UnindexedString()
ite.childIfdName = ii.Name()
}
// ChildIfdName returns the name of the child IFD
func (ite *IfdTagEntry) ChildIfdName() string {
return ite.childIfdName
}
// ChildIfdPath returns the path of the child IFD.
func (ite *IfdTagEntry) ChildIfdPath() string {
return ite.childIfdPath
}
// ChildFqIfdPath returns the complete path of the child IFD along with the
// numeric suffixes differentiating sibling occurrences of the same type. "0"
// indices are omitted.
func (ite *IfdTagEntry) ChildFqIfdPath() string {
return ite.childFqIfdPath
}
// IfdIdentity returns the IfdIdentity associated with this tag.
func (ite *IfdTagEntry) IfdIdentity() *exifcommon.IfdIdentity {
return ite.ifdIdentity
}
func (ite *IfdTagEntry) getValueContext() *exifcommon.ValueContext {
return exifcommon.NewValueContext(
ite.ifdIdentity.String(),
ite.tagId,
ite.unitCount,
ite.valueOffset,
ite.rawValueOffset,
ite.rs,
ite.tagType,
ite.byteOrder)
}

View File

@ -1,8 +0,0 @@
// Package exif parses raw EXIF information given a block of raw EXIF data. It
// can also construct new EXIF information, and provides tools for doing so.
// This package is not involved with the parsing of particular file-formats.
//
// The EXIF data must first be extracted and then provided to us. Conversely,
// when constructing new EXIF data, the caller is responsible for packaging
// this in whichever format they require.
package exif

View File

@ -1,475 +0,0 @@
package exif
import (
"fmt"
"sync"
"github.com/dsoprea/go-logging"
"gopkg.in/yaml.v2"
"github.com/dsoprea/go-exif/v3/common"
)
const (
// IFD1
// ThumbnailFqIfdPath is the fully-qualified IFD path that the thumbnail
// must be found in.
ThumbnailFqIfdPath = "IFD1"
// ThumbnailOffsetTagId returns the tag-ID of the thumbnail offset.
ThumbnailOffsetTagId = 0x0201
// ThumbnailSizeTagId returns the tag-ID of the thumbnail size.
ThumbnailSizeTagId = 0x0202
)
const (
// GPS
// TagGpsVersionId is the ID of the GPS version tag.
TagGpsVersionId = 0x0000
// TagLatitudeId is the ID of the GPS latitude tag.
TagLatitudeId = 0x0002
// TagLatitudeRefId is the ID of the GPS latitude orientation tag.
TagLatitudeRefId = 0x0001
// TagLongitudeId is the ID of the GPS longitude tag.
TagLongitudeId = 0x0004
// TagLongitudeRefId is the ID of the GPS longitude-orientation tag.
TagLongitudeRefId = 0x0003
// TagTimestampId is the ID of the GPS time tag.
TagTimestampId = 0x0007
// TagDatestampId is the ID of the GPS date tag.
TagDatestampId = 0x001d
// TagAltitudeId is the ID of the GPS altitude tag.
TagAltitudeId = 0x0006
// TagAltitudeRefId is the ID of the GPS altitude-orientation tag.
TagAltitudeRefId = 0x0005
)
var (
// tagsWithoutAlignment is a tag-lookup for tags whose value size won't
// necessarily be a multiple of its tag-type.
tagsWithoutAlignment = map[uint16]struct{}{
// The thumbnail offset is stored as a long, but its data is a binary
// blob (not a slice of longs).
ThumbnailOffsetTagId: {},
}
)
var (
tagsLogger = log.NewLogger("exif.tags")
)
// File structures.
type encodedTag struct {
// id is signed, here, because YAML doesn't have enough information to
// support unsigned.
Id int `yaml:"id"`
Name string `yaml:"name"`
TypeName string `yaml:"type_name"`
TypeNames []string `yaml:"type_names"`
}
// Indexing structures.
// IndexedTag describes one index lookup result.
type IndexedTag struct {
// Id is the tag-ID.
Id uint16
// Name is the tag name.
Name string
// IfdPath is the proper IFD path of this tag. This is not fully-qualified.
IfdPath string
// SupportedTypes is an unsorted list of allowed tag-types.
SupportedTypes []exifcommon.TagTypePrimitive
}
// String returns a descriptive string.
func (it *IndexedTag) String() string {
return fmt.Sprintf("TAG<ID=(0x%04x) NAME=[%s] IFD=[%s]>", it.Id, it.Name, it.IfdPath)
}
// IsName returns true if this tag matches the given tag name.
func (it *IndexedTag) IsName(ifdPath, name string) bool {
return it.Name == name && it.IfdPath == ifdPath
}
// Is returns true if this tag matched the given tag ID.
func (it *IndexedTag) Is(ifdPath string, id uint16) bool {
return it.Id == id && it.IfdPath == ifdPath
}
// GetEncodingType returns the largest type that this tag's value can occupy.
func (it *IndexedTag) GetEncodingType(value interface{}) exifcommon.TagTypePrimitive {
// For convenience, we handle encoding a `time.Time` directly.
if exifcommon.IsTime(value) == true {
// Timestamps are encoded as ASCII.
value = ""
}
if len(it.SupportedTypes) == 0 {
log.Panicf("IndexedTag [%s] (%d) has no supported types.", it.IfdPath, it.Id)
} else if len(it.SupportedTypes) == 1 {
return it.SupportedTypes[0]
}
supportsLong := false
supportsShort := false
supportsRational := false
supportsSignedRational := false
for _, supportedType := range it.SupportedTypes {
if supportedType == exifcommon.TypeLong {
supportsLong = true
} else if supportedType == exifcommon.TypeShort {
supportsShort = true
} else if supportedType == exifcommon.TypeRational {
supportsRational = true
} else if supportedType == exifcommon.TypeSignedRational {
supportsSignedRational = true
}
}
// We specifically check for the cases that we know to expect.
if supportsLong == true && supportsShort == true {
return exifcommon.TypeLong
} else if supportsRational == true && supportsSignedRational == true {
if value == nil {
log.Panicf("GetEncodingType: require value to be given")
}
if _, ok := value.(exifcommon.SignedRational); ok == true {
return exifcommon.TypeSignedRational
}
return exifcommon.TypeRational
}
log.Panicf("WidestSupportedType() case is not handled for tag [%s] (0x%04x): %v", it.IfdPath, it.Id, it.SupportedTypes)
return 0
}
// DoesSupportType returns true if this tag can be found/decoded with this type.
func (it *IndexedTag) DoesSupportType(tagType exifcommon.TagTypePrimitive) bool {
// This is always a very small collection. So, we keep it unsorted.
for _, thisTagType := range it.SupportedTypes {
if thisTagType == tagType {
return true
}
}
return false
}
// TagIndex is a tag-lookup facility.
type TagIndex struct {
tagsByIfd map[string]map[uint16]*IndexedTag
tagsByIfdR map[string]map[string]*IndexedTag
mutex sync.Mutex
doUniversalSearch bool
}
// NewTagIndex returns a new TagIndex struct.
func NewTagIndex() *TagIndex {
ti := new(TagIndex)
ti.tagsByIfd = make(map[string]map[uint16]*IndexedTag)
ti.tagsByIfdR = make(map[string]map[string]*IndexedTag)
return ti
}
// SetUniversalSearch enables a fallback to matching tags under *any* IFD.
func (ti *TagIndex) SetUniversalSearch(flag bool) {
ti.doUniversalSearch = flag
}
// UniversalSearch enables a fallback to matching tags under *any* IFD.
func (ti *TagIndex) UniversalSearch() bool {
return ti.doUniversalSearch
}
// Add registers a new tag to be recognized during the parse.
func (ti *TagIndex) Add(it *IndexedTag) (err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
ti.mutex.Lock()
defer ti.mutex.Unlock()
// Store by ID.
family, found := ti.tagsByIfd[it.IfdPath]
if found == false {
family = make(map[uint16]*IndexedTag)
ti.tagsByIfd[it.IfdPath] = family
}
if _, found := family[it.Id]; found == true {
log.Panicf("tag-ID defined more than once for IFD [%s]: (%02x)", it.IfdPath, it.Id)
}
family[it.Id] = it
// Store by name.
familyR, found := ti.tagsByIfdR[it.IfdPath]
if found == false {
familyR = make(map[string]*IndexedTag)
ti.tagsByIfdR[it.IfdPath] = familyR
}
if _, found := familyR[it.Name]; found == true {
log.Panicf("tag-name defined more than once for IFD [%s]: (%s)", it.IfdPath, it.Name)
}
familyR[it.Name] = it
return nil
}
func (ti *TagIndex) getOne(ifdPath string, id uint16) (it *IndexedTag, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
if len(ti.tagsByIfd) == 0 {
err := LoadStandardTags(ti)
log.PanicIf(err)
}
ti.mutex.Lock()
defer ti.mutex.Unlock()
family, found := ti.tagsByIfd[ifdPath]
if found == false {
return nil, ErrTagNotFound
}
it, found = family[id]
if found == false {
return nil, ErrTagNotFound
}
return it, nil
}
// Get returns information about the non-IFD tag given a tag ID. `ifdPath` must
// not be fully-qualified.
func (ti *TagIndex) Get(ii *exifcommon.IfdIdentity, id uint16) (it *IndexedTag, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
ifdPath := ii.UnindexedString()
it, err = ti.getOne(ifdPath, id)
if err == nil {
return it, nil
} else if err != ErrTagNotFound {
log.Panic(err)
}
if ti.doUniversalSearch == false {
return nil, ErrTagNotFound
}
// We've been told to fallback to look for the tag in other IFDs.
skipIfdPath := ii.UnindexedString()
for currentIfdPath, _ := range ti.tagsByIfd {
if currentIfdPath == skipIfdPath {
// Skip the primary IFD, which has already been checked.
continue
}
it, err = ti.getOne(currentIfdPath, id)
if err == nil {
tagsLogger.Warningf(nil,
"Found tag (0x%02x) in the wrong IFD: [%s] != [%s]",
id, currentIfdPath, ifdPath)
return it, nil
} else if err != ErrTagNotFound {
log.Panic(err)
}
}
return nil, ErrTagNotFound
}
var (
// tagGuessDefaultIfdIdentities describes which IFDs we'll look for a given
// tag-ID in, if it's not found where it's supposed to be. We suppose that
// Exif-IFD tags might be found in IFD0 or IFD1, or IFD0/IFD1 tags might be
// found in the Exif IFD. This is the only thing we've seen so far. So, this
// is the limit of our guessing.
tagGuessDefaultIfdIdentities = []*exifcommon.IfdIdentity{
exifcommon.IfdExifStandardIfdIdentity,
exifcommon.IfdStandardIfdIdentity,
}
)
// FindFirst looks for the given tag-ID in each of the given IFDs in the given
// order. If `fqIfdPaths` is `nil` then use a default search order. This defies
// the standard, which requires each tag to exist in certain IFDs. This is a
// contingency to make recommendations for malformed data.
//
// Things *can* end badly here, in that the same tag-ID in different IFDs might
// describe different data and different ata-types, and our decode might then
// produce binary and non-printable data.
func (ti *TagIndex) FindFirst(id uint16, typeId exifcommon.TagTypePrimitive, ifdIdentities []*exifcommon.IfdIdentity) (it *IndexedTag, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
if ifdIdentities == nil {
ifdIdentities = tagGuessDefaultIfdIdentities
}
for _, ii := range ifdIdentities {
it, err := ti.Get(ii, id)
if err != nil {
if err == ErrTagNotFound {
continue
}
log.Panic(err)
}
// Even though the tag might be mislocated, the type should still be the
// same. Check this so we don't accidentally end-up on a complete
// irrelevant tag with a totally different data type. This attempts to
// mitigate producing garbage.
for _, supportedType := range it.SupportedTypes {
if supportedType == typeId {
return it, nil
}
}
}
return nil, ErrTagNotFound
}
// GetWithName returns information about the non-IFD tag given a tag name.
func (ti *TagIndex) GetWithName(ii *exifcommon.IfdIdentity, name string) (it *IndexedTag, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
if len(ti.tagsByIfdR) == 0 {
err := LoadStandardTags(ti)
log.PanicIf(err)
}
ifdPath := ii.UnindexedString()
it, found := ti.tagsByIfdR[ifdPath][name]
if found != true {
log.Panic(ErrTagNotFound)
}
return it, nil
}
// LoadStandardTags registers the tags that all devices/applications should
// support.
func LoadStandardTags(ti *TagIndex) (err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
// Read static data.
encodedIfds := make(map[string][]encodedTag)
err = yaml.Unmarshal([]byte(tagsYaml), encodedIfds)
log.PanicIf(err)
// Load structure.
count := 0
for ifdPath, tags := range encodedIfds {
for _, tagInfo := range tags {
tagId := uint16(tagInfo.Id)
tagName := tagInfo.Name
tagTypeName := tagInfo.TypeName
tagTypeNames := tagInfo.TypeNames
if tagTypeNames == nil {
if tagTypeName == "" {
log.Panicf("no tag-types were given when registering standard tag [%s] (0x%04x) [%s]", ifdPath, tagId, tagName)
}
tagTypeNames = []string{
tagTypeName,
}
} else if tagTypeName != "" {
log.Panicf("both 'type_names' and 'type_name' were given when registering standard tag [%s] (0x%04x) [%s]", ifdPath, tagId, tagName)
}
tagTypes := make([]exifcommon.TagTypePrimitive, 0)
for _, tagTypeName := range tagTypeNames {
// TODO(dustin): Discard unsupported types. This helps us with non-standard types that have actually been found in real data, that we ignore for right now. e.g. SSHORT, FLOAT, DOUBLE
tagTypeId, found := exifcommon.GetTypeByName(tagTypeName)
if found == false {
tagsLogger.Warningf(nil, "Type [%s] for tag [%s] being loaded is not valid and is being ignored.", tagTypeName, tagName)
continue
}
tagTypes = append(tagTypes, tagTypeId)
}
if len(tagTypes) == 0 {
tagsLogger.Warningf(nil, "Tag [%s] (0x%04x) [%s] being loaded does not have any supported types and will not be registered.", ifdPath, tagId, tagName)
continue
}
it := &IndexedTag{
IfdPath: ifdPath,
Id: tagId,
Name: tagName,
SupportedTypes: tagTypes,
}
err = ti.Add(it)
log.PanicIf(err)
count++
}
}
tagsLogger.Debugf(nil, "(%d) tags loaded.", count)
return nil
}

View File

@ -1,968 +0,0 @@
package exif
var (
// From assets/tags.yaml . Needs to be here so it's embedded in the binary.
tagsYaml = `
# Notes:
#
# This file was produced from http://www.exiv2.org/tags.html, using the included
# tool, though that document appears to have some duplicates when all IDs are
# supposed to be unique (EXIF information only has IDs, not IFDs; IFDs are
# determined by our pre-existing knowledge of those tags).
#
# The webpage that we've produced this file from appears to indicate that
# ImageWidth is represented by both 0x0100 and 0x0001 depending on whether the
# encoding is RGB or YCbCr.
IFD/Exif:
- id: 0x829a
name: ExposureTime
type_name: RATIONAL
- id: 0x829d
name: FNumber
type_name: RATIONAL
- id: 0x8822
name: ExposureProgram
type_name: SHORT
- id: 0x8824
name: SpectralSensitivity
type_name: ASCII
- id: 0x8827
name: ISOSpeedRatings
type_name: SHORT
- id: 0x8828
name: OECF
type_name: UNDEFINED
- id: 0x8830
name: SensitivityType
type_name: SHORT
- id: 0x8831
name: StandardOutputSensitivity
type_name: LONG
- id: 0x8832
name: RecommendedExposureIndex
type_name: LONG
- id: 0x8833
name: ISOSpeed
type_name: LONG
- id: 0x8834
name: ISOSpeedLatitudeyyy
type_name: LONG
- id: 0x8835
name: ISOSpeedLatitudezzz
type_name: LONG
- id: 0x9000
name: ExifVersion
type_name: UNDEFINED
- id: 0x9003
name: DateTimeOriginal
type_name: ASCII
- id: 0x9004
name: DateTimeDigitized
type_name: ASCII
- id: 0x9010
name: OffsetTime
type_name: ASCII
- id: 0x9011
name: OffsetTimeOriginal
type_name: ASCII
- id: 0x9012
name: OffsetTimeDigitized
type_name: ASCII
- id: 0x9101
name: ComponentsConfiguration
type_name: UNDEFINED
- id: 0x9102
name: CompressedBitsPerPixel
type_name: RATIONAL
- id: 0x9201
name: ShutterSpeedValue
type_name: SRATIONAL
- id: 0x9202
name: ApertureValue
type_name: RATIONAL
- id: 0x9203
name: BrightnessValue
type_name: SRATIONAL
- id: 0x9204
name: ExposureBiasValue
type_name: SRATIONAL
- id: 0x9205
name: MaxApertureValue
type_name: RATIONAL
- id: 0x9206
name: SubjectDistance
type_name: RATIONAL
- id: 0x9207
name: MeteringMode
type_name: SHORT
- id: 0x9208
name: LightSource
type_name: SHORT
- id: 0x9209
name: Flash
type_name: SHORT
- id: 0x920a
name: FocalLength
type_name: RATIONAL
- id: 0x9214
name: SubjectArea
type_name: SHORT
- id: 0x927c
name: MakerNote
type_name: UNDEFINED
- id: 0x9286
name: UserComment
type_name: UNDEFINED
- id: 0x9290
name: SubSecTime
type_name: ASCII
- id: 0x9291
name: SubSecTimeOriginal
type_name: ASCII
- id: 0x9292
name: SubSecTimeDigitized
type_name: ASCII
- id: 0xa000
name: FlashpixVersion
type_name: UNDEFINED
- id: 0xa001
name: ColorSpace
type_name: SHORT
- id: 0xa002
name: PixelXDimension
type_names: [LONG, SHORT]
- id: 0xa003
name: PixelYDimension
type_names: [LONG, SHORT]
- id: 0xa004
name: RelatedSoundFile
type_name: ASCII
- id: 0xa005
name: InteroperabilityTag
type_name: LONG
- id: 0xa20b
name: FlashEnergy
type_name: RATIONAL
- id: 0xa20c
name: SpatialFrequencyResponse
type_name: UNDEFINED
- id: 0xa20e
name: FocalPlaneXResolution
type_name: RATIONAL
- id: 0xa20f
name: FocalPlaneYResolution
type_name: RATIONAL
- id: 0xa210
name: FocalPlaneResolutionUnit
type_name: SHORT
- id: 0xa214
name: SubjectLocation
type_name: SHORT
- id: 0xa215
name: ExposureIndex
type_name: RATIONAL
- id: 0xa217
name: SensingMethod
type_name: SHORT
- id: 0xa300
name: FileSource
type_name: UNDEFINED
- id: 0xa301
name: SceneType
type_name: UNDEFINED
- id: 0xa302
name: CFAPattern
type_name: UNDEFINED
- id: 0xa401
name: CustomRendered
type_name: SHORT
- id: 0xa402
name: ExposureMode
type_name: SHORT
- id: 0xa403
name: WhiteBalance
type_name: SHORT
- id: 0xa404
name: DigitalZoomRatio
type_name: RATIONAL
- id: 0xa405
name: FocalLengthIn35mmFilm
type_name: SHORT
- id: 0xa406
name: SceneCaptureType
type_name: SHORT
- id: 0xa407
name: GainControl
type_name: SHORT
- id: 0xa408
name: Contrast
type_name: SHORT
- id: 0xa409
name: Saturation
type_name: SHORT
- id: 0xa40a
name: Sharpness
type_name: SHORT
- id: 0xa40b
name: DeviceSettingDescription
type_name: UNDEFINED
- id: 0xa40c
name: SubjectDistanceRange
type_name: SHORT
- id: 0xa420
name: ImageUniqueID
type_name: ASCII
- id: 0xa430
name: CameraOwnerName
type_name: ASCII
- id: 0xa431
name: BodySerialNumber
type_name: ASCII
- id: 0xa432
name: LensSpecification
type_name: RATIONAL
- id: 0xa433
name: LensMake
type_name: ASCII
- id: 0xa434
name: LensModel
type_name: ASCII
- id: 0xa435
name: LensSerialNumber
type_name: ASCII
IFD/GPSInfo:
- id: 0x0000
name: GPSVersionID
type_name: BYTE
- id: 0x0001
name: GPSLatitudeRef
type_name: ASCII
- id: 0x0002
name: GPSLatitude
type_name: RATIONAL
- id: 0x0003
name: GPSLongitudeRef
type_name: ASCII
- id: 0x0004
name: GPSLongitude
type_name: RATIONAL
- id: 0x0005
name: GPSAltitudeRef
type_name: BYTE
- id: 0x0006
name: GPSAltitude
type_name: RATIONAL
- id: 0x0007
name: GPSTimeStamp
type_name: RATIONAL
- id: 0x0008
name: GPSSatellites
type_name: ASCII
- id: 0x0009
name: GPSStatus
type_name: ASCII
- id: 0x000a
name: GPSMeasureMode
type_name: ASCII
- id: 0x000b
name: GPSDOP
type_name: RATIONAL
- id: 0x000c
name: GPSSpeedRef
type_name: ASCII
- id: 0x000d
name: GPSSpeed
type_name: RATIONAL
- id: 0x000e
name: GPSTrackRef
type_name: ASCII
- id: 0x000f
name: GPSTrack
type_name: RATIONAL
- id: 0x0010
name: GPSImgDirectionRef
type_name: ASCII
- id: 0x0011
name: GPSImgDirection
type_name: RATIONAL
- id: 0x0012
name: GPSMapDatum
type_name: ASCII
- id: 0x0013
name: GPSDestLatitudeRef
type_name: ASCII
- id: 0x0014
name: GPSDestLatitude
type_name: RATIONAL
- id: 0x0015
name: GPSDestLongitudeRef
type_name: ASCII
- id: 0x0016
name: GPSDestLongitude
type_name: RATIONAL
- id: 0x0017
name: GPSDestBearingRef
type_name: ASCII
- id: 0x0018
name: GPSDestBearing
type_name: RATIONAL
- id: 0x0019
name: GPSDestDistanceRef
type_name: ASCII
- id: 0x001a
name: GPSDestDistance
type_name: RATIONAL
- id: 0x001b
name: GPSProcessingMethod
type_name: UNDEFINED
- id: 0x001c
name: GPSAreaInformation
type_name: UNDEFINED
- id: 0x001d
name: GPSDateStamp
type_name: ASCII
- id: 0x001e
name: GPSDifferential
type_name: SHORT
IFD:
- id: 0x000b
name: ProcessingSoftware
type_name: ASCII
- id: 0x00fe
name: NewSubfileType
type_name: LONG
- id: 0x00ff
name: SubfileType
type_name: SHORT
- id: 0x0100
name: ImageWidth
type_names: [LONG, SHORT]
- id: 0x0101
name: ImageLength
type_names: [LONG, SHORT]
- id: 0x0102
name: BitsPerSample
type_name: SHORT
- id: 0x0103
name: Compression
type_name: SHORT
- id: 0x0106
name: PhotometricInterpretation
type_name: SHORT
- id: 0x0107
name: Thresholding
type_name: SHORT
- id: 0x0108
name: CellWidth
type_name: SHORT
- id: 0x0109
name: CellLength
type_name: SHORT
- id: 0x010a
name: FillOrder
type_name: SHORT
- id: 0x010d
name: DocumentName
type_name: ASCII
- id: 0x010e
name: ImageDescription
type_name: ASCII
- id: 0x010f
name: Make
type_name: ASCII
- id: 0x0110
name: Model
type_name: ASCII
- id: 0x0111
name: StripOffsets
type_names: [LONG, SHORT]
- id: 0x0112
name: Orientation
type_name: SHORT
- id: 0x0115
name: SamplesPerPixel
type_name: SHORT
- id: 0x0116
name: RowsPerStrip
type_names: [LONG, SHORT]
- id: 0x0117
name: StripByteCounts
type_names: [LONG, SHORT]
- id: 0x011a
name: XResolution
type_name: RATIONAL
- id: 0x011b
name: YResolution
type_name: RATIONAL
- id: 0x011c
name: PlanarConfiguration
type_name: SHORT
- id: 0x0122
name: GrayResponseUnit
type_name: SHORT
- id: 0x0123
name: GrayResponseCurve
type_name: SHORT
- id: 0x0124
name: T4Options
type_name: LONG
- id: 0x0125
name: T6Options
type_name: LONG
- id: 0x0128
name: ResolutionUnit
type_name: SHORT
- id: 0x0129
name: PageNumber
type_name: SHORT
- id: 0x012d
name: TransferFunction
type_name: SHORT
- id: 0x0131
name: Software
type_name: ASCII
- id: 0x0132
name: DateTime
type_name: ASCII
- id: 0x013b
name: Artist
type_name: ASCII
- id: 0x013c
name: HostComputer
type_name: ASCII
- id: 0x013d
name: Predictor
type_name: SHORT
- id: 0x013e
name: WhitePoint
type_name: RATIONAL
- id: 0x013f
name: PrimaryChromaticities
type_name: RATIONAL
- id: 0x0140
name: ColorMap
type_name: SHORT
- id: 0x0141
name: HalftoneHints
type_name: SHORT
- id: 0x0142
name: TileWidth
type_name: SHORT
- id: 0x0143
name: TileLength
type_name: SHORT
- id: 0x0144
name: TileOffsets
type_name: SHORT
- id: 0x0145
name: TileByteCounts
type_name: SHORT
- id: 0x014a
name: SubIFDs
type_name: LONG
- id: 0x014c
name: InkSet
type_name: SHORT
- id: 0x014d
name: InkNames
type_name: ASCII
- id: 0x014e
name: NumberOfInks
type_name: SHORT
- id: 0x0150
name: DotRange
type_name: BYTE
- id: 0x0151
name: TargetPrinter
type_name: ASCII
- id: 0x0152
name: ExtraSamples
type_name: SHORT
- id: 0x0153
name: SampleFormat
type_name: SHORT
- id: 0x0154
name: SMinSampleValue
type_name: SHORT
- id: 0x0155
name: SMaxSampleValue
type_name: SHORT
- id: 0x0156
name: TransferRange
type_name: SHORT
- id: 0x0157
name: ClipPath
type_name: BYTE
- id: 0x015a
name: Indexed
type_name: SHORT
- id: 0x015b
name: JPEGTables
type_name: UNDEFINED
- id: 0x015f
name: OPIProxy
type_name: SHORT
- id: 0x0200
name: JPEGProc
type_name: LONG
- id: 0x0201
name: JPEGInterchangeFormat
type_name: LONG
- id: 0x0202
name: JPEGInterchangeFormatLength
type_name: LONG
- id: 0x0203
name: JPEGRestartInterval
type_name: SHORT
- id: 0x0205
name: JPEGLosslessPredictors
type_name: SHORT
- id: 0x0206
name: JPEGPointTransforms
type_name: SHORT
- id: 0x0207
name: JPEGQTables
type_name: LONG
- id: 0x0208
name: JPEGDCTables
type_name: LONG
- id: 0x0209
name: JPEGACTables
type_name: LONG
- id: 0x0211
name: YCbCrCoefficients
type_name: RATIONAL
- id: 0x0212
name: YCbCrSubSampling
type_name: SHORT
- id: 0x0213
name: YCbCrPositioning
type_name: SHORT
- id: 0x0214
name: ReferenceBlackWhite
type_name: RATIONAL
- id: 0x02bc
name: XMLPacket
type_name: BYTE
- id: 0x4746
name: Rating
type_name: SHORT
- id: 0x4749
name: RatingPercent
type_name: SHORT
- id: 0x800d
name: ImageID
type_name: ASCII
- id: 0x828d
name: CFARepeatPatternDim
type_name: SHORT
- id: 0x828e
name: CFAPattern
type_name: BYTE
- id: 0x828f
name: BatteryLevel
type_name: RATIONAL
- id: 0x8298
name: Copyright
type_name: ASCII
- id: 0x829a
name: ExposureTime
# NOTE(dustin): SRATIONAL isn't mentioned in the standard, but we have seen it in real data.
type_names: [RATIONAL, SRATIONAL]
- id: 0x829d
name: FNumber
# NOTE(dustin): SRATIONAL isn't mentioned in the standard, but we have seen it in real data.
type_names: [RATIONAL, SRATIONAL]
- id: 0x83bb
name: IPTCNAA
type_name: LONG
- id: 0x8649
name: ImageResources
type_name: BYTE
- id: 0x8769
name: ExifTag
type_name: LONG
- id: 0x8773
name: InterColorProfile
type_name: UNDEFINED
- id: 0x8822
name: ExposureProgram
type_name: SHORT
- id: 0x8824
name: SpectralSensitivity
type_name: ASCII
- id: 0x8825
name: GPSTag
type_name: LONG
- id: 0x8827
name: ISOSpeedRatings
type_name: SHORT
- id: 0x8828
name: OECF
type_name: UNDEFINED
- id: 0x8829
name: Interlace
type_name: SHORT
- id: 0x882b
name: SelfTimerMode
type_name: SHORT
- id: 0x9003
name: DateTimeOriginal
type_name: ASCII
- id: 0x9102
name: CompressedBitsPerPixel
type_name: RATIONAL
- id: 0x9201
name: ShutterSpeedValue
type_name: SRATIONAL
- id: 0x9202
name: ApertureValue
type_name: RATIONAL
- id: 0x9203
name: BrightnessValue
type_name: SRATIONAL
- id: 0x9204
name: ExposureBiasValue
type_name: SRATIONAL
- id: 0x9205
name: MaxApertureValue
type_name: RATIONAL
- id: 0x9206
name: SubjectDistance
type_name: SRATIONAL
- id: 0x9207
name: MeteringMode
type_name: SHORT
- id: 0x9208
name: LightSource
type_name: SHORT
- id: 0x9209
name: Flash
type_name: SHORT
- id: 0x920a
name: FocalLength
type_name: RATIONAL
- id: 0x920b
name: FlashEnergy
type_name: RATIONAL
- id: 0x920c
name: SpatialFrequencyResponse
type_name: UNDEFINED
- id: 0x920d
name: Noise
type_name: UNDEFINED
- id: 0x920e
name: FocalPlaneXResolution
type_name: RATIONAL
- id: 0x920f
name: FocalPlaneYResolution
type_name: RATIONAL
- id: 0x9210
name: FocalPlaneResolutionUnit
type_name: SHORT
- id: 0x9211
name: ImageNumber
type_name: LONG
- id: 0x9212
name: SecurityClassification
type_name: ASCII
- id: 0x9213
name: ImageHistory
type_name: ASCII
- id: 0x9214
name: SubjectLocation
type_name: SHORT
- id: 0x9215
name: ExposureIndex
type_name: RATIONAL
- id: 0x9216
name: TIFFEPStandardID
type_name: BYTE
- id: 0x9217
name: SensingMethod
type_name: SHORT
- id: 0x9c9b
name: XPTitle
type_name: BYTE
- id: 0x9c9c
name: XPComment
type_name: BYTE
- id: 0x9c9d
name: XPAuthor
type_name: BYTE
- id: 0x9c9e
name: XPKeywords
type_name: BYTE
- id: 0x9c9f
name: XPSubject
type_name: BYTE
- id: 0xc4a5
name: PrintImageMatching
type_name: UNDEFINED
- id: 0xc612
name: DNGVersion
type_name: BYTE
- id: 0xc613
name: DNGBackwardVersion
type_name: BYTE
- id: 0xc614
name: UniqueCameraModel
type_name: ASCII
- id: 0xc615
name: LocalizedCameraModel
type_name: BYTE
- id: 0xc616
name: CFAPlaneColor
type_name: BYTE
- id: 0xc617
name: CFALayout
type_name: SHORT
- id: 0xc618
name: LinearizationTable
type_name: SHORT
- id: 0xc619
name: BlackLevelRepeatDim
type_name: SHORT
- id: 0xc61a
name: BlackLevel
type_name: RATIONAL
- id: 0xc61b
name: BlackLevelDeltaH
type_name: SRATIONAL
- id: 0xc61c
name: BlackLevelDeltaV
type_name: SRATIONAL
- id: 0xc61d
name: WhiteLevel
type_name: SHORT
- id: 0xc61e
name: DefaultScale
type_name: RATIONAL
- id: 0xc61f
name: DefaultCropOrigin
type_name: SHORT
- id: 0xc620
name: DefaultCropSize
type_name: SHORT
- id: 0xc621
name: ColorMatrix1
type_name: SRATIONAL
- id: 0xc622
name: ColorMatrix2
type_name: SRATIONAL
- id: 0xc623
name: CameraCalibration1
type_name: SRATIONAL
- id: 0xc624
name: CameraCalibration2
type_name: SRATIONAL
- id: 0xc625
name: ReductionMatrix1
type_name: SRATIONAL
- id: 0xc626
name: ReductionMatrix2
type_name: SRATIONAL
- id: 0xc627
name: AnalogBalance
type_name: RATIONAL
- id: 0xc628
name: AsShotNeutral
type_name: SHORT
- id: 0xc629
name: AsShotWhiteXY
type_name: RATIONAL
- id: 0xc62a
name: BaselineExposure
type_name: SRATIONAL
- id: 0xc62b
name: BaselineNoise
type_name: RATIONAL
- id: 0xc62c
name: BaselineSharpness
type_name: RATIONAL
- id: 0xc62d
name: BayerGreenSplit
type_name: LONG
- id: 0xc62e
name: LinearResponseLimit
type_name: RATIONAL
- id: 0xc62f
name: CameraSerialNumber
type_name: ASCII
- id: 0xc630
name: LensInfo
type_name: RATIONAL
- id: 0xc631
name: ChromaBlurRadius
type_name: RATIONAL
- id: 0xc632
name: AntiAliasStrength
type_name: RATIONAL
- id: 0xc633
name: ShadowScale
type_name: SRATIONAL
- id: 0xc634
name: DNGPrivateData
type_name: BYTE
- id: 0xc635
name: MakerNoteSafety
type_name: SHORT
- id: 0xc65a
name: CalibrationIlluminant1
type_name: SHORT
- id: 0xc65b
name: CalibrationIlluminant2
type_name: SHORT
- id: 0xc65c
name: BestQualityScale
type_name: RATIONAL
- id: 0xc65d
name: RawDataUniqueID
type_name: BYTE
- id: 0xc68b
name: OriginalRawFileName
type_name: BYTE
- id: 0xc68c
name: OriginalRawFileData
type_name: UNDEFINED
- id: 0xc68d
name: ActiveArea
type_name: SHORT
- id: 0xc68e
name: MaskedAreas
type_name: SHORT
- id: 0xc68f
name: AsShotICCProfile
type_name: UNDEFINED
- id: 0xc690
name: AsShotPreProfileMatrix
type_name: SRATIONAL
- id: 0xc691
name: CurrentICCProfile
type_name: UNDEFINED
- id: 0xc692
name: CurrentPreProfileMatrix
type_name: SRATIONAL
- id: 0xc6bf
name: ColorimetricReference
type_name: SHORT
- id: 0xc6f3
name: CameraCalibrationSignature
type_name: BYTE
- id: 0xc6f4
name: ProfileCalibrationSignature
type_name: BYTE
- id: 0xc6f6
name: AsShotProfileName
type_name: BYTE
- id: 0xc6f7
name: NoiseReductionApplied
type_name: RATIONAL
- id: 0xc6f8
name: ProfileName
type_name: BYTE
- id: 0xc6f9
name: ProfileHueSatMapDims
type_name: LONG
- id: 0xc6fd
name: ProfileEmbedPolicy
type_name: LONG
- id: 0xc6fe
name: ProfileCopyright
type_name: BYTE
- id: 0xc714
name: ForwardMatrix1
type_name: SRATIONAL
- id: 0xc715
name: ForwardMatrix2
type_name: SRATIONAL
- id: 0xc716
name: PreviewApplicationName
type_name: BYTE
- id: 0xc717
name: PreviewApplicationVersion
type_name: BYTE
- id: 0xc718
name: PreviewSettingsName
type_name: BYTE
- id: 0xc719
name: PreviewSettingsDigest
type_name: BYTE
- id: 0xc71a
name: PreviewColorSpace
type_name: LONG
- id: 0xc71b
name: PreviewDateTime
type_name: ASCII
- id: 0xc71c
name: RawImageDigest
type_name: UNDEFINED
- id: 0xc71d
name: OriginalRawFileDigest
type_name: UNDEFINED
- id: 0xc71e
name: SubTileBlockSize
type_name: LONG
- id: 0xc71f
name: RowInterleaveFactor
type_name: LONG
- id: 0xc725
name: ProfileLookTableDims
type_name: LONG
- id: 0xc740
name: OpcodeList1
type_name: UNDEFINED
- id: 0xc741
name: OpcodeList2
type_name: UNDEFINED
- id: 0xc74e
name: OpcodeList3
type_name: UNDEFINED
# This tag may be used to specify the size of raster pixel spacing in the
# model space units, when the raster space can be embedded in the model space
# coordinate system without rotation, and consists of the following 3 values:
# ModelPixelScaleTag = (ScaleX, ScaleY, ScaleZ)
# where ScaleX and ScaleY give the horizontal and vertical spacing of raster
# pixels. The ScaleZ is primarily used to map the pixel value of a digital
# elevation model into the correct Z-scale, and so for most other purposes
# this value should be zero (since most model spaces are 2-D, with Z=0).
# Source: http://geotiff.maptools.org/spec/geotiff2.6.html#2.6.1
- id: 0x830e
name: ModelPixelScaleTag
type_name: DOUBLE
# This tag stores raster->model tiepoint pairs in the order
# ModelTiepointTag = (...,I,J,K, X,Y,Z...),
# where (I,J,K) is the point at location (I,J) in raster space with
# pixel-value K, and (X,Y,Z) is a vector in model space. In most cases the
# model space is only two-dimensional, in which case both K and Z should be
# set to zero; this third dimension is provided in anticipation of future
# support for 3D digital elevation models and vertical coordinate systems.
# Source: http://geotiff.maptools.org/spec/geotiff2.6.html#2.6.1
- id: 0x8482
name: ModelTiepointTag
type_name: DOUBLE
# This tag may be used to specify the transformation matrix between the
# raster space (and its dependent pixel-value space) and the (possibly 3D)
# model space.
# Source: http://geotiff.maptools.org/spec/geotiff2.6.html#2.6.1
- id: 0x85d8
name: ModelTransformationTag
type_name: DOUBLE
IFD/Exif/Iop:
- id: 0x0001
name: InteroperabilityIndex
type_name: ASCII
- id: 0x0002
name: InteroperabilityVersion
type_name: UNDEFINED
- id: 0x1000
name: RelatedImageFileFormat
type_name: ASCII
- id: 0x1001
name: RelatedImageWidth
type_name: LONG
- id: 0x1002
name: RelatedImageLength
type_name: LONG
`
)

View File

@ -1,188 +0,0 @@
package exif
import (
"path"
"reflect"
"testing"
"io/ioutil"
"github.com/dsoprea/go-logging"
"github.com/dsoprea/go-exif/v3/common"
)
var (
testExifData []byte
)
func getExifSimpleTestIb() *IfdBuilder {
defer func() {
if state := recover(); state != nil {
err := log.Wrap(state.(error))
log.Panic(err)
}
}()
im := exifcommon.NewIfdMapping()
err := exifcommon.LoadStandardIfds(im)
log.PanicIf(err)
ti := NewTagIndex()
ib := NewIfdBuilder(im, ti, exifcommon.IfdStandardIfdIdentity, exifcommon.TestDefaultByteOrder)
err = ib.AddStandard(0x000b, "asciivalue")
log.PanicIf(err)
err = ib.AddStandard(0x00ff, []uint16{0x1122})
log.PanicIf(err)
err = ib.AddStandard(0x0100, []uint32{0x33445566})
log.PanicIf(err)
err = ib.AddStandard(0x013e, []exifcommon.Rational{{Numerator: 0x11112222, Denominator: 0x33334444}})
log.PanicIf(err)
return ib
}
func getExifSimpleTestIbBytes() []byte {
defer func() {
if state := recover(); state != nil {
err := log.Wrap(state.(error))
log.Panic(err)
}
}()
im := exifcommon.NewIfdMapping()
err := exifcommon.LoadStandardIfds(im)
log.PanicIf(err)
ti := NewTagIndex()
ib := NewIfdBuilder(im, ti, exifcommon.IfdStandardIfdIdentity, exifcommon.TestDefaultByteOrder)
err = ib.AddStandard(0x000b, "asciivalue")
log.PanicIf(err)
err = ib.AddStandard(0x00ff, []uint16{0x1122})
log.PanicIf(err)
err = ib.AddStandard(0x0100, []uint32{0x33445566})
log.PanicIf(err)
err = ib.AddStandard(0x013e, []exifcommon.Rational{{Numerator: 0x11112222, Denominator: 0x33334444}})
log.PanicIf(err)
ibe := NewIfdByteEncoder()
exifData, err := ibe.EncodeToExif(ib)
log.PanicIf(err)
return exifData
}
func validateExifSimpleTestIb(exifData []byte, t *testing.T) {
defer func() {
if state := recover(); state != nil {
err := log.Wrap(state.(error))
log.Panic(err)
}
}()
im := exifcommon.NewIfdMapping()
err := exifcommon.LoadStandardIfds(im)
log.PanicIf(err)
ti := NewTagIndex()
eh, index, err := Collect(im, ti, exifData)
log.PanicIf(err)
if eh.ByteOrder != exifcommon.TestDefaultByteOrder {
t.Fatalf("EXIF byte-order is not correct: %v", eh.ByteOrder)
} else if eh.FirstIfdOffset != ExifDefaultFirstIfdOffset {
t.Fatalf("EXIF first IFD-offset not correct: (0x%02x)", eh.FirstIfdOffset)
}
if len(index.Ifds) != 1 {
t.Fatalf("There wasn't exactly one IFD decoded: (%d)", len(index.Ifds))
}
ifd := index.RootIfd
if ifd.ByteOrder() != exifcommon.TestDefaultByteOrder {
t.Fatalf("IFD byte-order not correct.")
} else if ifd.ifdIdentity.UnindexedString() != exifcommon.IfdStandardIfdIdentity.UnindexedString() {
t.Fatalf("IFD name not correct.")
} else if ifd.ifdIdentity.Index() != 0 {
t.Fatalf("IFD index not zero: (%d)", ifd.ifdIdentity.Index())
} else if ifd.Offset() != uint32(0x0008) {
t.Fatalf("IFD offset not correct.")
} else if len(ifd.Entries()) != 4 {
t.Fatalf("IFD number of entries not correct: (%d)", len(ifd.Entries()))
} else if ifd.nextIfdOffset != uint32(0) {
t.Fatalf("Next-IFD offset is non-zero.")
} else if ifd.nextIfd != nil {
t.Fatalf("Next-IFD pointer is non-nil.")
}
// Verify the values by using the actual, original types (this is awesome).
expected := []struct {
tagId uint16
value interface{}
}{
{tagId: 0x000b, value: "asciivalue"},
{tagId: 0x00ff, value: []uint16{0x1122}},
{tagId: 0x0100, value: []uint32{0x33445566}},
{tagId: 0x013e, value: []exifcommon.Rational{{Numerator: 0x11112222, Denominator: 0x33334444}}},
}
for i, ite := range ifd.Entries() {
if ite.TagId() != expected[i].tagId {
t.Fatalf("Tag-ID for entry (%d) not correct: (0x%02x) != (0x%02x)", i, ite.TagId(), expected[i].tagId)
}
value, err := ite.Value()
log.PanicIf(err)
if reflect.DeepEqual(value, expected[i].value) != true {
t.Fatalf("Value for entry (%d) not correct: [%v] != [%v]", i, value, expected[i].value)
}
}
}
func getTestImageFilepath() string {
assetsPath := exifcommon.GetTestAssetsPath()
testImageFilepath := path.Join(assetsPath, "NDM_8901.jpg")
return testImageFilepath
}
func getTestExifData() []byte {
if testExifData == nil {
assetsPath := exifcommon.GetTestAssetsPath()
filepath := path.Join(assetsPath, "NDM_8901.jpg.exif")
var err error
testExifData, err = ioutil.ReadFile(filepath)
log.PanicIf(err)
}
return testExifData
}
func getTestGpsImageFilepath() string {
assetsPath := exifcommon.GetTestAssetsPath()
testGpsImageFilepath := path.Join(assetsPath, "gps.jpg")
return testGpsImageFilepath
}
func getTestGeotiffFilepath() string {
assetsPath := exifcommon.GetTestAssetsPath()
testGeotiffFilepath := path.Join(assetsPath, "geotiff_example.tif")
return testGeotiffFilepath
}

View File

@ -1,4 +0,0 @@
## 0xa40b
The specification is not specific/clear enough to be handled. Without a working example ,we're deferring until some point in the future when either we or someone else has a better understanding.

View File

@ -1,62 +0,0 @@
package exifundefined
import (
"encoding/binary"
"github.com/dsoprea/go-logging"
"github.com/dsoprea/go-exif/v3/common"
)
// Encode encodes the given encodeable undefined value to bytes.
func Encode(value EncodeableValue, byteOrder binary.ByteOrder) (encoded []byte, unitCount uint32, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
encoderName := value.EncoderName()
encoder, found := encoders[encoderName]
if found == false {
log.Panicf("no encoder registered for type [%s]", encoderName)
}
encoded, unitCount, err = encoder.Encode(value, byteOrder)
log.PanicIf(err)
return encoded, unitCount, nil
}
// Decode constructs a value from raw encoded bytes
func Decode(valueContext *exifcommon.ValueContext) (value EncodeableValue, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
uth := UndefinedTagHandle{
IfdPath: valueContext.IfdPath(),
TagId: valueContext.TagId(),
}
decoder, found := decoders[uth]
if found == false {
// We have no choice but to return the error. We have no way of knowing how
// much data there is without already knowing what data-type this tag is.
return nil, exifcommon.ErrUnhandledUndefinedTypedTag
}
value, err = decoder.Decode(valueContext)
if err != nil {
if err == ErrUnparseableValue {
return nil, err
}
log.Panic(err)
}
return value, nil
}

View File

@ -1,148 +0,0 @@
package exifundefined
import (
"bytes"
"fmt"
"encoding/binary"
"github.com/dsoprea/go-logging"
"github.com/dsoprea/go-exif/v3/common"
)
type Tag8828Oecf struct {
Columns uint16
Rows uint16
ColumnNames []string
Values []exifcommon.SignedRational
}
func (oecf Tag8828Oecf) String() string {
return fmt.Sprintf("Tag8828Oecf<COLUMNS=(%d) ROWS=(%d)>", oecf.Columns, oecf.Rows)
}
func (oecf Tag8828Oecf) EncoderName() string {
return "Codec8828Oecf"
}
type Codec8828Oecf struct {
}
func (Codec8828Oecf) Encode(value interface{}, byteOrder binary.ByteOrder) (encoded []byte, unitCount uint32, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
// TODO(dustin): Add test
oecf, ok := value.(Tag8828Oecf)
if ok == false {
log.Panicf("can only encode a Tag8828Oecf")
}
b := new(bytes.Buffer)
err = binary.Write(b, byteOrder, oecf.Columns)
log.PanicIf(err)
err = binary.Write(b, byteOrder, oecf.Rows)
log.PanicIf(err)
for _, name := range oecf.ColumnNames {
_, err := b.Write([]byte(name))
log.PanicIf(err)
_, err = b.Write([]byte{0})
log.PanicIf(err)
}
ve := exifcommon.NewValueEncoder(byteOrder)
ed, err := ve.Encode(oecf.Values)
log.PanicIf(err)
_, err = b.Write(ed.Encoded)
log.PanicIf(err)
return b.Bytes(), uint32(b.Len()), nil
}
func (Codec8828Oecf) Decode(valueContext *exifcommon.ValueContext) (value EncodeableValue, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
// TODO(dustin): Add test using known good data.
valueContext.SetUndefinedValueType(exifcommon.TypeByte)
valueBytes, err := valueContext.ReadBytes()
log.PanicIf(err)
oecf := Tag8828Oecf{}
oecf.Columns = valueContext.ByteOrder().Uint16(valueBytes[0:2])
oecf.Rows = valueContext.ByteOrder().Uint16(valueBytes[2:4])
columnNames := make([]string, oecf.Columns)
// startAt is where the current column name starts.
startAt := 4
// offset is our current position.
offset := startAt
currentColumnNumber := uint16(0)
for currentColumnNumber < oecf.Columns {
if valueBytes[offset] == 0 {
columnName := string(valueBytes[startAt:offset])
if len(columnName) == 0 {
log.Panicf("SFR column (%d) has zero length", currentColumnNumber)
}
columnNames[currentColumnNumber] = columnName
currentColumnNumber++
offset++
startAt = offset
continue
}
offset++
}
oecf.ColumnNames = columnNames
rawRationalBytes := valueBytes[offset:]
rationalSize := exifcommon.TypeSignedRational.Size()
if len(rawRationalBytes)%rationalSize > 0 {
log.Panicf("OECF signed-rationals not aligned: (%d) %% (%d) > 0", len(rawRationalBytes), rationalSize)
}
rationalCount := len(rawRationalBytes) / rationalSize
parser := new(exifcommon.Parser)
byteOrder := valueContext.ByteOrder()
items, err := parser.ParseSignedRationals(rawRationalBytes, uint32(rationalCount), byteOrder)
log.PanicIf(err)
oecf.Values = items
return oecf, nil
}
func init() {
registerDecoder(
exifcommon.IfdExifStandardIfdIdentity.UnindexedString(),
0x8828,
Codec8828Oecf{})
}

View File

@ -1,69 +0,0 @@
package exifundefined
import (
"encoding/binary"
"github.com/dsoprea/go-logging"
"github.com/dsoprea/go-exif/v3/common"
)
type Tag9000ExifVersion struct {
ExifVersion string
}
func (Tag9000ExifVersion) EncoderName() string {
return "Codec9000ExifVersion"
}
func (ev Tag9000ExifVersion) String() string {
return ev.ExifVersion
}
type Codec9000ExifVersion struct {
}
func (Codec9000ExifVersion) Encode(value interface{}, byteOrder binary.ByteOrder) (encoded []byte, unitCount uint32, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
s, ok := value.(Tag9000ExifVersion)
if ok == false {
log.Panicf("can only encode a Tag9000ExifVersion")
}
return []byte(s.ExifVersion), uint32(len(s.ExifVersion)), nil
}
func (Codec9000ExifVersion) Decode(valueContext *exifcommon.ValueContext) (value EncodeableValue, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
valueContext.SetUndefinedValueType(exifcommon.TypeAsciiNoNul)
valueString, err := valueContext.ReadAsciiNoNul()
log.PanicIf(err)
ev := Tag9000ExifVersion{
ExifVersion: valueString,
}
return ev, nil
}
func init() {
registerEncoder(
Tag9000ExifVersion{},
Codec9000ExifVersion{})
registerDecoder(
exifcommon.IfdExifStandardIfdIdentity.UnindexedString(),
0x9000,
Codec9000ExifVersion{})
}

View File

@ -1,124 +0,0 @@
package exifundefined
import (
"bytes"
"fmt"
"encoding/binary"
"github.com/dsoprea/go-logging"
"github.com/dsoprea/go-exif/v3/common"
)
const (
TagUndefinedType_9101_ComponentsConfiguration_Channel_Y = 0x1
TagUndefinedType_9101_ComponentsConfiguration_Channel_Cb = 0x2
TagUndefinedType_9101_ComponentsConfiguration_Channel_Cr = 0x3
TagUndefinedType_9101_ComponentsConfiguration_Channel_R = 0x4
TagUndefinedType_9101_ComponentsConfiguration_Channel_G = 0x5
TagUndefinedType_9101_ComponentsConfiguration_Channel_B = 0x6
)
const (
TagUndefinedType_9101_ComponentsConfiguration_OTHER = iota
TagUndefinedType_9101_ComponentsConfiguration_RGB = iota
TagUndefinedType_9101_ComponentsConfiguration_YCBCR = iota
)
var (
TagUndefinedType_9101_ComponentsConfiguration_Names = map[int]string{
TagUndefinedType_9101_ComponentsConfiguration_OTHER: "OTHER",
TagUndefinedType_9101_ComponentsConfiguration_RGB: "RGB",
TagUndefinedType_9101_ComponentsConfiguration_YCBCR: "YCBCR",
}
TagUndefinedType_9101_ComponentsConfiguration_Configurations = map[int][]byte{
TagUndefinedType_9101_ComponentsConfiguration_RGB: {
TagUndefinedType_9101_ComponentsConfiguration_Channel_R,
TagUndefinedType_9101_ComponentsConfiguration_Channel_G,
TagUndefinedType_9101_ComponentsConfiguration_Channel_B,
0,
},
TagUndefinedType_9101_ComponentsConfiguration_YCBCR: {
TagUndefinedType_9101_ComponentsConfiguration_Channel_Y,
TagUndefinedType_9101_ComponentsConfiguration_Channel_Cb,
TagUndefinedType_9101_ComponentsConfiguration_Channel_Cr,
0,
},
}
)
type TagExif9101ComponentsConfiguration struct {
ConfigurationId int
ConfigurationBytes []byte
}
func (TagExif9101ComponentsConfiguration) EncoderName() string {
return "CodecExif9101ComponentsConfiguration"
}
func (cc TagExif9101ComponentsConfiguration) String() string {
return fmt.Sprintf("Exif9101ComponentsConfiguration<ID=[%s] BYTES=%v>", TagUndefinedType_9101_ComponentsConfiguration_Names[cc.ConfigurationId], cc.ConfigurationBytes)
}
type CodecExif9101ComponentsConfiguration struct {
}
func (CodecExif9101ComponentsConfiguration) Encode(value interface{}, byteOrder binary.ByteOrder) (encoded []byte, unitCount uint32, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
cc, ok := value.(TagExif9101ComponentsConfiguration)
if ok == false {
log.Panicf("can only encode a TagExif9101ComponentsConfiguration")
}
return cc.ConfigurationBytes, uint32(len(cc.ConfigurationBytes)), nil
}
func (CodecExif9101ComponentsConfiguration) Decode(valueContext *exifcommon.ValueContext) (value EncodeableValue, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
valueContext.SetUndefinedValueType(exifcommon.TypeByte)
valueBytes, err := valueContext.ReadBytes()
log.PanicIf(err)
for configurationId, configurationBytes := range TagUndefinedType_9101_ComponentsConfiguration_Configurations {
if bytes.Equal(configurationBytes, valueBytes) == true {
cc := TagExif9101ComponentsConfiguration{
ConfigurationId: configurationId,
ConfigurationBytes: valueBytes,
}
return cc, nil
}
}
cc := TagExif9101ComponentsConfiguration{
ConfigurationId: TagUndefinedType_9101_ComponentsConfiguration_OTHER,
ConfigurationBytes: valueBytes,
}
return cc, nil
}
func init() {
registerEncoder(
TagExif9101ComponentsConfiguration{},
CodecExif9101ComponentsConfiguration{})
registerDecoder(
exifcommon.IfdExifStandardIfdIdentity.UnindexedString(),
0x9101,
CodecExif9101ComponentsConfiguration{})
}

View File

@ -1,114 +0,0 @@
package exifundefined
import (
"fmt"
"strings"
"crypto/sha1"
"encoding/binary"
"github.com/dsoprea/go-logging"
"github.com/dsoprea/go-exif/v3/common"
)
type Tag927CMakerNote struct {
MakerNoteType []byte
MakerNoteBytes []byte
}
func (Tag927CMakerNote) EncoderName() string {
return "Codec927CMakerNote"
}
func (mn Tag927CMakerNote) String() string {
parts := make([]string, len(mn.MakerNoteType))
for i, c := range mn.MakerNoteType {
parts[i] = fmt.Sprintf("%02x", c)
}
h := sha1.New()
_, err := h.Write(mn.MakerNoteBytes)
log.PanicIf(err)
digest := h.Sum(nil)
return fmt.Sprintf("MakerNote<TYPE-ID=[%s] LEN=(%d) SHA1=[%020x]>", strings.Join(parts, " "), len(mn.MakerNoteBytes), digest)
}
type Codec927CMakerNote struct {
}
func (Codec927CMakerNote) Encode(value interface{}, byteOrder binary.ByteOrder) (encoded []byte, unitCount uint32, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
mn, ok := value.(Tag927CMakerNote)
if ok == false {
log.Panicf("can only encode a Tag927CMakerNote")
}
// TODO(dustin): Confirm this size against the specification.
return mn.MakerNoteBytes, uint32(len(mn.MakerNoteBytes)), nil
}
func (Codec927CMakerNote) Decode(valueContext *exifcommon.ValueContext) (value EncodeableValue, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
// MakerNote
// TODO(dustin): !! This is the Wild Wild West. This very well might be a child IFD, but any and all OEM's define their own formats. If we're going to be writing changes and this is complete EXIF (which may not have the first eight bytes), it might be fine. However, if these are just IFDs they'll be relative to the main EXIF, this will invalidate the MakerNote data for IFDs and any other implementations that use offsets unless we can interpret them all. It be best to return to this later and just exclude this from being written for now, though means a loss of a wealth of image metadata.
// -> We can also just blindly try to interpret as an IFD and just validate that it's looks good (maybe it will even have a 'next ifd' pointer that we can validate is 0x0).
valueContext.SetUndefinedValueType(exifcommon.TypeByte)
valueBytes, err := valueContext.ReadBytes()
log.PanicIf(err)
// TODO(dustin): Doesn't work, but here as an example.
// ie := NewIfdEnumerate(valueBytes, byteOrder)
// // TODO(dustin): !! Validate types (might have proprietary types, but it might be worth splitting the list between valid and not valid; maybe fail if a certain proportion are invalid, or maybe aren't less then a certain small integer)?
// ii, err := ie.Collect(0x0)
// for _, entry := range ii.RootIfd.Entries {
// fmt.Printf("ENTRY: 0x%02x %d\n", entry.TagId, entry.TagType)
// }
var makerNoteType []byte
if len(valueBytes) >= 20 {
makerNoteType = valueBytes[:20]
} else {
makerNoteType = valueBytes
}
mn := Tag927CMakerNote{
MakerNoteType: makerNoteType,
// MakerNoteBytes has the whole length of bytes. There's always
// the chance that the first 20 bytes includes actual data.
MakerNoteBytes: valueBytes,
}
return mn, nil
}
func init() {
registerEncoder(
Tag927CMakerNote{},
Codec927CMakerNote{})
registerDecoder(
exifcommon.IfdExifStandardIfdIdentity.UnindexedString(),
0x927c,
Codec927CMakerNote{})
}

View File

@ -1,142 +0,0 @@
package exifundefined
import (
"bytes"
"fmt"
"encoding/binary"
"github.com/dsoprea/go-logging"
"github.com/dsoprea/go-exif/v3/common"
)
var (
exif9286Logger = log.NewLogger("exifundefined.exif_9286_user_comment")
)
const (
TagUndefinedType_9286_UserComment_Encoding_ASCII = iota
TagUndefinedType_9286_UserComment_Encoding_JIS = iota
TagUndefinedType_9286_UserComment_Encoding_UNICODE = iota
TagUndefinedType_9286_UserComment_Encoding_UNDEFINED = iota
)
var (
TagUndefinedType_9286_UserComment_Encoding_Names = map[int]string{
TagUndefinedType_9286_UserComment_Encoding_ASCII: "ASCII",
TagUndefinedType_9286_UserComment_Encoding_JIS: "JIS",
TagUndefinedType_9286_UserComment_Encoding_UNICODE: "UNICODE",
TagUndefinedType_9286_UserComment_Encoding_UNDEFINED: "UNDEFINED",
}
TagUndefinedType_9286_UserComment_Encodings = map[int][]byte{
TagUndefinedType_9286_UserComment_Encoding_ASCII: {'A', 'S', 'C', 'I', 'I', 0, 0, 0},
TagUndefinedType_9286_UserComment_Encoding_JIS: {'J', 'I', 'S', 0, 0, 0, 0, 0},
TagUndefinedType_9286_UserComment_Encoding_UNICODE: {'U', 'n', 'i', 'c', 'o', 'd', 'e', 0},
TagUndefinedType_9286_UserComment_Encoding_UNDEFINED: {0, 0, 0, 0, 0, 0, 0, 0},
}
)
type Tag9286UserComment struct {
EncodingType int
EncodingBytes []byte
}
func (Tag9286UserComment) EncoderName() string {
return "Codec9286UserComment"
}
func (uc Tag9286UserComment) String() string {
var valuePhrase string
if uc.EncodingType == TagUndefinedType_9286_UserComment_Encoding_ASCII {
return fmt.Sprintf("[ASCII] %s", string(uc.EncodingBytes))
} else {
if len(uc.EncodingBytes) <= 8 {
valuePhrase = fmt.Sprintf("%v", uc.EncodingBytes)
} else {
valuePhrase = fmt.Sprintf("%v...", uc.EncodingBytes[:8])
}
}
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))
}
type Codec9286UserComment struct {
}
func (Codec9286UserComment) Encode(value interface{}, byteOrder binary.ByteOrder) (encoded []byte, unitCount uint32, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
uc, ok := value.(Tag9286UserComment)
if ok == false {
log.Panicf("can only encode a Tag9286UserComment")
}
encodingTypeBytes, found := TagUndefinedType_9286_UserComment_Encodings[uc.EncodingType]
if found == false {
log.Panicf("encoding-type not valid for unknown-type tag 9286 (UserComment): (%d)", uc.EncodingType)
}
encoded = make([]byte, len(uc.EncodingBytes)+8)
copy(encoded[:8], encodingTypeBytes)
copy(encoded[8:], uc.EncodingBytes)
// TODO(dustin): Confirm this size against the specification.
return encoded, uint32(len(encoded)), nil
}
func (Codec9286UserComment) Decode(valueContext *exifcommon.ValueContext) (value EncodeableValue, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
valueContext.SetUndefinedValueType(exifcommon.TypeByte)
valueBytes, err := valueContext.ReadBytes()
log.PanicIf(err)
if len(valueBytes) < 8 {
return nil, ErrUnparseableValue
}
unknownUc := Tag9286UserComment{
EncodingType: TagUndefinedType_9286_UserComment_Encoding_UNDEFINED,
EncodingBytes: []byte{},
}
encoding := valueBytes[:8]
for encodingIndex, encodingBytes := range TagUndefinedType_9286_UserComment_Encodings {
if bytes.Compare(encoding, encodingBytes) == 0 {
uc := Tag9286UserComment{
EncodingType: encodingIndex,
EncodingBytes: valueBytes[8:],
}
return uc, nil
}
}
exif9286Logger.Warningf(nil, "User-comment encoding not valid. Returning 'unknown' type (the default).")
return unknownUc, nil
}
func init() {
registerEncoder(
Tag9286UserComment{},
Codec9286UserComment{})
registerDecoder(
exifcommon.IfdExifStandardIfdIdentity.UnindexedString(),
0x9286,
Codec9286UserComment{})
}

View File

@ -1,69 +0,0 @@
package exifundefined
import (
"encoding/binary"
"github.com/dsoprea/go-logging"
"github.com/dsoprea/go-exif/v3/common"
)
type TagA000FlashpixVersion struct {
FlashpixVersion string
}
func (TagA000FlashpixVersion) EncoderName() string {
return "CodecA000FlashpixVersion"
}
func (fv TagA000FlashpixVersion) String() string {
return fv.FlashpixVersion
}
type CodecA000FlashpixVersion struct {
}
func (CodecA000FlashpixVersion) Encode(value interface{}, byteOrder binary.ByteOrder) (encoded []byte, unitCount uint32, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
s, ok := value.(TagA000FlashpixVersion)
if ok == false {
log.Panicf("can only encode a TagA000FlashpixVersion")
}
return []byte(s.FlashpixVersion), uint32(len(s.FlashpixVersion)), nil
}
func (CodecA000FlashpixVersion) Decode(valueContext *exifcommon.ValueContext) (value EncodeableValue, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
valueContext.SetUndefinedValueType(exifcommon.TypeAsciiNoNul)
valueString, err := valueContext.ReadAsciiNoNul()
log.PanicIf(err)
fv := TagA000FlashpixVersion{
FlashpixVersion: valueString,
}
return fv, nil
}
func init() {
registerEncoder(
TagA000FlashpixVersion{},
CodecA000FlashpixVersion{})
registerDecoder(
exifcommon.IfdExifStandardIfdIdentity.UnindexedString(),
0xa000,
CodecA000FlashpixVersion{})
}

View File

@ -1,160 +0,0 @@
package exifundefined
import (
"bytes"
"fmt"
"encoding/binary"
"github.com/dsoprea/go-logging"
"github.com/dsoprea/go-exif/v3/common"
)
type TagA20CSpatialFrequencyResponse struct {
Columns uint16
Rows uint16
ColumnNames []string
Values []exifcommon.Rational
}
func (TagA20CSpatialFrequencyResponse) EncoderName() string {
return "CodecA20CSpatialFrequencyResponse"
}
func (sfr TagA20CSpatialFrequencyResponse) String() string {
return fmt.Sprintf("CodecA20CSpatialFrequencyResponse<COLUMNS=(%d) ROWS=(%d)>", sfr.Columns, sfr.Rows)
}
type CodecA20CSpatialFrequencyResponse struct {
}
func (CodecA20CSpatialFrequencyResponse) Encode(value interface{}, byteOrder binary.ByteOrder) (encoded []byte, unitCount uint32, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
// TODO(dustin): Add test.
sfr, ok := value.(TagA20CSpatialFrequencyResponse)
if ok == false {
log.Panicf("can only encode a TagA20CSpatialFrequencyResponse")
}
b := new(bytes.Buffer)
err = binary.Write(b, byteOrder, sfr.Columns)
log.PanicIf(err)
err = binary.Write(b, byteOrder, sfr.Rows)
log.PanicIf(err)
// Write columns.
for _, name := range sfr.ColumnNames {
_, err := b.WriteString(name)
log.PanicIf(err)
err = b.WriteByte(0)
log.PanicIf(err)
}
// Write values.
ve := exifcommon.NewValueEncoder(byteOrder)
ed, err := ve.Encode(sfr.Values)
log.PanicIf(err)
_, err = b.Write(ed.Encoded)
log.PanicIf(err)
encoded = b.Bytes()
// TODO(dustin): Confirm this size against the specification.
return encoded, uint32(len(encoded)), nil
}
func (CodecA20CSpatialFrequencyResponse) Decode(valueContext *exifcommon.ValueContext) (value EncodeableValue, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
// TODO(dustin): Add test using known good data.
byteOrder := valueContext.ByteOrder()
valueContext.SetUndefinedValueType(exifcommon.TypeByte)
valueBytes, err := valueContext.ReadBytes()
log.PanicIf(err)
sfr := TagA20CSpatialFrequencyResponse{}
sfr.Columns = byteOrder.Uint16(valueBytes[0:2])
sfr.Rows = byteOrder.Uint16(valueBytes[2:4])
columnNames := make([]string, sfr.Columns)
// startAt is where the current column name starts.
startAt := 4
// offset is our current position.
offset := 4
currentColumnNumber := uint16(0)
for currentColumnNumber < sfr.Columns {
if valueBytes[offset] == 0 {
columnName := string(valueBytes[startAt:offset])
if len(columnName) == 0 {
log.Panicf("SFR column (%d) has zero length", currentColumnNumber)
}
columnNames[currentColumnNumber] = columnName
currentColumnNumber++
offset++
startAt = offset
continue
}
offset++
}
sfr.ColumnNames = columnNames
rawRationalBytes := valueBytes[offset:]
rationalSize := exifcommon.TypeRational.Size()
if len(rawRationalBytes)%rationalSize > 0 {
log.Panicf("SFR rationals not aligned: (%d) %% (%d) > 0", len(rawRationalBytes), rationalSize)
}
rationalCount := len(rawRationalBytes) / rationalSize
parser := new(exifcommon.Parser)
items, err := parser.ParseRationals(rawRationalBytes, uint32(rationalCount), byteOrder)
log.PanicIf(err)
sfr.Values = items
return sfr, nil
}
func init() {
registerEncoder(
TagA20CSpatialFrequencyResponse{},
CodecA20CSpatialFrequencyResponse{})
registerDecoder(
exifcommon.IfdExifStandardIfdIdentity.UnindexedString(),
0xa20c,
CodecA20CSpatialFrequencyResponse{})
}

View File

@ -1,79 +0,0 @@
package exifundefined
import (
"fmt"
"encoding/binary"
"github.com/dsoprea/go-logging"
"github.com/dsoprea/go-exif/v3/common"
)
type TagExifA300FileSource uint32
func (TagExifA300FileSource) EncoderName() string {
return "CodecExifA300FileSource"
}
func (af TagExifA300FileSource) String() string {
return fmt.Sprintf("0x%08x", uint32(af))
}
const (
TagUndefinedType_A300_SceneType_Others TagExifA300FileSource = 0
TagUndefinedType_A300_SceneType_ScannerOfTransparentType TagExifA300FileSource = 1
TagUndefinedType_A300_SceneType_ScannerOfReflexType TagExifA300FileSource = 2
TagUndefinedType_A300_SceneType_Dsc TagExifA300FileSource = 3
)
type CodecExifA300FileSource struct {
}
func (CodecExifA300FileSource) Encode(value interface{}, byteOrder binary.ByteOrder) (encoded []byte, unitCount uint32, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
st, ok := value.(TagExifA300FileSource)
if ok == false {
log.Panicf("can only encode a TagExifA300FileSource")
}
ve := exifcommon.NewValueEncoder(byteOrder)
ed, err := ve.Encode([]uint32{uint32(st)})
log.PanicIf(err)
// TODO(dustin): Confirm this size against the specification. It's non-specific about what type it is, but it looks to be no more than a single integer scalar. So, we're assuming it's a LONG.
return ed.Encoded, 1, nil
}
func (CodecExifA300FileSource) Decode(valueContext *exifcommon.ValueContext) (value EncodeableValue, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
valueContext.SetUndefinedValueType(exifcommon.TypeLong)
valueLongs, err := valueContext.ReadLongs()
log.PanicIf(err)
return TagExifA300FileSource(valueLongs[0]), nil
}
func init() {
registerEncoder(
TagExifA300FileSource(0),
CodecExifA300FileSource{})
registerDecoder(
exifcommon.IfdExifStandardIfdIdentity.UnindexedString(),
0xa300,
CodecExifA300FileSource{})
}

View File

@ -1,76 +0,0 @@
package exifundefined
import (
"fmt"
"encoding/binary"
"github.com/dsoprea/go-logging"
"github.com/dsoprea/go-exif/v3/common"
)
type TagExifA301SceneType uint32
func (TagExifA301SceneType) EncoderName() string {
return "CodecExifA301SceneType"
}
func (st TagExifA301SceneType) String() string {
return fmt.Sprintf("0x%08x", uint32(st))
}
const (
TagUndefinedType_A301_SceneType_DirectlyPhotographedImage TagExifA301SceneType = 1
)
type CodecExifA301SceneType struct {
}
func (CodecExifA301SceneType) Encode(value interface{}, byteOrder binary.ByteOrder) (encoded []byte, unitCount uint32, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
st, ok := value.(TagExifA301SceneType)
if ok == false {
log.Panicf("can only encode a TagExif9101ComponentsConfiguration")
}
ve := exifcommon.NewValueEncoder(byteOrder)
ed, err := ve.Encode([]uint32{uint32(st)})
log.PanicIf(err)
// TODO(dustin): Confirm this size against the specification. It's non-specific about what type it is, but it looks to be no more than a single integer scalar. So, we're assuming it's a LONG.
return ed.Encoded, uint32(int(ed.UnitCount)), nil
}
func (CodecExifA301SceneType) Decode(valueContext *exifcommon.ValueContext) (value EncodeableValue, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
valueContext.SetUndefinedValueType(exifcommon.TypeLong)
valueLongs, err := valueContext.ReadLongs()
log.PanicIf(err)
return TagExifA301SceneType(valueLongs[0]), nil
}
func init() {
registerEncoder(
TagExifA301SceneType(0),
CodecExifA301SceneType{})
registerDecoder(
exifcommon.IfdExifStandardIfdIdentity.UnindexedString(),
0xa301,
CodecExifA301SceneType{})
}

View File

@ -1,97 +0,0 @@
package exifundefined
import (
"bytes"
"fmt"
"encoding/binary"
"github.com/dsoprea/go-logging"
"github.com/dsoprea/go-exif/v3/common"
)
type TagA302CfaPattern struct {
HorizontalRepeat uint16
VerticalRepeat uint16
CfaValue []byte
}
func (TagA302CfaPattern) EncoderName() string {
return "CodecA302CfaPattern"
}
func (cp TagA302CfaPattern) String() string {
return fmt.Sprintf("TagA302CfaPattern<HORZ-REPEAT=(%d) VERT-REPEAT=(%d) CFA-VALUE=(%d)>", cp.HorizontalRepeat, cp.VerticalRepeat, len(cp.CfaValue))
}
type CodecA302CfaPattern struct {
}
func (CodecA302CfaPattern) Encode(value interface{}, byteOrder binary.ByteOrder) (encoded []byte, unitCount uint32, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
// TODO(dustin): Add test.
cp, ok := value.(TagA302CfaPattern)
if ok == false {
log.Panicf("can only encode a TagA302CfaPattern")
}
b := new(bytes.Buffer)
err = binary.Write(b, byteOrder, cp.HorizontalRepeat)
log.PanicIf(err)
err = binary.Write(b, byteOrder, cp.VerticalRepeat)
log.PanicIf(err)
_, err = b.Write(cp.CfaValue)
log.PanicIf(err)
encoded = b.Bytes()
// TODO(dustin): Confirm this size against the specification.
return encoded, uint32(len(encoded)), nil
}
func (CodecA302CfaPattern) Decode(valueContext *exifcommon.ValueContext) (value EncodeableValue, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
// TODO(dustin): Add test using known good data.
valueContext.SetUndefinedValueType(exifcommon.TypeByte)
valueBytes, err := valueContext.ReadBytes()
log.PanicIf(err)
cp := TagA302CfaPattern{}
cp.HorizontalRepeat = valueContext.ByteOrder().Uint16(valueBytes[0:2])
cp.VerticalRepeat = valueContext.ByteOrder().Uint16(valueBytes[2:4])
expectedLength := int(cp.HorizontalRepeat * cp.VerticalRepeat)
cp.CfaValue = valueBytes[4 : 4+expectedLength]
return cp, nil
}
func init() {
registerEncoder(
TagA302CfaPattern{},
CodecA302CfaPattern{})
registerDecoder(
exifcommon.IfdExifStandardIfdIdentity.UnindexedString(),
0xa302,
CodecA302CfaPattern{})
}

View File

@ -1,69 +0,0 @@
package exifundefined
import (
"encoding/binary"
"github.com/dsoprea/go-logging"
"github.com/dsoprea/go-exif/v3/common"
)
type Tag0002InteropVersion struct {
InteropVersion string
}
func (Tag0002InteropVersion) EncoderName() string {
return "Codec0002InteropVersion"
}
func (iv Tag0002InteropVersion) String() string {
return iv.InteropVersion
}
type Codec0002InteropVersion struct {
}
func (Codec0002InteropVersion) Encode(value interface{}, byteOrder binary.ByteOrder) (encoded []byte, unitCount uint32, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
s, ok := value.(Tag0002InteropVersion)
if ok == false {
log.Panicf("can only encode a Tag0002InteropVersion")
}
return []byte(s.InteropVersion), uint32(len(s.InteropVersion)), nil
}
func (Codec0002InteropVersion) Decode(valueContext *exifcommon.ValueContext) (value EncodeableValue, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
valueContext.SetUndefinedValueType(exifcommon.TypeAsciiNoNul)
valueString, err := valueContext.ReadAsciiNoNul()
log.PanicIf(err)
iv := Tag0002InteropVersion{
InteropVersion: valueString,
}
return iv, nil
}
func init() {
registerEncoder(
Tag0002InteropVersion{},
Codec0002InteropVersion{})
registerDecoder(
exifcommon.IfdExifIopStandardIfdIdentity.UnindexedString(),
0x0002,
Codec0002InteropVersion{})
}

View File

@ -1,65 +0,0 @@
package exifundefined
import (
"encoding/binary"
"github.com/dsoprea/go-logging"
"github.com/dsoprea/go-exif/v3/common"
)
type Tag001BGPSProcessingMethod struct {
string
}
func (Tag001BGPSProcessingMethod) EncoderName() string {
return "Codec001BGPSProcessingMethod"
}
func (gpm Tag001BGPSProcessingMethod) String() string {
return gpm.string
}
type Codec001BGPSProcessingMethod struct {
}
func (Codec001BGPSProcessingMethod) Encode(value interface{}, byteOrder binary.ByteOrder) (encoded []byte, unitCount uint32, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
s, ok := value.(Tag001BGPSProcessingMethod)
if ok == false {
log.Panicf("can only encode a Tag001BGPSProcessingMethod")
}
return []byte(s.string), uint32(len(s.string)), nil
}
func (Codec001BGPSProcessingMethod) Decode(valueContext *exifcommon.ValueContext) (value EncodeableValue, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
valueContext.SetUndefinedValueType(exifcommon.TypeAsciiNoNul)
valueString, err := valueContext.ReadAsciiNoNul()
log.PanicIf(err)
return Tag001BGPSProcessingMethod{valueString}, nil
}
func init() {
registerEncoder(
Tag001BGPSProcessingMethod{},
Codec001BGPSProcessingMethod{})
registerDecoder(
exifcommon.IfdGpsInfoStandardIfdIdentity.UnindexedString(),
0x001b,
Codec001BGPSProcessingMethod{})
}

View File

@ -1,65 +0,0 @@
package exifundefined
import (
"encoding/binary"
"github.com/dsoprea/go-logging"
"github.com/dsoprea/go-exif/v3/common"
)
type Tag001CGPSAreaInformation struct {
string
}
func (Tag001CGPSAreaInformation) EncoderName() string {
return "Codec001CGPSAreaInformation"
}
func (gai Tag001CGPSAreaInformation) String() string {
return gai.string
}
type Codec001CGPSAreaInformation struct {
}
func (Codec001CGPSAreaInformation) Encode(value interface{}, byteOrder binary.ByteOrder) (encoded []byte, unitCount uint32, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
s, ok := value.(Tag001CGPSAreaInformation)
if ok == false {
log.Panicf("can only encode a Tag001CGPSAreaInformation")
}
return []byte(s.string), uint32(len(s.string)), nil
}
func (Codec001CGPSAreaInformation) Decode(valueContext *exifcommon.ValueContext) (value EncodeableValue, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
valueContext.SetUndefinedValueType(exifcommon.TypeAsciiNoNul)
valueString, err := valueContext.ReadAsciiNoNul()
log.PanicIf(err)
return Tag001CGPSAreaInformation{valueString}, nil
}
func init() {
registerEncoder(
Tag001CGPSAreaInformation{},
Codec001CGPSAreaInformation{})
registerDecoder(
exifcommon.IfdGpsInfoStandardIfdIdentity.UnindexedString(),
0x001c,
Codec001CGPSAreaInformation{})
}

View File

@ -1,42 +0,0 @@
package exifundefined
import (
"github.com/dsoprea/go-logging"
)
// UndefinedTagHandle defines one undefined-type tag with a corresponding
// decoder.
type UndefinedTagHandle struct {
IfdPath string
TagId uint16
}
func registerEncoder(entity EncodeableValue, encoder UndefinedValueEncoder) {
typeName := entity.EncoderName()
_, found := encoders[typeName]
if found == true {
log.Panicf("encoder already registered: %v", typeName)
}
encoders[typeName] = encoder
}
func registerDecoder(ifdPath string, tagId uint16, decoder UndefinedValueDecoder) {
uth := UndefinedTagHandle{
IfdPath: ifdPath,
TagId: tagId,
}
_, found := decoders[uth]
if found == true {
log.Panicf("decoder already registered: %v", uth)
}
decoders[uth] = decoder
}
var (
encoders = make(map[string]UndefinedValueEncoder)
decoders = make(map[UndefinedTagHandle]UndefinedValueDecoder)
)

View File

@ -1,44 +0,0 @@
package exifundefined
import (
"errors"
"encoding/binary"
"github.com/dsoprea/go-exif/v3/common"
)
const (
// UnparseableUnknownTagValuePlaceholder is the string to use for an unknown
// undefined tag.
UnparseableUnknownTagValuePlaceholder = "!UNKNOWN"
// UnparseableHandledTagValuePlaceholder is the string to use for a known
// value that is not parseable.
UnparseableHandledTagValuePlaceholder = "!MALFORMED"
)
var (
// ErrUnparseableValue is the error for a value that we should have been
// able to parse but were not able to.
ErrUnparseableValue = errors.New("unparseable undefined tag")
)
// UndefinedValueEncoder knows how to encode an undefined-type tag's value to
// bytes.
type UndefinedValueEncoder interface {
Encode(value interface{}, byteOrder binary.ByteOrder) (encoded []byte, unitCount uint32, err error)
}
// EncodeableValue wraps a value with the information that will be needed to re-
// encode it later.
type EncodeableValue interface {
EncoderName() string
String() string
}
// UndefinedValueDecoder knows how to decode an undefined-type tag's value from
// bytes.
type UndefinedValueDecoder interface {
Decode(valueContext *exifcommon.ValueContext) (value EncodeableValue, err error)
}

View File

@ -1,237 +0,0 @@
package exif
import (
"fmt"
"io"
"math"
"github.com/dsoprea/go-logging"
"github.com/dsoprea/go-utility/v2/filesystem"
"github.com/dsoprea/go-exif/v3/common"
"github.com/dsoprea/go-exif/v3/undefined"
)
var (
utilityLogger = log.NewLogger("exif.utility")
)
// ExifTag is one simple representation of a tag in a flat list of all of them.
type ExifTag struct {
// IfdPath is the fully-qualified IFD path (even though it is not named as
// such).
IfdPath string `json:"ifd_path"`
// TagId is the tag-ID.
TagId uint16 `json:"id"`
// TagName is the tag-name. This is never empty.
TagName string `json:"name"`
// UnitCount is the recorded number of units constution of the value.
UnitCount uint32 `json:"unit_count"`
// TagTypeId is the type-ID.
TagTypeId exifcommon.TagTypePrimitive `json:"type_id"`
// TagTypeName is the type name.
TagTypeName string `json:"type_name"`
// Value is the decoded value.
Value interface{} `json:"value"`
// ValueBytes is the raw, encoded value.
ValueBytes []byte `json:"value_bytes"`
// Formatted is the human representation of the first value (tag values are
// always an array).
FormattedFirst string `json:"formatted_first"`
// Formatted is the human representation of the complete value.
Formatted string `json:"formatted"`
// ChildIfdPath is the name of the child IFD this tag represents (if it
// represents any). Otherwise, this is empty.
ChildIfdPath string `json:"child_ifd_path"`
}
// String returns a string representation.
func (et ExifTag) String() string {
return fmt.Sprintf(
"ExifTag<"+
"IFD-PATH=[%s] "+
"TAG-ID=(0x%02x) "+
"TAG-NAME=[%s] "+
"TAG-TYPE=[%s] "+
"VALUE=[%v] "+
"VALUE-BYTES=(%d) "+
"CHILD-IFD-PATH=[%s]",
et.IfdPath, et.TagId, et.TagName, et.TagTypeName, et.FormattedFirst,
len(et.ValueBytes), et.ChildIfdPath)
}
// GetFlatExifData returns a simple, flat representation of all tags.
func GetFlatExifData(exifData []byte, so *ScanOptions) (exifTags []ExifTag, med *MiscellaneousExifData, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
sb := rifs.NewSeekableBufferWithBytes(exifData)
exifTags, med, err = getFlatExifDataUniversalSearchWithReadSeeker(sb, so, false)
log.PanicIf(err)
return exifTags, med, nil
}
// RELEASE(dustin): GetFlatExifDataUniversalSearch is a kludge to allow univeral tag searching in a backwards-compatible manner. For the next release, undo this and simply add the flag to GetFlatExifData.
// GetFlatExifDataUniversalSearch returns a simple, flat representation of all
// tags.
func GetFlatExifDataUniversalSearch(exifData []byte, so *ScanOptions, doUniversalSearch bool) (exifTags []ExifTag, med *MiscellaneousExifData, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
sb := rifs.NewSeekableBufferWithBytes(exifData)
exifTags, med, err = getFlatExifDataUniversalSearchWithReadSeeker(sb, so, doUniversalSearch)
log.PanicIf(err)
return exifTags, med, nil
}
// RELEASE(dustin): GetFlatExifDataUniversalSearchWithReadSeeker is a kludge to allow using a ReadSeeker in a backwards-compatible manner. For the next release, drop this and refactor GetFlatExifDataUniversalSearch to take a ReadSeeker.
// GetFlatExifDataUniversalSearchWithReadSeeker returns a simple, flat
// representation of all tags given a ReadSeeker.
func GetFlatExifDataUniversalSearchWithReadSeeker(rs io.ReadSeeker, so *ScanOptions, doUniversalSearch bool) (exifTags []ExifTag, med *MiscellaneousExifData, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
exifTags, med, err = getFlatExifDataUniversalSearchWithReadSeeker(rs, so, doUniversalSearch)
log.PanicIf(err)
return exifTags, med, nil
}
// getFlatExifDataUniversalSearchWithReadSeeker returns a simple, flat
// representation of all tags given a ReadSeeker.
func getFlatExifDataUniversalSearchWithReadSeeker(rs io.ReadSeeker, so *ScanOptions, doUniversalSearch bool) (exifTags []ExifTag, med *MiscellaneousExifData, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
headerData := make([]byte, ExifSignatureLength)
if _, err = io.ReadFull(rs, headerData); err != nil {
if err == io.EOF {
return nil, nil, err
}
log.Panic(err)
}
eh, err := ParseExifHeader(headerData)
log.PanicIf(err)
im, err := exifcommon.NewIfdMappingWithStandard()
log.PanicIf(err)
ti := NewTagIndex()
if doUniversalSearch == true {
ti.SetUniversalSearch(true)
}
ebs := NewExifReadSeeker(rs)
ie := NewIfdEnumerate(im, ti, ebs, eh.ByteOrder)
exifTags = make([]ExifTag, 0)
visitor := func(ite *IfdTagEntry) (err error) {
// This encodes down to base64. Since this an example tool and we do not
// expect to ever decode the output, we are not worried about
// specifically base64-encoding it in order to have a measure of
// control.
valueBytes, err := ite.GetRawBytes()
if err != nil {
if err == exifundefined.ErrUnparseableValue {
return nil
}
log.Panic(err)
}
value, err := ite.Value()
if err != nil {
if err == exifcommon.ErrUnhandledUndefinedTypedTag {
value = exifundefined.UnparseableUnknownTagValuePlaceholder
} else if log.Is(err, exifcommon.ErrParseFail) == true {
utilityLogger.Warningf(nil,
"Could not parse value for tag [%s] (%04x) [%s].",
ite.IfdPath(), ite.TagId(), ite.TagName())
return nil
} else {
log.Panic(err)
}
}
et := ExifTag{
IfdPath: ite.IfdPath(),
TagId: ite.TagId(),
TagName: ite.TagName(),
UnitCount: ite.UnitCount(),
TagTypeId: ite.TagType(),
TagTypeName: ite.TagType().String(),
Value: value,
ValueBytes: valueBytes,
ChildIfdPath: ite.ChildIfdPath(),
}
et.Formatted, err = ite.Format()
log.PanicIf(err)
et.FormattedFirst, err = ite.FormatFirst()
log.PanicIf(err)
exifTags = append(exifTags, et)
return nil
}
med, err = ie.Scan(exifcommon.IfdStandardIfdIdentity, eh.FirstIfdOffset, visitor, nil)
log.PanicIf(err)
return exifTags, med, nil
}
// GpsDegreesEquals returns true if the two `GpsDegrees` are identical.
func GpsDegreesEquals(gi1, gi2 GpsDegrees) bool {
if gi2.Orientation != gi1.Orientation {
return false
}
degreesRightBound := math.Nextafter(gi1.Degrees, gi1.Degrees+1)
minutesRightBound := math.Nextafter(gi1.Minutes, gi1.Minutes+1)
secondsRightBound := math.Nextafter(gi1.Seconds, gi1.Seconds+1)
if gi2.Degrees < gi1.Degrees || gi2.Degrees >= degreesRightBound {
return false
} else if gi2.Minutes < gi1.Minutes || gi2.Minutes >= minutesRightBound {
return false
} else if gi2.Seconds < gi1.Seconds || gi2.Seconds >= secondsRightBound {
return false
}
return true
}

View File

View File

@ -1,14 +0,0 @@
language: go
go:
- master
- stable
- "1.13"
- "1.12"
env:
- GO111MODULE=on
install:
- go get -t ./...
- go get github.com/mattn/goveralls
script:
- go test -v ./...
- goveralls -v -service=travis-ci

View File

@ -1,21 +0,0 @@
MIT License
Copyright (c) 2020 Dustin Oprea
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -1,8 +0,0 @@
[![Build Status](https://travis-ci.org/dsoprea/go-iptc.svg?branch=master)](https://travis-ci.org/dsoprea/go-iptc)
[![Coverage Status](https://coveralls.io/repos/github/dsoprea/go-iptc/badge.svg?branch=master)](https://coveralls.io/github/dsoprea/go-iptc?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/dsoprea/go-iptc)](https://goreportcard.com/report/github.com/dsoprea/go-iptc)
[![GoDoc](https://godoc.org/github.com/dsoprea/go-iptc?status.svg)](https://godoc.org/github.com/dsoprea/go-iptc)
# Overview
This project provides functionality to parse a series of IPTC records/datasets. It also provides name resolution, but other constraints/validation is not yet implemented (though there is structure present that can accommodate this when desired/required).

View File

@ -1,101 +0,0 @@
package iptc
import (
"errors"
)
// StreamTagInfo encapsulates the properties of each tag.
type StreamTagInfo struct {
// Description is the human-readable description of the tag.
Description string
}
var (
standardTags = map[StreamTagKey]StreamTagInfo{
{1, 120}: {"ARM Identifier"},
{1, 122}: {"ARM Version"},
{2, 0}: {"Record Version"},
{2, 3}: {"Object Type Reference"},
{2, 4}: {"Object Attribute Reference"},
{2, 5}: {"Object Name"},
{2, 7}: {"Edit Status"},
{2, 8}: {"Editorial Update"},
{2, 10}: {"Urgency"},
{2, 12}: {"Subject Reference"},
{2, 15}: {"Category"},
{2, 20}: {"Supplemental Category"},
{2, 22}: {"Fixture Identifier"},
{2, 25}: {"Keywords"},
{2, 26}: {"Content Location Code"},
{2, 27}: {"Content Location Name"},
{2, 30}: {"Release Date"},
{2, 35}: {"Release Time"},
{2, 37}: {"Expiration Date"},
{2, 38}: {"Expiration Time"},
{2, 40}: {"Special Instructions"},
{2, 42}: {"Action Advised"},
{2, 45}: {"Reference Service"},
{2, 47}: {"Reference Date"},
{2, 50}: {"Reference Number"},
{2, 55}: {"Date Created"},
{2, 60}: {"Time Created"},
{2, 62}: {"Digital Creation Date"},
{2, 63}: {"Digital Creation Time"},
{2, 65}: {"Originating Program"},
{2, 70}: {"Program Version"},
{2, 75}: {"Object Cycle"},
{2, 80}: {"By-line"},
{2, 85}: {"By-line Title"},
{2, 90}: {"City"},
{2, 92}: {"Sublocation"},
{2, 95}: {"Province/State"},
{2, 100}: {"Country/Primary Location Code"},
{2, 101}: {"Country/Primary Location Name"},
{2, 103}: {"Original Transmission Reference"},
{2, 105}: {"Headline"},
{2, 110}: {"Credit"},
{2, 115}: {"Source"},
{2, 116}: {"Copyright Notice"},
{2, 118}: {"Contact"},
{2, 120}: {"Caption/Abstract"},
{2, 122}: {"Writer/Editor"},
{2, 125}: {"Rasterized Caption"},
{2, 130}: {"Image Type"},
{2, 131}: {"Image Orientation"},
{2, 135}: {"Language Identifier"},
{2, 150}: {"Audio Type"},
{2, 151}: {"Audio Sampling Rate"},
{2, 152}: {"Audio Sampling Resolution"},
{2, 153}: {"Audio Duration"},
{2, 154}: {"Audio Outcue"},
{2, 200}: {"ObjectData Preview File Format"},
{2, 201}: {"ObjectData Preview File Format Version"},
{2, 202}: {"ObjectData Preview Data"},
{7, 10}: {"Size Mode"},
{7, 20}: {"Max Subfile Size"},
{7, 90}: {"ObjectData Size Announced"},
{7, 95}: {"Maximum ObjectData Size"},
{8, 10}: {"Subfile"},
{9, 10}: {"Confirmed ObjectData Size"},
}
)
var (
// ErrTagNotStandard indicates that the given tag is not known among the
// documented standard set.
ErrTagNotStandard = errors.New("not a standard tag")
)
// GetTagInfo return the info for the given tag. Returns ErrTagNotStandard if
// not known.
func GetTagInfo(recordNumber, datasetNumber int) (sti StreamTagInfo, err error) {
stk := StreamTagKey{uint8(recordNumber), uint8(datasetNumber)}
sti, found := standardTags[stk]
if found == false {
return sti, ErrTagNotStandard
}
return sti, nil
}

View File

@ -1,277 +0,0 @@
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
}

View File

@ -1,73 +0,0 @@
package iptc
import (
"os"
"path"
"github.com/dsoprea/go-logging"
)
var (
testDataRelFilepath = "iptc.data"
)
var (
moduleRootPath = ""
assetsPath = ""
)
// GetModuleRootPath returns the root-path of the module.
func GetModuleRootPath() string {
if moduleRootPath == "" {
moduleRootPath = os.Getenv("IPTC_MODULE_ROOT_PATH")
if moduleRootPath != "" {
return moduleRootPath
}
currentWd, err := os.Getwd()
log.PanicIf(err)
currentPath := currentWd
visited := make([]string, 0)
for {
tryStampFilepath := path.Join(currentPath, ".MODULE_ROOT")
_, err := os.Stat(tryStampFilepath)
if err != nil && os.IsNotExist(err) != true {
log.Panic(err)
} else if err == nil {
break
}
visited = append(visited, tryStampFilepath)
currentPath = path.Dir(currentPath)
if currentPath == "/" {
log.Panicf("could not find module-root: %v", visited)
}
}
moduleRootPath = currentPath
}
return moduleRootPath
}
// GetTestAssetsPath returns the path of the test-assets.
func GetTestAssetsPath() string {
if assetsPath == "" {
moduleRootPath := GetModuleRootPath()
assetsPath = path.Join(moduleRootPath, "assets")
}
return assetsPath
}
// GetTestDataFilepath returns the file-path of the common test-data.
func GetTestDataFilepath() string {
assetsPath := GetTestAssetsPath()
filepath := path.Join(assetsPath, testDataRelFilepath)
return filepath
}

View File

@ -1,25 +0,0 @@
package iptc
import (
"bytes"
"fmt"
"github.com/dsoprea/go-logging"
)
// DumpBytesToString returns a stringified list of hex-encoded bytes.
func DumpBytesToString(data []byte) string {
b := new(bytes.Buffer)
for i, x := range data {
_, err := b.WriteString(fmt.Sprintf("%02x", x))
log.PanicIf(err)
if i < len(data)-1 {
_, err := b.WriteRune(' ')
log.PanicIf(err)
}
}
return b.String()
}

View File

@ -1,12 +0,0 @@
language: go
go:
- tip
install:
- go get -t ./...
- go get github.com/mattn/goveralls
script:
# v1
- go test -v .
# v2
- cd v2
- goveralls -v -service=travis-ci

View File

@ -1,9 +0,0 @@
MIT LICENSE
Copyright 2020 Dustin Oprea
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -1,223 +0,0 @@
[![Build Status](https://travis-ci.org/dsoprea/go-logging.svg?branch=master)](https://travis-ci.org/dsoprea/go-logging)
[![Coverage Status](https://coveralls.io/repos/github/dsoprea/go-logging/badge.svg?branch=master)](https://coveralls.io/github/dsoprea/go-logging?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/dsoprea/go-logging/v2)](https://goreportcard.com/report/github.com/dsoprea/go-logging/v2)
[![GoDoc](https://godoc.org/github.com/dsoprea/go-logging/v2?status.svg)](https://godoc.org/github.com/dsoprea/go-logging/v2)
## Introduction
This project bridges several gaps that are present in the standard logging support in Go:
- Equips errors with stacktraces and provides a facility for printing them
- Inherently supports the ability for each Go file to print its messages with a prefix representing that file/package
- Adds some functions to specifically log messages of different levels (e.g. debug, error)
- Adds a `PanicIf()` function that can be used to conditionally manage errors depending on whether an error variable is `nil` or actually has an error
- Adds support for pluggable logging adapters (so the output can be sent somewhere other than the console)
- Adds configuration (such as the logging level or adapter) that can be driven from the environment
- Supports filtering to show/hide the logging of certain places of the application
- The loggers can be definded at the package level, so you can determine which Go file any log message came from.
When used with the Panic-Defer-Recover pattern in Go, even panics rising from the Go runtime will be caught and wrapped with a stacktrace. This compartmentalizes which function they could have originated from, which is, otherwise, potentially non-trivial to figure out.
## AppEngine
Go under AppEngine is very stripped down, such as there being no logging type (e.g. `Logger` in native Go) and there is no support for prefixing. As each logging call from this project takes a `Context`, this works cooperatively to bridge the additional gaps in AppEngine's logging support.
With standard console logging outside of this context, that parameter will take a`nil`.
## Getting Started
The simplest, possible example:
```go
package thispackage
import (
"context"
"errors"
"github.com/dsoprea/go-logging/v2"
)
var (
thisfileLog = log.NewLogger("thispackage.thisfile")
)
func a_cry_for_help(ctx context.Context) {
err := errors.New("a big error")
thisfileLog.Errorf(ctx, err, "How big is my problem: %s", "pretty big")
}
func init() {
cla := log.NewConsoleLogAdapter()
log.AddAdapter("console", cla)
}
```
Notice two things:
1. We register the "console" adapter at the bottom. The first adapter registered will be used by default.
2. We pass-in a prefix (what we refer to as a "noun") to `log.NewLogger()`. This is a simple, descriptive name that represents the subject of the file. By convention, we construct this by dot-separating the current package and the name of the file. We recommend that you define a different log for every file at the package level, but it is your choice whether you want to do this or share the same logger over the entire package, define one in each struct, etc..
### Example Output
Example output from a real application (not from the above):
```
2016/09/09 12:57:44 DEBUG: user: User revisiting: [test@example.com]
2016/09/09 12:57:44 DEBUG: context: Session already inited: [DCRBDGRY6RMWANCSJXVLD7GULDH4NZEB6SBAQ3KSFIGA2LP45IIQ]
2016/09/09 12:57:44 DEBUG: session_data: Session save not necessary: [DCRBDGRY6RMWANCSJXVLD7GULDH4NZEB6SBAQ3KSFIGA2LP45IIQ]
2016/09/09 12:57:44 DEBUG: context: Got session: [DCRBDGRY6RMWANCSJXVLD7GULDH4NZEB6SBAQ3KSFIGA2LP45IIQ]
2016/09/09 12:57:44 DEBUG: session_data: Found user in session.
2016/09/09 12:57:44 DEBUG: cache: Cache miss: [geo.geocode.reverse:dhxp15x]
```
## Adapters
This project provides one built-in logging adapter, "console", which prints to the screen. To register it:
```go
cla := log.NewConsoleLogAdapter()
log.AddAdapter("console", cla)
```
### Custom Adapters
If you would like to implement your own logger, just create a struct type that satisfies the LogAdapter interface.
```go
type LogAdapter interface {
Debugf(lc *LogContext, message *string) error
Infof(lc *LogContext, message *string) error
Warningf(lc *LogContext, message *string) error
Errorf(lc *LogContext, message *string) error
}
```
The *LogContext* struct passed in provides additional information that you may need in order to do what you need to do:
```go
type LogContext struct {
Logger *Logger
Ctx context.Context
}
```
`Logger` represents your Logger instance.
Adapter example:
```go
type DummyLogAdapter struct {
}
func (dla *DummyLogAdapter) Debugf(lc *LogContext, message *string) error {
}
func (dla *DummyLogAdapter) Infof(lc *LogContext, message *string) error {
}
func (dla *DummyLogAdapter) Warningf(lc *LogContext, message *string) error {
}
func (dla *DummyLogAdapter) Errorf(lc *LogContext, message *string) error {
}
```
Then, register it:
```go
func init() {
log.AddAdapter("dummy", new(DummyLogAdapter))
}
```
If this is a task-specific implementation, just register it from the `init()` of the file that defines it.
If this is the first adapter you've registered, it will be the default one used. Otherwise, you'll have to deliberately specify it when you are creating a logger: Instead of calling `log.NewLogger(noun string)`, call `log.NewLoggerWithAdapterName(noun string, adapterName string)`.
We discuss how to configure the adapter from configuration in the "Configuration" section below.
### Adapter Notes
- The `Logger` instance exports `Noun()` in the event you want to discriminate where your log entries go in your adapter. It also exports `Adapter()` for if you need to access the adapter instance from your application.
- If no adapter is registered (specifically, the default adapter-name remains empty), logging calls will be a no-op. This allows libraries to implement *go-logging* where the larger application doesn't.
## Filters
We support the ability to exclusively log for a specific set of nouns (we'll exclude any not specified):
```go
log.AddIncludeFilter("nountoshow1")
log.AddIncludeFilter("nountoshow2")
```
Depending on your needs, you might just want to exclude a couple and include the rest:
```go
log.AddExcludeFilter("nountohide1")
log.AddExcludeFilter("nountohide2")
```
We'll first hit the include-filters. If it's in there, we'll forward the log item to the adapter. If not, and there is at least one include filter in the list, we won't do anything. If the list of include filters is empty but the noun appears in the exclude list, we won't do anything.
It is a good convention to exclude the nouns of any library you are writing whose logging you do not want to generally be aware of unless you are debugging. You might call `AddExcludeFilter()` from the `init()` function at the bottom of those files unless there is some configuration variable, such as "(LibraryNameHere)DoShowLogging", that has been defined and set to TRUE.
## Configuration
The following configuration items are available:
- *Format*: The default format used to build the message that gets sent to the adapter. It is assumed that the adapter already prefixes the message with time and log-level (since the default AppEngine logger does). The default value is: `{{.Noun}}: [{{.Level}}] {{if eq .ExcludeBypass true}} [BYPASS]{{end}} {{.Message}}`. The available tokens are "Level", "Noun", "ExcludeBypass", and "Message".
- *DefaultAdapterName*: The default name of the adapter to use when NewLogger() is called (if this isn't defined then the name of the first registered adapter will be used).
- *LevelName*: The priority-level of messages permitted to be logged (all others will be discarded). By default, it is "info". Other levels are: "debug", "warning", "error", "critical"
- *IncludeNouns*: Comma-separated list of nouns to log for. All others will be ignored.
- *ExcludeNouns*: Comma-separated list on nouns to exclude from logging.
- *ExcludeBypassLevelName*: The log-level at which we will show logging for nouns that have been excluded. Allows you to hide excessive, unimportant logging for nouns but to still see their warnings, errors, etc...
### Configuration Providers
You provide the configuration by setting a configuration-provider. Configuration providers must satisfy the `ConfigurationProvider` interface. The following are provided with the project:
- `EnvironmentConfigurationProvider`: Read values from the environment.
- `StaticConfigurationProvider`: Set values directly on the struct.
**The configuration provider must be applied before doing any logging (otherwise it will have no effect).**
Environments such as AppEngine work best with `EnvironmentConfigurationProvider` as this is generally how configuration is exposed *by* AppEngine *to* the application. You can define this configuration directly in *that* configuration.
By default, no configuration-provider is applied, the level is defaulted to INFO and the format is defaulted to "{{.Noun}}:{{if eq .ExcludeBypass true}} [BYPASS]{{end}} {{.Message}}".
Again, if a configuration-provider does not provide a log-level or format, they will be defaulted (or left alone, if already set). If it does not provide an adapter-name, the adapter-name of the first registered adapter will be used.
Usage instructions of both follow.
### Environment-Based Configuration
```go
ecp := log.NewEnvironmentConfigurationProvider()
log.LoadConfiguration(ecp)
```
Each of the items listed at the top of the "Configuration" section can be specified in the environment using a prefix of "Log" (e.g. LogDefaultAdapterName).
### Static Configuration
```go
scp := log.NewStaticConfigurationProvider()
scp.SetLevelName(log.LevelNameWarning)
log.LoadConfiguration(scp)
```

View File

@ -1,246 +0,0 @@
package log
import (
"fmt"
"os"
)
// Config keys.
const (
ckFormat = "LogFormat"
ckDefaultAdapterName = "LogDefaultAdapterName"
ckLevelName = "LogLevelName"
ckIncludeNouns = "LogIncludeNouns"
ckExcludeNouns = "LogExcludeNouns"
ckExcludeBypassLevelName = "LogExcludeBypassLevelName"
)
// Other constants
const (
defaultFormat = "{{.Noun}}: [{{.Level}}] {{if eq .ExcludeBypass true}} [BYPASS]{{end}} {{.Message}}"
defaultLevelName = LevelNameInfo
)
// Config
var (
// Alternative format.
format = defaultFormat
// Alternative adapter.
defaultAdapterName = ""
// Alternative level at which to display log-items
levelName = defaultLevelName
// Configuration-driven comma-separated list of nouns to include.
includeNouns = ""
// Configuration-driven comma-separated list of nouns to exclude.
excludeNouns = ""
// Level at which to disregard exclusion (if the severity of a message
// meets or exceed this, always display).
excludeBypassLevelName = ""
)
// Other
var (
configurationLoaded = false
)
// Return the current default adapter name.
func GetDefaultAdapterName() string {
return defaultAdapterName
}
// The adapter will automatically be the first one registered. This overrides
// that.
func SetDefaultAdapterName(name string) {
defaultAdapterName = name
}
func LoadConfiguration(cp ConfigurationProvider) {
configuredDefaultAdapterName := cp.DefaultAdapterName()
if configuredDefaultAdapterName != "" {
defaultAdapterName = configuredDefaultAdapterName
}
includeNouns = cp.IncludeNouns()
excludeNouns = cp.ExcludeNouns()
excludeBypassLevelName = cp.ExcludeBypassLevelName()
f := cp.Format()
if f != "" {
format = f
}
ln := cp.LevelName()
if ln != "" {
levelName = ln
}
configurationLoaded = true
}
func getConfigState() map[string]interface{} {
return map[string]interface{}{
"format": format,
"defaultAdapterName": defaultAdapterName,
"levelName": levelName,
"includeNouns": includeNouns,
"excludeNouns": excludeNouns,
"excludeBypassLevelName": excludeBypassLevelName,
}
}
func setConfigState(config map[string]interface{}) {
format = config["format"].(string)
defaultAdapterName = config["defaultAdapterName"].(string)
levelName = config["levelName"].(string)
includeNouns = config["includeNouns"].(string)
excludeNouns = config["excludeNouns"].(string)
excludeBypassLevelName = config["excludeBypassLevelName"].(string)
}
func getConfigDump() string {
return fmt.Sprintf(
"Current configuration:\n"+
" FORMAT=[%s]\n"+
" DEFAULT-ADAPTER-NAME=[%s]\n"+
" LEVEL-NAME=[%s]\n"+
" INCLUDE-NOUNS=[%s]\n"+
" EXCLUDE-NOUNS=[%s]\n"+
" EXCLUDE-BYPASS-LEVEL-NAME=[%s]",
format, defaultAdapterName, levelName, includeNouns, excludeNouns, excludeBypassLevelName)
}
func IsConfigurationLoaded() bool {
return configurationLoaded
}
type ConfigurationProvider interface {
// Alternative format (defaults to .
Format() string
// Alternative adapter (defaults to "appengine").
DefaultAdapterName() string
// Alternative level at which to display log-items (defaults to
// "info").
LevelName() string
// Configuration-driven comma-separated list of nouns to include. Defaults
// to empty.
IncludeNouns() string
// Configuration-driven comma-separated list of nouns to exclude. Defaults
// to empty.
ExcludeNouns() string
// Level at which to disregard exclusion (if the severity of a message
// meets or exceed this, always display). Defaults to empty.
ExcludeBypassLevelName() string
}
// Environment configuration-provider.
type EnvironmentConfigurationProvider struct {
}
func NewEnvironmentConfigurationProvider() *EnvironmentConfigurationProvider {
return new(EnvironmentConfigurationProvider)
}
func (ecp *EnvironmentConfigurationProvider) Format() string {
return os.Getenv(ckFormat)
}
func (ecp *EnvironmentConfigurationProvider) DefaultAdapterName() string {
return os.Getenv(ckDefaultAdapterName)
}
func (ecp *EnvironmentConfigurationProvider) LevelName() string {
return os.Getenv(ckLevelName)
}
func (ecp *EnvironmentConfigurationProvider) IncludeNouns() string {
return os.Getenv(ckIncludeNouns)
}
func (ecp *EnvironmentConfigurationProvider) ExcludeNouns() string {
return os.Getenv(ckExcludeNouns)
}
func (ecp *EnvironmentConfigurationProvider) ExcludeBypassLevelName() string {
return os.Getenv(ckExcludeBypassLevelName)
}
// Static configuration-provider.
type StaticConfigurationProvider struct {
format string
defaultAdapterName string
levelName string
includeNouns string
excludeNouns string
excludeBypassLevelName string
}
func NewStaticConfigurationProvider() *StaticConfigurationProvider {
return new(StaticConfigurationProvider)
}
func (scp *StaticConfigurationProvider) SetFormat(format string) {
scp.format = format
}
func (scp *StaticConfigurationProvider) SetDefaultAdapterName(adapterName string) {
scp.defaultAdapterName = adapterName
}
func (scp *StaticConfigurationProvider) SetLevelName(levelName string) {
scp.levelName = levelName
}
func (scp *StaticConfigurationProvider) SetIncludeNouns(includeNouns string) {
scp.includeNouns = includeNouns
}
func (scp *StaticConfigurationProvider) SetExcludeNouns(excludeNouns string) {
scp.excludeNouns = excludeNouns
}
func (scp *StaticConfigurationProvider) SetExcludeBypassLevelName(excludeBypassLevelName string) {
scp.excludeBypassLevelName = excludeBypassLevelName
}
func (scp *StaticConfigurationProvider) Format() string {
return scp.format
}
func (scp *StaticConfigurationProvider) DefaultAdapterName() string {
return scp.defaultAdapterName
}
func (scp *StaticConfigurationProvider) LevelName() string {
return scp.levelName
}
func (scp *StaticConfigurationProvider) IncludeNouns() string {
return scp.includeNouns
}
func (scp *StaticConfigurationProvider) ExcludeNouns() string {
return scp.excludeNouns
}
func (scp *StaticConfigurationProvider) ExcludeBypassLevelName() string {
return scp.excludeBypassLevelName
}
func init() {
// Do the initial configuration-load from the environment. We gotta seed it
// with something for simplicity's sake.
ecp := NewEnvironmentConfigurationProvider()
LoadConfiguration(ecp)
}

View File

@ -1,36 +0,0 @@
package log
import (
golog "log"
)
type ConsoleLogAdapter struct {
}
func NewConsoleLogAdapter() LogAdapter {
return new(ConsoleLogAdapter)
}
func (cla *ConsoleLogAdapter) Debugf(lc *LogContext, message *string) error {
golog.Println(*message)
return nil
}
func (cla *ConsoleLogAdapter) Infof(lc *LogContext, message *string) error {
golog.Println(*message)
return nil
}
func (cla *ConsoleLogAdapter) Warningf(lc *LogContext, message *string) error {
golog.Println(*message)
return nil
}
func (cla *ConsoleLogAdapter) Errorf(lc *LogContext, message *string) error {
golog.Println(*message)
return nil
}

View File

@ -1,537 +0,0 @@
package log
import (
"bytes"
e "errors"
"fmt"
"strings"
"sync"
"text/template"
"github.com/go-errors/errors"
"golang.org/x/net/context"
)
// TODO(dustin): Finish symbol documentation
// Config severity integers.
const (
LevelDebug = iota
LevelInfo = iota
LevelWarning = iota
LevelError = iota
)
// Config severity names.
const (
LevelNameDebug = "debug"
LevelNameInfo = "info"
LevelNameWarning = "warning"
LevelNameError = "error"
)
// Seveirty name->integer map.
var (
LevelNameMap = map[string]int{
LevelNameDebug: LevelDebug,
LevelNameInfo: LevelInfo,
LevelNameWarning: LevelWarning,
LevelNameError: LevelError,
}
LevelNameMapR = map[int]string{
LevelDebug: LevelNameDebug,
LevelInfo: LevelNameInfo,
LevelWarning: LevelNameWarning,
LevelError: LevelNameError,
}
)
// Errors
var (
ErrAdapterAlreadyRegistered = e.New("adapter already registered")
ErrFormatEmpty = e.New("format is empty")
ErrExcludeLevelNameInvalid = e.New("exclude bypass-level is invalid")
ErrNoAdapterConfigured = e.New("no default adapter configured")
ErrAdapterIsNil = e.New("adapter is nil")
ErrConfigurationNotLoaded = e.New("can not configure because configuration is not loaded")
)
// Other
var (
includeFilters = make(map[string]bool)
useIncludeFilters = false
excludeFilters = make(map[string]bool)
useExcludeFilters = false
adapters = make(map[string]LogAdapter)
// TODO(dustin): !! Finish implementing this.
excludeBypassLevel = -1
)
// Add global include filter.
func AddIncludeFilter(noun string) {
includeFilters[noun] = true
useIncludeFilters = true
}
// Remove global include filter.
func RemoveIncludeFilter(noun string) {
delete(includeFilters, noun)
if len(includeFilters) == 0 {
useIncludeFilters = false
}
}
// Add global exclude filter.
func AddExcludeFilter(noun string) {
excludeFilters[noun] = true
useExcludeFilters = true
}
// Remove global exclude filter.
func RemoveExcludeFilter(noun string) {
delete(excludeFilters, noun)
if len(excludeFilters) == 0 {
useExcludeFilters = false
}
}
func AddAdapter(name string, la LogAdapter) {
if _, found := adapters[name]; found == true {
Panic(ErrAdapterAlreadyRegistered)
}
if la == nil {
Panic(ErrAdapterIsNil)
}
adapters[name] = la
if GetDefaultAdapterName() == "" {
SetDefaultAdapterName(name)
}
}
func ClearAdapters() {
adapters = make(map[string]LogAdapter)
SetDefaultAdapterName("")
}
type LogAdapter interface {
Debugf(lc *LogContext, message *string) error
Infof(lc *LogContext, message *string) error
Warningf(lc *LogContext, message *string) error
Errorf(lc *LogContext, message *string) error
}
// TODO(dustin): !! Also populate whether we've bypassed an exception so that
// we can add a template macro to prefix an exclamation of
// some sort.
type MessageContext struct {
Level *string
Noun *string
Message *string
ExcludeBypass bool
}
type LogContext struct {
Logger *Logger
Ctx context.Context
}
type Logger struct {
isConfigured bool
an string
la LogAdapter
t *template.Template
systemLevel int
noun string
}
func NewLoggerWithAdapterName(noun string, adapterName string) (l *Logger) {
l = &Logger{
noun: noun,
an: adapterName,
}
return l
}
func NewLogger(noun string) (l *Logger) {
l = NewLoggerWithAdapterName(noun, "")
return l
}
func (l *Logger) Noun() string {
return l.noun
}
func (l *Logger) Adapter() LogAdapter {
return l.la
}
var (
configureMutex sync.Mutex
)
func (l *Logger) doConfigure(force bool) {
configureMutex.Lock()
defer configureMutex.Unlock()
if l.isConfigured == true && force == false {
return
}
if IsConfigurationLoaded() == false {
Panic(ErrConfigurationNotLoaded)
}
if l.an == "" {
l.an = GetDefaultAdapterName()
}
// If this is empty, then no specific adapter was given or no system
// default was configured (which implies that no adapters were registered).
// All of our logging will be skipped.
if l.an != "" {
la, found := adapters[l.an]
if found == false {
Panic(fmt.Errorf("adapter is not valid: %s", l.an))
}
l.la = la
}
// Set the level.
systemLevel, found := LevelNameMap[levelName]
if found == false {
Panic(fmt.Errorf("log-level not valid: [%s]", levelName))
}
l.systemLevel = systemLevel
// Set the form.
if format == "" {
Panic(ErrFormatEmpty)
}
if t, err := template.New("logItem").Parse(format); err != nil {
Panic(err)
} else {
l.t = t
}
l.isConfigured = true
}
func (l *Logger) flattenMessage(lc *MessageContext, format *string, args []interface{}) (string, error) {
m := fmt.Sprintf(*format, args...)
lc.Message = &m
var b bytes.Buffer
if err := l.t.Execute(&b, *lc); err != nil {
return "", err
}
return b.String(), nil
}
func (l *Logger) allowMessage(noun string, level int) bool {
if _, found := includeFilters[noun]; found == true {
return true
}
// If we didn't hit an include filter and we *had* include filters, filter
// it out.
if useIncludeFilters == true {
return false
}
if _, found := excludeFilters[noun]; found == true {
return false
}
return true
}
func (l *Logger) makeLogContext(ctx context.Context) *LogContext {
return &LogContext{
Ctx: ctx,
Logger: l,
}
}
type LogMethod func(lc *LogContext, message *string) error
func (l *Logger) log(ctx context.Context, level int, lm LogMethod, format string, args []interface{}) error {
if l.systemLevel > level {
return nil
}
// Preempt the normal filter checks if we can unconditionally allow at a
// certain level and we've hit that level.
//
// Notice that this is only relevant if the system-log level is letting
// *anything* show logs at the level we came in with.
canExcludeBypass := level >= excludeBypassLevel && excludeBypassLevel != -1
didExcludeBypass := false
n := l.Noun()
if l.allowMessage(n, level) == false {
if canExcludeBypass == false {
return nil
} else {
didExcludeBypass = true
}
}
levelName, found := LevelNameMapR[level]
if found == false {
Panic(fmt.Errorf("level not valid: (%d)", level))
}
levelName = strings.ToUpper(levelName)
lc := &MessageContext{
Level: &levelName,
Noun: &n,
ExcludeBypass: didExcludeBypass,
}
if s, err := l.flattenMessage(lc, &format, args); err != nil {
return err
} else {
lc := l.makeLogContext(ctx)
if err := lm(lc, &s); err != nil {
panic(err)
}
return e.New(s)
}
}
func (l *Logger) Debugf(ctx context.Context, format string, args ...interface{}) {
l.doConfigure(false)
if l.la != nil {
l.log(ctx, LevelDebug, l.la.Debugf, format, args)
}
}
func (l *Logger) Infof(ctx context.Context, format string, args ...interface{}) {
l.doConfigure(false)
if l.la != nil {
l.log(ctx, LevelInfo, l.la.Infof, format, args)
}
}
func (l *Logger) Warningf(ctx context.Context, format string, args ...interface{}) {
l.doConfigure(false)
if l.la != nil {
l.log(ctx, LevelWarning, l.la.Warningf, format, args)
}
}
func (l *Logger) mergeStack(err interface{}, format string, args []interface{}) (string, []interface{}) {
if format != "" {
format += "\n%s"
} else {
format = "%s"
}
var stackified *errors.Error
stackified, ok := err.(*errors.Error)
if ok == false {
stackified = errors.Wrap(err, 2)
}
args = append(args, stackified.ErrorStack())
return format, args
}
func (l *Logger) Errorf(ctx context.Context, errRaw interface{}, format string, args ...interface{}) {
l.doConfigure(false)
var err interface{}
if errRaw != nil {
_, ok := errRaw.(*errors.Error)
if ok == true {
err = errRaw
} else {
err = errors.Wrap(errRaw, 1)
}
}
if l.la != nil {
if errRaw != nil {
format, args = l.mergeStack(err, format, args)
}
l.log(ctx, LevelError, l.la.Errorf, format, args)
}
}
func (l *Logger) ErrorIff(ctx context.Context, errRaw interface{}, format string, args ...interface{}) {
if errRaw == nil {
return
}
var err interface{}
_, ok := errRaw.(*errors.Error)
if ok == true {
err = errRaw
} else {
err = errors.Wrap(errRaw, 1)
}
l.Errorf(ctx, err, format, args...)
}
func (l *Logger) Panicf(ctx context.Context, errRaw interface{}, format string, args ...interface{}) {
l.doConfigure(false)
var err interface{}
_, ok := errRaw.(*errors.Error)
if ok == true {
err = errRaw
} else {
err = errors.Wrap(errRaw, 1)
}
if l.la != nil {
format, args = l.mergeStack(err, format, args)
err = l.log(ctx, LevelError, l.la.Errorf, format, args)
}
Panic(err.(error))
}
func (l *Logger) PanicIff(ctx context.Context, errRaw interface{}, format string, args ...interface{}) {
if errRaw == nil {
return
}
var err interface{}
_, ok := errRaw.(*errors.Error)
if ok == true {
err = errRaw
} else {
err = errors.Wrap(errRaw, 1)
}
l.Panicf(ctx, err.(error), format, args...)
}
func Wrap(err interface{}) *errors.Error {
es, ok := err.(*errors.Error)
if ok == true {
return es
} else {
return errors.Wrap(err, 1)
}
}
func Errorf(message string, args ...interface{}) *errors.Error {
err := fmt.Errorf(message, args...)
return errors.Wrap(err, 1)
}
func Panic(err interface{}) {
_, ok := err.(*errors.Error)
if ok == true {
panic(err)
} else {
panic(errors.Wrap(err, 1))
}
}
func Panicf(message string, args ...interface{}) {
err := Errorf(message, args...)
Panic(err)
}
func PanicIf(err interface{}) {
if err == nil {
return
}
_, ok := err.(*errors.Error)
if ok == true {
panic(err)
} else {
panic(errors.Wrap(err, 1))
}
}
// Is checks if the left ("actual") error equals the right ("against") error.
// The right must be an unwrapped error (the kind that you'd initialize as a
// global variable). The left can be a wrapped or unwrapped error.
func Is(actual, against error) bool {
// If it's an unwrapped error.
if _, ok := actual.(*errors.Error); ok == false {
return actual == against
}
return errors.Is(actual, against)
}
// Print is a utility function to prevent the caller from having to import the
// third-party library.
func PrintError(err error) {
wrapped := Wrap(err)
fmt.Printf("Stack:\n\n%s\n", wrapped.ErrorStack())
}
// PrintErrorf is a utility function to prevent the caller from having to
// import the third-party library.
func PrintErrorf(err error, format string, args ...interface{}) {
wrapped := Wrap(err)
fmt.Printf(format, args...)
fmt.Printf("\n")
fmt.Printf("Stack:\n\n%s\n", wrapped.ErrorStack())
}
func init() {
if format == "" {
format = defaultFormat
}
if levelName == "" {
levelName = defaultLevelName
}
if includeNouns != "" {
for _, noun := range strings.Split(includeNouns, ",") {
AddIncludeFilter(noun)
}
}
if excludeNouns != "" {
for _, noun := range strings.Split(excludeNouns, ",") {
AddExcludeFilter(noun)
}
}
if excludeBypassLevelName != "" {
var found bool
if excludeBypassLevel, found = LevelNameMap[excludeBypassLevelName]; found == false {
panic(ErrExcludeLevelNameInvalid)
}
}
}

View File

@ -1,14 +0,0 @@
language: go
go:
- master
- stable
- "1.13"
- "1.12"
env:
- GO111MODULE=on
install:
- go get -t ./...
- go get github.com/mattn/goveralls
script:
- go test -v ./...
- goveralls -v -service=travis-ci

View File

@ -1,21 +0,0 @@
MIT License
Copyright (c) 2020 Dustin Oprea
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -1,8 +0,0 @@
[![Build Status](https://travis-ci.org/dsoprea/go-photoshop-info-format.svg?branch=master)](https://travis-ci.org/dsoprea/go-photoshop-info-format)
[![Coverage Status](https://coveralls.io/repos/github/dsoprea/go-photoshop-info-format/badge.svg?branch=master)](https://coveralls.io/github/dsoprea/go-photoshop-info-format?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/dsoprea/go-photoshop-info-format)](https://goreportcard.com/report/github.com/dsoprea/go-photoshop-info-format)
[![GoDoc](https://godoc.org/github.com/dsoprea/go-photoshop-info-format?status.svg)](https://godoc.org/github.com/dsoprea/go-photoshop-info-format)
# Overview
This is a minimal Photoshop format implementation to allow IPTC data to be extracted from a JPEG image. This project primarily services [go-jpeg-image-structure](https://github.com/dsoprea/go-jpeg-image-structure).

View File

@ -1,119 +0,0 @@
package photoshopinfo
import (
"fmt"
"io"
"encoding/binary"
"github.com/dsoprea/go-logging"
)
var (
defaultByteOrder = binary.BigEndian
)
// Photoshop30InfoRecord is the data for one parsed Photoshop-info record.
type Photoshop30InfoRecord struct {
// RecordType is the record-type.
RecordType string
// ImageResourceId is the image resource-ID.
ImageResourceId uint16
// Name is the name of the record. It is optional and will be an empty-
// string if not present.
Name string
// Data is the raw record data.
Data []byte
}
// String returns a descriptive string.
func (pir Photoshop30InfoRecord) String() string {
return fmt.Sprintf("RECORD-TYPE=[%s] IMAGE-RESOURCE-ID=[0x%04x] NAME=[%s] DATA-SIZE=(%d)", pir.RecordType, pir.ImageResourceId, pir.Name, len(pir.Data))
}
// ReadPhotoshop30InfoRecord parses a single photoshop-info record.
func ReadPhotoshop30InfoRecord(r io.Reader) (pir Photoshop30InfoRecord, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
recordType := make([]byte, 4)
_, err = io.ReadFull(r, recordType)
if err != nil {
if err == io.EOF {
return pir, err
}
log.Panic(err)
}
// TODO(dustin): Move BigEndian to constant/config.
irId := uint16(0)
err = binary.Read(r, defaultByteOrder, &irId)
log.PanicIf(err)
nameSize := uint8(0)
err = binary.Read(r, defaultByteOrder, &nameSize)
log.PanicIf(err)
// Add an extra byte if the two length+data size is odd to make the total
// bytes read even.
doAddPadding := (1+nameSize)%2 == 1
if doAddPadding == true {
nameSize++
}
name := make([]byte, nameSize)
_, err = io.ReadFull(r, name)
log.PanicIf(err)
// If the last byte is padding, truncate it.
if doAddPadding == true {
name = name[:nameSize-1]
}
dataSize := uint32(0)
err = binary.Read(r, defaultByteOrder, &dataSize)
log.PanicIf(err)
data := make([]byte, dataSize+dataSize%2)
_, err = io.ReadFull(r, data)
log.PanicIf(err)
data = data[:dataSize]
pir = Photoshop30InfoRecord{
RecordType: string(recordType),
ImageResourceId: irId,
Name: string(name),
Data: data,
}
return pir, nil
}
// ReadPhotoshop30Info parses a sequence of photoship-info records from the stream.
func ReadPhotoshop30Info(r io.Reader) (pirIndex map[uint16]Photoshop30InfoRecord, err error) {
pirIndex = make(map[uint16]Photoshop30InfoRecord)
for {
pir, err := ReadPhotoshop30InfoRecord(r)
if err != nil {
if err == io.EOF {
break
}
log.Panic(err)
}
pirIndex[pir.ImageResourceId] = pir
}
return pirIndex, nil
}

View File

@ -1,73 +0,0 @@
package photoshopinfo
import (
"os"
"path"
"github.com/dsoprea/go-logging"
)
var (
testDataRelFilepath = "photoshop.data"
)
var (
moduleRootPath = ""
assetsPath = ""
)
// GetModuleRootPath returns the root-path of the module.
func GetModuleRootPath() string {
if moduleRootPath == "" {
moduleRootPath = os.Getenv("PHOTOSHOPINFO_MODULE_ROOT_PATH")
if moduleRootPath != "" {
return moduleRootPath
}
currentWd, err := os.Getwd()
log.PanicIf(err)
currentPath := currentWd
visited := make([]string, 0)
for {
tryStampFilepath := path.Join(currentPath, ".MODULE_ROOT")
_, err := os.Stat(tryStampFilepath)
if err != nil && os.IsNotExist(err) != true {
log.Panic(err)
} else if err == nil {
break
}
visited = append(visited, tryStampFilepath)
currentPath = path.Dir(currentPath)
if currentPath == "/" {
log.Panicf("could not find module-root: %v", visited)
}
}
moduleRootPath = currentPath
}
return moduleRootPath
}
// GetTestAssetsPath returns the path of the test-assets.
func GetTestAssetsPath() string {
if assetsPath == "" {
moduleRootPath := GetModuleRootPath()
assetsPath = path.Join(moduleRootPath, "assets")
}
return assetsPath
}
// GetTestDataFilepath returns the file-path of the common test-data.
func GetTestDataFilepath() string {
assetsPath := GetTestAssetsPath()
filepath := path.Join(assetsPath, testDataRelFilepath)
return filepath
}

View File

@ -1,7 +0,0 @@
Copyright 2019 Random Ingenuity InformationWorks
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -1,64 +0,0 @@
[![GoDoc](https://godoc.org/github.com/dsoprea/go-utility/filesystem?status.svg)](https://godoc.org/github.com/dsoprea/go-utility/filesystem)
[![Build Status](https://travis-ci.org/dsoprea/go-utility.svg?branch=master)](https://travis-ci.org/dsoprea/go-utility)
[![Coverage Status](https://coveralls.io/repos/github/dsoprea/go-utility/badge.svg?branch=master)](https://coveralls.io/github/dsoprea/go-utility?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/dsoprea/go-utility)](https://goreportcard.com/report/github.com/dsoprea/go-utility)
# bounceback
An `io.ReadSeeker` and `io.WriteSeeker` that returns to the right place before
reading or writing. Useful when the same file resource is being reused for reads
or writes throughout that file.
# list_files
A recursive path walker that supports filters.
# seekable_buffer
A memory structure that satisfies `io.ReadWriteSeeker`.
# copy_bytes_between_positions
Given an `io.ReadWriteSeeker`, copy N bytes from one position to an earlier
position.
# read_counter, write_counter
Wrap `io.Reader` and `io.Writer` structs in order to report how many bytes were
transferred.
# readseekwritecloser
Provides the ReadWriteSeekCloser interface that combines a RWS and a Closer.
Also provides a no-op wrapper to augment a plain RWS with a closer.
# boundedreadwriteseek
Wraps a ReadWriteSeeker such that no seeks can be at an offset less than a
specific-offset.
# calculateseek
Provides a reusable function with which to calculate seek offsets.
# progress_wrapper
Provides `io.Reader` and `io.Writer` wrappers that also trigger callbacks after
each call. The reader wrapper also invokes the callback upon EOF.
# does_exist
Check whether a file/directory exists using a file-path.
# graceful_copy
Do a copy but correctly handle short-writes and reads that might return a non-
zero read count *and* EOF.
# readseeker_to_readerat
A wrapper that allows an `io.ReadSeeker` to be used as a `io.ReaderAt`.
# simplefileinfo
An implementation of `os.FileInfo` to support testing.

View File

@ -1,273 +0,0 @@
package rifs
import (
"fmt"
"io"
"github.com/dsoprea/go-logging"
)
// BouncebackStats describes operation counts.
type BouncebackStats struct {
reads int
writes int
seeks int
syncs int
}
func (bbs BouncebackStats) String() string {
return fmt.Sprintf(
"BouncebackStats<READS=(%d) WRITES=(%d) SEEKS=(%d) SYNCS=(%d)>",
bbs.reads, bbs.writes, bbs.seeks, bbs.syncs)
}
type bouncebackBase struct {
currentPosition int64
stats BouncebackStats
}
// Position returns the position that we're supposed to be at.
func (bb *bouncebackBase) Position() int64 {
// TODO(dustin): Add test
return bb.currentPosition
}
// StatsReads returns the number of reads that have been attempted.
func (bb *bouncebackBase) StatsReads() int {
// TODO(dustin): Add test
return bb.stats.reads
}
// StatsWrites returns the number of write operations.
func (bb *bouncebackBase) StatsWrites() int {
// TODO(dustin): Add test
return bb.stats.writes
}
// StatsSeeks returns the number of seeks.
func (bb *bouncebackBase) StatsSeeks() int {
// TODO(dustin): Add test
return bb.stats.seeks
}
// StatsSyncs returns the number of corrective seeks ("bounce-backs").
func (bb *bouncebackBase) StatsSyncs() int {
// TODO(dustin): Add test
return bb.stats.syncs
}
// Seek does a seek to an arbitrary place in the `io.ReadSeeker`.
func (bb *bouncebackBase) seek(s io.Seeker, offset int64, whence int) (newPosition int64, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
// If the seek is relative, make sure we're where we're supposed to be *first*.
if whence != io.SeekStart {
err = bb.checkPosition(s)
log.PanicIf(err)
}
bb.stats.seeks++
newPosition, err = s.Seek(offset, whence)
log.PanicIf(err)
// Update our internal tracking.
bb.currentPosition = newPosition
return newPosition, nil
}
func (bb *bouncebackBase) checkPosition(s io.Seeker) (err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
// Make sure we're where we're supposed to be.
// This should have no overhead, and enables us to collect stats.
realCurrentPosition, err := s.Seek(0, io.SeekCurrent)
log.PanicIf(err)
if realCurrentPosition != bb.currentPosition {
bb.stats.syncs++
_, err = s.Seek(bb.currentPosition, io.SeekStart)
log.PanicIf(err)
}
return nil
}
// BouncebackReader wraps a ReadSeeker, keeps track of our position, and
// seeks back to it before writing. This allows an underlying ReadWriteSeeker
// with an unstable position can still be used for a prolonged series of writes.
type BouncebackReader struct {
rs io.ReadSeeker
bouncebackBase
}
// NewBouncebackReader returns a `*BouncebackReader` struct.
func NewBouncebackReader(rs io.ReadSeeker) (br *BouncebackReader, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
initialPosition, err := rs.Seek(0, io.SeekCurrent)
log.PanicIf(err)
bb := bouncebackBase{
currentPosition: initialPosition,
}
br = &BouncebackReader{
rs: rs,
bouncebackBase: bb,
}
return br, nil
}
// Seek does a seek to an arbitrary place in the `io.ReadSeeker`.
func (br *BouncebackReader) Seek(offset int64, whence int) (newPosition int64, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
newPosition, err = br.bouncebackBase.seek(br.rs, offset, whence)
log.PanicIf(err)
return newPosition, nil
}
// Seek does a standard read.
func (br *BouncebackReader) Read(p []byte) (n int, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
br.bouncebackBase.stats.reads++
err = br.bouncebackBase.checkPosition(br.rs)
log.PanicIf(err)
// Do read.
n, err = br.rs.Read(p)
if err != nil {
if err == io.EOF {
return 0, io.EOF
}
log.Panic(err)
}
// Update our internal tracking.
br.bouncebackBase.currentPosition += int64(n)
return n, nil
}
// BouncebackWriter wraps a WriteSeeker, keeps track of our position, and
// seeks back to it before writing. This allows an underlying ReadWriteSeeker
// with an unstable position can still be used for a prolonged series of writes.
type BouncebackWriter struct {
ws io.WriteSeeker
bouncebackBase
}
// NewBouncebackWriter returns a new `BouncebackWriter` struct.
func NewBouncebackWriter(ws io.WriteSeeker) (bw *BouncebackWriter, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
initialPosition, err := ws.Seek(0, io.SeekCurrent)
log.PanicIf(err)
bb := bouncebackBase{
currentPosition: initialPosition,
}
bw = &BouncebackWriter{
ws: ws,
bouncebackBase: bb,
}
return bw, nil
}
// Seek puts us at a specific position in the internal writer for the next
// write/seek.
func (bw *BouncebackWriter) Seek(offset int64, whence int) (newPosition int64, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
newPosition, err = bw.bouncebackBase.seek(bw.ws, offset, whence)
log.PanicIf(err)
return newPosition, nil
}
// Write performs a write against the internal `WriteSeeker` starting at the
// position that we're supposed to be at.
func (bw *BouncebackWriter) Write(p []byte) (n int, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
bw.bouncebackBase.stats.writes++
// Make sure we're where we're supposed to be.
realCurrentPosition, err := bw.ws.Seek(0, io.SeekCurrent)
log.PanicIf(err)
if realCurrentPosition != bw.bouncebackBase.currentPosition {
bw.bouncebackBase.stats.seeks++
_, err = bw.ws.Seek(bw.bouncebackBase.currentPosition, io.SeekStart)
log.PanicIf(err)
}
// Do write.
n, err = bw.ws.Write(p)
log.PanicIf(err)
// Update our internal tracking.
bw.bouncebackBase.currentPosition += int64(n)
return n, nil
}

View File

@ -1,95 +0,0 @@
package rifs
import (
"io"
"github.com/dsoprea/go-logging"
)
// BoundedReadWriteSeekCloser wraps a RWS that is also a closer with boundaries.
// This proxies the RWS methods to the inner BRWS inside.
type BoundedReadWriteSeekCloser struct {
io.Closer
*BoundedReadWriteSeeker
}
// NewBoundedReadWriteSeekCloser returns a new BoundedReadWriteSeekCloser.
func NewBoundedReadWriteSeekCloser(rwsc ReadWriteSeekCloser, minimumOffset int64, staticFileSize int64) (brwsc *BoundedReadWriteSeekCloser, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
bs, err := NewBoundedReadWriteSeeker(rwsc, minimumOffset, staticFileSize)
log.PanicIf(err)
brwsc = &BoundedReadWriteSeekCloser{
Closer: rwsc,
BoundedReadWriteSeeker: bs,
}
return brwsc, nil
}
// Seek forwards calls to the inner RWS.
func (rwsc *BoundedReadWriteSeekCloser) Seek(offset int64, whence int) (newOffset int64, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
newOffset, err = rwsc.BoundedReadWriteSeeker.Seek(offset, whence)
log.PanicIf(err)
return newOffset, nil
}
// Read forwards calls to the inner RWS.
func (rwsc *BoundedReadWriteSeekCloser) Read(buffer []byte) (readCount int, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
readCount, err = rwsc.BoundedReadWriteSeeker.Read(buffer)
if err != nil {
if err == io.EOF {
return 0, err
}
log.Panic(err)
}
return readCount, nil
}
// Write forwards calls to the inner RWS.
func (rwsc *BoundedReadWriteSeekCloser) Write(buffer []byte) (writtenCount int, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
writtenCount, err = rwsc.BoundedReadWriteSeeker.Write(buffer)
log.PanicIf(err)
return writtenCount, nil
}
// Close forwards calls to the inner RWS.
func (rwsc *BoundedReadWriteSeekCloser) Close() (err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
err = rwsc.Closer.Close()
log.PanicIf(err)
return nil
}

View File

@ -1,156 +0,0 @@
package rifs
import (
"errors"
"io"
"os"
"github.com/dsoprea/go-logging"
)
var (
// ErrSeekBeyondBound is returned when a seek is requested beyond the
// statically-given file-size. No writes or seeks beyond boundaries are
// supported with a statically-given file size.
ErrSeekBeyondBound = errors.New("seek beyond boundary")
)
// BoundedReadWriteSeeker is a thin filter that ensures that no seeks can be done
// to offsets smaller than the one we were given. This supports libraries that
// might be expecting to read from the front of the stream being used on data
// that is in the middle of a stream instead.
type BoundedReadWriteSeeker struct {
io.ReadWriteSeeker
currentOffset int64
minimumOffset int64
staticFileSize int64
}
// NewBoundedReadWriteSeeker returns a new BoundedReadWriteSeeker instance.
func NewBoundedReadWriteSeeker(rws io.ReadWriteSeeker, minimumOffset int64, staticFileSize int64) (brws *BoundedReadWriteSeeker, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
if minimumOffset < 0 {
log.Panicf("BoundedReadWriteSeeker minimum offset must be zero or larger: (%d)", minimumOffset)
}
// We'll always started at a relative offset of zero.
_, err = rws.Seek(minimumOffset, os.SEEK_SET)
log.PanicIf(err)
brws = &BoundedReadWriteSeeker{
ReadWriteSeeker: rws,
currentOffset: 0,
minimumOffset: minimumOffset,
staticFileSize: staticFileSize,
}
return brws, nil
}
// Seek moves the offset to the given offset. Prevents offset from ever being
// moved left of `brws.minimumOffset`.
func (brws *BoundedReadWriteSeeker) Seek(offset int64, whence int) (updatedOffset int64, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
fileSize := brws.staticFileSize
// If we weren't given a static file-size, look it up whenever it is needed.
if whence == os.SEEK_END && fileSize == 0 {
realFileSizeRaw, err := brws.ReadWriteSeeker.Seek(0, os.SEEK_END)
log.PanicIf(err)
fileSize = realFileSizeRaw - brws.minimumOffset
}
updatedOffset, err = CalculateSeek(brws.currentOffset, offset, whence, fileSize)
log.PanicIf(err)
if brws.staticFileSize != 0 && updatedOffset > brws.staticFileSize {
//updatedOffset = int64(brws.staticFileSize)
// NOTE(dustin): Presumably, this will only be disruptive to writes that are beyond the boundaries, which, if we're being used at all, should already account for the boundary and prevent this error from ever happening. So, time will tell how disruptive this is.
return 0, ErrSeekBeyondBound
}
if updatedOffset != brws.currentOffset {
updatedRealOffset := updatedOffset + brws.minimumOffset
_, err = brws.ReadWriteSeeker.Seek(updatedRealOffset, os.SEEK_SET)
log.PanicIf(err)
brws.currentOffset = updatedOffset
}
return updatedOffset, nil
}
// Read forwards writes to the inner RWS.
func (brws *BoundedReadWriteSeeker) Read(buffer []byte) (readCount int, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
if brws.staticFileSize != 0 {
availableCount := brws.staticFileSize - brws.currentOffset
if availableCount == 0 {
return 0, io.EOF
}
if int64(len(buffer)) > availableCount {
buffer = buffer[:availableCount]
}
}
readCount, err = brws.ReadWriteSeeker.Read(buffer)
brws.currentOffset += int64(readCount)
if err != nil {
if err == io.EOF {
return 0, err
}
log.Panic(err)
}
return readCount, nil
}
// Write forwards writes to the inner RWS.
func (brws *BoundedReadWriteSeeker) Write(buffer []byte) (writtenCount int, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
if brws.staticFileSize != 0 {
log.Panicf("writes can not be performed if a static file-size was given")
}
writtenCount, err = brws.ReadWriteSeeker.Write(buffer)
brws.currentOffset += int64(writtenCount)
log.PanicIf(err)
return writtenCount, nil
}
// MinimumOffset returns the configured minimum-offset.
func (brws *BoundedReadWriteSeeker) MinimumOffset() int64 {
return brws.minimumOffset
}

View File

@ -1,52 +0,0 @@
package rifs
import (
"io"
"os"
"github.com/dsoprea/go-logging"
)
// SeekType is a convenience type to associate the different seek-types with
// printable descriptions.
type SeekType int
// String returns a descriptive string.
func (n SeekType) String() string {
if n == io.SeekCurrent {
return "SEEK-CURRENT"
} else if n == io.SeekEnd {
return "SEEK-END"
} else if n == io.SeekStart {
return "SEEK-START"
}
log.Panicf("unknown seek-type: (%d)", n)
return ""
}
// CalculateSeek calculates an offset in a file-stream given the parameters.
func CalculateSeek(currentOffset int64, delta int64, whence int, fileSize int64) (finalOffset int64, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
finalOffset = 0
}
}()
if whence == os.SEEK_SET {
finalOffset = delta
} else if whence == os.SEEK_CUR {
finalOffset = currentOffset + delta
} else if whence == os.SEEK_END {
finalOffset = fileSize + delta
} else {
log.Panicf("whence not valid: (%d)", whence)
}
if finalOffset < 0 {
finalOffset = 0
}
return finalOffset, nil
}

View File

@ -1,15 +0,0 @@
package rifs
import (
"os"
"path"
)
var (
appPath string
)
func init() {
goPath := os.Getenv("GOPATH")
appPath = path.Join(goPath, "src", "github.com", "dsoprea", "go-utility", "filesystem")
}

View File

@ -1,40 +0,0 @@
package rifs
import (
"io"
"os"
"github.com/dsoprea/go-logging"
)
// CopyBytesBetweenPositions will copy bytes from one position in the given RWS
// to an earlier position in the same RWS.
func CopyBytesBetweenPositions(rws io.ReadWriteSeeker, fromPosition, toPosition int64, count int) (n int, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
if fromPosition <= toPosition {
log.Panicf("from position (%d) must be larger than to position (%d)", fromPosition, toPosition)
}
br, err := NewBouncebackReader(rws)
log.PanicIf(err)
_, err = br.Seek(fromPosition, os.SEEK_SET)
log.PanicIf(err)
bw, err := NewBouncebackWriter(rws)
log.PanicIf(err)
_, err = bw.Seek(toPosition, os.SEEK_SET)
log.PanicIf(err)
written, err := io.CopyN(bw, br, int64(count))
log.PanicIf(err)
n = int(written)
return n, nil
}

View File

@ -1,19 +0,0 @@
package rifs
import (
"os"
)
// DoesExist returns true if we can open the given file/path without error. We
// can't simply use `os.IsNotExist()` because we'll get a different error when
// the parent directory doesn't exist, and really the only important thing is if
// it exists *and* it's readable.
func DoesExist(filepath string) bool {
f, err := os.Open(filepath)
if err != nil {
return false
}
f.Close()
return true
}

View File

@ -1,54 +0,0 @@
package rifs
import (
"fmt"
"io"
)
const (
defaultCopyBufferSize = 1024 * 1024
)
// GracefulCopy willcopy while enduring lesser normal issues.
//
// - We'll ignore EOF if the read byte-count is more than zero. Only an EOF when
// zero bytes were read will terminate the loop.
//
// - Ignore short-writes. If less bytes were written than the bytes that were
// given, we'll keep trying until done.
func GracefulCopy(w io.Writer, r io.Reader, buffer []byte) (copyCount int, err error) {
if buffer == nil {
buffer = make([]byte, defaultCopyBufferSize)
}
for {
readCount, err := r.Read(buffer)
if err != nil {
if err != io.EOF {
err = fmt.Errorf("read error: %s", err.Error())
return 0, err
}
// Only break on EOF if no bytes were actually read.
if readCount == 0 {
break
}
}
writeBuffer := buffer[:readCount]
for len(writeBuffer) > 0 {
writtenCount, err := w.Write(writeBuffer)
if err != nil {
err = fmt.Errorf("write error: %s", err.Error())
return 0, err
}
writeBuffer = writeBuffer[writtenCount:]
}
copyCount += readCount
}
return copyCount, nil
}

View File

@ -1,143 +0,0 @@
package rifs
import (
"io"
"os"
"path"
"github.com/dsoprea/go-logging"
)
// FileListFilterPredicate is the callback predicate used for filtering.
type FileListFilterPredicate func(parent string, child os.FileInfo) (hit bool, err error)
// VisitedFile is one visited file.
type VisitedFile struct {
Filepath string
Info os.FileInfo
Index int
}
// ListFiles feeds a continuous list of files from a recursive folder scan. An
// optional predicate can be provided in order to filter. When done, the
// `filesC` channel is closed. If there's an error, the `errC` channel will
// receive it.
func ListFiles(rootPath string, cb FileListFilterPredicate) (filesC chan VisitedFile, count int, errC chan error) {
defer func() {
if state := recover(); state != nil {
err := log.Wrap(state.(error))
log.Panic(err)
}
}()
// Make sure the path exists.
f, err := os.Open(rootPath)
log.PanicIf(err)
f.Close()
// Do our thing.
filesC = make(chan VisitedFile, 100)
errC = make(chan error, 1)
index := 0
go func() {
defer func() {
if state := recover(); state != nil {
err := log.Wrap(state.(error))
errC <- err
}
}()
queue := []string{rootPath}
for len(queue) > 0 {
// Pop the next folder to process off the queue.
var thisPath string
thisPath, queue = queue[0], queue[1:]
// Skip path if a symlink.
fi, err := os.Lstat(thisPath)
log.PanicIf(err)
if (fi.Mode() & os.ModeSymlink) > 0 {
continue
}
// Read information.
folderF, err := os.Open(thisPath)
if err != nil {
errC <- log.Wrap(err)
return
}
// Iterate through children.
for {
children, err := folderF.Readdir(1000)
if err == io.EOF {
break
} else if err != nil {
errC <- log.Wrap(err)
return
}
for _, child := range children {
filepath := path.Join(thisPath, child.Name())
// Skip if a file symlink.
fi, err := os.Lstat(filepath)
log.PanicIf(err)
if (fi.Mode() & os.ModeSymlink) > 0 {
continue
}
// If a predicate was given, determine if this child will be
// left behind.
if cb != nil {
hit, err := cb(thisPath, child)
if err != nil {
errC <- log.Wrap(err)
return
}
if hit == false {
continue
}
}
index++
// Push file to channel.
vf := VisitedFile{
Filepath: filepath,
Info: child,
Index: index,
}
filesC <- vf
// If a folder, queue for later processing.
if child.IsDir() == true {
queue = append(queue, filepath)
}
}
}
folderF.Close()
}
close(filesC)
close(errC)
}()
return filesC, index, errC
}

View File

@ -1,93 +0,0 @@
package rifs
import (
"io"
"time"
"github.com/dsoprea/go-logging"
)
// ProgressFunc receives progress updates.
type ProgressFunc func(n int, duration time.Duration, isEof bool) error
// WriteProgressWrapper wraps a reader and calls a callback after each read with
// count and duration info.
type WriteProgressWrapper struct {
w io.Writer
progressCb ProgressFunc
}
// NewWriteProgressWrapper returns a new WPW instance.
func NewWriteProgressWrapper(w io.Writer, progressCb ProgressFunc) io.Writer {
return &WriteProgressWrapper{
w: w,
progressCb: progressCb,
}
}
// Write does a write and calls the callback.
func (wpw *WriteProgressWrapper) Write(buffer []byte) (n int, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
startAt := time.Now()
n, err = wpw.w.Write(buffer)
log.PanicIf(err)
duration := time.Since(startAt)
err = wpw.progressCb(n, duration, false)
log.PanicIf(err)
return n, nil
}
// ReadProgressWrapper wraps a reader and calls a callback after each read with
// count and duration info.
type ReadProgressWrapper struct {
r io.Reader
progressCb ProgressFunc
}
// NewReadProgressWrapper returns a new RPW instance.
func NewReadProgressWrapper(r io.Reader, progressCb ProgressFunc) io.Reader {
return &ReadProgressWrapper{
r: r,
progressCb: progressCb,
}
}
// Read reads data and calls the callback.
func (rpw *ReadProgressWrapper) Read(buffer []byte) (n int, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
startAt := time.Now()
n, err = rpw.r.Read(buffer)
duration := time.Since(startAt)
if err != nil {
if err == io.EOF {
errInner := rpw.progressCb(n, duration, true)
log.PanicIf(errInner)
return n, err
}
log.Panic(err)
}
err = rpw.progressCb(n, duration, false)
log.PanicIf(err)
return n, nil
}

Some files were not shown because too many files have changed in this diff Show More