basant307/AI_Governance_Project
048
1"use strict";2Object.defineProperty(exports, "__esModule", { value: true });3exports.XdrEncoder = void 0;4class XdrEncoder {5 constructor(writer) {6 this.writer = writer;7 }8 encode(value) {9 const writer = this.writer;10 writer.reset();11 this.writeAny(value);12 return writer.flush();13 }14 writeUnknown(value) {15 this.writeVoid();16 }17 writeAny(value) {18 switch (typeof value) {19 case 'boolean':20 return this.writeBoolean(value);21 case 'number':22 return this.writeNumber(value);23 case 'string':24 return this.writeStr(value);25 case 'object': {26 if (value === null)27 return this.writeVoid();28 const constructor = value.constructor;29 switch (constructor) {30 case Uint8Array:31 return this.writeBin(value);32 default:33 return this.writeUnknown(value);34 }35 }36 case 'bigint':37 return this.writeHyper(value);38 case 'undefined':39 return this.writeVoid();40 default:41 return this.writeUnknown(value);42 }43 }44 writeVoid() {45 }46 writeNull() {47 this.writeVoid();48 }49 writeBoolean(bool) {50 this.writeInt(bool ? 1 : 0);51 }52 writeInt(int) {53 const writer = this.writer;54 writer.ensureCapacity(4);55 writer.view.setInt32(writer.x, Math.trunc(int), false);56 writer.move(4);57 }58 writeUnsignedInt(uint) {59 const writer = this.writer;60 writer.ensureCapacity(4);61 writer.view.setUint32(writer.x, Math.trunc(uint) >>> 0, false);62 writer.move(4);63 }64 writeHyper(hyper) {65 const writer = this.writer;66 writer.ensureCapacity(8);67 if (typeof hyper === 'bigint') {68 writer.view.setBigInt64(writer.x, hyper, false);69 }70 else {71 const truncated = Math.trunc(hyper);72 const high = Math.floor(truncated / 0x100000000);73 const low = truncated >>> 0;74 writer.view.setInt32(writer.x, high, false);75 writer.view.setUint32(writer.x + 4, low, false);76 }77 writer.move(8);78 }79 writeUnsignedHyper(uhyper) {80 const writer = this.writer;81 writer.ensureCapacity(8);82 if (typeof uhyper === 'bigint') {83 writer.view.setBigUint64(writer.x, uhyper, false);84 }85 else {86 const truncated = Math.trunc(Math.abs(uhyper));87 const high = Math.floor(truncated / 0x100000000);88 const low = truncated >>> 0;89 writer.view.setUint32(writer.x, high, false);90 writer.view.setUint32(writer.x + 4, low, false);91 }92 writer.move(8);93 }94 writeFloat(float) {95 const writer = this.writer;96 writer.ensureCapacity(4);97 writer.view.setFloat32(writer.x, float, false);98 writer.move(4);99 }100 writeDouble(double) {101 const writer = this.writer;102 writer.ensureCapacity(8);103 writer.view.setFloat64(writer.x, double, false);104 writer.move(8);105 }106 writeQuadruple(quad) {107 throw new Error('not implemented');108 }109 writeOpaque(data) {110 const size = data.length;111 const writer = this.writer;112 const paddedSize = Math.ceil(size / 4) * 4;113 writer.ensureCapacity(paddedSize);114 writer.buf(data, size);115 const padding = paddedSize - size;116 for (let i = 0; i < padding; i++) {117 writer.u8(0);118 }119 }120 writeVarlenOpaque(data) {121 this.writeUnsignedInt(data.length);122 this.writeOpaque(data);123 }124 writeStr(str) {125 const writer = this.writer;126 const lengthOffset = writer.x;127 writer.x += 4;128 const bytesWritten = writer.utf8(str);129 const paddedSize = Math.ceil(bytesWritten / 4) * 4;130 const padding = paddedSize - bytesWritten;131 for (let i = 0; i < padding; i++) {132 writer.u8(0);133 }134 const currentPos = writer.x;135 writer.x = lengthOffset;136 this.writeUnsignedInt(bytesWritten);137 writer.x = currentPos;138 }139 writeArr(arr) {140 throw new Error('not implemented');141 }142 writeObj(obj) {143 throw new Error('not implemented');144 }145 writeNumber(num) {146 if (Number.isInteger(num)) {147 if (num >= -2147483648 && num <= 2147483647) {148 this.writeInt(num);149 }150 else {151 this.writeHyper(num);152 }153 }154 else {155 this.writeDouble(num);156 }157 }158 writeInteger(int) {159 this.writeInt(int);160 }161 writeUInteger(uint) {162 this.writeUnsignedInt(uint);163 }164 writeBin(buf) {165 this.writeVarlenOpaque(buf);166 }167 writeAsciiStr(str) {168 this.writeStr(str);169 }170}171exports.XdrEncoder = XdrEncoder;172//# sourceMappingURL=XdrEncoder.js.map