From 32adbf2124eb59a9131731e5041edee3397cebde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Monda?= Date: Fri, 25 Mar 2016 21:02:18 +0100 Subject: [PATCH] Deserialize the binary representation of Keystroke. Check scancode validity. --- model/KeystrokeAction.ts | 18 ++++++++++++++++++ model/serializeConfig.ts | 2 -- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/model/KeystrokeAction.ts b/model/KeystrokeAction.ts index fddeb782..1d55fd7a 100644 --- a/model/KeystrokeAction.ts +++ b/model/KeystrokeAction.ts @@ -1,9 +1,24 @@ /// class KeystrokeAction implements Serializable { + + private static firstValidScancode = 1; + private static lastValidScancode = 231; + scancode: number; modifierMask: number; + static isScancodeValid(scancode) { + return KeystrokeAction.firstValidScancode <= scancode && + scancode <= KeystrokeAction.lastValidScancode; + } + + private static checkScancode(scancode) { + if (!KeystrokeAction.isScancodeValid(scancode)) { + throw 'Scancode ${scancode} is invalid'; + } + } + fromJsObject(jsObject: any) { this.scancode = jsObject.scancode; this.modifierMask = jsObject.modifierMask; @@ -17,6 +32,9 @@ class KeystrokeAction implements Serializable { } fromBinary(buffer: UhkBuffer) { + this.scancode = buffer.readUInt8(); + KeystrokeAction.checkScancode(this.scancode); + this.modifierMask = buffer.readUInt8(); } toBinary(buffer: UhkBuffer) { diff --git a/model/serializeConfig.ts b/model/serializeConfig.ts index fe3463d7..0a55aa6e 100644 --- a/model/serializeConfig.ts +++ b/model/serializeConfig.ts @@ -9,8 +9,6 @@ let keyActions = uhkConfig.keymaps[0].layers[0].modules[0].keyActions; let ARRAY_LAST_ELEMENT_ID = 0; -let KEY_ACTION_ID_KEYSTROKE_SCANCODE_FIRST = 1; -let KEY_ACTION_ID_KEYSTROKE_SCANCODE_LAST = 231; let KEY_ACTION_ID_SWITCH_LAYER = 232; let KEY_ACTION_ID_DUAL_ROLE_KEYSTROKE_MOD = 233; let KEY_ACTION_ID_DUAL_ROLE_KEYSTROKE_FN = 234;