Skip to content

Commit

Permalink
readl
Browse files Browse the repository at this point in the history
  • Loading branch information
hazae41 committed Feb 9, 2023
1 parent 2e6f299 commit c4ae517
Show file tree
Hide file tree
Showing 14 changed files with 43 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/mods/triplets/bit_string/bit_string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ export class BitString {

const length = Length.read(binary)

return this.read2(binary, length.value)
return this.readl(binary, length.value)
}

static read2(binary: Binary, length: number) {
static readl(binary: Binary, length: number) {
const start = binary.offset

const padding = binary.readUint8()
Expand Down
4 changes: 2 additions & 2 deletions src/mods/triplets/boolean/boolean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ export class Boolean {

const length = Length.read(binary)

return this.read2(binary, length.value)
return this.readl(binary, length.value)
}

static read2(binary: Binary, length: number) {
static readl(binary: Binary, length: number) {
const start = binary.offset

const value = binary.readUint8()
Expand Down
8 changes: 6 additions & 2 deletions src/mods/triplets/constructed/constructed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,18 @@ export class Constructed {

const length = Length.read(binary)

return this.readl(type, binary, length.value, read)
}

static readl(type: Type, binary: Binary, length: number, read: (binary: Binary) => Triplet) {
const start = binary.offset

const triplets = new Array<Triplet>()

while (binary.offset - start < length.value)
while (binary.offset - start < length)
triplets.push(read(binary))

if (binary.offset - start !== length.value)
if (binary.offset - start !== length)
throw new Error(`Invalid length`)

return new this(type, triplets)
Expand Down
4 changes: 2 additions & 2 deletions src/mods/triplets/ia5_string/ia5_string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ export class IA5String {

const length = Length.read(binary)

return this.read2(binary, length.value)
return this.readl(binary, length.value)
}

static read2(binary: Binary, length: number) {
static readl(binary: Binary, length: number) {
const start = binary.offset

const bytes = binary.read(length)
Expand Down
4 changes: 2 additions & 2 deletions src/mods/triplets/integer/integer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ export class Integer {

const length = Length.read(binary)

return this.read2(binary, length.value)
return this.readl(binary, length.value)
}

static read2(binary: Binary, length: number) {
static readl(binary: Binary, length: number) {
const start = binary.offset

let value = BigInt(0)
Expand Down
9 changes: 9 additions & 0 deletions src/mods/triplets/null/null.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ export class Null {

const length = Length.read(binary)

return this.readl(binary, length.value)
}

static readl(binary: Binary, length: number) {
const start = binary.offset

if (binary.offset - start !== length)
throw new Error(`Invalid length`)

return new this()
}

Expand Down
4 changes: 2 additions & 2 deletions src/mods/triplets/object_identifier/object_identifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ export class ObjectIdentifier {

const length = Length.read(binary)

return this.read2(binary, length.value)
return this.readl(binary, length.value)
}

static read2(binary: Binary, length: number) {
static readl(binary: Binary, length: number) {
const start = binary.offset

const header = binary.readUint8()
Expand Down
4 changes: 2 additions & 2 deletions src/mods/triplets/octet_string/octet_string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ export class OctetString {

const length = Length.read(binary)

return this.read2(binary, length.value)
return this.readl(binary, length.value)
}

static read2(binary: Binary, length: number) {
static readl(binary: Binary, length: number) {
const start = binary.offset

const buffer = binary.read(length)
Expand Down
4 changes: 2 additions & 2 deletions src/mods/triplets/printable_string/printable_string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ export class PrintableString {

const length = Length.read(binary)

return this.read2(binary, length.value)
return this.readl(binary, length.value)
}

static read2(binary: Binary, length: number) {
static readl(binary: Binary, length: number) {
const start = binary.offset

const value = binary.readString(length)
Expand Down
4 changes: 2 additions & 2 deletions src/mods/triplets/sequence/sequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ export class Sequence {

const length = Length.read(binary)

return this.read2(binary, length.value, read)
return this.readl(binary, length.value, read)
}

static read2(binary: Binary, length: number, read: (binary: Binary) => Triplet) {
static readl(binary: Binary, length: number, read: (binary: Binary) => Triplet) {
const start = binary.offset

const triplets = new Array<Triplet>()
Expand Down
4 changes: 2 additions & 2 deletions src/mods/triplets/set/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ export class Set {

const length = Length.read(binary)

return this.read2(binary, length.value, read)
return this.readl(binary, length.value, read)
}

static read2(binary: Binary, length: number, read: (binary: Binary) => Triplet) {
static readl(binary: Binary, length: number, read: (binary: Binary) => Triplet) {
const start = binary.offset

const inner = new Array<Triplet>()
Expand Down
9 changes: 6 additions & 3 deletions src/mods/triplets/unknown/unknown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,17 @@ export class Unknown {

static read(binary: Binary) {
const type = Type.read(binary)

const length = Length.read(binary)

return this.readl(type, binary, length.value)
}

static readl(type: Type, binary: Binary, length: number) {
const start = binary.offset

const buffer = binary.read(length.value)
const buffer = binary.read(length)

if (binary.offset - start !== length.value)
if (binary.offset - start !== length)
throw new Error(`Invalid length`)

return new this(type, buffer)
Expand Down
4 changes: 2 additions & 2 deletions src/mods/triplets/utc_time/utc_time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ export class UTCTime {

const length = Length.read(binary)

return this.read2(binary, length.value)
return this.readl(binary, length.value)
}

static read2(binary: Binary, length: number) {
static readl(binary: Binary, length: number) {
const start = binary.offset

const text = binary.readString(length)
Expand Down
4 changes: 2 additions & 2 deletions src/mods/triplets/utf8_string/utf8_string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ export class UTF8String {

const length = Length.read(binary)

return this.read2(binary, length.value)
return this.readl(binary, length.value)
}

static read2(binary: Binary, length: number) {
static readl(binary: Binary, length: number) {
const start = binary.offset

const value = binary.readString(length)
Expand Down

0 comments on commit c4ae517

Please sign in to comment.