mp4info.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include "stdafx.h"
  2. #include "mp4info.h"
  3. static unsigned int big_to_small_endian_32bit(unsigned int const big)
  4. {
  5. unsigned int data = big;
  6. unsigned int uret = 0;
  7. uret += (data >> 0 & 0xff) << 24;
  8. uret += (data >> 8 & 0xff) << 16;
  9. uret += (data >> 16 & 0xff) << 8;
  10. uret += (data >> 24 & 0xff) << 0;
  11. return uret;
  12. }
  13. //ftyp, free, mdat box not encryt
  14. uint32_t get_noencrypt_boxs_size(FILE* pSrcFile, const filecryption_callback_t* pcallback, bool bgetmoov)
  15. {
  16. uint32_t uret = 0;
  17. if (NULL == pSrcFile) {
  18. return uret;
  19. }
  20. int ipostion = ftell(pSrcFile);
  21. //ftyp box
  22. box_head_t box_head;
  23. fread(&box_head, sizeof(box_head_t), 1, pSrcFile);
  24. unsigned int uboxsize = big_to_small_endian_32bit(box_head.ibox_size);
  25. if (BOX_FTYP != big_to_small_endian_32bit(box_head.box_type)) {
  26. safe_log(pcallback, FILECRYPT_LEVEL_ERROR, "get ftyp box failed.");
  27. return 0;
  28. }
  29. safe_log(pcallback, FILECRYPT_LEVEL_DEBUG, "get ftyp box success, box size is %d.", uboxsize);
  30. uret += uboxsize;
  31. fseek(pSrcFile, uboxsize - sizeof(box_head_t), SEEK_CUR);
  32. //free box
  33. fread(&box_head, sizeof(box_head_t), 1, pSrcFile);
  34. uboxsize = big_to_small_endian_32bit(box_head.ibox_size);
  35. if (FREE_BOX != big_to_small_endian_32bit(box_head.box_type)) {
  36. safe_log(pcallback, FILECRYPT_LEVEL_ERROR, "get free box failed.");
  37. return 0;
  38. }
  39. safe_log(pcallback, FILECRYPT_LEVEL_DEBUG, "get free box success, box size is %d.", uboxsize);
  40. uret += uboxsize;
  41. fseek(pSrcFile, uboxsize - sizeof(box_head_t), SEEK_CUR);
  42. //mdat box
  43. fread(&box_head, sizeof(box_head_t), 1, pSrcFile);
  44. uboxsize = big_to_small_endian_32bit(box_head.ibox_size);
  45. if (MDAT_BOX != big_to_small_endian_32bit(box_head.box_type)) {
  46. safe_log(pcallback, FILECRYPT_LEVEL_ERROR, "get mdat box failed.");
  47. return 0;
  48. }
  49. safe_log(pcallback, FILECRYPT_LEVEL_DEBUG, "get mdat box success, box size is %d.", uboxsize);
  50. uret += uboxsize;
  51. safe_log(pcallback, FILECRYPT_LEVEL_DEBUG, "ftyp box, free box and mdat box total size is %u.", uret);
  52. //moov box
  53. if (bgetmoov) {
  54. fseek(pSrcFile, uboxsize - sizeof(box_head_t), SEEK_CUR);
  55. fread(&box_head, sizeof(box_head_t), 1, pSrcFile);
  56. uboxsize = big_to_small_endian_32bit(box_head.ibox_size);
  57. if (MOOV_BOX != big_to_small_endian_32bit(box_head.box_type)) {
  58. safe_log(pcallback, FILECRYPT_LEVEL_ERROR, "get moov box failed.");
  59. return 0;
  60. }
  61. safe_log(pcallback, FILECRYPT_LEVEL_DEBUG, "get moov box success, box size is %d.", uboxsize);
  62. }
  63. fseek(pSrcFile, ipostion, SEEK_SET);
  64. return uret;
  65. }
  66. bool is_mp4file_completed(const char* pSrcName, const filecryption_callback_t* pcallback)
  67. {
  68. bool bret = false;
  69. if (NULL == pSrcName) {
  70. return bret;
  71. }
  72. FILE* pSrcFile = fopen(pSrcName, "rb");
  73. if (NULL == pSrcFile) {
  74. return bret;
  75. }
  76. unsigned int unoencryptlen = get_noencrypt_boxs_size(pSrcFile, pcallback, true);
  77. fclose(pSrcFile);
  78. if (0 == unoencryptlen) {
  79. return bret;
  80. }
  81. bret = true;
  82. return bret;
  83. }