diff options
Diffstat (limited to 'xfa/src/fgas/src/crt/fx_encode.cpp')
-rw-r--r-- | xfa/src/fgas/src/crt/fx_encode.cpp | 189 |
1 files changed, 189 insertions, 0 deletions
diff --git a/xfa/src/fgas/src/crt/fx_encode.cpp b/xfa/src/fgas/src/crt/fx_encode.cpp new file mode 100644 index 0000000000..892b5a9059 --- /dev/null +++ b/xfa/src/fgas/src/crt/fx_encode.cpp @@ -0,0 +1,189 @@ +// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "../fgas_base.h"
+#include "fx_codepage.h"
+void FX_SwapByteOrder(FX_LPWSTR pStr, FX_INT32 iLength)
+{
+ FXSYS_assert(pStr != NULL);
+ if (iLength < 0) {
+ iLength = FXSYS_wcslen(pStr);
+ }
+ FX_WORD wch;
+ if (sizeof(FX_WCHAR) > 2) {
+ while (iLength -- > 0) {
+ wch = (FX_WORD) * pStr;
+ wch = (wch >> 8) | (wch << 8);
+ wch &= 0x00FF;
+ *pStr ++ = wch;
+ }
+ } else {
+ while (iLength -- > 0) {
+ wch = (FX_WORD) * pStr;
+ wch = (wch >> 8) | (wch << 8);
+ *pStr ++ = wch;
+ }
+ }
+}
+void FX_SwapByteOrderCopy(FX_LPCWSTR pSrc, FX_LPWSTR pDst, FX_INT32 iLength)
+{
+ FXSYS_assert(pSrc != NULL && pDst != NULL);
+ if (iLength < 0) {
+ iLength = FXSYS_wcslen(pSrc);
+ }
+ FX_WORD wch;
+ if (sizeof(FX_WCHAR) > 2) {
+ while (iLength -- > 0) {
+ wch = (FX_WORD) * pSrc ++;
+ wch = (wch >> 8) | (wch << 8);
+ wch &= 0x00FF;
+ *pDst ++ = wch;
+ }
+ } else {
+ while (iLength -- > 0) {
+ wch = (FX_WORD) * pSrc ++;
+ wch = (wch >> 8) | (wch << 8);
+ *pDst ++ = wch;
+ }
+ }
+}
+void FX_UTF16ToWChar(FX_LPVOID pBuffer, FX_INT32 iLength)
+{
+ FXSYS_assert(pBuffer != NULL && iLength > 0);
+ if (sizeof(FX_WCHAR) == 2) {
+ return;
+ }
+ FX_WORD *pSrc = (FX_WORD*)pBuffer;
+ FX_LPWSTR pDst = (FX_LPWSTR)pBuffer;
+ while (--iLength >= 0) {
+ pDst[iLength] = (FX_WCHAR)pSrc[iLength];
+ }
+}
+void FX_UTF16ToWCharCopy(const FX_WORD *pUTF16, FX_LPWSTR pWChar, FX_INT32 iLength)
+{
+ FXSYS_assert(pUTF16 != NULL && pWChar != NULL && iLength > 0);
+ if (sizeof(FX_WCHAR) == 2) {
+ FXSYS_memcpy(pWChar, pUTF16, iLength * sizeof(FX_WCHAR));
+ } else {
+ while (--iLength >= 0) {
+ pWChar[iLength] = (FX_WCHAR)pUTF16[iLength];
+ }
+ }
+}
+void FX_WCharToUTF16(FX_LPVOID pBuffer, FX_INT32 iLength)
+{
+ FXSYS_assert(pBuffer != NULL && iLength > 0);
+ if (sizeof(FX_WCHAR) == 2) {
+ return;
+ }
+ FX_LPCWSTR pSrc = (FX_LPCWSTR)pBuffer;
+ FX_WORD *pDst = (FX_WORD*)pBuffer;
+ while (--iLength >= 0) {
+ *pDst++ = (FX_WORD) * pSrc++;
+ }
+}
+void FX_WCharToUTF16Copy(FX_LPCWSTR pWChar, FX_WORD *pUTF16, FX_INT32 iLength)
+{
+ FXSYS_assert(pWChar != NULL && pUTF16 != NULL && iLength > 0);
+ if (sizeof(FX_WCHAR) == 2) {
+ FXSYS_memcpy(pUTF16, pWChar, iLength * sizeof(FX_WCHAR));
+ } else {
+ while (--iLength >= 0) {
+ *pUTF16++ = (FX_WORD) * pWChar++;
+ }
+ }
+}
+inline FX_DWORD FX_DWordFromBytes(FX_LPCBYTE pStr)
+{
+ return FXBSTR_ID(pStr[3], pStr[2], pStr[1], pStr[0]);
+}
+inline FX_WORD FX_WordFromBytes(FX_LPCBYTE pStr)
+{
+ return (pStr[1] << 8 | pStr[0]);
+}
+FX_INT32 FX_DecodeString(FX_WORD wCodePage, FX_LPCSTR pSrc, FX_INT32 *pSrcLen, FX_LPWSTR pDst, FX_INT32 *pDstLen, FX_BOOL bErrBreak)
+{
+ if (wCodePage == FX_CODEPAGE_UTF8) {
+ return FX_UTF8Decode(pSrc, pSrcLen, pDst, pDstLen);
+ }
+ return -1;
+}
+FX_INT32 FX_UTF8Decode(FX_LPCSTR pSrc, FX_INT32 *pSrcLen, FX_LPWSTR pDst, FX_INT32 *pDstLen)
+{
+ if (pSrcLen == NULL || pDstLen == NULL) {
+ return -1;
+ }
+ FX_INT32 iSrcLen = *pSrcLen;
+ if (iSrcLen < 1) {
+ *pSrcLen = *pDstLen = 0;
+ return 1;
+ }
+ FX_INT32 iDstLen = *pDstLen;
+ FX_BOOL bValidDst = (pDst != NULL && iDstLen > 0);
+ FX_DWORD dwCode = 0;
+ FX_INT32 iPending = 0;
+ FX_INT32 iSrcNum = 0, iDstNum = 0;
+ FX_INT32 k = 0;
+ FX_INT32 iIndex = 0;
+ k = 1;
+ while (iIndex < iSrcLen) {
+ FX_BYTE byte = (FX_BYTE) * (pSrc + iIndex);
+ if (byte < 0x80) {
+ iPending = 0;
+ k = 1;
+ iDstNum ++;
+ iSrcNum += k;
+ if (bValidDst) {
+ *pDst ++ = byte;
+ if (iDstNum >= iDstLen) {
+ break;
+ }
+ }
+ } else if (byte < 0xc0) {
+ if (iPending < 1) {
+ break;
+ }
+ iPending--;
+ dwCode |= (byte & 0x3f) << (iPending * 6);
+ if (iPending == 0) {
+ iDstNum ++;
+ iSrcNum += k;
+ if (bValidDst) {
+ *pDst ++ = dwCode;
+ if (iDstNum >= iDstLen) {
+ break;
+ }
+ }
+ }
+ } else if (byte < 0xe0) {
+ iPending = 1;
+ k = 2;
+ dwCode = (byte & 0x1f) << 6;
+ } else if (byte < 0xf0) {
+ iPending = 2;
+ k = 3;
+ dwCode = (byte & 0x0f) << 12;
+ } else if (byte < 0xf8) {
+ iPending = 3;
+ k = 4;
+ dwCode = (byte & 0x07) << 18;
+ } else if (byte < 0xfc) {
+ iPending = 4;
+ k = 5;
+ dwCode = (byte & 0x03) << 24;
+ } else if (byte < 0xfe) {
+ iPending = 5;
+ k = 6;
+ dwCode = (byte & 0x01) << 30;
+ } else {
+ break;
+ }
+ iIndex++;
+ }
+ *pSrcLen = iSrcNum;
+ *pDstLen = iDstNum;
+ return 1;
+}
|