OLD | NEW |
| (Empty) |
1 // Copyright 2014 PDFium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | |
6 | |
7 #include "xfa/src/fgas/crt/fgas_codepage.h" | |
8 | |
9 void FX_SwapByteOrder(FX_WCHAR* pStr, int32_t iLength) { | |
10 FXSYS_assert(pStr != NULL); | |
11 if (iLength < 0) { | |
12 iLength = FXSYS_wcslen(pStr); | |
13 } | |
14 FX_WORD wch; | |
15 if (sizeof(FX_WCHAR) > 2) { | |
16 while (iLength-- > 0) { | |
17 wch = (FX_WORD)*pStr; | |
18 wch = (wch >> 8) | (wch << 8); | |
19 wch &= 0x00FF; | |
20 *pStr++ = wch; | |
21 } | |
22 } else { | |
23 while (iLength-- > 0) { | |
24 wch = (FX_WORD)*pStr; | |
25 wch = (wch >> 8) | (wch << 8); | |
26 *pStr++ = wch; | |
27 } | |
28 } | |
29 } | |
30 void FX_SwapByteOrderCopy(const FX_WCHAR* pSrc, | |
31 FX_WCHAR* pDst, | |
32 int32_t iLength) { | |
33 FXSYS_assert(pSrc != NULL && pDst != NULL); | |
34 if (iLength < 0) { | |
35 iLength = FXSYS_wcslen(pSrc); | |
36 } | |
37 FX_WORD wch; | |
38 if (sizeof(FX_WCHAR) > 2) { | |
39 while (iLength-- > 0) { | |
40 wch = (FX_WORD)*pSrc++; | |
41 wch = (wch >> 8) | (wch << 8); | |
42 wch &= 0x00FF; | |
43 *pDst++ = wch; | |
44 } | |
45 } else { | |
46 while (iLength-- > 0) { | |
47 wch = (FX_WORD)*pSrc++; | |
48 wch = (wch >> 8) | (wch << 8); | |
49 *pDst++ = wch; | |
50 } | |
51 } | |
52 } | |
53 void FX_UTF16ToWChar(void* pBuffer, int32_t iLength) { | |
54 FXSYS_assert(pBuffer != NULL && iLength > 0); | |
55 if (sizeof(FX_WCHAR) == 2) { | |
56 return; | |
57 } | |
58 FX_WORD* pSrc = (FX_WORD*)pBuffer; | |
59 FX_WCHAR* pDst = (FX_WCHAR*)pBuffer; | |
60 while (--iLength >= 0) { | |
61 pDst[iLength] = (FX_WCHAR)pSrc[iLength]; | |
62 } | |
63 } | |
64 void FX_UTF16ToWCharCopy(const FX_WORD* pUTF16, | |
65 FX_WCHAR* pWChar, | |
66 int32_t iLength) { | |
67 FXSYS_assert(pUTF16 != NULL && pWChar != NULL && iLength > 0); | |
68 if (sizeof(FX_WCHAR) == 2) { | |
69 FXSYS_memcpy(pWChar, pUTF16, iLength * sizeof(FX_WCHAR)); | |
70 } else { | |
71 while (--iLength >= 0) { | |
72 pWChar[iLength] = (FX_WCHAR)pUTF16[iLength]; | |
73 } | |
74 } | |
75 } | |
76 void FX_WCharToUTF16(void* pBuffer, int32_t iLength) { | |
77 FXSYS_assert(pBuffer != NULL && iLength > 0); | |
78 if (sizeof(FX_WCHAR) == 2) { | |
79 return; | |
80 } | |
81 const FX_WCHAR* pSrc = (const FX_WCHAR*)pBuffer; | |
82 FX_WORD* pDst = (FX_WORD*)pBuffer; | |
83 while (--iLength >= 0) { | |
84 *pDst++ = (FX_WORD)*pSrc++; | |
85 } | |
86 } | |
87 void FX_WCharToUTF16Copy(const FX_WCHAR* pWChar, | |
88 FX_WORD* pUTF16, | |
89 int32_t iLength) { | |
90 FXSYS_assert(pWChar != NULL && pUTF16 != NULL && iLength > 0); | |
91 if (sizeof(FX_WCHAR) == 2) { | |
92 FXSYS_memcpy(pUTF16, pWChar, iLength * sizeof(FX_WCHAR)); | |
93 } else { | |
94 while (--iLength >= 0) { | |
95 *pUTF16++ = (FX_WORD)*pWChar++; | |
96 } | |
97 } | |
98 } | |
99 inline FX_DWORD FX_DWordFromBytes(const uint8_t* pStr) { | |
100 return FXBSTR_ID(pStr[3], pStr[2], pStr[1], pStr[0]); | |
101 } | |
102 inline FX_WORD FX_WordFromBytes(const uint8_t* pStr) { | |
103 return (pStr[1] << 8 | pStr[0]); | |
104 } | |
105 int32_t FX_DecodeString(FX_WORD wCodePage, | |
106 const FX_CHAR* pSrc, | |
107 int32_t* pSrcLen, | |
108 FX_WCHAR* pDst, | |
109 int32_t* pDstLen, | |
110 FX_BOOL bErrBreak) { | |
111 if (wCodePage == FX_CODEPAGE_UTF8) { | |
112 return FX_UTF8Decode(pSrc, pSrcLen, pDst, pDstLen); | |
113 } | |
114 return -1; | |
115 } | |
116 int32_t FX_UTF8Decode(const FX_CHAR* pSrc, | |
117 int32_t* pSrcLen, | |
118 FX_WCHAR* pDst, | |
119 int32_t* pDstLen) { | |
120 if (pSrcLen == NULL || pDstLen == NULL) { | |
121 return -1; | |
122 } | |
123 int32_t iSrcLen = *pSrcLen; | |
124 if (iSrcLen < 1) { | |
125 *pSrcLen = *pDstLen = 0; | |
126 return 1; | |
127 } | |
128 int32_t iDstLen = *pDstLen; | |
129 FX_BOOL bValidDst = (pDst != NULL && iDstLen > 0); | |
130 FX_DWORD dwCode = 0; | |
131 int32_t iPending = 0; | |
132 int32_t iSrcNum = 0, iDstNum = 0; | |
133 int32_t k = 0; | |
134 int32_t iIndex = 0; | |
135 k = 1; | |
136 while (iIndex < iSrcLen) { | |
137 uint8_t byte = (uint8_t) * (pSrc + iIndex); | |
138 if (byte < 0x80) { | |
139 iPending = 0; | |
140 k = 1; | |
141 iDstNum++; | |
142 iSrcNum += k; | |
143 if (bValidDst) { | |
144 *pDst++ = byte; | |
145 if (iDstNum >= iDstLen) { | |
146 break; | |
147 } | |
148 } | |
149 } else if (byte < 0xc0) { | |
150 if (iPending < 1) { | |
151 break; | |
152 } | |
153 iPending--; | |
154 dwCode |= (byte & 0x3f) << (iPending * 6); | |
155 if (iPending == 0) { | |
156 iDstNum++; | |
157 iSrcNum += k; | |
158 if (bValidDst) { | |
159 *pDst++ = dwCode; | |
160 if (iDstNum >= iDstLen) { | |
161 break; | |
162 } | |
163 } | |
164 } | |
165 } else if (byte < 0xe0) { | |
166 iPending = 1; | |
167 k = 2; | |
168 dwCode = (byte & 0x1f) << 6; | |
169 } else if (byte < 0xf0) { | |
170 iPending = 2; | |
171 k = 3; | |
172 dwCode = (byte & 0x0f) << 12; | |
173 } else if (byte < 0xf8) { | |
174 iPending = 3; | |
175 k = 4; | |
176 dwCode = (byte & 0x07) << 18; | |
177 } else if (byte < 0xfc) { | |
178 iPending = 4; | |
179 k = 5; | |
180 dwCode = (byte & 0x03) << 24; | |
181 } else if (byte < 0xfe) { | |
182 iPending = 5; | |
183 k = 6; | |
184 dwCode = (byte & 0x01) << 30; | |
185 } else { | |
186 break; | |
187 } | |
188 iIndex++; | |
189 } | |
190 *pSrcLen = iSrcNum; | |
191 *pDstLen = iDstNum; | |
192 return 1; | |
193 } | |
OLD | NEW |