WebKitTextCodec.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 "KURL.h"
  21. #include "TextCodecICU.h"
  22. #include <BlackBerryPlatformString.h>
  23. #include <wtf/Vector.h>
  24. #include <wtf/text/Base64.h>
  25. #include <wtf/text/CString.h>
  26. #include <wtf/text/WTFString.h>
  27. using WebCore::TextEncoding;
  28. using WebCore::TextCodecICU;
  29. namespace BlackBerry {
  30. namespace WebKit {
  31. #define COMPILE_ASSERT_MATCHING_ENUM(webkitName, wtfName) \
  32. COMPILE_ASSERT(static_cast<int>(webkitName) == static_cast<int>(WTF::wtfName), mismatchingEnums)
  33. COMPILE_ASSERT_MATCHING_ENUM(Base64FailOnInvalidCharacter, Base64FailOnInvalidCharacter);
  34. COMPILE_ASSERT_MATCHING_ENUM(Base64IgnoreWhitespace, Base64IgnoreWhitespace);
  35. COMPILE_ASSERT_MATCHING_ENUM(Base64IgnoreInvalidCharacters, Base64IgnoreInvalidCharacters);
  36. // FIXME: Base64InsertCRLF should be Base64InsertLFs. WTF::encodeBase64 doesn't insert CR.
  37. COMPILE_ASSERT_MATCHING_ENUM(Base64DoNotInsertCRLF, Base64DoNotInsertLFs);
  38. COMPILE_ASSERT_MATCHING_ENUM(Base64InsertCRLF, Base64InsertLFs);
  39. bool isSameEncoding(const char* encoding1, const char* encoding2)
  40. {
  41. return TextEncoding(encoding1) == TextEncoding(encoding2);
  42. }
  43. bool isASCIICompatibleEncoding(const char* encoding)
  44. {
  45. TextEncoding textEncoding(encoding);
  46. if (!textEncoding.isValid())
  47. return false;
  48. // Check the most common encodings first
  49. if (textEncoding == WebCore::UTF8Encoding() || textEncoding == WebCore::Latin1Encoding() || textEncoding == WebCore::ASCIIEncoding())
  50. return true;
  51. String lowercasedEncoding = String(encoding).lower();
  52. // This is slow and could easily be optimized by directly inspecting encoding[i].
  53. if (lowercasedEncoding.startsWith("iso-8859")
  54. || lowercasedEncoding.startsWith("windows")
  55. || lowercasedEncoding.startsWith("euc-jp")
  56. || lowercasedEncoding.startsWith("euc-kr"))
  57. return true;
  58. return false;
  59. }
  60. TranscodeResult transcode(const char* sourceEncoding, const char* targetEncoding, const char*& sourceStart, int sourceLength, char*& targetStart, unsigned targetLength)
  61. {
  62. TextEncoding textEncodingSource(sourceEncoding);
  63. if (!textEncodingSource.isValid())
  64. return SourceEncodingUnsupported;
  65. TextEncoding textEncodingTarget(targetEncoding);
  66. if (!textEncodingTarget.isValid())
  67. return TargetEncodingUnsupported;
  68. bool sawError = false;
  69. String ucs2 = TextCodecICU(textEncodingSource).decode(sourceStart, sourceLength, true, true, sawError);
  70. if (sawError)
  71. return SourceBroken;
  72. CString encoded = TextCodecICU(textEncodingTarget).encode(ucs2.characters(), ucs2.length(), WebCore::EntitiesForUnencodables);
  73. if (encoded.length() > targetLength)
  74. return TargetBufferInsufficient;
  75. strncpy(targetStart, encoded.data(), encoded.length());
  76. targetStart += encoded.length();
  77. return Success;
  78. }
  79. bool base64Decode(const BlackBerry::Platform::String& base64, std::vector<char>& binary, Base64DecodePolicy policy)
  80. {
  81. Vector<char> result;
  82. if (!WTF::base64Decode(base64.c_str(), base64.length(), result, static_cast<WTF::Base64DecodePolicy>(policy)))
  83. return false;
  84. binary.insert(binary.begin(), result.begin(), result.end());
  85. return true;
  86. }
  87. bool base64Encode(const std::vector<char>& binary, BlackBerry::Platform::String& base64, Base64EncodePolicy policy)
  88. {
  89. Vector<char> result;
  90. result.append(&binary[0], binary.size());
  91. WTF::base64Encode(&binary[0], binary.size(), result, static_cast<WTF::Base64EncodePolicy>(policy));
  92. base64 = BlackBerry::Platform::String::fromAscii(&result[0], result.size());
  93. return true;
  94. }
  95. void unescapeURL(const BlackBerry::Platform::String& escaped, BlackBerry::Platform::String& url)
  96. {
  97. url = WebCore::decodeURLEscapeSequences(escaped);
  98. }
  99. void escapeURL(const BlackBerry::Platform::String& url, BlackBerry::Platform::String& escaped)
  100. {
  101. escaped = WebCore::encodeWithURLEscapeSequences(url);
  102. }
  103. } // namespace WebKit
  104. } // namespace BlackBerry