forked from AmarnathCJD/gogram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_types.go
executable file
·89 lines (75 loc) · 2.32 KB
/
common_types.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
86
87
88
89
// Copyright (c) 2024 RoseLoverX
package tl
import (
"math/big"
)
// Int128 is alias-like type for fixed size of big int (1024 bit value). It using only for tl objects encoding
// cause native big.Int isn't supported for en(de)coding
type Int128 struct {
*big.Int
}
// NewInt128 creates int128 with zero value
func NewInt128() *Int128 {
return &Int128{Int: big.NewInt(0).SetBytes(make([]byte, Int128Len))}
}
// NewInt128 creates int128 with random value
func RandomInt128() *Int128 {
i := &Int128{Int: big.NewInt(0)}
i.SetBytes(RandomBytes(Int128Len))
return i
}
// func reflectIsInt128(v reflect.Value) bool {
// _, ok := v.Interface().(*Int128)
// return ok
// }
// MarshalTL implements tl marshaler from this package. Just don't use it by your hands, tl.Encoder does all
// what you need
func (i *Int128) MarshalTL(e *Encoder) error {
e.PutRawBytes(BigIntBytes(i.Int, Int128Len*bitsInByte))
return nil
}
// UnmarshalTL implements tl unmarshaler from this package. Just don't use it by your hands, tl.Decoder does
// all what you need
func (i *Int128) UnmarshalTL(d *Decoder) error {
val := d.PopRawBytes(Int128Len)
if d.err != nil {
return d.err
}
i.Int = big.NewInt(0).SetBytes(val)
return nil
}
// Int256 is alias-like type for fixed size of big int (2048 bit value). It using only for tl objects encoding
// cause native big.Int isn't supported for en(de)coding
type Int256 struct {
*big.Int
}
// NewInt256 creates int256 with zero value
func NewInt256() *Int256 {
return &Int256{Int: big.NewInt(0).SetBytes(make([]byte, Int256Len))}
}
// NewInt256 creates int256 with random value
func RandomInt256() *Int256 {
i := &Int256{big.NewInt(0)}
i.SetBytes(RandomBytes(Int256Len))
return i
}
// func reflectIsInt256(v reflect.Value) bool {
// _, ok := v.Interface().(*Int256)
// return ok
// }
// MarshalTL implements tl marshaler from this package. Just don't use it by your hands, tl.Encoder does all
// what you need
func (i *Int256) MarshalTL(e *Encoder) error {
e.PutRawBytes(BigIntBytes(i.Int, Int256Len*bitsInByte))
return nil
}
// UnmarshalTL implements tl unmarshaler from this package. Just don't use it by your hands, tl.Decoder does
// all what you need
func (i *Int256) UnmarshalTL(d *Decoder) error {
val := d.PopRawBytes(Int256Len)
if d.err != nil {
return d.err
}
i.Int = big.NewInt(0).SetBytes(val)
return nil
}