encode_crc.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* **************************************************************************
  2. This program creates a CRC checksum and encodes the file that is named
  3. in the command line.
  4. Compile with: gcc encode_crc.c -Wall -o encode_crc
  5. Author: Michael Margraf (michael.margraf@freecom.com)
  6. Copyright: Freecom Technology GmbH, Berlin, 2004
  7. www.freecom.com
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. ************************************************************************* */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <sys/stat.h>
  24. // *******************************************************************
  25. // CCITT polynom G(x)=x^16+x^12+x^5+1
  26. #define POLYNOM 0x1021
  27. // CRC algorithm with MSB first
  28. int make_crc16(int crc, char new)
  29. {
  30. int i;
  31. crc = crc ^ (((int)new) << 8);
  32. for(i=0; i<8; i++) { // work on 8 bits in "new"
  33. crc <<= 1; // MSBs first
  34. if(crc & 0x10000) crc ^= POLYNOM;
  35. }
  36. return crc & 0xFFFF;
  37. }
  38. // *******************************************************************
  39. // Reads the file "filename" into memory and returns pointer to the buffer.
  40. static char *readfile(char *filename, int *size)
  41. {
  42. FILE *fp;
  43. char *buffer;
  44. struct stat info;
  45. if (stat(filename,&info)!=0)
  46. return NULL;
  47. if ((fp=fopen(filename,"r"))==NULL)
  48. return NULL;
  49. buffer=NULL;
  50. for (;;)
  51. {
  52. if ((buffer=(char *)malloc(info.st_size+1))==NULL)
  53. break;
  54. if (fread(buffer,1,info.st_size,fp)!=info.st_size)
  55. {
  56. free(buffer);
  57. buffer=NULL;
  58. break;
  59. }
  60. buffer[info.st_size]='\0';
  61. if(size) *size = info.st_size;
  62. break;
  63. }
  64. (void)fclose(fp);
  65. return buffer;
  66. }
  67. // *******************************************************************
  68. int main(int argc, char** argv)
  69. {
  70. if(argc < 3) {
  71. printf("ERROR: Argument missing!\n\n");
  72. return 1;
  73. }
  74. int count; // size of file in bytes
  75. char *p, *master = readfile(argv[1], &count);
  76. if(!master) {
  77. printf("ERROR: File not found!\n");
  78. return 1;
  79. }
  80. int crc = 0xFFFF, z;
  81. p = master;
  82. for(z=0; z<count; z++)
  83. crc = make_crc16(crc, *(p++)); // calculate CRC
  84. short crc16 = (short)crc;
  85. /*
  86. if(argc > 2) { // with flag for device recognition ?
  87. p = argv[2];
  88. for(z=strlen(p); z>0; z--) {
  89. crc ^= (int)(*p);
  90. *(p++) = (char)crc; // encode device flag
  91. }
  92. }
  93. */
  94. p = master;
  95. for(z=0; z<count; z++) {
  96. crc ^= (int)(*p);
  97. *(p++) = (char)crc; // encode file
  98. }
  99. // write encoded file...
  100. FILE *fp = fopen(argv[2], "w");
  101. if(!fp) {
  102. printf("ERROR: File not writeable!\n");
  103. return 1;
  104. }
  105. if(argc > 3) { // add flag for device recognition ?
  106. fwrite(argv[3], strlen(argv[3]), sizeof(char), fp);
  107. }
  108. else {
  109. // Device is an FSG, so byte swap (IXP4xx is big endian)
  110. crc16 = ((crc16 >> 8) & 0xFF) | ((crc16 << 8) & 0xFF00);
  111. }
  112. fwrite(&crc16, 1, sizeof(short), fp); // first write CRC
  113. fwrite(master, count, sizeof(char), fp); // write content
  114. fclose(fp);
  115. free(master);
  116. return 0;
  117. }