74 lines
1.7 KiB
Go
74 lines
1.7 KiB
Go
package cp
|
|
|
|
import (
|
|
"bufio"
|
|
"encoding/binary"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
)
|
|
|
|
type CPReader struct {
|
|
FileHandle *os.File
|
|
streamReader *bufio.Reader
|
|
cursorOffset uint32
|
|
//h header
|
|
}
|
|
|
|
const (
|
|
CPE_FILE_IDENTIFIER string = "CPDFile"
|
|
CPE_FILE_IDENTIFIER_SIZE int = 7
|
|
CPE_FILE_VER int = 2
|
|
CPE_FILE_VER_SIZE int = 1
|
|
CPE_ENCRYPTED_FILE_VER int = 3
|
|
CPE_FILE_SUBVER int = 0
|
|
CPE_FILE_SUBVER_SIZE int = 1
|
|
CPE_FILE_HEADER_LENGTH int = 9
|
|
CPE_SECTION_NAME_SIZE int = 3
|
|
CPE_SECTION_LEN_SIZE int = 4
|
|
CPE_SECTION_COUNT int = 3
|
|
CPE_SECTION_EXT string = "EXT"
|
|
CPE_SECTION_CP string = "CPS"
|
|
CPE_SECTION_LLP string = "LLP"
|
|
CPE_SECTION_RADIOPASS string = "RPP"
|
|
CPE_SECTION_TONEFILE string = "TON"
|
|
)
|
|
|
|
func NewReader(fileName string) (_ CPReader, err error) {
|
|
return CPReader{}.NewReader(fileName)
|
|
}
|
|
func (r CPReader) NewReader(fileName string) (_ CPReader, err error) {
|
|
r.FileHandle, err = os.Open(fileName)
|
|
if err != nil {
|
|
return r, err
|
|
}
|
|
r.streamReader = bufio.NewReader(r.FileHandle)
|
|
return r, nil
|
|
}
|
|
func (r *CPReader) Close() {
|
|
r.FileHandle.Close()
|
|
}
|
|
func (r *CPReader) read32Bit() (value int, err error) {
|
|
bytes := make([]byte, 4)
|
|
_, err = io.ReadFull(r.streamReader, bytes)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
r.cursorOffset += 4
|
|
return int(binary.LittleEndian.Uint32(bytes)), nil
|
|
}
|
|
func (r *CPReader) Test() (err error) {
|
|
firstInt, _ := r.read32Bit()
|
|
fmt.Printf("first int is %d\n", firstInt)
|
|
if firstInt == 0 {
|
|
return errors.New("table empty")
|
|
}
|
|
tableIndex := 0
|
|
if tableIndex < firstInt {
|
|
recordType, _ := r.read32Bit()
|
|
fmt.Printf("recordType is %d\n", recordType)
|
|
|
|
}
|
|
return nil
|
|
}
|