~ubuntu-branches/ubuntu/maverick/mysql-5.1/maverick-proposed

« back to all changes in this revision

Viewing changes to storage/ibmdb2i/db2i_myconv.cc

  • Committer: Package Import Robot
  • Author(s): Marc Deslauriers
  • Date: 2012-02-22 14:16:05 UTC
  • mto: This revision was merged to the branch mainline in revision 20.
  • Revision ID: package-import@ubuntu.com-20120222141605-nxlu9yzc6attylc2
Tags: upstream-5.1.61
ImportĀ upstreamĀ versionĀ 5.1.61

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
Licensed Materials - Property of IBM
3
 
DB2 Storage Engine Enablement
4
 
Copyright IBM Corporation 2007,2008
5
 
All rights reserved
6
 
 
7
 
Redistribution and use in source and binary forms, with or without modification,
8
 
are permitted provided that the following conditions are met: 
9
 
 (a) Redistributions of source code must retain this list of conditions, the
10
 
     copyright notice in section {d} below, and the disclaimer following this
11
 
     list of conditions. 
12
 
 (b) Redistributions in binary form must reproduce this list of conditions, the
13
 
     copyright notice in section (d) below, and the disclaimer following this
14
 
     list of conditions, in the documentation and/or other materials provided
15
 
     with the distribution. 
16
 
 (c) The name of IBM may not be used to endorse or promote products derived from
17
 
     this software without specific prior written permission. 
18
 
 (d) The text of the required copyright notice is: 
19
 
       Licensed Materials - Property of IBM
20
 
       DB2 Storage Engine Enablement 
21
 
       Copyright IBM Corporation 2007,2008 
22
 
       All rights reserved
23
 
 
24
 
THIS SOFTWARE IS PROVIDED BY IBM CORPORATION "AS IS" AND ANY EXPRESS OR IMPLIED
25
 
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26
 
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
27
 
SHALL IBM CORPORATION BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28
 
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
29
 
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30
 
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31
 
CONTRACT, STRICT LIABILITY, OR TORT INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32
 
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
33
 
OF SUCH DAMAGE.
34
 
*/
35
 
 
36
 
/**
37
 
  @file
38
 
  
39
 
  @brief  A direct map optimization of iconv and related functions
40
 
          This was show to significantly reduce character conversion cost
41
 
          for short strings when compared to calling iconv system code.
42
 
*/
43
 
 
44
 
#include <stdlib.h>
45
 
#include <stdio.h>
46
 
#include <wchar.h>
47
 
#include <errno.h>
48
 
#include <iconv.h>
49
 
#include <ctype.h>
50
 
#include <stdarg.h>
51
 
#include <string.h>
52
 
#include <as400_protos.h>
53
 
 
54
 
#include "db2i_myconv.h"
55
 
#include "db2i_global.h"
56
 
 
57
 
int32_t myconvDebug=0;
58
 
 
59
 
static  char    szGetTimeString[20];
60
 
static  char *  GetTimeString(time_t    now)
61
 
{
62
 
  struct tm *   tm;
63
 
 
64
 
  now = time(&now);
65
 
  tm = (struct tm *) localtime(&now);
66
 
  sprintf(szGetTimeString, "%04d/%02d/%02d %02d:%02d:%02d",
67
 
          tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
68
 
          tm->tm_hour,        tm->tm_min,   tm->tm_sec);
69
 
 
70
 
  return szGetTimeString;
71
 
}
72
 
 
73
 
static MEM_ROOT dmapMemRoot;
74
 
 
75
 
void initMyconv()
76
 
{
77
 
  init_alloc_root(&dmapMemRoot, 0x200, 0);
78
 
}
79
 
 
80
 
void cleanupMyconv()
81
 
{
82
 
  free_root(&dmapMemRoot,0);
83
 
}
84
 
 
85
 
 
86
 
#ifdef DEBUG
87
 
/* type:        */
88
 
#define STDOUT_WITH_TIME        -1      /*  to stdout with time */
89
 
#define STDERR_WITH_TIME        -2      /*  to stderr with time */
90
 
#define STDOUT_WO_TIME          1       /* : to stdout          */
91
 
#define STDERR_WO_TIME          2       /* : to stderr          */
92
 
 
93
 
 
94
 
static void MyPrintf(long       type,
95
 
                     char *     fmt, ...)
96
 
{
97
 
  char          StdoutFN[256];
98
 
  va_list       ap;
99
 
  char *        p;
100
 
  time_t        now;
101
 
  FILE *        fd=stderr;
102
 
 
103
 
  if (type < 0)
104
 
  {
105
 
    now = time(&now);
106
 
    fprintf(fd, "%s ", GetTimeString(now));
107
 
  }
108
 
  va_start(ap, fmt);
109
 
  vfprintf(fd, fmt, ap);
110
 
  va_end(ap);
111
 
}
112
 
#endif
113
 
 
114
 
 
115
 
 
116
 
 
117
 
#define MAX_CONVERTER           128
118
 
 
119
 
mycstoccsid(const char* pname)
120
 
{
121
 
  if (strcmp(pname, "UTF-16")==0)
122
 
    return 1200;
123
 
  else if (strcmp(pname, "big5")==0)
124
 
    return 950;
125
 
  else
126
 
    return cstoccsid(pname);
127
 
}
128
 
#define cstoccsid mycstoccsid
129
 
 
130
 
static  struct __myconv_rec     myconv_rec      [MAX_CONVERTER];
131
 
static  struct __dmap_rec       dmap_rec        [MAX_CONVERTER];
132
 
 
133
 
static int      dmap_open(const char *  to,
134
 
                          const char *  from,
135
 
                          const int32_t idx)
136
 
{
137
 
  if (myconvIsSBCS(from) && myconvIsSBCS(to)) {
138
 
    dmap_rec[idx].codingSchema = DMAP_S2S;
139
 
    if ((dmap_rec[idx].dmapS2S = (uchar *) alloc_root(&dmapMemRoot, 0x100)) == NULL) {
140
 
#ifdef DEBUG
141
 
        MyPrintf(STDERR_WITH_TIME,
142
 
                 "dmap_open(%s,%s,%d), CS=%d failed with malloc(), errno = %d in %s at %d\n",
143
 
                   to, from, idx, DMAP_S2S, errno, __FILE__,__LINE__);
144
 
#endif
145
 
      return -1;
146
 
    }
147
 
    memset(dmap_rec[idx].dmapS2S, 0x00, 0x100);
148
 
    myconv_rec[idx].allocatedSize=0x100;
149
 
 
150
 
    {
151
 
      char      dmapSrc[0x100];
152
 
      iconv_t   cd;
153
 
      int32_t   i;
154
 
      size_t    inBytesLeft=0x100;
155
 
      size_t    outBytesLeft=0x100;
156
 
      size_t    len;
157
 
      char *    inBuf=dmapSrc;
158
 
      char *    outBuf=(char *) dmap_rec[idx].dmapS2S;
159
 
 
160
 
      if ((cd = iconv_open(to, from)) == (iconv_t) -1) {
161
 
#ifdef DEBUG
162
 
        MyPrintf(STDERR_WITH_TIME,
163
 
                 "dmap_open(%s,%s,%d) failed with iconv_open(), errno = %d in %s at %d\n",
164
 
                   to, from, idx, errno, __FILE__,__LINE__);
165
 
#endif
166
 
        return -1;
167
 
      }
168
 
 
169
 
      inBytesLeft = 0x100;
170
 
      for (i = 0; i < inBytesLeft; ++i)
171
 
        dmapSrc[i]=i;
172
 
 
173
 
      do {
174
 
        if ((len = iconv(cd, &inBuf, &inBytesLeft, &outBuf, &outBytesLeft)) != (size_t) 0) {
175
 
#ifdef DEBUG
176
 
          if (myconvDebug) {
177
 
            MyPrintf(STDERR_WITH_TIME,
178
 
                     "dmap_open(%s,%s,%d), CS=%d: iconv() returns %d, errno = %d in %s at %d\n",
179
 
                     to, from, idx, DMAP_S2S, len, errno, __FILE__,__LINE__);
180
 
            MyPrintf(STDERR_WITH_TIME,
181
 
                     "inBytesLeft = %d, inBuf - dmapSrc = %d\n", inBytesLeft, inBuf-dmapSrc);
182
 
            MyPrintf(STDERR_WITH_TIME,
183
 
                     "outBytesLeft = %d, outBuf - dmapS2S = %d\n", outBytesLeft, outBuf-(char *) dmap_rec[idx].dmapS2S);
184
 
          }
185
 
          if ((inBytesLeft == 86 || inBytesLeft == 64 || inBytesLeft == 1) &&
186
 
              memcmp(from, "IBM-1256", 9) == 0 &&
187
 
              memcmp(to, "IBM-420", 8) == 0) {
188
 
            /* Known problem for IBM-1256_IBM-420 */
189
 
            --inBytesLeft;
190
 
            ++inBuf;
191
 
            *outBuf=0x00;
192
 
            ++outBuf;
193
 
            --outBytesLeft;
194
 
            continue;
195
 
          } else if ((inBytesLeft == 173 || inBytesLeft == 172 ||
196
 
                      inBytesLeft == 74  || inBytesLeft == 73  ||
197
 
                      inBytesLeft == 52  || inBytesLeft == 50  ||
198
 
                      inBytesLeft == 31  || inBytesLeft == 20  ||
199
 
                      inBytesLeft == 6) &&
200
 
                     memcmp(to, "IBM-1256", 9) == 0 &&
201
 
                     memcmp(from, "IBM-420", 8) == 0) {
202
 
            /* Known problem for IBM-420_IBM-1256 */
203
 
            --inBytesLeft;
204
 
            ++inBuf;
205
 
            *outBuf=0x00;
206
 
            ++outBuf;
207
 
            --outBytesLeft;
208
 
            continue;
209
 
          } else if ((128 >= inBytesLeft) &&
210
 
                     memcmp(to, "IBM-037", 8) == 0 &&
211
 
                     memcmp(from, "IBM-367", 8) == 0) {
212
 
            /* Known problem for IBM-367_IBM-037 */
213
 
            --inBytesLeft;
214
 
            ++inBuf;
215
 
            *outBuf=0x00;
216
 
            ++outBuf;
217
 
            --outBytesLeft;
218
 
            continue;
219
 
          } else if (((1 <= inBytesLeft && inBytesLeft <= 4) || (97 <= inBytesLeft && inBytesLeft <= 128)) &&
220
 
                     memcmp(to, "IBM-838", 8) == 0 &&
221
 
                     memcmp(from, "TIS-620", 8) == 0) {
222
 
            /* Known problem for TIS-620_IBM-838 */
223
 
            --inBytesLeft;
224
 
            ++inBuf;
225
 
            *outBuf=0x00;
226
 
            ++outBuf;
227
 
            --outBytesLeft;
228
 
            continue;
229
 
          }
230
 
          iconv_close(cd);
231
 
          return -1;
232
 
#else
233
 
          /* Tolerant to undefined conversions for any converter */
234
 
          --inBytesLeft;
235
 
          ++inBuf;
236
 
          *outBuf=0x00;
237
 
          ++outBuf;
238
 
          --outBytesLeft;
239
 
          continue;
240
 
#endif
241
 
        }
242
 
      } while (inBytesLeft > 0);
243
 
 
244
 
      if (myconvIsISO(to))
245
 
        myconv_rec[idx].subS=0x1A;
246
 
      else if (myconvIsASCII(to))
247
 
        myconv_rec[idx].subS=0x7F;
248
 
      else if (myconvIsEBCDIC(to))
249
 
        myconv_rec[idx].subS=0x3F;
250
 
 
251
 
      if (myconvIsISO(from))
252
 
        myconv_rec[idx].srcSubS=0x1A;
253
 
      else if (myconvIsASCII(from))
254
 
        myconv_rec[idx].srcSubS=0x7F;
255
 
      else if (myconvIsEBCDIC(from))
256
 
        myconv_rec[idx].srcSubS=0x3F;
257
 
 
258
 
      iconv_close(cd);
259
 
    }
260
 
  } else if (((myconvIsSBCS(from) && myconvIsUnicode2(to)) && (dmap_rec[idx].codingSchema = DMAP_S2U)) ||
261
 
             ((myconvIsSBCS(from) && myconvIsUTF8(to)) && (dmap_rec[idx].codingSchema = DMAP_S28))) {
262
 
    int i;
263
 
    
264
 
    /* single byte mapping */
265
 
    if ((dmap_rec[idx].dmapD12U = (UniChar *) alloc_root(&dmapMemRoot, 0x100 * 2)) == NULL) {
266
 
#ifdef DEBUG
267
 
      MyPrintf(STDERR_WITH_TIME,
268
 
               "dmap_open(%s,%s,%d), CS=%d failed with malloc(), errno = %d in %s at %d\n",
269
 
               to, from, idx, DMAP_S2U, errno, __FILE__,__LINE__);
270
 
#endif
271
 
      return -1;
272
 
    }
273
 
    memset(dmap_rec[idx].dmapD12U, 0x00, 0x100 * 2);
274
 
    myconv_rec[idx].allocatedSize=0x100 * 2;
275
 
 
276
 
    
277
 
    {
278
 
      char      dmapSrc[2];
279
 
      iconv_t   cd;
280
 
      int32_t   i;
281
 
      size_t    inBytesLeft;
282
 
      size_t    outBytesLeft;
283
 
      size_t    len;
284
 
      char *    inBuf;
285
 
      char *    outBuf;
286
 
      char      SS=0x1A;
287
 
#ifdef support_surrogate
288
 
      if ((cd = iconv_open("UTF-16", from)) == (iconv_t) -1) {
289
 
#else
290
 
      if ((cd = iconv_open("UCS-2", from)) == (iconv_t) -1) {
291
 
#endif
292
 
#ifdef DEBUG
293
 
        MyPrintf(STDERR_WITH_TIME,
294
 
                 "dmap_open(%s,%s,%d) failed with iconv_open(), errno = %d in %s at %d\n",
295
 
                 to, from, idx, errno, __FILE__,__LINE__);
296
 
#endif
297
 
        return -1;
298
 
      }
299
 
 
300
 
      for (i = 0; i < 0x100; ++i) {
301
 
        dmapSrc[0]=i;
302
 
        inBuf=dmapSrc;
303
 
        inBytesLeft=1;
304
 
        outBuf=(char *) &(dmap_rec[idx].dmapD12U[i]);
305
 
        outBytesLeft=2;
306
 
        if ((len = iconv(cd, &inBuf, &inBytesLeft, &outBuf, &outBytesLeft)) != (size_t) 0) {
307
 
          if ((errno == EILSEQ || errno == EINVAL) &&
308
 
              inBytesLeft == 1 &&
309
 
              outBytesLeft == 2) {
310
 
            continue;
311
 
          } else {
312
 
#ifdef DEBUG
313
 
            if (myconvDebug) {
314
 
              MyPrintf(STDERR_WITH_TIME,
315
 
                       "dmap_open(%s,%s,%d) failed to initialize with iconv(cd,%02x,%d,%02x%02x,%d), errno = %d in %s at %d\n",
316
 
                       to, from, idx, dmapSrc[0], 1,
317
 
                       (&dmap_rec[idx].dmapD12U[i])[0],(&dmap_rec[idx].dmapD12U[i])[1], 2,
318
 
                       errno, __FILE__,__LINE__);
319
 
              MyPrintf(STDERR_WITH_TIME,
320
 
                       "inBytesLeft=%d, outBytesLeft=%d, %02x%02x\n",
321
 
                       inBytesLeft, outBytesLeft,
322
 
                       (&dmap_rec[idx].dmapD12U[i])[0],(&dmap_rec[idx].dmapD12U[i])[1]);
323
 
            }
324
 
#endif
325
 
            iconv_close(cd);
326
 
            return -1;
327
 
          }            
328
 
          dmap_rec[idx].dmapD12U[i]=0x0000;
329
 
        }
330
 
        if (dmap_rec[idx].dmapE02U[i] == 0x001A &&      /* pick the first one */
331
 
            myconv_rec[idx].srcSubS == 0x00) {
332
 
          myconv_rec[idx].srcSubS=i;
333
 
        }
334
 
      }
335
 
      iconv_close(cd);
336
 
    }
337
 
    myconv_rec[idx].subS=0x1A;
338
 
    myconv_rec[idx].subD=0xFFFD;
339
 
    
340
 
 
341
 
  } else if (((myconvIsUCS2(from)  && myconvIsSBCS(to)) && (dmap_rec[idx].codingSchema = DMAP_U2S)) ||
342
 
             ((myconvIsUTF16(from) && myconvIsSBCS(to)) && (dmap_rec[idx].codingSchema = DMAP_T2S)) ||
343
 
             ((myconvIsUTF8(from)  && myconvIsSBCS(to)) && (dmap_rec[idx].codingSchema = DMAP_82S))) {
344
 
    /* UTF-16 -> SBCS, the direct map a bit of waste of space,
345
 
     * binary search may be reasonable alternative
346
 
     */
347
 
    if ((dmap_rec[idx].dmapU2S = (uchar *) alloc_root(&dmapMemRoot, 0x10000 * 2)) == NULL) {
348
 
#ifdef DEBUG
349
 
        MyPrintf(STDERR_WITH_TIME,
350
 
                 "dmap_open(%s,%s,%d), CS=%d failed with malloc(), errno = %d in %s at %d\n",
351
 
                 to, from, idx, DMAP_U2S, errno, __FILE__,__LINE__);
352
 
#endif
353
 
      return -1;
354
 
    }
355
 
    memset(dmap_rec[idx].dmapU2S, 0x00, 0x10000);
356
 
    myconv_rec[idx].allocatedSize=(0x10000 * 2);
357
 
 
358
 
    {
359
 
      iconv_t   cd;
360
 
      int32_t   i;
361
 
 
362
 
#ifdef support_surrogate
363
 
      if ((cd = iconv_open(to, "UTF-16")) == (iconv_t) -1) {
364
 
#else
365
 
      if ((cd = iconv_open(to, "UCS-2")) == (iconv_t) -1) {
366
 
#endif
367
 
#ifdef DEBUG
368
 
        MyPrintf(STDERR_WITH_TIME,
369
 
                 "dmap_open(%s,%s,%d) failed with iconv_open(), errno = %d in %s at %d\n",
370
 
                 to, from, idx, errno, __FILE__,__LINE__);
371
 
#endif
372
 
        return -1;
373
 
      }
374
 
 
375
 
      for (i = 0; i < 0x100; ++i) {
376
 
        UniChar         dmapSrc[0x100];
377
 
        int32_t         j;
378
 
        for (j = 0; j < 0x100; ++j) {
379
 
          dmapSrc[j]=i * 0x100 + j;
380
 
        }
381
 
        char *  inBuf=(char *) dmapSrc;
382
 
        char *  outBuf=(char *) &(dmap_rec[idx].dmapU2S[i*0x100]);
383
 
        size_t  inBytesLeft=sizeof(dmapSrc);
384
 
        size_t  outBytesLeft=0x100;
385
 
        size_t  len;
386
 
 
387
 
        if ((len = iconv(cd, &inBuf, &inBytesLeft, &outBuf, &outBytesLeft)) != (size_t) 0) {
388
 
          if (inBytesLeft == 0 && outBytesLeft == 0) {    /* a number of substitution returns */
389
 
            continue;
390
 
          }
391
 
#ifdef DEBUG
392
 
          if (myconvDebug) {
393
 
            MyPrintf(STDERR_WITH_TIME,
394
 
                     "dmap_open(%s,%s,%d) failed to initialize with iconv(), errno = %d in %s at %d\n",
395
 
                     from, to, idx, errno, __FILE__,__LINE__);
396
 
            MyPrintf(STDERR_WITH_TIME,
397
 
                     "iconv() retuns %d, errno=%d, InBytesLeft=%d, OutBytesLeft=%d\n",
398
 
                     len, errno, inBytesLeft, outBytesLeft, __FILE__,__LINE__);
399
 
          }
400
 
#endif
401
 
          iconv_close(cd);
402
 
          return -1;
403
 
        }
404
 
      }
405
 
      iconv_close(cd);
406
 
 
407
 
      myconv_rec[idx].subS = dmap_rec[idx].dmapU2S[0x1A];
408
 
      myconv_rec[idx].subD = dmap_rec[idx].dmapU2S[0xFFFD];
409
 
      myconv_rec[idx].srcSubS = 0x1A;
410
 
      myconv_rec[idx].srcSubD = 0xFFFD;
411
 
    }
412
 
 
413
 
 
414
 
 
415
 
  } else if (((myconvIsDBCS(from) && myconvIsUnicode2(to)) && (dmap_rec[idx].codingSchema = DMAP_D2U)) ||
416
 
             ((myconvIsDBCS(from) && myconvIsUTF8(to)) && (dmap_rec[idx].codingSchema = DMAP_D28))) {
417
 
    int i;
418
 
    /* single byte mapping */
419
 
    if ((dmap_rec[idx].dmapD12U = (UniChar *) alloc_root(&dmapMemRoot, 0x100 * 2)) == NULL) {
420
 
#ifdef DEBUG
421
 
        MyPrintf(STDERR_WITH_TIME,
422
 
                 "dmap_open(%s,%s,%d), CS=%d failed with malloc(), errno = %d in %s at %d\n",
423
 
                 to, from, idx, DMAP_D2U, errno, __FILE__,__LINE__);
424
 
#endif
425
 
      return -1;
426
 
    }
427
 
    memset(dmap_rec[idx].dmapD12U, 0x00, 0x100 * 2);
428
 
 
429
 
    /* double byte mapping, assume 7 bit ASCII is not use as the first byte of DBCS. */
430
 
    if ((dmap_rec[idx].dmapD22U = (UniChar *) alloc_root(&dmapMemRoot, 0x8000 * 2)) == NULL) {
431
 
#ifdef DEBUG
432
 
        MyPrintf(STDERR_WITH_TIME,
433
 
                 "dmap_open(%s,%s,%d), CS=%d failed with malloc(), errno = %d in %s at %d\n",
434
 
                 to, from, idx, DMAP_D2U, errno, __FILE__,__LINE__);
435
 
#endif
436
 
      return -1;
437
 
    }
438
 
    memset(dmap_rec[idx].dmapD22U, 0x00, 0x8000 * 2);
439
 
 
440
 
    myconv_rec[idx].allocatedSize=(0x100 + 0x8000) * 2;
441
 
 
442
 
    
443
 
    {
444
 
      char      dmapSrc[2];
445
 
      iconv_t   cd;
446
 
      int32_t   i;
447
 
      size_t    inBytesLeft;
448
 
      size_t    outBytesLeft;
449
 
      size_t    len;
450
 
      char *    inBuf;
451
 
      char *    outBuf;
452
 
      char      SS=0x1A;
453
 
 
454
 
#ifdef support_surrogate
455
 
      if ((cd = iconv_open("UTF-16", from)) == (iconv_t) -1) {
456
 
#else
457
 
      if ((cd = iconv_open("UCS-2", from)) == (iconv_t) -1) {
458
 
#endif
459
 
#ifdef DEBUG
460
 
        MyPrintf(STDERR_WITH_TIME,
461
 
                 "dmap_open(%s,%s,%d) failed with iconv_open(), errno = %d in %s at %d\n",
462
 
                   to, from, idx, errno, __FILE__,__LINE__);
463
 
#endif
464
 
        return -1;
465
 
      }
466
 
 
467
 
      for (i = 0; i < 0x100; ++i) {
468
 
        dmapSrc[0]=i;
469
 
        inBuf=dmapSrc;
470
 
        inBytesLeft=1;
471
 
        outBuf=(char *) (&dmap_rec[idx].dmapD12U[i]);
472
 
        outBytesLeft=2;
473
 
        if ((len = iconv(cd, &inBuf, &inBytesLeft, &outBuf, &outBytesLeft)) != (size_t) 0) {
474
 
          if ((errno == EILSEQ || errno == EINVAL) &&
475
 
              inBytesLeft == 1 &&
476
 
              outBytesLeft == 2) {
477
 
            continue;
478
 
          } else {
479
 
#ifdef DEBUG
480
 
            if (myconvDebug) {
481
 
              MyPrintf(STDERR_WITH_TIME,
482
 
                       "dmap_open(%s,%s,%d) failed to initialize with iconv(cd,%02x,%d,%02x%02x,%d), errno = %d in %s at %d\n",
483
 
                       to, from, idx, dmapSrc[0], 1,
484
 
                       (&dmap_rec[idx].dmapD12U[i])[0],(&dmap_rec[idx].dmapD12U[i])[1], 2,
485
 
                       errno, __FILE__,__LINE__);
486
 
              MyPrintf(STDERR_WITH_TIME,
487
 
                       "inBytesLeft=%d, outBytesLeft=%d, %02x%02x\n",
488
 
                       inBytesLeft, outBytesLeft,
489
 
                       (&dmap_rec[idx].dmapD12U[i])[0],(&dmap_rec[idx].dmapD12U[i])[1]);
490
 
            }
491
 
#endif
492
 
            iconv_close(cd);
493
 
            return -1;
494
 
          }            
495
 
          dmap_rec[idx].dmapD12U[i]=0x0000;
496
 
        }
497
 
        if (dmap_rec[idx].dmapD12U[i] == 0x001A &&      /* pick the first one */
498
 
            myconv_rec[idx].srcSubS == 0x00) {
499
 
          myconv_rec[idx].srcSubS=i;
500
 
        }
501
 
      }
502
 
 
503
 
      
504
 
      for (i = 0x80; i < 0x100; ++i) {
505
 
        int j;
506
 
        if (dmap_rec[idx].dmapD12U[i] != 0x0000)
507
 
          continue;
508
 
        for (j = 0x01; j < 0x100; ++j) {
509
 
          dmapSrc[0]=i;
510
 
          dmapSrc[1]=j;
511
 
          int offset = i-0x80;
512
 
          offset<<=8;
513
 
          offset+=j;
514
 
 
515
 
          inBuf=dmapSrc;
516
 
          inBytesLeft=2;
517
 
          outBuf=(char *) &(dmap_rec[idx].dmapD22U[offset]);
518
 
          outBytesLeft=2;
519
 
          if ((len = iconv(cd, &inBuf, &inBytesLeft, &outBuf, &outBytesLeft)) != (size_t) 0) {
520
 
            if (inBytesLeft == 2 && outBytesLeft == 2 && (errno == EILSEQ || errno == EINVAL)) {
521
 
              ;  /* invalid DBCS character, dmapDD2U[offset] remains 0x0000 */
522
 
            } else {
523
 
#ifdef DEBUG
524
 
              if (myconvDebug) {
525
 
                MyPrintf(STDERR_WITH_TIME,
526
 
                         "dmap_open(%s,%s,%d) failed to initialize with iconv(cd,%p,2,%p,2), errno = %d in %s at %d\n",
527
 
                         to, from, idx,
528
 
                         dmapSrc, &(dmap_rec[idx].dmapD22U[offset]),
529
 
                         errno, __FILE__,__LINE__);
530
 
                MyPrintf(STDERR_WO_TIME, 
531
 
                         "iconv(cd,0x%02x%02x,2,0x%04x,2) returns %d, inBytesLeft=%d, outBytesLeft=%d\n",
532
 
                         dmapSrc[0], dmapSrc[1],
533
 
                         dmap_rec[idx].dmapD22U[offset],
534
 
                         len, inBytesLeft, outBytesLeft);
535
 
              }
536
 
#endif
537
 
              iconv_close(cd);
538
 
              return -1;
539
 
            }
540
 
          } else {
541
 
#ifdef TRACE_DMAP
542
 
            if (myconvDebug) {
543
 
              MyPrintf(STDERR_WITH_TIME,
544
 
                       "dmap_open(%s,%s,%d) failed to initialize with iconv(), rc=%d, errno=%d in %s at %d\n",
545
 
                       to, from, idx, len, errno, __FILE__,__LINE__);
546
 
              MyPrintf(STDERR_WITH_TIME,
547
 
                       "%04X: src=%04X%04X, inBuf=0x%02X%02X, inBytesLeft=%d, outBuf=%02X%02X%02X, outBytesLeft=%d\n",
548
 
                       i, dmapSrc[0], dmapSrc[1], inBuf[0], inBuf[1],
549
 
                       inBytesLeft, outBuf[-2], outBuf[-1], outBuf[0], outBytesLeft);
550
 
              MyPrintf(STDERR_WITH_TIME,
551
 
                       "&dmapSrc=%p, inBuf=%p, %p, outBuf=%p\n",
552
 
                       dmapSrc, inBuf, dmap_rec[idx].dmapU2M3 + (i - 0x80) * 2, outBuf);
553
 
            }
554
 
#endif
555
 
          }
556
 
        }
557
 
        if (dmap_rec[idx].dmapD12U[i] == 0xFFFD) {      /* pick the last one */
558
 
          myconv_rec[idx].srcSubD=i* 0x100 + j;
559
 
        }
560
 
      }
561
 
      iconv_close(cd);
562
 
    }
563
 
 
564
 
    myconv_rec[idx].subS=0x1A;
565
 
    myconv_rec[idx].subD=0xFFFD;
566
 
    myconv_rec[idx].srcSubD=0xFCFC;
567
 
    
568
 
 
569
 
  } else if (((myconvIsUCS2(from)  && myconvIsDBCS(to)) && (dmap_rec[idx].codingSchema = DMAP_U2D)) ||
570
 
             ((myconvIsUTF16(from) && myconvIsDBCS(to)) && (dmap_rec[idx].codingSchema = DMAP_T2D)) ||
571
 
             ((myconvIsUTF8(from)  && myconvIsDBCS(to)) && (dmap_rec[idx].codingSchema = DMAP_82D))) {
572
 
    /* UTF-16 -> DBCS single/double byte */
573
 
    /* A single table will cover all characters, assuming no second byte is 0x00. */
574
 
    if ((dmap_rec[idx].dmapU2D = (uchar *) alloc_root(&dmapMemRoot, 0x10000 * 2)) == NULL) {
575
 
#ifdef DEBUG
576
 
        MyPrintf(STDERR_WITH_TIME,
577
 
                 "dmap_open(%s,%s,%d), CS=%d failed with malloc(), errno = %d in %s at %d\n",
578
 
                 to, from, idx, DMAP_U2D, errno, __FILE__,__LINE__);
579
 
#endif
580
 
      return -1;
581
 
    }
582
 
 
583
 
    memset(dmap_rec[idx].dmapU2D, 0x00, 0x10000 * 2);
584
 
    myconv_rec[idx].allocatedSize=(0x10000 * 2);
585
 
 
586
 
    {
587
 
      UniChar   dmapSrc[1];
588
 
      iconv_t   cd;
589
 
      int32_t   i;
590
 
      size_t    inBytesLeft;
591
 
      size_t    outBytesLeft;
592
 
      size_t    len;
593
 
      char *    inBuf;
594
 
      char *    outBuf;
595
 
 
596
 
#ifdef support_surrogate
597
 
      if ((cd = iconv_open(to, "UTF-16")) == (iconv_t) -1) {
598
 
#else
599
 
      if ((cd = iconv_open(to, "UCS-2")) == (iconv_t) -1) {
600
 
#endif
601
 
#ifdef DEBUG
602
 
        MyPrintf(STDERR_WITH_TIME,
603
 
                 "dmap_open(%s,%s,%d) failed with iconv_open(), errno = %d in %s at %d\n",
604
 
                 to, from, idx, errno, __FILE__,__LINE__);
605
 
#endif
606
 
        return -1;
607
 
      }
608
 
 
609
 
      /* easy implementation, convert 1 Unicode character at one time. */
610
 
      /* If the open performance is an issue, convert a chunk such as 128 chracters.  */
611
 
      /* if the converted length is not the same as the original, convert one by one. */
612
 
      (dmap_rec[idx].dmapU2D)[0x0000]=0x00;
613
 
      for (i = 1; i < 0x10000; ++i) {
614
 
        dmapSrc[0]=i;
615
 
        inBuf=(char *) dmapSrc;
616
 
        inBytesLeft=2;
617
 
        outBuf=(char *) &((dmap_rec[idx].dmapU2D)[2*i]);
618
 
        outBytesLeft=2;
619
 
        do {
620
 
          if ((len = iconv(cd, &inBuf, &inBytesLeft, &outBuf, &outBytesLeft)) != (size_t) 0) {
621
 
            if (len == 1 && inBytesLeft == 0 && outBytesLeft == 1 && (dmap_rec[idx].dmapU2D)[2*i] == 0x1A) {
622
 
              /* UCS-2_TIS-620:0x0080 => 0x1A, converted to SBCS replacement character */
623
 
              (dmap_rec[idx].dmapU2D)[2*i+1]=0x00;
624
 
              break;
625
 
            } else if (len == 1 && inBytesLeft == 0 && outBytesLeft == 0) {
626
 
              break;
627
 
            }
628
 
            if (errno == EILSEQ || errno == EINVAL) {
629
 
#ifdef DEBUG
630
 
              if (myconvDebug) {
631
 
                MyPrintf(STDERR_WITH_TIME,
632
 
                         "dmap_open(%s,%s,%d) failed to initialize with iconv(), errno = %d in %s at %d\n",
633
 
                         to, from, idx, errno, __FILE__,__LINE__);
634
 
                MyPrintf(STDERR_WO_TIME,
635
 
                         "iconv(cd,%04x,2,%02x%02x,2) returns inBytesLeft=%d, outBytesLeft=%d\n",
636
 
                         dmapSrc[0],
637
 
                         (dmap_rec[idx].dmapU2D)[2*i], (dmap_rec[idx].dmapU2D)[2*i+1],
638
 
                         inBytesLeft, outBytesLeft);
639
 
                if (outBuf - (char *) dmap_rec[idx].dmapU2M2 > 1)
640
 
                  MyPrintf(STDERR_WO_TIME, "outBuf[-2..2]=%02X%02X%02X%02X%02X\n", outBuf[-2],outBuf[-1],outBuf[0],outBuf[1],outBuf[2]);
641
 
                else
642
 
                  MyPrintf(STDERR_WO_TIME, "outBuf[0..2]=%02X%02X%02X\n", outBuf[0],outBuf[1],outBuf[2]);
643
 
              }
644
 
#endif
645
 
              inBuf+=2;
646
 
              inBytesLeft-=2;
647
 
              memcpy(outBuf, (char *) &(myconv_rec[idx].subD), 2);
648
 
              outBuf+=2;
649
 
              outBytesLeft-=2;
650
 
            } else {
651
 
#ifdef DEBUG
652
 
              MyPrintf(STDERR_WITH_TIME,
653
 
                       "[%d] dmap_open(%s,%s,%d) failed to initialize with iconv(), errno = %d in %s at %d\n",
654
 
                       i, to, from, idx, errno, __FILE__,__LINE__);
655
 
              MyPrintf(STDERR_WO_TIME, 
656
 
                       "iconv(cd,%04x,2,%02x%02x,2) returns %d inBytesLeft=%d, outBytesLeft=%d\n",
657
 
                       dmapSrc[0],
658
 
                       (dmap_rec[idx].dmapU2D)[2*i],
659
 
                       (dmap_rec[idx].dmapU2D)[2*i+1],
660
 
                       len, inBytesLeft,outBytesLeft);
661
 
              if (i == 1) {
662
 
                MyPrintf(STDERR_WO_TIME, 
663
 
                         " inBuf [-1..2]=%02x%02x%02x%02x\n",
664
 
                         inBuf[-1],inBuf[0],inBuf[1],inBuf[2]);
665
 
                MyPrintf(STDERR_WO_TIME,
666
 
                         " outBuf [-1..2]=%02x%02x%02x%02x\n",
667
 
                         outBuf[-1],outBuf[0],outBuf[1],outBuf[2]);
668
 
              } else {
669
 
                MyPrintf(STDERR_WO_TIME, 
670
 
                         " inBuf [-2..2]=%02x%02x%02x%02x%02x\n",
671
 
                         inBuf[-2],inBuf[-1],inBuf[0],inBuf[1],inBuf[2]);
672
 
                MyPrintf(STDERR_WO_TIME,
673
 
                         " outBuf [-2..2]=%02x%02x%02x%02x%02x\n",
674
 
                         outBuf[-2],outBuf[-1],outBuf[0],outBuf[1],outBuf[2]);
675
 
              }
676
 
#endif
677
 
              iconv_close(cd);
678
 
              return -1;
679
 
            }
680
 
            if (len == 0 && inBytesLeft == 0 && outBytesLeft == 1) { /* converted to SBCS */
681
 
              (dmap_rec[idx].dmapU2D)[2*i+1]=0x00;
682
 
              break;
683
 
            }
684
 
          }
685
 
        } while (inBytesLeft > 0);
686
 
      }
687
 
      iconv_close(cd);
688
 
      myconv_rec[idx].subS = dmap_rec[idx].dmapU2D[2*0x1A];
689
 
      myconv_rec[idx].subD = dmap_rec[idx].dmapU2D[2*0xFFFD] * 0x100 
690
 
        + dmap_rec[idx].dmapU2D[2*0xFFFD+1];
691
 
      myconv_rec[idx].srcSubS = 0x1A;
692
 
      myconv_rec[idx].srcSubD = 0xFFFD;
693
 
    }
694
 
 
695
 
 
696
 
  } else if (((myconvIsEUC(from) && myconvIsUnicode2(to)) && (dmap_rec[idx].codingSchema = DMAP_E2U)) ||
697
 
             ((myconvIsEUC(from) && myconvIsUTF8(to)) && (dmap_rec[idx].codingSchema = DMAP_E28))) {
698
 
    int i;
699
 
    /* S0: 0x00 - 0x7F */
700
 
    if ((dmap_rec[idx].dmapE02U = (UniChar *) alloc_root(&dmapMemRoot, 0x100 * 2)) == NULL) {
701
 
#ifdef DEBUG
702
 
        MyPrintf(STDERR_WITH_TIME,
703
 
                 "dmap_open(%s,%s,%d), CS=%d failed with malloc(), errno = %d in %s at %d\n",
704
 
                 to, from, idx, DMAP_E2U, errno, __FILE__,__LINE__);
705
 
#endif
706
 
      return -1;
707
 
    }
708
 
    memset(dmap_rec[idx].dmapE02U, 0x00, 0x100 * 2);
709
 
 
710
 
    /* S1: 0xA0 - 0xFF, 0xA0 - 0xFF */
711
 
    if ((dmap_rec[idx].dmapE12U = (UniChar *) alloc_root(&dmapMemRoot, 0x60 * 0x60 * 2)) == NULL) {
712
 
#ifdef DEBUG
713
 
        MyPrintf(STDERR_WITH_TIME,
714
 
                 "dmap_open(%s,%s,%d), CS=%d failed with malloc(), errno = %d in %s at %d\n",
715
 
                 to, from, idx, DMAP_E2U, errno, __FILE__,__LINE__);
716
 
#endif
717
 
      return -1;
718
 
    }
719
 
    memset(dmap_rec[idx].dmapE12U, 0x00, 0x60 * 0x60 * 2);
720
 
 
721
 
    /* SS2: 0x8E + 0xA0 - 0xFF, 0xA0 - 0xFF */
722
 
    if ((dmap_rec[idx].dmapE22U = (UniChar *) alloc_root(&dmapMemRoot, 0x60 * 0x61 * 2)) == NULL) {
723
 
#ifdef DEBUG
724
 
        MyPrintf(STDERR_WITH_TIME,
725
 
                 "dmap_open(%s,%s,%d), CS=%d failed with malloc(), errno = %d in %s at %d\n",
726
 
                 to, from, idx, DMAP_E2U, errno, __FILE__,__LINE__);
727
 
#endif
728
 
      return -1;
729
 
    }
730
 
    memset(dmap_rec[idx].dmapE22U, 0x00, 0x60 * 0x61 * 2);
731
 
 
732
 
    /* SS3: 0x8F + 0xA0 - 0xFF, 0xA0 - 0xFF */
733
 
    if ((dmap_rec[idx].dmapE32U = (UniChar *) alloc_root(&dmapMemRoot, 0x60 * 0x61 * 2)) == NULL) {
734
 
#ifdef DEBUG
735
 
        MyPrintf(STDERR_WITH_TIME,
736
 
                 "dmap_open(%s,%s,%d), CS=%d failed with malloc(), errno = %d in %s at %d\n",
737
 
                 to, from, idx, DMAP_E2U, errno, __FILE__,__LINE__);
738
 
#endif
739
 
      return -1;
740
 
    }
741
 
    memset(dmap_rec[idx].dmapE32U, 0x00, 0x60 * 0x61 * 2);
742
 
 
743
 
    myconv_rec[idx].allocatedSize=(0x100 + 0x60 * 0x60  + 0x60 * 0x61* 2) * 2;
744
 
 
745
 
    
746
 
    {
747
 
      char      dmapSrc[0x60 * 0x60 * 3];
748
 
      iconv_t   cd;
749
 
      int32_t   i;
750
 
      size_t    inBytesLeft;
751
 
      size_t    outBytesLeft;
752
 
      size_t    len;
753
 
      char *    inBuf;
754
 
      char *    outBuf;
755
 
      char      SS=0x8E;
756
 
 
757
 
#ifdef support_surrogate
758
 
      if ((cd = iconv_open("UTF-16", from)) == (iconv_t) -1) {
759
 
#else
760
 
      if ((cd = iconv_open("UCS-2", from)) == (iconv_t) -1) {
761
 
#endif
762
 
#ifdef DEBUG
763
 
        MyPrintf(STDERR_WITH_TIME,
764
 
                 "dmap_open(%s,%s,%d) failed with iconv_open(), errno = %d in %s at %d\n",
765
 
                   to, from, idx, errno, __FILE__,__LINE__);
766
 
#endif
767
 
        return -1;
768
 
      }
769
 
 
770
 
      for (i = 0; i < 0x100; ++i) {
771
 
        dmapSrc[0]=i;
772
 
        inBuf=dmapSrc;
773
 
        inBytesLeft=1;
774
 
        outBuf=(char *) (&dmap_rec[idx].dmapE02U[i]);
775
 
        outBytesLeft=2;
776
 
        if ((len = iconv(cd, &inBuf, &inBytesLeft, &outBuf, &outBytesLeft)) != (size_t) 0) {
777
 
#ifdef DEBUG
778
 
          if (myconvDebug) {
779
 
            MyPrintf(STDERR_WITH_TIME,
780
 
                     "dmap_open(%s,%s,%d) failed to initialize with iconv(), errno = %d in %s at %d\n",
781
 
                     to, from, idx, errno, __FILE__,__LINE__);
782
 
          }
783
 
#endif
784
 
          dmap_rec[idx].dmapE02U[i]=0x0000;
785
 
        }
786
 
        if (dmap_rec[idx].dmapE02U[i] == 0x001A &&      /* pick the first one */
787
 
            myconv_rec[idx].srcSubS == 0x00) {
788
 
          myconv_rec[idx].srcSubS=i;
789
 
        }
790
 
      }
791
 
 
792
 
      
793
 
      inBuf=dmapSrc;
794
 
      for (i = 0; i < 0x60; ++i) {
795
 
        int j;
796
 
        for (j = 0; j < 0x60; ++j) {
797
 
          *inBuf=i+0xA0;
798
 
          ++inBuf;
799
 
          *inBuf=j+0xA0;
800
 
          ++inBuf;
801
 
        }
802
 
      }
803
 
      inBuf=dmapSrc;
804
 
      inBytesLeft=0x60 * 0x60 * 2;
805
 
      outBuf=(char *) dmap_rec[idx].dmapE12U;
806
 
      outBytesLeft=0x60 * 0x60 * 2;
807
 
      do {
808
 
        if ((len = iconv(cd, &inBuf, &inBytesLeft, &outBuf, &outBytesLeft)) != (size_t) 0) {
809
 
          if (errno == EILSEQ) {
810
 
#ifdef DEBUG
811
 
            if (myconvDebug) {
812
 
              MyPrintf(STDERR_WITH_TIME,
813
 
                       "dmap_open(%s,%s,%d) failed to initialize with iconv(), errno = %d in %s at %d\n",
814
 
                       to, from, idx, errno, __FILE__,__LINE__);
815
 
              MyPrintf(STDERR_WO_TIME, "inBytesLeft=%d, outBytesLeft=%d\n", inBytesLeft, outBytesLeft);
816
 
              if (inBuf - dmapSrc > 1 && inBuf - dmapSrc <= sizeof(dmapSrc) - 2)
817
 
                MyPrintf(STDERR_WO_TIME, "inBuf[-2..2]=%02X%02X%02X%02X%02X\n", inBuf[-2],inBuf[-1],inBuf[0],inBuf[1],inBuf[2]);
818
 
              else
819
 
                MyPrintf(STDERR_WO_TIME, "inBuf[0..2]=%02X%02X%02X\n", inBuf[0],inBuf[1],inBuf[2]);
820
 
              if (outBuf - (char *) dmap_rec[idx].dmapE12U > 1)
821
 
                MyPrintf(STDERR_WO_TIME, "outBuf[-2..2]=%02X%02X%02X%02X%02X\n", outBuf[-2],outBuf[-1],outBuf[0],outBuf[1],outBuf[2]);
822
 
              else
823
 
                MyPrintf(STDERR_WO_TIME, "outBuf[0..2]=%02X%02X%02X\n", outBuf[0],outBuf[1],outBuf[2]);
824
 
            }
825
 
#endif
826
 
            inBuf+=2;
827
 
            inBytesLeft-=2;
828
 
            outBuf[0]=0x00;
829
 
            outBuf[1]=0x00;
830
 
            outBuf+=2;
831
 
            outBytesLeft-=2;
832
 
          } else {
833
 
#ifdef DEBUG
834
 
            MyPrintf(STDERR_WITH_TIME,
835
 
                     "dmap_open(%s,%s,%d) failed to initialize with iconv(), errno = %d in %s at %d\n",
836
 
                     to, from, idx, errno, __FILE__,__LINE__);
837
 
#endif
838
 
            iconv_close(cd);
839
 
            return -1;
840
 
          }
841
 
        }
842
 
      } while (inBytesLeft > 0);
843
 
 
844
 
      /* SS2: 0x8E + 1 or 2 bytes */
845
 
      /* SS3: 0x8E + 1 or 2 bytes */
846
 
      while (SS != 0x00) {
847
 
        int32_t numSuccess=0;
848
 
        for (i = 0; i < 0x60; ++i) {
849
 
          inBuf=dmapSrc;
850
 
          inBuf[0]=SS;
851
 
          inBuf[1]=i+0xA0;
852
 
          inBytesLeft=2;
853
 
          if (SS == 0x8E)
854
 
            outBuf=(char *) &(dmap_rec[idx].dmapE22U[i]);
855
 
          else
856
 
            outBuf=(char *) &(dmap_rec[idx].dmapE32U[i]);
857
 
          outBytesLeft=2;
858
 
          if ((len = iconv(cd, &inBuf, &inBytesLeft, &outBuf, &outBytesLeft)) != (size_t) 0) {
859
 
            if (SS == 0x8E)
860
 
              dmap_rec[idx].dmapE22U[i]=0x0000;
861
 
            else
862
 
              dmap_rec[idx].dmapE32U[i]=0x0000;
863
 
          } else {
864
 
            ++numSuccess;
865
 
          }
866
 
        }
867
 
        if (numSuccess == 0) { /* SS2 is 2 bytes */
868
 
          inBuf=dmapSrc;
869
 
          for (i = 0; i < 0x60; ++i) {
870
 
            int j;
871
 
            for (j = 0; j < 0x60; ++j) {
872
 
              *inBuf=SS;
873
 
              ++inBuf;
874
 
              *inBuf=i+0xA0;
875
 
              ++inBuf;
876
 
              *inBuf=j+0xA0;
877
 
              ++inBuf;
878
 
            }
879
 
          }
880
 
          inBuf=dmapSrc;
881
 
          inBytesLeft=0x60 * 0x60 * 3;
882
 
          if (SS == 0x8E)
883
 
            outBuf=(char *) &(dmap_rec[idx].dmapE22U[0x60]);
884
 
          else
885
 
            outBuf=(char *) &(dmap_rec[idx].dmapE32U[0x60]);
886
 
          outBytesLeft=0x60 * 0x60 * 2;
887
 
          do {
888
 
            if ((len = iconv(cd, &inBuf, &inBytesLeft, &outBuf, &outBytesLeft)) != (size_t) 0) {
889
 
#ifdef DEBUG
890
 
              if (myconvDebug) {
891
 
                MyPrintf(STDERR_WITH_TIME,
892
 
                         "%02X:dmap_open(%s,%s,%d) failed to initialize with iconv(), errno = %d in %s at %d\n",
893
 
                         SS, to, from, idx, errno, __FILE__,__LINE__);
894
 
                MyPrintf(STDERR_WO_TIME, "inBytesLeft=%d, outBytesLeft=%d\n", inBytesLeft, outBytesLeft);
895
 
                if (inBuf - dmapSrc > 1 && inBuf - dmapSrc <= sizeof(dmapSrc) - 2)
896
 
                  MyPrintf(STDERR_WO_TIME, "inBuf[-2..2]=%02X%02X%02X%02X%02X\n", inBuf[-2],inBuf[-1],inBuf[0],inBuf[1],inBuf[2]);
897
 
                else
898
 
                  MyPrintf(STDERR_WO_TIME, "inBuf[0..2]=%02X%02X%02X\n", inBuf[0],inBuf[1],inBuf[2]);
899
 
              }
900
 
#endif
901
 
              if (errno == EILSEQ || errno == EINVAL) {
902
 
                inBuf+=3;
903
 
                inBytesLeft-=3;
904
 
                outBuf[0]=0x00;
905
 
                outBuf[1]=0x00;
906
 
                outBuf+=2;
907
 
                outBytesLeft-=2;
908
 
              } else {
909
 
#ifdef DEBUG
910
 
                MyPrintf(STDERR_WITH_TIME,
911
 
                         "%02X:dmap_open(%s,%s,%d) failed to initialize with iconv(), errno = %d in %s at %d\n",
912
 
                         SS, to, from, idx, errno, __FILE__,__LINE__);
913
 
#endif
914
 
                iconv_close(cd);
915
 
                return -1;
916
 
              }
917
 
            }
918
 
          } while (inBytesLeft > 0);
919
 
        }
920
 
        if (SS == 0x8E)
921
 
          SS=0x8F;
922
 
        else 
923
 
          SS = 0x00;
924
 
      }
925
 
      iconv_close(cd);
926
 
 
927
 
      myconv_rec[idx].subS=0x1A;
928
 
      myconv_rec[idx].subD=0xFFFD;
929
 
      for (i = 0; i < 0x80; ++i) {
930
 
        if (dmap_rec[idx].dmapE02U[i] == 0x001A) {
931
 
          myconv_rec[idx].srcSubS=i;    /* pick the first one */
932
 
          break;
933
 
        }
934
 
      }
935
 
 
936
 
      for (i = 0; i < 0x60 * 0x60; ++i) {
937
 
        if (dmap_rec[idx].dmapE12U[i] == 0xFFFD) {
938
 
          uchar byte1=i / 0x60;
939
 
          uchar byte2=i % 0x60;
940
 
          myconv_rec[idx].srcSubD=(byte1 + 0xA0) * 0x100 + (byte2 + 0xA0);    /* pick the last one */
941
 
        }
942
 
      }
943
 
 
944
 
    }
945
 
 
946
 
  } else if (((myconvIsUCS2(from)  && myconvIsEUC(to)) && (dmap_rec[idx].codingSchema = DMAP_U2E)) ||
947
 
             ((myconvIsUTF16(from) && myconvIsEUC(to)) && (dmap_rec[idx].codingSchema = DMAP_T2E)) ||
948
 
             ((myconvIsUTF8(from)  && myconvIsEUC(to)) && (dmap_rec[idx].codingSchema = DMAP_82E))) {
949
 
    /* S0: 0x00 - 0xFF */
950
 
    if ((dmap_rec[idx].dmapU2S = (uchar *) alloc_root(&dmapMemRoot, 0x100)) == NULL) {
951
 
#ifdef DEBUG
952
 
        MyPrintf(STDERR_WITH_TIME,
953
 
                 "dmap_open(%s,%s,%d), CS=%d failed with malloc(), errno = %d in %s at %d\n",
954
 
                 to, from, idx, DMAP_U2E, errno, __FILE__,__LINE__);
955
 
#endif
956
 
      return -1;
957
 
    }
958
 
    memset(dmap_rec[idx].dmapU2S, 0x00, 0x100);
959
 
 
960
 
    /* U0080 - UFFFF -> S1: 0xA0 - 0xFF, 0xA0 - 0xFF */
961
 
    if ((dmap_rec[idx].dmapU2M2 = (uchar *) alloc_root(&dmapMemRoot, 0xFF80 * 2)) == NULL) {
962
 
#ifdef DEBUG
963
 
        MyPrintf(STDERR_WITH_TIME,
964
 
                 "dmap_open(%s,%s,%d), CS=%d failed with malloc(), errno = %d in %s at %d\n",
965
 
                 to, from, idx, DMAP_U2E, errno, __FILE__,__LINE__);
966
 
#endif
967
 
      return -1;
968
 
    }
969
 
    memset(dmap_rec[idx].dmapU2M2, 0x00, 0xFF80 * 2);
970
 
 
971
 
    /* U0080 - UFFFF -> SS2: 0x8E + 0xA0 - 0xFF, 0xA0 - 0xFF
972
 
     *                  SS3: 0x8F + 0xA0 - 0xFF, 0xA0 - 0xFF */
973
 
    if ((dmap_rec[idx].dmapU2M3 = (uchar *) alloc_root(&dmapMemRoot, 0xFF80 * 3)) == NULL) {
974
 
#ifdef DEBUG
975
 
      MyPrintf(STDERR_WITH_TIME,
976
 
               "dmap_open(%s,%s,%d), CS=%d failed with malloc(), errno = %d in %s at %d\n",
977
 
               to, from, idx, DMAP_U2E, errno, __FILE__,__LINE__);
978
 
#endif
979
 
      return -1;
980
 
    }
981
 
    memset(dmap_rec[idx].dmapU2M3, 0x00, 0xFF80 * 3);
982
 
    myconv_rec[idx].allocatedSize=(0x100 + 0xFF80 * 2 + 0xFF80 * 3);
983
 
 
984
 
    {
985
 
      UniChar   dmapSrc[0x80];
986
 
      iconv_t   cd;
987
 
      int32_t   i;
988
 
      size_t    inBytesLeft;
989
 
      size_t    outBytesLeft;
990
 
      size_t    len;
991
 
      char *    inBuf;
992
 
      char *    outBuf;
993
 
 
994
 
#ifdef support_surrogate
995
 
      if ((cd = iconv_open(to, "UTF-16")) == (iconv_t) -1) {
996
 
#else
997
 
      if ((cd = iconv_open(to, "UCS-2")) == (iconv_t) -1) {
998
 
#endif
999
 
#ifdef DEBUG
1000
 
        MyPrintf(STDERR_WITH_TIME,
1001
 
                 "dmap_open(%s,%s,%d) failed with iconv_open(), errno = %d in %s at %d\n",
1002
 
                   to, from, idx, errno, __FILE__,__LINE__);
1003
 
#endif
1004
 
        return -1;
1005
 
      }
1006
 
 
1007
 
      for (i = 0; i < 0x80; ++i)
1008
 
        dmapSrc[i]=i;
1009
 
      inBuf=(char *) dmapSrc;
1010
 
      inBytesLeft=0x80 * 2;
1011
 
      outBuf=(char *) dmap_rec[idx].dmapU2S;
1012
 
      outBytesLeft=0x80;
1013
 
      do {
1014
 
        if ((len = iconv(cd, &inBuf, &inBytesLeft, &outBuf, &outBytesLeft)) != (size_t) 0) {
1015
 
#ifdef DEBUG
1016
 
          MyPrintf(STDERR_WITH_TIME,
1017
 
                   "dmap_open(%s,%s,%d) failed to initialize with iconv(), errno = %d in %s at %d\n",
1018
 
                   to, from, idx, errno, __FILE__,__LINE__);
1019
 
#endif
1020
 
          iconv_close(cd);
1021
 
          return -1;
1022
 
        }
1023
 
      } while (inBytesLeft > 0);
1024
 
 
1025
 
      myconv_rec[idx].srcSubS = 0x1A;
1026
 
      myconv_rec[idx].srcSubD = 0xFFFD;
1027
 
      myconv_rec[idx].subS = dmap_rec[idx].dmapU2S[0x1A];
1028
 
      
1029
 
      outBuf=(char *) &(myconv_rec[idx].subD);
1030
 
      dmapSrc[0]=0xFFFD;
1031
 
      inBuf=(char *) dmapSrc;
1032
 
      inBytesLeft=2;
1033
 
      outBytesLeft=2;
1034
 
      if ((len = iconv(cd, &inBuf, &inBytesLeft, &outBuf, &outBytesLeft)) != (size_t) 0) {
1035
 
#ifdef DEBUG
1036
 
        if (myconvDebug) {
1037
 
          MyPrintf(STDERR_WITH_TIME,
1038
 
                   "dmap_open(%s,%s,%d) failed to initialize with iconv(), rc=%d, errno=%d in %s at %d\n",
1039
 
                   to, from, idx, len, errno, __FILE__,__LINE__);
1040
 
          MyPrintf(STDERR_WO_TIME, "iconv(0x1A,1,%p,1) returns outBuf=%p, outBytesLeft=%d\n",
1041
 
                   dmapSrc, outBuf, outBytesLeft);
1042
 
        }
1043
 
#endif
1044
 
        if (outBytesLeft == 0) {
1045
 
          /* UCS-2_IBM-eucKR returns error.
1046
 
             myconv(iconv) rc=1, error=0, InBytesLeft=0, OutBytesLeft=18
1047
 
             myconv(iconvRev) rc=-1, error=116, InBytesLeft=2, OutBytesLeft=20
1048
 
             iconv: 0xFFFD => 0xAFFE => 0x    rc=1,-1  sub=0,0
1049
 
          */
1050
 
          ;
1051
 
        } else {
1052
 
          iconv_close(cd);
1053
 
          return -1;
1054
 
        }
1055
 
      }
1056
 
      
1057
 
      for (i = 0x80; i < 0xFFFF; ++i) {
1058
 
        uchar   eucBuf[3];
1059
 
        dmapSrc[0]=i;
1060
 
        inBuf=(char *) dmapSrc;
1061
 
        inBytesLeft=2;
1062
 
        outBuf=(char *) eucBuf;
1063
 
        outBytesLeft=sizeof(eucBuf);
1064
 
        errno=0;
1065
 
        if ((len = iconv(cd, &inBuf, &inBytesLeft, &outBuf, &outBytesLeft)) != (size_t) 0) {
1066
 
          if (len == 1 && errno == 0 && inBytesLeft == 0 && outBytesLeft == 1) {  /* substitution occurred. */            continue;
1067
 
          }
1068
 
 
1069
 
          if (errno == EILSEQ) {
1070
 
#ifdef DEBUG
1071
 
            if (myconvDebug) {
1072
 
              MyPrintf(STDERR_WITH_TIME,
1073
 
                       "dmap_open(%s,%s,%d) failed to initialize with iconv(), errno = %d in %s at %d\n",
1074
 
                       to, from, idx, errno, __FILE__,__LINE__);
1075
 
              MyPrintf(STDERR_WO_TIME, "inBytesLeft=%d, outBytesLeft=%d\n", inBytesLeft, outBytesLeft);
1076
 
              if (inBuf - (char *) dmapSrc > 1 && inBuf - (char *) dmapSrc <= sizeof(dmapSrc) - 2)
1077
 
                MyPrintf(STDERR_WO_TIME, "inBuf[-2..2]=%02X%02X%02X%02X%02X\n", inBuf[-2],inBuf[-1],inBuf[0],inBuf[1],inBuf[2]);
1078
 
              else
1079
 
                MyPrintf(STDERR_WO_TIME, "inBuf[0..2]=%02X%02X%02X\n", inBuf[0],inBuf[1],inBuf[2]);
1080
 
              if (outBuf - (char *) dmap_rec[idx].dmapU2M2 > 1)
1081
 
                MyPrintf(STDERR_WO_TIME, "outBuf[-2..2]=%02X%02X%02X%02X%02X\n", outBuf[-2],outBuf[-1],outBuf[0],outBuf[1],outBuf[2]);
1082
 
              else
1083
 
                MyPrintf(STDERR_WO_TIME, "outBuf[0..2]=%02X%02X%02X\n", outBuf[0],outBuf[1],outBuf[2]);
1084
 
            }
1085
 
#endif
1086
 
            inBuf+=2;
1087
 
            inBytesLeft-=2;
1088
 
            memcpy(outBuf, (char *) &(myconv_rec[idx].subD), 2);
1089
 
            outBuf+=2;
1090
 
            outBytesLeft-=2;
1091
 
          } else {
1092
 
#ifdef DEBUG
1093
 
            if (myconvDebug) {
1094
 
              MyPrintf(STDERR_WITH_TIME,
1095
 
                       "dmap_open(%s,%s,%d) failed to initialize with iconv(), rc = %d, errno = %d in %s at %d\n",
1096
 
                       to, from, idx, len, errno, __FILE__,__LINE__);
1097
 
              MyPrintf(STDERR_WITH_TIME,
1098
 
                       "%04X: src=%04X%04X, inBuf=0x%02X%02X, inBytesLeft=%d, outBuf[-2..0]=%02X%02X%02X, outBytesLeft=%d\n",
1099
 
                       i, dmapSrc[0], dmapSrc[1], inBuf[0], inBuf[1],
1100
 
                       inBytesLeft, outBuf[-2], outBuf[-1], outBuf[0], outBytesLeft);
1101
 
              MyPrintf(STDERR_WITH_TIME,
1102
 
                       "&dmapSrc=%p, inBuf=%p, dmapU2M2 + %d = %p, outBuf=%p\n",
1103
 
                       dmapSrc, inBuf, (i - 0x80) * 2, dmap_rec[idx].dmapU2M2 + (i - 0x80) * 2, outBuf);
1104
 
            }
1105
 
#endif
1106
 
            iconv_close(cd);
1107
 
            return -1;
1108
 
          }
1109
 
        }
1110
 
        if (sizeof(eucBuf) - outBytesLeft == 1) {
1111
 
          if (i < 0x100) {
1112
 
            (dmap_rec[idx].dmapU2S)[i]=eucBuf[0];
1113
 
          } else {
1114
 
            dmap_rec[idx].dmapU2M2[(i - 0x80) * 2]        = eucBuf[0];
1115
 
            dmap_rec[idx].dmapU2M2[(i - 0x80) * 2 + 1]    = 0x00;
1116
 
          }
1117
 
        } else if (sizeof(eucBuf) - outBytesLeft == 2) {  /* 2 bytes */
1118
 
          dmap_rec[idx].dmapU2M2[(i - 0x80) * 2]        = eucBuf[0];
1119
 
          dmap_rec[idx].dmapU2M2[(i - 0x80) * 2 + 1]    = eucBuf[1];
1120
 
        } else if (sizeof(eucBuf) - outBytesLeft == 3) { /* 3 byte SS2/SS3 */
1121
 
          dmap_rec[idx].dmapU2M3[(i - 0x80) * 3]        = eucBuf[0];
1122
 
          dmap_rec[idx].dmapU2M3[(i - 0x80) * 3 + 1]    = eucBuf[1];
1123
 
          dmap_rec[idx].dmapU2M3[(i - 0x80) * 3 + 2]    = eucBuf[2];
1124
 
        } else {
1125
 
#ifdef DEBUG
1126
 
          if (myconvDebug) {
1127
 
            MyPrintf(STDERR_WITH_TIME,
1128
 
                     "dmap_open(%s,%s,%d) failed to initialize with iconv(), rc=%d, errno=%d in %s at %d\n",
1129
 
                     to, from, idx, len, errno, __FILE__,__LINE__);
1130
 
            MyPrintf(STDERR_WITH_TIME,
1131
 
                     "%04X: src=%04X%04X, inBuf=0x%02X%02X, inBytesLeft=%d, outBuf=%02X%02X%02X, outBytesLeft=%d\n",
1132
 
                     i, dmapSrc[0], dmapSrc[1], inBuf[0], inBuf[1],
1133
 
                     inBytesLeft, outBuf[-2], outBuf[-1], outBuf[0], outBytesLeft);
1134
 
            MyPrintf(STDERR_WITH_TIME,
1135
 
                     "&dmapSrc=%p, inBuf=%p, %p, outBuf=%p\n",
1136
 
                     dmapSrc, inBuf, dmap_rec[idx].dmapU2M3 + (i - 0x80) * 2, outBuf);
1137
 
          }
1138
 
#endif
1139
 
          return -1;
1140
 
        }
1141
 
          
1142
 
      }
1143
 
      iconv_close(cd);
1144
 
    }
1145
 
 
1146
 
  } else if (myconvIsUTF16(from) && myconvIsUTF8(to)) {
1147
 
    dmap_rec[idx].codingSchema = DMAP_T28;
1148
 
 
1149
 
  } else if (myconvIsUCS2(from) && myconvIsUTF8(to)) {
1150
 
    dmap_rec[idx].codingSchema = DMAP_U28;
1151
 
 
1152
 
  } else if (myconvIsUTF8(from) && myconvIsUnicode2(to)) {
1153
 
    dmap_rec[idx].codingSchema = DMAP_82U;
1154
 
 
1155
 
  } else if (myconvIsUnicode2(from) && myconvIsUnicode2(to)) {
1156
 
    dmap_rec[idx].codingSchema = DMAP_U2U;
1157
 
 
1158
 
  } else {
1159
 
    
1160
 
    return -1;
1161
 
  }
1162
 
  myconv_rec[idx].cnv_dmap=&(dmap_rec[idx]);
1163
 
  return 0;
1164
 
}
1165
 
 
1166
 
 
1167
 
 
1168
 
static int      bins_open(const char *  to,
1169
 
                          const char *  from,
1170
 
                          const int32_t idx)
1171
 
{
1172
 
  return -1;
1173
 
}
1174
 
 
1175
 
 
1176
 
 
1177
 
static int32_t  dmap_close(const int32_t        idx)
1178
 
{
1179
 
  if (dmap_rec[idx].codingSchema == DMAP_S2S) {
1180
 
    if (dmap_rec[idx].dmapS2S != NULL) {
1181
 
      dmap_rec[idx].dmapS2S=NULL;
1182
 
    }
1183
 
  } else if (dmap_rec[idx].codingSchema = DMAP_E2U) {
1184
 
    if (dmap_rec[idx].dmapE02U != NULL) {
1185
 
      dmap_rec[idx].dmapE02U=NULL;
1186
 
    }
1187
 
    if (dmap_rec[idx].dmapE12U != NULL) {
1188
 
      dmap_rec[idx].dmapE12U=NULL;
1189
 
    }
1190
 
    if (dmap_rec[idx].dmapE22U != NULL) {
1191
 
      dmap_rec[idx].dmapE22U=NULL;
1192
 
    }
1193
 
    if (dmap_rec[idx].dmapE32U != NULL) {
1194
 
      dmap_rec[idx].dmapE32U=NULL;
1195
 
    }
1196
 
  }
1197
 
 
1198
 
  return 0;
1199
 
}
1200
 
 
1201
 
 
1202
 
static int32_t  bins_close(const int32_t        idx)
1203
 
{
1204
 
  return 0;
1205
 
}
1206
 
 
1207
 
 
1208
 
myconv_t myconv_open(const char *       toCode,
1209
 
                     const char *       fromCode,
1210
 
                     int32_t            converter)
1211
 
{
1212
 
  int32 i;
1213
 
  for (i = 0; i < MAX_CONVERTER; ++i) {
1214
 
    if (myconv_rec[i].converterType == 0)
1215
 
      break;
1216
 
  }
1217
 
  if (i >= MAX_CONVERTER)
1218
 
    return ((myconv_t) -1);
1219
 
 
1220
 
  myconv_rec[i].converterType = converter;
1221
 
  myconv_rec[i].index=i;
1222
 
  myconv_rec[i].fromCcsid=cstoccsid(fromCode);
1223
 
  if (myconv_rec[i].fromCcsid == 0 && memcmp(fromCode, "big5",5) == 0)
1224
 
    myconv_rec[i].fromCcsid=950;
1225
 
  myconv_rec[i].toCcsid=cstoccsid(toCode);
1226
 
  if (myconv_rec[i].toCcsid == 0 && memcmp(toCode, "big5",5) == 0)
1227
 
    myconv_rec[i].toCcsid=950;
1228
 
  strncpy(myconv_rec[i].from,   fromCode,       sizeof(myconv_rec[i].from)-1);
1229
 
  strncpy(myconv_rec[i].to,     toCode,         sizeof(myconv_rec[i].to)-1);
1230
 
 
1231
 
  if (converter == CONVERTER_ICONV) {
1232
 
    if ((myconv_rec[i].cnv_iconv=iconv_open(toCode, fromCode)) == (iconv_t) -1) {
1233
 
      return ((myconv_t) -1);
1234
 
    }
1235
 
    myconv_rec[i].allocatedSize = -1;
1236
 
    myconv_rec[i].srcSubS=myconvGetSubS(fromCode);
1237
 
    myconv_rec[i].srcSubD=myconvGetSubD(fromCode);
1238
 
    myconv_rec[i].subS=myconvGetSubS(toCode);
1239
 
    myconv_rec[i].subD=myconvGetSubD(toCode);
1240
 
    return &(myconv_rec[i]);
1241
 
  } else if (converter == CONVERTER_DMAP &&
1242
 
             dmap_open(toCode, fromCode, i) != -1) {
1243
 
    return &(myconv_rec[i]);
1244
 
  }
1245
 
  return ((myconv_t) -1);
1246
 
}
1247
 
 
1248
 
 
1249
 
 
1250
 
int32_t myconv_close(myconv_t cd)
1251
 
{
1252
 
  int32_t   ret=0;
1253
 
 
1254
 
  if (cd->converterType == CONVERTER_ICONV) {
1255
 
    ret=iconv_close(cd->cnv_iconv);
1256
 
  } else if (cd->converterType == CONVERTER_DMAP) {
1257
 
    ret=dmap_close(cd->index);
1258
 
  }
1259
 
  memset(&(myconv_rec[cd->index]), 0x00, sizeof(myconv_rec[cd->index]));
1260
 
  return ret;
1261
 
}
1262
 
 
1263
 
 
1264
 
 
1265
 
 
1266
 
/* reference: http://www-306.ibm.com/software/globalization/other/es.jsp */
1267
 
/* systemCL would be expensive, and myconvIsXXXXX is called frequently.
1268
 
   need to cache entries */
1269
 
#define MAX_CCSID       256
1270
 
static int      ccsidList      [MAX_CCSID];
1271
 
static int      esList         [MAX_CCSID];
1272
 
int32 getEncodingScheme(const uint16 inCcsid, int32& outEncodingScheme);
1273
 
EXTERN int      myconvGetES(CCSID     ccsid) 
1274
 
{
1275
 
  /* call QtqValidateCCSID in ILE to get encoding schema */
1276
 
  /*  return QtqValidateCCSID(ccsid); */
1277
 
  int   i;
1278
 
  for (i = 0; i < MAX_CCSID; ++i) {
1279
 
    if (ccsidList[i] == ccsid)
1280
 
      return esList[i];
1281
 
    if (ccsidList[i] == 0x00)
1282
 
      break;
1283
 
  }
1284
 
 
1285
 
  if (i >= MAX_CCSID) {
1286
 
    i=MAX_CCSID-1;
1287
 
  }
1288
 
 
1289
 
  { 
1290
 
    ccsidList[i]=ccsid;
1291
 
    getEncodingScheme(ccsid, esList[i]);
1292
 
#ifdef DEBUG_PASE
1293
 
    if (myconvDebug) {
1294
 
      fprintf(stderr, "CCSID=%d, ES=0x%04X\n", ccsid, esList[i]);
1295
 
    }
1296
 
#endif
1297
 
    return esList[i];
1298
 
  }
1299
 
  return 0;
1300
 
}
1301
 
 
1302
 
 
1303
 
EXTERN  int     myconvIsEBCDIC(const char *     pName)
1304
 
{
1305
 
  int   es = myconvGetES(cstoccsid(pName));
1306
 
  if (es == 0x1100 ||
1307
 
      es == 0x1200 ||
1308
 
      es == 0x6100 ||
1309
 
      es == 0x6200 ||
1310
 
      es == 0x1301 ) {
1311
 
    return TRUE;
1312
 
  }
1313
 
  return FALSE;
1314
 
}
1315
 
 
1316
 
 
1317
 
EXTERN int      myconvIsISO(const char *    pName)
1318
 
{
1319
 
  int   es = myconvGetES(cstoccsid(pName));
1320
 
  if (es == 0x4100 ||
1321
 
      es == 0x4105 ||
1322
 
      es == 0x4155 ||
1323
 
      es == 0x5100 ||
1324
 
      es == 0x5150 ||
1325
 
      es == 0x5200 ||
1326
 
      es == 0x5404 ||
1327
 
      es == 0x5409 ||
1328
 
      es == 0x540A ||
1329
 
      es == 0x5700) {
1330
 
    return TRUE;
1331
 
  }
1332
 
  return FALSE;
1333
 
}
1334
 
 
1335
 
 
1336
 
EXTERN  int     myconvIsASCII(const char *      pName)
1337
 
{
1338
 
  int   es = myconvGetES(cstoccsid(pName));
1339
 
  if (es == 0x2100 ||
1340
 
      es == 0x3100 ||
1341
 
      es == 0x8100 ||
1342
 
      es == 0x2200 ||
1343
 
      es == 0x3200 ||
1344
 
      es == 0x9200 ||
1345
 
      es == 0x2300 ||
1346
 
      es == 0x2305 ||
1347
 
      es == 0x3300 ||
1348
 
      es == 0x2900 ||
1349
 
      es == 0x2A00) {
1350
 
    return TRUE;
1351
 
  } else if (memcmp(pName, "big5", 5) == 0) {
1352
 
    return TRUE;
1353
 
  }
1354
 
  return FALSE;
1355
 
}
1356
 
 
1357
 
 
1358
 
 
1359
 
EXTERN  int     myconvIsUCS2(const char *       pName)
1360
 
{
1361
 
  if (cstoccsid(pName) == 13488) {
1362
 
    return TRUE;
1363
 
  }
1364
 
  return FALSE;
1365
 
}
1366
 
 
1367
 
 
1368
 
EXTERN  int     myconvIsUTF16(const char *       pName)
1369
 
{
1370
 
  if (cstoccsid(pName) == 1200) {
1371
 
    return TRUE;
1372
 
  }
1373
 
  return FALSE;
1374
 
}
1375
 
 
1376
 
 
1377
 
EXTERN  int     myconvIsUnicode2(const char *       pName)
1378
 
{
1379
 
  int   es = myconvGetES(cstoccsid(pName));
1380
 
  if (es == 0x7200 ||
1381
 
      es == 0x720B ||
1382
 
      es == 0x720F) {
1383
 
    return TRUE;
1384
 
  }
1385
 
  return FALSE;
1386
 
}
1387
 
 
1388
 
 
1389
 
EXTERN  int     myconvIsUTF8(const char *       pName)
1390
 
{
1391
 
  int   es = myconvGetES(cstoccsid(pName));
1392
 
  if (es == 0x7807) {
1393
 
    return TRUE;
1394
 
  }
1395
 
  return FALSE;
1396
 
}
1397
 
 
1398
 
 
1399
 
EXTERN  int     myconvIsUnicode(const char *       pName)
1400
 
{
1401
 
  int   es = myconvGetES(cstoccsid(pName));
1402
 
  if (es == 0x7200 ||
1403
 
      es == 0x720B ||
1404
 
      es == 0x720F ||
1405
 
      es == 0x7807) {
1406
 
    return TRUE;
1407
 
  }
1408
 
  return FALSE;
1409
 
}
1410
 
 
1411
 
 
1412
 
EXTERN  int     myconvIsEUC(const char *        pName)
1413
 
{
1414
 
  int   es = myconvGetES(cstoccsid(pName));
1415
 
  if (es == 0x4403) {
1416
 
    return TRUE;
1417
 
  }
1418
 
  return FALSE;
1419
 
}
1420
 
 
1421
 
 
1422
 
EXTERN int      myconvIsDBCS(const char *   pName)
1423
 
{
1424
 
  int   es = myconvGetES(cstoccsid(pName));
1425
 
  if (es == 0x1200 ||
1426
 
      es == 0x2200 ||
1427
 
      es == 0x2300 ||
1428
 
      es == 0x2305 ||
1429
 
      es == 0x2A00 ||
1430
 
      es == 0x3200 ||
1431
 
      es == 0x3300 ||
1432
 
      es == 0x5200 ||
1433
 
      es == 0x6200 ||
1434
 
      es == 0x9200) {
1435
 
    return TRUE;
1436
 
  } else if (memcmp(pName, "big5", 5) == 0) {
1437
 
    return TRUE;
1438
 
  }
1439
 
  return FALSE;
1440
 
}
1441
 
 
1442
 
 
1443
 
EXTERN int      myconvIsSBCS(const char *   pName)
1444
 
{
1445
 
  int   es = myconvGetES(cstoccsid(pName));
1446
 
  if (es == 0x1100 ||
1447
 
      es == 0x2100 ||
1448
 
      es == 0x3100 ||
1449
 
      es == 0x4100 ||
1450
 
      es == 0x4105 ||
1451
 
      es == 0x5100 ||
1452
 
      es == 0x5150 ||
1453
 
      es == 0x6100 ||
1454
 
      es == 0x8100) {
1455
 
    return TRUE;
1456
 
  }
1457
 
  return FALSE;
1458
 
}
1459
 
 
1460
 
 
1461
 
 
1462
 
EXTERN char myconvGetSubS(const char *     code) 
1463
 
{
1464
 
  if (myconvIsEBCDIC(code)) {
1465
 
    return 0x3F;
1466
 
  } else if (myconvIsASCII(code)) {
1467
 
    return 0x1A;
1468
 
  } else if (myconvIsISO(code)) {
1469
 
    return 0x1A;
1470
 
  } else if (myconvIsEUC(code)) {
1471
 
    return 0x1A;
1472
 
  } else if (myconvIsUCS2(code)) {
1473
 
    return 0x00;
1474
 
  } else if (myconvIsUTF8(code)) {
1475
 
    return 0x1A;
1476
 
  }
1477
 
  return 0x00;
1478
 
}
1479
 
 
1480
 
 
1481
 
EXTERN UniChar myconvGetSubD(const char *     code) 
1482
 
{
1483
 
  if (myconvIsEBCDIC(code)) {
1484
 
    return 0xFDFD;
1485
 
  } else if (myconvIsASCII(code)) {
1486
 
    return 0xFCFC;
1487
 
  } else if (myconvIsISO(code)) {
1488
 
    return 0x00;
1489
 
  } else if (myconvIsEUC(code)) {
1490
 
    return 0x00;
1491
 
  } else if (myconvIsUCS2(code)) {
1492
 
    return 0xFFFD;
1493
 
  } else if (myconvIsUTF8(code)) {
1494
 
    return 0x00;
1495
 
  }
1496
 
  return 0x00;
1497
 
}
1498