void printChosen(char* s, unsigned size, struct information info) { printf("Length: %u bytes\n", size-1); if(info.hex) { if(info.doXor) printf("XOR: 0x%02x\n", xor_hash(s, size)); if(info.doCrc16) printf("CRC-16: 0x%04x\n", crc_16(s, size)); if(info.doCrc32) printf("CRC-32: 0x%08x\n", crc_32(s, size)); } else { if(info.doXor) printf("XOR: %u\n", xor_hash(s, size)); if(info.doCrc16) printf("CRC-16: %u\n", crc_16(s, size)); if(info.doCrc32) printf("CRC-32: %u\n", crc_32(s, size)); } if(info.doMd5) { printf("MD5: "); printMD5(s, size); printf("\n"); } }
int make_LUH() { FILE *fp_luh,*f_su; struct multi_file_crc files; int k,i,m; int luh_file_length; int luh_fpos; int len,ret; uint64_t len_64; unsigned int su_len; unsigned short length,nu_tar,nu_with,nu_posi,nu_file; unsigned short file_num; unsigned short load_count; unsigned short check_type,check_type_temp; unsigned short crc_type_8,crc_type_16,crc_type_32; unsigned short data_crc_8[10],sup_crc_8[10],multi_crc8; unsigned short data_crc_16[10],sup_crc_16[10]; unsigned short sup_total_crc_16,multi_crc16,header_file_crc; unsigned int data_crc_32[10],sup_crc_32[10]; unsigned int multi_crc32,load_crc; char *ch; char s[15]="load_"; char path_name[50],luh_name[70],data_name[70],sup_name[70]; char target_ID[10][30]= { "luh_target_ID_001","luh_target_ID_002","luh_target_ID_003", "luh_target_ID_004","luh_target_ID_005", "luh_target_ID_006","luh_target_ID_007","luh_target_ID_008", "luh_target_ID_009","luh_target_ID_010" }; char target_with_position[10][40]= { "luh_target_with_position_001","luh_target_with_position_002","luh_target_with_position_003", "luh_target_with_position_004","luh_target_with_position_005", "luh_target_with_position_006","luh_target_with_position_007","luh_target_with_position_008", "luh_target_with_position_009","luh_target_with_position_010" }; char number_target_position[10][40]= { "luh_number_target_position_001","luh_number_target_position_002","luh_number_target_position_003", "luh_number_target_position_004","luh_number_target_position_005", "luh_number_target_position_006","luh_number_target_position_007","luh_number_target_position_008", "luh_number_target_position_009","luh_number_target_position_010" }; char position[10][5][30]= { {"luh_position_001_001","luh_position_001_002","luh_position_001_003","luh_position_001_004","luh_position_001_005"}, {"luh_position_002_001","luh_position_002_002","luh_position_002_003","luh_position_002_004","luh_position_002_005"}, {"luh_position_003_001","luh_position_003_002","luh_position_003_003","luh_position_003_004","luh_position_003_005"}, {"luh_position_004_001","luh_position_004_002","luh_position_004_003","luh_position_004_004","luh_position_004_005"}, {"luh_position_005_001","luh_position_005_002","luh_position_005_003","luh_position_005_004","luh_position_005_005"}, {"luh_position_006_001","luh_position_006_002","luh_position_006_003","luh_position_006_004","luh_position_006_005"}, {"luh_position_007_001","luh_position_007_002","luh_position_007_003","luh_position_007_004","luh_position_007_005"}, {"luh_position_008_001","luh_position_008_002","luh_position_008_003","luh_position_008_004","luh_position_008_005"}, {"luh_position_009_001","luh_position_009_002","luh_position_009_003","luh_position_009_004","luh_position_009_005"}, {"luh_position_010_001","luh_position_010_002","luh_position_010_003","luh_position_010_004","luh_position_010_005"} }; /***************data file*******************/ char data_file_p[10][30]= { "0x0000","0x0000","0x0000", "0x0000","0x0000", "0x0000","0x0000","0x0000", "0x0000","0x0000" }; char data_file_name[10][30]= { "luh_data_file_name_001","luh_data_file_name_002","luh_data_file_name_003", "luh_data_file_name_004","luh_data_file_name_005", "luh_data_file_name_006","luh_data_file_name_007","luh_data_file_name_008", "luh_data_file_name_009","luh_data_file_name_010" }; char data_file_PN[10][30]= { "luh_data_file_PN_001","luh_data_file_PN_002","luh_data_file_PN_003", "luh_data_file_PN_004","luh_data_file_PN_005", "luh_data_file_PN_006","luh_data_file_PN_007","luh_data_file_PN_008", "luh_data_file_PN_009","luh_data_file_PN_010" }; char data_check_value_type[10][40]= { "luh_data_file_check_value_type_001","luh_data_file_check_value_type_002","luh_data_file_check_value_type_003", "luh_data_file_check_value_type_004","luh_data_file_check_value_type_005", "luh_data_file_check_value_type_006","luh_data_file_check_value_type_007","luh_data_file_check_value_type_008", "luh_data_file_check_value_type_009","luh_data_file_check_value_type_010" }; /***************support file*******************/ char support_file_p[10][30]= { "0x0000","0x0000","0x0000", "0x0000","0x0000", "0x0000","0x0000","0x0000", "0x0000","0x0000" }; char support_file_name[10][30]= { "luh_support_file_name_001","luh_support_file_name_002","luh_support_file_name_003", "luh_support_file_name_004","luh_support_file_name_005", "luh_support_file_name_006","luh_support_file_name_007","luh_support_file_name_008", "luh_support_file_name_009","luh_support_file_name_010" }; char support_file_PN[10][30]= { "luh_support_file_PN_001","luh_support_file_PN_002","luh_support_file_PN_003", "luh_support_file_PN_004","luh_support_file_PN_005", "luh_support_file_PN_006","luh_support_file_PN_007","luh_support_file_PN_008", "luh_support_file_PN_009","luh_support_file_PN_010" }; char support_check_value_type[10][40]= { "luh_support_file_check_value_type_001","luh_support_file_check_value_type_002","luh_support_file_check_value_type_003", "luh_support_file_check_value_type_004","luh_support_file_check_value_type_005", "luh_support_file_check_value_type_006","luh_support_file_check_value_type_007","luh_support_file_check_value_type_008", "luh_support_file_check_value_type_009","luh_support_file_check_value_type_010" }; ret=select_section("[media_set]"); if(FAILED==ret) { printf("select_section error\n"); return FAILED; } ch=get_config_var("load_count"); if(ch==NULL) { printf("get load_count error\n"); return FAILED; } load_count=htoi(ch); for(m=1;m<=load_count;m++)//make multi luh files { sprintf(s,"[load_%.3d]",m); //printf("s=%s\n",s); ret=select_section(s); if(FAILED==ret) { printf("select_section error\n"); return FAILED; } strcpy(path_name,"./media665-3"); ch=get_config_var("load_path"); if(ch==NULL) { printf("get load_path error\n"); return FAILED; } change_symbol(ch); strcat(path_name,ch); ch=get_config_var("head_file_name"); if(ch==NULL) { printf("get head_file_name error\n"); return FAILED; } strcpy(luh_name,path_name); strcat(luh_name,ch); fp_luh=fopen(luh_name, "wb+"); if (NULL == fp_luh) { printf("can't open '%s' as config file:%s\n",fp_luh,strerror(errno)); goto EXIT; } /********make LUH**********/ fseek(fp_luh,4,0);//the leading four bytes is file length ch=get_config_var("luh_file_version"); if(ch==NULL) { printf("get luh_file_version error\n"); return FAILED; } ret=fwrite_atohex(ch,fp_luh); if(ret<0) { printf("fwrite_atohex error\n"); return FAILED; } ch=get_config_var("luh_part_flags"); if(ch==NULL) { printf("get luh_part_flags error\n"); return FAILED; } ret=fwrite_atohex(ch,fp_luh); if(ret<0) { printf("fwrite_atohex error\n"); return FAILED; } ch=get_config_var("luh_p_PN_length"); if(ch==NULL) { printf("get luh_p_PN_length error\n"); return FAILED; } ret=fwrite_atohex(ch,fp_luh); if(ret<0) { printf("fwrite_atohex error\n"); return FAILED; } ch=get_config_var("luh_p_target_ID"); if(ch==NULL) { printf("get luh_p_target_ID error\n"); return FAILED; } ret=fwrite_atohex(ch,fp_luh); if(ret<0) { printf("fwrite_atohex error\n"); return FAILED; } ch=get_config_var("luh_p_number_data_file"); if(ch==NULL) { printf("get luh_p_number_data_file error\n"); return FAILED; } ret=fwrite_atohex(ch,fp_luh); if(ret<0) { printf("fwrite_atohex error\n"); return FAILED; } ch=get_config_var("luh_p_number_support_file"); if(ch==NULL) { printf("get luh_p_number_support_file error\n"); return FAILED; } ret=fwrite_atohex(ch,fp_luh); if(ret<0) { printf("fwrite_atohex error\n"); return FAILED; } ch=get_config_var("luh_p_user_defined_data"); if(ch==NULL) { printf("get luh_p_user_defined_data error\n"); return FAILED; } ret=fwrite_atohex(ch,fp_luh); if(ret<0) { printf("fwrite_atohex error\n"); return FAILED; } ch=get_config_var("luh_p_type_description_length"); if(ch==NULL) { printf("get luh_p_type_description_length error\n"); return FAILED; } ret=fwrite_atohex(ch,fp_luh); if(ret<0) { printf("fwrite_atohex error\n"); return FAILED; } ch=get_config_var("luh_p_number_target_positions"); if(ch==NULL) { printf("get luh_p_number_target_positions error\n"); return FAILED; } ret=fwrite_atohex(ch,fp_luh); if(ret<0) { printf("fwrite_atohex error\n"); return FAILED; } ch=get_config_var("luh_p_check_value_length"); if(ch==NULL) { printf("get luh_p_check_value_length error\n"); return FAILED; } ret=fwrite_atohex(ch,fp_luh); if(ret<0) { printf("fwrite_atohex error\n"); return FAILED; } ch=get_config_var("luh_load_PN"); if(ch==NULL) { printf("get luh_load_PN error\n"); return FAILED; } length=htons(strlen(ch)); ret=fwrite(&length,sizeof(short),1,fp_luh);//write PN length if(ret<0) { printf("fwrite error\n"); return FAILED; } ret=fwrite_chars(ch,fp_luh);//write load PN if(ret<0) { printf("fwrite_chars error\n"); return FAILED; } if((strlen(ch)%2)!=0) { ret=fwrite_atohex("0x00",fp_luh); if(ret<0) { printf("fwrite_atohex error\n"); return FAILED; } }//if odd ,add 0x00 ch=get_config_var("luh_type_description"); if(ch==NULL) { printf("get luh_type_description error\n"); } length=htons(strlen(ch)); fwrite(&length,sizeof(short),1,fp_luh);//write description length if(ret<0) { printf("fwrite error\n"); } ret=fwrite_chars(ch,fp_luh);//write description if(ret<0) { printf("fwrite_chars error\n"); return FAILED; } if(strlen(ch)%2!=0) { ret=fwrite_atohex("0x00",fp_luh); if(ret<0) { printf("fwrite_atohex error\n"); return FAILED; } } ch=get_config_var("luh_type_ID"); if(ch==NULL) { printf("get luh_type_ID error\n"); return FAILED; } ret=fwrite_atohex(ch,fp_luh); if(ret<0) { printf("fwrite_atohex error\n"); return FAILED; } ch=get_config_var("luh_number_target_ID"); if(ch==NULL) { printf("get luh_number_target_ID error\n"); return FAILED; } ret=fwrite_atohex(ch,fp_luh); if(ret<0) { printf("fwrite_atohex error\n"); return FAILED; } nu_tar=htoi(ch); for(k=0;k<nu_tar;k++) //number of target HW ID { ch=get_config_var(target_ID[k]); if(ch==NULL) { printf("get target_ID error\n"); return FAILED; } length=htons(strlen(ch)); fwrite(&length,sizeof(short),1,fp_luh);//write position length if(ret<0) { printf("fwrite error\n"); return FAILED; } ret=fwrite_chars(ch,fp_luh);//write target id if(ret<0) { printf("fwrite_chars error\n"); return FAILED; } if(strlen(ch)%2!=0) { ret=fwrite_atohex("0x00",fp_luh); if(ret<0) { printf("fwrite_atohex error\n"); return FAILED; } } } ch=get_config_var("luh_number_target_with_position"); if(ch==NULL) { printf("get luh_number_target_with_position error\n"); return FAILED; } ret=fwrite_atohex(ch,fp_luh); if(ret<0) { printf("fwrite_atohex error\n"); return FAILED; } nu_with=htoi(ch); for(k=0;k<nu_with;k++) //number of target HW ID with position循环 { ch=get_config_var(target_with_position[k]); if(ch==NULL) { printf("get target_with_position error\n"); return FAILED; } length=htons(strlen(ch)); fwrite(&length,sizeof(short),1,fp_luh);//write length if(ret<0) { printf("fwrite error\n"); return FAILED; } ret=fwrite_chars(ch,fp_luh);//write target with position if(ret<0) { printf("fwrite_chars error\n"); return FAILED; } if(strlen(ch)%2!=0) { ret=fwrite_atohex("0x00",fp_luh); if(ret<0) { printf("fwrite_atohex error\n"); return FAILED; } } ch=get_config_var(number_target_position[k]); if(ch==NULL) { printf("get number_target_position error\n"); return FAILED; } ret=fwrite_atohex(ch,fp_luh); if(ret<0) { printf("fwrite_atohex error\n"); return FAILED; } nu_posi=htoi(ch); for(i=0;i<nu_posi;i++) //number of target HW ID position { ch=get_config_var(position[k][i]); if(ch==NULL) { printf("get position error\n"); return FAILED; } length=htons(strlen(ch)); fwrite(&length,sizeof(short),1,fp_luh);//write length if(ret<0) { printf("fwrite error\n"); return FAILED; } ret=fwrite_chars(ch,fp_luh);//write position if(ret<0) { printf("fwrite_chars error\n"); return FAILED; } if(strlen(ch)%2!=0) { ret=fwrite_atohex("0x00",fp_luh); if(ret<0) { printf("fwrite_atohex error\n"); return FAILED; } } } } crc_type_8=htons(CRC_8_TYPE); crc_type_16=htons(CRC_16_TYPE); crc_type_32=htons(CRC_32_TYPE); /***************data file*******************/ #if 1 ch=get_config_var("luh_number_data_file"); if(ch==NULL) { printf("get luh_number_data_file error\n"); return FAILED; } ret=fwrite_atohex(ch,fp_luh); if(ret<0) { printf("fwrite_atohex error\n"); return FAILED; } file_num=htoi(ch); for(k=0;k<file_num;k++) { ret=fwrite_atohex(data_file_p[k],fp_luh); if(ret<0) { printf("fwrite_atohex error\n"); return FAILED; } ch=get_config_var(data_file_name[k]); if(ch==NULL) { printf("get data_file_name error\n"); return FAILED; } //printf("k=%d\n",k); strcpy(data_name,path_name); strcat(data_name,ch); //printf("data_name=%s\n",data_name); strcpy(files.data_file_name[k],data_name); length=htons(strlen(ch)); ret=fwrite(&length,sizeof(short),1,fp_luh);//write file name length if(ret<0) { printf("fwrite error\n"); return FAILED; } ret=fwrite_chars(ch,fp_luh);//write name if(ret<0) { printf("fwrite_chars error\n"); return FAILED; } if(strlen(ch)%2!=0) { ret=fwrite_atohex("0x00",fp_luh); if(ret<0) { printf("fwrite_atohex error\n"); return FAILED; } } ch=get_config_var(data_file_PN[k]); if(ch==NULL) { printf("get data_file_PN error\n"); return FAILED; } length=htons(strlen(ch)); ret=fwrite(&length,sizeof(short),1,fp_luh);//write data PN length if(ret<0) { printf("fwrite error\n"); return FAILED; } ret=fwrite_chars(ch,fp_luh);//write data PN if(ret<0) { printf("fwrite_chars error\n"); return FAILED; } if(strlen(ch)%2!=0) { ret=fwrite_atohex("0x00",fp_luh); if(ret<0) { printf("fwrite_atohex error\n"); return FAILED; } } len=calculate_file_length_32(data_name); if(len%2!=0) { len=(len+1)/2; } else { len=len/2; } len=htonl(len); ret=fwrite(&len,sizeof(int),1,fp_luh);//write data file length if(ret<0) { printf("fwrite error\n"); return FAILED; } data_crc_16[k]=crc_16(data_name); //CRC_16校验 printf("data_crc_16[%d]=0x%x\n",k,data_crc_16[k]); data_crc_16[k]=htons(data_crc_16[k]); ret=fwrite(&data_crc_16[k],sizeof(short),1,fp_luh); if(ret<0) { printf("fwrite error\n"); return FAILED; } len_64=calculate_file_length_64(data_name); //printf("*****data file length_64: %x\n",len); len_64=hton64(len_64);//big end or small end //printf("*****change data file length_64: %llx\n",len); ret=fwrite(&len_64,sizeof(char),8,fp_luh); //write data file length in bytes if(ret<0) { printf("fwrite error\n"); return FAILED; } ch=get_config_var(data_check_value_type[k]); if(ch==NULL) { printf("get data_check_value_type error\n"); return FAILED; } check_type=chtoshort_hex(ch); //printf("data file check type=0x%x\n",check_type); check_type_temp=htons(check_type); switch(check_type) { case 1: ret=fwrite(&crc_type_8,sizeof(short),1,fp_luh);//write data check value length if(ret<0) { printf("fwrite error\n"); return FAILED; } ret=fwrite(&check_type_temp,sizeof(short),1,fp_luh);//write data check value type if(ret<0) { printf("fwrite error\n"); return FAILED; } data_crc_8[k]=crc_8(data_name); //printf("data_crc_8[%d]=0x%x\n",k,data_crc_8[k]); data_crc_8[k]=htonl(data_crc_8[k]); ret=fwrite(&data_crc_8[k],sizeof(char),1,fp_luh);//write data check value if(ret<0) { printf("fwrite error\n"); return FAILED; } break; case 2: ret=fwrite(&crc_type_16,sizeof(short),1,fp_luh);//write data check value length if(ret<0) { printf("fwrite error\n"); return FAILED; } ret=fwrite(&check_type_temp,sizeof(short),1,fp_luh);//write data check value type if(ret<0) { printf("fwrite error\n"); return FAILED; } ret=fwrite(&data_crc_16[k],sizeof(short),1,fp_luh);//write data check value if(ret<0) { printf("fwrite error\n"); return FAILED; } break; case 3: ret=fwrite(&crc_type_32,sizeof(short),1,fp_luh);//write data check value length if(ret<0) { printf("fwrite error\n"); return FAILED; } ret=fwrite(&check_type_temp,sizeof(short),1,fp_luh);//write data check value type if(ret<0) { printf("fwrite error\n"); return FAILED; } data_crc_32[k]=crc_32(data_name); //printf("data_crc_32[%d]=0x%x\n",k,data_crc_32[k]); data_crc_32[k]=htonl(data_crc_32[k]); ret=fwrite(&data_crc_32[k],sizeof(int),1,fp_luh);//write data check value if(ret<0) { printf("fwrite error\n"); return FAILED; } break; default: ; } } #endif /*******support file*********/ ch=get_config_var("luh_number_support_file"); if(ch==NULL) { printf("get luh_number_support_file error\n"); return FAILED; } ret=fwrite_atohex(ch,fp_luh); if(ret<0) { printf("fwrite_atohex error\n"); return FAILED; } nu_file=htoi(ch); for(k=0;k<nu_file;k++) //number of support files { ret=fwrite_atohex(support_file_p[k],fp_luh); if(ret<0) { printf("fwrite_atohex error\n"); return FAILED; } ch=get_config_var(support_file_name[k]); if(ch==NULL) { printf("get support_file_name error\n"); return FAILED; } strcpy(sup_name,path_name); strcat(sup_name,ch); //printf("sup_name=%s\n",sup_name); strcpy(files.su_file_name[k],sup_name); f_su=fopen(sup_name,"r"); if(f_su==NULL) { printf("can`t open support file\n"); return FAILED; } length=htons(strlen(ch)); ret=fwrite(&length,sizeof(short),1,fp_luh);//write support file name length if(ret<0) { printf("fwrite error\n"); return FAILED; } ret=fwrite_chars(ch,fp_luh);//write name if(ret<0) { printf("fwrite_chars error\n"); return FAILED; } if(strlen(ch)%2!=0) { ret=fwrite_atohex("0x00",fp_luh); if(ret<0) { printf("fwrite_atohex error\n"); return FAILED; } } ch=get_config_var(support_file_PN[k]); if(ch==NULL) { printf("get support_file_PN error\n"); return FAILED; } length=htons(strlen(ch)); ret=fwrite(&length,sizeof(short),1,fp_luh);//write PN length if(ret<0) { printf("fwrite error\n"); return FAILED; } ret=fwrite_chars(ch,fp_luh);//write PN if(ret<0) { printf("fwrite_chars error\n"); return FAILED; } if(strlen(ch)%2!=0) { ret=fwrite_atohex("0x00",fp_luh); if(ret<0) { printf("fwrite_atohex error\n"); return FAILED; } } fseek(f_su,0,2);//move to the file end su_len=ftell(f_su); su_len=htonl(su_len); ret=fwrite(&su_len,sizeof(unsigned int),1,fp_luh);//write support file length if(ret<0) { printf("fwrite error\n"); return FAILED; } rewind(f_su);//move to the file start fclose(f_su); sup_crc_16[k]=crc_16(sup_name); sup_crc_16[k]=htons(sup_crc_16[k]); ret=fwrite(&sup_crc_16[k],sizeof(short),1,fp_luh);//write support file crc if(ret<0) { printf("fwrite error\n"); return FAILED; } //printf("sup_crc_16[%d]=0x%x\n",k,sup_crc_16[k]); ch=get_config_var(support_check_value_type[k]); if(ch==NULL) { printf("get support_check_value_type error\n"); return FAILED; } check_type=chtoshort_hex(ch); //printf("support file check type=0x%x\n",check_type); check_type_temp=htons(check_type); switch(check_type) { case 1: ret=fwrite(&crc_type_8,sizeof(short),1,fp_luh); if(ret<0) { printf("fwrite error\n"); return FAILED; } ret=fwrite(&check_type_temp,sizeof(short),1,fp_luh); if(ret<0) { printf("fwrite error\n"); return FAILED; } sup_crc_8[k]=crc_8(sup_name); //printf("sup_crc_8[%d]=0x%x\n",k,sup_crc_8[k]); sup_crc_8[k]=htonl(sup_crc_8[k]); ret=fwrite(&sup_crc_8[k],sizeof(char),1,fp_luh); if(ret<0) { printf("fwrite error\n"); return FAILED; } break; case 2: ret=fwrite(&crc_type_16,sizeof(short),1,fp_luh);//write support check value length if(ret<0) { printf("fwrite error\n"); return FAILED; } ret=fwrite(&check_type_temp,sizeof(short),1,fp_luh);//write support check value type if(ret<0) { printf("fwrite error\n"); return FAILED; } ret=fwrite(&sup_crc_16[k],sizeof(short),1,fp_luh);//write support check value if(ret<0) { printf("fwrite error\n"); return FAILED; } break; case 3: ret=fwrite(&crc_type_32,sizeof(short),1,fp_luh);//write support check value length if(ret<0) { printf("fwrite error\n"); return FAILED; } ret=fwrite(&check_type_temp,sizeof(short),1,fp_luh);//write support check value type if(ret<0) { printf("fwrite error\n"); return FAILED; } sup_crc_32[k]=crc_32(sup_name); // printf("sup_crc_32[%d]=0x%x\n",k,sup_crc_32[k]); sup_crc_32[k]=htonl(sup_crc_32[k]); ret=fwrite(&sup_crc_32[k],sizeof(int),1,fp_luh);//write support check value if(ret<0) { printf("fwrite error\n"); return FAILED; } break; default: ; } } ch=get_config_var("luh_user_defined_data"); if(ch==NULL) { printf("get luh_user_defined_data error\n"); return FAILED; } ret=fwrite_chars(ch,fp_luh); if(ret<0) { printf("fwrite_chars error\n"); return FAILED; } luh_fpos=ftell(fp_luh); files.data_file_num=file_num; files.su_file_num=nu_file; strcpy(files.luh_file_name,luh_name); files.luh_length=luh_fpos; ch=get_config_var("luh_load_check_value_type"); if(ch==NULL) { printf("get luh_load_check_value_type error\n"); return FAILED; } check_type=chtoshort_hex(ch); //printf("load check type=0x%x\n",check_type); check_type_temp=htons(check_type); switch(check_type) { case 1: ret=fwrite(&crc_type_8,sizeof(short),1,fp_luh);//write load crc check value length if(ret<0) { printf("fwrite error\n"); return FAILED; } ret=fwrite(&check_type_temp,sizeof(short),1,fp_luh);//write load crc check value type if(ret<0) { printf("fwrite error\n"); return FAILED; } multi_crc8=multi_crc_8(files); //printf("multi_crc8=0x%x\n",multi_crc8); multi_crc8=htons(multi_crc8); ret=fwrite(&multi_crc8,sizeof(short),1,fp_luh);//write load crc check value if(ret<0) { printf("fwrite error\n"); return FAILED; } break; case 2: ret=fwrite(&crc_type_16,sizeof(short),1,fp_luh);//write load crc check value length if(ret<0) { printf("fwrite error\n"); return FAILED; } ret=fwrite(&check_type_temp,sizeof(short),1,fp_luh);//write load crc check value type if(ret<0) { printf("fwrite error\n"); return FAILED; } multi_crc16=multi_crc_16(files); //printf("multi_crc16=0x%x\n",multi_crc16); multi_crc16=htons(multi_crc16); ret=fwrite(&multi_crc16,sizeof(short),1,fp_luh);//write load crc check value if(ret<0) { printf("fwrite error\n"); return FAILED; } break; case 3: ret=fwrite(&crc_type_32,sizeof(short),1,fp_luh);//write load crc check value length if(ret<0) { printf("fwrite error\n"); return FAILED; } ret=fwrite(&check_type_temp,sizeof(short),1,fp_luh);//write load crc check value type if(ret<0) { printf("fwrite error\n"); return FAILED; } multi_crc32=multi_crc_32(files); //printf("multi_crc32=0x%x\n",multi_crc32); multi_crc32=htonl(multi_crc32); ret=fwrite(&multi_crc32,sizeof(int),1,fp_luh);//write load crc check value if(ret<0) { printf("fwrite error\n"); return FAILED; } break; default: ; } luh_fpos=ftell(fp_luh); header_file_crc=crc_file_pos_16(luh_name,luh_fpos); printf("header_file_crc=0x%x\n",header_file_crc); header_file_crc=htons(header_file_crc); ret=fwrite(&header_file_crc,sizeof(short),1,fp_luh);//header file crc if(ret<0) { printf("fwrite error\n"); return FAILED; } luh_fpos=ftell(fp_luh); load_crc=crc_file_pos_32(luh_name,luh_fpos); printf("load_crc=0x%x\n",load_crc); load_crc=htons(load_crc); ret=fwrite(&load_crc,sizeof(int),1,fp_luh);//load crc if(ret<0) { printf("fwrite error\n"); return FAILED; } fseek(fp_luh,0,2);//move to the file end luh_file_length=ftell(fp_luh); luh_file_length=htonl(luh_file_length); rewind(fp_luh);//move to the file start ret=fwrite(&luh_file_length,sizeof(long),1,fp_luh); if(ret<0) { printf("fwrite error\n"); return FAILED; } EXIT: fclose(fp_luh); return FAILED; } return OK; }
int main(int argc,char *argv[]) { #if 0 unsigned short a,b,c; char data_files[10][50]={"data_1","data_2"}; FILE *fp; fp=fopen("data","wb"); fwrite(crcbuff,sizeof(crcbuff),1,fp); fclose(fp); fp=fopen("data_1","wb"); fwrite(crcbuff_2,sizeof(crcbuff_2),1,fp); fclose(fp); fp=fopen("data_2","wb"); fwrite(crcbuff_3,sizeof(crcbuff_3),1,fp); fclose(fp); fp=fopen("data_3","wb"); fwrite(crcbuff_4,sizeof(crcbuff_4),1,fp); fclose(fp); fp=fopen("data_4","wb"); fwrite(crcbuff_5,sizeof(crcbuff_5),1,fp); fclose(fp); int fpos; fp=fopen("data_4","r"); fseek(fp,2,0); fpos=ftell(fp); printf("fpos=%d\n",fpos); fclose(fp); //struct multi_file_crc file={{"data_1","data_2","data_3","data_4"},4}; struct multi_file_crc file={(char *)data_files,2,{"data_3"},1,"data_4",2}; /* memcpy(file.data_file_name,data_files,sizeof(data_files)); printf("###########%s\n",file.data_file_name[0]); printf("###########%s\n",file.data_file_name[1]); file.data_file_num=2; memcpy(file.su_file_name,"data_3",strlen("data_3")); file.su_file_num=1; strcpy(file.luh_file_name,"data_4"); file.luh_length=2; */ crc=0; a=multi_crc_16(file); crc=0; b=crc_16("data"); crc=0; c=table_crc_16(crcbuff,8); printf("a=%x\n",a); printf("b=%x\n",b); printf("c=%x\n",c); #else FILE *fp; unsigned short crc_data; fp=fopen("good","wb"); char test[256]; int i; for(i=0;i<256;i++) test[i]=(char)i; fwrite(test,sizeof(test),1,fp); fclose(fp); crc_data=crc_16("good"); printf("%x\n",crc_data); #endif }