shiftjis.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. "errors"
  7. "unicode/utf8"
  8. "golang.org/x/text/encoding"
  9. "golang.org/x/text/encoding/internal"
  10. "golang.org/x/text/encoding/internal/identifier"
  11. "golang.org/x/text/transform"
  12. )
  13. // ShiftJIS is the Shift JIS encoding, also known as Code Page 932 and
  14. // Windows-31J.
  15. var ShiftJIS encoding.Encoding = &shiftJIS
  16. var shiftJIS = internal.Encoding{
  17. &internal.SimpleEncoding{shiftJISDecoder{}, shiftJISEncoder{}},
  18. "Shift JIS",
  19. identifier.ShiftJIS,
  20. }
  21. var errInvalidShiftJIS = errors.New("japanese: invalid Shift JIS encoding")
  22. type shiftJISDecoder struct{ transform.NopResetter }
  23. func (shiftJISDecoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
  24. r, size := rune(0), 0
  25. loop:
  26. for ; nSrc < len(src); nSrc += size {
  27. switch c0 := src[nSrc]; {
  28. case c0 < utf8.RuneSelf:
  29. r, size = rune(c0), 1
  30. case 0xa1 <= c0 && c0 < 0xe0:
  31. r, size = rune(c0)+(0xff61-0xa1), 1
  32. case (0x81 <= c0 && c0 < 0xa0) || (0xe0 <= c0 && c0 < 0xfd):
  33. if c0 <= 0x9f {
  34. c0 -= 0x70
  35. } else {
  36. c0 -= 0xb0
  37. }
  38. c0 = 2*c0 - 0x21
  39. if nSrc+1 >= len(src) {
  40. err = transform.ErrShortSrc
  41. break loop
  42. }
  43. c1 := src[nSrc+1]
  44. switch {
  45. case c1 < 0x40:
  46. err = errInvalidShiftJIS
  47. break loop
  48. case c1 < 0x7f:
  49. c0--
  50. c1 -= 0x40
  51. case c1 == 0x7f:
  52. err = errInvalidShiftJIS
  53. break loop
  54. case c1 < 0x9f:
  55. c0--
  56. c1 -= 0x41
  57. case c1 < 0xfd:
  58. c1 -= 0x9f
  59. default:
  60. err = errInvalidShiftJIS
  61. break loop
  62. }
  63. r, size = '\ufffd', 2
  64. if i := int(c0)*94 + int(c1); i < len(jis0208Decode) {
  65. r = rune(jis0208Decode[i])
  66. if r == 0 {
  67. r = '\ufffd'
  68. }
  69. }
  70. default:
  71. err = errInvalidShiftJIS
  72. break loop
  73. }
  74. if nDst+utf8.RuneLen(r) > len(dst) {
  75. err = transform.ErrShortDst
  76. break loop
  77. }
  78. nDst += utf8.EncodeRune(dst[nDst:], r)
  79. }
  80. if atEOF && err == transform.ErrShortSrc {
  81. err = errInvalidShiftJIS
  82. }
  83. return nDst, nSrc, err
  84. }
  85. type shiftJISEncoder struct{ transform.NopResetter }
  86. func (shiftJISEncoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
  87. r, size := rune(0), 0
  88. loop:
  89. for ; nSrc < len(src); nSrc += size {
  90. r = rune(src[nSrc])
  91. // Decode a 1-byte rune.
  92. if r < utf8.RuneSelf {
  93. size = 1
  94. } else {
  95. // Decode a multi-byte rune.
  96. r, size = utf8.DecodeRune(src[nSrc:])
  97. if size == 1 {
  98. // All valid runes of size 1 (those below utf8.RuneSelf) were
  99. // handled above. We have invalid UTF-8 or we haven't seen the
  100. // full character yet.
  101. if !atEOF && !utf8.FullRune(src[nSrc:]) {
  102. err = transform.ErrShortSrc
  103. break loop
  104. }
  105. }
  106. // func init checks that the switch covers all tables.
  107. switch {
  108. case encode0Low <= r && r < encode0High:
  109. if r = rune(encode0[r-encode0Low]); r>>tableShift == jis0208 {
  110. goto write2
  111. }
  112. case encode1Low <= r && r < encode1High:
  113. if r = rune(encode1[r-encode1Low]); r>>tableShift == jis0208 {
  114. goto write2
  115. }
  116. case encode2Low <= r && r < encode2High:
  117. if r = rune(encode2[r-encode2Low]); r>>tableShift == jis0208 {
  118. goto write2
  119. }
  120. case encode3Low <= r && r < encode3High:
  121. if r = rune(encode3[r-encode3Low]); r>>tableShift == jis0208 {
  122. goto write2
  123. }
  124. case encode4Low <= r && r < encode4High:
  125. if r = rune(encode4[r-encode4Low]); r>>tableShift == jis0208 {
  126. goto write2
  127. }
  128. case encode5Low <= r && r < encode5High:
  129. if 0xff61 <= r && r < 0xffa0 {
  130. r -= 0xff61 - 0xa1
  131. goto write1
  132. }
  133. if r = rune(encode5[r-encode5Low]); r>>tableShift == jis0208 {
  134. goto write2
  135. }
  136. }
  137. err = internal.ErrASCIIReplacement
  138. break
  139. }
  140. write1:
  141. if nDst >= len(dst) {
  142. err = transform.ErrShortDst
  143. break
  144. }
  145. dst[nDst] = uint8(r)
  146. nDst++
  147. continue
  148. write2:
  149. j1 := uint8(r>>codeShift) & codeMask
  150. j2 := uint8(r) & codeMask
  151. if nDst+2 > len(dst) {
  152. err = transform.ErrShortDst
  153. break loop
  154. }
  155. if j1 <= 61 {
  156. dst[nDst+0] = 129 + j1/2
  157. } else {
  158. dst[nDst+0] = 193 + j1/2
  159. }
  160. if j1&1 == 0 {
  161. dst[nDst+1] = j2 + j2/63 + 64
  162. } else {
  163. dst[nDst+1] = j2 + 159
  164. }
  165. nDst += 2
  166. continue
  167. }
  168. return nDst, nSrc, err
  169. }