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.

103 lines
3.0KB

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