Documentation
¶
Index ¶
Examples ¶
Constants ¶
View Source
const ( // Version is the current version of the .trix file format. Version = 2 // MaxHeaderSize is the maximum allowed size for the header. MaxHeaderSize = 16 * 1024 * 1024 // 16 MB )
Variables ¶
View Source
var ( // ErrInvalidMagicNumber is returned when the magic number is incorrect. ErrInvalidMagicNumber = errors.New("trix: invalid magic number") // ErrInvalidVersion is returned when the version is incorrect. ErrInvalidVersion = errors.New("trix: invalid version") // ErrMagicNumberLength is returned when the magic number is not 4 bytes long. ErrMagicNumberLength = errors.New("trix: magic number must be 4 bytes long") // ErrNilSigil is returned when a sigil is nil. ErrNilSigil = errors.New("trix: sigil cannot be nil") // ErrChecksumMismatch is returned when the checksum does not match. ErrChecksumMismatch = errors.New("trix: checksum mismatch") // ErrHeaderTooLarge is returned when the header size exceeds the maximum allowed. ErrHeaderTooLarge = errors.New("trix: header size exceeds maximum allowed") )
Functions ¶
func Encode ¶
Encode serializes a Trix struct into the .trix binary format. It returns the encoded data as a byte slice.
Example ¶
package main
import (
"fmt"
"log"
"github.com/Snider/Enchantrix/pkg/trix"
)
func main() {
t := &trix.Trix{
Header: map[string]interface{}{"author": "Jules"},
Payload: []byte("Hello, Trix!"),
}
encoded, err := trix.Encode(t, "TRIX", nil)
if err != nil {
log.Fatalf("Encode failed: %v", err)
}
fmt.Printf("Encoded data is not empty: %v\n", len(encoded) > 0)
}
Output: Encoded data is not empty: true
Types ¶
type Trix ¶
type Trix struct {
Header map[string]interface{}
Payload []byte
InSigils []string `json:"-"` // Ignore Sigils during JSON marshaling
OutSigils []string `json:"-"` // Ignore Sigils during JSON marshaling
ChecksumAlgo crypt.HashType `json:"-"`
}
Trix represents the structure of a .trix file. It contains a header, a payload, and optional sigils for data transformation.
func Decode ¶
Decode deserializes the .trix binary format into a Trix struct. It returns the decoded Trix struct. Note: Sigils are not stored in the format and must be re-attached by the caller.
Example ¶
package main
import (
"fmt"
"log"
"github.com/Snider/Enchantrix/pkg/trix"
)
func main() {
t := &trix.Trix{
Header: map[string]interface{}{"author": "Jules"},
Payload: []byte("Hello, Trix!"),
}
encoded, err := trix.Encode(t, "TRIX", nil)
if err != nil {
log.Fatalf("Encode failed: %v", err)
}
decoded, err := trix.Decode(encoded, "TRIX", nil)
if err != nil {
log.Fatalf("Decode failed: %v", err)
}
fmt.Printf("Decoded payload: %s\n", decoded.Payload)
fmt.Printf("Decoded header: %v\n", decoded.Header)
}
Output: Decoded payload: Hello, Trix! Decoded header: map[author:Jules]
func (*Trix) Pack ¶
Pack applies the In method of all attached sigils to the payload. It modifies the Trix struct in place.
Example ¶
package main
import (
"fmt"
"log"
"github.com/Snider/Enchantrix/pkg/trix"
)
func main() {
t := &trix.Trix{
Payload: []byte("secret message"),
InSigils: []string{"base64", "reverse"},
}
err := t.Pack()
if err != nil {
log.Fatalf("Pack failed: %v", err)
}
fmt.Printf("Packed payload: %s\n", t.Payload)
}
Output: Packed payload: =U2ZhN3cl1GI0VmcjV2c
Example (Checksum) ¶
package main
import (
"fmt"
"log"
"github.com/Snider/Enchantrix/pkg/crypt"
"github.com/Snider/Enchantrix/pkg/trix"
)
func main() {
t := &trix.Trix{
Header: map[string]interface{}{},
Payload: []byte("secret message"),
InSigils: []string{"base64", "reverse"},
ChecksumAlgo: crypt.SHA256,
}
encoded, err := trix.Encode(t, "TRIX", nil)
if err != nil {
log.Fatalf("Encode failed: %v", err)
}
decoded, err := trix.Decode(encoded, "TRIX", nil)
if err != nil {
log.Fatalf("Decode failed: %v", err)
}
fmt.Printf("Decoded payload: %s\n", decoded.Payload)
fmt.Printf("Checksum verified: %v\n", decoded.Header["checksum"] != nil)
}
Output: Decoded payload: secret message Checksum verified: true
func (*Trix) Unpack ¶
Unpack applies the Out method of all sigils in reverse order. It modifies the Trix struct in place.
Example ¶
package main
import (
"fmt"
"log"
"github.com/Snider/Enchantrix/pkg/trix"
)
func main() {
t := &trix.Trix{
Payload: []byte("=U2ZhN3cl1GI0VmcjV2c"),
OutSigils: []string{"base64", "reverse"},
}
err := t.Unpack()
if err != nil {
log.Fatalf("Unpack failed: %v", err)
}
fmt.Printf("Unpacked payload: %s\n", t.Payload)
}
Output: Unpacked payload: secret message
Click to show internal directories.
Click to hide internal directories.