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
62
63
64
65
66
67
68
69
70
71
72
73
'use strict'
 
const findNameStart = require('./find-name-start')
const findNameEnd = require('./find-name-end')
const isValidAttributeTypeName = require('./is-valid-attribute-type-name')
const readAttributeValue = require('./read-attribute-value')
 
/**
 * @typedef {object} AttributePair
 * @property {string | import('@ldapjs/asn1').BerReader} name Property name is
 * actually the property name of the attribute pair. The value will be a string,
 * or, in the case of the value being a hex encoded string, an instance of
 * `BerReader`.
 *
 * @example
 * const input = 'foo=bar'
 * const pair = { foo: 'bar' }
 */
 
/**
 * @typedef {object} ReadAttributePairResult
 * @property {number} endPos The ending position in the input search buffer that
 * is the end of the read attribute pair.
 * @property {AttributePair} pair The parsed attribute pair.
 */
 
/**
 * Read an RDN attribute type and attribute value pair from the provided
 * search buffer at the given starting position.
 *
 * @param {Buffer} searchBuffer
 * @param {number} startPos
 *
 * @returns {ReadAttributePairResult}
 *
 * @throws When there is some problem with the input string.
 */
module.exports = function readAttributePair ({ searchBuffer, startPos }) {
  let pos = startPos
 
  const nameStartPos = findNameStart({
    searchBuffer,
    startPos: pos
  })
  if (nameStartPos < 0) {
    throw Error('invalid attribute name leading character encountered')
  }
 
  const nameEndPos = findNameEnd({
    searchBuffer,
    startPos: nameStartPos
  })
  if (nameStartPos < 0) {
    throw Error('invalid character in attribute name encountered')
  }
 
  const attributeName = searchBuffer.subarray(nameStartPos, nameEndPos).toString('utf8')
  if (isValidAttributeTypeName(attributeName) === false) {
    throw Error('invalid attribute type name: ' + attributeName)
  }
 
  const valueReadResult = readAttributeValue({
    searchBuffer,
    startPos: nameEndPos
  })
  pos = valueReadResult.endPos
  const attributeValue = valueReadResult.value
 
  return {
    endPos: pos,
    pair: { [attributeName]: attributeValue }
  }
}