Browse Source

Z991239-5471 #comment 优化文件加密库,增加日志级别

80274480 1 year ago
parent
commit
5ba353d44e

+ 8 - 3
Module/mod_SalesRecorder/mod_SalesRecorder.cpp

@@ -310,8 +310,13 @@ static unsigned long GetFileSize(const char* pfilename)
 }
 
 
-static void rvcDbg(const char* fmt, ...)
+static void rvcDbg(filecrypt_loglevel elevel, const char* fmt, ...)
 {
+	LOG_LEVEL_E eloglevel = LOG_LEVEL_DEBUG;
+	if (FILECRYPT_LOG_INFO <= elevel) {
+		eloglevel = LOG_LEVEL_INFO;
+	}
+
 	va_list arg;
 	va_start(arg, fmt);
 
@@ -319,13 +324,13 @@ static void rvcDbg(const char* fmt, ...)
 	if (n >= MAX_LOG_LEN) {
 		char* buf = (char*)malloc((size_t)(n + 1));
 		vsnprintf(buf, n + 1, fmt, arg);
-		DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("%s", buf);
+		DbgWithLink(eloglevel, LOG_TYPE_SYSTEM)("%s", buf);
 		free(buf);
 	}
 	else {
 		char strlog[MAX_LOG_LEN] = { 0 };
 		vsnprintf(strlog, MAX_LOG_LEN, fmt, arg);
-		DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("%s", strlog);
+		DbgWithLink(eloglevel, LOG_TYPE_SYSTEM)("%s", strlog);
 	}
 	va_end(arg);
 }

+ 8 - 3
Module/mod_recorder/mod_recorder.cpp

@@ -78,8 +78,13 @@ static void LogVideoSizeInfo(const char* pszMessage)
 	LogWarn(Severity_Low, Error_Debug, LOG_EVT_RECORDER_VIDEO_SIZE, CSimpleStringA::Format("%s file size is %u byte.", pszMessage, ufilesize).GetData());
 }
 
-static void rvcDbg(const char* fmt, ...)
+static void rvcDbg(filecrypt_loglevel elevel, const char* fmt, ...)
 {
+	LOG_LEVEL_E eloglevel = LOG_LEVEL_DEBUG;
+	if (FILECRYPT_LOG_INFO <= elevel) {
+		eloglevel = LOG_LEVEL_INFO;
+	}
+
 	va_list arg;
 	va_start(arg, fmt);
 
@@ -87,13 +92,13 @@ static void rvcDbg(const char* fmt, ...)
 	if (n >= MAX_LOG_LEN) {
 		char* buf = (char*)malloc((size_t)(n + 1));
 		vsnprintf(buf, n + 1, fmt, arg);
-		DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("%s", buf);
+		DbgWithLink(eloglevel, LOG_TYPE_SYSTEM)("%s", buf);
 		free(buf);
 	}
 	else{
 		char strlog[MAX_LOG_LEN] = {0};
 		vsnprintf(strlog, MAX_LOG_LEN, fmt, arg);
-		DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("%s", strlog);
+		DbgWithLink(eloglevel, LOG_TYPE_SYSTEM)("%s", strlog);
 	}
 	va_end(arg);
 }

+ 19 - 19
Other/libfilecryption/filecryption.cpp

@@ -48,7 +48,7 @@ static int encrypt_asf_file(char* poutfilename, char* phead, uint32_t uheaderlen
 	char strGuid[HEADER_OBJECT_GUID_LEN] = { 0 };
 	fread(strGuid, 1, HEADER_OBJECT_GUID_LEN, pSrcFile);
 	if (0 != memcmp(strGuid, ASF_Header_GUID, HEADER_OBJECT_GUID_LEN)) {
-		safe_log(pcallback, "%s", "math guid failed, current format is not surpport!");
+		safe_log(pcallback, FILECRYPT_LOG_ERROR, "%s", "math guid failed, current format is not surpport!");
 		fclose(pDestFile);
 		return iret;
 	}
@@ -102,7 +102,7 @@ static int encrypt_mp4_file(char* poutfilename, char* phead, uint32_t uheaderlen
 	if (0 == unoencryptlen) {
 		return iret;
 	}
-	safe_log(pcallback, "not encrypt file length is %u.", unoencryptlen);
+	safe_log(pcallback, FILECRYPT_LOG_DEBUG, "not encrypt file length is %u.", unoencryptlen);
 
 	FILE* pDestFile = fopen(poutfilename, "wb");
 	if (NULL == pDestFile) {
@@ -113,7 +113,7 @@ static int encrypt_mp4_file(char* poutfilename, char* phead, uint32_t uheaderlen
 	fseek(pSrcFile, 0, SEEK_END);
 	unsigned int usrcfilelen = ftell(pSrcFile);
 	rewind(pSrcFile);
-	safe_log(pcallback, "src file length is %u.", usrcfilelen);
+	safe_log(pcallback, FILECRYPT_LOG_DEBUG, "src file length is %u.", usrcfilelen);
 
 	//1. 新增自定义文件头
 	fwrite(phead, 1, uheaderlen, pDestFile);
@@ -147,7 +147,7 @@ static int encrypt_mp4_file(char* poutfilename, char* phead, uint32_t uheaderlen
 	int32_t 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);
+	safe_log(pcallback, FILECRYPT_LOG_DEBUG, "after encode, moov box length is %u.", iencdatalen);
 
 	//3. 填充加密后的moov box长度
 	fwrite(&iencdatalen, 1, sizeof(uint32_t), pDestFile);
@@ -268,7 +268,7 @@ static int decrypt_mp4_file(char* poutfilename, FILE* pSrcFile, unsigned char* p
 	if (0 == unoencryptlen) {
 		return iret;
 	}
-	safe_log(pcallback, "not encrypt file length is %u.", unoencryptlen);
+	safe_log(pcallback, FILECRYPT_LOG_DEBUG, "not encrypt file length is %u.", unoencryptlen);
 
 	//1. 填充ftyp, free, mdat box
 	char* data_buffer = (char*)malloc(RVC_READ_BUFFER_SIZE);
@@ -289,7 +289,7 @@ static int decrypt_mp4_file(char* poutfilename, FILE* pSrcFile, unsigned char* p
 	//2. 获取加密后的moov box大小
 	int32_t uencryptboxsize = 0;
 	fread(&uencryptboxsize, 1, sizeof(uint32_t), pSrcFile);
-	safe_log(pcallback, "encrypt moov box size is = %d.", uencryptboxsize);
+	safe_log(pcallback, FILECRYPT_LOG_DEBUG, "encrypt moov box size is = %d.", uencryptboxsize);
 
 	//3. 读取加密后的moov box
 	unsigned char* pmoovboxdata = (unsigned char*)malloc(uencryptboxsize);
@@ -299,7 +299,7 @@ static int decrypt_mp4_file(char* poutfilename, FILE* pSrcFile, unsigned char* p
 	
 	//4. 解密moov box
 	SM4DecECBMode(pstrkey, pmoovboxdata, uencryptboxsize, pdecmoovboxdata, &ioutlen);
-	safe_log(pcallback, "decrypt moov box size is = %d.", ioutlen);
+	safe_log(pcallback, FILECRYPT_LOG_DEBUG, "decrypt moov box size is = %d.", ioutlen);
 
 	//5. 填充解密后的moov box
 	fwrite(pdecmoovboxdata, 1, ioutlen, pDestFile);
@@ -320,20 +320,20 @@ int decryption_file(char* poutfile, uint32_t uoutlen, const char* pfilename, con
 	int iret = -1;
 
 	if (NULL == pfilename || eversion >= sizeof(cryption_ver_flag_table)/sizeof(char*) || eversion < 0){
-		safe_log(pcallback,"%s","invalid encryption version param.");
+		safe_log(pcallback, FILECRYPT_LOG_ERROR, "%s","invalid encryption version param.");
 		return iret;
 	}
 
 	FILE* pSrcFile = fopen(pfilename, "rb");
 	if (NULL == pSrcFile){
-		safe_log(pcallback,"open file %s failed!",pfilename);
+		safe_log(pcallback, FILECRYPT_LOG_ERROR, "open file %s failed!",pfilename);
 		return iret;
 	}
 
 	char strrvcflag[RVC_FILE_HEADER_FLAG_LEN] = {0};
 	fread(strrvcflag,1, RVC_FILE_HEADER_FLAG_LEN, pSrcFile);
 	if (0 != memcmp(strrvcflag, rvc_header, RVC_FILE_HEADER_FLAG_LEN)){
-		safe_log(pcallback,"file %s is not encryption!",pfilename);
+		safe_log(pcallback, FILECRYPT_LOG_INFO, "file %s is not encryption!",pfilename);
 		fclose(pSrcFile);
 		return iret;
 	}
@@ -346,7 +346,7 @@ int decryption_file(char* poutfile, uint32_t uoutlen, const char* pfilename, con
 
 	if (0 != memcmp(prvcbuffer+sizeof(uint32_t), cryption_ver_flag_table[eversion], RVC_CRYPTION_VER_FLAG_LEN)){
 		if (0 == memcmp(prvcbuffer+sizeof(uint32_t), cryption_ver_flag_table[eversion], RVC_CRYPTION_VER_FLAG_LEN-1)){
-			safe_log(pcallback,"file %s encryption and decryption version is not matched!",pfilename);
+			safe_log(pcallback, FILECRYPT_LOG_ERROR, "file %s encryption and decryption version is not matched!",pfilename);
 		} 
 		fclose(pSrcFile);
 		return iret;
@@ -383,7 +383,7 @@ bool is_file_encrypted(const char* pfilename, const filecryption_callback_t* pca
 
 	FILE* pSrcFile = fopen(pfilename, "rb");
 	if (NULL == pSrcFile){
-		safe_log(pcallback,"open file %s failed!",pfilename);
+		safe_log(pcallback, FILECRYPT_LOG_ERROR, "open file %s failed!",pfilename);
 		return bret;
 	}
 
@@ -393,7 +393,7 @@ bool is_file_encrypted(const char* pfilename, const filecryption_callback_t* pca
 			bret = true;
 		} 
 		else{
-			safe_log(pcallback,"file %s is not encryption!",pfilename);
+			safe_log(pcallback, FILECRYPT_LOG_INFO, "file %s is not encryption!",pfilename);
 		}
 	}
 	fclose(pSrcFile);
@@ -411,13 +411,13 @@ int rvc_file_decrypt(unsigned char** pdechead, uint32_t* udecheadlen, int* ioffs
 {
 	int iret = -1;
 	if (NULL == pfilename || eversion >= sizeof(cryption_ver_flag_table)/sizeof(char*) || eversion < 0){
-		safe_log(pcallback,"%s","invalid cryption version param.");
+		safe_log(pcallback, FILECRYPT_LOG_ERROR, "%s","invalid cryption version param.");
 		return iret;
 	}
 
 	FILE* pSrcFile = fopen(pfilename, "rb");
 	if (NULL == pSrcFile){
-		safe_log(pcallback,"open file %s failed!",pfilename);
+		safe_log(pcallback, FILECRYPT_LOG_ERROR, "open file %s failed!",pfilename);
 		return iret;
 	}
 
@@ -427,7 +427,7 @@ int rvc_file_decrypt(unsigned char** pdechead, uint32_t* udecheadlen, int* ioffs
 	char strrvcflag[RVC_FILE_HEADER_FLAG_LEN] = {0};
 	fread(strrvcflag,1, RVC_FILE_HEADER_FLAG_LEN, pSrcFile);
 	if (0 != memcmp(strrvcflag, rvc_header, RVC_FILE_HEADER_FLAG_LEN)){
-		//safe_log(pcallback,"file %s is not encrypted!",pfilename);
+		safe_log(pcallback, FILECRYPT_LOG_INFO, "file %s is not encrypted!",pfilename);
 		*pdechead = NULL;
 		*udecheadlen = 0;
 		*ioffset = 0;
@@ -444,7 +444,7 @@ int rvc_file_decrypt(unsigned char** pdechead, uint32_t* udecheadlen, int* ioffs
 
 		if (0 != memcmp(prvcbuffer+sizeof(uint32_t), cryption_ver_flag_table[eversion], RVC_CRYPTION_VER_FLAG_LEN)){
 			if (0 == memcmp(prvcbuffer+sizeof(uint32_t), cryption_ver_flag_table[eversion], RVC_CRYPTION_VER_FLAG_LEN-1)){
-				safe_log(pcallback,"file %s encryption and decrption version is not matched!",pfilename);
+				safe_log(pcallback, FILECRYPT_LOG_ERROR, "file %s encryption and decrption version is not matched!",pfilename);
 			} 
 			safe_free(prvcbuffer);
 			fclose(pSrcFile);
@@ -459,7 +459,7 @@ int rvc_file_decrypt(unsigned char** pdechead, uint32_t* udecheadlen, int* ioffs
 
 		int ijson = get_file_json_infos_from_rvc_header(pstrjson, ujsonlen, prvcbuffer, irvcheadlen - RVC_FILE_HEADER_FLAG_LEN - sizeof(uint32_t));
 		if (0 == ijson){
-			safe_log(pcallback,"get_file_json_infos_from_rvc_header success!");
+			safe_log(pcallback, FILECRYPT_LOG_DEBUG, "get_file_json_infos_from_rvc_header success!");
 		}
 		safe_free(prvcbuffer);
 	}
@@ -467,7 +467,7 @@ int rvc_file_decrypt(unsigned char** pdechead, uint32_t* udecheadlen, int* ioffs
 	char strasfheader[ASF_HEADER_GUID_LEN] = {0};
 	if (ASF_HEADER_GUID_LEN == fread(strasfheader, 1, ASF_HEADER_GUID_LEN, pSrcFile)){
 		if (0 != memcmp(strasfheader, ASF_Header_GUID, ASF_HEADER_GUID_LEN)){
-			safe_log(pcallback,"file %s is current not surrport format!",pfilename);
+			safe_log(pcallback, FILECRYPT_LOG_ERROR, "file %s is current not surrport format!",pfilename);
 			fclose(pSrcFile);
 			return iret;
 		}

+ 9 - 3
Other/libfilecryption/filecryption.h

@@ -34,18 +34,24 @@ enum eRvcCryptionVersion{
 	eVerD
 };
 
+enum filecrypt_loglevel {
+	FILECRYPT_LOG_NO,
+	FILECRYPT_LOG_DEBUG,
+	FILECRYPT_LOG_INFO,
+	FILECRYPT_LOG_ERROR
+};
 
 typedef struct filecryption_callback_s  {
 	void (*oncryptionexception)(void *user_data);
 	void (*oncryptionfinished)(void *user_data);
-	void (*dbg)(const char* fmt, ...);
+	void (*dbg)(filecrypt_loglevel elevel, const char* fmt, ...);
 }filecryption_callback_t;
 
 
 #ifndef safe_log
-#define safe_log(x, strformat, ...) \
+#define safe_log(x, elevel, strformat, ...) \
 		do{\
-			if (x && x->dbg){ x->dbg(strformat, ##__VA_ARGS__);}\
+			if (x && x->dbg){ x->dbg(elevel, strformat, ##__VA_ARGS__);}\
 		}while(0)
 #endif
 

+ 9 - 9
Other/libfilecryption/mp4info.cpp

@@ -29,10 +29,10 @@ unsigned int get_noencrypt_boxs_size(FILE* pSrcFile, const filecryption_callback
 	fread(&box_head, sizeof(box_head_t), 1, pSrcFile);
 	unsigned int uboxsize = big_to_small_endian_32bit(box_head.ibox_size);
 	if (BOX_FTYP != big_to_small_endian_32bit(box_head.box_type)) {
-		safe_log(pcallback, "get ftyp box failed.");
+		safe_log(pcallback, FILECRYPT_LOG_ERROR, "get ftyp box failed.");
 		return 0;
 	}
-	safe_log(pcallback, "get ftyp box success, box size is %d.", uboxsize);
+	safe_log(pcallback, FILECRYPT_LOG_DEBUG, "get ftyp box success, box size is %d.", uboxsize);
 	uret += uboxsize;
 	fseek(pSrcFile, uboxsize - sizeof(box_head_t), SEEK_CUR);
 
@@ -40,10 +40,10 @@ unsigned int get_noencrypt_boxs_size(FILE* pSrcFile, const filecryption_callback
 	fread(&box_head, sizeof(box_head_t), 1, pSrcFile);
 	uboxsize = big_to_small_endian_32bit(box_head.ibox_size);
 	if (FREE_BOX != big_to_small_endian_32bit(box_head.box_type)) {
-		safe_log(pcallback, "get free box failed.");
+		safe_log(pcallback, FILECRYPT_LOG_ERROR, "get free box failed.");
 		return 0;
 	}
-	safe_log(pcallback, "get free box success, box size is %d.", uboxsize);
+	safe_log(pcallback, FILECRYPT_LOG_DEBUG, "get free box success, box size is %d.", uboxsize);
 	uret += uboxsize;
 	fseek(pSrcFile, uboxsize - sizeof(box_head_t), SEEK_CUR);
 
@@ -51,12 +51,12 @@ unsigned int get_noencrypt_boxs_size(FILE* pSrcFile, const filecryption_callback
 	fread(&box_head, sizeof(box_head_t), 1, pSrcFile);
 	uboxsize = big_to_small_endian_32bit(box_head.ibox_size);
 	if (MDAT_BOX != big_to_small_endian_32bit(box_head.box_type)) {
-		safe_log(pcallback, "get mdat box failed.");
+		safe_log(pcallback, FILECRYPT_LOG_ERROR, "get mdat box failed.");
 		return 0;
 	}
-	safe_log(pcallback, "get mdat box success, box size is %d.", uboxsize);
+	safe_log(pcallback, FILECRYPT_LOG_DEBUG, "get mdat box success, box size is %d.", uboxsize);
 	uret += uboxsize;
-	safe_log(pcallback, "ftyp box, free box and mdat box total size is %u.", uret);
+	safe_log(pcallback, FILECRYPT_LOG_DEBUG, "ftyp box, free box and mdat box total size is %u.", uret);
 
 	//moov box
 	if (bgetmoov) {
@@ -64,10 +64,10 @@ unsigned int get_noencrypt_boxs_size(FILE* pSrcFile, const filecryption_callback
 		fread(&box_head, sizeof(box_head_t), 1, pSrcFile);
 		uboxsize = big_to_small_endian_32bit(box_head.ibox_size);
 		if (MOOV_BOX != big_to_small_endian_32bit(box_head.box_type)) {
-			safe_log(pcallback, "get moov box failed.");
+			safe_log(pcallback, FILECRYPT_LOG_ERROR, "get moov box failed.");
 			return 0;
 		}
-		safe_log(pcallback, "get moov box success, box size is %d.", uboxsize);
+		safe_log(pcallback, FILECRYPT_LOG_DEBUG, "get moov box success, box size is %d.", uboxsize);
 	}
 
 	fseek(pSrcFile, ipostion, SEEK_SET);

+ 5 - 5
Other/libfilecryption/rvcfileheader.cpp

@@ -25,7 +25,7 @@ int constrcut_rvc_file_header(char* pbuffer, uint32_t* ulen, const char* pfilena
 	//4. 源文件hash值
 	char strhash[RVC_FILE_HASH_LEN] = {0};
 	if (get_srcfile_hash(strhash, RVC_FILE_HASH_LEN, pfilename)){
-		safe_log(pcallback,"%s","get file hash failed!");
+		safe_log(pcallback, FILECRYPT_LOG_ERROR, "%s","get file hash failed!");
 		return iret;
 	}
 	if (0 == fill_tag_buffer(pbuffer + udatalen, *ulen - udatalen, strhash, RVC_FILE_HASH_LEN)){
@@ -36,7 +36,7 @@ int constrcut_rvc_file_header(char* pbuffer, uint32_t* ulen, const char* pfilena
 	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, FILECRYPT_LOG_DEBUG, "src file size is %d.", usize);
 	//6. 源文件文件名
 	char strname[MAX_PATH] = {0};
 	get_srcfile_name(strname, MAX_PATH, pfilename);
@@ -49,7 +49,7 @@ int constrcut_rvc_file_header(char* pbuffer, uint32_t* ulen, const char* pfilena
 	if (0 == fill_tag_buffer(pbuffer + udatalen, *ulen - udatalen, strformat, strlen(strformat))){
 		udatalen += (strlen(strformat) + sizeof(uint32_t));
 	}
-	safe_log(pcallback,"file name is %s, and file format is %s.",strname, strformat);
+	safe_log(pcallback, FILECRYPT_LOG_DEBUG, "file name is %s, and file format is %s.",strname, strformat);
 	*ulen = udatalen;
 	memcpy(pbuffer+RVC_FILE_HEADER_FLAG_LEN, &udatalen, sizeof(uint32_t));
 	iret = 0;
@@ -139,12 +139,12 @@ int get_key_from_header_info(unsigned char* pkey, uint32_t ukeysize, char* phead
 
 	rvc_fileheader_t t_param = {0};
 	if (0 != get_rvc_file_header_info(&t_param, pheader, udatalen, pcallback)){
-		safe_log(pcallback, "%s", "get rvc file header infos failed!");
+		safe_log(pcallback, FILECRYPT_LOG_ERROR, "%s", "get rvc file header infos failed!");
 		return iret;
 	}
 
 	if (0 == GenerateSM4Key(pkey, ukeysize, t_param.strhash, RVC_FILE_HASH_LEN, t_param.usrcfilelen)){
-		//safe_log(pcallback, "%s", "Generate SM4Key success!");
+		safe_log(pcallback, FILECRYPT_LOG_DEBUG, "%s", "Generate SM4Key success!");
 		iret = 0;
 	}