gbk.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. // Copyright 2013 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package simplifiedchinese
  5. import (
  6. "unicode/utf8"
  7. "golang.org/x/text/encoding"
  8. "golang.org/x/text/encoding/internal"
  9. "golang.org/x/text/encoding/internal/identifier"
  10. "golang.org/x/text/transform"
  11. )
  12. var (
  13. // GB18030 is the GB18030 encoding.
  14. GB18030 encoding.Encoding = &gbk18030
  15. // GBK is the GBK encoding. It encodes an extension of the GB2312 character set
  16. // and is also known as Code Page 936.
  17. GBK encoding.Encoding = &gbk
  18. )
  19. var gbk = internal.Encoding{
  20. &internal.SimpleEncoding{
  21. gbkDecoder{gb18030: false},
  22. gbkEncoder{gb18030: false},
  23. },
  24. "GBK",
  25. identifier.GBK,
  26. }
  27. var gbk18030 = internal.Encoding{
  28. &internal.SimpleEncoding{
  29. gbkDecoder{gb18030: true},
  30. gbkEncoder{gb18030: true},
  31. },
  32. "GB18030",
  33. identifier.GB18030,
  34. }
  35. type gbkDecoder struct {
  36. transform.NopResetter
  37. gb18030 bool
  38. }
  39. func (d gbkDecoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
  40. r, size := rune(0), 0
  41. loop:
  42. for ; nSrc < len(src); nSrc += size {
  43. switch c0 := src[nSrc]; {
  44. case c0 < utf8.RuneSelf:
  45. r, size = rune(c0), 1
  46. // Microsoft's Code Page 936 extends GBK 1.0 to encode the euro sign U+20AC
  47. // as 0x80. The HTML5 specification at http://encoding.spec.whatwg.org/#gbk
  48. // says to treat "gbk" as Code Page 936.
  49. case c0 == 0x80:
  50. r, size = '€', 1
  51. case c0 < 0xff:
  52. if nSrc+1 >= len(src) {
  53. if !atEOF {
  54. err = transform.ErrShortSrc
  55. break loop
  56. }
  57. r, size = utf8.RuneError, 1
  58. goto write
  59. }
  60. c1 := src[nSrc+1]
  61. switch {
  62. case 0x40 <= c1 && c1 < 0x7f:
  63. c1 -= 0x40
  64. case 0x80 <= c1 && c1 < 0xff:
  65. c1 -= 0x41
  66. case d.gb18030 && 0x30 <= c1 && c1 < 0x40:
  67. if nSrc+3 >= len(src) {
  68. if !atEOF {
  69. err = transform.ErrShortSrc
  70. break loop
  71. }
  72. // The second byte here is always ASCII, so we can set size
  73. // to 1 in all cases.
  74. r, size = utf8.RuneError, 1
  75. goto write
  76. }
  77. c2 := src[nSrc+2]
  78. if c2 < 0x81 || 0xff <= c2 {
  79. r, size = utf8.RuneError, 1
  80. goto write
  81. }
  82. c3 := src[nSrc+3]
  83. if c3 < 0x30 || 0x3a <= c3 {
  84. r, size = utf8.RuneError, 1
  85. goto write
  86. }
  87. size = 4
  88. r = ((rune(c0-0x81)*10+rune(c1-0x30))*126+rune(c2-0x81))*10 + rune(c3-0x30)
  89. if r < 39420 {
  90. i, j := 0, len(gb18030)
  91. for i < j {
  92. h := i + (j-i)/2
  93. if r >= rune(gb18030[h][0]) {
  94. i = h + 1
  95. } else {
  96. j = h
  97. }
  98. }
  99. dec := &gb18030[i-1]
  100. r += rune(dec[1]) - rune(dec[0])
  101. goto write
  102. }
  103. r -= 189000
  104. if 0 <= r && r < 0x100000 {
  105. r += 0x10000
  106. } else {
  107. r, size = utf8.RuneError, 1
  108. }
  109. goto write
  110. default:
  111. r, size = utf8.RuneError, 1
  112. goto write
  113. }
  114. r, size = '\ufffd', 2
  115. if i := int(c0-0x81)*190 + int(c1); i < len(decode) {
  116. r = rune(decode[i])
  117. if r == 0 {
  118. r = '\ufffd'
  119. }
  120. }
  121. default:
  122. r, size = utf8.RuneError, 1
  123. }
  124. write:
  125. if nDst+utf8.RuneLen(r) > len(dst) {
  126. err = transform.ErrShortDst
  127. break loop
  128. }
  129. nDst += utf8.EncodeRune(dst[nDst:], r)
  130. }
  131. return nDst, nSrc, err
  132. }
  133. type gbkEncoder struct {
  134. transform.NopResetter
  135. gb18030 bool
  136. }
  137. func (e gbkEncoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
  138. r, r2, size := rune(0), rune(0), 0
  139. for ; nSrc < len(src); nSrc += size {
  140. r = rune(src[nSrc])
  141. // Decode a 1-byte rune.
  142. if r < utf8.RuneSelf {
  143. size = 1
  144. } else {
  145. // Decode a multi-byte rune.
  146. r, size = utf8.DecodeRune(src[nSrc:])
  147. if size == 1 {
  148. // All valid runes of size 1 (those below utf8.RuneSelf) were
  149. // handled above. We have invalid UTF-8 or we haven't seen the
  150. // full character yet.
  151. if !atEOF && !utf8.FullRune(src[nSrc:]) {
  152. err = transform.ErrShortSrc
  153. break
  154. }
  155. }
  156. // func init checks that the switch covers all tables.
  157. switch {
  158. case encode0Low <= r && r < encode0High:
  159. if r2 = rune(encode0[r-encode0Low]); r2 != 0 {
  160. goto write2
  161. }
  162. case encode1Low <= r && r < encode1High:
  163. // Microsoft's Code Page 936 extends GBK 1.0 to encode the euro sign U+20AC
  164. // as 0x80. The HTML5 specification at http://encoding.spec.whatwg.org/#gbk
  165. // says to treat "gbk" as Code Page 936.
  166. if r == '€' {
  167. r = 0x80
  168. goto write1
  169. }
  170. if r2 = rune(encode1[r-encode1Low]); r2 != 0 {
  171. goto write2
  172. }
  173. case encode2Low <= r && r < encode2High:
  174. if r2 = rune(encode2[r-encode2Low]); r2 != 0 {
  175. goto write2
  176. }
  177. case encode3Low <= r && r < encode3High:
  178. if r2 = rune(encode3[r-encode3Low]); r2 != 0 {
  179. goto write2
  180. }
  181. case encode4Low <= r && r < encode4High:
  182. if r2 = rune(encode4[r-encode4Low]); r2 != 0 {
  183. goto write2
  184. }
  185. }
  186. if e.gb18030 {
  187. if r < 0x10000 {
  188. i, j := 0, len(gb18030)
  189. for i < j {
  190. h := i + (j-i)/2
  191. if r >= rune(gb18030[h][1]) {
  192. i = h + 1
  193. } else {
  194. j = h
  195. }
  196. }
  197. dec := &gb18030[i-1]
  198. r += rune(dec[0]) - rune(dec[1])
  199. goto write4
  200. } else if r < 0x110000 {
  201. r += 189000 - 0x10000
  202. goto write4
  203. }
  204. }
  205. err = internal.ErrASCIIReplacement
  206. break
  207. }
  208. write1:
  209. if nDst >= len(dst) {
  210. err = transform.ErrShortDst
  211. break
  212. }
  213. dst[nDst] = uint8(r)
  214. nDst++
  215. continue
  216. write2:
  217. if nDst+2 > len(dst) {
  218. err = transform.ErrShortDst
  219. break
  220. }
  221. dst[nDst+0] = uint8(r2 >> 8)
  222. dst[nDst+1] = uint8(r2)
  223. nDst += 2
  224. continue
  225. write4:
  226. if nDst+4 > len(dst) {
  227. err = transform.ErrShortDst
  228. break
  229. }
  230. dst[nDst+3] = uint8(r%10 + 0x30)
  231. r /= 10
  232. dst[nDst+2] = uint8(r%126 + 0x81)
  233. r /= 126
  234. dst[nDst+1] = uint8(r%10 + 0x30)
  235. r /= 10
  236. dst[nDst+0] = uint8(r + 0x81)
  237. nDst += 4
  238. continue
  239. }
  240. return nDst, nSrc, err
  241. }
  242. func init() {
  243. // Check that the hard-coded encode switch covers all tables.
  244. if numEncodeTables != 5 {
  245. panic("bad numEncodeTables")
  246. }
  247. }