natInput_SJIS.cc 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* Copyright (C) 1999 Free Software Foundation
  2. This file is part of libgcj.
  3. This software is copyrighted work licensed under the terms of the
  4. Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
  5. details. */
  6. #include <config.h>
  7. #include <gcj/cni.h>
  8. #include <gnu/gcj/convert/Input_SJIS.h>
  9. #define ERROR_CHAR 0xFFFD
  10. extern unsigned short JIS0208_to_Unicode[84][94];
  11. extern unsigned short JIS0212_to_Unicode[76][94];
  12. jint
  13. gnu::gcj::convert::Input_SJIS::read(jcharArray outbuffer, jint outpos,
  14. jint count)
  15. {
  16. jint start_outpos = outpos;
  17. for (;;)
  18. {
  19. if (outpos - start_outpos >= count)
  20. break;
  21. if (inpos >= inlength)
  22. break;
  23. int b = ((unsigned char*) elements(inbuffer))[inpos++];
  24. if (first_byte == 0)
  25. {
  26. if (b < 128)
  27. {
  28. #if 1
  29. // Technically, we should translate 0x5c to Yen symbol;
  30. // in practice, it is not clear.
  31. if (b == 0x5c)
  32. b = 0x00A5; // Yen sign.
  33. #endif
  34. elements(outbuffer)[outpos++] = (char) b;
  35. }
  36. else if (b >= 0xA1 && b <= 0xDF)
  37. {
  38. b += 0xFF61 - 0xA1;
  39. elements(outbuffer)[outpos++] = b;
  40. }
  41. else
  42. first_byte = b;
  43. }
  44. else
  45. {
  46. // From Lunde: "CJKV Informatio Processing", O'Reilly, 1999, p 420:
  47. bool adjust = b < 159;
  48. int rowOffset = first_byte < 160 ? 112 : 176;
  49. int cellOffset = adjust ? (b > 127 ? 32 : 31) : 126;
  50. first_byte = ((first_byte - rowOffset) << 1) - adjust;
  51. b -= cellOffset;
  52. first_byte -= 33;
  53. b -= 33;
  54. if ((unsigned) first_byte >= 84 || (unsigned) b >= 94)
  55. b = ERROR_CHAR;
  56. else
  57. {
  58. b = JIS0208_to_Unicode[first_byte][b];
  59. if (b == 0)
  60. b = ERROR_CHAR;
  61. }
  62. elements(outbuffer)[outpos++] = b;
  63. first_byte = 0;
  64. }
  65. }
  66. return outpos - start_outpos;
  67. }