Exemplo n.º 1
0
//* This function changes hex char[4] to uint16_t.
void HexToByte_4(char *hexstring_4, uint16_t *byte)
{
char tempp[2];
uint8_t right_dec = 0;
uint8_t left_dec = 0;
uint16_t right_dec16 = 0;
uint16_t left_dec16 = 0;

tempp[0] = hexstring_4[0];
tempp[1] = hexstring_4[1];
HexToByte(tempp,&left_dec);

tempp[0] = hexstring_4[2];
tempp[1] = hexstring_4[3];
HexToByte(tempp,&right_dec);

left_dec16 = left_dec16 | left_dec;
right_dec16 = right_dec16 | right_dec;

*byte = (left_dec16 << 8) | right_dec16;
}
Exemplo n.º 2
0
char* Decrypt(const char* szSource, const char* szPassWord) // 解密,返回解密结果
{
    if(szSource == NULL || (strlen(szSource)%2 != 0) || szPassWord == NULL)
        return NULL;
    unsigned char* src = HexToByte(szSource);
    unsigned char* ret = new unsigned char[strlen(szSource) / 2 + 1];
    int ret_len = 0;
    memset(ret, (int)strlen(szSource) / 2 + 1,0);
    if(RC4(src, (int)strlen(szSource) / 2, (unsigned char*)szPassWord, (int)strlen(szPassWord), ret, &ret_len) != 0)
        return NULL;
    ret[ret_len] = '\0';
    return (char*)ret;
}
Exemplo n.º 3
0
static bool ParseSha1(const CXmlItem &item, const char *name, Byte *digest)
{
  int index = item.FindSubTag(name);
  if (index  < 0)
    return false;
  const CXmlItem &checkItem = item.SubItems[index];
  AString style = checkItem.GetPropertyValue("style");
  if (style == "SHA1")
  {
    AString s = checkItem.GetSubString();
    if (s.Length() != 40)
      return false;
    for (int i = 0; i < s.Length(); i += 2)
    {
      Byte b0, b1;
      if (!HexToByte(s[i], b0) || !HexToByte(s[i + 1], b1))
        return false;
      digest[i / 2] = (b0 << 4) | b1;
    }
    return true;
  }
  return false;
}
Exemplo n.º 4
0
uint8_t GetLRC(char *frame)
{
uint8_t LRCsum = 0;
char temp[2];
uint8_t TempSum;
frame++;

while(*frame)
{
temp[0] = *frame++;
temp[1] = *frame++;
HexToByte(temp, &TempSum);
LRCsum += TempSum;
}
LRCsum = (~(LRCsum)+1);
return LRCsum;
}
Exemplo n.º 5
0
//Illegal value in querry frame
void ErrorMessage(uint8_t ErrorCode)
{
char OutputFrame[OFS];   // output frame
char temp[2];
uint8_t FunC;
uint8_t ErrFuncCode;
uint8_t k;

//Clears table
for(k = 0; k<OFS; k++){OutputFrame[k] = '\0';}

//Rewrite first chars
RewritingChars(OutputFrame,0,2);

//Read Function Code from message
temp[0] = word[3];
temp[1] = word[4];
HexToByte(temp, &FunC);

//Calculate Function Code for Message with Error
ErrFuncCode = FunC + 80;
ByteToHex(temp,ErrFuncCode);
OutputFrame[3] = temp[0];
OutputFrame[4] = temp[1];

//Fill Data field with ILLEGAL FUNCTION CODE
ByteToHex(temp,ErrorCode);
OutputFrame[5] = temp[0];
OutputFrame[6] = temp[1];

//* Writes LRC.
ByteToHex(temp,GetLRC(OutputFrame));
OutputFrame[7] = temp[0];
OutputFrame[8] = temp[1];
OutputFrame[9] = 0x0D;
OutputFrame[10] = 0x0A;
OutputFrame[11] = 0x0A;

//sending frame 
UART_SendStr(OutputFrame); 
}
Exemplo n.º 6
0
UnicodeString TCopyParamType::RestoreChars(const UnicodeString & AFileName) const
{
  UnicodeString FileName = AFileName;
  if (GetInvalidCharsReplacement() == TokenReplacement)
  {
    wchar_t * InvalidChar = const_cast<wchar_t *>(FileName.c_str());
    while ((InvalidChar = wcschr(InvalidChar, TokenPrefix)) != nullptr)
    {
      intptr_t Index = InvalidChar - FileName.c_str() + 1;
      if (FileName.Length() >= Index + 2)
      {
        UnicodeString Hex = FileName.SubString(Index + 1, 2);
        wchar_t Char = static_cast<wchar_t>(HexToByte(Hex));
        if ((Char != L'\0') &&
            ((FTokenizibleChars.Pos(Char) > 0) ||
             (((Char == L' ') || (Char == L'.')) && (Index == FileName.Length() - 2))))
        {
          FileName[Index] = Char;
          FileName.Delete(Index + 1, 2);
          InvalidChar = const_cast<wchar_t *>(FileName.c_str() + Index);
        }
        else if ((Hex == L"00") &&
                 ((Index == FileName.Length() - 2) || (FileName[Index + 3] == L'.')) &&
                 IsReservedName(FileName.SubString(1, Index - 1) + FileName.SubString(Index + 3, FileName.Length() - Index - 3 + 1)))
        {
          FileName.Delete(Index, 3);
          InvalidChar = const_cast<wchar_t *>(FileName.c_str() + Index - 1);
        }
        else
        {
          InvalidChar++;
        }
      }
      else
      {
        InvalidChar++;
      }
    }
  }
  return FileName;
}
Exemplo n.º 7
0
//* If LRC in frame[] is correct returns 1, else 0. 
bool CheckLRC(char *frame)
{
	uint8_t a = 0;
	uint8_t Sum;
	char tempByte[2];
	uint8_t tempSum = 0;
	char temp[4];
	uint8_t LRC_calculated = 0;

	uint8_t LRC_dec_from_frame = 0;	
	
//* counts chars  in frame
	while(word[a] != '\r')
			{
				a++;
			}

temp[1] = frame[a-1];
temp[0] = frame[a-2];

HexToByte(temp,&LRC_dec_from_frame);

frame[a-1] = '\0';
frame[a-2] = '\0';

//* calculates LRC
LRC_calculated = GetLRC(frame);

if (LRC_calculated == LRC_dec_from_frame)
{
	return 1;
}
else
{
	return 0;
}
}
Exemplo n.º 8
0
UCVarValue FBaseCVar::FromString (const char *value, ECVarType type)
{
	UCVarValue ret;
	int i;

	switch (type)
	{
	case CVAR_Bool:
		if (stricmp (value, "true") == 0)
			ret.Bool = true;
		else if (stricmp (value, "false") == 0)
			ret.Bool = false;
		else
			ret.Bool = strtol (value, NULL, 0) != 0;
		break;

	case CVAR_Int:
		if (stricmp (value, "true") == 0)
			ret.Int = 1;
		else if (stricmp (value, "false") == 0)
			ret.Int = 0;
		else
			ret.Int = strtol (value, NULL, 0);
		break;

	case CVAR_Float:
		ret.Float = (float)strtod (value, NULL);
		break;

	case CVAR_String:
		ret.String = const_cast<char *>(value);
		break;

	case CVAR_GUID:
		// {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
		// 01234567890123456789012345678901234567
		// 0         1         2         3

		ret.pGUID = NULL;
		for (i = 0; i < 38; ++i)
		{
			if (value[i] == 0)
			{
				break;
			}
			bool goodv = true;
			switch (i)
			{
			case 0:
				if (value[i] != '{')
					goodv = false;
				break;
			case 9:
			case 14:
			case 19:
			case 24:
				if (value[i] != '-')
					goodv = false;
				break;
			case 37:
				if (value[i] != '}')
					goodv = false;
				break;
			default:
				if (value[i] < '0' && value[i] > '9' &&
					value[i] < 'A' && value[i] > 'F' &&
					value[i] < 'a' && value[i] > 'f')
				{
					goodv = false;
				}
				break;
			}
		}
		if (i == 38 && value[i] == 0)
		{
			cGUID.Data1 = strtoul (value + 1, NULL, 16);
			cGUID.Data2 = (WORD)strtoul (value + 10, NULL, 16);
			cGUID.Data3 = (WORD)strtoul (value + 15, NULL, 16);
			cGUID.Data4[0] = HexToByte (value + 20);
			cGUID.Data4[1] = HexToByte (value + 22);
			cGUID.Data4[2] = HexToByte (value + 25);
			cGUID.Data4[3] = HexToByte (value + 27);
			cGUID.Data4[4] = HexToByte (value + 29);
			cGUID.Data4[5] = HexToByte (value + 31);
			cGUID.Data4[6] = HexToByte (value + 33);
			cGUID.Data4[7] = HexToByte (value + 35);
			ret.pGUID = &cGUID;
		}
		break;

	default:
		break;
	}

	return ret;
}
Exemplo n.º 9
0
//* FC16 This command    writes the contents of  analog output holding registers
void ForceMultipleRegisters()
{
char OutputFrame[OFS];  
uint8_t counter = 0;
uint16_t Value=0;
uint16_t FirstRegister;
uint16_t NumberOfRegs;
uint16_t Coil;
uint8_t NumberOfDataBytes;
char temp[2];
char temp4[4];
uint16_t n = 0;

//Clears table
for(n = 0; n<OFS; n++){OutputFrame[n] = '\0';}

// rewrites slave's address & number of function
RewritingChars(OutputFrame,0,12);
	
//gets number of first coil
temp4[0] = word[5];
temp4[1] = word[6];
temp4[2] = word[7];
temp4[3] = word[8];
HexToByte_4(temp4, &FirstRegister);

//gets number  of coils to written	
temp4[0] = word[9];
temp4[1] = word[10];
temp4[2] = word[11];
temp4[3] = word[12];
HexToByte_4(temp4, &NumberOfRegs);

//gets the number of data bytes to follow
temp[0] = word[13];
temp[1] = word[14];
HexToByte(temp, &NumberOfDataBytes);
if(Check_DataAddr_F03_F04(FirstRegister, NumberOfRegs) & Check_DataVal_F16(NumberOfDataBytes))
{

	counter=15;
	for(n=0; n<(NumberOfDataBytes/2); n++)
	{
		temp4[0] = word[counter];
		counter++;
		temp4[1] = word[counter];
		counter++;
		temp4[2] = word[counter];
		counter++;
		temp4[3] = word[counter];		
		counter++;
	
		HexToByte_4(temp4, &Value);
		Output_Registers[FirstRegister+n] = Value;
	}
	
	//* Writes LRC.
	counter = 13;
	ByteToHex(temp,GetLRC(OutputFrame));
	OutputFrame[counter] = temp[0];
	counter++;
	OutputFrame[counter] = temp[1];
	counter++;
	OutputFrame[counter] = 0x0D;
	counter++;
	OutputFrame[counter] = 0x0A;
	counter++;
	OutputFrame[counter] = 0x0A;
	counter++;

	//sending frame 
	UART_SendStr(OutputFrame); 
}
}
Exemplo n.º 10
0
static int Parse(const Byte *p)
{
  int c1 = HexToByte(p[0]); if (c1 < 0) return -1;
  int c2 = HexToByte(p[1]); if (c2 < 0) return -1;
  return (c1 << 4) | c2;
}
Exemplo n.º 11
0
//----------------------------------------------------------------//
void MOAITextFrame::Parse () {
	
	float xScaleAdvance = this->mRightToLeft ? -1.0f : 1.0f;
	
	this->mLineBottom = 0;
	this->mLineTop = 0;
	
	this->mLineCount = 0;
	this->mLineXMax = 0.0f;
	this->mTokenXMin = 0.0f;
	this->mTokenXMax = 0.0f;
	
	float points = this->mPoints;
	
	float lineHeight = this->mFont->GetLineSpacing () * this->mPoints * this->mLineSpacing;
	this->mTotalLines = ( u32 )floorf ( this->mFrame.Height () / lineHeight );
	if ( !this->mTotalLines ) return;

	this->mPen.Init ( 0.0f, this->mFrame.mYMin );

	u32 colorSize = 0;
	u8 color [ COLOR_MAX ];

	u32 c = 0;
	this->mGlyph = 0;

	bool inToken = false;
	int resetIdx = 0;
	
	u32 state = META_START;
	while ( state != META_FINISH ) {
		
		switch ( state ) {
			
			//================================================================//
			// META
			//----------------------------------------------------------------//
			
			//----------------------------------------------------------------//
			// check to see if we've encountered a style escape
			case META_START: {
				
				resetIdx = this->mIdx;
				c = this->DecodeChar ();

				if ( c == 0 ) {
					
					this->FlushToken ();
					
					if ( this->mLineCount >= this->mTotalLines ) {
						TRANSITION ( META_FINISH );
					}
					
					// save the cursor just before the EOF
					this->mCursor->mIndex = resetIdx;
					this->mCursor->mRGBA  = this->mRGBA;
					
					this->FlushLine ();
					
					TRANSITION ( META_FINISH );
				}
				
				TRANSITION_ON_MATCH ( '<', STYLE_START );
				TRANSITION ( TEXT_START );
			}
			
			//================================================================//
			// TEXT
			//----------------------------------------------------------------//
			
			//----------------------------------------------------------------//
			// process a single char as text (ignore style escape)
			case TEXT_START: {
				
				if ( c == '\n' ) {
					
					this->FlushToken ();
					inToken = false;
					
					if ( this->mLineCount >= this->mTotalLines ) {
						TRANSITION ( META_FINISH );
					}
					
					this->mCursor->mIndex = resetIdx;
					this->mCursor->mRGBA  = this->mRGBA;
					
					this->FlushLine ();
					
					this->mPen.mX = 0.0f;
					this->mTokenXMax = 0.0f;
					this->mTokenXMin = 0.0f;
					this->mGlyph = 0;
					
					TRANSITION ( META_START );
				}
				
				const MOAIGlyph* prevGlyph = this->mGlyph;
				const MOAIGlyph* glyph = &this->mFont->GetGlyphForChar ( c );
				this->mGlyph = glyph;
				
				assert ( glyph );
				
				bool hasWidth = ( glyph->mWidth > 0.0f );
				
				// apply kerning
				if ( prevGlyph ) {
					MOAIKernVec kernVec = prevGlyph->GetKerning ( glyph->mCode );
					this->mPen.mX += kernVec.mX * points * xScaleAdvance;
				}
				
				if ( hasWidth ) {
	
					if ( inToken == false ) {
						
						// save the cursor just before the new token
						this->mCursor->mIndex = resetIdx;
						this->mCursor->mRGBA  = this->mRGBA;
						
						this->mTokenXMin = this->mPen.mX;
						this->mTokenXMax = this->mPen.mX;
						inToken = true;
					}
					
					// push the glyph
					float penX = this->mPen.mX + (( glyph->mWidth + glyph->mBearingX ) * points * xScaleAdvance );
					float glyphX = this->mRightToLeft ? penX : this->mPen.mX;
					
					this->mLayout->PushGlyph ( glyph, resetIdx, glyphX, 0.0f, points, this->mRGBA );
					this->mTokenXMax = penX;
				}
				else if ( inToken ) {
				
					this->FlushToken ();
					
					// bail if line count overflow
					if ( this->mLineCount >= this->mTotalLines ) {
						TRANSITION ( META_FINISH );
					}
					inToken = false;
				}
				
				// advance the pen
				this->mPen.mX += glyph->mAdvanceX * points * xScaleAdvance;
				
				// back to start
				TRANSITION ( META_START );
			}
			
			//================================================================//
			// STYLE
			//----------------------------------------------------------------//
			
			//----------------------------------------------------------------//
			// see which style we're parsing
			case STYLE_START: {
				
				c = this->DecodeChar ();
				TRANSITION_ON_MATCH ( 'c', COLOR_START );
				TRANSITION ( STYLE_ABORT );
			}
			
			// reset the cursor and go directly to text mode
			case STYLE_ABORT: {
			
				this->mIdx = resetIdx;
				c = this->DecodeChar ();
				TRANSITION ( TEXT_START );
			}
			
			//================================================================//
			// COLOR
			//----------------------------------------------------------------//
			
			//----------------------------------------------------------------//
			case COLOR_START: {
			
				colorSize = 0;
			
				c = this->DecodeChar ();
				if ( IsWhitespace ( c )) continue;
				
				if ( c == ':' ) {
					state = COLOR_BODY;
					break;
				}
				
				TRANSITION_ON_MATCH ( ':', COLOR_BODY );
				TRANSITION_ON_MATCH ( '>', COLOR_FINISH );
				
				TRANSITION ( STYLE_ABORT );
			}
			
			//----------------------------------------------------------------//
			case COLOR_BODY: {
				
				c = this->DecodeChar ();
				if ( IsWhitespace ( c )) continue;
				
				TRANSITION_ON_MATCH ( '>', COLOR_FINISH );
				
				u8 hex = HexToByte ( c );
				if (( hex != 0xff ) && ( colorSize < COLOR_MAX )) {
					color [ colorSize++ ] = hex;
					break;
				}
				
				TRANSITION ( STYLE_ABORT );
			}
			
			//----------------------------------------------------------------//
			case COLOR_FINISH: {
				
				this->mRGBA = this->PackColor ( color, colorSize );
				TRANSITION ( META_START );
			}
		}
	}
	
	// discard overflow
	this->mLayout->SetTop ( this->mLineTop );
}
Exemplo n.º 12
0
static int Lwm2mClient_Start(Options * options)
{
    FILE * logFile = NULL;
    uint8_t * loadedClientCert = NULL;
    int result = 0;
    uint8_t * key = NULL;

    if (options->Daemonise)
    {
        Daemonise(options->Verbose);
    }
    else
    {
        signal(SIGINT, Lwm2m_CtrlCSignalHandler);
    }

    signal(SIGTERM, Lwm2m_CtrlCSignalHandler);

    if (options->LogFile)
    {
        errno = 0;
        logFile = fopen(options->LogFile, "at");
        if (logFile != NULL)
        {
            Lwm2m_SetOutput(logFile);

            // redirect stdout
            dup2(fileno(logFile), STDOUT_FILENO);
        }
        else
        {
            Lwm2m_Error("Failed to open log file %s: %s\n", options->LogFile, strerror(errno));
        }
    }

    if (options->Version)
    {
        Lwm2m_Printf(0, "%s\n", version);
        goto error_close_log;
    }

    srandom((int)time(NULL)*getpid());
    if (options->CoapPort == 0)
    {
        options->CoapPort = 6000 + (rand() % 32768);
    }

    Lwm2m_SetLogLevel((options->Verbose) ? DebugLevel_Debug : DebugLevel_Info);
    Lwm2m_PrintBanner();
    if (options->Verbose)
    {
        PrintOptions(options);
    }
    Lwm2m_Info("Awa LWM2M Client, version %s\n", version);
    Lwm2m_Info("  Process ID     : %d\n", getpid());
    Lwm2m_Info("  Endpoint name  : \'%s\'\n", options->EndPointName);
    Lwm2m_Info("  DTLS library   : %s\n", DTLS_LibraryName);
    Lwm2m_Info("  CoAP library   : %s\n", coap_LibraryName);
    Lwm2m_Info("  CoAP port      : %d\n", options->CoapPort);
    Lwm2m_Info("  IPC port       : %d\n", options->IpcPort);
    Lwm2m_Info("  Address family : IPv%d\n", options->AddressFamily == AF_INET ? 4 : 6);

    Lwm2mCore_SetDefaultContentType(options->DefaultContentType);

    CoapInfo * coap = coap_Init((options->AddressFamily == AF_INET) ? "0.0.0.0" : "::", options->CoapPort, false /* not a server */, (options->Verbose) ? DebugLevel_Debug : DebugLevel_Info);
    if (coap == NULL)
    {
        Lwm2m_Error("Failed to initialise CoAP on port %d\n", options->CoapPort);
        result = 1;
        goto error_close_log;
    }

    // always set key
    if (options->CertificateFile)
    {
        loadedClientCert = LoadCertificateFile(options->CertificateFile);
    }
    else
        coap_SetCertificate(clientCert, sizeof(clientCert), AwaCertificateFormat_PEM);

    if (options->PskIdentity && options->PskKey)
    {
        int hexKeyLength = strlen(options->PskKey);
        int keyLength = hexKeyLength / 2;
        key = (uint8_t *)malloc(keyLength);
        if (key)
        {
           char * value = options->PskKey;
           int index;
           for (index = 0; index < keyLength; index++)
           {
               key[index] = HexToByte(value);
               value += 2;
           }
           coap_SetPSK(options->PskIdentity, key, keyLength);
        }
        else
            coap_SetPSK(pskIdentity, pskKey, sizeof(pskKey));
    }
    else
        coap_SetPSK(pskIdentity, pskKey, sizeof(pskKey));

    // if required read the bootstrap information from a file
    const BootstrapInfo * factoryBootstrapInfo;
    if (options->FactoryBootstrapFile != NULL)
    {
        factoryBootstrapInfo = BootstrapInformation_ReadConfigFile(options->FactoryBootstrapFile);
        if (factoryBootstrapInfo == NULL)
        {
            Lwm2m_Error("Factory Bootstrap configuration file load failed\n");
            result = 1;
            goto error_coap;
        }
        else
        {
            Lwm2m_Info("Factory Bootstrap:\n");
            Lwm2m_Info("Server Configuration\n");
            Lwm2m_Info("====================\n");
            BootstrapInformation_Dump(factoryBootstrapInfo);
        }
    }
    else
    {
        factoryBootstrapInfo = NULL;
    }

    Lwm2mContextType * context = Lwm2mCore_Init(coap, options->EndPointName);

    // Must happen after coap_Init().
    RegisterObjects(context, options);

    if (factoryBootstrapInfo != NULL)
    {
        Lwm2mCore_SetFactoryBootstrap(context, factoryBootstrapInfo);
    }

    // bootstrap information has been loaded, no need to hang onto this anymore
    BootstrapInformation_DeleteBootstrapInfo(factoryBootstrapInfo);

    // load any specified objDef files
    if (LoadObjectDefinitionsFromFiles(context, options->ObjDefsFiles, options->NumObjDefsFiles) != 0)
    {
        goto error_core;
    }

    // Listen for UDP packets on IPC port
    int xmlFd = xmlif_init(context, options->IpcPort);
    if (xmlFd < 0)
    {
        Lwm2m_Error("Failed to initialise XML interface on port %d\n", options->IpcPort);
        result = 1;
        goto error_core;
    }
    xmlif_RegisterHandlers();

    // Wait for messages on both the IPC and CoAP interfaces
    while (!quit)
    {
        int loop_result;
        struct pollfd fds[2];
        int nfds = 2;
        int timeout;

        fds[0].fd = coap->fd;
        fds[0].events = POLLIN;

        fds[1].fd = xmlFd;
        fds[1].events = POLLIN;

        timeout = Lwm2mCore_Process(context);

        loop_result = poll(fds, nfds, timeout);
        if (loop_result < 0)
        {
            if (errno == EINTR)
            {
                continue;
            }
            perror("poll:");
            break;
        }
        else if (loop_result > 0)
        {
            if (fds[0].revents == POLLIN)
            {
                coap_HandleMessage();
            }
            if (fds[1].revents == POLLIN)
            {
                xmlif_process(fds[1].fd);
            }
        }
        coap_Process();
    }
    Lwm2m_Debug("Exit triggered\n");

    xmlif_DestroyExecuteHandlers();
    xmlif_destroy(xmlFd);
error_core:
    Lwm2mCore_Destroy(context);
error_coap:
    coap_Destroy();

error_close_log:
    free(loadedClientCert);
    free(key);
    Lwm2m_Info("Client exiting\n");
    if (logFile)
    {
        fclose(logFile);
    }

    return result;
}
Exemplo n.º 13
0
int main()
{
    int success;

    //HexToValue TEST
    success = 1;
    if( HexToValue( 'd' ) != HexToValue( 'D' ) )
        success = 0;
    if( HexToValue( 'Z' ) != 255 )
        success = 0;
    if( HexToValue( 'b' ) != 11 )
        success = 0;
    if( HexToValue( '4' ) != 4 )
        success = 0;
    evaluate("HEXTOVALUE",success);
    //EOT
    //HEXTOBYTE
    success = 1;
    if( HexToByte( '1', '2' ) != 0x12 )
        success = 0;
    if( HexToByte( 'a', 'B' ) != 0xAB )
        success = 0;
    if( HexToByte( '3', '4' ) != 0x34 )
        success = 0;
    if( HexToByte( '1', '2' ) != 0x12 )
        success = 0;
    evaluate("HEXTOBYTE",success);
    //EOT
    //B64ToValue
    success = 1;
    if( B64ToValue( 'a' ) != 26 )
        success = 0;
    if( B64ToValue( 'B' ) != 1 )
        success = 0;
    if( B64ToValue( '+' ) != 62 )
        success = 0;
    if( B64ToValue( 'Z' ) != 25 )
        success = 0;
    evaluate("B64ToVal",success);
    //EOT
    //ValueToHex
    success = 1;
    if( ValueToHex(3) != '3' )
        success = 0;
    if( ValueToHex(11) != 'B' )
        success = 0;
    if( ValueToHex(18) != -1 )
        success = 0;
    evaluate("ValueToHex",success);
    //EOT
    //ValueToB64
    success = 1;
    if( ValueToB64(3) != 'D' )
        success = 0;
    if( ValueToB64(42) != 'q' )
        success = 0;
    if( ValueToB64(55) != '3' )
        success = 0;
    if( ValueToB64(62) != '+' )
        success = 0;
    if( ValueToB64(63) != '/' )
        success = 0;
    if( ValueToB64(66) != -1 )
        success = 0;
    evaluate("ValueToB64",success);
    //B64ToBytes
    success = 1;

    byte_t *b = malloc(3);
    
    if( ( B64ToBytes( b, "TWlL" ) != 0) )
        success = 0;
    if( b[0] != 0x4D || b[1] != 0x69 || b[2] != 0x4B )
        success = 0;
    
    if( ( B64ToBytes( b, "Z29z" ) != 0) )
        success = 0;
    if( b[0] != 0x67 || b[1] != 0x6F || b[2] != 0x73 )
        success = 0;

    if( ( B64ToBytes( b, "YQ==" ) != 0) )
        success = 0;
    if( b[0] != 0x61 || b[1] != 0x00 || b[2] != 0x00 )
        success = 0;

    if( ( B64ToBytes( b, "YWM=" ) != 0) )
        success = 0;
    if( b[0] != 0x61 || b[1] != 0x63 || b[2] != 0x00 )
        success = 0;
    evaluate("B64ToBytes",success);
    //EOT
    //ByteToHex
    success = 1;
    char b2h[3];
    b2h[2] = '\0';
    ByteToHex( b2h ,0xAF );
    if( b2h[0] != 'A' || b2h[1] != 'F' )
        success = 0;

    ByteToHex( b2h ,0x02 );
    if( b2h[0] != '0' || b2h[1] != '2' )
        success = 0;

    evaluate("ByteToHex",success);
    //EOT
    //BytesToB64
    char b2b[4];
    BytesToB64( b2b, (unsigned char *)"gat", 3);
    if( b2b[0] != 'Z' || b2b[1] != '2' || b2b[2] != 'F' || b2b[3] != '0' )
        success = 0;
    BytesToB64( b2b, (unsigned char *)"pa", 2);
    if( b2b[0] != 'c' || b2b[1] != 'G' || b2b[2] != 'E' || b2b[3] != '=' )
        success = 0;
    BytesToB64( b2b, (unsigned char *)"g", 1);
    if( b2b[0] != 'Z' || b2b[1] != 'w' || b2b[2] != '=' || b2b[3] != '=' )
        success = 0;
    evaluate("BytesToB64",success);
    //EOT
    //HexStringToBytes
    success = 1;

    byte_t *dest;
    int temp = HexStringToBytes( &dest, LOREMHEX );
    char * str;
    BytesToString( &str, dest, (size_t)temp );

    if( strcmp( str, LOREM ) )
        success = 0;
    free(dest);
    free(str);
    evaluate("HexStringToBytes",success);
    //EOT
    //B64StringToBytes
    success = 1;

    temp = B64StringToBytes( &dest, LOREMB64 );
    BytesToString( &str, dest, (size_t)temp );

    if( strcmp( str, LOREM ) )
        success = 0;
    free(dest);
    free(str);
    evaluate("B64StringToBytes",success);
    //EOT
    //BytesToHexString
    success = 1;

    dest = (byte_t *) LOREM;
    BytesToHexString( &str, dest, strlen( LOREM ) );
    if( strcmp( LOREMHEX, str ) )
        success = 0;
    evaluate("BytesToHexString",success);



    //BytesToB64String
    success = 1;
    
    BytesToB64String( &str, (byte_t *)LOREM, strlen(LOREM) );
    
    if( strcmp( LOREMB64, str ) )
        success = 0;

    evaluate("B64StringToBytes",success);
    free(str);


    BytesToB64String( &str, (byte_t *)"A", 1 );
    if( strcmp( str, "QQ==" ) )
        success = 0;
    free(str);


    BytesToB64String( &str, (byte_t *)"AB", 2 );
    if( strcmp( str, "QUI=" ) )
        success = 0;
    free(str);


    BytesToB64String( &str,(byte_t *)"ABC", 3 );
    if( strcmp( str, "QUJD" ) )
        success = 0;
    free(str);

    evaluate("B64StringToBytes",success);
    //eot
    









    return 0;
}
Exemplo n.º 14
0
//----------------------------------------------------------------//
bool MOAITextStyler::ParseStyle () {

	if ( this->mStr [ this->mIdx ] != '<' ) return false;

	u32 colorSize = 0;
	u8 color [ COLOR_MAX ];

	u32 c = 0;
	
	int startIdx = this->mIdx;
	
	u32 state = STYLE_START;
	while ( state != DONE ) {
		
		switch ( state ) {

			//================================================================//
			// STYLE
			//----------------------------------------------------------------//
			
			//----------------------------------------------------------------//
			// see which style we're parsing
			case STYLE_START: {
				
				c = this->GetChar ();
				
				TRANSITION_ON_MATCH ( '<', STYLE_BODY );
				TRANSITION ( STYLE_ABORT );
			}
			
			//----------------------------------------------------------------//
			case STYLE_BODY: {
				
				c = this->GetChar ();

				if ( c == '<' ) {
					this->mIdx = startIdx + 1;
					this->FinishToken ();
					startIdx = this->mIdx;
					TRANSITION ( DONE );
				}
				
				TRANSITION_ON_MATCH ( '/', STYLE_POP_START );
				TRANSITION_ON_MATCH ( '>', STYLE_POP_FINISH );
				TRANSITION_ON_MATCH ( 'c', COLOR_START );
				TRANSITION ( STYLE_NAME_START );
			}
			
			//----------------------------------------------------------------//
			case STYLE_ABORT: {
				
				this->mIdx = startIdx;
				TRANSITION ( DONE );
			}
			
			//================================================================//
			// COLOR
			//----------------------------------------------------------------//
			
			//----------------------------------------------------------------//
			case COLOR_START: {
				
				colorSize = 0;
				c = this->GetChar ();
				
				TRANSITION_ON_MATCH ( ':', COLOR_BODY );
				
				// reset and try to parse style name instead
				this->mIdx = startIdx;
				TRANSITION ( STYLE_NAME_START );
			}
			
			//----------------------------------------------------------------//
			case COLOR_BODY: {
				
				c = this->GetChar ();

				TRANSITION_ON_MATCH ( '>', COLOR_FINISH );
				
				u8 hex = HexToByte ( c );
				if (( hex != 0xff ) && ( colorSize < COLOR_MAX )) {
					color [ colorSize++ ] = hex;
					TRANSITION ( COLOR_BODY );
				}
				TRANSITION ( STYLE_ABORT );
			}
			
			//----------------------------------------------------------------//
			case COLOR_FINISH: {
				
				this->FinishToken ();
				
				MOAITextStyle* style = this->mTextBox->AddAnonymousStyle ( this->mCurrentStyle );
				style->mColor = this->PackColor ( color, colorSize );
				this->PushStyle ( style );
				
				TRANSITION ( DONE );
			}
			
			//================================================================//
			// STYLE_NAME
			//----------------------------------------------------------------//
			
			//----------------------------------------------------------------//
			case STYLE_NAME_START: {
				
				c = this->GetChar ();
				
				if ( MOAIFont::IsControl ( c ) || MOAIFont::IsWhitespace ( c )) {
					TRANSITION ( STYLE_ABORT );
				}
				
				TRANSITION_ON_MATCH ( '>', STYLE_NAME_FINISH );
				TRANSITION ( STYLE_NAME_START );
			}
			
			//----------------------------------------------------------------//
			case STYLE_NAME_FINISH: {

				this->FinishToken ();

				int namesize = this->mIdx - startIdx - 2;
				assert ( namesize > 0 );

				char* name = ( char* )alloca ( namesize + 1 );
				memcpy ( name, &this->mStr [ startIdx + 1 ], namesize );
				name [ namesize ] = 0;
				
				MOAITextStyle* style = this->mTextBox->GetStyle ( name );
				this->PushStyle ( style );
				
				TRANSITION ( DONE );
			}
			
			//================================================================//
			// STYLE_POP
			//----------------------------------------------------------------//
			
			//----------------------------------------------------------------//
			case STYLE_POP_START: {
				
				c = this->GetChar ();
				
				if ( MOAIFont::IsControl ( c ) || MOAIFont::IsWhitespace ( c )) {
					TRANSITION ( STYLE_ABORT );
				}
				
				TRANSITION_ON_MATCH ( '>', STYLE_POP_FINISH );
				TRANSITION ( STYLE_POP_START );
			}
			
			//----------------------------------------------------------------//
			case STYLE_POP_FINISH: {
				
				this->FinishToken ();
				this->PopStyle ();
				TRANSITION ( DONE );
			}
		}
	}
	
	return ( this->mIdx > startIdx );
}
Exemplo n.º 15
0
::stringstream ss;ss<<std::hex<<hex;ss>>x;return x;}int ROL(std::string 
blockhash,int iPos,std::string hash,int hexpos){std::string cpid3="";if(iPos<=(
int)hash.length()-(0x1c97+497-0x1e87)){std::string hex=hash.substr(iPos,
(0xa5d+6424-0x2373));int rorcount=BitwiseCount(blockhash,hexpos);int b=HexToByte
(hex)-rorcount;if(b>=(0x5b2+1768-0xc9a)){return b;}}return HexToByte("\x30\x30")
;}std::string CPID::Update6(std::string non_finalized,uint256 block_hash){std::