-
Notifications
You must be signed in to change notification settings - Fork 109
/
iconv.go
131 lines (114 loc) · 3.05 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Package iconv is golang bindings to libiconv that converts string to
// requested character encoding.
package iconv
// #cgo darwin LDFLAGS: -liconv
// #cgo freebsd LDFLAGS: -liconv
// #cgo windows LDFLAGS: -liconv
// #include <iconv.h>
// #include <stdlib.h>
// #include <errno.h>
//
// size_t bridge_iconv(iconv_t cd,
// char *inbuf, size_t *inbytesleft,
// char *outbuf, size_t *outbytesleft) {
// return iconv(cd, &inbuf, inbytesleft, &outbuf, outbytesleft);
// }
import "C"
import (
"bytes"
"io"
"syscall"
"unsafe"
)
var (
// EILSEQ error
EILSEQ = syscall.Errno(C.EILSEQ)
// E2BIG error
E2BIG = syscall.Errno(C.E2BIG)
)
// DefaultBufSize const
const DefaultBufSize = 4096
// Iconv represents an iconv handle.
type Iconv struct {
Handle C.iconv_t
}
// Open returns a conversion descriptor cd, cd contains a conversion state and can not be used in multiple threads simultaneously.
func Open(tocode string, fromcode string) (cd Iconv, err error) {
tocode1 := C.CString(tocode)
defer C.free(unsafe.Pointer(tocode1))
fromcode1 := C.CString(fromcode)
defer C.free(unsafe.Pointer(fromcode1))
ret, err := C.iconv_open(tocode1, fromcode1)
if err != nil {
return
}
cd = Iconv{ret}
return
}
// Close closes the iconv handle.
func (cd Iconv) Close() error {
_, err := C.iconv_close(cd.Handle)
return err
}
// Conv converts text to requested character encoding.
func (cd Iconv) Conv(b []byte, outbuf []byte) (out []byte, inleft int, err error) {
outn, inleft, err := cd.Do(b, len(b), outbuf)
if err == nil || err != E2BIG {
out = outbuf[:outn]
return
}
w := bytes.NewBuffer(nil)
w.Write(outbuf[:outn])
inleft, err = cd.DoWrite(w, b[len(b)-inleft:], inleft, outbuf)
if err != nil {
return
}
out = w.Bytes()
return
}
// ConvString converts string to requested character encoding.
func (cd Iconv) ConvString(s string) string {
var outbuf [512]byte
s1, _, err := cd.Conv([]byte(s), outbuf[:])
if err != nil {
return ""
}
return string(s1)
}
// Do converts text to requested character encoding.
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.bridge_iconv(cd.Handle,
(*C.char)(unsafe.Pointer(inptr)), &inbytes,
(*C.char)(unsafe.Pointer(outptr)), &outbytes)
out = len(outbuf) - int(outbytes)
inleft = int(inbytes)
return
}
// DoWrite converts text to requested character encoding and writes into a Writer.
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)
for inbytes > 0 {
in = int(inbytes)
inptr := &inbuf[len(inbuf)-in]
outbytes := C.size_t(len(outbuf))
outptr := &outbuf[0]
_, err = C.bridge_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
}