binary

package
v1.0.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 23, 2024 License: MPL-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package binary implements encoding and decoding documents in WhatsApp's binary XML format.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidType    = errors.New("unsupported payload type")
	ErrInvalidJIDType = errors.New("invalid JID type")
	ErrInvalidNode    = errors.New("invalid node")
	ErrInvalidToken   = errors.New("invalid token with tag")
	ErrNonStringKey   = errors.New("non-string key")
)

Errors returned by the binary XML decoder.

View Source
var (
	IndentXML            = false
	MaxBytesToPrintAsHex = 128
)

Options to control how Node.XMLString behaves.

Functions

func Marshal

func Marshal(n Node) ([]byte, error)

Marshal encodes an XML element (Node) into WhatsApp's binary XML representation.

func Unpack

func Unpack(data []byte) ([]byte, error)

Unpack unpacks the given decrypted data from the WhatsApp web API.

It checks the first byte to decide whether to uncompress the data with zlib or just return as-is (without the first byte). There's currently no corresponding Pack function because Marshal already returns the data with a leading zero (i.e. not compressed).

Types

type AttrUtility

type AttrUtility struct {
	Attrs  Attrs
	Errors []error
}

AttrUtility is a helper struct for reading multiple XML attributes and checking for errors afterwards.

The functions return values directly and append any decoding errors to the Errors slice. The slice can then be checked after all necessary attributes are read, instead of having to check each attribute for errors separately.

func (*AttrUtility) Bool

func (au *AttrUtility) Bool(key string) bool

func (*AttrUtility) Error

func (au *AttrUtility) Error() error

Error returns the list of errors as a single error interface, or nil if there are no errors.

func (*AttrUtility) GetBool

func (au *AttrUtility) GetBool(key string, require bool) (bool, bool)

func (*AttrUtility) GetInt64

func (au *AttrUtility) GetInt64(key string, require bool) (int64, bool)

func (*AttrUtility) GetJID

func (au *AttrUtility) GetJID(key string, require bool) (jidVal types.JID, ok bool)

func (*AttrUtility) GetString

func (au *AttrUtility) GetString(key string, require bool) (strVal string, ok bool)

func (*AttrUtility) GetUint64

func (au *AttrUtility) GetUint64(key string, require bool) (uint64, bool)

func (*AttrUtility) GetUnixMilli

func (au *AttrUtility) GetUnixMilli(key string, require bool) (time.Time, bool)

func (*AttrUtility) GetUnixTime

func (au *AttrUtility) GetUnixTime(key string, require bool) (time.Time, bool)

func (*AttrUtility) Int

func (au *AttrUtility) Int(key string) int

func (*AttrUtility) Int64

func (au *AttrUtility) Int64(key string) int64

func (*AttrUtility) JID

func (au *AttrUtility) JID(key string) types.JID

JID returns the JID under the given key. If there's no valid JID under the given key, an error will be stored and a blank JID struct will be returned.

func (*AttrUtility) OK

func (au *AttrUtility) OK() bool

OK returns true if there are no errors.

func (*AttrUtility) OptionalBool

func (au *AttrUtility) OptionalBool(key string) bool

func (*AttrUtility) OptionalInt

func (au *AttrUtility) OptionalInt(key string) int

func (*AttrUtility) OptionalJID

func (au *AttrUtility) OptionalJID(key string) *types.JID

OptionalJID returns the JID under the given key. If there's no valid JID under the given key, this will return nil. However, if the attribute is completely missing, this will not store an error.

func (*AttrUtility) OptionalJIDOrEmpty

func (au *AttrUtility) OptionalJIDOrEmpty(key string) types.JID

OptionalJIDOrEmpty returns the JID under the given key. If there's no valid JID under the given key, this will return an empty JID. However, if the attribute is completely missing, this will not store an error.

func (*AttrUtility) OptionalString

func (au *AttrUtility) OptionalString(key string) string

OptionalString returns the string under the given key.

func (*AttrUtility) OptionalUnixMilli

func (au *AttrUtility) OptionalUnixMilli(key string) time.Time

func (*AttrUtility) OptionalUnixTime

func (au *AttrUtility) OptionalUnixTime(key string) time.Time

func (*AttrUtility) String

func (au *AttrUtility) String(key string) string

String returns the string under the given key. If there's no valid string under the given key, an error will be stored and an empty string will be returned.

func (*AttrUtility) Uint64

func (au *AttrUtility) Uint64(key string) uint64

func (*AttrUtility) UnixMilli

func (au *AttrUtility) UnixMilli(key string) time.Time

func (*AttrUtility) UnixTime

func (au *AttrUtility) UnixTime(key string) time.Time

type Attrs

type Attrs = map[string]any

Attrs is a type alias for the attributes of an XML element (Node).

type ErrorList

type ErrorList []error

ErrorList is a list of errors that implements the error interface itself.

func (ErrorList) Error

func (el ErrorList) Error() string

Error returns all the errors in the list as a string.

type Node

type Node struct {
	Tag     string      // The tag of the element.
	Attrs   Attrs       // The attributes of the element.
	Content interface{} // The content inside the element. Can be nil, a list of Nodes, or a byte array.
}

Node represents an XML element.

func Unmarshal

func Unmarshal(data []byte) (*Node, error)

Unmarshal decodes WhatsApp's binary XML representation into a Node.

func (*Node) AttrGetter

func (n *Node) AttrGetter() *AttrUtility

AttrGetter returns the AttrUtility for this Node.

func (*Node) GetChildByTag

func (n *Node) GetChildByTag(tags ...string) Node

GetChildByTag does the same thing as GetOptionalChildByTag, but returns the Node directly without the ok boolean.

func (*Node) GetChildren

func (n *Node) GetChildren() []Node

GetChildren returns the Content of the node as a list of nodes. If the content is not a list of nodes, this returns nil.

func (*Node) GetChildrenByTag

func (n *Node) GetChildrenByTag(tag string) (children []Node)

GetChildrenByTag returns the same list as GetChildren, but filters it by tag first.

func (*Node) GetOptionalChildByTag

func (n *Node) GetOptionalChildByTag(tags ...string) (val Node, ok bool)

GetOptionalChildByTag finds the first child with the given tag and returns it. Each provided tag will recurse in, so this is useful for getting a specific nested element.

func (*Node) UnmarshalJSON

func (n *Node) UnmarshalJSON(data []byte) error

func (*Node) XMLString

func (n *Node) XMLString() string

XMLString converts the Node to its XML representation

Directories

Path Synopsis
Package proto contains the compiled protobuf structs from WhatsApp's protobuf schema.
Package proto contains the compiled protobuf structs from WhatsApp's protobuf schema.
Package token contains maps of predefined tokens that WhatsApp's binary XML encoding uses to save bytes when sending commonly used strings.
Package token contains maps of predefined tokens that WhatsApp's binary XML encoding uses to save bytes when sending commonly used strings.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL