void RichRuler::RightDown(Point p, dword) { if(p.x < x0 - 3) { newtabalign--; if(newtabalign < ALIGN_LEFT) newtabalign = ALIGN_CENTER; Refresh(); return; } track = FindMarker(p); if(track < 0) pos = ((p.x - x0) / zoom + snap / 2) / snap * snap; WhenRightDown(); }
int ReadHex(FILE *infile, BitSequence *A, int Length, char *str) { int i, ch, started; BitSequence ich; if ( Length == 0 ) { A[0] = 0x00; return 1; } memset(A, 0x00, Length); started = 0; i = 0; if ( FindMarker(infile, str) ) while ( (ch = fgetc(infile)) != EOF ) { if ( !isxdigit(ch) ) { if ( !started ) { if ( ch == '\n' ) break; else continue; } else break; } started = 1; if ( (ch >= '0') && (ch <= '9') ) ich = ch - '0'; else if ( (ch >= 'A') && (ch <= 'F') ) ich = ch - 'A' + 10; else if ( (ch >= 'a') && (ch <= 'f') ) ich = ch - 'a' + 10; A[i / 2] = (A[i / 2] << 4) | ich; if ( (++i / 2) == Length ) break; } else return 0; return 1; }
void RichRuler::LeftDown(Point p, dword) { track = FindMarker(p); if(track >= 0) { trackdx = marker[track].pos * zoom + x0 - p.x; SetCapture(); WhenBeginTrack(); } else if(p.x < 16) { newtabalign++; if(newtabalign > ALIGN_CENTER) newtabalign = ALIGN_LEFT; Refresh(); return; } else { pos = ((p.x - x0) / zoom + snap / 2) / snap * snap; WhenLeftDown(); } }
int main( void ) { unsigned long long inlen; int result = 0; FILE *fp_in; char marker[20]; int refLen; #ifdef cKeccakFixedOutputLengthInBytes refLen = cKeccakFixedOutputLengthInBytes; #else refLen = cKeccakR_SizeInBytes; #endif printf( "Testing Keccak[r=%u, c=%u] against %s over %d squeezed bytes\n", cKeccakR, cKeccakB - cKeccakR, testVectorFile, refLen ); if ( (fp_in = fopen(testVectorFile, "r")) == NULL ) { printf("Couldn't open <%s> for read\n", testVectorFile); return 1; } for ( inlen = 0; inlen <= cKeccakMaxMessageSizeInBytes; ++inlen ) { sprintf( marker, "Len = %u", inlen * 8 ); if ( !FindMarker(fp_in, marker) ) { printf("ERROR: no test vector found (%u bytes)\n", inlen ); result = 1; break; } if ( !ReadHex(fp_in, input, (int)inlen, "Msg = ") ) { printf("ERROR: unable to read 'Msg' (%u bytes)\n", inlen ); result = 1; break; } result = crypto_hash( output, input, inlen ); if ( result != 0 ) { printf("ERROR: crypto_hash() (%u bytes)\n", inlen); result = 1; break; } #ifdef cKeccakFixedOutputLengthInBytes if ( !ReadHex(fp_in, input, refLen, "MD = ") ) #else if ( !ReadHex(fp_in, input, refLen, "Squeezed = ") ) #endif { printf("ERROR: unable to read 'Squeezed/MD' (%u bytes)\n", inlen ); result = 1; break; } if ( memcmp( output, input, refLen ) != 0) { printf("ERROR: hash verification (%u bytes)\n", inlen ); for(result=0; result<refLen; result++) printf("%02X ", output[result]); printf("\n"); result = 1; break; } } fclose( fp_in ); if ( !result ) printf( "\nSuccess!\n"); //printf( "\nPress a key ..."); //getchar(); //printf( "\n"); return ( result ); }
size_t BazisLib::Network::BufferedSocketBase::RecvToMarker( void *pBuffer, size_t length, const void *pMarker, size_t markerSize ) { if (!markerSize) { m_LastRecvError = kInvalidArgument; return 0; } //1. Try searching for the marker in the internal buffer //2. If failed, copy data to external buffer and try receiving more until the buffer is full //3. After every iteration try finding the marker //4. If failed, return 0 // If found, put what's left to the internal buffer size_t availableInternally = m_Buffer.GetSize() - m_BufferOffset; size_t off = FindMarker(m_Buffer.GetData(m_BufferOffset), min(availableInternally, length), pMarker, markerSize); if (off != -1) { ASSERT((off + markerSize) <= length); memcpy(pBuffer, m_Buffer.GetData(m_BufferOffset), off + markerSize); m_BufferOffset += (off + markerSize); m_LastRecvError = kSuccess; return off + markerSize; } //The marker was not found in the buffer. Try to fetch more if (availableInternally >= length) { m_LastRecvError = kBufferFull; return 0; } memcpy(pBuffer, m_Buffer.GetData(m_BufferOffset), availableInternally); //At this point the internal buffer has been completely copied to the user buffer. We will copy it back if we go to the fallback routine. size_t bufOff = availableInternally, prevOff = 0; while (bufOff < length) { size_t todo = length - bufOff; size_t done = RawRecv(((char *)pBuffer) + bufOff, todo); if (done == 0) { m_LastRecvError = kSocketClosed; return 0; } else if (done == -1) { m_LastRecvError = kSocketClosed; return 0; } off = FindMarker(((char *)pBuffer) + prevOff, bufOff + done - prevOff, pMarker, markerSize); if (off != -1) { off += prevOff; //Adjust 'off' so that it contains the offset from the beginning of buffer off += markerSize; //Off now points to the end of the data we are returning size_t writeBackSize = bufOff + done - off; if (!m_Buffer.EnsureSize(writeBackSize)) { m_LastRecvError = kOutOfMemoryAndLostData; return 0; } m_BufferOffset = 0; memcpy(m_Buffer.GetData(), ((char *)pBuffer) + off, writeBackSize); m_Buffer.SetSize(writeBackSize); return off; } bufOff += done; if (bufOff > markerSize) prevOff = bufOff - markerSize + 1; else prevOff = 0; } //Fallback case. We cannot go further as the user-supplied buffer has been filled completely. //This should happen relatively rarely, so copying the data back to our internal buffer //should not be a performance problem here. //The data from the internal buffer has been copied to the user buffer, as we assumed that we //are going to return it. Now we are copying it back. ASSERT(bufOff == length); //An optimization is possible here: if the original m_BufferOffset was 0, we do not need to //copy back the first part of the buffer. However, it's probably impractical to implement. if (!m_Buffer.EnsureSize(bufOff)) { m_LastRecvError = kOutOfMemoryAndLostData; return 0; } m_BufferOffset = 0; memcpy(m_Buffer.GetData(), pBuffer, bufOff); m_Buffer.SetSize(bufOff); m_LastRecvError = kBufferFull; return 0; }
int main( void ) { unsigned long long inlen; unsigned long long offset; unsigned long long size; int result = 0; FILE *fp_in; char marker[20]; int refLen; hashState state; #ifdef cKeccakFixedOutputLengthInBytes refLen = cKeccakFixedOutputLengthInBytes; #else refLen = cKeccakR_SizeInBytes; #endif printf( "Testing Keccak[r=%u, c=%u] using crypto_hash() against %s over %d squeezed bytes\n", cKeccakR, cKeccakB - cKeccakR, testVectorFile, refLen ); if ( (fp_in = fopen(testVectorFile, "r")) == NULL ) { printf("Couldn't open <%s> for read\n", testVectorFile); return 1; } for ( inlen = 0; inlen <= cKeccakMaxMessageSizeInBytes; ++inlen ) { sprintf( marker, "Len = %u", inlen * 8 ); if ( !FindMarker(fp_in, marker) ) { printf("ERROR: no test vector found (%u bytes)\n", inlen ); result = 1; break; } if ( !ReadHex(fp_in, input, (int)inlen, "Msg = ") ) { printf("ERROR: unable to read 'Msg' (%u bytes)\n", inlen ); result = 1; break; } result = crypto_hash( output, input, inlen ); if ( result != 0 ) { printf("ERROR: crypto_hash() (%u bytes)\n", inlen); result = 1; break; } #ifdef cKeccakFixedOutputLengthInBytes if ( !ReadHex(fp_in, input, refLen, "MD = ") ) #else if ( !ReadHex(fp_in, input, refLen, "Squeezed = ") ) #endif { printf("ERROR: unable to read 'Squeezed/MD' (%u bytes)\n", inlen ); result = 1; break; } if ( memcmp( output, input, refLen ) != 0) { printf("ERROR: hash verification (%u bytes)\n", inlen ); for(result=0; result<refLen; result++) printf("%02X ", output[result]); printf("\n"); result = 1; break; } } if ( !result ) printf( "\nSuccess!\n"); result = 0; refLen = cKeccakHashRefSizeInBytes; printf( "\nTesting Keccak[r=%u, c=%u] using Init/Update/Final() against %s over %d squeezed bytes\n", cKeccakR, cKeccakB - cKeccakR, testVectorFile, refLen ); fseek( fp_in, 0, SEEK_SET ); for ( inlen = 0; inlen <= cKeccakMaxMessageSizeInBits; ++inlen ) { sprintf( marker, "Len = %u", inlen ); if ( !FindMarker(fp_in, marker) ) { printf("ERROR: no test vector found (%u bits)\n", inlen ); result = 1; break; } if ( !ReadHex(fp_in, input, (int)inlen, "Msg = ") ) { printf("ERROR: unable to read 'Msg' (%u bits)\n", inlen ); result = 1; break; } result = Init( &state ); if ( result != 0 ) { printf("ERROR: Init() (%u bits)\n", inlen); result = 1; break; } for ( offset = 0; offset < inlen; offset += size ) { // vary sizes for Update() if ( (inlen %8) < 2 ) { // byte per byte size = 8; } else if ( (inlen %8) < 4 ) { // incremental size = offset + 8; } else { // random size = ((rand() % ((inlen + 8) / 8)) + 1) * 8; } if ( size > (inlen - offset) ) { size = inlen - offset; } //printf("Update() inlen %u, size %u, offset %u\n", (unsigned int)inlen, (unsigned int)size, (unsigned int)offset ); result = Update( &state, input + offset / 8, size ); if ( result != 0 ) { printf("ERROR: Update() (%u bits)\n", inlen); result = 1; break; } } result = Final( &state, output, refLen ); if ( result != 0 ) { printf("ERROR: Final() (%u bits)\n", inlen); result = 1; break; } #ifdef cKeccakFixedOutputLengthInBytes if ( !ReadHex(fp_in, input, refLen, "MD = ") ) #else if ( !ReadHex(fp_in, input, refLen, "Squeezed = ") ) #endif { printf("ERROR: unable to read 'Squeezed/MD' (%u bits)\n", inlen ); result = 1; break; } if ( memcmp( output, input, refLen ) != 0) { printf("ERROR: hash verification (%u bits)\n", inlen ); for(result=0; result<refLen; result++) printf("%02X ", output[result]); printf("\n"); result = 1; break; } } fclose( fp_in ); if ( !result ) printf( "\nSuccess!\n"); //printf( "\nPress a key ..."); //getchar(); //printf( "\n"); return ( result ); }
/**************************************************************************//*! * * @name HandleSerialClassReady * * @brief This function handles Serial Event when Agent is in READY * State * * @param controller_ID : Serial Controller ID * @param event_type : Serial Bus Event ID * @param psBuffer : Pointer to Buffer Structure * * @return None ****************************************************************************** * This function handles Serial Event when Agent is in READY State *****************************************************************************/ static void HandleSerialClassReady( uint_8 controller_ID, /* [IN] Controller ID */ uint_8 event_type, /* [IN] Serial Bus Event ID */ PBUFFER psBuffer /* [IN] Pointer to Buffer Structure */ ) { static PBUFFER psBuffer_tmp; psBuffer_tmp = psBuffer; switch(event_type) { case SCI_TRANSMIT_COMPLETE: { break; } case SCI_RECEIVE_COMPLETE: { if(g_read_enable == FALSE) { /* Disable Packet Decoding until there is a Read call from app */ PrepareRecvPcktHeader(PACKET_HEADER_SIZE); break; } switch(psBuffer->pBuffer[0]) { case MASTER_SEND_HEADER: { /* Master send header received */ PSERIAL_COMM pRxComm = &g_Serial.RxComm; uint_16 recv_pkt_size = *(uint_16_ptr)(&psBuffer->pBuffer[1]); /* Change State to Wait Receive */ g_Serial.SerialFsm = SERIAL_WAIT_RECEIVE; /* We Prepare to receive data here */ pRxComm->Buff.pBuffer = (g_recv_buffer.pBuffer == NULL) ? DummyRecvBuffer : g_recv_buffer.pBuffer; pRxComm->Buff.Length = recv_pkt_size; pRxComm->Buff.CurOffSet = 0; /* Setup Data receive */ (void)SCI_Read(controller_ID, &pRxComm->Buff); /* Send wait send response */ SendPktHeader(MASTER_SEND_ACK, 0); break; } case MASTER_RECV_HEADER: { /* Master receive header received */ uint_8 producer = g_send_queue.producer; uint_8 consumer = g_send_queue.consumer; uint_8 queue; PTR_SERIAL_CLASS_BUFFER pserial_class_buffer; /* Queue not full */ if(producer != consumer) { uint_16 send_pkt_size; queue = (uint_8) (g_send_queue.consumer % MAX_XMIT_QUEUE_ELEMENTS); pserial_class_buffer = &g_send_queue.class_buffer[queue]; send_pkt_size = (uint_16)(pserial_class_buffer->transfer_size - pserial_class_buffer->cur_offset); /* Change State to Transmit */ g_Serial.SerialFsm = SERIAL_WAIT_TRANSMIT; /* Send wait recv response */ SendPktHeader(MASTER_RECV_ACK, MIN(send_pkt_size, g_Serial.MaxPktSize)); } else { /* Prepare to receive next packet header */ PrepareRecvPcktHeader(PACKET_HEADER_SIZE); } break; } default: { /* We have lost sync with Manager here */ /* Find our Marker */ if(psBuffer->CurOffSet < PACKET_HEADER_SIZE) { /* Prepare to receive header */ PrepareRecvPcktHeader(PACKET_HEADER_SIZE); } else { /* Find the error position and correct it */ uint_16 pos = FindMarker(psBuffer->pBuffer, psBuffer->CurOffSet); PrepareRecvPcktHeader(pos); } break; } } break; } case SCI_RECEIVE_OVERRUN: case SCI_RECEIVE_NOISE_DETECTED: case SCI_RECEIVE_FRAME_ERROR: case SCI_RECEIVE_PARITY_ERROR: { } } }