Преглед изворни кода

Z991239-5368 #comment 优化mp4格式录像加密功能

80274480 пре 1 година
родитељ
комит
b342acdbc8

+ 42 - 2
Other/libfilecryption/filecryption.cpp

@@ -107,11 +107,11 @@ static int encrypt_mp4_file(char* poutfilename, char* phead, size_t uheaderlen,
 {
 	int iret = -1;
 
-	unsigned int unoencryptlen = get_noencrypt_boxs_size(pfilename);
+	unsigned int unoencryptlen = get_noencrypt_boxs_size(pfilename, pcallback);
 	if (0 == unoencryptlen) {
 		return iret;
 	}
-	safe_log(pcallback, "not encryt file length is %u.", unoencryptlen);
+	safe_log(pcallback, "not encrypt file length is %u.", unoencryptlen);
 
 	FILE* pSrcFile = fopen(pfilename, "rb");
 	if (NULL == pSrcFile) {
@@ -124,15 +124,55 @@ static int encrypt_mp4_file(char* poutfilename, char* phead, size_t uheaderlen,
 		return iret;
 	}
 
+	fseek(pSrcFile, 0, SEEK_END);
+	unsigned int usrcfilelen = ftell(pSrcFile);
+	rewind(pSrcFile);
+	safe_log(pcallback, "src file length is %u.", usrcfilelen);
+
 	//1. 新增自定义文件头
 	fwrite(phead, 1, uheaderlen, pDestFile);
 
+	//2. 填充ftyp, free, mdat box
+	char* data_buffer = (char*)malloc(RVC_READ_BUFFER_SIZE);
+	unsigned int uleftfilelen = unoencryptlen;
+	do {
+		if (RVC_READ_BUFFER_SIZE == fread(data_buffer, 1, RVC_READ_BUFFER_SIZE, pSrcFile)) {
+			fwrite(data_buffer, 1, RVC_READ_BUFFER_SIZE, pDestFile);
+		}
+		uleftfilelen -= RVC_READ_BUFFER_SIZE;
+	} while (uleftfilelen >= RVC_READ_BUFFER_SIZE);
+
+	if (uleftfilelen > 0) {
+		if (uleftfilelen == fread(data_buffer, 1, uleftfilelen, pSrcFile)) {
+			fwrite(data_buffer, 1, uleftfilelen, pDestFile);
+		}
+	}
+
+	//3. 读取moov box信息
+	//size_t udatalen = 0;
+	//while (udatalen = fread(data_buffer, 1, RVC_READ_BUFFER_SIZE, pSrcFile)) {
+	//	fwrite(data_buffer, 1, udatalen, pDestFile);
+	//}
+
+	uint64_t umoovboxlen = usrcfilelen - unoencryptlen;
+	unsigned char* pmoovboxdata = (unsigned char*)malloc(umoovboxlen);
+	fread(pmoovboxdata, 1, umoovboxlen, pSrcFile);
 
+	int iencdatalen = umoovboxlen + SM4ENC_BLOCK_SIZE;
+	unsigned char* pobjdataenc = (unsigned char*)malloc(iencdatalen);
+	SM4EncECBMode(pstrkey, pmoovboxdata, umoovboxlen, pobjdataenc, &iencdatalen);
+	safe_log(pcallback, "after encode, moov box length is %u.", iencdatalen);
 
+	if (iencdatalen > 0) {
+		fwrite(pobjdataenc, 1, iencdatalen, pDestFile);
+	}
 
 	fclose(pSrcFile);
 	fclose(pDestFile);
 
+	safe_free(data_buffer);
+	safe_free(pmoovboxdata);
+	safe_free(pobjdataenc);
 
 	iret = 0;
 

+ 4 - 1
Other/libfilecryption/filecryption.h

@@ -42,7 +42,10 @@ typedef struct filecryption_callback_s  {
 
 
 #ifndef safe_log
-#define safe_log(x, strformat, ...) if (x && x->dbg){ x->dbg(strformat, ##__VA_ARGS__);}
+#define safe_log(x, strformat, ...) \
+		do{\
+			if (x && x->dbg){ x->dbg(strformat, ##__VA_ARGS__);}\
+		}while(0)
 #endif
 
 

+ 30 - 12
Other/libfilecryption/mp4info.cpp

@@ -1,6 +1,7 @@
 #include "stdafx.h"
 #include "mp4info.h"
 
+
 static unsigned int big_to_small_endian_32bit(unsigned int const big)
 {
 	unsigned int data = big;
@@ -12,6 +13,7 @@ static unsigned int big_to_small_endian_32bit(unsigned int const big)
 	return uret;
 }
 
+
 static __int64 big_to_small_endian_64bit(__int64 const big)
 {
 	__int64 data = big;
@@ -29,7 +31,7 @@ static __int64 big_to_small_endian_64bit(__int64 const big)
 
 
 //ftyp, free, mdat box not encryt
-unsigned int get_noencrypt_boxs_size(const char* pfilename)
+unsigned int get_noencrypt_boxs_size(const char* pfilename, const filecryption_callback_t* pcallback)
 {
 	unsigned int uret = 0;
 	if (NULL == pfilename) {
@@ -45,34 +47,50 @@ unsigned int get_noencrypt_boxs_size(const char* pfilename)
 	//ftyp box
 	box_head_t box_head;
 	fread(&box_head, sizeof(box_head_t), 1, pFile);
-	int iboxsize = big_to_small_endian_32bit(box_head.ibox_size);
+	unsigned int uboxsize = big_to_small_endian_32bit(box_head.ibox_size);
 	if (BOX_FTYP != big_to_small_endian_32bit(box_head.box_type)) {
 		fclose(pFile);
+		safe_log(pcallback, "get ftyp box failed.");
 		return 0;
 	}
-	uret += iboxsize;
-
-	fseek(pFile, iboxsize - sizeof(box_head_t), SEEK_CUR);
+	safe_log(pcallback, "get ftyp box success, box size is %d.", uboxsize);
+	uret += uboxsize;
+	fseek(pFile, uboxsize - sizeof(box_head_t), SEEK_CUR);
 
 	//free box
 	fread(&box_head, sizeof(box_head_t), 1, pFile);
-	iboxsize = big_to_small_endian_32bit(box_head.ibox_size);
-	if (TREF_BOX != big_to_small_endian_32bit(box_head.box_type)) {
+	uboxsize = big_to_small_endian_32bit(box_head.ibox_size);
+	if (FREE_BOX != big_to_small_endian_32bit(box_head.box_type)) {
 		fclose(pFile);
+		safe_log(pcallback, "get free box failed.");
 		return 0;
 	}
-	uret += iboxsize;
-
-	fseek(pFile, iboxsize - sizeof(box_head_t), SEEK_CUR);
+	safe_log(pcallback, "get free box success, box size is %d.", uboxsize);
+	uret += uboxsize;
+	fseek(pFile, uboxsize - sizeof(box_head_t), SEEK_CUR);
 
 	//mdat box 
 	fread(&box_head, sizeof(box_head_t), 1, pFile);
-	iboxsize = big_to_small_endian_32bit(box_head.ibox_size);
+	uboxsize = big_to_small_endian_32bit(box_head.ibox_size);
 	if (MDAT_BOX != big_to_small_endian_32bit(box_head.box_type)) {
 		fclose(pFile);
+		safe_log(pcallback, "get mdat box failed.");
+		return 0;
+	}
+	safe_log(pcallback, "get mdat box success, box size is %d.", uboxsize);
+	uret += uboxsize;
+	fseek(pFile, uboxsize - sizeof(box_head_t), SEEK_CUR);
+	safe_log(pcallback, "uret is %u.", uret);
+
+	//moov box
+	fread(&box_head, sizeof(box_head_t), 1, pFile);
+	uboxsize = big_to_small_endian_32bit(box_head.ibox_size);
+	if (MOOV_BOX != big_to_small_endian_32bit(box_head.box_type)) {
+		fclose(pFile);
+		safe_log(pcallback, "get moov box failed.");
 		return 0;
 	}
-	uret += iboxsize;
+	safe_log(pcallback, "get moov box success, box size is %d.", uboxsize);
 
 	fclose(pFile);
 

+ 2 - 1
Other/libfilecryption/mp4info.h

@@ -2,6 +2,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include "filecryption.h"
 
 #define BOX_FTYP 0x66747970
 #define MDAT_BOX 0x6D646174
@@ -46,4 +47,4 @@ typedef struct box_head_s{
 }box_head_t;
 
 
-unsigned int get_noencrypt_boxs_size(const char* pfilename);
+unsigned int get_noencrypt_boxs_size(const char* pfilename, const filecryption_callback_t* pcallback);

+ 1 - 1
Other/libfilecryption/rvcfileheader.cpp

@@ -36,7 +36,7 @@ int constrcut_rvc_file_header(char* pbuffer, size_t* ulen, const char* pfilename
 	unsigned long usize = get_srcfile_size(pfilename);
 	memcpy(pbuffer+udatalen, &usize, sizeof(unsigned long));
 	udatalen += sizeof(unsigned long);
-	safe_log(pcallback,"src file size is %d.",usize)
+	safe_log(pcallback, "src file size is %d.", usize);
 	//6. 源文件文件名
 	char strname[MAX_PATH] = {0};
 	get_srcfile_name(strname, MAX_PATH, pfilename);