gfx/skia/trunk/src/ports/SkXMLParser_expat.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1
michael@0 2 /*
michael@0 3 * Copyright 2006 The Android Open Source Project
michael@0 4 *
michael@0 5 * Use of this source code is governed by a BSD-style license that can be
michael@0 6 * found in the LICENSE file.
michael@0 7 */
michael@0 8
michael@0 9
michael@0 10 #include "SkXMLParser.h"
michael@0 11 #include "SkString.h"
michael@0 12 #include "SkStream.h"
michael@0 13
michael@0 14 #include "expat.h"
michael@0 15
michael@0 16 #ifdef SK_BUILD_FOR_PPI
michael@0 17 #define CHAR_16_TO_9
michael@0 18 #endif
michael@0 19
michael@0 20 #if defined CHAR_16_TO_9
michael@0 21 inline size_t sk_wcslen(const short* char16) {
michael@0 22 const short* start = char16;
michael@0 23 while (*char16)
michael@0 24 char16++;
michael@0 25 return char16 - start;
michael@0 26 }
michael@0 27
michael@0 28 inline const char* ConvertUnicodeToChar(const short* ch16, size_t len, SkAutoMalloc& ch8Malloc) {
michael@0 29 char* ch8 = (char*) ch8Malloc.get();
michael@0 30 int index;
michael@0 31 for (index = 0; index < len; index++)
michael@0 32 ch8[index] = (char) ch16[index];
michael@0 33 ch8[index] = '\0';
michael@0 34 return ch8;
michael@0 35 }
michael@0 36 #endif
michael@0 37
michael@0 38 static void XMLCALL start_proc(void *data, const char *el, const char **attr)
michael@0 39 {
michael@0 40 #if defined CHAR_16_TO_9
michael@0 41 size_t len = sk_wcslen((const short*) el);
michael@0 42 SkAutoMalloc el8(len + 1);
michael@0 43 el = ConvertUnicodeToChar((const short*) el, len, el8);
michael@0 44 #endif
michael@0 45 if (((SkXMLParser*)data)->startElement(el)) {
michael@0 46 XML_StopParser((XML_Parser) ((SkXMLParser*)data)->fParser, false);
michael@0 47 return;
michael@0 48 }
michael@0 49 while (*attr)
michael@0 50 {
michael@0 51 const char* attr0 = attr[0];
michael@0 52 const char* attr1 = attr[1];
michael@0 53 #if defined CHAR_16_TO_9
michael@0 54 size_t len0 = sk_wcslen((const short*) attr0);
michael@0 55 SkAutoMalloc attr0_8(len0 + 1);
michael@0 56 attr0 = ConvertUnicodeToChar((const short*) attr0, len0, attr0_8);
michael@0 57 size_t len1 = sk_wcslen((const short*) attr1);
michael@0 58 SkAutoMalloc attr1_8(len1 + 1);
michael@0 59 attr1 = ConvertUnicodeToChar((const short*) attr1, len1, attr1_8);
michael@0 60 #endif
michael@0 61 if (((SkXMLParser*)data)->addAttribute(attr0, attr1)) {
michael@0 62 XML_StopParser((XML_Parser) ((SkXMLParser*)data)->fParser, false);
michael@0 63 return;
michael@0 64 }
michael@0 65 attr += 2;
michael@0 66 }
michael@0 67 }
michael@0 68
michael@0 69 static void XMLCALL end_proc(void *data, const char *el)
michael@0 70 {
michael@0 71 #if defined CHAR_16_TO_9
michael@0 72 size_t len = sk_wcslen((const short*) el);
michael@0 73 SkAutoMalloc el8(len + 1);
michael@0 74 el = ConvertUnicodeToChar((const short*) el, len, el8);
michael@0 75 #endif
michael@0 76 if (((SkXMLParser*)data)->endElement(el))
michael@0 77 XML_StopParser((XML_Parser) ((SkXMLParser*)data)->fParser, false);
michael@0 78 }
michael@0 79
michael@0 80 static void XMLCALL text_proc(void* data, const char* text, int len)
michael@0 81 {
michael@0 82 #if defined CHAR_16_TO_9
michael@0 83 SkAutoMalloc text8(len + 1);
michael@0 84 text = ConvertUnicodeToChar((const short*) text, len, text8);
michael@0 85 #endif
michael@0 86 if (((SkXMLParser*)data)->text(text, len))
michael@0 87 XML_StopParser((XML_Parser) ((SkXMLParser*)data)->fParser, false);
michael@0 88 }
michael@0 89
michael@0 90 bool SkXMLParser::parse(const char doc[], size_t len)
michael@0 91 {
michael@0 92 if (len == 0) {
michael@0 93 fError->fCode = SkXMLParserError::kEmptyFile;
michael@0 94 reportError(NULL);
michael@0 95 return false;
michael@0 96 }
michael@0 97 XML_Parser p = XML_ParserCreate(NULL);
michael@0 98 SkASSERT(p);
michael@0 99 fParser = p;
michael@0 100 XML_SetElementHandler(p, start_proc, end_proc);
michael@0 101 XML_SetCharacterDataHandler(p, text_proc);
michael@0 102 XML_SetUserData(p, this);
michael@0 103
michael@0 104 bool success = true;
michael@0 105 int error = XML_Parse(p, doc, len, true);
michael@0 106 if (error == XML_STATUS_ERROR) {
michael@0 107 reportError(p);
michael@0 108 success = false;
michael@0 109 }
michael@0 110 XML_ParserFree(p);
michael@0 111 return success;
michael@0 112 }
michael@0 113
michael@0 114 bool SkXMLParser::parse(SkStream& input)
michael@0 115 {
michael@0 116 size_t len = input.getLength();
michael@0 117 SkAutoMalloc am(len);
michael@0 118 char* doc = (char*)am.get();
michael@0 119
michael@0 120 input.rewind();
michael@0 121 size_t len2 = input.read(doc, len);
michael@0 122 SkASSERT(len2 == len);
michael@0 123
michael@0 124 return this->parse(doc, len2);
michael@0 125 }
michael@0 126
michael@0 127 void SkXMLParser::reportError(void* p)
michael@0 128 {
michael@0 129 XML_Parser parser = (XML_Parser) p;
michael@0 130 if (fError && parser) {
michael@0 131 fError->fNativeCode = XML_GetErrorCode(parser);
michael@0 132 fError->fLineNumber = XML_GetCurrentLineNumber(parser);
michael@0 133 }
michael@0 134 }
michael@0 135
michael@0 136 void SkXMLParser::GetNativeErrorString(int error, SkString* str)
michael@0 137 {
michael@0 138 if (str)
michael@0 139 str->set(XML_ErrorString((XML_Error) error));
michael@0 140 }

mercurial