You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.2KB

  1. /*
  2. * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "common.h"
  21. #include "crc.h"
  22. #if LIBAVUTIL_VERSION_INT < (50<<16)
  23. AVCRC *av_crcEDB88320;
  24. AVCRC *av_crc04C11DB7;
  25. AVCRC *av_crc8005 ;
  26. AVCRC *av_crc07 ;
  27. #else
  28. AVCRC av_crcEDB88320[257];
  29. AVCRC av_crc04C11DB7[257];
  30. AVCRC av_crc8005 [257];
  31. AVCRC av_crc07 [257];
  32. #endif
  33. /**
  34. * Inits a crc table.
  35. * @param ctx must be an array of sizeof(AVCRC)*257 or sizeof(AVCRC)*1024
  36. * @param cts_size size of ctx in bytes
  37. * @return <0 on failure
  38. */
  39. int av_crc_init(AVCRC *ctx, int le, int bits, uint32_t poly, int ctx_size){
  40. int i, j;
  41. uint32_t c;
  42. if (bits < 8 || bits > 32 || poly >= (1LL<<bits))
  43. return -1;
  44. if (ctx_size != sizeof(AVCRC)*257 && ctx_size != sizeof(AVCRC)*1024)
  45. return -1;
  46. for (i = 0; i < 256; i++) {
  47. if (le) {
  48. for (c = i, j = 0; j < 8; j++)
  49. c = (c>>1)^(poly & (-(c&1)));
  50. ctx[i] = c;
  51. } else {
  52. for (c = i << 24, j = 0; j < 8; j++)
  53. c = (c<<1) ^ ((poly<<(32-bits)) & (((int32_t)c)>>31) );
  54. ctx[i] = bswap_32(c);
  55. }
  56. }
  57. ctx[256]=1;
  58. #ifndef CONFIG_SMALL
  59. if(ctx_size >= sizeof(AVCRC)*1024)
  60. for (i = 0; i < 256; i++)
  61. for(j=0; j<3; j++)
  62. ctx[256*(j+1) + i]= (ctx[256*j + i]>>8) ^ ctx[ ctx[256*j + i]&0xFF ];
  63. #endif
  64. return 0;
  65. }
  66. uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length){
  67. const uint8_t *end= buffer+length;
  68. #ifndef CONFIG_SMALL
  69. if(!ctx[256])
  70. while(buffer<end-3){
  71. crc ^= le2me_32(*(uint32_t*)buffer); buffer+=4;
  72. crc = ctx[3*256 + ( crc &0xFF)]
  73. ^ctx[2*256 + ((crc>>8 )&0xFF)]
  74. ^ctx[1*256 + ((crc>>16)&0xFF)]
  75. ^ctx[0*256 + ((crc>>24) )];
  76. }
  77. #endif
  78. while(buffer<end)
  79. crc = ctx[((uint8_t)crc) ^ *buffer++] ^ (crc >> 8);
  80. return crc;
  81. }
  82. #ifdef TEST
  83. #undef printf
  84. main(){
  85. uint8_t buf[1999];
  86. int i;
  87. int p[4][4]={{1, 32, 0xedb88320L, 0x3D5CDD04},
  88. {0, 32, 0x04c11db7L, 0xC0F5BAE0},
  89. {0, 16, 0x8005 , 0x1FBB },
  90. {0, 8, 0x07 , 0xE3 },};
  91. AVCRC ctx[1 ? 1024:257];
  92. for(i=0; i<sizeof(buf); i++)
  93. buf[i]= i+i*i;
  94. for(i=0; i<4; i++){
  95. av_crc_init(ctx, p[i][0], p[i][1], p[i][2], sizeof(ctx));
  96. printf("crc %08X =%X\n", p[i][2], av_crc(ctx, 0, buf, sizeof(buf)));
  97. }
  98. }
  99. #endif