private static BDictionary ReadDictionary(ref string bencodedString, ref int index) { Contract.Requires(bencodedString != null); Contract.Requires(0 <= index); Contract.Requires((index + 1) < bencodedString.Length); Contract.Ensures(index >= Contract.OldValue(index)); index++; BDictionary dict = new BDictionary(); try { int tmpIndex = index; while (bencodedString.Length > tmpIndex && bencodedString[tmpIndex] != 'e') { BString k = ReadString(ref bencodedString, ref tmpIndex); if (bencodedString.Length <= tmpIndex) { throw new BencodingException("Invalid dictionary"); } IBElement v = ReadElement(ref bencodedString, ref tmpIndex); dict.Add(k, v); } index = tmpIndex; } catch (BencodingException) { throw; } catch (Exception e) { throw Error(e); } index++; return(dict); }
private static BDictionary ReadDictionary(BinaryReader binaryReader) { Contract.Requires(binaryReader != null); int i = binaryReader.ReadByte(); if (i != 'd') { throw Error(); } BDictionary dict = new BDictionary(); try { for (int c = binaryReader.PeekChar(); ; c = binaryReader.PeekChar()) { if (c == -1) { throw Error(); } if (c == 'e') { binaryReader.ReadByte(); break; } BString k = ReadString(binaryReader); IBElement v = ReadElement(binaryReader); dict.Add(k, v); } } catch (BencodingException) { throw; } catch (Exception e) { throw Error(e); } return(dict); }