PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/webkit-efl/Source/WebKit/blackberry/Api/WebKitTextCodec.cpp

https://review.tizen.org/git/
C++ | 137 lines | 89 code | 28 blank | 20 comment | 18 complexity | d2ba4bd8cc8684784e9d11aaab4cda3e MD5 | raw file
Possible License(s): GPL-3.0, AGPL-3.0, GPL-2.0, MPL-2.0, JSON, WTFPL, CC-BY-SA-4.0, CC-BY-3.0, BSD-3-Clause, LGPL-2.0, MPL-2.0-no-copyleft-exception, AGPL-1.0, 0BSD, Zlib, Unlicense, BSD-2-Clause, Apache-2.0, LGPL-3.0, ISC, MIT, CC-BY-SA-3.0, CC0-1.0, LGPL-2.1
  1. /*
  2. * Copyright (C) 2009, 2010, 2011 Research In Motion Limited. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "config.h"
  19. #include "WebKitTextCodec.h"
  20. #include "Base64.h"
  21. #include "KURL.h"
  22. #include "TextCodecICU.h"
  23. #include <wtf/Vector.h>
  24. #include <wtf/text/CString.h>
  25. #include <wtf/text/WTFString.h>
  26. using WebCore::TextEncoding;
  27. using WebCore::TextCodecICU;
  28. namespace BlackBerry {
  29. namespace WebKit {
  30. bool isSameEncoding(const char* encoding1, const char* encoding2)
  31. {
  32. return TextEncoding(encoding1) == TextEncoding(encoding2);
  33. }
  34. bool isASCIICompatibleEncoding(const char* encoding)
  35. {
  36. TextEncoding textEncoding(encoding);
  37. if (!textEncoding.isValid())
  38. return false;
  39. // Check the most common encodings first
  40. if (textEncoding == WebCore::UTF8Encoding() || textEncoding == WebCore::Latin1Encoding() || textEncoding == WebCore::ASCIIEncoding())
  41. return true;
  42. String lowercasedEncoding = String(encoding).lower();
  43. // This is slow and could easily be optimized by directly inspecting encoding[i].
  44. if (lowercasedEncoding.startsWith("iso-8859")
  45. || lowercasedEncoding.startsWith("windows")
  46. || lowercasedEncoding.startsWith("euc-jp")
  47. || lowercasedEncoding.startsWith("euc-kr"))
  48. return true;
  49. return false;
  50. }
  51. TranscodeResult transcode(const char* sourceEncoding, const char* targetEncoding, const char*& sourceStart, int sourceLength, char*& targetStart, unsigned int targetLength)
  52. {
  53. TextEncoding textEncodingSource(sourceEncoding);
  54. if (!textEncodingSource.isValid())
  55. return SourceEncodingUnsupported;
  56. TextEncoding textEncodingTarget(targetEncoding);
  57. if (!textEncodingTarget.isValid())
  58. return TargetEncodingUnsupported;
  59. bool sawError = false;
  60. String ucs2 = TextCodecICU(textEncodingSource).decode(sourceStart, sourceLength, true, true, sawError);
  61. if (sawError)
  62. return SourceBroken;
  63. CString encoded = TextCodecICU(textEncodingTarget).encode(ucs2.characters(), ucs2.length(), WebCore::EntitiesForUnencodables);
  64. if (encoded.length() > targetLength)
  65. return TargetBufferInsufficient;
  66. strncpy(targetStart, encoded.data(), encoded.length());
  67. targetStart += encoded.length();
  68. return Success;
  69. }
  70. WebCore::Base64DecodePolicy base64DecodePolicyForWebCore(Base64DecodePolicy policy)
  71. {
  72. // Must make sure Base64DecodePolicy is the same in WebKit and WebCore!
  73. return static_cast<WebCore::Base64DecodePolicy>(policy);
  74. }
  75. bool base64Decode(const std::string& base64, std::vector<char>& binary, Base64DecodePolicy policy)
  76. {
  77. Vector<char> result;
  78. if (!WebCore::base64Decode(base64.c_str(), base64.length(), result, base64DecodePolicyForWebCore(policy)))
  79. return false;
  80. binary.insert(binary.begin(), result.begin(), result.end());
  81. return true;
  82. }
  83. bool base64Encode(const std::vector<char>& binary, std::string& base64, Base64EncodePolicy policy)
  84. {
  85. Vector<char> result;
  86. result.append(&binary[0], binary.size());
  87. WebCore::base64Encode(&binary[0], binary.size(), result, Base64InsertCRLF == policy ? true : false);
  88. base64.clear();
  89. base64.append(&result[0], result.size());
  90. return true;
  91. }
  92. void unescapeURL(const std::string& escaped, std::string& url)
  93. {
  94. String escapedString(escaped.data(), escaped.length());
  95. String urlString = WebCore::decodeURLEscapeSequences(escapedString);
  96. CString utf8 = urlString.utf8();
  97. url.clear();
  98. url.append(utf8.data(), utf8.length());
  99. }
  100. void escapeURL(const std::string& url, std::string& escaped)
  101. {
  102. String urlString(url.data(), url.length());
  103. String escapedString = WebCore::encodeWithURLEscapeSequences(urlString);
  104. CString utf8 = escapedString.utf8();
  105. escaped.clear();
  106. escaped.append(utf8.data(), utf8.length());
  107. }
  108. } // namespace WebKit
  109. } // namespace BlackBerry