/
iconv.go
112 lines (90 loc) · 2.09 KB
/
iconv.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//
// iconv.go
//
package iconv
// #cgo LDFLAGS: -liconv
// #include <iconv.h>
// #include <errno.h>
// #include <stdlib.h>
import "C"
import (
"bytes"
"io"
"syscall"
"unsafe"
)
const EILSEQ = syscall.Errno(C.EILSEQ)
const E2BIG = syscall.Errno(C.E2BIG)
const DefaultBufSize = 512
type Iconv struct {
Handle C.iconv_t
}
func Open(tocode string, fromcode string) (cd Iconv, err error) {
ctocode := C.CString(tocode)
cfromcode := C.CString(fromcode)
defer func() {
C.free(unsafe.Pointer(ctocode))
C.free(unsafe.Pointer(cfromcode))
}()
ret, err := C.iconv_open(ctocode, cfromcode)
if err != nil {
return
}
cd = Iconv{ret}
return
}
func (cd Iconv) Close() error {
_, err := C.iconv_close(cd.Handle)
return err
}
func (cd Iconv) Conv(b []byte, outbuf []byte) (out []byte, inleft int, err error) {
outn, inleft, err := cd.Do(b, len(b), outbuf)
out = outbuf[:outn]
if err == nil && err != E2BIG {
return
}
w := bytes.NewBuffer(nil)
w.Write(out)
inleft, err = cd.DoWrite(w, b[len(b)-inleft:], inleft, outbuf)
out = w.Bytes()
return
}
func (cd Iconv) ConvString(s string) string {
var outbuf [DefaultBufSize]byte
s1, _, _ := cd.Conv([]byte(s), outbuf[:])
return string(s1)
}
func (cd Iconv) Do(inbuf []byte, in int, outbuf []byte) (out, inleft int, err error) {
if in == 0 {
return
}
inbytes := C.size_t(in)
inptr := &inbuf[0]
outbytes := C.size_t(len(outbuf))
outptr := &outbuf[0]
_, err = C.iconv(cd.Handle,
(**C.char)(unsafe.Pointer(&inptr)), &inbytes,
(**C.char)(unsafe.Pointer(&outptr)), &outbytes)
out = len(outbuf) - int(outbytes)
inleft = int(inbytes)
return
}
func (cd Iconv) DoWrite(w io.Writer, inbuf []byte, in int, outbuf []byte) (inleft int, err error) {
if in == 0 {
return
}
inbytes := C.size_t(in)
inptr := &inbuf[0]
for inbytes > 0 {
outbytes := C.size_t(len(outbuf))
outptr := &outbuf[0]
_, err = C.iconv(cd.Handle,
(**C.char)(unsafe.Pointer(&inptr)), &inbytes,
(**C.char)(unsafe.Pointer(&outptr)), &outbytes)
w.Write(outbuf[:len(outbuf)-int(outbytes)])
if err != nil && err != E2BIG {
return int(inbytes), err
}
}
return 0, nil
}