1 | /* |
2 | * Copyright (C) 2015 Igalia S.L. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
23 | * THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #include "config.h" |
27 | #include "KeyedDecoderGlib.h" |
28 | |
29 | #include <wtf/text/CString.h> |
30 | |
31 | namespace WebCore { |
32 | |
33 | std::unique_ptr<KeyedDecoder> KeyedDecoder::decoder(const uint8_t* data, size_t size) |
34 | { |
35 | return std::make_unique<KeyedDecoderGlib>(data, size); |
36 | } |
37 | |
38 | KeyedDecoderGlib::KeyedDecoderGlib(const uint8_t* data, size_t size) |
39 | { |
40 | GRefPtr<GBytes> bytes = adoptGRef(g_bytes_new(data, size)); |
41 | GRefPtr<GVariant> variant = g_variant_new_from_bytes(G_VARIANT_TYPE("a{sv}" ), bytes.get(), TRUE); |
42 | m_dictionaryStack.append(dictionaryFromGVariant(variant.get())); |
43 | } |
44 | |
45 | KeyedDecoderGlib::~KeyedDecoderGlib() |
46 | { |
47 | ASSERT(m_dictionaryStack.size() == 1); |
48 | ASSERT(m_arrayStack.isEmpty()); |
49 | ASSERT(m_arrayIndexStack.isEmpty()); |
50 | } |
51 | |
52 | HashMap<String, GRefPtr<GVariant>> KeyedDecoderGlib::dictionaryFromGVariant(GVariant* variant) |
53 | { |
54 | HashMap<String, GRefPtr<GVariant>> dictionary; |
55 | GVariantIter iter; |
56 | g_variant_iter_init(&iter, variant); |
57 | const char* key; |
58 | GVariant* value; |
59 | while (g_variant_iter_loop(&iter, "{&sv}" , &key, &value)) { |
60 | if (key) |
61 | dictionary.set(String::fromUTF8(key), value); |
62 | } |
63 | return dictionary; |
64 | } |
65 | |
66 | bool KeyedDecoderGlib::decodeBytes(const String& key, const uint8_t*& bytes, size_t& size) |
67 | { |
68 | GRefPtr<GVariant> value = m_dictionaryStack.last().get(key); |
69 | if (!value) |
70 | return false; |
71 | |
72 | size = g_variant_get_size(value.get()); |
73 | bytes = static_cast<const uint8_t*>(g_variant_get_data(value.get())); |
74 | return true; |
75 | } |
76 | |
77 | template<typename T, typename F> |
78 | bool KeyedDecoderGlib::decodeSimpleValue(const String& key, T& result, F getFunction) |
79 | { |
80 | GRefPtr<GVariant> value = m_dictionaryStack.last().get(key); |
81 | if (!value) |
82 | return false; |
83 | |
84 | result = getFunction(value.get()); |
85 | return true; |
86 | } |
87 | |
88 | bool KeyedDecoderGlib::decodeBool(const String& key, bool& result) |
89 | { |
90 | return decodeSimpleValue(key, result, g_variant_get_boolean); |
91 | } |
92 | |
93 | bool KeyedDecoderGlib::decodeUInt32(const String& key, uint32_t& result) |
94 | { |
95 | return decodeSimpleValue(key, result, g_variant_get_uint32); |
96 | } |
97 | |
98 | bool KeyedDecoderGlib::decodeUInt64(const String& key, uint64_t& result) |
99 | { |
100 | return decodeSimpleValue(key, result, g_variant_get_uint64); |
101 | } |
102 | |
103 | bool KeyedDecoderGlib::decodeInt32(const String& key, int32_t& result) |
104 | { |
105 | return decodeSimpleValue(key, result, g_variant_get_int32); |
106 | } |
107 | |
108 | bool KeyedDecoderGlib::decodeInt64(const String& key, int64_t& result) |
109 | { |
110 | return decodeSimpleValue(key, result, g_variant_get_int64); |
111 | } |
112 | |
113 | bool KeyedDecoderGlib::decodeFloat(const String& key, float& result) |
114 | { |
115 | return decodeSimpleValue(key, result, g_variant_get_double); |
116 | } |
117 | |
118 | bool KeyedDecoderGlib::decodeDouble(const String& key, double& result) |
119 | { |
120 | return decodeSimpleValue(key, result, g_variant_get_double); |
121 | } |
122 | |
123 | bool KeyedDecoderGlib::decodeString(const String& key, String& result) |
124 | { |
125 | GRefPtr<GVariant> value = m_dictionaryStack.last().get(key); |
126 | if (!value) |
127 | return false; |
128 | |
129 | result = String::fromUTF8(g_variant_get_string(value.get(), nullptr)); |
130 | return true; |
131 | } |
132 | |
133 | bool KeyedDecoderGlib::beginObject(const String& key) |
134 | { |
135 | GRefPtr<GVariant> value = m_dictionaryStack.last().get(key); |
136 | if (!value) |
137 | return false; |
138 | |
139 | m_dictionaryStack.append(dictionaryFromGVariant(value.get())); |
140 | return true; |
141 | } |
142 | |
143 | void KeyedDecoderGlib::endObject() |
144 | { |
145 | m_dictionaryStack.removeLast(); |
146 | } |
147 | |
148 | bool KeyedDecoderGlib::beginArray(const String& key) |
149 | { |
150 | GRefPtr<GVariant> value = m_dictionaryStack.last().get(key); |
151 | if (!value) |
152 | return false; |
153 | |
154 | m_arrayStack.append(value.get()); |
155 | m_arrayIndexStack.append(0); |
156 | return true; |
157 | } |
158 | |
159 | bool KeyedDecoderGlib::beginArrayElement() |
160 | { |
161 | if (m_arrayIndexStack.last() >= g_variant_n_children(m_arrayStack.last())) |
162 | return false; |
163 | |
164 | GRefPtr<GVariant> variant = adoptGRef(g_variant_get_child_value(m_arrayStack.last(), m_arrayIndexStack.last()++)); |
165 | m_dictionaryStack.append(dictionaryFromGVariant(variant.get())); |
166 | return true; |
167 | } |
168 | |
169 | void KeyedDecoderGlib::endArrayElement() |
170 | { |
171 | m_dictionaryStack.removeLast(); |
172 | } |
173 | |
174 | void KeyedDecoderGlib::endArray() |
175 | { |
176 | m_arrayStack.removeLast(); |
177 | m_arrayIndexStack.removeLast(); |
178 | } |
179 | |
180 | } // namespace WebCore |
181 | |