UNPKG

@hclsoftware/secagent

Version:

IAST agent

127 lines (116 loc) 3.87 kB
//IASTIGNORE /* * **************************************************** * Licensed Materials - Property of HCL. * (c) Copyright HCL Technologies Ltd. 2017, 2025. * Note to U.S. Government Users *Restricted Rights. * **************************************************** */ const logger = require('../Logger/IastLogger') const Utils = require('./Utils') module.exports.FixedSizeMap = class FixedSizeMap { constructor (equals, hashFunc, size) { this.equals = equals this.hashFunc = hashFunc this.arr = new Array(size) this.length = 0 } set (key, value) { const hashCode = this.getHashCode(key) const chain = this.arr[hashCode] const newItem = new KeyValuePair(key, value) if (chain == null) { this.arr[hashCode] = [newItem] this.length++ } else { let newKey = true for (let i = 0; i < chain.length; i++) { if (this.equals(chain[i].key, key)) { newKey = false chain[i] = newItem break } } if (newKey) { chain.push(newItem) this.length++ } } } get (key) { const hashCode = this.getHashCode(key) const chain = this.arr[hashCode] if (chain == null) { return undefined } else { for (let i = 0; i < chain.length; i++) { if (this.equals(chain[i].key, key)) { return chain[i].value } } } } delete (key) { const hashCode = this.getHashCode(key) const chain = this.arr[hashCode] if (chain == null || chain.length === 0) { return false } else { for (let i = 0; i < chain.length; i++) { if (this.equals(chain[i].key, key)) { chain.splice(i, 1) this.length-- return true } } } } getHashCode (key) { let hash = this.hashFunc(key) if (typeof hash === 'string' || hash instanceof String) { hash = stringToHashCode(hash) } const res = Math.abs(hash % this.arr.length) if (isNaN(res)) { logger.eventLog.error('got NaN Hash-Code') } return res } [Symbol.iterator] () { return { // this is the iterator object, returning a single element (the string "bye") next: function () { let current = this.chainIter != null ? this.chainIter.next() : null if (current == null || current.done) { let next = null do { next = this.tableIter.next() } while ((next.value == null || next.value.length === 0) && !next.done) if (next.value == null) { return { done: true } } this.chainIter = next.value[Symbol.iterator]() current = this.chainIter.next() } return { value: current.value, done: false } }, tableIter: this.arr[Symbol.iterator](), chainIter: null } } } class KeyValuePair { constructor (key, value) { this.key = key this.value = value } } function stringToHashCode (str) { let hash = 0; let i; let chr for (i = 0; i < str.length; i++) { chr = str.charCodeAt(i) hash = ((hash << 5) - hash) + chr hash |= 0 // Convert to 32bit integer } return hash }