Skip to content

Commit 43d19c8

Browse files
Stronger types
1 parent f96fa01 commit 43d19c8

3 files changed

Lines changed: 83 additions & 58 deletions

File tree

Security/blob.ts

Lines changed: 78 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,30 @@ import {
1313
} from '@hqtsm/struct';
1414
import { CSMAGIC_BLOBWRAPPER } from '../kern/cs_blobs.ts';
1515
import { EINVAL, ENOMEM } from '../libc/errno.ts';
16+
import type { bool, int, size_t, uchar } from '../libc/c.ts';
17+
import type { uint32_t, uint8_t } from '../libc/stdint.ts';
1618
import { malloc } from '../libc/stdlib.ts';
1719
import { type ArrayBufferLikeData, asUint8Array } from '../util/memory.ts';
1820
import type { Reader } from '../util/reader.ts';
21+
import type { Endian } from './endian.ts';
1922
import { MacOSError, UnixError } from './errors.ts';
2023
import { errSecAllocate } from './SecBase.ts';
2124

25+
/**
26+
* BlobCore offset.
27+
*/
28+
export type BlobCoreOffset = uint32_t;
29+
30+
/**
31+
* BlobCore magic number.
32+
*/
33+
export type BlobCoreMagic = uint32_t;
34+
2235
/**
2336
* BlobCore BlobType.
2437
*/
2538
export type BlobCoreBlobType =
26-
& { readonly typeMagic: number }
39+
& { readonly typeMagic: BlobCoreMagic }
2740
& typeof BlobCore;
2841

2942
/**
@@ -36,7 +49,7 @@ export class BlobCore extends Struct {
3649
* @param _this This.
3750
* @returns Magic number.
3851
*/
39-
public static magic(_this: BlobCore): number {
52+
public static magic(_this: BlobCore): BlobCoreMagic {
4053
return _this.mMagic;
4154
}
4255

@@ -48,7 +61,7 @@ export class BlobCore extends Struct {
4861
* @param _this This.
4962
* @returns Byte length.
5063
*/
51-
public static size(_this: BlobCore): number;
64+
public static size(_this: BlobCore): size_t;
5265

5366
/**
5467
* Set blob length.
@@ -58,7 +71,7 @@ export class BlobCore extends Struct {
5871
* @param _this This.
5972
* @param size Byte length.
6073
*/
61-
public static size(_this: BlobCore, size: number): void;
74+
public static size(_this: BlobCore, size: size_t): void;
6275

6376
/**
6477
* Get or set blob length.
@@ -67,7 +80,7 @@ export class BlobCore extends Struct {
6780
* @param size Byte length to set or undefined to get.
6881
* @returns Byte length on get or undefined on set.
6982
*/
70-
public static size(_this: BlobCore, size?: number): number | void {
83+
public static size(_this: BlobCore, size?: size_t): size_t | void {
7184
if (size === undefined) {
7285
return _this.mLength;
7386
}
@@ -80,7 +93,11 @@ export class BlobCore extends Struct {
8093
* @param magic Magic number.
8194
* @param length Length.
8295
*/
83-
public static initialize(_this: BlobCore, magic: number, length = 0): void {
96+
public static initialize(
97+
_this: BlobCore,
98+
magic: BlobCoreMagic,
99+
length: size_t = 0,
100+
): void {
84101
_this.mMagic = magic;
85102
_this.mLength = length >>> 0;
86103
}
@@ -97,11 +114,11 @@ export class BlobCore extends Struct {
97114
*/
98115
public static validateBlob(
99116
_this: BlobCore,
100-
magic: number,
101-
minSize?: number,
102-
maxSize?: number,
103-
context?: { errno: number },
104-
): boolean {
117+
magic: BlobCoreMagic,
118+
minSize?: size_t,
119+
maxSize?: size_t,
120+
context?: { errno: int },
121+
): bool {
105122
const length = _this.mLength;
106123
if (magic && magic !== _this.mMagic) {
107124
if (context) context.errno = EINVAL;
@@ -135,7 +152,7 @@ export class BlobCore extends Struct {
135152
byteOffset?: number,
136153
littleEndian?: boolean | null,
137154
) => T,
138-
offset: number,
155+
offset: BlobCoreOffset,
139156
littleEndian: boolean | null = null,
140157
): T {
141158
return new Type(
@@ -155,9 +172,9 @@ export class BlobCore extends Struct {
155172
*/
156173
public static contains(
157174
_this: BlobCore,
158-
offset: number,
159-
size: number,
160-
): boolean {
175+
offset: size_t,
176+
size: size_t,
177+
): bool {
161178
return (
162179
offset >= BlobCore.BYTE_LENGTH &&
163180
size >= 0 &&
@@ -172,7 +189,10 @@ export class BlobCore extends Struct {
172189
* @param offset Byte offset.
173190
* @returns String pointer if null terminated string or null.
174191
*/
175-
public static stringAt(_this: BlobCore, offset: number): Int8Ptr | null {
192+
public static stringAt(
193+
_this: BlobCore,
194+
offset: BlobCoreOffset,
195+
): Int8Ptr | null {
176196
let length = BlobCore.size(_this);
177197
if (offset >= 0 && offset < length) {
178198
const s = BlobCore.at(_this, Int8Ptr, offset) as Int8Ptr;
@@ -222,7 +242,7 @@ export class BlobCore extends Struct {
222242
* @param _this This.
223243
* @returns Uint8 byte array.
224244
*/
225-
public static innerData(_this: BlobCore): Const<Arr<number>> {
245+
public static innerData(_this: BlobCore): Const<Arr<uint8_t>> {
226246
const o = BlobCore.BYTE_LENGTH;
227247
const p = BlobCore.at(_this, Uint8Ptr, o);
228248
return new (array(Uint8Ptr, BlobCore.size(_this) - o))(
@@ -240,7 +260,7 @@ export class BlobCore extends Struct {
240260
* @param BlobType Blob type.
241261
* @returns Is the same type.
242262
*/
243-
public static is(this: BlobCoreBlobType, _this: BlobCore): boolean {
263+
public static is(this: BlobCoreBlobType, _this: BlobCore): bool {
244264
return BlobCore.magic(_this) === this.typeMagic;
245265
}
246266

@@ -252,7 +272,7 @@ export class BlobCore extends Struct {
252272
*/
253273
public static async readBlob(
254274
reader: Reader,
255-
context?: { errno: number },
275+
context?: { errno: int },
256276
): Promise<BlobCore | null> {
257277
return await BlobCore.readBlobInternal(reader, 0, 0, 0, 0, context);
258278
}
@@ -269,11 +289,11 @@ export class BlobCore extends Struct {
269289
*/
270290
protected static async readBlobInternal(
271291
reader: Reader,
272-
offset: number,
273-
magic: number,
274-
minSize: number,
275-
maxSize: number,
276-
context?: { errno: number },
292+
offset: size_t,
293+
magic: uint32_t,
294+
minSize: size_t,
295+
maxSize: size_t,
296+
context?: { errno: int },
277297
): Promise<BlobCore | null> {
278298
reader = reader.slice(offset);
279299
if (reader.size < 8) {
@@ -310,12 +330,12 @@ export class BlobCore extends Struct {
310330
/**
311331
* Magic number.
312332
*/
313-
declare protected mMagic: number;
333+
declare protected mMagic: Endian<uint32_t>;
314334

315335
/**
316336
* Blob length.
317337
*/
318-
declare protected mLength: number;
338+
declare protected mLength: Endian<uint32_t>;
319339

320340
static {
321341
toStringTag(this, 'BlobCore');
@@ -340,7 +360,7 @@ export abstract class Blob extends BlobCore {
340360
* @param _this This.
341361
* @param size Length.
342362
*/
343-
public static initializeSize(_this: Blob, size = 0): void {
363+
public static initializeSize(_this: Blob, size: size_t = 0): void {
344364
BlobCore.initialize(_this, this.typeMagic, size);
345365
}
346366

@@ -349,7 +369,7 @@ export abstract class Blob extends BlobCore {
349369
*
350370
* @returns Type magic number.
351371
*/
352-
public static readonly typeMagic: number = 0;
372+
public static readonly typeMagic: BlobCoreMagic = 0;
353373

354374
/**
355375
* Validate blob with length, using known type magic.
@@ -360,8 +380,8 @@ export abstract class Blob extends BlobCore {
360380
*/
361381
public static validateBlobSize(
362382
_this: Blob,
363-
context?: { errno: number },
364-
): boolean;
383+
context?: { errno: int },
384+
): bool;
365385

366386
/**
367387
* Validate blob with length, using known type magic.
@@ -373,9 +393,9 @@ export abstract class Blob extends BlobCore {
373393
*/
374394
public static validateBlobSize(
375395
_this: Blob,
376-
length: number,
377-
context?: { errno: number },
378-
): boolean;
396+
length: size_t,
397+
context?: { errno: int },
398+
): bool;
379399

380400
/**
381401
* Validate blob with length, using known type magic.
@@ -387,16 +407,16 @@ export abstract class Blob extends BlobCore {
387407
*/
388408
public static validateBlobSize(
389409
_this: Blob,
390-
length?: number | { errno: number },
391-
context?: { errno: number },
392-
): boolean {
410+
length?: size_t | { errno: int },
411+
context?: { errno: int },
412+
): bool {
393413
if (typeof length === 'number') {
394414
return (
395415
length >= _this.byteLength &&
396416
Blob.validateBlobSize.call<
397417
typeof this,
398418
[typeof _this, typeof context],
399-
boolean
419+
bool
400420
>(this, _this, context) &&
401421
_this.mLength === length
402422
);
@@ -422,13 +442,13 @@ export abstract class Blob extends BlobCore {
422442
public static specific<T extends TemplateBlob>(
423443
this: T,
424444
blob: BlobCore,
425-
context?: { errno: number },
445+
context?: { errno: int },
426446
): T['prototype'] | null {
427447
const p = new this(blob.buffer, blob.byteOffset, blob.littleEndian);
428448
return Blob.validateBlobSize.call<
429449
typeof this,
430450
[typeof p, typeof context],
431-
boolean
451+
bool
432452
>(this, p, context)
433453
? p
434454
: null;
@@ -469,7 +489,7 @@ export abstract class Blob extends BlobCore {
469489
public static override clone<T extends TemplateBlob>(
470490
this: T,
471491
_this: Blob,
472-
context?: { errno: number },
492+
context?: { errno: int },
473493
): T['prototype'] | null {
474494
const c = BlobCore.clone(_this);
475495
return c && Blob.specific.call(this, c, context);
@@ -487,7 +507,7 @@ export abstract class Blob extends BlobCore {
487507
public static override async readBlob<T extends TemplateBlob>(
488508
this: T,
489509
reader: Reader,
490-
context?: { errno: number },
510+
context?: { errno: int },
491511
): Promise<T['prototype'] | null>;
492512

493513
/**
@@ -504,9 +524,9 @@ export abstract class Blob extends BlobCore {
504524
public static override async readBlob<T extends TemplateBlob>(
505525
this: T,
506526
reader: Reader,
507-
offset: number,
508-
maxSize?: number,
509-
context?: { errno: number },
527+
offset: size_t,
528+
maxSize?: size_t,
529+
context?: { errno: int },
510530
): Promise<T['prototype'] | null>;
511531

512532
/**
@@ -523,9 +543,9 @@ export abstract class Blob extends BlobCore {
523543
public static override async readBlob<T extends TemplateBlob>(
524544
this: T,
525545
reader: Reader,
526-
offset?: number | { errno: number },
527-
maxSize?: number,
528-
context?: { errno: number },
546+
offset?: size_t | { errno: int },
547+
maxSize?: size_t,
548+
context?: { errno: int },
529549
): Promise<T['prototype'] | null> {
530550
if (typeof offset !== 'number') {
531551
context = offset;
@@ -562,7 +582,7 @@ export class BlobWrapper extends Blob {
562582
* @param magic Magic number.
563583
* @returns Blob.
564584
*/
565-
public static alloc(length: number, magic?: number): BlobWrapper;
585+
public static alloc(length: size_t, magic?: BlobCoreMagic): BlobWrapper;
566586

567587
/**
568588
* Wrap data into a new blob.
@@ -574,8 +594,8 @@ export class BlobWrapper extends Blob {
574594
*/
575595
public static alloc(
576596
data: ArrayBufferPointer | ArrayBufferLike,
577-
length: number,
578-
magic?: number,
597+
length: size_t,
598+
magic?: BlobCoreMagic,
579599
): BlobWrapper;
580600

581601
/**
@@ -587,9 +607,9 @@ export class BlobWrapper extends Blob {
587607
* @returns Blob.
588608
*/
589609
public static alloc(
590-
data: number | ArrayBufferPointer | ArrayBufferLike,
591-
length?: number,
592-
magic?: number,
610+
data: size_t | ArrayBufferPointer | ArrayBufferLike,
611+
length?: size_t,
612+
magic?: BlobCoreMagic,
593613
): BlobWrapper {
594614
const { BYTE_LENGTH } = BlobWrapper;
595615
let view;
@@ -614,7 +634,7 @@ export class BlobWrapper extends Blob {
614634
/**
615635
* Data of payload (only).
616636
*/
617-
declare public readonly dataArea: Uint8Ptr;
637+
declare public readonly dataArea: Arr<uchar>;
618638

619639
/**
620640
* Data of payload (only).
@@ -638,7 +658,7 @@ export class BlobWrapper extends Blob {
638658
* @param _this This.
639659
* @returns Byte length.
640660
*/
641-
public static override length(_this: BlobWrapper): number;
661+
public static override length(_this: BlobWrapper): size_t;
642662

643663
/**
644664
* Set blob length for full blob, including magic and length.
@@ -647,7 +667,7 @@ export class BlobWrapper extends Blob {
647667
* @param _this This.
648668
* @param size Byte length.
649669
*/
650-
public static override length(_this: BlobWrapper, size: number): void;
670+
public static override length(_this: BlobWrapper, size: size_t): void;
651671

652672
/**
653673
* Get or set blob length.
@@ -658,8 +678,8 @@ export class BlobWrapper extends Blob {
658678
*/
659679
public static override length(
660680
_this: BlobWrapper,
661-
size?: number,
662-
): number | void {
681+
size?: size_t,
682+
): size_t | void {
663683
if (size === undefined) {
664684
return BlobCore.size(_this) - BlobCore.BYTE_LENGTH;
665685
}

Security/endian.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* NBO endian.
3+
*/
4+
export type Endian<T extends number | bigint> = T;

Security/mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export * from './blob.ts';
22
export * from './CodeSigning/mod.ts';
33
export * from './CSCommon.ts';
44
export * from './CSCommonPriv.ts';
5+
export * from './endian.ts';
56
export * from './errors.ts';
67
export * from './hashing.ts';
78
export * from './LowLevelMemoryUtilities/mod.ts';

0 commit comments

Comments
 (0)