-
Notifications
You must be signed in to change notification settings - Fork 1
/
url.go
85 lines (76 loc) · 1.77 KB
/
url.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package mp4
import (
"io"
"io/ioutil"
)
// URLBox - DataEntryUrlBox ('url ')
//
// Contained in : DrefBox (dref
type URLBox struct {
Version byte
Flags uint32
Location string // Zero-terminated string
}
const dataIsSelfContainedFlag = 0x000001
// DecodeURLBox - box-specific decode
func DecodeURLBox(hdr *boxHeader, startPos uint64, r io.Reader) (Box, error) {
data, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
s := NewSliceReader(data)
versionAndFlags := s.ReadUint32()
version := byte(versionAndFlags >> 24)
flags := versionAndFlags & flagsMask
location := ""
if flags != dataIsSelfContainedFlag {
location, err = s.ReadZeroTerminatedString()
}
u := &URLBox{
Version: version,
Flags: flags,
Location: location,
}
return u, err
}
// CreateURLBox - Create a self-referencing URL box
func CreateURLBox() *URLBox {
return &URLBox{
Version: 0,
Flags: dataIsSelfContainedFlag,
Location: "",
}
}
// Type - return box type
func (u *URLBox) Type() string {
return "url "
}
// Size - return calculated size
func (u *URLBox) Size() uint64 {
size := uint64(boxHeaderSize + 4)
if u.Flags != uint32(dataIsSelfContainedFlag) {
size += uint64(len(u.Location) + 1)
}
return size
}
// Encode - write box to w
func (u *URLBox) Encode(w io.Writer) error {
err := EncodeHeader(u, w)
if err != nil {
return err
}
buf := makebuf(u)
sw := NewSliceWriter(buf)
versionAndFlags := (uint32(u.Version) << 24) + u.Flags
sw.WriteUint32(versionAndFlags)
if u.Flags != dataIsSelfContainedFlag {
sw.WriteString(u.Location, true)
}
_, err = w.Write(buf)
return err
}
func (u *URLBox) Info(w io.Writer, specificBoxLevels, indent, indentStep string) error {
bd := newInfoDumper(w, indent, u, -1, 0)
bd.write(" - location: %q", u.Location)
return bd.err
}