hz.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (C) 1999-2001, 2008 Free Software Foundation, Inc.
  3. * This file is part of the GNU LIBICONV Library.
  4. *
  5. * The GNU LIBICONV Library is free software; you can redistribute it
  6. * and/or modify it under the terms of the GNU Library General Public
  7. * License as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. *
  10. * The GNU LIBICONV Library is distributed in the hope that it will be
  11. * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Library General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Library General Public
  16. * License along with the GNU LIBICONV Library; see the file COPYING.LIB.
  17. * If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. /*
  20. * HZ
  21. */
  22. /* Specification: RFC 1842, RFC 1843 */
  23. /*
  24. * The state is 1 in GB mode, 0 in ASCII mode.
  25. */
  26. static int
  27. hz_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n)
  28. {
  29. state_t state = conv->istate;
  30. unsigned int count = 0;
  31. unsigned char c;
  32. for (;;) {
  33. c = *s;
  34. if (c == '~') {
  35. if (n < count+2)
  36. goto none;
  37. c = s[1];
  38. if (state == 0) {
  39. if (c == '~') {
  40. *pwc = (ucs4_t) '~';
  41. conv->istate = state;
  42. return count+2;
  43. }
  44. if (c == '{') {
  45. state = 1;
  46. s += 2; count += 2;
  47. if (n < count+1)
  48. goto none;
  49. continue;
  50. }
  51. if (c == '\n') {
  52. s += 2; count += 2;
  53. if (n < count+1)
  54. goto none;
  55. continue;
  56. }
  57. } else {
  58. if (c == '}') {
  59. state = 0;
  60. s += 2; count += 2;
  61. if (n < count+1)
  62. goto none;
  63. continue;
  64. }
  65. }
  66. goto ilseq;
  67. }
  68. break;
  69. }
  70. if (state == 0) {
  71. *pwc = (ucs4_t) c;
  72. conv->istate = state;
  73. return count+1;
  74. } else {
  75. int ret;
  76. if (n < count+2)
  77. goto none;
  78. ret = gb2312_mbtowc(conv,pwc,s,2);
  79. if (ret == RET_ILSEQ)
  80. goto ilseq;
  81. if (ret != 2) abort();
  82. conv->istate = state;
  83. return count+2;
  84. }
  85. none:
  86. conv->istate = state;
  87. return RET_TOOFEW(count);
  88. ilseq:
  89. conv->istate = state;
  90. return RET_SHIFT_ILSEQ(count);
  91. }
  92. static int
  93. hz_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, int n)
  94. {
  95. state_t state = conv->ostate;
  96. unsigned char buf[2];
  97. int ret;
  98. /* Code set 0 (ASCII or GB 1988-89) */
  99. ret = ascii_wctomb(conv,buf,wc,1);
  100. if (ret != RET_ILUNI) {
  101. if (ret != 1) abort();
  102. if (buf[0] < 0x80) {
  103. int count = (state ? 3 : 1);
  104. if (n < count)
  105. return RET_TOOSMALL;
  106. if (state) {
  107. r[0] = '~';
  108. r[1] = '}';
  109. r += 2;
  110. state = 0;
  111. }
  112. r[0] = buf[0];
  113. conv->ostate = state;
  114. return count;
  115. }
  116. }
  117. /* Code set 1 (GB 2312-1980) */
  118. ret = gb2312_wctomb(conv,buf,wc,2);
  119. if (ret != RET_ILUNI) {
  120. if (ret != 2) abort();
  121. if (buf[0] < 0x80 && buf[1] < 0x80) {
  122. int count = (state ? 2 : 4);
  123. if (n < count)
  124. return RET_TOOSMALL;
  125. if (!state) {
  126. r[0] = '~';
  127. r[1] = '{';
  128. r += 2;
  129. state = 1;
  130. }
  131. r[0] = buf[0];
  132. r[1] = buf[1];
  133. conv->ostate = state;
  134. return count;
  135. }
  136. }
  137. return RET_ILUNI;
  138. }
  139. static int
  140. hz_reset (conv_t conv, unsigned char *r, int n)
  141. {
  142. state_t state = conv->ostate;
  143. if (state) {
  144. if (n < 2)
  145. return RET_TOOSMALL;
  146. r[0] = '~';
  147. r[1] = '}';
  148. /* conv->ostate = 0; will be done by the caller */
  149. return 2;
  150. } else
  151. return 0;
  152. }