1 /* **************************************************************************
3 This program creates a modified 16bit checksum used for the Netgear
4 DGN3500 series routers. The difference between this and a standard
5 checksum is that every 0x100 bytes added 0x100 have to be subtracted
8 (C) 2013 Marco Antonio Mauro <marcus90 at gmail.com>
10 Based on previous unattributed work.
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 ************************************************************************* */
34 unsigned char PidDataWW[70] =
36 0x73, 0x45, 0x72, 0x43, 0x6F, 0x4D, 0x6D, 0x00, 0x00, 0x00, 0x00, 0x59, 0x50, 0x35, 0x37, 0x32,
37 0x33, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
38 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x41, 0x00, 0x37,
39 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x73,
40 0x45, 0x72, 0x43, 0x6F, 0x4D, 0x6D,
43 unsigned char PidDataDE[70] =
45 0x73, 0x45, 0x72, 0x43, 0x6F, 0x4D, 0x6D, 0x00, 0x00, 0x00, 0x00, 0x59, 0x50, 0x35, 0x37, 0x32,
46 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
47 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x37,
48 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x73,
49 0x45, 0x72, 0x43, 0x6F, 0x4D, 0x6D,
52 unsigned char PidDataNA[70] =
54 0x73, 0x45, 0x72, 0x43, 0x6F, 0x4D, 0x6D, 0x00, 0x00, 0x00, 0x00, 0x59, 0x50, 0x35, 0x37, 0x32,
55 0x36, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
56 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x41, 0x00, 0x37,
57 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x73,
58 0x45, 0x72, 0x43, 0x6F, 0x4D, 0x6D,
61 /* *******************************************************************
62 Reads the file into memory and returns pointer to the buffer. */
63 static char *readfile(char *filename, int *size)
69 if (stat(filename,&info)!=0)
72 if ((fp=fopen(filename,"r"))==NULL)
78 if ((buffer=(char *)malloc(info.st_size+1))==NULL)
81 if (fread(buffer,1,info.st_size,fp)!=info.st_size)
88 buffer[info.st_size]='\0';
89 if(size) *size = info.st_size;
100 /* ******************************************************************* */
101 int main(int argc, char** argv)
103 unsigned long start, i;
104 char *endptr, *buffer, *p;
105 int count; // size of file in bytes
106 unsigned short sum, sum1;
110 printf("ERROR: Argument missing!\n\nUsage %s filename starting offset in hex [PID code]\n\n", argv[0]);
115 FILE *fp = fopen(argv[1], "a");
117 printf("ERROR: File not writeable!\n");
122 printf("%s: PID type: %s\n", argv[0], argv[3]);
123 if(strcmp(argv[3], "DE")==0)
124 fwrite(PidDataDE, sizeof(PidDataDE), sizeof(char), fp); /* write DE pid */
125 else if(strcmp(argv[3], "NA")==0)
126 fwrite(PidDataNA, sizeof(PidDataNA), sizeof(char), fp); /* write NA pid */
127 else /* if(strcmp(argv[3], "WW")) */
128 fwrite(PidDataWW, sizeof(PidDataWW), sizeof(char), fp); /* write WW pid */
131 fwrite(PidDataWW, sizeof(PidDataWW), sizeof(char), fp); /* write WW pid if unspecified */
135 /* Read the file to calculate the checksums */
136 buffer = readfile(argv[1], &count);
138 printf("ERROR: File %s not found!\n", argv[1]);
143 for(i = 0; i < count; i++)
148 start = strtol(argv[2], &endptr, 16);
151 for(i = 0; i < count - start; i++)
156 sprintf(sumbuf,"%04X%04X",sum1,sum);
157 /* Append the 2 checksums to end of file */
158 fp = fopen(argv[1], "a");
160 printf("ERROR: File not writeable!\n");
163 fwrite(sumbuf, 8, sizeof(char), fp);