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/fgas/crt/fgas_codepage.h" | |
8 | |
9 void FX_SwapByteOrder(FX_WCHAR* pStr, int32_t iLength) { | |
10 ASSERT(pStr != NULL); | |
11 if (iLength < 0) { | |
12 iLength = FXSYS_wcslen(pStr); | |
13 } | |
14 uint16_t wch; | |
15 if (sizeof(FX_WCHAR) > 2) { | |
16 while (iLength-- > 0) { | |
17 wch = (uint16_t)*pStr; | |
18 wch = (wch >> 8) | (wch << 8); | |
19 wch &= 0x00FF; | |
20 *pStr++ = wch; | |
21 } | |
22 } else { | |
23 while (iLength-- > 0) { | |
24 wch = (uint16_t)*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 ASSERT(pSrc != NULL && pDst != NULL); | |
34 if (iLength < 0) { | |
35 iLength = FXSYS_wcslen(pSrc); | |
36 } | |
37 uint16_t wch; | |
38 if (sizeof(FX_WCHAR) > 2) { | |
39 while (iLength-- > 0) { | |
40 wch = (uint16_t)*pSrc++; | |
41 wch = (wch >> 8) | (wch << 8); | |
42 wch &= 0x00FF; | |
43 *pDst++ = wch; | |
44 } | |
45 } else { | |
46 while (iLength-- > 0) { | |
47 wch = (uint16_t)*pSrc++; | |
48 wch = (wch >> 8) | (wch << 8); | |
49 *pDst++ = wch; | |
50 } | |
51 } | |
52 } | |
53 void FX_UTF16ToWChar(void* pBuffer, int32_t iLength) { | |
54 ASSERT(pBuffer != NULL && iLength > 0); | |
55 if (sizeof(FX_WCHAR) == 2) { | |
56 return; | |
57 } | |
58 uint16_t* pSrc = (uint16_t*)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 uint16_t* pUTF16, | |
65 FX_WCHAR* pWChar, | |
66 int32_t iLength) { | |
67 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 ASSERT(pBuffer != NULL && iLength > 0); | |
78 if (sizeof(FX_WCHAR) == 2) { | |
79 return; | |
80 } | |
81 const FX_WCHAR* pSrc = (const FX_WCHAR*)pBuffer; | |
82 uint16_t* pDst = (uint16_t*)pBuffer; | |
83 while (--iLength >= 0) { | |
84 *pDst++ = (uint16_t)*pSrc++; | |
85 } | |
86 } | |
87 void FX_WCharToUTF16Copy(const FX_WCHAR* pWChar, | |
88 uint16_t* pUTF16, | |
89 int32_t iLength) { | |
90 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++ = (uint16_t)*pWChar++; | |
96 } | |
97 } | |
98 } | |
99 inline uint32_t FX_DWordFromBytes(const uint8_t* pStr) { | |
100 return FXBSTR_ID(pStr[3], pStr[2], pStr[1], pStr[0]); | |
101 } | |
102 inline uint16_t FX_WordFromBytes(const uint8_t* pStr) { | |
103 return (pStr[1] << 8 | pStr[0]); | |
104 } | |
105 int32_t FX_DecodeString(uint16_t 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 uint32_t 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 |