manifest_mbdb_parse.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. const crypto = require('crypto')
  2. const fs = require('fs')
  3. const BufferReader = require('buffer-reader');
  4. const getInt = (reader, intSize, debug) => {
  5. //Retrieve an integer (big-endian)
  6. let value = 0
  7. while (intSize > 0) {
  8. let int8 = reader.nextUInt8()
  9. value = (value << 8) + int8
  10. intSize = intSize - 1
  11. }
  12. return value
  13. }
  14. const getString = (reader) => {
  15. if (reader.nextBuffer(1).toString('hex') === 'ff') {
  16. if (reader.nextBuffer(1).toString('hex') === 'ff')
  17. return '' // Blank string
  18. else reader.move(-2) //Move reader back if is a valid string length
  19. } else reader.move(-1) //Move reader back accordingly
  20. const length = getInt(reader, 2) // 2-byte length
  21. value = reader.nextString(length)
  22. return value
  23. }
  24. module.exports.process = (filename, resolve, reject) => {
  25. let mbdb = []
  26. let contents = null
  27. try {
  28. contents = fs.readFileSync(filename)
  29. const reader = new BufferReader(contents);
  30. if (reader.nextString(4) === 'mbdb' && reader.nextBuffer(2).toString('hex') === '0500'){
  31. while (reader.tell() < contents.byteLength) {
  32. let fileInfo = {}
  33. fileInfo['domain'] = getString(reader)
  34. fileInfo['filename'] = getString(reader)
  35. fileInfo['linktarget'] = getString(reader)
  36. fileInfo['datahash'] = getString(reader)
  37. fileInfo['enckey'] = getString(reader)
  38. fileInfo['mode'] = getInt(reader, 2)
  39. fileInfo['inode'] = getInt(reader, 8)
  40. fileInfo['userid'] = getInt(reader, 4)
  41. fileInfo['groupid'] = getInt(reader, 4)
  42. fileInfo['mtime'] = getInt(reader, 4)
  43. fileInfo['atime'] = getInt(reader, 4)
  44. fileInfo['ctime'] = getInt(reader, 4)
  45. fileInfo['filelen'] = getInt(reader, 8)
  46. fileInfo['flag'] = getInt(reader, 1)
  47. fileInfo['numprops'] = getInt(reader, 1)
  48. fileInfo['properties'] = {}
  49. for (let i = 0; i < fileInfo['numprops']; i++) {
  50. fileInfo['properties'][getString(reader)] = getString(reader)
  51. }
  52. const fullpath = fileInfo['domain'] + '-' + fileInfo['filename']
  53. fileInfo['fileID'] = crypto.createHash('sha1').update(fullpath).digest('hex')
  54. mbdb.push(fileInfo)
  55. }
  56. } else {
  57. const err = 'This does not look like an MBDB file'
  58. reject(err)
  59. }
  60. } catch (e) {
  61. console.log('Cannot open Manifest.mbdb')
  62. reject(e)
  63. }
  64. resolve(mbdb)
  65. }