bin_comparator_impl.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 "config.h"
  27. #include "main_helper.h"
  28. typedef struct {
  29. FILE* fp;
  30. } binCompContext;
  31. BOOL BinComparator_Create(
  32. ComparatorImpl* impl,
  33. char* path
  34. )
  35. {
  36. binCompContext* ctx;
  37. FILE* fp;
  38. if ((fp=osal_fopen(path, "rb")) == NULL) {
  39. VLOG(ERR, "%s:%d failed to open bin file: %s\n", __FUNCTION__, __LINE__, path);
  40. return FALSE;
  41. }
  42. if ((ctx=(binCompContext*)osal_malloc(sizeof(binCompContext))) == NULL) {
  43. osal_fclose(fp);
  44. return FALSE;
  45. }
  46. ctx->fp = fp;
  47. impl->context = ctx;
  48. impl->eof = FALSE;
  49. return TRUE;
  50. }
  51. BOOL BinComparator_Destroy(
  52. ComparatorImpl* impl
  53. )
  54. {
  55. binCompContext* ctx = (binCompContext*)impl->context;
  56. osal_fclose(ctx->fp);
  57. osal_free(ctx);
  58. return TRUE;
  59. }
  60. BOOL BinComparator_Compare(
  61. ComparatorImpl* impl,
  62. void* data,
  63. PhysicalAddress size
  64. )
  65. {
  66. Uint8* pBin = NULL;
  67. binCompContext* ctx = (binCompContext*)impl->context;
  68. BOOL match = FALSE;
  69. pBin = (Uint8*)osal_malloc(size);
  70. osal_fread(pBin, size, 1, ctx->fp);
  71. if (IsEndOfFile(ctx->fp) == TRUE)
  72. impl->eof = TRUE;
  73. else
  74. impl->numOfFrames++;
  75. match = (osal_memcmp(data, (void*)pBin, size) == 0 ? TRUE : FALSE);
  76. if (match == FALSE) {
  77. FILE* fpGolden;
  78. FILE* fpOutput;
  79. char tmp[200];
  80. sprintf(tmp, "./golden_%s_%05d.bin", GetBasename(impl->filename), impl->curIndex-1);
  81. if ((fpGolden=osal_fopen(tmp, "wb")) == NULL) {
  82. VLOG(ERR, "Faild to create %s\n", tmp);
  83. osal_free(pBin);
  84. return FALSE;
  85. }
  86. VLOG(ERR, "Saving... Golden Bin at %s\n", tmp);
  87. osal_fwrite(pBin, size, 1, fpGolden);
  88. osal_fclose(fpGolden);
  89. sprintf(tmp, "./encoded_%s_%05d.bin", GetBasename(impl->filename), impl->curIndex-1);
  90. if ((fpOutput=osal_fopen(tmp, "wb")) == NULL) {
  91. VLOG(ERR, "Faild to create %s\n", tmp);
  92. osal_free(pBin);
  93. return FALSE;
  94. }
  95. VLOG(ERR, "Saving... encoded Bin at %s\n", tmp);
  96. osal_fwrite(data, size, 1, fpOutput);
  97. osal_fclose(fpOutput);
  98. }
  99. osal_free(pBin);
  100. return match;
  101. }
  102. BOOL BinComparator_Configure(
  103. ComparatorImpl* impl,
  104. ComparatorConfType type,
  105. void* val
  106. )
  107. {
  108. UNREFERENCED_PARAMETER(impl);
  109. UNREFERENCED_PARAMETER(type);
  110. UNREFERENCED_PARAMETER(val);
  111. return FALSE;
  112. }
  113. ComparatorImpl binComparatorImpl = {
  114. NULL,
  115. NULL,
  116. 0,
  117. 0,
  118. BinComparator_Create,
  119. BinComparator_Destroy,
  120. BinComparator_Compare,
  121. BinComparator_Configure,
  122. FALSE,
  123. };