Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions bzip2/_demo/compress/compress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package main

import (
"fmt"
"unsafe"

"github.com/goplus/llgo/c"
"github.com/goplus/llpkg/bzip2"
)

func CompressFile(inPath, outPath string) error {
inPathC := c.AllocaCStr(inPath)

inFile := c.Fopen(inPathC, c.Str("rb"))
if inFile == nil {
return fmt.Errorf("failed to open input file: %s", inPath)
}
defer c.Fclose(inFile)

outPathC := c.AllocaCStr(outPath)
outFile := c.Fopen(outPathC, c.Str("wb"))
if outFile == nil {
return fmt.Errorf("failed to open output file: %s", outPath)
}
defer c.Fclose(outFile)

var bzerr c.Int
bzfile := bzip2.WriteOpen(&bzerr, outFile, 9, 0, 30)
if bzfile == nil || bzerr != bzip2.OK {
return fmt.Errorf("BzWriteOpen error, code=%d", bzerr)
}

buf := make([]byte, 4096)
for {
n := c.Fread(unsafe.Pointer(&buf[0]), 1, uintptr(len(buf)), inFile)
if n == 0 {
break
}

bzip2.Write(&bzerr, bzfile, unsafe.Pointer(&buf[0]), c.Int(n))
if bzerr != bzip2.OK {
return fmt.Errorf("BzWrite error, code=%d", bzerr)
}

if n < uintptr(len(buf)) {
break
}
}

bzip2.WriteClose(&bzerr, bzfile, 0, nil, nil)
if bzerr != bzip2.OK {
return fmt.Errorf("BzWriteClose error, code=%d", bzerr)
}

return nil
}

func main() {
CompressFile("test.txt", "../decompress/test.bz2")
}
1 change: 1 addition & 0 deletions bzip2/_demo/compress/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12345678
76 changes: 76 additions & 0 deletions bzip2/_demo/decompress/decompress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package main

import (
"fmt"
"log"
"os"
"unsafe"

"github.com/goplus/llgo/c"
"github.com/goplus/llpkg/bzip2"
)

func DecompressFile(inPath, outPath string) error {
inPathC := c.AllocaCStr(inPath)

inFile := c.Fopen(inPathC, c.Str("rb"))
if inFile == nil {
return fmt.Errorf("failed to open input file: %s", inPath)
}
defer c.Fclose(inFile)

outPathC := c.AllocaCStr(outPath)

outFile := c.Fopen(outPathC, c.Str("wb"))
if outFile == nil {
return fmt.Errorf("failed to open output file: %s", outPath)
}
defer c.Fclose(outFile)

var bzerr c.Int
bzfile := bzip2.ReadOpen(&bzerr, inFile, 0, 0, nil, 0)
if bzfile == nil || bzerr != bzip2.OK {
return fmt.Errorf("BzReadOpen error, code=%d", bzerr)
}

buf := make([]byte, 4096)
for {
n := bzip2.Read(&bzerr, bzfile, unsafe.Pointer(&buf[0]), c.Int(len(buf)))
if bzerr == bzip2.STREAM_END {
if n > 0 {
c.Fwrite(unsafe.Pointer(&buf[0]), 1, uintptr(n), outFile)
}
break
}

if bzerr != bzip2.OK && bzerr != bzip2.STREAM_END {
return fmt.Errorf("BzRead error, code=%d", bzerr)
}
if n > 0 {
c.Fwrite(unsafe.Pointer(&buf[0]), 1, uintptr(n), outFile)
} else {
break
}
}

bzip2.ReadClose(&bzerr, bzfile)
if bzerr != bzip2.OK {
return fmt.Errorf("BzReadClose error, code=%d", bzerr)
}

return nil
}

func main() {
err := DecompressFile("test.bz2", "ttt.test")
if err != nil {
panic(err)
}

b, err := os.ReadFile("ttt.test")
if err != nil {
panic(err)
}

log.Println(string(b))
}
3 changes: 3 additions & 0 deletions bzip2/bzip2_autogen_link.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package bzip2

const LLGoPackage string = "link: $(pkg-config --libs bzip2);"
136 changes: 136 additions & 0 deletions bzip2/bzlib.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package bzip2

import (
"github.com/goplus/llgo/c"
"unsafe"
)

const RUN = 0
const FLUSH = 1
const FINISH = 2
const OK = 0
const RUN_OK = 1
const FLUSH_OK = 2
const FINISH_OK = 3
const STREAM_END = 4
const MAX_UNUSED = 5000

type BzStream struct {
NextIn *int8
AvailIn c.Uint
TotalInLo32 c.Uint
TotalInHi32 c.Uint
NextOut *int8
AvailOut c.Uint
TotalOutLo32 c.Uint
TotalOutHi32 c.Uint
State unsafe.Pointer
Bzalloc unsafe.Pointer
Bzfree unsafe.Pointer
Opaque unsafe.Pointer
}

/*-- Core (low-level) library functions --*/
// llgo:link (*BzStream).CompressInit C.BZ2_bzCompressInit
func (recv_ *BzStream) CompressInit(blockSize100k c.Int, verbosity c.Int, workFactor c.Int) c.Int {
return 0
}

// llgo:link (*BzStream).Compress C.BZ2_bzCompress
func (recv_ *BzStream) Compress(action c.Int) c.Int {
return 0
}

// llgo:link (*BzStream).CompressEnd C.BZ2_bzCompressEnd
func (recv_ *BzStream) CompressEnd() c.Int {
return 0
}

// llgo:link (*BzStream).DecompressInit C.BZ2_bzDecompressInit
func (recv_ *BzStream) DecompressInit(verbosity c.Int, small c.Int) c.Int {
return 0
}

// llgo:link (*BzStream).Decompress C.BZ2_bzDecompress
func (recv_ *BzStream) Decompress() c.Int {
return 0
}

// llgo:link (*BzStream).DecompressEnd C.BZ2_bzDecompressEnd
func (recv_ *BzStream) DecompressEnd() c.Int {
return 0
}

type BZFILE [0]byte

//go:linkname ReadOpen C.BZ2_bzReadOpen
func ReadOpen(bzerror *c.Int, f *c.FILE, verbosity c.Int, small c.Int, unused unsafe.Pointer, nUnused c.Int) *BZFILE

//go:linkname ReadClose C.BZ2_bzReadClose
func ReadClose(bzerror *c.Int, b *BZFILE)

//go:linkname ReadGetUnused C.BZ2_bzReadGetUnused
func ReadGetUnused(bzerror *c.Int, b *BZFILE, unused *unsafe.Pointer, nUnused *c.Int)

//go:linkname Read C.BZ2_bzRead
func Read(bzerror *c.Int, b *BZFILE, buf unsafe.Pointer, len c.Int) c.Int

//go:linkname WriteOpen C.BZ2_bzWriteOpen
func WriteOpen(bzerror *c.Int, f *c.FILE, blockSize100k c.Int, verbosity c.Int, workFactor c.Int) *BZFILE

//go:linkname Write C.BZ2_bzWrite
func Write(bzerror *c.Int, b *BZFILE, buf unsafe.Pointer, len c.Int)

//go:linkname WriteClose C.BZ2_bzWriteClose
func WriteClose(bzerror *c.Int, b *BZFILE, abandon c.Int, nbytes_in *c.Uint, nbytes_out *c.Uint)

//go:linkname WriteClose64 C.BZ2_bzWriteClose64
func WriteClose64(bzerror *c.Int, b *BZFILE, abandon c.Int, nbytes_in_lo32 *c.Uint, nbytes_in_hi32 *c.Uint, nbytes_out_lo32 *c.Uint, nbytes_out_hi32 *c.Uint)

/*-- Utility functions --*/
//go:linkname BuffToBuffCompress C.BZ2_bzBuffToBuffCompress
func BuffToBuffCompress(dest *int8, destLen *c.Uint, source *int8, sourceLen c.Uint, blockSize100k c.Int, verbosity c.Int, workFactor c.Int) c.Int

//go:linkname BuffToBuffDecompress C.BZ2_bzBuffToBuffDecompress
func BuffToBuffDecompress(dest *int8, destLen *c.Uint, source *int8, sourceLen c.Uint, small c.Int, verbosity c.Int) c.Int

/*--
Code contributed by Yoshioka Tsuneo ([email protected])
to support better zlib compatibility.
This code is not _officially_ part of libbzip2 (yet);
I haven't tested it, documented it, or considered the
threading-safeness of it.
If this code breaks, please contact both Yoshioka and me.
--*/
//go:linkname LibVersion C.BZ2_bzlibVersion
func LibVersion() *int8

//go:linkname Open C.BZ2_bzopen
func Open(path *int8, mode *int8) *BZFILE

//go:linkname Dopen C.BZ2_bzdopen
func Dopen(fd c.Int, mode *int8) *BZFILE

// llgo:link (*BZFILE).Read C.BZ2_bzread
func (recv_ *BZFILE) Read(buf unsafe.Pointer, len c.Int) c.Int {
return 0
}

// llgo:link (*BZFILE).Write C.BZ2_bzwrite
func (recv_ *BZFILE) Write(buf unsafe.Pointer, len c.Int) c.Int {
return 0
}

// llgo:link (*BZFILE).Flush C.BZ2_bzflush
func (recv_ *BZFILE) Flush() c.Int {
return 0
}

// llgo:link (*BZFILE).Close C.BZ2_bzclose
func (recv_ *BZFILE) Close() {
}

// llgo:link (*BZFILE).Error C.BZ2_bzerror
func (recv_ *BZFILE) Error(errnum *c.Int) *int8 {
return nil
}
5 changes: 5 additions & 0 deletions bzip2/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/goplus/llpkg/bzip2

go 1.20

require github.com/goplus/llgo v0.10.0
2 changes: 2 additions & 0 deletions bzip2/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/goplus/llgo v0.10.0 h1:s3U3cnO3cploF1xCCJleAb16NQFAmHxdUmdrNhRH3hY=
github.com/goplus/llgo v0.10.0/go.mod h1:YfOHsT/g3lc9b4GclLj812YzdSsJr0kd3CCB830TqHE=
22 changes: 22 additions & 0 deletions bzip2/llcppg.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "bzip2",
"cflags": "$(pkg-config --cflags bzip2)",
"libs": "$(pkg-config --libs bzip2)",
"include": [
"bzlib.h"
],
"trimPrefixes": ["BZ2_bz", "BZ_"],
"cplusplus": false,
"deps": [],
"keepUnderScore": false,
"impl": [
{
"files": [],
"cond": {
"os": [],
"arch": []
}
}
],
"mix": false
}
2 changes: 2 additions & 0 deletions bzip2/llcppg.pub
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
BZFILE
bz_stream BzStream
Loading
Loading