euckr.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 korean
  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. // All is a list of all defined encodings in this package.
  14. var All = []encoding.Encoding{EUCKR}
  15. // EUCKR is the EUC-KR encoding, also known as Code Page 949.
  16. var EUCKR encoding.Encoding = &eucKR
  17. var eucKR = internal.Encoding{
  18. &internal.SimpleEncoding{eucKRDecoder{}, eucKREncoder{}},
  19. "EUC-KR",
  20. identifier.EUCKR,
  21. }
  22. var errInvalidEUCKR = errors.New("korean: invalid EUC-KR encoding")
  23. type eucKRDecoder struct{ transform.NopResetter }
  24. func (eucKRDecoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
  25. r, size := rune(0), 0
  26. loop:
  27. for ; nSrc < len(src); nSrc += size {
  28. switch c0 := src[nSrc]; {
  29. case c0 < utf8.RuneSelf:
  30. r, size = rune(c0), 1
  31. case 0x81 <= c0 && c0 < 0xff:
  32. if nSrc+1 >= len(src) {
  33. err = transform.ErrShortSrc
  34. break loop
  35. }
  36. c1 := src[nSrc+1]
  37. if c0 < 0xc7 {
  38. r = 178 * rune(c0-0x81)
  39. switch {
  40. case 0x41 <= c1 && c1 < 0x5b:
  41. r += rune(c1) - (0x41 - 0*26)
  42. case 0x61 <= c1 && c1 < 0x7b:
  43. r += rune(c1) - (0x61 - 1*26)
  44. case 0x81 <= c1 && c1 < 0xff:
  45. r += rune(c1) - (0x81 - 2*26)
  46. default:
  47. err = errInvalidEUCKR
  48. break loop
  49. }
  50. } else if 0xa1 <= c1 && c1 < 0xff {
  51. r = 178*(0xc7-0x81) + rune(c0-0xc7)*94 + rune(c1-0xa1)
  52. } else {
  53. err = errInvalidEUCKR
  54. break loop
  55. }
  56. if int(r) < len(decode) {
  57. r = rune(decode[r])
  58. if r == 0 {
  59. r = '\ufffd'
  60. }
  61. } else {
  62. r = '\ufffd'
  63. }
  64. size = 2
  65. default:
  66. err = errInvalidEUCKR
  67. break loop
  68. }
  69. if nDst+utf8.RuneLen(r) > len(dst) {
  70. err = transform.ErrShortDst
  71. break loop
  72. }
  73. nDst += utf8.EncodeRune(dst[nDst:], r)
  74. }
  75. if atEOF && err == transform.ErrShortSrc {
  76. err = errInvalidEUCKR
  77. }
  78. return nDst, nSrc, err
  79. }
  80. type eucKREncoder struct{ transform.NopResetter }
  81. func (eucKREncoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
  82. r, size := rune(0), 0
  83. for ; nSrc < len(src); nSrc += size {
  84. r = rune(src[nSrc])
  85. // Decode a 1-byte rune.
  86. if r < utf8.RuneSelf {
  87. size = 1
  88. if nDst >= len(dst) {
  89. err = transform.ErrShortDst
  90. break
  91. }
  92. dst[nDst] = uint8(r)
  93. nDst++
  94. continue
  95. } else {
  96. // Decode a multi-byte rune.
  97. r, size = utf8.DecodeRune(src[nSrc:])
  98. if size == 1 {
  99. // All valid runes of size 1 (those below utf8.RuneSelf) were
  100. // handled above. We have invalid UTF-8 or we haven't seen the
  101. // full character yet.
  102. if !atEOF && !utf8.FullRune(src[nSrc:]) {
  103. err = transform.ErrShortSrc
  104. break
  105. }
  106. }
  107. // func init checks that the switch covers all tables.
  108. switch {
  109. case encode0Low <= r && r < encode0High:
  110. if r = rune(encode0[r-encode0Low]); r != 0 {
  111. goto write2
  112. }
  113. case encode1Low <= r && r < encode1High:
  114. if r = rune(encode1[r-encode1Low]); r != 0 {
  115. goto write2
  116. }
  117. case encode2Low <= r && r < encode2High:
  118. if r = rune(encode2[r-encode2Low]); r != 0 {
  119. goto write2
  120. }
  121. case encode3Low <= r && r < encode3High:
  122. if r = rune(encode3[r-encode3Low]); r != 0 {
  123. goto write2
  124. }
  125. case encode4Low <= r && r < encode4High:
  126. if r = rune(encode4[r-encode4Low]); r != 0 {
  127. goto write2
  128. }
  129. case encode5Low <= r && r < encode5High:
  130. if r = rune(encode5[r-encode5Low]); r != 0 {
  131. goto write2
  132. }
  133. case encode6Low <= r && r < encode6High:
  134. if r = rune(encode6[r-encode6Low]); r != 0 {
  135. goto write2
  136. }
  137. }
  138. err = internal.ErrASCIIReplacement
  139. break
  140. }
  141. write2:
  142. if nDst+2 > len(dst) {
  143. err = transform.ErrShortDst
  144. break
  145. }
  146. dst[nDst+0] = uint8(r >> 8)
  147. dst[nDst+1] = uint8(r)
  148. nDst += 2
  149. continue
  150. }
  151. return nDst, nSrc, err
  152. }
  153. func init() {
  154. // Check that the hard-coded encode switch covers all tables.
  155. if numEncodeTables != 7 {
  156. panic("bad numEncodeTables")
  157. }
  158. }