Skip to content

Commit

Permalink
replace wrap error handling from std to cockroachdb/errors
Browse files Browse the repository at this point in the history
  • Loading branch information
matsuuram committed Dec 11, 2023
1 parent ed48433 commit 996b684
Show file tree
Hide file tree
Showing 10 changed files with 304 additions and 169 deletions.
92 changes: 68 additions & 24 deletions bitbuffer/bitbuffer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package bitbuffer

import "fmt"
import (
"github.com/cockroachdb/errors"
)

// BitBuffer Peek buffer by the bit
type BitBuffer struct {
Expand All @@ -17,7 +19,7 @@ func (b *BitBuffer) Set(src []byte) {
// Skip Only increase position. Not Peek data.
func (b *BitBuffer) Skip(length uint16) error {
if (b.pos + length) > uint16(len(b.buf)*8) {
return fmt.Errorf("Length(%d) is out of range(%d)", length, len(b.buf))
return errors.Newf("Length(%d) is out of range(%d)", length, len(b.buf))
}
b.pos += length
return nil
Expand All @@ -26,7 +28,7 @@ func (b *BitBuffer) Skip(length uint16) error {
// PeekUint8 return type uint8
func (b *BitBuffer) PeekUint8(length uint16) (uint8, error) {
if length > 8 || (b.pos+length) > uint16(len(b.buf)*8) {
return 0, fmt.Errorf("Length(%d) is out of range(0-8)", length)
return 0, errors.Newf("Length(%d) is out of range(0-8)", length)
}

index := b.pos / 8
Expand Down Expand Up @@ -58,56 +60,98 @@ func (b *BitBuffer) PeekUint8(length uint16) (uint8, error) {
// PeekUint16 return type uint16
func (b *BitBuffer) PeekUint16(length uint16) (uint16, error) {
if length > 16 || (b.pos+length) > uint16(len(b.buf)*8) {
return 0, fmt.Errorf("Length(%d) is out of range(0-16)%d %d", length, b.pos, len(b.buf)*8)
return 0, errors.Newf("Length(%d) is out of range(0-16)%d %d", length, b.pos, len(b.buf)*8)
}

if length <= 8 {
val, err := b.PeekUint8(length)
return uint16(val), err
}
first8, err := b.PeekUint8(8)
second8, err := b.PeekUint8(length - 8)
return uint16(first8)<<(length-8) | uint16(second8), err
var err error
var first8, second8 uint8
first8, err = b.PeekUint8(8)
if err != nil {
return 0, err
}
second8, err = b.PeekUint8(length - 8)
if err != nil {
return 0, err
}
return uint16(first8)<<(length-8) | uint16(second8), nil
}

// PeekUint32 return type uint32
func (b *BitBuffer) PeekUint32(length uint16) (uint32, error) {
if length > 32 || (b.pos+length) > uint16(len(b.buf)*8) {
return 0, fmt.Errorf("Length(%d) is out of range(0-32)", length)
return 0, errors.Newf("Length(%d) is out of range(0-32)", length)
}

var err error
var second8 uint8
var val, first16, second16 uint16
if length <= 16 {
val, err := b.PeekUint16(length)
val, err = b.PeekUint16(length)
return uint32(val), err
} else if length-16 <= 8 {
first16, err := b.PeekUint16(16)
second8, err := b.PeekUint8(length - 16)
first16, err = b.PeekUint16(16)
if err != nil {
return 0, err
}
second8, err = b.PeekUint8(length - 16)
return uint32(first16)<<(length-16) | uint32(second8), err
}
first16, err := b.PeekUint16(16)
second16, err := b.PeekUint16(length - 16)
first16, err = b.PeekUint16(16)
if err != nil {
return 0, err
}
second16, err = b.PeekUint16(length - 16)
return uint32(first16)<<(length-16) | uint32(second16), err
}

// PeekUint64 return type uint64
func (b *BitBuffer) PeekUint64(length uint16) (uint64, error) {
if length > 64 || (b.pos+length) > uint16(len(b.buf)*8) {
return 0, fmt.Errorf("Length(%d) is out of range(0-64)", length)
return 0, errors.Newf("Length(%d) is out of range(0-64)", length)
}

var err error
var second8 uint8
var second16 uint16
var val, first32, second32 uint32
if length <= 32 {
val, err := b.PeekUint32(length)
val, err = b.PeekUint32(length)
if err != nil {
return 0, err
}
return uint64(val), err
} else if length-32 <= 8 {
first32, err := b.PeekUint32(32)
second8, err := b.PeekUint8(length - 32)
return uint64(first32)<<(length-32) | uint64(second8), err
first32, err = b.PeekUint32(32)
if err != nil {
return 0, err
}
second8, err = b.PeekUint8(length - 32)
if err != nil {
return 0, err
}
return uint64(first32)<<(length-32) | uint64(second8), nil
} else if length-32 <= 16 {
first32, err := b.PeekUint32(32)
second16, err := b.PeekUint16(length - 16)
return uint64(first32)<<(length-32) | uint64(second16), err
first32, err = b.PeekUint32(32)
if err != nil {
return 0, err
}
second16, err = b.PeekUint16(length - 16)
if err != nil {
return 0, err
}
return uint64(first32)<<(length-32) | uint64(second16), nil
}
first32, err = b.PeekUint32(32)
if err != nil {
return 0, err
}
second32, err = b.PeekUint32(length - 32)
if err != nil {
return 0, err
}
first32, err := b.PeekUint32(32)
second32, err := b.PeekUint32(length - 32)
return uint64(first32)<<(length-32) | uint64(second32), err
return uint64(first32)<<(length-32) | uint64(second32), nil
}
29 changes: 24 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"

"github.com/cockroachdb/errors"
"github.com/small-teton/mpeg-ts-analyzer/options"
"github.com/small-teton/mpeg-ts-analyzer/tsparser"
"github.com/spf13/cobra"
Expand All @@ -28,7 +29,7 @@ var rootCmd = &cobra.Command{
return
}

opt := loadFlags(cmd)
opt, _ := loadFlags(cmd)
tsparser.ParseTsFile(args[0], opt)
},
}
Expand All @@ -52,20 +53,38 @@ func init() {
rootCmd.Flags().Bool("version", false, "show mpeg-ts-analyzer version.")
}

func loadFlags(cmd *cobra.Command) options.Options {
dumpHeader, _ := cmd.Flags().GetBool("dump-ts-header")
func loadFlags(cmd *cobra.Command) (options.Options, error) {
var opt options.Options
dumpHeader, err := cmd.Flags().GetBool("dump-ts-header")
if err != nil {
return opt, errors.Wrap(err, "failed flag parse --dump-ts-header")
}
dumpPayload, _ := cmd.Flags().GetBool("dump-ts-payload")
if err != nil {
return opt, errors.Wrap(err, "failed flag parse --dump-ts-payload")
}
dumpAdaptationField, _ := cmd.Flags().GetBool("dump-adaptation-field")
if err != nil {
return opt, errors.Wrap(err, "failed flag parse --dump-adaptation-field")
}
dumpPsi, _ := cmd.Flags().GetBool("dump-psi")
if err != nil {
return opt, errors.Wrap(err, "failed flag parse --dump-psi")
}
dumpPesHeader, _ := cmd.Flags().GetBool("dump-pes-header")
if err != nil {
return opt, errors.Wrap(err, "failed flag parse --dump-pes-header")
}
dumpTimestamp, _ := cmd.Flags().GetBool("dump-timestamp")
var opt options.Options
if err != nil {
return opt, errors.Wrap(err, "failed flag parse --dump-timestamp")
}
opt.SetDumpHeader(dumpHeader)
opt.SetDumpPayload(dumpPayload)
opt.SetDumpAdaptationField(dumpAdaptationField)
opt.SetDumpPsi(dumpPsi)
opt.SetDumpPesHeader(dumpPesHeader)
opt.SetDumpTimestamp(dumpTimestamp)

return opt
return opt, nil
}
11 changes: 11 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ go 1.21.4
require github.com/spf13/cobra v1.8.0

require (
github.com/cockroachdb/errors v1.11.1 // indirect
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect
github.com/cockroachdb/redact v1.1.5 // indirect
github.com/getsentry/sentry-go v0.18.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
)
53 changes: 53 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,63 @@
github.com/cockroachdb/errors v1.11.1 h1:xSEW75zKaKCWzR3OfxXUxgrk/NtT4G1MiOv5lWZazG8=
github.com/cockroachdb/errors v1.11.1/go.mod h1:8MUxA3Gi6b25tYlFEBGLf+D8aISL+M4MIpiWMSNRfxw=
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b h1:r6VH0faHjZeQy818SGhaone5OnYfxFR/+AzdY3sf5aE=
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b/go.mod h1:Vz9DsVWQQhf3vs21MhPMZpMGSht7O/2vFW2xusFUVOs=
github.com/cockroachdb/redact v1.1.5 h1:u1PMllDkdFfPWaNGMyLD1+so+aq3uUItthCFqzwPJ30=
github.com/cockroachdb/redact v1.1.5/go.mod h1:BVNblN9mBWFyMyqK1k3AAiSxhvhfK2oOZZ2lK+dpvRg=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/getsentry/sentry-go v0.18.0 h1:MtBW5H9QgdcJabtZcuJG80BMOwaBpkRDZkxRkNC1sN0=
github.com/getsentry/sentry-go v0.18.0/go.mod h1:Kgon4Mby+FJ7ZWHFUAZgVaIa8sxHtnRJRLTXZr51aKQ=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading

0 comments on commit 996b684

Please sign in to comment.