Created
March 17, 2016 19:20
-
-
Save lackofdream/1ce97c0a1b03acb8f62c to your computer and use it in GitHub Desktop.
Append CRC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -w | |
use strict; | |
my $TempCompFileName = shift(@ARGV); | |
my $OutFileName = shift(@ARGV); | |
my ($start_addr, $appd_cks, $appd_len) = (0); | |
sub appendCks | |
{ | |
my ($fileName) = @_; | |
my ($cks, $len) = (0, 0); | |
open APPENDCKS_FH, "+<$fileName" or die "fail to open file $fileName : $!"; | |
until ( eof APPENDCKS_FH ) { | |
$cks = ( $cks + ord( getc( APPENDCKS_FH )) ) % 0x100 ; | |
$len++ ; | |
} | |
$cks = 0xFF - $cks; | |
close APPENDCKS_FH; | |
printf ("append checksum => file : %s, len : 0x%X, checksum : 0x%X \n", , $fileName, $len, $cks); | |
$appd_cks =$cks; | |
$appd_len =$len; | |
} | |
appendCks($TempCompFileName); | |
# Then append Checksum (include the 4bytes starting address) | |
open INS_FH, "$TempCompFileName" or die "fail to open file $TempCompFileName : $!"; | |
open OUT_FH, ">$OutFileName" or die "fail to Append file $OutFileName: $!"; | |
while (<INS_FH>) | |
{ | |
print OUT_FH $_; | |
} | |
print OUT_FH chr($appd_cks); | |
close INS_FH; | |
close OUT_FH; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
from https://forum.openwrt.org/viewtopic.php?id=39142