Reactos
at master 138 lines 3.7 kB view raw
1/*** 2*tojisjms.c: Converts JIS to JMS code, and JMS to JIS code. 3* 4* Copyright (c) Microsoft Corporation. All rights reserved. 5* 6*Purpose: 7* Convert JIS code into Microsoft Kanji code, and vice versa. 8* 9*******************************************************************************/ 10#ifndef _MBCS 11 #error This file should only be compiled with _MBCS defined 12#endif 13 14#include <corecrt_internal_mbstring.h> 15#include <locale.h> 16 17/*** 18*unsigned int _mbcjistojms(c) - Converts JIS code to Microsoft Kanji Code. 19* 20*Purpose: 21* Convert JIS code to Microsoft Kanji code. 22* 23*Entry: 24* unsigned int c - JIS code to be converted. First byte is the upper 25* 8 bits, and second is the lower 8 bits. 26* 27*Exit: 28* Returns related Microsoft Kanji Code. First byte is the upper 8 bits 29* and second byte is the lower 8 bits. 30* 31*Exceptions: 32* If c is out of range, _mbcjistojms returns zero. 33* 34*******************************************************************************/ 35 36extern "C" unsigned int __cdecl _mbcjistojms_l( 37 unsigned int c, 38 _locale_t plocinfo 39 ) 40{ 41 unsigned int h, l; 42 _LocaleUpdate _loc_update(plocinfo); 43 44 if (_loc_update.GetLocaleT()->mbcinfo->mbcodepage != _KANJI_CP) 45 return (c); 46 47 h = (c >> 8) & 0xff; 48 l = c & 0xff; 49 if (h < 0x21 || h > 0x7e || l < 0x21 || l > 0x7e) 50 { 51 errno = EILSEQ; 52 return 0; 53 } 54 if (h & 0x01) { /* first byte is odd */ 55 if (l <= 0x5f) 56 l += 0x1f; 57 else 58 l += 0x20; 59 } 60 else 61 l += 0x7e; 62 63 h = ((h - 0x21) >> 1) + 0x81; 64 if (h > 0x9f) 65 h += 0x40; 66 return (h << 8) | l; 67} 68extern "C" unsigned int (__cdecl _mbcjistojms)( 69 unsigned int c 70 ) 71{ 72 return _mbcjistojms_l(c, nullptr); 73} 74 75 76/*** 77*unsigned int _mbcjmstojis(c) - Converts Microsoft Kanji code into JIS code. 78* 79*Purpose: 80* To convert Microsoft Kanji code into JIS code. 81* 82*Entry: 83* unsigned int c - Microsoft Kanji code to be converted. First byte is 84* the upper 8 bits, and the second is the lower 8 bits. 85* 86*Exit: 87* Returns related JIS Code. First byte is the upper 8 bits and the second 88* byte is the lower 8 bits. If c is out of range, return zero. 89* 90*Exceptions: 91* 92*******************************************************************************/ 93 94extern "C" unsigned int __cdecl _mbcjmstojis_l( 95 unsigned int c, 96 _locale_t plocinfo 97 ) 98{ 99 unsigned int h, l; 100 _LocaleUpdate _loc_update(plocinfo); 101 102 if ( _loc_update.GetLocaleT()->mbcinfo->mbcodepage != _KANJI_CP ) 103 return (c); 104 105 h = (c >> 8) & 0xff; 106 l = c & 0xff; 107 108 /* make sure input is valid shift-JIS */ 109 if ( (!(_ismbblead_l(h, _loc_update.GetLocaleT()))) || (!(_ismbbtrail_l(l, _loc_update.GetLocaleT()))) ) 110 { 111 errno = EILSEQ; 112 return 0; 113 } 114 115 h -= (h >= 0xa0) ? 0xc1 : 0x81; 116 if(l >= 0x9f) { 117 c = (h << 9) + 0x2200; 118 c |= l - 0x7e; 119 } else { 120 c = (h << 9) + 0x2100; 121 c |= l - ((l <= 0x7e) ? 0x1f : 0x20); 122 } 123 124 /* not all shift-JIS maps to JIS, so make sure output is valid */ 125 if ( (c>0x7E7E) || (c<0x2121) || ((c&0xFF)>0x7E) || ((c&0xFF)<0x21) ) 126 { 127 errno = EILSEQ; 128 return 0; 129 } 130 131 return c; 132} 133extern "C" unsigned int (__cdecl _mbcjmstojis)( 134 unsigned int c 135 ) 136{ 137 return _mbcjmstojis_l(c, nullptr); 138}