other-licenses/7zstub/src/Common/UTFConvert.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // UTFConvert.cpp
michael@0 2
michael@0 3 #include "StdAfx.h"
michael@0 4
michael@0 5 #include "UTFConvert.h"
michael@0 6 #include "Types.h"
michael@0 7
michael@0 8 static Byte kUtf8Limits[5] = { 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
michael@0 9
michael@0 10 // These functions are for UTF8 <-> UTF16 conversion.
michael@0 11
michael@0 12 bool ConvertUTF8ToUnicode(const AString &src, UString &dest)
michael@0 13 {
michael@0 14 dest.Empty();
michael@0 15 for(int i = 0; i < src.Length();)
michael@0 16 {
michael@0 17 Byte c = (Byte)src[i++];
michael@0 18 if (c < 0x80)
michael@0 19 {
michael@0 20 dest += (wchar_t)c;
michael@0 21 continue;
michael@0 22 }
michael@0 23 if(c < 0xC0)
michael@0 24 return false;
michael@0 25 int numAdds;
michael@0 26 for (numAdds = 1; numAdds < 5; numAdds++)
michael@0 27 if (c < kUtf8Limits[numAdds])
michael@0 28 break;
michael@0 29 UInt32 value = (c - kUtf8Limits[numAdds - 1]);
michael@0 30 do
michael@0 31 {
michael@0 32 if (i >= src.Length())
michael@0 33 return false;
michael@0 34 Byte c2 = (Byte)src[i++];
michael@0 35 if (c2 < 0x80 || c2 >= 0xC0)
michael@0 36 return false;
michael@0 37 value <<= 6;
michael@0 38 value |= (c2 - 0x80);
michael@0 39 numAdds--;
michael@0 40 }
michael@0 41 while(numAdds > 0);
michael@0 42 if (value < 0x10000)
michael@0 43 dest += (wchar_t)(value);
michael@0 44 else
michael@0 45 {
michael@0 46 value -= 0x10000;
michael@0 47 if (value >= 0x100000)
michael@0 48 return false;
michael@0 49 dest += (wchar_t)(0xD800 + (value >> 10));
michael@0 50 dest += (wchar_t)(0xDC00 + (value & 0x3FF));
michael@0 51 }
michael@0 52 }
michael@0 53 return true;
michael@0 54 }
michael@0 55
michael@0 56 bool ConvertUnicodeToUTF8(const UString &src, AString &dest)
michael@0 57 {
michael@0 58 dest.Empty();
michael@0 59 for(int i = 0; i < src.Length();)
michael@0 60 {
michael@0 61 UInt32 value = (UInt32)src[i++];
michael@0 62 if (value < 0x80)
michael@0 63 {
michael@0 64 dest += (char)value;
michael@0 65 continue;
michael@0 66 }
michael@0 67 if (value >= 0xD800 && value < 0xE000)
michael@0 68 {
michael@0 69 if (value >= 0xDC00)
michael@0 70 return false;
michael@0 71 if (i >= src.Length())
michael@0 72 return false;
michael@0 73 UInt32 c2 = (UInt32)src[i++];
michael@0 74 if (c2 < 0xDC00 || c2 >= 0xE000)
michael@0 75 return false;
michael@0 76 value = ((value - 0xD800) << 10) | (c2 - 0xDC00);
michael@0 77 }
michael@0 78 int numAdds;
michael@0 79 for (numAdds = 1; numAdds < 5; numAdds++)
michael@0 80 if (value < (((UInt32)1) << (numAdds * 5 + 6)))
michael@0 81 break;
michael@0 82 dest += (char)(kUtf8Limits[numAdds - 1] + (value >> (6 * numAdds)));
michael@0 83 do
michael@0 84 {
michael@0 85 numAdds--;
michael@0 86 dest += (char)(0x80 + ((value >> (6 * numAdds)) & 0x3F));
michael@0 87 }
michael@0 88 while(numAdds > 0);
michael@0 89 }
michael@0 90 return true;
michael@0 91 }

mercurial