uint32_t compute_checksum(uint32_t(*calculate_crc)(const uint8_t* data, uint32_t len), const uint8_t* server, size_t server_len, const uint8_t* device, size_t device_len) { uint32_t sum[2]; sum[0] = calculate_crc(server, server_len); sum[1] = calculate_crc(device, device_len); uint32_t result = calculate_crc((uint8_t*)&sum, sizeof(sum)); return result; }
int store_iap(const void *pbuf,unsigned short len) { unsigned int addr; if(icount.size+len>CODE_SIZE){ //PrintLog(LOGTYPE_SHORT,"iap flash overflow\n"); return -1; } if(icount.records>=UPDATE_RECORDS){ //PrintLog(LOGTYPE_SHORT,"iap records overflow\n"); return -1; } //代码写到Flash------------------------------------------------------------- addr = UPDATE_CODE_ADDR+icount.size; flash_program(addr,pbuf,len); //写记录数据---------------------------------------------------------------- irecord.offset = icount.size; irecord.bytes = len; irecord.codecrc = calculate_crc(pbuf,len); addr = UPDATE_RECORD_ADDR; addr = UPDATE_RECORD_ADDR+UPDATE_RECORD_SIZE*icount.records; f_program(addr,&irecord,sizeof(irecord)); icount.size+=len; icount.records++; //PrintLog(LOGTYPE_SHORT,"save code frame:%d,size:%d",icount.records,icount.size); return 0; }
void SendDataccTalk(uint8_t slave,uint8_t numberdata,uint8_t master,uint8_t command,uint8_t *data){ uint8_t i; uint8_t checksum=0; cctalk.FlagReceiveData=0; cctalk.PointerEcho = 5+numberdata; CleanBufferInput(); while(AS_CCTALK_SendChar(slave)!=ERR_OK); while(AS_CCTALK_SendChar(numberdata)!=ERR_OK); while(AS_CCTALK_SendChar(master)!=ERR_OK); while(AS_CCTALK_SendChar(command)!=ERR_OK); if(numberdata){ for(i=0;i<numberdata;i++){ while(AS_CCTALK_SendChar(data[i])!=ERR_OK); } } checksum=calculate_crc(slave,master,command,numberdata,data); while(AS_CCTALK_SendChar(checksum)!=ERR_OK); }
uint16 Volume::_GroupCheckSum(ext2_block_group *group, int32 index) { uint16 checksum = 0; if (HasChecksumFeature()) { int32 number = B_HOST_TO_LENDIAN_INT32(index); checksum = calculate_crc(0xffff, fSuperBlock.uuid, sizeof(fSuperBlock.uuid)); checksum = calculate_crc(checksum, (uint8*)&number, sizeof(number)); checksum = calculate_crc(checksum, (uint8*)group, 30); if (Has64bitFeature()) { checksum = calculate_crc(checksum, (uint8*)group + 34, fGroupDescriptorSize - 34); } } return checksum; }
void configuration_load(void) { eeprom_read_block(¤t_configuration, 0, sizeof(configuration_type)); if (calculate_crc(¤t_configuration) != current_configuration.crc) { memcpy(¤t_configuration, &default_configuration, sizeof(configuration_type)); } dispatch_configuration_change_event(); }
/*! * @brief Reads the information block from the chip. * @return #mxt_rc */ int mxt_read_info_block(struct mxt_device *mxt) { int ret; /* Read the ID Information from the chip */ uint8_t *info_blk = (uint8_t *)calloc(1, sizeof(struct mxt_id_info)); if (info_blk == NULL) { mxt_err(mxt->ctx, "Memory allocation failure"); return MXT_ERROR_NO_MEM; } ret = mxt_read_register(mxt, info_blk, 0, sizeof(struct mxt_id_info)); if (ret) { mxt_err(mxt->ctx, "Failed to read ID information"); return ret; } /* Determine the number of bytes for checksum calculation */ int num_objects = ((struct mxt_id_info*) info_blk)->num_objects; size_t crc_area_size = sizeof(struct mxt_id_info) + num_objects * sizeof(struct mxt_object); /* Allocate space to read Information Block AND Checksum from the chip */ size_t info_block_size = crc_area_size + sizeof(struct mxt_raw_crc); info_blk = (uint8_t *)realloc(info_blk, info_block_size); if (info_blk == NULL) { mxt_err(mxt->ctx, "Memory allocation failure"); return MXT_ERROR_NO_MEM; } /* Read the entire Information Block from the chip */ ret = mxt_read_register(mxt, info_blk, 0, info_block_size); if (ret) { mxt_err(mxt->ctx, "Failed to read Information Block"); return ret; } /* Update pointers in device structure */ mxt->info.raw_info = info_blk; mxt->info.id = (struct mxt_id_info*) info_blk; mxt->info.objects = (struct mxt_object*)(info_blk + sizeof(struct mxt_id_info)); mxt->info.crc = convert_crc((struct mxt_raw_crc*) (info_blk + crc_area_size)); /* Calculate and compare Information Block Checksum */ ret = calculate_crc(mxt, mxt->info.crc, info_blk, crc_area_size); if (ret) return ret; return MXT_SUCCESS; }
qbyte wtp_signature ( const char *filename) /* Name of executable program */ { static char signature [30]; /* Signature is XXXXXXXX-XXXXXXXX-XXXXXXXX (hash-size-time) */ /* then passed through 32-bit CRC calculation. */ sprintf (signature, "%08lX-%08lX-%08lX", strhash (filename), get_file_size (filename), get_file_time (filename)); return (calculate_crc ((byte *) signature, strlen (signature))); }
status_t DirectoryIterator::Lookup(const char* name, size_t nameLength, ino_t* _id) { if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) { if (strcmp(name, ".") == 0 || fInode->ID() == BTRFS_OBJECT_ID_CHUNK_TREE) { *_id = fInode->ID(); return B_OK; } return fInode->FindParent(_id); } uint32 hash = calculate_crc((uint32)~1, (uint8*)name, nameLength); struct btrfs_key key; key.SetType(BTRFS_KEY_TYPE_DIR_ITEM); key.SetObjectID(fInode->ID()); key.SetOffset(hash); btrfs_dir_entry *entries; size_t length; status_t status = fInode->GetVolume()->FSTree()->FindExact(key, (void**)&entries, &length); if (status != B_OK) { TRACE("DirectoryIterator::Lookup(): Couldn't find entry with hash %lu " "\"%s\"\n", hash, name); return status; } btrfs_dir_entry *entry = entries; uint16 current = 0; while (current < length) { current += entry->Length(); break; // TODO there could be several entries with the same name hash entry = (btrfs_dir_entry *)((uint8*)entry + entry->Length()); } TRACE("DirectoryIterator::Lookup() entries_length %ld name_length %d\n", length, entry->NameLength()); *_id = entry->InodeID(); free(entries); return B_OK; }
void ModBus_Recive(AS1_TComData dat) { byte i; word crc; msj[Step]=dat; switch(Step){ case 0:if (msj[0]!= iId || !Timer_isfinish(&comu_timer)){ Timer_setTime(&comu_timer,3); break; //Id } case 1: case 2: case 3: case 4: case 5: case 6: Step++; Timer_setTime(&comu_timer,3);// ckeckeo que no pasen 3ms break; case 7: Step++; Timer_Restart(&comu_timer);// ckeckeo que no pasen 3ms crc = calculate_crc(msj,6); if ( (crc%256)!=msj[6] || (crc/256)!=msj[7] ){ Step=0; break; } for(i=0;ModbusOnRecive[i].OnFunction!=NULL;i++){ if(ModbusOnRecive[i].func_number==msj[1]){ (*ModbusOnRecive[i].OnFunction)(msj); break; } } } }
/* ** =================================================================== ** Method : ModBus_Send ** ** Description : ** llamar a esta función para enviar un mensaje por el puerto ** serie ** =================================================================== */ byte ModBus_Send(byte * data,byte cant){ byte i; word crc; if(AS1_Tx) return ERR_BUSY; for(i=0;i<cant;i++) msj_out[i]=data[i]; crc = calculate_crc(msj_out,cant); msj_out[i]= crc%256; // a los siguientes 2 bytes les agrego el crc msj_out[i+1]= crc/256; // a los siguientes 2 bytes les agrego el crc cant_msjs_out=cant+2; AS1_Tx=TRUE; onSend = ModBus_OnSend; ModBus_OnSend();//Puede ser demasiado rápido llamar a esta funcion aca, verificar return ERR_OK; }
int _tmain(int argc, _TCHAR* argv[]) { unsigned char p[133] = { 0x01,1,254,0x0f,12,1,1,1,1,1,0,//120 0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A, 0,0 } ; unsigned char pp[128] ={ 0x0f,12,1,1,1,1,1,0,//120 0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A }; unsigned int crc = calculate_crc(pp,128); unsigned char hi =(crc&0xFF00)>>8; unsigned char lo =crc&0x00FF; printf("crc = %d,,hi = %d,,lo = %d",crc,hi,lo); getchar(); return 0; }
///----------------------------------------------- // Validates the data for crc and mandatory fields ///----------------------------------------------- int verify_fru_data(const uint8_t *data, const size_t len) { uint8_t checksum = 0; int rc = -1; // Validate for first byte to always have a value of [1] if(data[0] != IPMI_FRU_HDR_BYTE_ZERO) { fprintf(stderr, "Invalid entry:[%d] in byte-0\n",data[0]); return rc; } #ifdef __IPMI_DEBUG__ else { printf("SUCCESS: Validated [0x%X] in entry_1 of fru_data\n",data[0]); } #endif // See if the calculated CRC matches with the embedded one. // CRC to be calculated on all except the last one that is CRC itself. checksum = calculate_crc(data, len - 1); if(checksum != data[len-1]) { #ifdef __IPMI_DEBUG__ fprintf(stderr, "Checksum mismatch." " Calculated:[0x%X], Embedded:[0x%X]\n", checksum, data[len]); #endif return rc; } #ifdef __IPMI_DEBUG__ else { printf("SUCCESS: Checksum matches:[0x%X]\n",checksum); } #endif return EXIT_SUCCESS; }
void test_iap(void) { unsigned int i,addr; //unsigned char *pbuf; store_iap_start(); for(i=0;i<150;i++){ memset(iapbuffer,i,sizeof(iapbuffer)); store_iap(iapbuffer,sizeof(iapbuffer)); delay_us(30000); } store_iap_end(); memset(&icount,0,sizeof icount); if(is_iap_legal()==0){ PrintLog(LOGTYPE_SHORT,"iap unlegal\n"); return; } for(i=0;i<icount.records;i++){ //先读record------------------------------------------------------------- addr = UPDATE_RECORD_ADDR+i*UPDATE_RECORD_SIZE; if(f_read(addr,&irecord,sizeof(irecord))!=sizeof(irecord)){ PrintLog(LOGTYPE_SHORT,"read iap record error\n"); exit(0); } PrintLog(LOGTYPE_SHORT,"read iap record %d ok\n",i); //读数据区--------------------------------------------------------------- addr = UPDATE_CODE_ADDR+irecord.offset; flash_read(addr,iapbuffer,irecord.bytes); if(calculate_crc(iapbuffer,irecord.bytes)!=irecord.codecrc){ PrintLog(LOGTYPE_SHORT,"read code error\n");//代码区校验出错 exit(0); } PrintLog(LOGTYPE_SHORT,"read iap code %d ok\n",i); } }
uint8_t rf12_rxfinish( char *data ) { uint16_t crc, crc_chk = 0; uint8_t crc8, crc_chk8=0; uint8_t i, size = 12; if(RF01_status.Rx) return(255); //odbiór jeszcze nie zakoñczony if(!RF01_status.New) return(254); //jeszcze stare dane w buforze if( size > RF01_DataLength ) { data[0] = 0; RF01_status.New = 0; return 0; // b³¹d wielkoœci ramki } crc_chk8 = calculate_crc(RF01_Data); crc8=RF01_Data[size]; /* for(i=0; i<size +1 ; i++) crc_chk = crcUpdate(crc_chk, RF01_Data[i]); crc = RF01_Data[i++]; crc |= RF01_Data[i] << 8; */ crc = 0; RF01_status.New = 0; if(crc8 != crc_chk8) return(0); //b³¹d sumy CRC lub rozmiaru ramki else { for(i=0; i<size-1; i++) data[i] = RF01_Data[i]; data[ size-1 ] = 0; // zakoñczenie ramki zerem return( size-1 ); // rozmiar odebranej ramki w bajtach } }
// Find expanded hash, optimized with caching, return JSON. // If optimizing, cache hashes and sources. std::string scan_manager_t::find_expanded_hash_json( const bool optimizing, const std::string& block_hash) { // fields to hold the scan uint64_t k_entropy; std::string block_label; uint64_t count; hashdb::source_offsets_t* source_offsets = new hashdb::source_offsets_t; // scan bool matched = scan_manager_t::find_hash(block_hash, k_entropy, block_label, count, *source_offsets); // done if no match if (matched == false) { delete source_offsets; return ""; } // prepare JSON rapidjson::Document json_doc; rapidjson::Document::AllocatorType& allocator = json_doc.GetAllocator(); json_doc.SetObject(); // block_hash std::string hex_block_hash = hashdb::bin_to_hex(block_hash); json_doc.AddMember("block_hash", v(hex_block_hash, allocator), allocator); // report hash if not caching or this is the first time for the hash if (!optimizing || hashes->locked_insert(block_hash)) { // add entropy json_doc.AddMember("k_entropy", k_entropy, allocator); // add block_label json_doc.AddMember("block_label", v(block_label, allocator), allocator); // add count json_doc.AddMember("count", count, allocator); // add source_list_id uint32_t crc = calculate_crc(*source_offsets); json_doc.AddMember("source_list_id", crc, allocator); // the sources array rapidjson::Value json_sources(rapidjson::kArrayType); // add each source object for (hashdb::source_offsets_t::const_iterator it = source_offsets->begin(); it != source_offsets->end(); ++it) { if (!optimizing || sources->locked_insert(it->file_hash)) { // create a json_source object for the json_sources array rapidjson::Value json_source(rapidjson::kObjectType); // provide the complete source information for this source provide_source_information(*this, it->file_hash, allocator, json_source); json_sources.PushBack(json_source, allocator); } } json_doc.AddMember("sources", json_sources, allocator); // add source_offsets as triplets of file hash, sub_count, offset list rapidjson::Value json_source_offsets(rapidjson::kArrayType); for (hashdb::source_offsets_t::const_iterator it = source_offsets->begin(); it != source_offsets->end(); ++it) { // file hash json_source_offsets.PushBack( v(hashdb::bin_to_hex(it->file_hash), allocator), allocator); // sub_count json_source_offsets.PushBack(it->sub_count, allocator); // file offset array rapidjson::Value json_file_offsets(rapidjson::kArrayType); for (std::set<uint64_t>::const_iterator it2 = it->file_offsets.begin(); it2 != it->file_offsets.end(); ++it2) { json_file_offsets.PushBack(*it2, allocator); } json_source_offsets.PushBack(json_file_offsets, allocator); } json_doc.AddMember("source_offsets", json_source_offsets, allocator); } delete source_offsets; // return JSON text rapidjson::StringBuffer strbuf; rapidjson::Writer<rapidjson::StringBuffer> writer(strbuf); json_doc.Accept(writer); return strbuf.GetString(); }
//------------------------------------------------------------------------- // Validates the CRC and if found good, calls fru areas parser and calls // Inventory Dbus with the dictionary of Name:Value for updating. //------------------------------------------------------------------------- int ipmi_validate_and_update_inventory(const uint8_t fruid, const uint8_t *fru_data) { // Used for generic checksum calculation uint8_t checksum = 0; // This can point to any FRU entry. uint8_t fru_entry; // A generic offset locator for any FRU record. uint8_t area_offset = 0; // First 2 bytes in the record. uint8_t fru_area_hdr[2] = {0}; // To hold info about individual FRU record areas. fru_area_t fru_area; // For parsing and updating Inventory. fru_area_vec_t fru_area_vec; int rc = 0; uint8_t common_hdr[sizeof(struct common_header)] = {0}; memset(common_hdr, 0x0, sizeof(common_hdr)); // Copy first 8 bytes to verify common header memcpy(common_hdr, fru_data, sizeof(common_hdr)); // Validate for first byte to always have a value of [1] if(common_hdr[0] != IPMI_FRU_HDR_BYTE_ZERO) { fprintf(stderr, "ERROR: Common Header entry_1:[0x%X] Invalid.\n",common_hdr[0]); return -1; } else { printf("SUCCESS: Validated [0x%X] in common header\n",common_hdr[0]); } // Validate the header checskum that is at last byte ( Offset: 7 ) checksum = calculate_crc(common_hdr, sizeof(common_hdr)-1); if(checksum != common_hdr[IPMI_FRU_HDR_CRC_OFFSET]) { #ifdef __IPMI__DEBUG__ fprintf(stderr, "ERROR: Common Header checksum mismatch." " Calculated:[0x%X], Embedded:[0x%X]\n", checksum, common_hdr[IPMI_FRU_HDR_CRC_OFFSET]); #endif return -1; } else { printf("SUCCESS: Common Header checksum MATCH:[0x%X]\n",checksum); } //------------------------------------------- // TODO: Add support for Multi Record later //------------------------------------------- // Now start walking the common_hdr array that has offsets into other FRU // record areas and validate those. Starting with second entry since the // first one is always a [0x01] for(fru_entry = IPMI_FRU_INTERNAL_OFFSET; fru_entry < (sizeof(struct common_header) -2); fru_entry++) { // Offset is 'value given in' internal_offset * 8 from the START of // common header. So an an example, 01 00 00 00 01 00 00 fe has // product area set at the offset 01 * 8 --> 8 bytes from the START of // common header. That means, soon after the header checksum. area_offset = common_hdr[fru_entry] * IPMI_EIGHT_BYTES; if(area_offset) { memset((void *)&fru_area, 0x0, sizeof(fru_area_t)); // Enumerated FRU area. fru_area.type = get_fru_area_type(fru_entry); // From start of fru header + record offset, copy 2 bytes. fru_area.offset = &((uint8_t *)fru_data)[area_offset]; memcpy(fru_area_hdr, fru_area.offset, sizeof(fru_area_hdr)); // A NON zero value means that the vpd packet has the data for that // area. err if first element in the record header is _not_ a [0x01]. if(fru_area_hdr[0] != IPMI_FRU_HDR_BYTE_ZERO) { fprintf(stderr, "ERROR: Unexpected :[0x%X] found at Record header\n", fru_area_hdr[0]); // This vector by now may have had some entries. Since this is a // failure now, clear the state data. fru_area_vec.clear(); return -1; } else { printf("SUCCESS: Validated [0x%X] in fru record:[%d] header\n", fru_area_hdr[0],fru_entry); } // Read Length bytes ( makes a complete record read now ) fru_area.len = fru_area_hdr[1] * IPMI_EIGHT_BYTES; #ifdef __IPMI_DEBUG__ printf("AREA NO[%d], SIZE = [%d]\n",fru_entry, fru_area.len); #endif uint8_t fru_area_data[fru_area.len]; memset(fru_area_data, 0x0, sizeof(fru_area_data)); memmove(fru_area_data, fru_area.offset, sizeof(fru_area_data)); // Calculate checksum (from offset -> (Length-1)). // All the bytes except the last byte( which is CRC :) ) will // participate in calculating the checksum. checksum = calculate_crc(fru_area_data, sizeof(fru_area_data)-1); // Verify the embedded checksum in last byte with calculated checksum // record_len -1 since length is some N but numbering is 0..N-1 if(checksum != fru_area_data[fru_area.len-1]) { #ifdef __IPMI_DEBUG__ fprintf(stderr, "ERROR: FRU Header checksum mismatch. " " Calculated:[0x%X], Embedded:[0x%X]\n", checksum, fru_area_data[fru_area.len - 1]); #endif // This vector by now may have had some entries. Since this is a // failure now, clear the state data. fru_area_vec.clear(); return -1; } else { printf("SUCCESS: FRU Header checksum MATCH:[0x%X]\n",checksum); } // Everything is rihgt about this particular FRU record, fru_area_vec.push_back(fru_area); // Update the internal structure with info about this entry that is // needed while handling each areas. } // If the packet has data for a particular data record. } // End walking all the fru records. // If we reach here, then we have validated the crc for all the records and // time to call FRU area parser to get a Name:Value pair dictionary. // This will start iterating all over again on the buffer -BUT- now with the // job of taking each areas, getting it parsed and then updating the // DBUS. if(!(fru_area_vec.empty())) { rc = ipmi_update_inventory(fruid, fru_data, fru_area_vec); } // We are done with this FRU write packet. fru_area_vec.clear(); return rc; }
void configuration_save(void) { current_configuration.crc = calculate_crc(¤t_configuration); eeprom_write_block(¤t_configuration, 0, sizeof(configuration_type)); dispatch_configuration_change_event(); }