Attachments.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "Attachments.h"
  2. #include "read.h"
  3. #include "global_elements.h"
  4. static uint64_t ReadAttachedFile(nsmkv::MKVReader *reader, uint64_t size, nsmkv::AttachedFile &attached_file)
  5. {
  6. uint64_t total_bytes_read=0;
  7. while (size)
  8. {
  9. ebml_node node;
  10. uint64_t bytes_read = read_ebml_node(reader, &node);
  11. if (bytes_read == 0)
  12. return 0;
  13. // benski> checking bytes_read and node.size separately prevents possible integer overflow attack
  14. if (bytes_read > size)
  15. return 0;
  16. total_bytes_read+=bytes_read;
  17. size-=bytes_read;
  18. if (node.size > size)
  19. return 0;
  20. total_bytes_read+=node.size;
  21. size-=node.size;
  22. switch(node.id)
  23. {
  24. case mkv_attachments_filename:
  25. {
  26. char *utf8 = 0;
  27. if (read_utf8(reader, node.size, &utf8) == 0)
  28. return 0;
  29. if (utf8)
  30. printf("Filename: %s\n", utf8);
  31. attached_file.Own(attached_file.filename, utf8);
  32. }
  33. break;
  34. case mkv_attachments_filemimetype:
  35. {
  36. char *utf8 = 0;
  37. if (read_utf8(reader, node.size, &utf8) == 0)
  38. return 0;
  39. if (utf8)
  40. printf("File MIME Type: %s\n", utf8);
  41. attached_file.Own(attached_file.mime_type, utf8);
  42. }
  43. break;
  44. case mkv_attachments_filedata:
  45. {
  46. printf("File Data: binary size %I64u\n", node.size);
  47. reader->Skip(node.size);
  48. }
  49. break;
  50. case mkv_attachments_fileuid:
  51. {
  52. uint64_t val;
  53. if (read_unsigned(reader, node.size, &val) == 0)
  54. return 0;
  55. printf("File UID: %I64x\n", val);
  56. attached_file.file_uid = val;
  57. }
  58. break;
  59. default:
  60. nsmkv::ReadGlobal(reader, node.id, node.size);
  61. }
  62. }
  63. return total_bytes_read;
  64. }
  65. uint64_t nsmkv::ReadAttachment(nsmkv::MKVReader *reader, uint64_t size, nsmkv::Attachments &attachments)
  66. {
  67. uint64_t total_bytes_read=0;
  68. while (size)
  69. {
  70. ebml_node node;
  71. uint64_t bytes_read = read_ebml_node(reader, &node);
  72. if (bytes_read == 0)
  73. return 0;
  74. // benski> checking bytes_read and node.size separately prevents possible integer overflow attack
  75. if (bytes_read > size)
  76. return 0;
  77. total_bytes_read+=bytes_read;
  78. size-=bytes_read;
  79. if (node.size > size)
  80. return 0;
  81. total_bytes_read+=node.size;
  82. size-=node.size;
  83. switch(node.id)
  84. {
  85. case mkv_attachments_attachedfile:
  86. {
  87. printf("Attachmented File\\n");
  88. nsmkv::AttachedFile attached_file;
  89. ReadAttachedFile(reader, node.size, attached_file);
  90. }
  91. break;
  92. default:
  93. ReadGlobal(reader, node.id, node.size);
  94. }
  95. }
  96. return total_bytes_read;
  97. }