import {AxdrType} from "../asn1.axdr/AxdrType"; import {ReverseByteArrayInputStream} from "../ReverseByteArrayInputStream"; import {ReverseByteArrayOutputStream} from "../ReverseByteArrayOutputStream"; import {Buffer} from "buffer"; import {AxdrOctetString} from "../asn1.axdr/AxdrOctetString"; import {AxdrEnum} from "../asn1.axdr/AxdrEnum"; import {MAC} from "./MAC"; import {AxdrOptional} from "../asn1.axdr/AxdrOptional"; enum AppUnitChoice { _ERR_NONE_SELECTED = -1, APP_UNIT_PALIN = 0, APP_UNIT = 1, DAR = 2, } export class SubChoiceAppUnit implements AxdrType{ dataCode : Buffer | null = null; choice : AppUnitChoice = AppUnitChoice._ERR_NONE_SELECTED; appUnitPalin : AxdrOctetString | null = null; appUnit : AxdrOctetString | null = null; dar : AxdrEnum | null = null; constructor() { } set_dataCode(dataCode : Buffer){ this.dataCode = dataCode; } setAppUnit(appUnit : AxdrOctetString){ this.resetChoices(); this.appUnit = appUnit; this.choice = AppUnitChoice.APP_UNIT; } setAppUnitPalin(appUnitPalin : AxdrOctetString){ this.resetChoices(); this.appUnitPalin = appUnitPalin; this.choice = AppUnitChoice.APP_UNIT_PALIN; } setDar(dar : AxdrEnum){ this.resetChoices(); this.dar = dar; this.choice = AppUnitChoice.DAR; } decode(input: ReverseByteArrayInputStream): number { let codeLength = 0; let choosen = new AxdrEnum(); choosen.set_const(); codeLength += choosen.decode(input); this.resetChoices(); this.choice = choosen.getValue(); if (this.choice == AppUnitChoice.APP_UNIT_PALIN) { this.appUnitPalin = new AxdrOctetString(); codeLength += this.appUnitPalin.decode(input); return codeLength; }else if (this.choice == AppUnitChoice.APP_UNIT) { this.appUnit = new AxdrOctetString(); codeLength += this.appUnit.decode(input); return codeLength; }else if(this.choice == AppUnitChoice.DAR){ this.dar = new AxdrEnum(); this.dar.set_const(); codeLength += this.dar.decode(input); return codeLength; } throw new Error("Error decoding AxdrChoice: Identifier matched to no item."); } encode(output: ReverseByteArrayOutputStream): number { if (this.dataCode != null) { for (let i = this.dataCode.length - 1; i >= 0; i--) { output.write(this.dataCode[i]); } return this.dataCode.length; } if (this.choice == AppUnitChoice._ERR_NONE_SELECTED) { throw new Error("Error encoding AxdrChoice: No item in choice was selected."); } let codeLength = 0; if(this.choice == AppUnitChoice.DAR){ if(this.dar != null){ codeLength += this.dar.encode(output); let c = new AxdrEnum(); c.set_min_max_val(2) codeLength += c.encode(output); return codeLength; }else{ throw new Error("dar is null"); } }else if (this.choice == AppUnitChoice.APP_UNIT) { if(this.appUnit != null){ codeLength += this.appUnit.encode(output); let c = new AxdrEnum(); c.set_min_max_val(1) codeLength += c.encode(output); return codeLength; }else{ throw new Error("appunit is null"); } }else if (this.choice == AppUnitChoice.APP_UNIT_PALIN) { if(this.appUnitPalin != null){ codeLength += this.appUnitPalin.encode(output); let c = new AxdrEnum(); c.set_min_max_val(0); codeLength += c.encode(output); return codeLength; }else{ throw new Error("appunitpalin is null"); } } throw new Error("Error encoding AxdrChoice: No item in choice was encoded."); } encodeAndSave(encodingSizeGuess: number) { let revOStream = new ReverseByteArrayOutputStream(); revOStream.setBufSize(encodingSizeGuess); this.encode(revOStream); this.dataCode = revOStream.getArray(); } getChoiceIndex(): AppUnitChoice { return this.choice; } resetChoices() { this.choice = AppUnitChoice._ERR_NONE_SELECTED; this.appUnitPalin = null; this.appUnit = null; this.dar = null; } toString(): string { if (this.choice == AppUnitChoice.APP_UNIT_PALIN) { if (this.appUnitPalin != null) { return "choice: {appUnitPalin: " + this.appUnitPalin + "}"; } else { return "choice is appUnitPalin but appUnitPalin is null"; } }else if(this.choice == AppUnitChoice.APP_UNIT){ if(this.appUnit != null){ return "choice: {appUnit: " + this.appUnit + "}"; }else{ return "choice is appUnit but appUnit is null"; } }else if(this.choice == AppUnitChoice.DAR){ if(this.dar != null){ return "choice: {dar: " + this.dar + "}"; }else{ return "choice is dar but dar is null"; } }else { return "unknown"; } } } enum DataValidChoice { _ERR_NONE_SELECTED = -1, MAC = 0, } export class SubChoiceDataValid implements AxdrType{ dataCode : Buffer | null = null; choice : DataValidChoice | null = null; mac : MAC | null = null; constructor() { } set_dataCode(dataCode : Buffer){ this.dataCode = dataCode; } setMAC(mac : MAC){ this.resetChoices(); this.choice = DataValidChoice.MAC; this.mac = mac; } decode(input: ReverseByteArrayInputStream): number { let codeLength = 0; let choosen = new AxdrEnum(); choosen.set_const(); codeLength += choosen.decode(input); this.resetChoices(); this.choice = choosen.getValue(); if (this.choice == DataValidChoice.MAC) { this.mac = new MAC(); codeLength += this.mac.decode(input); return codeLength; } throw new Error("Error decoding AxdrChoice: Identifier matched to no item."); } encode(output: ReverseByteArrayOutputStream): number { if (this.dataCode != null) { for (let i = this.dataCode.length - 1; i >= 0; i--) { output.write(this.dataCode[i]); } return this.dataCode.length; } if (this.choice == DataValidChoice._ERR_NONE_SELECTED) { throw new Error("Error encoding AxdrChoice: No item in choice was selected."); } let codeLength = 0; if (this.choice == DataValidChoice.MAC) { codeLength += this.mac.encode(output); let c = new AxdrEnum(); c.set_min_max_val(0); codeLength += c.encode(output); return codeLength; } throw new Error("Error encoding AxdrChoice: No item in choice was encoded."); } encodeAndSave(encodingSizeGuess: number) { let revOStream = new ReverseByteArrayOutputStream(); revOStream.setBufSize(encodingSizeGuess); this.encode(revOStream); this.dataCode = revOStream.getArray(); } getChoiceIndex(): DataValidChoice { return this.choice; } resetChoices() { this.choice = DataValidChoice._ERR_NONE_SELECTED; this.mac = null; } toString(): string { if (this.choice == DataValidChoice.MAC) { if (this.mac != null) { return "choice: {mac: " + this.mac + "}"; } else { return "choice is mac but mac is null"; } }else { return "unknown"; } } } export class Security_Response implements AxdrType{ dataCode : Buffer | null = null; appUnit : SubChoiceAppUnit | null = null; dataValid : AxdrOptional = new AxdrOptional(new SubChoiceDataValid(), false); constructor() { } set_dataCode(dataCode : Buffer){ this.dataCode = dataCode; } set_all(appUnit : SubChoiceAppUnit, dataValid : SubChoiceDataValid){ this.appUnit = appUnit; this.dataValid.setValue(dataValid); } decode(input: ReverseByteArrayInputStream): number { let codeLength = 0; this.appUnit = new SubChoiceAppUnit(); codeLength += this.appUnit.decode(input); this.dataValid = new AxdrOptional(new SubChoiceDataValid(), false); codeLength += this.dataValid.decode(input); return codeLength; } encode(output: ReverseByteArrayOutputStream): number { let codeLength; if (this.dataCode != null) { codeLength = this.dataCode.length; for (let i = this.dataCode.length - 1; i >= 0; i--) { output.write(this.dataCode[i]); } } else { if(this.appUnit != null && this.dataValid != null){ codeLength = 0; codeLength += this.dataValid.encode(output); codeLength += this.appUnit.encode(output); }else{ throw new Error("dataValid or appUnit is null"); } } return codeLength; } encodeAndSave(encodingSizeGuess: number) { let revOStream = new ReverseByteArrayOutputStream(); revOStream.setBufSize(encodingSizeGuess); this.encode(revOStream); this.dataCode = revOStream.getArray(); } toString(): string { if (this.appUnit != null && this.dataValid != null) { return "sequence: {"+ "appUnit: " + this.appUnit + ", dataValid: " + this.dataValid + "}"; } else { return "appUnit or dataValid is null"; } } }