File: avc_mbyte.c

package info (click to toggle)
avce00 2.0.0-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 940 kB
  • sloc: ansic: 9,851; makefile: 3
file content (534 lines) | stat: -rw-r--r-- 18,009 bytes parent folder | download | duplicates (7)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/* $Id: avc_mbyte.c,v 1.3 2005/06/03 03:49:59 daniel Exp $
 *
 * Name:     avc_mbyte.c
 * Project:  Arc/Info vector coverage (AVC)  E00->BIN conversion library
 * Language: ANSI C
 * Purpose:  Functions to handle multibyte character conversions.
 * Author:   Daniel Morissette, dmorissette@dmsolutions.ca
 *
 **********************************************************************
 * Copyright (c) 1999-2005, Daniel Morissette
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 **********************************************************************
 *
 * $Log: avc_mbyte.c,v $
 * Revision 1.3  2005/06/03 03:49:59  daniel
 * Update email address, website url, and copyright dates
 *
 * Revision 1.2  2000/09/22 19:45:21  daniel
 * Switch to MIT-style license
 *
 * Revision 1.1  2000/05/29 15:31:03  daniel
 * Initial revision - Japanese support
 *
 **********************************************************************/

#include "avc.h"

#ifdef _WIN32
#  include <mbctype.h>
#endif

static int _AVCDetectJapaneseEncoding(const unsigned char *pszLine);
static const char *_AVCJapanese2ArcDBCS(AVCDBCSInfo *psDBCSInfo,
                                        const unsigned char *pszLine,
                                        int nMaxOutputLen);
static const char *_AVCArcDBCS2JapaneseShiftJIS(AVCDBCSInfo *psDBCSInfo, 
                                                const unsigned char *pszLine,
                                                int nMaxOutputLen);

/*=====================================================================
 * Functions to handle multibyte char conversions
 *====================================================================*/

#define IS_ASCII(c)           ((c) < 0x80)


/**********************************************************************
 *                          AVCAllocDBCSInfo()
 *
 * Alloc and init a new AVCDBCSInfo structure.
 **********************************************************************/
AVCDBCSInfo *AVCAllocDBCSInfo()
{
    AVCDBCSInfo *psInfo;

    psInfo = (AVCDBCSInfo*)CPLCalloc(1, sizeof(AVCDBCSInfo));

    psInfo->nDBCSCodePage = AVCGetDBCSCodePage();
    psInfo->nDBCSEncoding = AVC_CODE_UNKNOWN;
    psInfo->pszDBCSBuf    = NULL;
    psInfo->nDBCSBufSize  = 0;

    return psInfo;
}

/**********************************************************************
 *                          AVCFreeDBCSInfo()
 *
 * Release all memory associated with a AVCDBCSInfo structure.
 **********************************************************************/
void AVCFreeDBCSInfo(AVCDBCSInfo *psInfo)
{
    if (psInfo)
    {
        CPLFree(psInfo->pszDBCSBuf);
        CPLFree(psInfo);
    }
}

/**********************************************************************
 *                          AVCGetDBCSCodePage()
 *
 * Fetch current multibyte codepage on the system.  
 * Returns a valid codepage number, or 0 if the codepage is single byte or
 * unsupported.
 **********************************************************************/
int AVCGetDBCSCodePage()
{
#ifdef _WIN32
    int nCP;
    nCP = _getmbcp();

    /* Check if that's a supported codepage */
    if (nCP == AVC_DBCS_JAPANESE)
        return nCP;
#endif

    return 0;
}


/**********************************************************************
 *                          AVCE00DetectEncoding()
 *
 * Try to detect the encoding used in the current file by examining lines
 * of input.
 *
 * Returns TRUE once the encoding is established, or FALSE if more lines
 * of input are required to establish the encoding.
 **********************************************************************/
GBool AVCE00DetectEncoding(AVCDBCSInfo *psDBCSInfo, const char *pszLine)
{
    if (psDBCSInfo == NULL || psDBCSInfo->nDBCSCodePage == 0 ||
        psDBCSInfo->nDBCSEncoding != AVC_CODE_UNKNOWN)
    {
        /* Either single byte codepage, or encoding has already been detected
         */
        return TRUE;
    }

    switch (psDBCSInfo->nDBCSCodePage)
    {
      case AVC_DBCS_JAPANESE:
        psDBCSInfo->nDBCSEncoding = 
                  _AVCDetectJapaneseEncoding((const unsigned char *)pszLine);
        break;
      default:
        psDBCSInfo->nDBCSEncoding = AVC_CODE_UNKNOWN;
        return TRUE;  /* Codepage not supported... no need to scan more lines*/
    }
    
    if (psDBCSInfo->nDBCSEncoding != AVC_CODE_UNKNOWN)
        return TRUE;  /* We detected the encoding! */

    return FALSE;
}


/**********************************************************************
 *                          AVCE00Convert2ArcDBCS()
 *
 * If encoding is still unknown, try to detect the encoding used in the
 * current file, and then convert the string to an encoding validfor output
 * to a coverage.
 *
 * Returns a reference to a const buffer that should not be freed by the
 * caller.  It can be either the original string buffer or a ref. to an
 * internal buffer.
 **********************************************************************/
const char *AVCE00Convert2ArcDBCS(AVCDBCSInfo *psDBCSInfo, 
                                       const char *pszLine,
                                       int nMaxOutputLen)
{
    const char *pszOutBuf = NULL;
    unsigned char *pszTmp;
    GBool bAllAscii;

    if (psDBCSInfo == NULL || 
        psDBCSInfo->nDBCSCodePage == 0 || pszLine == NULL)
    {
        /* Single byte codepage... nothing to do
         */
        return pszLine;
    }

    /* If string is all ASCII then there is nothing to do...
     */
    pszTmp = (unsigned char *)pszLine;
    for(bAllAscii = TRUE ; bAllAscii && pszTmp && *pszTmp; pszTmp++)
    {
        if ( !IS_ASCII(*pszTmp) )
            bAllAscii = FALSE;
    }
    if (bAllAscii)
        return pszLine;

    /* Make sure output buffer is large enough.
     * We add 2 chars to buffer size to simplify processing... no need to
     * check if second byte of a pair would overflow buffer.
     */
    if (psDBCSInfo->pszDBCSBuf == NULL || 
        psDBCSInfo->nDBCSBufSize < nMaxOutputLen+2)
    {
        psDBCSInfo->nDBCSBufSize = nMaxOutputLen+2;
        psDBCSInfo->pszDBCSBuf = 
            (unsigned char *)CPLRealloc(psDBCSInfo->pszDBCSBuf,
                                        psDBCSInfo->nDBCSBufSize*
                                        sizeof(unsigned char));
    }

    /* Do the conversion according to current code page 
     */
    switch (psDBCSInfo->nDBCSCodePage)
    {
      case AVC_DBCS_JAPANESE:
        pszOutBuf = (char*)_AVCJapanese2ArcDBCS(psDBCSInfo,
                                                (const unsigned char *)pszLine,
                                                nMaxOutputLen);
        break;
      default:
        /* We should never get here anyways, but just in case return pszLine 
         */
        pszOutBuf = pszLine;
    }
    
    return pszOutBuf;
}

/**********************************************************************
 *                          AVCE00ConvertFromArcDBCS()
 *
 * Convert DBCS encoding in binary coverage files to E00 encoding.
 *
 * Returns a reference to a const buffer that should not be freed by the
 * caller.  It can be either the original string buffer or a ref. to an
 * internal buffer.
 **********************************************************************/
const char *AVCE00ConvertFromArcDBCS(AVCDBCSInfo *psDBCSInfo, 
                                     const char *pszLine,
                                     int nMaxOutputLen)
{
    const char *pszOutBuf = NULL;
    unsigned char *pszTmp;
    GBool bAllAscii;

    if (psDBCSInfo == NULL || 
        psDBCSInfo->nDBCSCodePage == 0 || pszLine == NULL)
    {
        /* Single byte codepage... nothing to do
         */
        return pszLine;
    }

    /* If string is all ASCII then there is nothing to do...
     */
    pszTmp = (unsigned char *)pszLine;
    for(bAllAscii = TRUE ; bAllAscii && pszTmp && *pszTmp; pszTmp++)
    {
        if ( !IS_ASCII(*pszTmp) )
            bAllAscii = FALSE;
    }
    if (bAllAscii)
        return pszLine;

    /* Make sure output buffer is large enough.
     * We add 2 chars to buffer size to simplify processing... no need to
     * check if second byte of a pair would overflow buffer.
     */
    if (psDBCSInfo->pszDBCSBuf == NULL || 
        psDBCSInfo->nDBCSBufSize < nMaxOutputLen+2)
    {
        psDBCSInfo->nDBCSBufSize = nMaxOutputLen+2;
        psDBCSInfo->pszDBCSBuf = 
            (unsigned char *)CPLRealloc(psDBCSInfo->pszDBCSBuf,
                                        psDBCSInfo->nDBCSBufSize*
                                        sizeof(unsigned char));
    }

    /* Do the conversion according to current code page 
     */
    switch (psDBCSInfo->nDBCSCodePage)
    {
      case AVC_DBCS_JAPANESE:
        pszOutBuf = (char*)_AVCArcDBCS2JapaneseShiftJIS(psDBCSInfo,
                                                (const unsigned char *)pszLine,
                                                        nMaxOutputLen);
        break;
      default:
        /* We should never get here anyways, but just in case return pszLine 
         */
        pszOutBuf = pszLine;
    }
    
    return pszOutBuf;
}

/*=====================================================================
 *=====================================================================
 * Functions Specific to Japanese encoding (CodePage 932).  
 *
 * For now we assume that we can receive only Katakana, Shift-JIS, or EUC
 * encoding as input.  Coverages use EUC encoding in most cases, except
 * for Katakana characters that are prefixed with a 0x8e byte.
 *
 * Most of the Japanese conversion functions are based on information and
 * algorithms found at:
 *  http://www.mars.dti.ne.jp/~torao/program/appendix/japanese-en.html
 *=====================================================================
 *====================================================================*/

/**********************************************************************
 *                          _AVCDetectJapaneseEncoding()
 *
 * Scan a line of text to try to establish the type of japanese encoding
 *
 * Returns the encoding number (AVC_CODE_JAP_*), or AVC_CODE_UNKNOWN if no
 * specific encoding was detected.
 **********************************************************************/

#define IS_JAP_SHIFTJIS_1(c)  ((c) >= 0x81 && (c) <= 0x9f)
#define IS_JAP_SHIFTJIS_2(c)  (((c) >= 0x40 && (c) <= 0x7e) ||   \
                               ((c) >= 0x80 && (c) <= 0xA0) )
#define IS_JAP_EUC_1(c)       ((c) >= 0xF0 && (c) <= 0xFE)
#define IS_JAP_EUC_2(c)       ((c) >= 0xFD && (c) <= 0xFE)
#define IS_JAP_KANA(c)        ((c) >= 0xA1 && (c) <= 0xDF)

static int _AVCDetectJapaneseEncoding(const unsigned char *pszLine)
{
    int nEncoding = AVC_CODE_UNKNOWN;

    for( ; nEncoding == AVC_CODE_UNKNOWN && pszLine && *pszLine; pszLine++)
    {
        if (IS_ASCII(*pszLine))
            continue;
        else if (IS_JAP_SHIFTJIS_1(*pszLine))
        {
            nEncoding = AVC_CODE_JAP_SHIFTJIS;
            break;
        }
        else if (IS_JAP_KANA(*pszLine) && *(pszLine+1) &&
                 (IS_ASCII(*(pszLine+1)) || 
                  (*(pszLine+1)>=0x80 && *(pszLine+1)<=0xA0) ) )
        {
            nEncoding = AVC_CODE_JAP_SHIFTJIS; /* SHIFT-JIS + Kana */
            break;
        }
        else if (IS_JAP_EUC_1(*pszLine))
        {
            nEncoding = AVC_CODE_JAP_EUC;
            break;
        }

        if (*(++pszLine) == '\0')
            break;

        if (IS_JAP_SHIFTJIS_2(*pszLine))
        {
            nEncoding = AVC_CODE_JAP_SHIFTJIS;
            break;
        }
        else if (IS_JAP_EUC_2(*pszLine))
        {
            nEncoding = AVC_CODE_JAP_EUC;
            break;
        }
    }

    return nEncoding;
}


/**********************************************************************
 *                          _AVCJapanese2ArcDBCS()
 *
 * Try to detect type of Japanese encoding if not done yet, and convert
 * string from Japanese to proper coverage DBCS encoding.
 **********************************************************************/
static const char *_AVCJapanese2ArcDBCS(AVCDBCSInfo *psDBCSInfo, 
                                        const unsigned char *pszLine,
                                        int nMaxOutputLen)
{
    unsigned char *pszOut;
    int iDst;

    pszOut = psDBCSInfo->pszDBCSBuf;

    if (psDBCSInfo->nDBCSEncoding == AVC_CODE_UNKNOWN)
    {
        /* Type of encoding (Shift-JIS or EUC) not known yet... try to
         * detect it now.
         */
        psDBCSInfo->nDBCSEncoding = _AVCDetectJapaneseEncoding(pszLine);

/*
        if (psDBCSInfo->nDBCSEncoding == AVC_CODE_JAP_SHIFTJIS)
        {
            printf("Found Japanese Shift-JIS encoding\n");
        }
        else if (psDBCSInfo->nDBCSEncoding == AVC_CODE_JAP_EUC)
        {
            printf("Found Japanese EUC encoding\n");
        }
*/
    }

    for(iDst=0; *pszLine && iDst < nMaxOutputLen; pszLine++)
    {
        if (IS_ASCII(*pszLine))
        {
            /* No transformation required for ASCII */
            pszOut[iDst++] = *pszLine;
        }
        else if ( psDBCSInfo->nDBCSEncoding==AVC_CODE_JAP_EUC && *(pszLine+1) )
        {
            /* This must be a pair of EUC chars and both should be in
             * the range 0xA1-0xFE
             */
            pszOut[iDst++] = *(pszLine++);
            pszOut[iDst++] = *pszLine;
        }
        else if ( IS_JAP_KANA(*pszLine) )
        {
            /* Katakana char. prefix it with 0x8e */
            pszOut[iDst++] = 0x8e;
            pszOut[iDst++] = *pszLine;
        }
        else if ( *(pszLine+1) )
        {
            /* This must be a pair of Shift-JIS chars... convert them to EUC
             *
             * If we haven't been able to establish the encoding for sure
             * yet, then it is possible that a pair of EUC chars could be
             * treated as shift-JIS here... but there is not much we can do
             * about that unless we scan the whole E00 input before we
             * start the conversion.
             */
            unsigned char leader, trailer;
            leader = *(pszLine++);
            trailer = *pszLine;

            if(leader <= 0x9F)  leader -= 0x71;
            else                leader -= 0xB1;
            leader = (leader << 1) + 1;

            if(trailer > 0x7F)  trailer --;
            if(trailer >= 0x9E)
            {
                trailer -= 0x7D;
                leader ++;
            }
            else 
            {
                trailer -= 0x1F;
            }  

            pszOut[iDst++] = leader | 0x80;
            pszOut[iDst++] = trailer | 0x80;
        }
        else
        {
            /* We should never get here unless a double-byte pair was 
             * truncated... but just in case...
             */
            pszOut[iDst++] = *pszLine;
        }

    }

    pszOut[iDst] = '\0';

    return psDBCSInfo->pszDBCSBuf;
}


/**********************************************************************
 *                          _AVCArcDBCS2JapaneseShiftJIS()
 *
 * Convert string from coverage DBCS (EUC) to Japanese Shift-JIS.
 *
 * We know that binary coverages use a custom EUC encoding for japanese
 * which is EUC + all Katakana chars are prefixed with 0x8e.  So this
 * function just does a simple conversion.
 **********************************************************************/
static const char *_AVCArcDBCS2JapaneseShiftJIS(AVCDBCSInfo *psDBCSInfo, 
                                                const unsigned char *pszLine,
                                                int nMaxOutputLen)
{
    unsigned char *pszOut;
    int iDst;

    pszOut = psDBCSInfo->pszDBCSBuf;

    for(iDst=0; *pszLine && iDst < nMaxOutputLen; pszLine++)
    {
        if (IS_ASCII(*pszLine))
        {
            /* No transformation required for ASCII */
            pszOut[iDst++] = *pszLine;            
        }
        else if (*pszLine == 0x8e && *(pszLine+1))
        {
            pszLine++;  /* Flush the 0x8e */
            pszOut[iDst++] = *pszLine;
        }
        else if (*(pszLine+1))
        {
            /* This is a pair of EUC chars... convert them to Shift-JIS 
             */
            unsigned char leader, trailer;
            leader  = *(pszLine++) & 0x7F;
            trailer = *pszLine & 0x7F;

            if((leader & 0x01) != 0)    trailer += 0x1F;
            else                        trailer += 0x7D;
            if(trailer >= 0x7F)         trailer ++;
    
            leader = ((leader - 0x21) >> 1) + 0x81;
            if(leader > 0x9F)          leader += 0x40;

            pszOut[iDst++] = leader;
            pszOut[iDst++] = trailer;
        }
        else
        {
            /* We should never get here unless a double-byte pair was 
             * truncated... but just in case...
             */
            pszOut[iDst++] = *pszLine;
        }

    }

    pszOut[iDst] = '\0';

    return psDBCSInfo->pszDBCSBuf;
}