/ pkg / httpx / encodings.go
encodings.go
 1  // Copyright (c) 2024-2026 Tencent Zhuque Lab. All rights reserved.
 2  //
 3  // Licensed under the Apache License, Version 2.0 (the "License");
 4  // you may not use this file except in compliance with the License.
 5  // You may obtain a copy of the License at
 6  //
 7  //     http://www.apache.org/licenses/LICENSE-2.0
 8  //
 9  // Unless required by applicable law or agreed to in writing, software
10  // distributed under the License is distributed on an "AS IS" BASIS,
11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  // See the License for the specific language governing permissions and
13  // limitations under the License.
14  //
15  // Requirement: Any integration or derivative work must explicitly attribute
16  // Tencent Zhuque Lab (https://github.com/Tencent/AI-Infra-Guard) in its
17  // documentation or user interface, as detailed in the NOTICE file.
18  
19  // Package httpx 编码相关
20  package httpx
21  
22  import (
23  	"bytes"
24  	"io/ioutil"
25  
26  	"golang.org/x/text/encoding/simplifiedchinese"
27  	"golang.org/x/text/encoding/traditionalchinese"
28  	"golang.org/x/text/transform"
29  )
30  
31  // Credits: https://gist.github.com/zhangbaohe/c691e1da5bbdc7f41ca5
32  
33  // Decodegbk converts GBK to UTF-8
34  func Decodegbk(s []byte) ([]byte, error) {
35  	I := bytes.NewReader(s)
36  	O := transform.NewReader(I, simplifiedchinese.GBK.NewDecoder())
37  	d, e := ioutil.ReadAll(O)
38  	if e != nil {
39  		return nil, e
40  	}
41  	return d, nil
42  }
43  
44  // Decodebig5 converts BIG5 to UTF-8
45  func Decodebig5(s []byte) ([]byte, error) {
46  	I := bytes.NewReader(s)
47  	O := transform.NewReader(I, traditionalchinese.Big5.NewDecoder())
48  	d, e := ioutil.ReadAll(O)
49  	if e != nil {
50  		return nil, e
51  	}
52  	return d, nil
53  }
54  
55  // Encodebig5 converts UTF-8 to BIG5
56  func Encodebig5(s []byte) ([]byte, error) {
57  	I := bytes.NewReader(s)
58  	O := transform.NewReader(I, traditionalchinese.Big5.NewEncoder())
59  	d, e := ioutil.ReadAll(O)
60  	if e != nil {
61  		return nil, e
62  	}
63  	return d, nil
64  }