🎨 Improve Bencode parser
This commit is contained in:
parent
a221ee3a6b
commit
a578d21663
2 changed files with 64 additions and 57 deletions
|
@ -7,7 +7,7 @@ import { TextDecoder } from 'util';
|
|||
const Decoder = new TextDecoder('utf-8');
|
||||
|
||||
/** Takes a Bencoded-encoded file, parses it at the given starting position and returns a JSON object, or rejects on error. */
|
||||
export default function bpParse(dv: DataView, posIn: number = 0): { obj: any, pos: number } {
|
||||
function bpParse(dv: DataView, posIn: number = 0): { obj: any, pos: number } {
|
||||
let pos = posIn;
|
||||
let obj;
|
||||
const header = dv.getUint8(pos); pos += 1;
|
||||
|
@ -57,6 +57,9 @@ export default function bpParse(dv: DataView, posIn: number = 0): { obj: any, po
|
|||
let curChar: number = header;
|
||||
let strLen = 0;
|
||||
while (curChar !== 0x3A) { //':' - integer delimiter, beginning of string
|
||||
if (curChar < 0x30 || curChar > 0x39) {
|
||||
throw new Error('Unexpected string length during bencode parsing');
|
||||
}
|
||||
strLen *= 10;
|
||||
strLen += curChar - 0x30;
|
||||
curChar = dv.getUint8(pos); pos += 1;
|
||||
|
@ -71,3 +74,7 @@ export default function bpParse(dv: DataView, posIn: number = 0): { obj: any, po
|
|||
}
|
||||
return { obj, pos };
|
||||
}
|
||||
|
||||
export default function parseBencode(dv: DataView): any {
|
||||
return bpParse(dv).obj;
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ import getUrlContents from '../getUrlContents';
|
|||
import { Product } from '../interfaces/ISettings';
|
||||
import ISolid from '../interfaces/ISolidFile';
|
||||
import verifyProductName from '../ssn/verifyProductName';
|
||||
import bpParse from './bencodeParser';
|
||||
import parseBencode from './bencodeParser';
|
||||
import extractFile from './extractFile';
|
||||
import readSsnFile from './readSsnFile';
|
||||
import verifySolidpkg from './verifySolidpkg';
|
||||
|
@ -39,7 +39,7 @@ export default async function getSolidpkg(product: Product, from: number, to: nu
|
|||
|
||||
//Extract metafile.solid file
|
||||
const solidFile = await extractFile(firstFile, [new DataView(ssnFile)]);
|
||||
const solidContents = bpParse(new DataView(solidFile)).obj as ISolid;
|
||||
const solidContents = parseBencode(new DataView(solidFile)) as ISolid;
|
||||
|
||||
//Verify metafile.solid for correctness
|
||||
verifySolidpkg(solidContents, { product, from, to });
|
||||
|
|
Loading…
Reference in a new issue