md5_comparator_impl.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // SPDX-License-Identifier: LGPL-2.1 OR BSD-3-Clause
  2. /*
  3. * Copyright (c) 2019, Chips&Media
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  19. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <errno.h>
  27. #include "main_helper.h"
  28. typedef struct {
  29. osal_file_t fp;
  30. Uint32 md5Size;
  31. Uint32 prevMd5[12];
  32. Uint32 loopCount;
  33. BOOL isMonochrome;
  34. } md5CompContext;
  35. BOOL MD5Comparator_Create(
  36. ComparatorImpl* impl,
  37. char* path
  38. )
  39. {
  40. md5CompContext* ctx;
  41. osal_file_t fp;
  42. Uint32 temp;
  43. if ((fp=osal_fopen(path, "r")) == NULL) {
  44. VLOG(ERR, "%s:%d failed to open md5 file: %s, errno(%d)\n", __FUNCTION__, __LINE__, path, errno);
  45. return FALSE;
  46. }
  47. if ((ctx=(md5CompContext*)osal_malloc(sizeof(md5CompContext))) == NULL) {
  48. osal_fclose(fp);
  49. return FALSE;
  50. }
  51. while (!osal_feof(fp)) {
  52. if (osal_fscanf((FILE*)fp, "%08x", &temp) < 1) break;
  53. impl->numOfFrames++;
  54. }
  55. osal_fseek(fp, 0, SEEK_SET);
  56. ctx->fp = fp;
  57. ctx->md5Size = 12;
  58. ctx->loopCount = 1;
  59. impl->context = ctx;
  60. impl->eof = FALSE;
  61. return TRUE;
  62. }
  63. BOOL MD5Comparator_Destroy(
  64. ComparatorImpl* impl
  65. )
  66. {
  67. md5CompContext* ctx = (md5CompContext*)impl->context;
  68. osal_fclose(ctx->fp);
  69. osal_free(ctx);
  70. return TRUE;
  71. }
  72. BOOL MD5Comparator_Compare(
  73. ComparatorImpl* impl,
  74. void* data,
  75. PhysicalAddress size
  76. )
  77. {
  78. md5CompContext* ctx = (md5CompContext*)impl->context;
  79. BOOL match = TRUE;
  80. BOOL lineMatch[12] = {0,};
  81. Uint32 md5[12];
  82. Uint32 idx;
  83. Uint32* decodedMD5 = NULL;
  84. Uint32 nullData[12] = {0,};
  85. decodedMD5 = (NULL == data) ? nullData : (Uint32*)data;
  86. if ( data == (void *)COMPARATOR_SKIP ) {
  87. for (idx=0; idx<ctx->md5Size; idx++) {
  88. osal_fscanf((FILE*)ctx->fp, "%08x", &md5[idx]);
  89. if (IsEndOfFile(ctx->fp) == TRUE) {
  90. impl->eof = TRUE;
  91. break;
  92. }
  93. };
  94. return TRUE;
  95. }
  96. do {
  97. osal_memset((void*)md5, 0x00, sizeof(md5));
  98. if (impl->usePrevDataOneTime == TRUE) {
  99. impl->usePrevDataOneTime = FALSE;
  100. osal_memcpy(md5, ctx->prevMd5, ctx->md5Size*sizeof(md5[0]));
  101. }
  102. else {
  103. for (idx=0; idx<ctx->md5Size; idx++) {
  104. /* FIXME: osal_osal_fscanf has problem on Windows.
  105. */
  106. osal_fscanf((FILE*)ctx->fp, "%08x", &md5[idx]);
  107. if (IsEndOfFile(ctx->fp) == TRUE) {
  108. impl->eof = TRUE;
  109. break;
  110. }
  111. }
  112. }
  113. match = TRUE;
  114. for (idx=0; idx<ctx->md5Size; idx++) {
  115. if (TRUE == ctx->isMonochrome && 3 < idx) {
  116. //for monochrome
  117. osal_memset(decodedMD5+idx, 0x00, sizeof(decodedMD5[0]));
  118. }
  119. if (md5[idx] != decodedMD5[idx]) {
  120. match = FALSE;
  121. lineMatch[idx]=FALSE;
  122. }
  123. else
  124. lineMatch[idx]=TRUE;
  125. }
  126. } while (impl->enableScanMode == TRUE && match == FALSE && impl->eof == FALSE);
  127. osal_memcpy(ctx->prevMd5, md5, ctx->md5Size*sizeof(md5[0]));
  128. if (match == FALSE ) {
  129. VLOG(ERR, "MISMATCH WITH GOLDEN MD5 at %d frame\n", impl->curIndex);
  130. VLOG(ERR, "GOLDEN DECODED RESULT\n"
  131. "------------------------------------\n");
  132. for (idx=0; idx<ctx->md5Size; idx++) {
  133. VLOG(ERR, "%08x %08x %s\n", md5[idx], decodedMD5[idx], lineMatch[idx]==TRUE ? " O " : "XXX");
  134. }
  135. }
  136. return match;
  137. }
  138. BOOL MD5Comparator_Configure(
  139. ComparatorImpl* impl,
  140. ComparatorConfType type,
  141. void* val
  142. )
  143. {
  144. md5CompContext* ctx = (md5CompContext*)impl->context;
  145. BOOL ret = TRUE;
  146. switch (type) {
  147. case COMPARATOR_CONF_SET_GOLDEN_DATA_SIZE:
  148. ctx->md5Size = *(Uint32*)val;
  149. impl->numOfFrames /= ctx->md5Size;
  150. break;
  151. case COMPARATOR_CONF_SET_MONOCHROME:
  152. ctx->isMonochrome = TRUE;
  153. break;
  154. default:
  155. ret = FALSE;
  156. break;
  157. }
  158. return ret;
  159. }
  160. BOOL MD5Comparator_Rewind(
  161. ComparatorImpl* impl
  162. )
  163. {
  164. md5CompContext* ctx = (md5CompContext*)impl->context;
  165. Int32 ret;
  166. if ((ret=osal_fseek(ctx->fp, 0, SEEK_SET)) != 0) {
  167. VLOG(ERR, "%s:%d Failed to osal_fseek(ret: %d)\n", __FUNCTION__, __LINE__, ret);
  168. return FALSE;
  169. }
  170. impl->eof = FALSE;
  171. return TRUE;
  172. }
  173. ComparatorImpl md5ComparatorImpl = {
  174. NULL,
  175. NULL,
  176. 0,
  177. 0,
  178. MD5Comparator_Create,
  179. MD5Comparator_Destroy,
  180. MD5Comparator_Compare,
  181. MD5Comparator_Configure,
  182. MD5Comparator_Rewind,
  183. };