file_for_patch.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //file_for_patch.h
  2. // patch demo file tool
  3. //
  4. /*
  5. This is the HDiffPatch copyright.
  6. Copyright (c) 2012-2019 HouSisong All Rights Reserved.
  7. Permission is hereby granted, free of charge, to any person
  8. obtaining a copy of this software and associated documentation
  9. files (the "Software"), to deal in the Software without
  10. restriction, including without limitation the rights to use,
  11. copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. copies of the Software, and to permit persons to whom the
  13. Software is furnished to do so, subject to the following
  14. conditions:
  15. The above copyright notice and this permission notice shall be
  16. included in all copies of the Software.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  19. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  21. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  23. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  24. OTHER DEALINGS IN THE SOFTWARE.
  25. */
  26. #ifndef HPatch_file_for_patch_h
  27. #define HPatch_file_for_patch_h
  28. #include "patch_types.h"
  29. #include "ff_stdio.h"
  30. //#define HPATCH_FILE_USE_FILESYSTEM
  31. #ifndef HPATCH_FILE_USE_FILESYSTEM
  32. //#define HPATCH_FILE_CHECK_DEBUG
  33. #endif
  34. # define _set_errno_new(v)
  35. # define set_ferr(_saved_errno,_throw_errno) /*save new errno*/\
  36. do { if (_throw_errno) _saved_errno=_throw_errno; } while(0)
  37. # define _update_ferr(fe) do { fe=hpatch_TRUE; } while(0)
  38. # define _update_ferrv(fe,v) _update_ferr(fe)
  39. # define mix_ferr(_saved_errno,_throw_errno) set_ferr(_saved_errno,_throw_errno)
  40. #ifdef __cplusplus
  41. extern "C" {
  42. #endif
  43. static const char kPatch_dirSeparator = '/';
  44. typedef unsigned char TByte;
  45. #define hpatch_kFileIOBestMaxSize (1<<20)
  46. #define hpatch_kPathMaxSize (1024*2)
  47. hpatch_inline static
  48. hpatch_BOOL hpatch_getIsDirName(const char* path_utf8){
  49. size_t len=strlen(path_utf8);
  50. return (len>0)&&(path_utf8[len-1]==kPatch_dirSeparator);
  51. }
  52. hpatch_inline static const char* findUntilEnd(const char* str,char c){
  53. const char* result=strchr(str,c);
  54. return (result!=0)?result:(str+strlen(str));
  55. }
  56. #define _path_noEndDirSeparator(dst_path,src_path) \
  57. char dst_path[hpatch_kPathMaxSize]; \
  58. { size_t len=strlen(src_path); \
  59. if (len>=hpatch_kPathMaxSize) { _set_errno_new(ENAMETOOLONG); return hpatch_FALSE;} /* error */ \
  60. if ((len>0)&&(src_path[len-1]==kPatch_dirSeparator)) --len; /* without '/' */\
  61. memcpy(dst_path,src_path,len); \
  62. dst_path[len]='\0'; } /* safe */
  63. hpatch_inline static
  64. hpatch_BOOL hpatch_getIsSamePath(const char* xPath_utf8,const char* yPath_utf8){
  65. _path_noEndDirSeparator(xPath,xPath_utf8);
  66. { _path_noEndDirSeparator(yPath,yPath_utf8);
  67. if (0==strcmp(xPath,yPath)){
  68. return hpatch_TRUE;
  69. }else{
  70. // WARING!!! better return getCanonicalPath(xPath)==getCanonicalPath(yPath);
  71. return hpatch_FALSE;
  72. }
  73. }
  74. }
  75. hpatch_inline static
  76. int hpatch_printPath_utf8(const char* pathTxt_utf8){
  77. return printf("%s",pathTxt_utf8);
  78. }
  79. hpatch_inline static
  80. int hpatch_printStdErrPath_utf8(const char* pathTxt_utf8){
  81. return LOG_ERR("%s",pathTxt_utf8);
  82. }
  83. #ifdef HPATCH_FILE_USE_FILESYSTEM
  84. typedef FF_FILE* hpatch_FileHandle;
  85. #else
  86. #include "ota_update.h"
  87. #include "sfud.h"
  88. extern const char *hpatch_NofsFileName[];
  89. typedef struct {
  90. int filetype;
  91. int newfile;
  92. uint32_t offset;
  93. uint32_t size;
  94. uint32_t pos;
  95. uint32_t checksum;
  96. sfud_flash *sflash;
  97. #ifdef HPATCH_FILE_CHECK_DEBUG
  98. uint8_t *filebuf;
  99. #endif
  100. } DEV_FILE;
  101. typedef DEV_FILE *hpatch_FileHandle;
  102. void hpatch_setPatchFileSize(uint32_t size);
  103. #endif
  104. typedef struct hpatch_TFileStreamInput{
  105. hpatch_TStreamInput base;
  106. hpatch_FileHandle m_file;
  107. hpatch_StreamPos_t m_fpos;
  108. hpatch_StreamPos_t m_offset;
  109. hpatch_FileError_t fileError;
  110. } hpatch_TFileStreamInput;
  111. hpatch_inline
  112. static void hpatch_TFileStreamInput_init(hpatch_TFileStreamInput* self){
  113. memset(self,0,sizeof(hpatch_TFileStreamInput));
  114. }
  115. hpatch_BOOL hpatch_TFileStreamInput_open(hpatch_TFileStreamInput* self,const char* fileName_utf8);
  116. hpatch_BOOL hpatch_TFileStreamInput_setOffset(hpatch_TFileStreamInput* self,hpatch_StreamPos_t offset);
  117. hpatch_BOOL hpatch_TFileStreamInput_close(hpatch_TFileStreamInput* self);
  118. typedef struct hpatch_TFileStreamOutput{ //is hpatch_TFileStreamInput !
  119. hpatch_TStreamOutput base; //is hpatch_TStreamInput + write
  120. hpatch_FileHandle m_file;
  121. hpatch_StreamPos_t m_fpos;
  122. hpatch_StreamPos_t m_offset; //now not used
  123. hpatch_FileError_t fileError; // 0: no error; other: saved errno value;
  124. //
  125. hpatch_BOOL is_random_out;
  126. hpatch_BOOL is_in_readModel;
  127. hpatch_StreamPos_t out_length;
  128. } hpatch_TFileStreamOutput;
  129. hpatch_inline
  130. static void hpatch_TFileStreamOutput_init(hpatch_TFileStreamOutput* self){
  131. memset(self,0,sizeof(hpatch_TFileStreamOutput));
  132. }
  133. hpatch_BOOL hpatch_TFileStreamOutput_open(hpatch_TFileStreamOutput* self,const char* fileName_utf8,
  134. hpatch_StreamPos_t max_file_length);
  135. hpatch_inline static
  136. void hpatch_TFileStreamOutput_setRandomOut(hpatch_TFileStreamOutput* self,hpatch_BOOL is_random_out){
  137. self->is_random_out=is_random_out;
  138. }
  139. hpatch_BOOL hpatch_TFileStreamOutput_flush(hpatch_TFileStreamOutput* self);
  140. hpatch_BOOL hpatch_TFileStreamOutput_close(hpatch_TFileStreamOutput* self);
  141. hpatch_BOOL hpatch_TFileStreamOutput_reopen(hpatch_TFileStreamOutput* self,const char* fileName_utf8,
  142. hpatch_StreamPos_t max_file_length);
  143. //hpatch_BOOL hpatch_TFileStreamOutput_truncate(hpatch_TFileStreamOutput* self,hpatch_StreamPos_t new_file_length);
  144. #ifdef __cplusplus
  145. }
  146. #endif
  147. #endif