ConspiracyDecoder/requetes/entete.go

73 lines
1.6 KiB
Go

package requetes
import (
"ConspiracyDecoder/binary"
"errors"
)
const VERSION int32 = 8
var PREAMBULES []int64 = []int64{
2323970988526936351,
-3592950365806191536,
-5122968913806141892,
8933515957217044033,
6339469214218321472,
3767780935981252362,
7646783252089175650,
5853113887207920170,
-394272200419613097,
7360280507499510862,
4357950641317498845,
6624737535948636999,
-553746225865565502,
661090631784662623,
}
type Entete struct {
Preambule int64 `json:"preambule"`
Version int32 `json:"version"`
NumReq int32 `json:"num_req"`
TailleReq int32 `json:"taille_req"`
}
func isPreambuleValide(preambule int64) bool {
for _, el := range PREAMBULES {
if preambule == el {
return true
}
}
return false
}
func isVersionValide(version int32) bool {
return version == VERSION
}
func ReadEntete(bytes []byte) (Entete, error) {
entete := Entete{}
entete.Preambule = binary.BinaryGetInt(bytes, 0, 8)
if !isPreambuleValide(entete.Preambule) {
return Entete{}, errors.New("invalid preambule")
}
entete.Version = int32(binary.BinaryGetInt(bytes, 8, 4))
if !isVersionValide(entete.Version) {
return Entete{}, errors.New("invalid version")
}
entete.NumReq = int32(binary.BinaryGetInt(bytes, 12, 4))
entete.TailleReq = int32(binary.BinaryGetInt(bytes, 16, 4))
return entete, nil
}
func WriteEntete(entete Entete) []byte {
bytes := binary.BinarySetInt(entete.Preambule, 8)
bytes = append(bytes, binary.BinarySetInt(int64(entete.Version), 4)...)
bytes = append(bytes, binary.BinarySetInt(int64(entete.NumReq), 4)...)
bytes = append(bytes, binary.BinarySetInt(int64(entete.TailleReq), 4)...)
return bytes
}