find-name-end.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. 'use strict'
  2. /**
  3. * Find the ending position of the attribute type name portion of an RDN.
  4. * This function does not verify if the name is a valid description string
  5. * or numeric OID. It merely reads a string from the given starting position
  6. * to the spec defined end of an attribute type string.
  7. *
  8. * @param {Buffer} searchBuffer A buffer representing the RDN.
  9. * @param {number} startPos The position in the `searchBuffer` to start
  10. * searching from.
  11. *
  12. * @returns {number} The position of the end of the RDN's attribute type name,
  13. * or `-1` if an invalid character has been encountered.
  14. */
  15. module.exports = function findNameEnd ({ searchBuffer, startPos }) {
  16. let pos = startPos
  17. while (pos < searchBuffer.byteLength) {
  18. const char = searchBuffer[pos]
  19. if (char === 0x20 || char === 0x3d) {
  20. // Name ends with a space or an '=' character.
  21. break
  22. }
  23. if (isValidNameChar(char) === true) {
  24. pos += 1
  25. continue
  26. }
  27. return -1
  28. }
  29. return pos
  30. }
  31. /**
  32. * Determine if a character is a valid `attributeType` character as defined
  33. * in RFC 4514 §3.
  34. *
  35. * @param {number} c The character to verify. Should be the byte representation
  36. * of the character from a {@link Buffer} instance.
  37. *
  38. * @returns {boolean}
  39. */
  40. function isValidNameChar (c) {
  41. if (c >= 0x41 && c <= 0x5a) { // A - Z
  42. return true
  43. }
  44. if (c >= 0x61 && c <= 0x7a) { // a - z
  45. return true
  46. }
  47. if (c >= 0x30 && c <= 0x39) { // 0 - 9
  48. return true
  49. }
  50. if (c === 0x2d || c === 0x2e) { // - or .
  51. return true
  52. }
  53. return false
  54. }