| #include <string.h> |
| #include <stdlib.h> |
| #include <unistd.h> |
| #include <sys/stat.h> |
| #include <fcntl.h> |
| #include <stdio.h> |
| #include "md5.h" |
| #include "mtk_device_wrap.h" |
| #include "liblog/lynq_deflog.h" |
| |
| |
| int ota_file_open(char* path, char* flag) |
| { |
| #if 1 |
| if(strcmp(flag, "r") == 0 || strcmp(flag, "rb") == 0) |
| return mtk_device_wrap_open(path, O_RDONLY); |
| else if(strcmp(flag, "r+") == 0 || strcmp(flag, "rb+") == 0) |
| return mtk_device_wrap_open(path, O_RDWR | O_CREAT); |
| else if(strcmp(flag, "w+") == 0 ||strcmp(flag, "wb+") == 0 || strcmp(flag, "w") == 0 || strcmp(flag, "wb") == 0 ) |
| return mtk_device_wrap_open(path, O_RDWR | O_CREAT | O_TRUNC); |
| else if(strcmp(flag, "a+") == 0 ||strcmp(flag, "ab+") == 0 || strcmp(flag, "a") == 0 || strcmp(flag, "ab") == 0 ) |
| return mtk_device_wrap_open(path, O_RDWR | O_CREAT | O_APPEND); |
| #endif |
| } |
| |
| |
| int ota_file_seek(int handle, int offset, int flag) |
| { |
| return mtk_device_wrap_seek(handle,offset,flag); |
| } |
| |
| |
| |
| int ota_file_read(char* buffer, size_t count, int file) |
| { |
| return mtk_device_wrap_read(file,buffer,count); |
| } |
| |
| |
| |
| void ota_file_close(int handle) |
| { |
| mtk_device_wrap_close(handle); |
| } |
| |
| #define MD5_READ_BUFFER_LEN 2*1024 |
| int lynq_md5_file_verfy(char* filePath, char* file_md5) |
| { |
| int ret = -1; |
| int handle; |
| |
| LYVERBLOG("[+MD5]:calc file md5: %s\n", filePath); |
| handle = open(filePath, O_RDONLY); |
| LYVERBLOG("[+MD5]:handle:%d\n",handle); |
| if(handle >= 0){ |
| { |
| int read_len = 0; |
| unsigned char buffer[MD5_READ_BUFFER_LEN] = {0}; |
| unsigned char decrypt[16]; |
| int i; |
| MD5_CTX md5; |
| MD5Init(&md5); |
| //strcpy(buffer,"12345"); |
| while ((read_len = read(handle,buffer, MD5_READ_BUFFER_LEN)) > 0) |
| { |
| //printf("readlen:%d\n",read_len); |
| MD5Update(&md5, (unsigned char*)buffer, read_len); |
| memset(buffer,0,sizeof(buffer)); |
| } |
| MD5Final(&md5, (unsigned char*)decrypt); |
| memset(buffer, 0, MD5_READ_BUFFER_LEN); |
| for(i = 0; i < 16; i++) |
| { |
| sprintf((char*)(buffer + i * 2), "%02x",decrypt[i]); |
| } |
| LYVERBLOG("[+MD5]:md5:%s\n", buffer); |
| LYVERBLOG("[+MD5]:md5:%s\n", file_md5); |
| |
| ret = strncmp((const char*)buffer, (const char*)file_md5,32); |
| LYVERBLOG("[+MD5]:ret:%d\n", ret); |
| } |
| close(handle); |
| } |
| return ret; |
| } |
| |
| #if 0 |
| int md5_file_verfy_new(char* filePath, char* file_md5,int packe_len) |
| { |
| int ret = -1; |
| int handle; |
| int tatal_packe_len = 0; |
| |
| LYVERBLOG("[+MD5]:calc file md5: %s\n", filePath); |
| handle = mtk_device_wrap_open(filePath, O_RDONLY); |
| LYVERBLOG("[+MD5]:handle:%d\n",handle); |
| if(handle >= 0){ |
| { |
| int read_len = 0; |
| unsigned char buffer[MD5_READ_BUFFER_LEN] = {0}; |
| unsigned char decrypt[16]; |
| int i; |
| MD5_CTX md5; |
| MD5Init(&md5); |
| //strcpy(buffer,"12345"); |
| tatal_packe_len = packe_len; |
| LYVERBLOG("[+MD5]:tatal_packe_len:%d\n", tatal_packe_len); |
| while ((read_len = mtk_device_wrap_read(handle,buffer, MD5_READ_BUFFER_LEN)) > 0) |
| { |
| //printf("readlen:%d\n",read_len); |
| if(tatal_packe_len >= read_len) |
| { |
| MD5Update(&md5, (unsigned char*)buffer, read_len); |
| tatal_packe_len -= read_len; |
| } |
| else if(tatal_packe_len > 0) |
| { |
| LYVERBLOG("[+MD5]:tatal_packe_len:%d\n", tatal_packe_len); |
| MD5Update(&md5, (unsigned char*)buffer, tatal_packe_len); |
| tatal_packe_len -= read_len; |
| } |
| LYVERBLOG("[+MD5]:tatal_packe_len:%d\n", tatal_packe_len); |
| memset(buffer,0,sizeof(buffer)); |
| } |
| MD5Final(&md5, (unsigned char*)decrypt); |
| memset(buffer, 0, MD5_READ_BUFFER_LEN); |
| for(i = 0; i < 16; i++) |
| { |
| sprintf((char*)(buffer + i * 2), "%02x",decrypt[i]); |
| } |
| LYVERBLOG("[+MD5]:md5:%s\n", buffer); |
| LYVERBLOG("[+MD5]:md5:%s\n", file_md5); |
| |
| ret = strncmp((const char*)buffer, (const char*)file_md5,32); |
| LYVERBLOG("[+MD5]:ret:%d\n", ret); |
| } |
| mtk_device_wrap_close(handle); |
| } |
| return ret; |
| } |
| #endif |