From 37a4e3175f449656ecc9c68db52976953c5c3ce3 Mon Sep 17 00:00:00 2001 From: Unbewohnte Date: Mon, 19 Jul 2021 10:24:25 +0300 Subject: [PATCH] =?UTF-8?q?=E2=96=A9=20Converting=20v2.Frame=20to=20bytes?= =?UTF-8?q?=20=E2=96=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- util/bits.go | 5 ++- v2/frame.go | 98 +++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 82 insertions(+), 21 deletions(-) diff --git a/util/bits.go b/util/bits.go index e0f7030..d398ce8 100644 --- a/util/bits.go +++ b/util/bits.go @@ -10,7 +10,10 @@ func GetBit(b byte, bitN int) bool { } // Sets bit to 1 in provided byte, if bitN <= 0 -// returns original b without modifications +// returns original b without modifications. +// bitN is the position of the bit FROM THE LAST RIGHT one! +// ie: byte := 0 // 00000000 +// newbyte := SetBit(byte, 1) // 1 // 00000001 func SetBit(b byte, bitN int) byte { if bitN <= 0 { return b diff --git a/v2/frame.go b/v2/frame.go index 3fce864..775981e 100644 --- a/v2/frame.go +++ b/v2/frame.go @@ -1,6 +1,7 @@ package v2 import ( + "bytes" "fmt" "io" "strings" @@ -267,29 +268,86 @@ func (f *Frame) Text() string { return util.DecodeText(f.Contents) } -// Converts frame to ready-to-write bytes -// func (f *Frame) ToBytes() []byte { -// buff := new(bytes.Buffer) +func v23FlagsToBytes(v23f FrameFlags) []byte { + var flags = []byte{0, 0} + + if v23f.TagAlterPreservation { + flags[0] = util.SetBit(flags[0], 8) + } + if v23f.FileAlterPreservation { + flags[0] = util.SetBit(flags[0], 7) + } + if v23f.ReadOnly { + flags[0] = util.SetBit(flags[0], 6) + } + + if v23f.Compressed { + flags[1] = util.SetBit(flags[1], 8) + } + if v23f.Encrypted { + flags[1] = util.SetBit(flags[1], 7) + } + if v23f.InGroup { + flags[1] = util.SetBit(flags[1], 6) + } + + return flags +} + +func v24FlagsToBytes(v24f FrameFlags) []byte { + var flagBytes = []byte{0, 0} + + if v24f.TagAlterPreservation { + flagBytes[0] = util.SetBit(flagBytes[0], 7) + } + if v24f.FileAlterPreservation { + flagBytes[0] = util.SetBit(flagBytes[0], 6) + } + if v24f.ReadOnly { + flagBytes[0] = util.SetBit(flagBytes[0], 5) + } -// // identifier -// buff.Write([]byte(f.Header.ID)) -// // size -// buff.Write(util.IntToBytesSynchsafe(f.Header.Size)) -// // flags + if v24f.InGroup { + flagBytes[1] = util.SetBit(flagBytes[1], 7) + } + if v24f.Compressed { + flagBytes[1] = util.SetBit(flagBytes[1], 4) + } + if v24f.Encrypted { + flagBytes[1] = util.SetBit(flagBytes[1], 3) + } + if v24f.Unsyrchronised { + flagBytes[1] = util.SetBit(flagBytes[1], 2) + } + if v24f.HasDataLengthIndicator { + flagBytes[1] = util.SetBit(flagBytes[1], 1) + } -// return buff.Bytes() -// } + return flagBytes +} -// Returns bytes of the frame that can be -// written into a file. -// func (f *Frame) Bytes() ([]byte, error) { -// header := f.Header -// contents := f.Contents +// Converts frame to ready-to-write bytes +func (f *Frame) toBytes(version string) []byte { + buff := new(bytes.Buffer) -// var headerbytes []byte + // identifier + buff.Write([]byte(f.Header.ID)) -// identifierBytes := []byte(header.ID) -// // sizeBytes + // size + buff.Write(util.IntToBytesSynchsafe(f.Header.Size)) -// return nil, nil -// } + // flags + var flagBytes []byte + switch version { + case V2_2: + break + case V2_3: + flagBytes = v23FlagsToBytes(f.Header.Flags) + buff.Write(flagBytes) + case V2_4: + flagBytes = v24FlagsToBytes(f.Header.Flags) + buff.Write(flagBytes) + } + + return buff.Bytes() +}