1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
|
/*
KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2024 Dominik Reichl <dominik.reichl@t-online.de>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Text;
using KeePass.Resources;
using KeePassLib.Resources;
using KeePassLib.Utility;
namespace KeePass.DataExchange
{
public sealed class JsonObject
{
private const int MaxTreeHeight = 50000;
private readonly Dictionary<string, object> m_dItems = new Dictionary<string, object>();
public IDictionary<string, object> Items
{
get { return m_dItems; }
}
private JsonObject()
{
}
public JsonObject(CharStream csJsonData)
{
if(csJsonData == null) throw new ArgumentNullException("csJsonData");
Load(csJsonData);
}
private static void ThrowDataException()
{
Debug.Assert(false);
throw new FormatException(KLRes.InvalidDataWhileDecoding);
}
private static void ThrowImplException()
{
Debug.Assert(false);
throw new InvalidOperationException(KLRes.UnknownError);
}
private void Load(CharStream cs)
{
char chInit = cs.ReadChar(true);
if(chInit != '{') ThrowDataException();
Stack<object> sCtx = new Stack<object>();
sCtx.Push(this);
while(sCtx.Count != 0)
{
if(sCtx.Count > MaxTreeHeight)
{
Debug.Assert(false);
throw new InvalidOperationException(KLRes.StructsTooDeep);
}
object oCtx = sCtx.Peek();
JsonObject joCtx = (oCtx as JsonObject);
if(joCtx != null) { ReadObjectPart(cs, joCtx, sCtx); continue; }
List<object> lCtx = (oCtx as List<object>);
if(lCtx != null) { ReadArrayPart(cs, lCtx, sCtx); continue; }
ThrowImplException(); // Unknown context object
}
}
private static void ReadObjectPart(CharStream cs, JsonObject joCtx,
Stack<object> sCtx)
{
char ch = cs.PeekChar(true);
if(ch == '}') EndContainer(cs, joCtx, sCtx);
else if(ch == '\"')
{
string strName = ReadString(cs);
char chSep = cs.ReadChar(true);
if(chSep != ':') ThrowDataException();
object oSub = TryBeginContainer(cs, sCtx);
if(oSub != null)
joCtx.m_dItems[strName] = oSub;
else
{
joCtx.m_dItems[strName] = ReadAtomicValue(cs);
if(cs.PeekChar(true) == ',') cs.ReadChar(true);
}
}
else ThrowDataException();
}
private static void ReadArrayPart(CharStream cs, List<object> lCtx,
Stack<object> sCtx)
{
char ch = cs.PeekChar(true);
if(ch == ']') { EndContainer(cs, lCtx, sCtx); return; }
object oSub = TryBeginContainer(cs, sCtx);
if(oSub != null)
lCtx.Add(oSub);
else
{
lCtx.Add(ReadAtomicValue(cs));
if(cs.PeekChar(true) == ',') cs.ReadChar(true);
}
}
private static object TryBeginContainer(CharStream cs, Stack<object> sCtx)
{
char ch = cs.PeekChar(true);
object oNew = null;
if(ch == '{') oNew = new JsonObject();
else if(ch == '[') oNew = new List<object>();
if(oNew != null)
{
cs.ReadChar(true);
sCtx.Push(oNew);
}
return oNew;
}
private static void EndContainer(CharStream cs, object oCtx,
Stack<object> sCtx)
{
Debug.Assert(object.ReferenceEquals(oCtx, sCtx.Peek())); // For Pop()
char chTerm = cs.ReadChar(true);
if(chTerm == '}')
{
if(!(oCtx is JsonObject)) ThrowImplException();
}
else if(chTerm == ']')
{
if(!(oCtx is List<object>)) ThrowImplException();
}
else ThrowImplException(); // Unknown terminator
sCtx.Pop();
if(sCtx.Count == 0) return;
object oParent = sCtx.Peek();
if((oParent is JsonObject) || (oParent is List<object>))
{
if(cs.PeekChar(true) == ',') cs.ReadChar(true);
}
else ThrowImplException(); // Unknown context object
}
private static object ReadAtomicValue(CharStream cs)
{
char chInit = cs.PeekChar(true);
if(chInit == '\"') return ReadString(cs);
if(chInit == 't')
{
cs.ReadChar(true);
if(cs.ReadChar() != 'r') ThrowDataException();
if(cs.ReadChar() != 'u') ThrowDataException();
if(cs.ReadChar() != 'e') ThrowDataException();
return true;
}
if(chInit == 'f')
{
cs.ReadChar(true);
if(cs.ReadChar() != 'a') ThrowDataException();
if(cs.ReadChar() != 'l') ThrowDataException();
if(cs.ReadChar() != 's') ThrowDataException();
if(cs.ReadChar() != 'e') ThrowDataException();
return false;
}
if(chInit == 'n')
{
cs.ReadChar(true);
if(cs.ReadChar() != 'u') ThrowDataException();
if(cs.ReadChar() != 'l') ThrowDataException();
if(cs.ReadChar() != 'l') ThrowDataException();
return null;
}
return ReadNumber(cs);
}
private static string ReadString(CharStream cs)
{
char chInit = cs.ReadChar(true);
if(chInit != '\"') ThrowImplException(); // Caller should ensure '\"'
StringBuilder sb = new StringBuilder();
while(true)
{
char ch = cs.ReadChar();
if(ch == char.MinValue) ThrowDataException(); // End of JSON data
if(ch == '\"') break; // End of string
if(ch == '\\')
{
char chNext = cs.ReadChar();
switch(chNext)
{
case '\"':
case '\'': // C
case '/':
case '\\':
case '?': // C
sb.Append(chNext);
break;
case 'a': sb.Append('\a'); break; // C
case 'b': sb.Append('\b'); break;
case 'f': sb.Append('\f'); break;
case 'n': sb.Append('\n'); break;
case 'r': sb.Append('\r'); break;
case 't': sb.Append('\t'); break;
case 'v': sb.Append('\v'); break; // C
case 'u':
case 'x': // C, only special case supported
sb.Append(ReadHex4Char(cs));
break;
default: ThrowDataException(); break;
}
}
else sb.Append(ch);
}
return sb.ToString();
}
private static char ReadHex4Char(CharStream cs)
{
char ch1 = cs.ReadChar();
char ch2 = cs.ReadChar();
char ch3 = cs.ReadChar();
char ch4 = cs.ReadChar();
char[] vHex = new char[4] { ch1, ch2, ch3, ch4 };
if(Array.IndexOf(vHex, char.MinValue) >= 0) ThrowDataException();
string strHex = new string(vHex);
if(!StrUtil.IsHexString(strHex, true)) ThrowDataException();
return (char)Convert.ToUInt16(strHex, 16);
}
private static object ReadNumber(CharStream cs)
{
StringBuilder sb = new StringBuilder();
bool bFloat = false, bNeg = false;
while(true)
{
char ch = cs.PeekChar(true);
if(((ch >= '0') && (ch <= '9')) || (ch == '+')) { }
else if(ch == '-') bNeg = true;
else if(ch == '.') bFloat = true;
else if((ch == 'e') || (ch == 'E')) bFloat = true;
else break;
cs.ReadChar(true);
sb.Append(ch);
}
if(sb.Length == 0) ThrowDataException();
string str = sb.ToString();
NumberFormatInfo nfi = NumberFormatInfo.InvariantInfo;
if(bFloat)
{
double d;
if(!double.TryParse(str, NumberStyles.Float, nfi, out d))
ThrowDataException();
return d;
}
if(bNeg)
{
long i;
if(!long.TryParse(str, NumberStyles.Integer, nfi, out i))
ThrowDataException();
return i;
}
ulong u;
if(!ulong.TryParse(str, NumberStyles.Integer, nfi, out u))
ThrowDataException();
return u;
}
private static T ConvertOrDefault<T>(object o, T tDefault)
where T : struct // Use 'as' for class
{
if(o == null) return tDefault;
try
{
if(o is T) return (T)o;
return (T)Convert.ChangeType(o, typeof(T));
}
catch(Exception) { Debug.Assert(false); }
try { return (T)o; }
catch(Exception) { Debug.Assert(false); }
return tDefault;
}
public T GetValue<T>(string strKey)
where T : class
{
if(strKey == null) throw new ArgumentNullException("strKey");
object o;
m_dItems.TryGetValue(strKey, out o);
return (o as T);
}
public T GetValue<T>(string strKey, T tDefault)
where T : struct
{
if(strKey == null) throw new ArgumentNullException("strKey");
object o;
m_dItems.TryGetValue(strKey, out o);
return ConvertOrDefault(o, tDefault);
}
public T[] GetValueArray<T>(string strKey)
where T : class
{
return GetValueArray<T>(strKey, false);
}
internal T[] GetValueArray<T>(string strKey, bool bEmptyIfNotExists)
where T : class
{
List<object> lO = GetValue<List<object>>(strKey);
if(lO == null) return (bEmptyIfNotExists ? MemUtil.EmptyArray<T>() : null);
T[] vT = new T[lO.Count];
for(int i = 0; i < lO.Count; ++i) vT[i] = (lO[i] as T);
return vT;
}
public T[] GetValueArray<T>(string strKey, T tDefault)
where T : struct
{
return GetValueArray<T>(strKey, tDefault, false);
}
internal T[] GetValueArray<T>(string strKey, T tDefault, bool bEmptyIfNotExists)
where T : struct
{
List<object> lO = GetValue<List<object>>(strKey);
if(lO == null) return (bEmptyIfNotExists ? MemUtil.EmptyArray<T>() : null);
T[] vT = new T[lO.Count];
for(int i = 0; i < lO.Count; ++i)
vT[i] = ConvertOrDefault(lO[i], tDefault);
return vT;
}
}
}
|