int sf_closeFile(SaveFile* file){ sprintf(lastErrorDetected, ""); if( file->saving ){ DECL_STAMP(stamp); md5_finish(&file->state, stamp); #ifdef DEBUG_PRINT printmd5("", stamp); printf("Bytes written(without stamp): %i\n", file->bytesWritten ); printf("Bytes written(with stamp): %i\n", file->bytesWritten + STAMP_SIZE); #endif fwrite(stamp, STAMP_SIZE, 1, file->file); } if(! file ){ sprintf(lastErrorDetected, "file input is null"); return 0; } if( file->file ){ fclose( file->file ); file->file = 0; } if( file->key ){ otp_destroyKey(&file->key); file->key = 0; } free(file); return 1; }
main () { int HASH = R_HASH_MD5; RHash *h = r_hash_new (1, HASH); r_hash_do_begin (h, HASH); r_hash_do_md5 (h, "hello", 5); printmd5("hello", h); r_hash_do_md5 (h, "world", 5); printmd5("world", h); r_hash_do_end (h, HASH); printmd5("FINISH", h); r_hash_do_md5 (h, "helloworld", 10); printmd5("helloworld", h); }
void DataTerminal::processByte(uint8_t byte) { switch(mState) { case STATE_WAITING: if ( byte == START_TRANSFER_CMD ) { mState = STATE_IN_TRANSFER_HEADER; mByteCount = 0; trace_printf("Receiving metadata\n"); GPIO_SetBits(GPIOB, GPIO_Pin_14); writeCmd(ACK); } else { trace_printf("Bad command: %.2x\n", byte); fail(); } break; case STATE_IN_TRANSFER_HEADER: { //trace_printf("1 byte\n"); char *p = (char*)&mMetadata; memcpy(p + mByteCount, &byte, 1); ++mByteCount; if ( mByteCount == sizeof mMetadata ) { trace_printf("Magick: %.8x\n", mMetadata.magic); // Is the metadata sane? if ( mMetadata.magic == METADATA_MAGIC ) { mState = STATE_IN_TRANSFER_BLOCK; trace_printf("Expecting %d bytes\n", mMetadata.size); printmd5(mMetadata.md5); GPIO_SetBits(GPIOB, GPIO_Pin_14); mByteCount = 0; mWriteAddress = APPLICATION_ADDRESS; MD5Init(&mMD5); unlockFlash(); writeCmd(ACK); } else { trace_printf("Bad metadata \n"); fail(); } } } break; case STATE_IN_TRANSFER_BLOCK: MD5Update(&mMD5, &byte, 1); if ( mByteCount == 0 ) { GPIO_SetBits(GPIOB, GPIO_Pin_15); } size_t offset = mByteCount % FLASH_PAGE_SIZE; assert(offset < sizeof mCurrPage); memcpy(mCurrPage + offset, &byte, 1); ++mByteCount; if ( mByteCount % FLASH_PAGE_SIZE == 0 ) { flushPage(); } // Acknowledge every 1K if ( mByteCount % 1024 == 0 ) { trace_printf("Sending ACK\n"); writeCmd(ACK); } if ( mByteCount == mMetadata.size ) { if ( mMetadata.size % 1024 ) { // We have a partial page to write flushPage(); writeCmd(ACK); } // Last byte! trace_printf("Received %d bytes\n", mMetadata.size); GPIO_ResetBits(GPIOB, GPIO_Pin_14); mState = STATE_WAITING; uint8_t d[16]; MD5Final(d, &mMD5); printmd5(d); if ( memcmp(d, mMetadata.md5, 16) ) { // Bad MD5 trace_printf("MD5 mismatch :( \n"); fail(); } else { // Good transfer trace_printf("MD5 match :) \n"); writeCmd(ACK); flushMetadata(); lockFlash(); NVIC_SystemReset(); } } break; } }