start implementing in ts

This commit is contained in:
DavidVentura
2024-01-25 00:09:57 +01:00
parent 65d033b82d
commit 094ffc0914
2 changed files with 41 additions and 6 deletions
+17
View File
@@ -0,0 +1,17 @@
import "./shim.ts";
const str2byte = (s: string): number[] => {
return Array.from(s).map((_, i) => s.charCodeAt(i));
};
const SendUsrChk = (username: string, password: string): DataView => {
// type is char account[0x20]; char password[0x80];
let buf = new Uint8Array(0x20 + 0x80);
buf.fill(0);
let cmd_payload = new DataView(buf.buffer);
cmd_payload.writeByteArray(str2byte(username));
cmd_payload.add(0x20).writeByteArray(str2byte(password));
return cmd_payload;
};
console.log(SendUsrChk("admin", "admin"));
+24 -6
View File
@@ -25,8 +25,13 @@ DataView.prototype.readU32 = function () {
DataView.prototype.readU64 = function () {
return this.getUint64(0);
};
const isDataView = (x: DataView | number[]): x is DataView => {
return x instanceof DataView;
};
DataView.prototype.writeByteArray = function (arr) {
for (let i = 0; i < (arr.byteLength || arr.length); i++) {
const len = isDataView(arr) ? arr.byteLength : arr.length;
for (let i = 0; i < len; i++) {
this.setUint8(i, arr[i]);
}
};
@@ -39,10 +44,23 @@ DataView.prototype.readByteArray = function (len) {
};
const Memory = {
alloc: (len) => new DataView(new ArrayBuffer(len + 1)),
copy: (outbuf, inbuf, len) => outbuf.writeByteArray(inbuf.readByteArray(len)),
alloc: (len: number) => new DataView(new ArrayBuffer(len + 1)),
copy: (outbuf: DataView, inbuf: DataView, len: number) => outbuf.writeByteArray(inbuf.readByteArray(len)),
};
//module.exports = {
// Memory,
//};
declare global {
interface DataView {
add(offset: number): DataView;
readByteArray(len: number): DataView;
readU16(): undefined;
readU32(): undefined;
readU64(): undefined;
readU8(): undefined;
writeByteArray(arr: DataView | number[]): undefined;
writeU16(n: number): undefined;
writeU32(n: number): undefined;
writeU64(n: number): undefined;
writeU8(n: number): undefined;
}
}
export default global;