Update dependencies (#2180)

* Update dependencies

* Fix whatsmeow API changes
This commit is contained in:
Wim
2024-08-27 19:04:05 +02:00
committed by GitHub
parent d16645c952
commit c4157a4d5b
589 changed files with 681707 additions and 198856 deletions

View File

@@ -5,9 +5,12 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"math"
"os"
"reflect"
"strconv"
"strings"
"time"
"unicode/utf8"
)
@@ -352,13 +355,22 @@ func readPacket(reader io.Reader) (*Packet, int, error) {
if MaxPacketLengthBytes > 0 && int64(length) > MaxPacketLengthBytes {
return nil, read, fmt.Errorf("length %d greater than maximum %d", length, MaxPacketLengthBytes)
}
content := make([]byte, length)
var content []byte
if length > 0 {
_, err := io.ReadFull(reader, content)
// Read the content and limit it to the parsed length.
// If the content is less than the length, we return an EOF error.
content, err = ioutil.ReadAll(io.LimitReader(reader, int64(length)))
if err == nil && len(content) < int(length) {
err = io.EOF
}
if err != nil {
return nil, read, unexpectedEOF(err)
}
read += length
read += len(content)
} else {
// If length == 0, we set the ByteValue to an empty slice
content = make([]byte, 0)
}
if p.ClassType == ClassUniversal {
@@ -381,6 +393,10 @@ func readPacket(reader io.Reader) (*Packet, int, error) {
p.Value = DecodeString(content)
case TagNULL:
case TagObjectIdentifier:
oid, err := parseObjectIdentifier(content)
if err == nil {
p.Value = OIDToString(oid)
}
case TagObjectDescriptor:
case TagExternal:
case TagRealFloat:
@@ -396,6 +412,10 @@ func readPacket(reader io.Reader) (*Packet, int, error) {
p.Value = val
}
case TagRelativeOID:
oid, err := parseObjectIdentifier(content)
if err == nil {
p.Value = OIDToString(oid)
}
case TagSequence:
case TagSet:
case TagNumericString:
@@ -623,3 +643,166 @@ func NewReal(classType Class, tagType Type, tag Tag, value interface{}, descript
}
return p
}
func NewOID(classType Class, tagType Type, tag Tag, value interface{}, description string) *Packet {
p := Encode(classType, tagType, tag, nil, description)
switch v := value.(type) {
case string:
encoded, err := encodeOID(v)
if err != nil {
fmt.Printf("failed writing %v", err)
return nil
}
p.Value = v
p.Data.Write(encoded)
// TODO: support []int already ?
default:
panic(fmt.Sprintf("Invalid type %T, expected float{64|32}", v))
}
return p
}
// encodeOID takes a string representation of an OID and returns its DER-encoded byte slice along with any error.
func encodeOID(oidString string) ([]byte, error) {
// Convert the string representation to an asn1.ObjectIdentifier
parts := strings.Split(oidString, ".")
oid := make([]int, len(parts))
for i, part := range parts {
var val int
if _, err := fmt.Sscanf(part, "%d", &val); err != nil {
return nil, fmt.Errorf("invalid OID part '%s': %w", part, err)
}
oid[i] = val
}
if len(oid) < 2 || oid[0] > 2 || (oid[0] < 2 && oid[1] >= 40) {
panic(fmt.Sprintf("invalid object identifier % d", oid)) // TODO: not elegant
}
encoded := make([]byte, 0)
encoded = appendBase128Int(encoded[:0], int64(oid[0]*40+oid[1]))
for i := 2; i < len(oid); i++ {
encoded = appendBase128Int(encoded, int64(oid[i]))
}
return encoded, nil
}
func appendBase128Int(dst []byte, n int64) []byte {
l := base128IntLength(n)
for i := l - 1; i >= 0; i-- {
o := byte(n >> uint(i*7))
o &= 0x7f
if i != 0 {
o |= 0x80
}
dst = append(dst, o)
}
return dst
}
func base128IntLength(n int64) int {
if n == 0 {
return 1
}
l := 0
for i := n; i > 0; i >>= 7 {
l++
}
return l
}
func OIDToString(oi []int) string {
var s strings.Builder
s.Grow(32)
buf := make([]byte, 0, 19)
for i, v := range oi {
if i > 0 {
s.WriteByte('.')
}
s.Write(strconv.AppendInt(buf, int64(v), 10))
}
return s.String()
}
// parseObjectIdentifier parses an OBJECT IDENTIFIER from the given bytes and
// returns it. An object identifier is a sequence of variable length integers
// that are assigned in a hierarchy.
func parseObjectIdentifier(bytes []byte) (s []int, err error) {
if len(bytes) == 0 {
err = fmt.Errorf("zero length OBJECT IDENTIFIER")
return
}
// In the worst case, we get two elements from the first byte (which is
// encoded differently) and then every varint is a single byte long.
s = make([]int, len(bytes)+1)
// The first varint is 40*value1 + value2:
// According to this packing, value1 can take the values 0, 1 and 2 only.
// When value1 = 0 or value1 = 1, then value2 is <= 39. When value1 = 2,
// then there are no restrictions on value2.
v, offset, err := parseBase128Int(bytes, 0)
if err != nil {
return
}
if v < 80 {
s[0] = v / 40
s[1] = v % 40
} else {
s[0] = 2
s[1] = v - 80
}
i := 2
for ; offset < len(bytes); i++ {
v, offset, err = parseBase128Int(bytes, offset)
if err != nil {
return
}
s[i] = v
}
s = s[0:i]
return
}
// parseBase128Int parses a base-128 encoded int from the given offset in the
// given byte slice. It returns the value and the new offset.
func parseBase128Int(bytes []byte, initOffset int) (ret, offset int, err error) {
offset = initOffset
var ret64 int64
for shifted := 0; offset < len(bytes); shifted++ {
// 5 * 7 bits per byte == 35 bits of data
// Thus the representation is either non-minimal or too large for an int32
if shifted == 5 {
err = fmt.Errorf("base 128 integer too large")
return
}
ret64 <<= 7
b := bytes[offset]
// integers should be minimally encoded, so the leading octet should
// never be 0x80
if shifted == 0 && b == 0x80 {
err = fmt.Errorf("integer is not minimally encoded")
return
}
ret64 |= int64(b & 0x7f)
offset++
if b&0x80 == 0 {
ret = int(ret64)
// Ensure that the returned value fits in an int on all platforms
if ret64 > math.MaxInt32 {
err = fmt.Errorf("base 128 integer too large")
}
return
}
}
err = fmt.Errorf("truncated base 128 integer")
return
}