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 
6 #ifndef nsUTF16ToUnicode_h_
7 #define nsUTF16ToUnicode_h_
8 
9 #include "nsISupports.h"
10 #include "nsUCSupport.h"
11 
12 // internal base class
13 class nsUTF16ToUnicodeBase : public nsBasicDecoderSupport
14 {
15 protected:
16   // ctor accessible only by child classes
nsUTF16ToUnicodeBase()17   nsUTF16ToUnicodeBase() { Reset();}
18 
19   nsresult UTF16ConvertToUnicode(const char* aSrc,
20                                  int32_t* aSrcLength, char16_t* aDest,
21                                  int32_t* aDestLength, bool aSwapBytes);
22 
23 public:
24   //--------------------------------------------------------------------
25   // Subclassing of nsDecoderSupport class [declaration]
26 
27   MOZ_MUST_USE NS_IMETHOD GetMaxLength(const char* aSrc,
28                                        int32_t aSrcLength,
29                                        int32_t* aDestLength) override;
30   NS_IMETHOD Reset() override;
31 
32 protected:
33   uint8_t mState;
34   // to store an odd byte left over between runs
35   uint8_t mOddByte;
36   // to store an odd high surrogate left over between runs
37   char16_t mOddHighSurrogate;
38   // to store an odd low surrogate left over between runs
39   char16_t mOddLowSurrogate;
40 };
41 
42 // UTF-16 big endian
43 class nsUTF16BEToUnicode : public nsUTF16ToUnicodeBase
44 {
45 public:
46 
47   NS_IMETHOD Convert(const char* aSrc, int32_t* aSrcLength,
48       char16_t* aDest, int32_t* aDestLength);
49 };
50 
51 // UTF-16 little endian
52 class nsUTF16LEToUnicode : public nsUTF16ToUnicodeBase
53 {
54 public:
55 
56   NS_IMETHOD Convert(const char* aSrc, int32_t* aSrcLength,
57       char16_t* aDest, int32_t* aDestLength);
58 };
59 
60 // UTF-16 with BOM
61 class nsUTF16ToUnicode : public nsUTF16ToUnicodeBase
62 {
63 public:
64 
nsUTF16ToUnicode()65   nsUTF16ToUnicode() { Reset();}
66   NS_IMETHOD Convert(const char* aSrc, int32_t* aSrcLength,
67       char16_t* aDest, int32_t* aDestLength);
68 
69   NS_IMETHOD Reset();
70 
71 private:
72 
73   enum Endian {kUnknown, kBigEndian, kLittleEndian};
74   Endian  mEndian;
75   bool    mFoundBOM;
76 };
77 
78 #endif /* nsUTF16ToUnicode_h_ */
79