merge.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include "myFile.h"
  2. #include "md5.h"
  3. #include "upgrade.h"
  4. static void print_md5(md5_t* m)
  5. {
  6. int i;
  7. printf("md5:");
  8. for (i = 0; i < 32; i++) {
  9. printf("%c", m->data[i]);
  10. }
  11. }
  12. static void print_info(const char* s, upgrade_file_t* hdr, md5_t *m)
  13. {
  14. printf("\n");
  15. printf("__%s__hdr.magic: 0x%08x\n", s, hdr->magic);
  16. printf("__%s__hdr.obj.num: %d\n", s, hdr->obj.num);
  17. printf("__%s__hdr.obj.stra: %d\n", s, hdr->obj.stra);
  18. printf("__%s__hdr.obj.obj1: %s\n", s, hdr->obj.obj1);
  19. printf("__%s__hdr.obj.obj2: %s\n", s, hdr->obj.obj2);
  20. print_md5(m);
  21. }
  22. int main(int argc, char *argv[])
  23. {
  24. int r;
  25. FILE* fp;
  26. node_t nd;
  27. md5_t md5;
  28. char newPath[1000];
  29. upgrade_obj_t obj;
  30. upgrade_file_t hdr;
  31. char* path = NULL;
  32. myFile mf;
  33. //TCHAR curPath[500];
  34. //GetCurrentDirectory(500, curPath);
  35. //_tprintf(_T("curPath: %s\n"), curPath);
  36. if(argc<5) {
  37. printf("usage: pack num stra obj1 obj2 packageFile\n");
  38. return -1;
  39. }
  40. obj.num = (uint8_t)atoi(argv[1]);
  41. obj.stra = (uint8_t)atoi(argv[2]);
  42. strcpy(obj.obj1, argv[3]);
  43. strcpy(obj.obj2, argv[4]);
  44. path = argv[5];
  45. strcpy(newPath, path);
  46. char* dot = strrchr(newPath, '/');
  47. if (!dot) {
  48. dot = strrchr(newPath, '\\');
  49. }
  50. if (dot) {
  51. *(dot + 1) = 0;
  52. strcat(newPath, "pdupkg");
  53. }
  54. else {
  55. strcat(newPath, "./pdupkg");
  56. }
  57. fp = fopen(newPath, "wb");
  58. if (!fp) {
  59. printf("%s create failed\n", newPath);
  60. return -1;
  61. }
  62. r = mf.read_file(path, &nd);
  63. if (r) {
  64. return -1;
  65. }
  66. hdr.magic = UPG_FILE_MAGIC;
  67. hdr.obj = obj;
  68. md5_calc(&hdr, sizeof(hdr), nd.pbuf, nd.dlen, (char*)md5.data);
  69. fwrite(&hdr, 1, sizeof(hdr), fp);
  70. fwrite(nd.pbuf, 1, nd.dlen, fp);
  71. fwrite(&md5, 1, sizeof(md5), fp);
  72. print_info("$$$", &hdr, &md5);
  73. fclose(fp);
  74. free(nd.pbuf);
  75. return 0;
  76. }