eucjp.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 japanese
  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. // EUCJP is the EUC-JP encoding.
  13. var EUCJP encoding.Encoding = &eucJP
  14. var eucJP = internal.Encoding{
  15. &internal.SimpleEncoding{eucJPDecoder{}, eucJPEncoder{}},
  16. "EUC-JP",
  17. identifier.EUCPkdFmtJapanese,
  18. }
  19. type eucJPDecoder struct{ transform.NopResetter }
  20. // See https://encoding.spec.whatwg.org/#euc-jp-decoder.
  21. func (eucJPDecoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
  22. r, size := rune(0), 0
  23. loop:
  24. for ; nSrc < len(src); nSrc += size {
  25. switch c0 := src[nSrc]; {
  26. case c0 < utf8.RuneSelf:
  27. r, size = rune(c0), 1
  28. case c0 == 0x8e:
  29. if nSrc+1 >= len(src) {
  30. if !atEOF {
  31. err = transform.ErrShortSrc
  32. break loop
  33. }
  34. r, size = utf8.RuneError, 1
  35. break
  36. }
  37. c1 := src[nSrc+1]
  38. switch {
  39. case c1 < 0xa1:
  40. r, size = utf8.RuneError, 1
  41. case c1 > 0xdf:
  42. r, size = utf8.RuneError, 2
  43. if c1 == 0xff {
  44. size = 1
  45. }
  46. default:
  47. r, size = rune(c1)+(0xff61-0xa1), 2
  48. }
  49. case c0 == 0x8f:
  50. if nSrc+2 >= len(src) {
  51. if !atEOF {
  52. err = transform.ErrShortSrc
  53. break loop
  54. }
  55. r, size = utf8.RuneError, 1
  56. if p := nSrc + 1; p < len(src) && 0xa1 <= src[p] && src[p] < 0xfe {
  57. size = 2
  58. }
  59. break
  60. }
  61. c1 := src[nSrc+1]
  62. if c1 < 0xa1 || 0xfe < c1 {
  63. r, size = utf8.RuneError, 1
  64. break
  65. }
  66. c2 := src[nSrc+2]
  67. if c2 < 0xa1 || 0xfe < c2 {
  68. r, size = utf8.RuneError, 2
  69. break
  70. }
  71. r, size = utf8.RuneError, 3
  72. if i := int(c1-0xa1)*94 + int(c2-0xa1); i < len(jis0212Decode) {
  73. r = rune(jis0212Decode[i])
  74. if r == 0 {
  75. r = utf8.RuneError
  76. }
  77. }
  78. case 0xa1 <= c0 && c0 <= 0xfe:
  79. if nSrc+1 >= len(src) {
  80. if !atEOF {
  81. err = transform.ErrShortSrc
  82. break loop
  83. }
  84. r, size = utf8.RuneError, 1
  85. break
  86. }
  87. c1 := src[nSrc+1]
  88. if c1 < 0xa1 || 0xfe < c1 {
  89. r, size = utf8.RuneError, 1
  90. break
  91. }
  92. r, size = utf8.RuneError, 2
  93. if i := int(c0-0xa1)*94 + int(c1-0xa1); i < len(jis0208Decode) {
  94. r = rune(jis0208Decode[i])
  95. if r == 0 {
  96. r = utf8.RuneError
  97. }
  98. }
  99. default:
  100. r, size = utf8.RuneError, 1
  101. }
  102. if nDst+utf8.RuneLen(r) > len(dst) {
  103. err = transform.ErrShortDst
  104. break loop
  105. }
  106. nDst += utf8.EncodeRune(dst[nDst:], r)
  107. }
  108. return nDst, nSrc, err
  109. }
  110. type eucJPEncoder struct{ transform.NopResetter }
  111. func (eucJPEncoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
  112. r, size := rune(0), 0
  113. for ; nSrc < len(src); nSrc += size {
  114. r = rune(src[nSrc])
  115. // Decode a 1-byte rune.
  116. if r < utf8.RuneSelf {
  117. size = 1
  118. } else {
  119. // Decode a multi-byte rune.
  120. r, size = utf8.DecodeRune(src[nSrc:])
  121. if size == 1 {
  122. // All valid runes of size 1 (those below utf8.RuneSelf) were
  123. // handled above. We have invalid UTF-8 or we haven't seen the
  124. // full character yet.
  125. if !atEOF && !utf8.FullRune(src[nSrc:]) {
  126. err = transform.ErrShortSrc
  127. break
  128. }
  129. }
  130. // func init checks that the switch covers all tables.
  131. switch {
  132. case encode0Low <= r && r < encode0High:
  133. if r = rune(encode0[r-encode0Low]); r != 0 {
  134. goto write2or3
  135. }
  136. case encode1Low <= r && r < encode1High:
  137. if r = rune(encode1[r-encode1Low]); r != 0 {
  138. goto write2or3
  139. }
  140. case encode2Low <= r && r < encode2High:
  141. if r = rune(encode2[r-encode2Low]); r != 0 {
  142. goto write2or3
  143. }
  144. case encode3Low <= r && r < encode3High:
  145. if r = rune(encode3[r-encode3Low]); r != 0 {
  146. goto write2or3
  147. }
  148. case encode4Low <= r && r < encode4High:
  149. if r = rune(encode4[r-encode4Low]); r != 0 {
  150. goto write2or3
  151. }
  152. case encode5Low <= r && r < encode5High:
  153. if 0xff61 <= r && r < 0xffa0 {
  154. goto write2
  155. }
  156. if r = rune(encode5[r-encode5Low]); r != 0 {
  157. goto write2or3
  158. }
  159. }
  160. err = internal.ErrASCIIReplacement
  161. break
  162. }
  163. if nDst >= len(dst) {
  164. err = transform.ErrShortDst
  165. break
  166. }
  167. dst[nDst] = uint8(r)
  168. nDst++
  169. continue
  170. write2or3:
  171. if r>>tableShift == jis0208 {
  172. if nDst+2 > len(dst) {
  173. err = transform.ErrShortDst
  174. break
  175. }
  176. } else {
  177. if nDst+3 > len(dst) {
  178. err = transform.ErrShortDst
  179. break
  180. }
  181. dst[nDst] = 0x8f
  182. nDst++
  183. }
  184. dst[nDst+0] = 0xa1 + uint8(r>>codeShift)&codeMask
  185. dst[nDst+1] = 0xa1 + uint8(r)&codeMask
  186. nDst += 2
  187. continue
  188. write2:
  189. if nDst+2 > len(dst) {
  190. err = transform.ErrShortDst
  191. break
  192. }
  193. dst[nDst+0] = 0x8e
  194. dst[nDst+1] = uint8(r - (0xff61 - 0xa1))
  195. nDst += 2
  196. continue
  197. }
  198. return nDst, nSrc, err
  199. }
  200. func init() {
  201. // Check that the hard-coded encode switch covers all tables.
  202. if numEncodeTables != 6 {
  203. panic("bad numEncodeTables")
  204. }
  205. }