You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
3.1 KiB

1 year ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace CDPShared
  8. {
  9. public class YellowFile
  10. {
  11. private string _filePath;
  12. public string FileId { get; set; } // this is actually the FMAP Id internally
  13. public string CloudId { get; set; } // this isn't being used yet. Gene 2021.11.17
  14. public string CircleId { get; set; }
  15. public string TopicId { get; set; }
  16. public string DecryptionId { get; set; }
  17. public string OwnerId { get; set; } // this is a device Id
  18. public Int32 RevisionNo { get; set; }
  19. public Int32 EncryptionMethod { get; set; }
  20. public UInt64 EncryptedBlockSize { get; set; }
  21. public DateTime FileAccessTime { get; set; } // I'm not sure when this gets 'set' or what it should be. but make it UTC
  22. public DateTime FileModificationTime { get; set; } // this should be when the file header was built in UTC
  23. public DateTime FileStatusTime { get; set; } // not sure on this one either.
  24. public string Comment;
  25. public YellowFile(string filePath)
  26. {
  27. _filePath = filePath;
  28. using (FileStream fsIn = new FileStream(_filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
  29. {
  30. ReadHeader(fsIn);
  31. }
  32. }
  33. protected void ReadHeader(FileStream fs)
  34. {
  35. BinaryReader br = new BinaryReader(fs);
  36. var versionBytes = br.ReadBytes(8); // should be 'Circle1'
  37. UInt64 encyptedDataPos = br.ReadUInt64();
  38. FileId = new Guid(br.ReadBytes(16)).ToString();
  39. CloudId = new Guid(br.ReadBytes(16)).ToString();
  40. CircleId = new Guid(br.ReadBytes(16)).ToString();
  41. TopicId = new Guid(br.ReadBytes(16)).ToString();
  42. DecryptionId = new Guid(br.ReadBytes(16)).ToString();
  43. OwnerId = new Guid(br.ReadBytes(16)).ToString();
  44. // the file format has IV being 32 byte, but we only need 16
  45. br.ReadBytes(16); // skip over IV
  46. br.ReadBytes(16); // skip over the extra 16 bytes
  47. RevisionNo = br.ReadInt32();
  48. EncryptionMethod = br.ReadInt32();
  49. EncryptedBlockSize = br.ReadUInt64();
  50. FileAccessTime = DateTime.FromBinary(br.ReadInt64());
  51. FileModificationTime = DateTime.FromBinary(br.ReadInt64());
  52. FileStatusTime = DateTime.FromBinary(br.ReadInt64());
  53. Comment = Encoding.Unicode.GetString(br.ReadBytes(256)).Trim('\0');
  54. br.ReadBytes(40); // reserved
  55. byte[] hash = br.ReadBytes(32);
  56. Debug.Assert(fs.Position == 512);
  57. }
  58. public void UpdateOwner(string ownerId)
  59. {
  60. Guid owner = Guid.Parse(ownerId);
  61. using (FileStream fs = new FileStream(_filePath, FileMode.Open))
  62. {
  63. fs.Position = 96;
  64. byte[] guidBytes = owner.ToByteArray();
  65. fs.Write(guidBytes, 0, guidBytes.Length);
  66. }
  67. }
  68. }
  69. }