blob: 219d792f1a36631e48af7d0a8fd110eea470b165 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
// **********************************************************************
//
// Copyright (c) 2003-2014 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
#include <Ice/Ice.h>
#include <StringConverterI.h>
using namespace std;
Demo::StringConverterI::StringConverterI()
{
}
Demo::StringConverterI::~StringConverterI()
{
}
Ice::Byte*
Demo::StringConverterI::toUTF8(const char* sourceStart, const char* sourceEnd, Ice::UTF8Buffer& buffer) const
{
size_t inputSize = static_cast<size_t>(sourceEnd - sourceStart);
size_t chunkSize = std::max<size_t>(inputSize, 6);
size_t outputBytesLeft = chunkSize;
Ice::Byte* targetStart = buffer.getMoreBytes(chunkSize, 0);
size_t offset = 0;
for(unsigned int i = 0; i < inputSize; ++i)
{
unsigned char byte = sourceStart[i];
if(byte <= 0x7F)
{
if(outputBytesLeft == 0)
{
targetStart = buffer.getMoreBytes(chunkSize, targetStart + chunkSize);
offset = 0;
}
targetStart[offset] = byte;
++offset;
--outputBytesLeft;
}
else
{
if(outputBytesLeft <= 1)
{
targetStart = buffer.getMoreBytes(chunkSize, targetStart + chunkSize - outputBytesLeft);
offset = 0;
}
targetStart[offset] = 0xC0 | ((byte & 0xC0) >> 6);
targetStart[offset + 1] = 0x80 | (byte & 0x3F);
offset += 2;
outputBytesLeft -= 2;
}
}
return targetStart + offset;
}
void
Demo::StringConverterI::fromUTF8(const Ice::Byte* sourceStart, const Ice::Byte* sourceEnd,
string& target) const
{
size_t inSize = static_cast<size_t>(sourceEnd - sourceStart);
target.resize(inSize);
unsigned int targetIndex = 0;
unsigned int i = 0;
while(i < inSize)
{
if((sourceStart[i] & 0xC0) == 0xC0)
{
if(i + 1 >= inSize)
{
throw Ice::StringConversionException(__FILE__, __LINE__, "UTF-8 string source exhausted");
}
target[targetIndex] = (sourceStart[i] & 0x03) << 6;
target[targetIndex] = target[targetIndex] | (sourceStart[i + 1] & 0x3F);
i += 2;
}
else
{
target[targetIndex] = sourceStart[i];
++i;
}
++targetIndex;
}
target.resize(targetIndex);
}
|