From e44c395c51518bafbf8f8466ea5a0f4b1f2b2efe Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Thu, 21 Jun 2018 13:05:48 -0400 Subject: QUtil::toUTF16 --- libqpdf/QUtil.cc | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'libqpdf/QUtil.cc') diff --git a/libqpdf/QUtil.cc b/libqpdf/QUtil.cc index 39118854..e2bc0bac 100644 --- a/libqpdf/QUtil.cc +++ b/libqpdf/QUtil.cc @@ -506,6 +506,41 @@ QUtil::toUTF8(unsigned long uval) return result; } +std::string +QUtil::toUTF16(unsigned long uval) +{ + std::string result; + if ((uval >= 0xd800) && (uval <= 0xdfff)) + { + result = "\xff\xfd"; + } + else if (uval <= 0xffff) + { + char out[2]; + out[0] = (uval & 0xff00) >> 8; + out[1] = (uval & 0xff); + result = std::string(out, 2); + } + else if (uval <= 0x10ffff) + { + char out[4]; + uval -= 0x10000; + unsigned short high = ((uval & 0xffc00) >> 10) + 0xd800; + unsigned short low = (uval & 0x3ff) + 0xdc00; + out[0] = (high & 0xff00) >> 8; + out[1] = (high & 0xff); + out[2] = (low & 0xff00) >> 8; + out[3] = (low & 0xff); + result = std::string(out, 4); + } + else + { + result = "\xff\xfd"; + } + + return result; +} + // Random data support long -- cgit v1.2.3-54-g00ecf