wangzhibo
2026-05-09 53418655c061ac26f66058253f39200aced1ab1d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
'use strict'
 
const { BerReader } = require('@ldapjs/asn1')
 
const isValidHexCode = code => /[0-9a-fA-F]{2}/.test(code) === true
 
/**
 * @typedef {object} ReadHexStringResult
 * @property {number} endPos The position in the buffer where the end of the
 * hex string was encountered.
 * @property {import('@ldapjs/asn1').BerReader} berReader The parsed hex string
 * as an BER object.
 */
 
/**
 * Read a sequence of bytes as a hex encoded octet string. The sequence is
 * assumed to be a spec compliant encoded BER object.
 *
 * @param {Buffer} searchBuffer The buffer to read.
 * @param {number} startPos The position in the buffer to start reading from.
 *
 * @returns {ReadHexStringResult}
 *
 * @throws When an invalid hex pair has been encountered.
 */
module.exports = function readHexString ({ searchBuffer, startPos }) {
  const bytes = []
 
  let pos = startPos
  while (pos < searchBuffer.byteLength) {
    if (isEndChar(searchBuffer[pos])) {
      break
    }
 
    const hexPair = String.fromCharCode(searchBuffer[pos]) +
      String.fromCharCode(searchBuffer[pos + 1])
    if (isValidHexCode(hexPair) === false) {
      throw Error('invalid hex pair encountered: 0x' + hexPair)
    }
 
    bytes.push(parseInt(hexPair, 16))
    pos += 2
  }
 
  return {
    endPos: pos,
    berReader: new BerReader(Buffer.from(bytes))
  }
}
 
function isEndChar (c) {
  switch (c) {
    case 0x20: // space
    case 0x2b: // +
    case 0x2c: // ,
    case 0x3b: // ;
      return true
    default:
      return false
  }
}