Commit 3d03a218 authored by Jeremy Banks's avatar Jeremy Banks

Convert the intermediate integer typed used in Bencoding from Int64 to BigInteger.

parent e9b2b0ad
Pipeline #7394578 failed with stage
in 61 minutes and 24 seconds
using System;
using System.Collections.Generic;
using System.IO;
using System.Numerics;
using Ditto.Common;
namespace Ditto {
......@@ -53,7 +53,7 @@ namespace Ditto {
if (end != 'e') {
throw new DecodingException($"Expected 'e' after 'i0', got: {end} '{char.ConvertFromUtf32(end)}'.");
}
return (Int64) 0;
return (BigInteger) 0;
}
if (valueDigits.Count == 1 && next == '0' && valueDigits[0] == '-') {
......@@ -85,7 +85,7 @@ namespace Ditto {
var valueDigitsString = valueDigits.ToArray().FromASCII();
try {
return Int64.Parse(valueDigitsString);
return BigInteger.Parse(valueDigitsString);
} catch (System.OverflowException exception) {
throw new DecodingException(
$"Integer out of supported 64-bit bounds: {valueDigitsString}", exception);
......
......@@ -2,8 +2,9 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using Ditto.Common;
using Microsoft.Extensions.Logging;
namespace Ditto {
public static partial class Bencoding {
......@@ -17,6 +18,10 @@ namespace Ditto {
public static void Encode(Stream stream, object value) {
switch (value) {
case Int64 x:
logger.LogWarning("Using deprecated support for Int64 in Ditto.Bencoding.Encode.");
Encode(stream, (BigInteger) x);
break;
case BigInteger x:
Encode(stream, x);
break;
case byte[] x:
......
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Text;
using Ditto.Common;
using Microsoft.Extensions.Logging;
namespace Ditto {
partial class Bencoding {
static ILogger logger { get; } = GlobalLogger.CreateLogger<object>();
public static Dictionary<byte[], object> Dict() {
return new Dictionary<byte[], object>(ByteArrayComparer.Instance);
}
......@@ -30,8 +34,8 @@ namespace Ditto {
}
}
result.Append("\"");
} else if (value is Int64) {
var i = (Int64) value;
} else if (value is BigInteger) {
var i = (BigInteger) value;
result.Append(i.ToString());
} else if (value is Dictionary<byte[], object>) {
var d = (Dictionary<byte[], object>) value;
......
......@@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
......@@ -206,10 +207,14 @@ namespace Ditto.Common {
}
public static class BencodedDictExtensions {
public static Int64 GetInt(this Dictionary<byte[], object> bDict, string key) {
public static Int64 GetInt64(this Dictionary<byte[], object> bDict, string key) {
return (Int64) bDict[key.ToASCII()];
}
public static BigInteger GetInt(this Dictionary<byte[], object> bDict, string key) {
return (BigInteger) bDict[key.ToASCII()];
}
public static byte[] GetBytes(this Dictionary<byte[], object> bDict, string key) {
return (byte[]) bDict[key.ToASCII()];
}
......@@ -231,7 +236,7 @@ namespace Ditto.Common {
}
public static void Set(this Dictionary<byte[], object> bDict, string key, Int32 value) {
bDict[key.ToASCII()] = (Int64) value;
bDict[key.ToASCII()] = (BigInteger) value;
}
public static void Set(this Dictionary<byte[], object> bDict, string key, object value) {
......
......@@ -8,6 +8,7 @@ using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Ditto.Common;
using System.Numerics;
namespace Ditto.DHT {
// Identifier for a DHT query that can be used as a dictionary key.
......@@ -236,7 +237,7 @@ namespace Ditto.DHT {
pendingQueries.Remove(key);
var errors = value.GetList("e");
var code = (Int64)errors[0];
var code = (Int64) (BigInteger) errors[0];
var errorMessage = ((byte[])errors[1]).FromASCII();
var exception = new Exception($"{code} {errorMessage}");
......
......@@ -69,9 +69,9 @@ namespace Ditto
var dict = Bencoding.DecodeFirstDict(data, out Int64 dictSize);
var postDict = data.Slice((Int32)dictSize); // This is the metadata itself -- a bencoded dictionary of utf8 strings
if (dict.GetInt("piece") != currentPiece)
if (dict.GetInt64("piece") != currentPiece)
{
throw new Exception($"Expected piece {currentPiece}. Instead, received {dict.GetInt("piece")}");
throw new Exception($"Expected piece {currentPiece}. Instead, received {dict.GetInt64("piece")}");
}
logger.LogInformation(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment