├── go.mod ├── .travis.yml ├── .gitignore ├── examples ├── convstr │ └── convstr.go ├── iconvreader │ └── reader.go └── iconvwriter │ └── writer.go ├── CHANGELOG.md ├── reader.go ├── writer.go ├── README.md ├── iconv.go ├── iconv_test.go └── LICENSE /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/qiniu/iconv 2 | 3 | go 1.14 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: go 2 | go: 3 | - "1.14" 4 | script: 5 | - go test -race -coverprofile=coverage.txt -covermode=atomic ./... 6 | after_success: 7 | - bash <(curl -s https://codecov.io/bash) 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | -------------------------------------------------------------------------------- /examples/convstr/convstr.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/qiniu/iconv" 7 | ) 8 | 9 | func main() { 10 | 11 | cd, err := iconv.Open("gbk", "utf-8") 12 | if err != nil { 13 | fmt.Println("iconv.Open failed!") 14 | return 15 | } 16 | defer cd.Close() 17 | fmt.Println("go") 18 | gbk := cd.ConvString( 19 | ` 你好,世界!你好,世界!你好,世界!你好,世界! 20 | 你好,世界!你好,世界!你好,世界!你好,世界!`) 21 | fmt.Println(gbk) 22 | } 23 | -------------------------------------------------------------------------------- /examples/iconvreader/reader.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "io" 6 | "os" 7 | 8 | "github.com/qiniu/iconv" 9 | ) 10 | 11 | func main() { 12 | 13 | cd, err := iconv.Open("utf-8", "gbk") // gbk => utf8 14 | if err != nil { 15 | fmt.Println("iconv.Open failed!") 16 | return 17 | } 18 | defer cd.Close() 19 | 20 | r := iconv.NewReader(cd, os.Stdin, 0) 21 | 22 | _, err = io.Copy(os.Stdout, r) 23 | if err != nil { 24 | fmt.Println("\nio.Copy failed:", err) 25 | return 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /examples/iconvwriter/writer.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | 7 | "github.com/qiniu/iconv" 8 | ) 9 | 10 | func main() { 11 | cd, err := iconv.Open("gbk", "utf-8") // utf8 => gbk 12 | if err != nil { 13 | fmt.Println("iconv.Open failed!") 14 | return 15 | } 16 | defer cd.Close() 17 | 18 | autoSync := false 19 | w := iconv.NewWriter(cd, os.Stdout, 0, autoSync) 20 | 21 | fmt.Fprintln(w, 22 | ` 你好,世界!你好,世界!你好,世界!你好,世界! 23 | 你好,世界!你好,世界!你好,世界!你好,世界!`) 24 | 25 | w.Sync() // call it by yourself if autoSync == false 26 | } 27 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | #CHANGELOG 2 | 3 | ## v1.1.01 4 | 5 | - support golang 1.6.x 6 | - support gopkg API 7 | 8 | ## v1.0.03 9 | 10 | 2013-09-07 Issue [#13](https://github.com/qiniu/iconv/pull/13), [#14](https://github.com/qiniu/iconv/pull/14): 11 | 12 | - 解决了 Open 时的内存泄漏 13 | - Conv/ConvString bugfix: 在转换结果 > 512 字节时结果不正确 14 | 15 | 16 | ## v1.0.02 17 | 18 | 2013-04-30 Issue [#7](https://github.com/qiniu/iconv/pull/7): 19 | 20 | - Mac OS 支持 21 | 22 | 23 | ## v1.0.01 24 | 25 | 2013-03-11 Issue [#3](https://github.com/qiniu/iconv/pull/3): 26 | 27 | - Travis-CI 支持 28 | -------------------------------------------------------------------------------- /reader.go: -------------------------------------------------------------------------------- 1 | package iconv 2 | 3 | import ( 4 | "io" 5 | ) 6 | 7 | // Reader represents an auto encoding converting Reader. 8 | type Reader struct { 9 | rdbuf []byte 10 | cnvbuf []byte 11 | cd Iconv 12 | input io.Reader 13 | from, to int // rdbuf[from:to] is valid 14 | m int // cnvbuf[:m] is valid 15 | err error 16 | } 17 | 18 | // NewReader creates a new reader. 19 | func NewReader(cd Iconv, input io.Reader, bufSize int) *Reader { 20 | if bufSize < 16 { 21 | bufSize = DefaultBufSize 22 | } 23 | rdbuf := make([]byte, bufSize) 24 | cnvbuf := make([]byte, bufSize) 25 | return &Reader{rdbuf, cnvbuf, cd, input, 0, 0, 0, nil} 26 | } 27 | 28 | // Input changes the input stream. 29 | func (r *Reader) Input(r1 io.Reader) { 30 | r.input = r1 31 | r.from, r.to, r.m = 0, 0, 0 32 | r.err = nil 33 | } 34 | 35 | func (r *Reader) fetch() error { 36 | var m int 37 | if r.err != nil { 38 | return r.err 39 | } 40 | 41 | m, r.err = r.input.Read(r.cnvbuf[r.m:]) 42 | m += r.m 43 | if m == 0 { 44 | return io.EOF 45 | } 46 | 47 | r.from = 0 48 | r.to, r.m, r.err = r.cd.Do(r.cnvbuf, m, r.rdbuf) 49 | if r.err != EILSEQ { 50 | r.err = nil 51 | } 52 | if r.m > 0 { 53 | copy(r.cnvbuf[:r.m], r.cnvbuf[m-r.m:m]) 54 | } 55 | if r.to == 0 { 56 | if r.err == nil { 57 | return io.EOF 58 | } 59 | return r.err 60 | } 61 | return nil 62 | } 63 | 64 | func (r *Reader) Read(b []byte) (n int, err error) { 65 | for { 66 | if r.from < r.to { 67 | n1 := copy(b, r.rdbuf[r.from:r.to]) 68 | n += n1 69 | r.from += n1 70 | if n1 == len(b) { 71 | break 72 | } 73 | b = b[n1:] 74 | } 75 | err = r.fetch() 76 | if err != nil { 77 | break 78 | } 79 | } 80 | return 81 | } 82 | -------------------------------------------------------------------------------- /writer.go: -------------------------------------------------------------------------------- 1 | package iconv 2 | 3 | import ( 4 | "io" 5 | "syscall" 6 | ) 7 | 8 | // Writer represents an auto encoding converting Writer. 9 | type Writer struct { 10 | inbuf []byte 11 | outbuf []byte 12 | cd Iconv 13 | output io.Writer 14 | n int // inbuf[0:n] is valid 15 | autoSync bool 16 | } 17 | 18 | // NewWriter creates a new writer. 19 | func NewWriter(cd Iconv, output io.Writer, bufSize int, autoSync bool) *Writer { 20 | if bufSize < 16 { 21 | bufSize = DefaultBufSize 22 | } 23 | outbuf := make([]byte, bufSize) 24 | var inbuf []byte 25 | if !autoSync { 26 | inbuf = make([]byte, bufSize) 27 | } 28 | return &Writer{inbuf, outbuf, cd, output, 0, autoSync} 29 | } 30 | 31 | // Output changes the output stream. 32 | func (w *Writer) Output(w1 io.Writer) { 33 | w.Sync() 34 | w.output = w1 35 | w.n = 0 36 | } 37 | 38 | // AutoSync sets the autosync flag. 39 | func (w *Writer) AutoSync(b bool) { 40 | w.autoSync = b 41 | if !b && w.inbuf == nil { 42 | w.inbuf = make([]byte, len(w.outbuf)) 43 | } 44 | } 45 | 46 | // Sync syncs buffered text to output stream. 47 | func (w *Writer) Sync() error { 48 | if w.n == 0 { 49 | return nil 50 | } 51 | inleft, err := w.cd.DoWrite(w.output, w.inbuf, w.n, w.outbuf) 52 | if inleft > 0 { 53 | copy(w.inbuf, w.inbuf[w.n-inleft:w.n]) 54 | } 55 | w.n = inleft 56 | return err 57 | } 58 | 59 | func (w *Writer) Write(b []byte) (n int, err error) { 60 | if w.autoSync { 61 | var inleft int 62 | inleft, err = w.cd.DoWrite(w.output, b, len(b), w.outbuf) 63 | n = len(b) - inleft 64 | return 65 | } 66 | for { 67 | n1 := copy(w.inbuf[w.n:], b) 68 | if n1 == 0 { 69 | if len(b) > 0 { 70 | return n, EILSEQ 71 | } 72 | break 73 | } 74 | w.n += n1 75 | n += n1 76 | if w.n == len(w.inbuf) { 77 | err = w.Sync() 78 | if err != nil && err != syscall.EINVAL { 79 | return 80 | } 81 | } 82 | if len(b) == n1 { 83 | break 84 | } 85 | b = b[n1:] 86 | } 87 | return n, nil 88 | } 89 | 90 | // WriteString writes a string. 91 | func (w *Writer) WriteString(b string) (n int, err error) { 92 | if w.autoSync { 93 | var inleft int 94 | inleft, err = w.cd.DoWrite(w.output, []byte(b), len(b), w.outbuf) 95 | n = len(b) - inleft 96 | return 97 | } 98 | for { 99 | n1 := copy(w.inbuf[w.n:], b) 100 | if n1 == 0 { 101 | if len(b) > 0 { 102 | return n, EILSEQ 103 | } 104 | break 105 | } 106 | w.n += n1 107 | n += n1 108 | if w.n == len(w.inbuf) { 109 | err = w.Sync() 110 | if err != nil && err != syscall.EINVAL { 111 | return 112 | } 113 | } 114 | if len(b) == n1 { 115 | break 116 | } 117 | b = b[n1:] 118 | } 119 | return n, nil 120 | } 121 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | iconv: libiconv for go 2 | ====== 3 | 4 | [![LICENSE](https://img.shields.io/github/license/qiniu/iconv.svg)](https://github.com/qiniu/iconv/blob/master/LICENSE) 5 | [![Build Status](https://travis-ci.org/qiniu/iconv.svg?branch=master)](https://travis-ci.org/qiniu/iconv) 6 | [![Go Report Card](https://goreportcard.com/badge/github.com/qiniu/iconv)](https://goreportcard.com/report/github.com/qiniu/iconv) 7 | [![GitHub release](https://img.shields.io/github/v/tag/qiniu/iconv.svg?label=release)](https://github.com/qiniu/iconv/releases) 8 | [![Coverage Status](https://codecov.io/gh/qiniu/iconv/branch/master/graph/badge.svg)](https://codecov.io/gh/qiniu/iconv) 9 | [![GoDoc](https://img.shields.io/badge/Godoc-reference-blue.svg)](https://godoc.org/github.com/qiniu/iconv) 10 | 11 | [![Qiniu Logo](http://open.qiniudn.com/logo.png)](http://www.qiniu.com/) 12 | 13 | iconv is a libiconv wrapper for go. libiconv Convert string to requested character encoding. 14 | 15 | # Document 16 | 17 | See http://godoc.org/github.com/qiniu/iconv 18 | 19 | Note: Open returns a conversion descriptor cd, cd contains a conversion state and can not be used in multiple threads simultaneously. 20 | 21 | # Install 22 | 23 | ``` 24 | go get github.com/qiniu/iconv 25 | ``` 26 | 27 | # Example 28 | 29 | ## Convert string 30 | 31 | ```go 32 | package main 33 | 34 | import ( 35 | "fmt" 36 | "github.com/qiniu/iconv" 37 | ) 38 | 39 | func main() { 40 | 41 | cd, err := iconv.Open("gbk", "utf-8") // convert utf-8 to gbk 42 | if err != nil { 43 | fmt.Println("iconv.Open failed!") 44 | return 45 | } 46 | defer cd.Close() 47 | 48 | gbk := cd.ConvString("你好,世界!") 49 | 50 | fmt.Println(gbk) 51 | } 52 | ``` 53 | 54 | ## Output to io.Writer 55 | 56 | ```go 57 | package main 58 | 59 | import ( 60 | "fmt" 61 | "github.com/qiniu/iconv" 62 | ) 63 | 64 | func main() { 65 | 66 | cd, err := iconv.Open("gbk", "utf-8") // convert utf-8 to gbk 67 | if err != nil { 68 | fmt.Println("iconv.Open failed!") 69 | return 70 | } 71 | defer cd.Close() 72 | 73 | output := ... // eg. output := os.Stdout || ouput, err := os.Create(file) 74 | autoSync := false // buffered or not 75 | bufSize := 0 // default if zero 76 | w := iconv.NewWriter(cd, output, bufSize, autoSync) 77 | 78 | fmt.Fprintln(w, "你好,世界!") 79 | 80 | w.Sync() // if autoSync = false, you need call Sync() by yourself 81 | } 82 | ``` 83 | 84 | ## Input from io.Reader 85 | 86 | ```go 87 | package main 88 | 89 | import ( 90 | "fmt" 91 | "io" 92 | "os" 93 | "github.com/qiniu/iconv" 94 | ) 95 | 96 | func main() { 97 | 98 | cd, err := iconv.Open("utf-8", "gbk") // convert gbk to utf8 99 | if err != nil { 100 | fmt.Println("iconv.Open failed!") 101 | return 102 | } 103 | defer cd.Close() 104 | 105 | input := ... // eg. input := os.Stdin || input, err := os.Open(file) 106 | bufSize := 0 // default if zero 107 | r := iconv.NewReader(cd, input, bufSize) 108 | 109 | _, err = io.Copy(os.Stdout, r) 110 | if err != nil { 111 | fmt.Println("\nio.Copy failed:", err) 112 | return 113 | } 114 | } 115 | ``` 116 | -------------------------------------------------------------------------------- /iconv.go: -------------------------------------------------------------------------------- 1 | // Package iconv is golang bindings to libiconv that converts string to 2 | // requested character encoding. 3 | package iconv 4 | 5 | // #cgo darwin LDFLAGS: -liconv 6 | // #cgo freebsd LDFLAGS: -liconv 7 | // #cgo windows LDFLAGS: -liconv 8 | // #include 9 | // #include 10 | // #include 11 | // 12 | // size_t bridge_iconv(iconv_t cd, 13 | // char *inbuf, size_t *inbytesleft, 14 | // char *outbuf, size_t *outbytesleft) { 15 | // return iconv(cd, &inbuf, inbytesleft, &outbuf, outbytesleft); 16 | // } 17 | import "C" 18 | 19 | import ( 20 | "bytes" 21 | "io" 22 | "syscall" 23 | "unsafe" 24 | ) 25 | 26 | var ( 27 | // EILSEQ error 28 | EILSEQ = syscall.Errno(C.EILSEQ) 29 | // E2BIG error 30 | E2BIG = syscall.Errno(C.E2BIG) 31 | ) 32 | 33 | // DefaultBufSize const 34 | const DefaultBufSize = 4096 35 | 36 | // Iconv represents an iconv handle. 37 | type Iconv struct { 38 | Handle C.iconv_t 39 | } 40 | 41 | // Open returns a conversion descriptor cd, cd contains a conversion state and can not be used in multiple threads simultaneously. 42 | func Open(tocode string, fromcode string) (cd Iconv, err error) { 43 | tocode1 := C.CString(tocode) 44 | defer C.free(unsafe.Pointer(tocode1)) 45 | 46 | fromcode1 := C.CString(fromcode) 47 | defer C.free(unsafe.Pointer(fromcode1)) 48 | 49 | ret, err := C.iconv_open(tocode1, fromcode1) 50 | if err != nil { 51 | return 52 | } 53 | cd = Iconv{ret} 54 | return 55 | } 56 | 57 | // Close closes the iconv handle. 58 | func (cd Iconv) Close() error { 59 | _, err := C.iconv_close(cd.Handle) 60 | return err 61 | } 62 | 63 | // Conv converts text to requested character encoding. 64 | func (cd Iconv) Conv(b []byte, outbuf []byte) (out []byte, inleft int, err error) { 65 | outn, inleft, err := cd.Do(b, len(b), outbuf) 66 | if err == nil || err != E2BIG { 67 | out = outbuf[:outn] 68 | return 69 | } 70 | 71 | w := bytes.NewBuffer(nil) 72 | w.Write(outbuf[:outn]) 73 | 74 | inleft, err = cd.DoWrite(w, b[len(b)-inleft:], inleft, outbuf) 75 | if err != nil { 76 | return 77 | } 78 | out = w.Bytes() 79 | return 80 | } 81 | 82 | // ConvString converts string to requested character encoding. 83 | func (cd Iconv) ConvString(s string) string { 84 | var outbuf [512]byte 85 | s1, _, err := cd.Conv([]byte(s), outbuf[:]) 86 | if err != nil { 87 | return "" 88 | } 89 | return string(s1) 90 | } 91 | 92 | // Do converts text to requested character encoding. 93 | func (cd Iconv) Do(inbuf []byte, in int, outbuf []byte) (out, inleft int, err error) { 94 | if in == 0 { 95 | return 96 | } 97 | inbytes := C.size_t(in) 98 | inptr := &inbuf[0] 99 | 100 | outbytes := C.size_t(len(outbuf)) 101 | outptr := &outbuf[0] 102 | _, err = C.bridge_iconv(cd.Handle, 103 | (*C.char)(unsafe.Pointer(inptr)), &inbytes, 104 | (*C.char)(unsafe.Pointer(outptr)), &outbytes) 105 | 106 | out = len(outbuf) - int(outbytes) 107 | inleft = int(inbytes) 108 | return 109 | } 110 | 111 | // DoWrite converts text to requested character encoding and writes into a Writer. 112 | func (cd Iconv) DoWrite(w io.Writer, inbuf []byte, in int, outbuf []byte) (inleft int, err error) { 113 | if in == 0 { 114 | return 115 | } 116 | inbytes := C.size_t(in) 117 | for inbytes > 0 { 118 | in = int(inbytes) 119 | inptr := &inbuf[len(inbuf)-in] 120 | outbytes := C.size_t(len(outbuf)) 121 | outptr := &outbuf[0] 122 | _, err = C.bridge_iconv(cd.Handle, 123 | (*C.char)(unsafe.Pointer(inptr)), &inbytes, 124 | (*C.char)(unsafe.Pointer(outptr)), &outbytes) 125 | w.Write(outbuf[:len(outbuf)-int(outbytes)]) 126 | if err != nil && err != E2BIG { 127 | return int(inbytes), err 128 | } 129 | } 130 | return 0, nil 131 | } 132 | -------------------------------------------------------------------------------- /iconv_test.go: -------------------------------------------------------------------------------- 1 | package iconv 2 | 3 | import "testing" 4 | 5 | var tstData1 = []struct { 6 | srcChrTyp, dstChrTyp string 7 | src, out string 8 | }{ 9 | { 10 | srcChrTyp: "UTF8", dstChrTyp: "utf-8", 11 | src: "1111111111111111111111111111111111111111111111111111111111111111" + 12 | "2222222222222222222222222222222222222222222222222222222222222222" + 13 | "3333333333333333333333333333333333333333333333333333333333333333" + 14 | "4444444444444444444444444444444444444444444444444444444444444444" + 15 | "5555555555555555555555555555555555555555555555555555555555555555" + 16 | "6666666666666666666666666666666666666666666666666666666666666666" + 17 | "7777777777777777777777777777777777777777777777777777777777777777" + 18 | "8888888888888888888888888888888888888888888888888888888888888888" + 19 | "9999999999999999999999999999999999999999999999999999999999999999" + 20 | "0000000000000000000000000000000000000000000000000000000000000000", 21 | out: "1111111111111111111111111111111111111111111111111111111111111111" + 22 | "2222222222222222222222222222222222222222222222222222222222222222" + 23 | "3333333333333333333333333333333333333333333333333333333333333333" + 24 | "4444444444444444444444444444444444444444444444444444444444444444" + 25 | "5555555555555555555555555555555555555555555555555555555555555555" + 26 | "6666666666666666666666666666666666666666666666666666666666666666" + 27 | "7777777777777777777777777777777777777777777777777777777777777777" + 28 | "8888888888888888888888888888888888888888888888888888888888888888" + 29 | "9999999999999999999999999999999999999999999999999999999999999999" + 30 | "0000000000000000000000000000000000000000000000000000000000000000", 31 | }, 32 | { 33 | srcChrTyp: "UTF8", dstChrTyp: "utf-8", 34 | src: "1111111111111111111111111111111111111111111111111111111111111111" + 35 | "2222222222222222222222222222222222222222222222222222222222222222" + 36 | "3333333333333333333333333333333333333333333333333333333333333333" + 37 | "4444444444444444444444444444444444444444444444444444444444444444" + 38 | "5555555555555555555555555555555555555555555555555555555555555555" + 39 | "6666666666666666666666666666666666666666666666666666666666666666" + 40 | "7777777777777777777777777777777777777777777777777777777777777777" + 41 | "8888888888888888888888888888888888888888888888888888888888888888" + 42 | "9999999999999999999999999999999999999999999999999999999999999999" + 43 | "0000000000000000000000000000000000000000000000000000000000000000" + 44 | "1111111111111111111111111111111111111111111111111111111111111111" + 45 | "2222222222222222222222222222222222222222222222222222222222222222" + 46 | "3333333333333333333333333333333333333333333333333333333333333333" + 47 | "4444444444444444444444444444444444444444444444444444444444444444" + 48 | "5555555555555555555555555555555555555555555555555555555555555555" + 49 | "6666666666666666666666666666666666666666666666666666666666666666" + 50 | "7777777777777777777777777777777777777777777777777777777777777777" + 51 | "8888888888888888888888888888888888888888888888888888888888888888" + 52 | "9999999999999999999999999999999999999999999999999999999999999999" + 53 | "0000000000000000000000000000000000000000000000000000000000000000", 54 | out: "1111111111111111111111111111111111111111111111111111111111111111" + 55 | "2222222222222222222222222222222222222222222222222222222222222222" + 56 | "3333333333333333333333333333333333333333333333333333333333333333" + 57 | "4444444444444444444444444444444444444444444444444444444444444444" + 58 | "5555555555555555555555555555555555555555555555555555555555555555" + 59 | "6666666666666666666666666666666666666666666666666666666666666666" + 60 | "7777777777777777777777777777777777777777777777777777777777777777" + 61 | "8888888888888888888888888888888888888888888888888888888888888888" + 62 | "9999999999999999999999999999999999999999999999999999999999999999" + 63 | "0000000000000000000000000000000000000000000000000000000000000000" + 64 | "1111111111111111111111111111111111111111111111111111111111111111" + 65 | "2222222222222222222222222222222222222222222222222222222222222222" + 66 | "3333333333333333333333333333333333333333333333333333333333333333" + 67 | "4444444444444444444444444444444444444444444444444444444444444444" + 68 | "5555555555555555555555555555555555555555555555555555555555555555" + 69 | "6666666666666666666666666666666666666666666666666666666666666666" + 70 | "7777777777777777777777777777777777777777777777777777777777777777" + 71 | "8888888888888888888888888888888888888888888888888888888888888888" + 72 | "9999999999999999999999999999999999999999999999999999999999999999" + 73 | "0000000000000000000000000000000000000000000000000000000000000000", 74 | }, 75 | { 76 | srcChrTyp: "UTF8", dstChrTyp: "ISO-8859-1", 77 | src: "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + 78 | "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + 79 | "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + 80 | "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + 81 | "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB" + 82 | "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC" + 83 | "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd" + 84 | "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" + 85 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" + 86 | "GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG" + 87 | "hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh" + 88 | "iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii" + 89 | "jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj" + 90 | "kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk" + 91 | "llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll" + 92 | "mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm" + 93 | "nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn" + 94 | "oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo" + 95 | "pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp" + 96 | "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 97 | out: "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + 98 | "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + 99 | "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + 100 | "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + 101 | "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB" + 102 | "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC" + 103 | "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd" + 104 | "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" + 105 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" + 106 | "GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG" + 107 | "hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh" + 108 | "iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii" + 109 | "jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj" + 110 | "kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk" + 111 | "llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll" + 112 | "mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm" + 113 | "nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn" + 114 | "oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo" + 115 | "pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp" + 116 | "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 117 | }, 118 | } 119 | 120 | func TestIconv1(t *testing.T) { 121 | var n int 122 | var b []byte 123 | var outbuf [512]byte 124 | for i, d := range tstData1 { 125 | cd, err := Open(d.srcChrTyp, d.dstChrTyp) 126 | if err != nil { 127 | t.Fatal("Open failed:", err) 128 | } 129 | b, n, err = cd.Conv([]byte(d.src), outbuf[:]) 130 | if err != nil { 131 | t.Fatalf("Test iconv1 return error non nil: %v", err) 132 | } 133 | if d.out != string(b) { 134 | t.Errorf("Wanted:%s\nGot:%s\nTest-Iconv1 test #%d failed with n=%d", 135 | d.out, string(b), i+1, n) 136 | } 137 | } 138 | } 139 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. --------------------------------------------------------------------------------