index.js 823 B

1234567891011121314151617181920212223242526272829303132
  1. 'use strict'
  2. module.exports = function parseBytea (input) {
  3. if (/^\\x/.test(input)) {
  4. // new 'hex' style response (pg >9.0)
  5. return new Buffer(input.substr(2), 'hex')
  6. }
  7. var output = ''
  8. var i = 0
  9. while (i < input.length) {
  10. if (input[i] !== '\\') {
  11. output += input[i]
  12. ++i
  13. } else {
  14. if (/[0-7]{3}/.test(input.substr(i + 1, 3))) {
  15. output += String.fromCharCode(parseInt(input.substr(i + 1, 3), 8))
  16. i += 4
  17. } else {
  18. var backslashes = 1
  19. while (i + backslashes < input.length && input[i + backslashes] === '\\') {
  20. backslashes++
  21. }
  22. for (var k = 0; k < Math.floor(backslashes / 2); ++k) {
  23. output += '\\'
  24. }
  25. i += Math.floor(backslashes / 2) * 2
  26. }
  27. }
  28. }
  29. return new Buffer(output, 'binary')
  30. }