bool FileMapInfo::FileMapHeader::validate() { if (VerifySharedSpaces && compute_crc() != _crc) { fail_continue("Header checksum verification failed."); return false; } if (_version != current_version()) { FileMapInfo::fail_continue("The shared archive file is the wrong version."); return false; } if (_magic != (int)0xf00baba2) { FileMapInfo::fail_continue("The shared archive file has a bad magic number."); return false; } char header_version[JVM_IDENT_MAX]; get_header_version(header_version); if (strncmp(_jvm_ident, header_version, JVM_IDENT_MAX-1) != 0) { if (TraceClassPaths) { tty->print_cr("Expected: %s", header_version); tty->print_cr("Actual: %s", _jvm_ident); } FileMapInfo::fail_continue("The shared archive file was created by a different" " version or build of HotSpot"); return false; } if (_obj_alignment != ObjectAlignmentInBytes) { FileMapInfo::fail_continue("The shared archive file's ObjectAlignmentInBytes of %d" " does not equal the current ObjectAlignmentInBytes of %d.", _obj_alignment, ObjectAlignmentInBytes); return false; } return true; }
int valid_frame(frame frame) { if (frame.crc != compute_crc(frame)) { return 0; } if (frame.type == 1) { if (frame.length > MAX_PAYLOAD_SIZE) { return 0; } if (frame.window != 0) { return 0; } } else if (frame.type == 2) { if (frame.length != 0) { return 0; } if (strlen(frame.payload) != 0) { return 0; } } else return 0; return 1; }
int write_data_crc(FILE *file, const void *data, size_t size, uint32_t* crc) { int ret = write_data(file, data, size); *crc = compute_crc(*crc, (uint8_t*)data, size); return ret; }
void writedata(off_t offset, size_t size, void *buf) { ssize_t cc; /* fprintf(stderr, "Writing %d bytes at %qd\n", size, offset); */ #ifndef FRISBEE if (docrconly) { compute_crc(buf, size, &crc); cc = size; } else #endif if (seekable) { cc = pwrite(outfd, buf, size, offset); } else if (offset == nextwriteoffset) { cc = write(outfd, buf, size); } else { fprintf(stderr, "Non-contiguous write @ %qu (should be %qu)\n", offset, nextwriteoffset); exit(1); } if (cc != size) { if (cc < 0) perror("write error"); else fprintf(stderr, "Short write!\n"); exit(1); } nextwriteoffset = offset + cc; totalrdata += cc; }
/* * Generates an rtp packet acknowledging the sequence number seqnumb. * */ void acknowledge(int lastack) { packetstruct packet; packet.type = PTYPE_ACK; packet.window = BUFFSIZE; packet.seqnum = lastack + 1; // idx of the next expected packet packet.length = htons(0); // set the payload to zero memset(packet.payload, 0, PAYLOADSIZE); uint32_t crc; if(compute_crc(&packet, &crc)) { die("Error computing CRC"); } packet.crc = htonl(crc); ssize_t lensent = sendto(sock_id, &packet,sizeof(packetstruct),0, (struct sockaddr *) &src_host, src_len); if(verbose){ printf(" Ack sent with seq : %u\n", packet.seqnum); } if(lensent != sizeof(packetstruct)) { die("Mismatch in number of sent bytes"); } }
void create_ack_frame(uint8_t seq, frame* frame) { frame->type = 2; frame->window = MAX_WINDOW_SIZE - 1; frame->seq = get_seq(seq); frame->length = 0; init_payload(frame->payload); frame->crc = compute_crc(*frame); }
int crc_check(word *tabel, unsigned char buffer[], unsigned char length) { unsigned char upper_byte, lower_byte; word acum; acum = compute_crc(tabel, buffer + 1, length - 1); upper_byte = (acum & 0xff00) >> 8; lower_byte = acum && 0xff; return ( upper_byte == buffer [ length ] ) && ( lower_byte == buffer [ length + 1 ] ); }
void create_data_frame(uint8_t seq, char* data, uint16_t len, frame* frame) { frame->type = 1; frame->window = 0; frame->seq = get_seq(seq); frame->length = len; init_payload(frame->payload); memcpy(frame->payload, data, frame->length + 1); frame->crc = compute_crc(*frame); }
void crc_packet(word *tabel, unsigned char buffer[], unsigned char length) { unsigned char upper_byte, lower_byte; word acum; acum = compute_crc(tabel, buffer + 1, length - 1); upper_byte = (acum & 0xff00) >> 8; lower_byte = acum && 0xff; buffer [ length ] = upper_byte; buffer [ length + 1 ] = lower_byte; }
void writezeros(off_t offset, off_t zcount) { size_t zcc; assert((offset & (SECSIZE-1)) == 0); #ifndef FRISBEE if (docrconly) nextwriteoffset = offset; else #endif if (seekable) { /* * We must always seek, even if offset == nextwriteoffset, * since we are using pwrite. */ if (lseek(outfd, offset, SEEK_SET) < 0) { perror("lseek to write zeros"); exit(1); } nextwriteoffset = offset; } else if (offset != nextwriteoffset) { fprintf(stderr, "Non-contiguous write @ %qu (should be %qu)\n", offset, nextwriteoffset); exit(1); } while (zcount) { if (zcount <= OUTSIZE) zcc = zcount; else zcc = OUTSIZE; #ifndef FRISBEE if (docrconly) compute_crc(zeros, zcc, &crc); else #endif if ((zcc = write(outfd, zeros, zcc)) != zcc) { if (zcc < 0) { perror("Writing Zeros"); } exit(1); } zcount -= zcc; totalrdata += zcc; nextwriteoffset += zcc; } }
/** * \brief Compute CRC of a buffer and compare it with the reference CRC. * * \param p_Buffer The buffer holding the data. * \param ul_length The buffer length. * \param ul_type The polynomial type(CRCCU_MR_PTYPE_XXX). * \param ul_ref_crc Reference CRC for the buffer. * * \return CRC of the buffer. */ static uint32_t compute_crc_and_compare(uint8_t *p_buffer, uint32_t ul_length, uint32_t ul_type, uint32_t ul_ref_crc) { uint32_t ul_crc; /* Compute CRC */ ul_crc = compute_crc(p_buffer, ul_length, ul_type); /* Compare CRC */ if (ul_crc == ul_ref_crc) { puts(" CRC matches the reference value.\r"); } else { puts(" CRC does NOT match the reference value.\r"); } return ul_crc; }
int main(void) { char msg[1024]; int crc; for (;;) { gets(msg); if (msg[0] == '#') { break; } crc = compute_crc(msg); printf("%02X %02X\n", crc >> 8, crc & 0xff); } return EXIT_SUCCESS; }
/** * \brief Application entry point for CRCCU example. * * \return Unused (ANSI-C compatibility). */ int main(void) { uint32_t ul_counter; uint32_t ul_crc; /* Initialize the system */ sysclk_init(); board_init(); /* Configure the console uart */ configure_console(); /* Output example information */ puts(STRING_HEADER); /* Enable CRCCU peripheral clock */ sysclk_enable_peripheral_clock(CRCCU); /* Fill data buffer in SRAM (The data may be random data) */ for (ul_counter = 0; ul_counter < BUFFER_LENGTH; ul_counter++) { g_uc_data_buf[ul_counter] = ul_counter; } /* Fill data buffer in Flash, the data is same as in SRAM */ flashcalw_memcpy((void *)FLASH_BUFFER_ADDRESS, g_uc_data_buf, sizeof(g_uc_data_buf), true); /* Test CRC with CCITT-16 polynomial */ puts("\n\r====Test CRC with CCITT 16 (0x1021) ====\r"); /* Compute CRC in SRAM */ puts("Test CRC in SRAM buffer\r"); ul_crc = compute_crc(g_uc_data_buf, BUFFER_LENGTH, CRCCU_MR_PTYPE_CCITT16); /* Compute CRC in Flash and compare it with the result in SRAM */ puts("Test CRC in Flash buffer\r"); compute_crc_and_compare((uint8_t *) FLASH_BUFFER_ADDRESS, BUFFER_LENGTH, CRCCU_MR_PTYPE_CCITT16, ul_crc); /* Test CRC with CASTAGNOLI polynomial */ puts("\n\r====Test CRC with CASTAGNOLI (0x1EDC6F41) ====\r"); /* Compute CRC in SRAM */ puts("Test CRC in SRAM buffer\r"); ul_crc = compute_crc(g_uc_data_buf, BUFFER_LENGTH, CRCCU_MR_PTYPE_CASTAGNOLI); /* Compute CRC in Flash and compare it with the result in SRAM */ puts("Test CRC in Flash buffer\r"); compute_crc_and_compare((uint8_t *) FLASH_BUFFER_ADDRESS, BUFFER_LENGTH, CRCCU_MR_PTYPE_CASTAGNOLI, ul_crc); /* Test CRC with CCITT 802.3 polynomial */ puts("\n\r====Test CRC with CCITT 802.3 (0x04C11DB7) ====\r"); /* Compute CRC in SRAM */ puts("Test CRC in SRAM buffer\r"); ul_crc = compute_crc(g_uc_data_buf, BUFFER_LENGTH, CRCCU_MR_PTYPE_CCITT8023); /* Compute CRC in Flash and compare it with the result in SRAM */ puts("Test CRC in Flash buffer\r"); compute_crc_and_compare((uint8_t *) FLASH_BUFFER_ADDRESS, BUFFER_LENGTH, CRCCU_MR_PTYPE_CCITT8023, ul_crc); while (1) { } }
void fujitsu_control::send_data() { int swing, air_mode, fan_mode, temp; swing = m_swing_h ? 0x02 : 0x00; swing |= m_swing_v ? 0x01 : 0x00; switch (m_air_mode) { case MODE_COOL: air_mode = FJTSU_AIR_MODE_COOL; break; case MODE_AUTO: air_mode = FJTSU_AIR_MODE_AUTO; break; case MODE_HEAT: air_mode = FJTSU_AIR_MODE_HEAT; break; case MODE_DRY: air_mode = FJTSU_AIR_MODE_DRY; break; case MODE_FAN: air_mode = FJTSU_AIR_MODE_FAN; break; } switch (m_fan_mode) { case FAN_SPEED_AUTO: fan_mode = FJTSU_FAN_SPEED_AUTO; break; case FAN_SPEED_LOW: fan_mode = FJTSU_FAN_SPEED_LOW; break; case FAN_SPEED_MID: fan_mode = FJTSU_FAN_SPEED_MID; break; case FAN_SPEED_HIGH: fan_mode = FJTSU_FAN_SPEED_HIGH; break; case FAN_SPEED_QUIET: fan_mode = FJTSU_FAN_SPEED_QUIET; break; } // Clamp to airconditioner limit temp = m_temperature > 31 ? 31 : m_temperature; temp = temp < 16 ? 16 : temp; char bytes[16], i; bytes[0] = 0x14; bytes[1] = 0x63; bytes[2] = 0x00; bytes[3] = 0x10; bytes[4] = 0x10; bytes[5] = 0xFE; bytes[6] = 0x09; bytes[7] = 0x30; bytes[8] = ((temp - 16) << 4) | m_powermem; bytes[9] = air_mode; bytes[10] = fan_mode | (swing << 4); bytes[11] = 0x00; bytes[12] = 0x00; bytes[13] = 0x00; bytes[14] = 0x20; bytes[15] = compute_crc(bytes); m_powermem = false; while(!ir.can_begin_send()){ // Don't start until we risk a clock overflow } noInterrupts(); send_leader(); for (i = 0; i < 16; ++i) { send_byte(bytes[i]); } send_trailer(); interrupts(); }
/*! @brief Set-up outgoing packet's address and am_type */ uint8_t serial_msg_build( uint16_t destination, uint8_t* packet_buffer ) { uint8_t total_size = 0; uint8_t buffer_index = 0; uint16_t tmpcrc; out_packet.source = 0xcc; out_packet.destination = destination; // broadcast out_packet.packet_type = 0xbb; // ?? out_packet.group = 0xdd; out_packet.length = sizeof( test_serial_msg_t ); out_packet.am_type = AM_SERIAL_MSG; // Add start character p_tmp_buffer[0] = SYNC_BYTE; // Copy the packet header memcpy( (void*)( p_tmp_buffer + 1), (void*)( &out_packet ), sizeof( tos_packet_header_t ) ); // Copy data memcpy( (void*)( p_tmp_buffer + 1 + sizeof( tos_packet_header_t ) ), (void*)( &p_data_buffer), sizeof( test_serial_msg_t ) ); // calculate CRC tmpcrc = compute_crc( p_tmp_buffer, sizeof( tos_packet_header_t ) + sizeof( test_serial_msg_t ) + 4 ); // Add CRC memcpy( (void*)( p_tmp_buffer + 1 + sizeof( tos_packet_header_t ) + sizeof( test_serial_msg_t ) ), (void*)(&tmpcrc), sizeof(uint16_t) ); // add end byte memset( (void*)( p_tmp_buffer + 1 + sizeof( tos_packet_header_t ) + sizeof( test_serial_msg_t ) + sizeof(uint16_t) ), SYNC_BYTE, 1 ); //total_size = sizeof( tos_packet_header_t ) + sizeof( test_serial_msg_t ) // + 2 + 2; // 2 CRC bytes + header and footer // escape characters and copy to output buffer packet_buffer[total_size++]=p_tmp_buffer[buffer_index++]; while( buffer_index < ( 1 +sizeof( tos_packet_header_t ) + sizeof( test_serial_msg_t ) + sizeof(uint16_t) + 1 - 1) ) { if( ( SYNC_BYTE == p_tmp_buffer[buffer_index] ) || ( ESCAPE_BYTE == p_tmp_buffer[buffer_index] ) ) { packet_buffer[total_size++] = ESCAPE_BYTE; packet_buffer[total_size++] = p_tmp_buffer[buffer_index++] ^ 0x20; } else { packet_buffer[total_size++] = p_tmp_buffer[buffer_index++]; } } packet_buffer[total_size++] = p_tmp_buffer[buffer_index++]; return total_size; }
int recv_file(SOCKET soc, char *filename) { WSADATA wsa_data; SOCKET data_socket = INVALID_SOCKET; struct sockaddr_in recv_data_addr, addr; HPACKET packet; HPARTITION partition; FILE *fp; int return_code; unsigned long partition_id, at_location, write_amount, tries; unsigned char packet_count, packets_recv; unsigned int addr_len = sizeof(addr); char control_message[MAX_INPUT_LENGTH]; __int64 file_size; DWORD timing; float transfer_kb, transfer_sec, transfer_mbps; return_code = getsockname(soc, (struct sockaddr *)&addr, (socklen_t *)&addr_len); if(return_code < 0) { printf("Fetch of local port failed.\n"); return 0; } if(fopen_s(&fp, filename, "wb") > 0) { printf("Unable to write file.\n"); return 0; } WSAStartup(MAKEWORD(2,2), &wsa_data); data_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); recv_data_addr.sin_family = AF_INET; recv_data_addr.sin_port = htons(CONNECT_PORT_N);//addr.sin_port; recv_data_addr.sin_addr.s_addr = htonl(INADDR_ANY);//addr.sin_addr; bind(data_socket, (SOCKADDR *)&recv_data_addr, sizeof(recv_data_addr)); file_size = partition_id = 0; timing = GetTickCount(); printf("Downloading %s", filename); memset(control_message, 0, sizeof(MAX_INPUT_LENGTH)); control_message[0] = CONTROL_MESSAGE_OK_START_SENDING; return_code = send(soc, control_message, (int)strlen(control_message), 0); if(return_code == SOCKET_ERROR) { printf("failed to send command: %d\n", WSAGetLastError()); return 0; } while(1) { for(tries = 0; tries < CONTROL_MESSAGE_RECV_RETRIES; tries++) { memset(control_message, 0, sizeof(MAX_INPUT_LENGTH)); return_code = recv(soc, control_message, MAX_INPUT_LENGTH-1, 0); if(return_code < 1) { printf("failed to recv command: %d\n", WSAGetLastError()); closesocket(data_socket); return 0; } if(control_message[0] == CONTROL_MESSAGE_SENDING_DATA) { partition.actual_size = atol(&control_message[2]); break; } if(control_message[0] == CONTROL_MESSAGE_ALL_DATA_SENT) break; if(control_message[0] == CONTROL_MESSAGE_NO_SUCH_FILE) { printf("\rRemote file does not exist!\n\n"); return 1; } } if(control_message[0] == CONTROL_MESSAGE_ALL_DATA_SENT) break; if(tries >= CONTROL_MESSAGE_RECV_RETRIES) { printf("\nNo CONTROL_MESSAGE_SENDING_DATA from server\n"); closesocket(data_socket); return 0; } memset(partition.packet_stats, 0, MAX_PARTITION_DIVISIONS+1); memset(partition.data, 0, sizeof(PARTITION_LENGTH_TOTAL)); packet_count = (unsigned char)ceil(partition.actual_size / PACKET_LENGTH_DATA); while(1) { for(packets_recv = 0; packets_recv < packet_count; packets_recv++) { if(partition.packet_stats[packets_recv] != 1) break; } if(packets_recv == packet_count) break; for(packets_recv = 0; packets_recv < packet_count; packets_recv++) { return_code = recvfrom(data_socket, (char *)&packet, sizeof(packet), 0, NULL, NULL); if(return_code < 1) { printf("\nfailed to recv command: %d\n", WSAGetLastError()); closesocket(data_socket); return 0; } if(packet.partition_id == partition_id) { if(compute_crc((const unsigned char *)packet.data, PACKET_LENGTH_DATA) == packet.crc) { partition.packet_stats[packet.packet_id] = 1; at_location = packet.packet_id * PACKET_LENGTH_DATA; write_amount = at_location + PACKET_LENGTH_DATA < partition.actual_size ? PACKET_LENGTH_DATA : partition.actual_size - at_location; memcpy(&partition.data[at_location], packet.data, write_amount); } } } for(tries = 0; tries < CONTROL_MESSAGE_RECV_RETRIES; tries++) { memset(control_message, 0, sizeof(MAX_INPUT_LENGTH)); return_code = recv(soc, control_message, MAX_INPUT_LENGTH-1, 0); if(return_code < 1) { printf("\nfailed to recv command: %d\n", WSAGetLastError()); closesocket(data_socket); return 0; } if(control_message[0] == CONTROL_MESSAGE_PARTITION_SENT) break; } if(tries >= CONTROL_MESSAGE_RECV_RETRIES) { printf("\nNo CONTROL_MESSAGE_PARTITION_SENT from server\n"); closesocket(data_socket); return 0; } memset(control_message, 0, sizeof(MAX_INPUT_LENGTH)); sprintf_s(control_message, MAX_INPUT_LENGTH, " %s", partition.packet_stats); control_message[0] = CONTROL_MESSAGE_PARTITION_STATUS; return_code = send(soc, control_message, (int)strlen(control_message)+1, 0); if(return_code == SOCKET_ERROR) { printf("\nfailed to send command: %d\n", WSAGetLastError()); closesocket(data_socket); return 0; } } fwrite(partition.data, partition.actual_size, 1, fp); partition_id++; file_size += partition.actual_size; printf("."); } fclose(fp); closesocket(data_socket); printf("Done!\n"); transfer_sec = (float)(GetTickCount() - timing) / 1000.0; transfer_kb = (float)file_size / 1000.0; transfer_mbps = (file_size / 1000.0) / transfer_sec; printf("Transfer of %0.f KB took %0.1f seconds (%0.3f MBps)!\n\n", transfer_kb, transfer_sec, transfer_mbps); return 1; }
static void parse_bank(ulong bank) { int i; ulong flstart, flend; flash_info_t *info; info = &flash_info[bank]; if (info->flash_id != FLASH_MAN_CFI) { printf("Bank %lu: missing or unknown FLASH type\n", bank); return; } if (!info->sector_count) { printf("Bank %lu: no FLASH sectors\n", bank); return; } flstart = info->start[0]; flend = flstart + info->size; for (i = 0; i < info->sector_count; ++i) { ulong secend; u32 foot1, foot2; if (ctrlc()) break; if (i == info->sector_count-1) secend = flend; else secend = info->start[i+1]; /* Check for v1 header */ foot1 = readl((void *)secend - 0x0c); if (foot1 == 0xA0FFFF9FU) { struct afs_image *afi = &afs_images[num_afs_images]; ulong imginfo; afi->flinfo = info; afi->version = 1; afi->flash_mem_start = readl((void *)secend - 0x10); afi->flash_mem_end = readl((void *)secend - 0x14); afi->attributes = readl((void *)secend - 0x08); /* Adjust to even address */ imginfo = afi->flash_mem_end + afi->flash_mem_end % 4; /* Record as a single region */ afi->region_count = 1; afi->regions[0].offset = readl((void *)imginfo + 0x04); afi->regions[0].load_address = readl((void *)imginfo + 0x08); afi->regions[0].size = readl((void *)imginfo + 0x0C); afi->entrypoint = readl((void *)imginfo + 0x10); afi->name = (const char *)imginfo + 0x14; num_afs_images++; } /* Check for v2 header */ foot1 = readl((void *)secend - 0x04); foot2 = readl((void *)secend - 0x08); /* This makes up the string "HSLFTOOF" flash footer */ if (foot1 == 0x464F4F54U && foot2 == 0x464C5348U) { struct afs_image *afi = &afs_images[num_afs_images]; ulong imginfo; u32 block_start, block_end; int j; afi->flinfo = info; afi->version = readl((void *)secend - 0x0c); imginfo = secend - 0x30 - readl((void *)secend - 0x10); afi->name = (const char *)secend - 0x30; afi->entrypoint = readl((void *)imginfo+0x08); afi->attributes = readl((void *)imginfo+0x0c); afi->region_count = readl((void *)imginfo+0x10); block_start = readl((void *)imginfo+0x54); block_end = readl((void *)imginfo+0x58); afi->flash_mem_start = afi->flinfo->start[block_start]; afi->flash_mem_end = afi->flinfo->start[block_end]; /* * Check footer CRC, the algorithm saves the inverse * checksum as part of the summed words, and thus * the result should be zero. */ if (compute_crc(imginfo + 8, 0x88) != 0) { printf("BAD CRC on ARM image info\n"); printf("(continuing anyway)\n"); } /* Parse regions */ for (j = 0; j < afi->region_count; j++) { afi->regions[j].load_address = readl((void *)imginfo+0x14 + j*0x10); afi->regions[j].size = readl((void *)imginfo+0x18 + j*0x10); afi->regions[j].offset = readl((void *)imginfo+0x1c + j*0x10); /* * At offset 0x20 + j*0x10 there is a region * checksum which seems to be the running * sum + 3, however since we anyway checksum * the entire footer this is skipped over for * checking here. */ } num_afs_images++; } } }
bool Microslice::check_crc() const { return compute_crc() == desc_ptr_->crc; }
/** * \brief Application entry point for CRCCU example. * * \return Unused (ANSI-C compatibility). */ int main(void) { uint32_t ul_counter; uint32_t ul_crc; /* Initialize the system */ sysclk_init(); board_init(); /* Configure the console uart */ configure_console(); /* Output example information */ puts(STRING_HEADER); /* Enable CRCCU peripheral clock */ pmc_enable_periph_clk(ID_CRCCU); /* Initialize flash: 6 wait states for flash writing. */ flash_init(FLASH_ACCESS_MODE_128, FLASH_WAIT_STATE_NBR); /* Unlock flash page. */ flash_unlock(FLASH_BUFFER_ADDRESS, FLASH_BUFFER_ADDRESS + IFLASH_PAGE_SIZE - 1, NULL, NULL); /* Fill data buffer in SRAM (The data may be random data) */ for (ul_counter = 0; ul_counter < BUFFER_LENGTH; ul_counter++) { g_uc_data_buf[ul_counter] = ul_counter; } #if SAM4S || SAMG55 /* Fill data buffer in Flash, the data is same as in SRAM */ #if SAM4S flash_erase_page(FLASH_BUFFER_ADDRESS, IFLASH_ERASE_PAGES_8); #else flash_erase_page(FLASH_BUFFER_ADDRESS, IFLASH_ERASE_PAGES_16); #endif flash_write(FLASH_BUFFER_ADDRESS, (void *)g_uc_data_buf, BUFFER_LENGTH, 0); #else flash_write(FLASH_BUFFER_ADDRESS, (void *)g_uc_data_buf, BUFFER_LENGTH, 1); #endif /* Test CRC with CCITT-16 polynomial */ puts("\n\r====Test CRC with CCITT 16 (0x1021) ====\r"); /* Compute CRC in SRAM */ puts("Test CRC in SRAM buffer\r"); ul_crc = compute_crc(g_uc_data_buf, BUFFER_LENGTH, CRCCU_MR_PTYPE_CCITT16); /* Compute CRC in Flash and compare it with the result in SRAM */ puts("Test CRC in Flash buffer\r"); compute_crc_and_compare((uint8_t *) FLASH_BUFFER_ADDRESS, BUFFER_LENGTH, CRCCU_MR_PTYPE_CCITT16, ul_crc); /* Test CRC with CASTAGNOLI polynomial */ puts("\n\r====Test CRC with CASTAGNOLI (0x1EDC6F41) ====\r"); /* Compute CRC in SRAM */ puts("Test CRC in SRAM buffer\r"); ul_crc = compute_crc(g_uc_data_buf, BUFFER_LENGTH, CRCCU_MR_PTYPE_CASTAGNOLI); /* Compute CRC in Flash and compare it with the result in SRAM */ puts("Test CRC in Flash buffer\r"); compute_crc_and_compare((uint8_t *) FLASH_BUFFER_ADDRESS, BUFFER_LENGTH, CRCCU_MR_PTYPE_CASTAGNOLI, ul_crc); /* Test CRC with CCITT 802.3 polynomial */ puts("\n\r====Test CRC with CCITT 802.3 (0x04C11DB7) ====\r"); /* Compute CRC in SRAM */ puts("Test CRC in SRAM buffer\r"); ul_crc = compute_crc(g_uc_data_buf, BUFFER_LENGTH, CRCCU_MR_PTYPE_CCITT8023); /* Compute CRC in Flash and compare it with the result in SRAM */ puts("Test CRC in Flash buffer\r"); compute_crc_and_compare((uint8_t *) FLASH_BUFFER_ADDRESS, BUFFER_LENGTH, CRCCU_MR_PTYPE_CCITT8023, ul_crc); while (1) { } }
int send_file(HTHREAD_PTR descr, char *filename) { WSADATA wsa_data; SOCKET data_socket = INVALID_SOCKET; struct sockaddr_in send_data_addr; HPACKET packet; HPARTITION partition; FILE *fp; int return_code; unsigned long at_location, read_amount, tries; unsigned char packet_count; char control_message[MAX_INPUT_LENGTH]; if(fopen_s(&fp, filename, "rb") > 0) { memset(control_message, 0, sizeof(MAX_INPUT_LENGTH)); control_message[0] = CONTROL_MESSAGE_NO_SUCH_FILE; return_code = send(descr->socket, control_message, (int)strlen(control_message), 0); return 1; } for(tries = 0; tries < CONTROL_MESSAGE_RECV_RETRIES; tries++) { memset(control_message, 0, sizeof(MAX_INPUT_LENGTH)); return_code = recv(descr->socket, control_message, MAX_INPUT_LENGTH-1, 0); if(return_code < 1) { printf("failed to recv command: %d\n", WSAGetLastError()); closesocket(data_socket); return 0; } if(control_message[0] == CONTROL_MESSAGE_OK_START_SENDING) break; } if(tries >= CONTROL_MESSAGE_RECV_RETRIES) { printf("No CONTROL_MESSAGE_OK_START_SENDING from %s\n", inet_ntoa(descr->address.sin_addr)); closesocket(data_socket); return 0; } WSAStartup(MAKEWORD(2,2), &wsa_data); data_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); send_data_addr.sin_family = AF_INET; send_data_addr.sin_port = htons(CONNECT_PORT_N);//descr->address.sin_port; send_data_addr.sin_addr = descr->address.sin_addr;//inet_addr("123.456.789.1"); packet.partition_id = 0; packet.reserved = 0; while(!feof(fp)) { memset(partition.packet_stats, 0, MAX_PARTITION_DIVISIONS+1); partition.actual_size = (unsigned long)fread(partition.data, 1, PARTITION_LENGTH_TOTAL, fp); packet_count = (unsigned char)ceil(partition.actual_size / PACKET_LENGTH_DATA); memset(control_message, 0, sizeof(MAX_INPUT_LENGTH)); sprintf_s(control_message, MAX_INPUT_LENGTH, " %u", partition.actual_size); control_message[0] = CONTROL_MESSAGE_SENDING_DATA; return_code = send(descr->socket, control_message, (int)strlen(control_message)+1, 0); if(return_code == SOCKET_ERROR) { printf("failed to send command: %d\n", WSAGetLastError()); closesocket(data_socket); return 0; } while(1) { for(packet.packet_id = 0; packet.packet_id < packet_count; packet.packet_id++) { if(partition.packet_stats[packet.packet_id] != 1) break; } if(packet.packet_id == packet_count) break; for(packet.packet_id = 0; packet.packet_id < packet_count; packet.packet_id++) { if(partition.packet_stats[packet.packet_id] != 1) { memset(packet.data, 0, sizeof(PARTITION_LENGTH_TOTAL)); at_location = packet.packet_id * PACKET_LENGTH_DATA; read_amount = at_location + PACKET_LENGTH_DATA < partition.actual_size ? PACKET_LENGTH_DATA : partition.actual_size - at_location; memcpy(packet.data, &partition.data[at_location], read_amount); packet.crc = compute_crc((const unsigned char *)packet.data, PACKET_LENGTH_DATA); return_code = sendto(data_socket, (char *)&packet, sizeof(packet), 0, (SOCKADDR *)&send_data_addr, sizeof(send_data_addr)); if(return_code == SOCKET_ERROR) { printf("failed to send command: %d\n", WSAGetLastError()); closesocket(data_socket); return 0; } Sleep(5); } } memset(control_message, 0, sizeof(MAX_INPUT_LENGTH)); control_message[0] = CONTROL_MESSAGE_PARTITION_SENT; return_code = send(descr->socket, control_message, (int)strlen(control_message)+1, 0); if(return_code == SOCKET_ERROR) { printf("failed to send command: %d\n", WSAGetLastError()); closesocket(data_socket); return 0; } for(tries = 0; tries < CONTROL_MESSAGE_RECV_RETRIES; tries++) { memset(control_message, 0, sizeof(MAX_INPUT_LENGTH)); return_code = recv(descr->socket, control_message, MAX_INPUT_LENGTH-1, 0); if(return_code < 1) { printf("failed to recv command: %d\n", WSAGetLastError()); closesocket(data_socket); return 0; } if(control_message[0] == CONTROL_MESSAGE_PARTITION_STATUS) { memset(partition.packet_stats, 0, MAX_PARTITION_DIVISIONS+1); memcpy(partition.packet_stats, &control_message[2], MAX_PARTITION_DIVISIONS); break; } } if(tries >= CONTROL_MESSAGE_RECV_RETRIES) { printf("No CONTROL_MESSAGE_PARTITION_STATUS from %s\n", inet_ntoa(descr->address.sin_addr)); closesocket(data_socket); return 0; } } packet.partition_id++; } fclose(fp); memset(control_message, 0, sizeof(MAX_INPUT_LENGTH)); control_message[0] = CONTROL_MESSAGE_ALL_DATA_SENT; return_code = send(descr->socket, control_message, (int)strlen(control_message)+1, 0); if(return_code == SOCKET_ERROR) { printf("failed to send command: %d\n", WSAGetLastError()); closesocket(data_socket); return 1; } closesocket(data_socket); return 1; }