-
Notifications
You must be signed in to change notification settings - Fork 2
/
sjis.c
executable file
·86 lines (70 loc) · 2.21 KB
/
sjis.c
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
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include "sjis.h"
static unsigned int *LookupTable;
static unsigned int NumLookupTableEntries;
int SetSJISToUnicodeLookupTable(void *table, unsigned int TableLength)
{
LookupTable = table;
NumLookupTableEntries = TableLength / sizeof(unsigned int) / 2;
return 0;
}
wchar_t ConvertSJISToUnicodeChar(unsigned short int SourceChar)
{
unsigned int index;
wchar_t UnicodeChar;
if (SourceChar < 0x80) {
UnicodeChar = SourceChar;
} else {
UnicodeChar = UNICODE_REPLACEMENT_CHAR;
for (index = 0; index < NumLookupTableEntries; index++) {
if (LookupTable[index * 2 + 0] == SourceChar) {
UnicodeChar = LookupTable[index * 2 + 1];
break;
}
}
}
return UnicodeChar;
}
int SJISToUnicode(const unsigned char *SJISStringIn, int length, wchar_t *UnicodeStringOut, unsigned int NumUChars)
{
unsigned int i, NumChars;
const char *SourceString;
unsigned short int SJISCharacter;
int result;
for (result = 0, i = 0, SourceString = SJISStringIn, NumChars = 0; NumChars < NumUChars; i += GetSJISCharLength(SJISCharacter), NumChars++) {
if (length > 0 && i >= length)
break;
if ((SJISCharacter = GetNextSJISChar(&SourceString)) == '\0') {
UnicodeStringOut[NumChars++] = '\0';
break;
}
UnicodeStringOut[NumChars] = ConvertSJISToUnicodeChar(SJISCharacter);
}
return (result == 0 ? NumChars : result);
}
int GetSJISCharLengthFromString(const char *SJISCharacter)
{
return ((SJISCharacter[0] & 0x80) ? 2 : 1);
}
int GetSJISCharLength(unsigned short int SJISCharacter)
{
return ((SJISCharacter & 0x8000) ? 2 : 1);
}
unsigned short int GetNextSJISChar(const char **string)
{
int CharLength;
unsigned short int NextSJISChar;
CharLength = GetSJISCharLengthFromString(*string);
if (CharLength == 1) {
NextSJISChar = (*string)[0];
(*string)++;
} else {
((unsigned char *)&NextSJISChar)[1] = (*string)[0];
((unsigned char *)&NextSJISChar)[0] = (*string)[1];
(*string) += 2;
}
return NextSJISChar;
}