nsUnicodeToUTF16.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "nsUnicodeToUTF16.h"
  6. #include "mozilla/CheckedInt.h"
  7. #include <string.h>
  8. NS_IMETHODIMP nsUnicodeToUTF16BE::Convert(const char16_t * aSrc, int32_t * aSrcLength,
  9. char * aDest, int32_t * aDestLength)
  10. {
  11. int32_t srcInLen = *aSrcLength;
  12. int32_t destInLen = *aDestLength;
  13. int32_t srcOutLen = 0;
  14. int32_t destOutLen = 0;
  15. int32_t copyCharLen;
  16. char16_t *p = (char16_t*)aDest;
  17. // Handle BOM if necessary
  18. if (0!=mBOM) {
  19. if (destInLen < 2) {
  20. goto needmoreoutput;
  21. }
  22. *p++ = mBOM;
  23. mBOM = 0;
  24. destOutLen +=2;
  25. }
  26. // find out the length of copy
  27. copyCharLen = srcInLen;
  28. if (copyCharLen > (destInLen - destOutLen) / 2) {
  29. copyCharLen = (destInLen - destOutLen) / 2;
  30. }
  31. // copy the data by swaping
  32. CopyData((char*)p , aSrc, copyCharLen );
  33. srcOutLen += copyCharLen;
  34. destOutLen += copyCharLen * 2;
  35. if (copyCharLen < srcInLen) {
  36. goto needmoreoutput;
  37. }
  38. *aSrcLength = srcOutLen;
  39. *aDestLength = destOutLen;
  40. return NS_OK;
  41. needmoreoutput:
  42. *aSrcLength = srcOutLen;
  43. *aDestLength = destOutLen;
  44. return NS_OK_UENC_MOREOUTPUT;
  45. }
  46. NS_IMETHODIMP nsUnicodeToUTF16BE::GetMaxLength(const char16_t * aSrc, int32_t aSrcLength,
  47. int32_t * aDestLength)
  48. {
  49. mozilla::CheckedInt32 length = aSrcLength;
  50. if (0 != mBOM) {
  51. length += 1;
  52. }
  53. length *= 2;
  54. if (!length.isValid()) {
  55. return NS_ERROR_OUT_OF_MEMORY;
  56. }
  57. *aDestLength = length.value();
  58. return NS_OK_UENC_EXACTLENGTH;
  59. }
  60. NS_IMETHODIMP nsUnicodeToUTF16BE::Finish(char * aDest, int32_t * aDestLength)
  61. {
  62. if (0 != mBOM) {
  63. if (*aDestLength >= 2) {
  64. *((char16_t*)aDest)= mBOM;
  65. mBOM=0;
  66. *aDestLength = 2;
  67. return NS_OK;
  68. } else {
  69. *aDestLength = 0;
  70. return NS_OK; // xxx should be error
  71. }
  72. } else {
  73. *aDestLength = 0;
  74. return NS_OK;
  75. }
  76. }
  77. NS_IMETHODIMP nsUnicodeToUTF16BE::Reset()
  78. {
  79. mBOM = 0;
  80. return NS_OK;
  81. }
  82. NS_IMETHODIMP nsUnicodeToUTF16BE::SetOutputErrorBehavior(int32_t aBehavior,
  83. nsIUnicharEncoder * aEncoder, char16_t aChar)
  84. {
  85. return NS_OK;
  86. }
  87. NS_IMETHODIMP nsUnicodeToUTF16BE::CopyData(char* aDest, const char16_t* aSrc, int32_t aLen )
  88. {
  89. mozilla::NativeEndian::copyAndSwapToBigEndian(aDest, aSrc, aLen);
  90. return NS_OK;
  91. }
  92. NS_IMETHODIMP nsUnicodeToUTF16LE::CopyData(char* aDest, const char16_t* aSrc, int32_t aLen )
  93. {
  94. mozilla::NativeEndian::copyAndSwapToLittleEndian(aDest, aSrc, aLen);
  95. return NS_OK;
  96. }