fileanalysis.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. #include "stdafx.h"
  2. #include "stdlib.h"
  3. #include "fileanalysis.h"
  4. #include "CMBSMDLL.h"
  5. #include <string.h>
  6. #include "rvcfileheader.h"
  7. #include "cJSON.h"
  8. #ifdef _WIN32
  9. #ifndef FILE_SLASH
  10. #define FILE_SLASH '\\'
  11. #endif
  12. #else
  13. #ifndef FILE_SLASH
  14. #define FILE_SLASH '/'
  15. #endif
  16. #endif // _WIN32
  17. static unsigned char seedkey[] = "Q&@09#5*AZWSsdff";
  18. int get_srcfile_name(char* pname, uint32_t ulen, const char* pfilename)
  19. {
  20. int iret = -1;
  21. if (NULL == pfilename){
  22. return iret;
  23. }
  24. const char* pindex = strrchr(pfilename, FILE_SLASH);
  25. const char* pend = strrchr(pfilename, '.');
  26. if (pindex&&pend){
  27. size_t udatalen = pend - pindex - 1;
  28. if (ulen >= udatalen){
  29. memcpy(pname, pindex+1, udatalen);
  30. iret = 0;
  31. }
  32. }
  33. return iret;
  34. }
  35. int get_srcfile_format(char* pformat, uint32_t ulen, const char* pfilename)
  36. {
  37. int iret = -1;
  38. if (NULL == pfilename){
  39. return iret;
  40. }
  41. const char* pindex = strstr(pfilename, ".");
  42. if (pindex){
  43. size_t udatalen = strlen(pindex);
  44. if (ulen >= udatalen){
  45. memcpy(pformat, pindex+1, udatalen);
  46. iret = 0;
  47. }
  48. }
  49. return iret;
  50. }
  51. int get_srcfile_hash(char* phash, uint32_t ulen, const char* pfilename)
  52. {
  53. int iret = -1;
  54. unsigned char hash[RVC_FILE_HASH_LEN] = { 0 };
  55. if(0 == CMBSM3FileDigest((char*)pfilename, hash)){
  56. if (RVC_FILE_HASH_LEN <= ulen){
  57. memcpy((void*)phash, (void*)hash, RVC_FILE_HASH_LEN);
  58. iret = 0;
  59. }
  60. }
  61. return iret;
  62. }
  63. uint32_t get_srcfile_size(const char* pfilename)
  64. {
  65. uint32_t usize = 0;
  66. if (NULL == pfilename){
  67. return usize;
  68. }
  69. FILE* pFile = fopen(pfilename, "rb");
  70. if (pFile){
  71. fseek(pFile, 0, SEEK_END);
  72. usize = (uint32_t)ftell(pFile);
  73. fclose(pFile);
  74. }
  75. return usize;
  76. }
  77. static int bin2str(unsigned char* x, int xlen, char* str, int str_size)
  78. {
  79. static const char* hex2char = "0123456789ABCDEF";
  80. int i, k = 0;
  81. if (str_size <= xlen * 2)
  82. return -1;
  83. for (i = 0; i < xlen; ++i) {
  84. int h = x[i] >> 4;
  85. int l = x[i] & 0xf;
  86. str[k++] = hex2char[h];
  87. str[k++] = hex2char[l];
  88. }
  89. str[k] = 0;
  90. return k;
  91. }
  92. int get_encrytion_filename(char* strbuffer, uint32_t ulen, bool bencname, const char* strprefix, uint32_t uprefixlen, const char* pfilename)
  93. {
  94. int iret = -1;
  95. const char* pIndex = strrchr(pfilename, FILE_SLASH);
  96. if (pIndex){
  97. pIndex++;
  98. int ifolderlen = pIndex - pfilename;
  99. char strfolder[MAX_PATH] = { 0 };
  100. if (MAX_PATH > ifolderlen) {
  101. memcpy(strfolder, pfilename, ifolderlen);
  102. }
  103. if (false == bencname){
  104. snprintf(strbuffer, ulen, "%s%s%s", strfolder, strprefix, pIndex);
  105. }
  106. else{
  107. unsigned char strSM[RVC_FILE_HASH_LEN] = {0};
  108. CMBSM3Digest((unsigned char*)pIndex, strlen(pIndex), strSM);
  109. char strHash[MAX_PATH] = {0};
  110. bin2str(strSM, RVC_FILE_HASH_LEN, strHash, MAX_PATH);
  111. snprintf(strbuffer, ulen, "%s%s", strfolder, strHash);
  112. }
  113. iret = 0;
  114. }
  115. return iret;
  116. }
  117. int get_decrytion_filename(char* strbuffer, uint32_t ulen, const char* strprefix, uint32_t uprefixlen, const char* pfilename)
  118. {
  119. int iret = -1;
  120. const char* pIndex = strrchr(pfilename, FILE_SLASH);
  121. if (pIndex){
  122. pIndex++;
  123. int ifolderlen = pIndex - pfilename;
  124. char strfolder[MAX_PATH] = { 0 };
  125. if (MAX_PATH > ifolderlen) {
  126. memcpy(strfolder, pfilename, ifolderlen);
  127. }
  128. snprintf(strbuffer, ulen, "%s%s%s", strfolder, strprefix, pIndex);
  129. iret = 0;
  130. }
  131. return iret;
  132. }
  133. int SM4EncECBMode(unsigned char key[16], unsigned char *input, int length, unsigned char *output, int *output_len)
  134. {
  135. return CMBSM4EncryptWithECB(key, input, length, output, output_len);
  136. }
  137. int SM4DecECBMode(unsigned char key[16], unsigned char *input, int length, unsigned char *output, int *output_len)
  138. {
  139. return CMBSM4DecryptWithECB(key, input, length, output, output_len);
  140. }
  141. int GenerateSM4Key(unsigned char* pkey, uint32_t ukeysize, unsigned char* phash, uint32_t uhashlen, uint32_t ufilesize)
  142. {
  143. int iret = -1;
  144. if (SM4ENC_BLOCK_SIZE > ukeysize || NULL == pkey){
  145. return iret;
  146. }
  147. if (NULL == phash || RVC_FILE_HASH_LEN != uhashlen || 0 == ufilesize){
  148. memcpy(pkey, seedkey, SM4ENC_BLOCK_SIZE);
  149. iret = 0;
  150. return iret;
  151. }
  152. char strHash[MAX_PATH] = {0};
  153. bin2str(phash, RVC_FILE_HASH_LEN, strHash, MAX_PATH);
  154. unsigned char outputdata[MAX_PATH] = {0};
  155. int ioutputlen = MAX_PATH;
  156. CMBSM4EncryptWithECB(seedkey, (unsigned char*)strHash, strlen(strHash), outputdata, &ioutputlen);
  157. int icount = ioutputlen/SM4ENC_BLOCK_SIZE;
  158. if (icount > 0){
  159. unsigned char *pindex = pkey;
  160. int i = ufilesize % icount;
  161. for (; i < ioutputlen; i += icount){
  162. *pindex++ = outputdata[i];
  163. }
  164. }
  165. else{
  166. memcpy(pkey, seedkey, SM4ENC_BLOCK_SIZE);
  167. }
  168. iret = 0;
  169. return iret;
  170. }
  171. int get_file_json_infos_from_rvc_header(char** pstrjson, uint32_t* ujsonlen, char* pheader, uint32_t udatalen)
  172. {
  173. int iret = -1;
  174. rvc_fileheader_t t_param = {0};
  175. if (0 != get_rvc_file_header_info(&t_param, pheader, udatalen, NULL)){
  176. return iret;
  177. }
  178. char strHash[MAX_PATH] = {0};
  179. bin2str(t_param.strhash, RVC_FILE_HASH_LEN, strHash, MAX_PATH);
  180. cJSON* root = cJSON_CreateObject();
  181. cJSON_AddItemToObject(root, "Hash",cJSON_CreateString(strHash));
  182. cJSON_AddItemToObject(root, "FileName",cJSON_CreateString(t_param.strsrcfilename));
  183. cJSON_AddItemToObject(root, "FileFormat",cJSON_CreateString(t_param.strsrcfileformat));
  184. cJSON_AddItemToObject(root, "FileSize",cJSON_CreateNumber(t_param.usrcfilelen));
  185. char *pret = cJSON_PrintUnformatted(root);
  186. if (pret){
  187. size_t ulen = strlen(pret);
  188. *pstrjson = (char*)malloc(ulen+1);
  189. memset(*pstrjson, 0, ulen+1);
  190. memcpy(*pstrjson, pret, ulen);
  191. *ujsonlen = ulen;
  192. iret = 0;
  193. cJSON_free(pret);
  194. }
  195. cJSON_Delete(root);
  196. return iret;
  197. }
  198. int get_file_json_infos_from_file(char** pstrjson, uint32_t* ujsonlen, const char* pfilename, FILE* pFile)
  199. {
  200. int iret = -1;
  201. if (NULL == pFile || NULL == pfilename){
  202. return iret;
  203. }
  204. char strname[MAX_PATH] = {0};
  205. get_srcfile_name(strname, MAX_PATH, pfilename);
  206. char strformat[MAX_PATH] = {0};
  207. get_srcfile_format(strformat, MAX_PATH, pfilename);
  208. unsigned char strhash[RVC_FILE_HASH_LEN] = {0};
  209. get_srcfile_hash((char*)strhash, RVC_FILE_HASH_LEN, pfilename);
  210. char strValue[MAX_PATH] = {0};
  211. bin2str(strhash, RVC_FILE_HASH_LEN, strValue, MAX_PATH);
  212. size_t usize = 0;
  213. if (pFile){
  214. fseek(pFile, 0, SEEK_END);
  215. usize = ftell(pFile);
  216. }
  217. cJSON* root = cJSON_CreateObject();
  218. cJSON_AddItemToObject(root, "Hash",cJSON_CreateString(strValue));
  219. cJSON_AddItemToObject(root, "FileName",cJSON_CreateString(strname));
  220. cJSON_AddItemToObject(root, "FileFormat",cJSON_CreateString(strformat));
  221. cJSON_AddItemToObject(root, "FileSize",cJSON_CreateNumber(usize));
  222. char *pret = cJSON_PrintUnformatted(root);
  223. if (pret){
  224. size_t ulen = strlen(pret);
  225. *pstrjson = (char*)malloc(ulen+1);
  226. memset(*pstrjson, 0, ulen+1);
  227. memcpy(*pstrjson, pret, ulen);
  228. *ujsonlen = ulen;
  229. iret = 0;
  230. cJSON_free(pret);
  231. }
  232. cJSON_Delete(root);
  233. return iret;
  234. }
  235. int get_file_hashstring(char* pbuffer, uint32_t ubuflen, const char* pfilename)
  236. {
  237. int iret = -1;
  238. if (!pbuffer || !pfilename) {
  239. return iret;
  240. }
  241. unsigned char strhash[RVC_FILE_HASH_LEN] = { 0 };
  242. if (0 != get_srcfile_hash((char*)strhash, RVC_FILE_HASH_LEN, pfilename)) {
  243. return iret;
  244. }
  245. if (0 == bin2str(strhash, RVC_FILE_HASH_LEN, pbuffer, ubuflen)) {
  246. iret = 0;
  247. }
  248. return iret;
  249. }