package terminfo
import (
)
const (
maxFileLength = 4096
magic = 0o432
magicExtended = 0o1036
)
const (
fieldMagic = iota
fieldNameSize
fieldBoolCount
fieldNumCount
fieldStringCount
fieldTableSize
)
const (
fieldExtBoolCount = iota
fieldExtNumCount
fieldExtStringCount
fieldExtOffsetCount
fieldExtTableSize
)
func ( []int) bool {
return [fieldBoolCount] > CapCountBool ||
[fieldNumCount] > CapCountNum ||
[fieldStringCount] > CapCountString
}
func ( []int) int {
return [fieldNameSize] +
[fieldBoolCount] +
([fieldNameSize]+[fieldBoolCount])%2 +
[fieldNumCount]*2 +
[fieldStringCount]*2 +
[fieldTableSize]
}
func ( []int) bool {
return [fieldExtBoolCount]+
[fieldExtNumCount]+
[fieldExtStringCount]*2 != [fieldExtOffsetCount]
}
func ( []int, int) int {
return [fieldExtBoolCount] +
[fieldExtBoolCount]%2 +
[fieldExtNumCount]*(/8) +
[fieldExtOffsetCount]*2 +
[fieldExtTableSize]
}
func ( []byte, int) int {
for ; < len(); ++ {
if [] == 0 {
return
}
}
return -1
}
func ( []int, []byte, int) (map[int][]byte, int, error) {
var int
:= make(map[int][]byte)
for := 0; < ; ++ {
:= []
if < 0 {
continue
}
if := findNull(, ); != -1 {
[], = [:], +1
} else {
return nil, 0, ErrInvalidStringTable
}
}
return , , nil
}
type decoder struct {
buf []byte
pos int
n int
}
func ( *decoder) ( int) ([]byte, error) {
if .n < .pos+ {
return nil, ErrUnexpectedFileEnd
}
, .pos = .pos, .pos+
return .buf[:.pos], nil
}
func ( *decoder) (, int) ([]int, error) {
/= 8
:= *
, := .readBytes()
if != nil {
return nil,
}
.pos += .pos % 2
:= make([]int, )
for , := 0, 0; < ; , = +, +1 {
switch {
case 1:
[] = int([])
case 2:
[] = int(int16([+1])<<8 | int16([]))
case 4:
[] = int([+3])<<24 | int([+2])<<16 | int([+1])<<8 | int([])
}
}
return , nil
}
func ( *decoder) ( int) (map[int]bool, map[int]bool, error) {
, := .readInts(, 8)
if != nil {
return nil, nil,
}
, := make(map[int]bool), make(map[int]bool)
for , := range {
[] = == 1
if int8() == -2 {
[] = true
}
}
return , , nil
}
func ( *decoder) (, int) (map[int]int, map[int]bool, error) {
, := .readInts(, )
if != nil {
return nil, nil,
}
, := make(map[int]int), make(map[int]bool)
for := 0; < ; ++ {
[] = []
if [] == -2 {
[] = true
}
}
return , , nil
}
func ( *decoder) (, int) ([][]byte, []int, error) {
, := .readInts(, 16)
if != nil {
return nil, nil,
}
, := .readBytes()
if != nil {
return nil, nil,
}
.pos += .pos % 2
:= make([][]byte, )
var []int
for := 0; < ; ++ {
:= []
if == -2 {
= append(, )
} else if >= 0 {
if := findNull(, ); != -1 {
[] = [:]
} else {
return nil, nil, ErrInvalidStringTable
}
}
}
return , , nil
}
func ( *decoder) (, int) (map[int][]byte, map[int]bool, error) {
, , := .readStringTable(, )
if != nil {
return nil, nil,
}
:= make(map[int][]byte)
for , := range {
if == AcsChars {
= canonicalizeAscChars()
}
[] =
}
:= make(map[int]bool, len())
for , := range {
[] = true
}
return , , nil
}
func ( []byte) []byte {
var []byte
:= make(map[byte]byte, len()/2)
for := 0; < len(); += 2 {
if , := [[]]; ! {
, := [], [+1]
, [] = append(, ),
}
}
sort.Slice(, func(, int) bool {
return [] < []
})
:= make([]byte, 2*len())
for := 0; < len(); ++ {
[*2], [*2+1] = [], [[]]
}
return
}