File: e2u.c

package info (click to toggle)
ruby-uconv 0.6.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 4,716 kB
  • ctags: 197
  • sloc: ansic: 161,247; ruby: 44,420; makefile: 2
file content (135 lines) | stat: -rw-r--r-- 3,313 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * Unicode Conversion Library (EUC-JP to UTF-16)
 * 1997-2004 by yoshidam
 *
 */

#ifdef USE_EUC

#include <string.h>
#include "uconv.h"
#include "e2u.h"
#include "hojo2u.h"
#include "ustring.h"

#define REPLACEMENT_CHAR '?'

static unsigned long
call_unknown_e_conv(UString* u,
                    unknown_euc unknown_e_conv,
                    const unsigned char* e, int len) {
  VALUE ret;
  unsigned char ue[4];
  int i;

  if (unknown_e_conv == NULL)
    return REPLACEMENT_CHAR;

  for (i = 0; i < len && i < sizeof(ue) - 1; i++)
    ue[i] = e[i];
  ue[i] = '\0';
  ret = unknown_e_conv(ue);
  if (TYPE(ret) != T_FIXNUM) {
    UStr_free(u);
    rb_exc_raise(ret);
  }
  return FIX2INT(ret);
}

static void
append_uchar(UString* u, unsigned long ec) {
  if (ec >= 0x10000) {
    unsigned int high = ((ec - 0x10000) >> 10) | 0xd800;
    unsigned int low = (ec & 1023) | 0xdc00;
    UStr_addChar4(u, high & 0xff, high >> 8, low & 0xff, low >> 8);
  }
  else
    UStr_addChar2(u, ec & 0xff, ec >> 8);
}

int
e2u_conv2(const unsigned char* e, UString* u,
          unknown_euc unknown_e_conv,
          unknown_euc e2u_hook) {
  int i;
  int len = strlen((char*)e);

  UStr_alloc(u);

  for (i = 0; i < len; i++) {
    unsigned long ec = 0;
    int clen = 0;

    if (e2u_hook != NULL) {
      VALUE ret;
      unsigned char estr[4];
      if (e[i] == 0x8e && i < len - 1) { /* JIS X 0201 kana */
        estr[0] = e[i]; estr[1] = e[i+1]; estr[2] = '\0';
        clen = 1;
      }
      else if (e[i] == 0x8f && i < len - 2) { /* JIS X 0212 */
        estr[0] = e[i]; estr[1] = e[i+1]; estr[2] = e[i+2]; estr[3] = '\0';
        clen = 2;
      }
      else if (e[i] >= 0xa0 && e[i] != 0xff && i < len - 1) { /* JIX X 0208 */
        estr[0] = e[i]; estr[1] = e[i+1]; estr[2] = '\0';
        clen = 1;
      }
      else { /* ASCII or C1 */
        estr[0] = e[i]; estr[1] = '\0';
      }
      if ((ret = e2u_hook(estr)) != Qnil) {
        if (TYPE(ret) != T_FIXNUM) {
          UStr_free(u);
          rb_exc_raise(ret);
        }
        ec = FIX2INT(ret);

        if (ec == 0)
          ec = call_unknown_e_conv(u, unknown_e_conv, e + i, clen + 1);

        append_uchar(u, ec);
        i += clen;
        continue;
      }
    }

    clen = 0;
    if (e[i] == 0x8e && i < len - 1) { /* JIS X 0201 kana */
      if (e[i + 1] >= 0xa1 && e[i + 1] <= 0xdf)
        ec = 0xff00 | (e[i + 1] - 0x40);
      clen = 1;
    }
    else if (e[i] == 0x8f && i < len - 2) { /* JIS X 0212 */
      int hi = e[i + 1] &  0x7f;
      int low = e[i + 2] &  0x7f;
      int key = (hi - 32) * 96 + (low - 32);
      if (hi >= 32 && low >= 32 &&
          key < sizeof(hojo2u_tbl)/sizeof(unsigned short))
        ec = hojo2u_tbl[key];
      clen = 2;
    }
    else if (e[i] >= 0xa0 && e[i] != 0xff && i < len - 1) { /* JIX X 0208 */
      int hi = e[i] &  0x7f;
      int low = e[i + 1] &  0x7f;
      int key = (hi - 32) * 96 + (low - 32);
      if (hi >= 32 && low >= 32 &&
          key < sizeof(e2u_tbl)/sizeof(unsigned short))
        ec = e2u_tbl[key];
      clen = 1;
    }
    else if (e[i] < 0xa0) { /* ASCII or C1 */
      ec = e[i];
    }

    if (ec == 0)
      ec = call_unknown_e_conv(u, unknown_e_conv, e + i, clen + 1);

    append_uchar(u, ec);
    i += clen;
  }

  return u->len;
}

#endif /* USE_EUC */