Esempio n. 1
0
static BOOL GuidFromSz(__in LPCTSTR s, __out GUID *lpGuid)
{
  SIZE_T i;
  BYTE val, val2;
  LPCTSTR s2;

  if (s == NULL || s[0] != _T('{'))
    return FALSE;
  memset(lpGuid, 0, sizeof(GUID));
  //parse {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
  for (i=1; i<9; i++)
  {
    val = Char2Hex(s[i]);
    if (val == 255)
      return FALSE;
    lpGuid->Data1 = (lpGuid->Data1 << 4) | val;
  }
  if (s[9] != _T('-'))
    return FALSE;
  for (i=10; i<14; i++)
  {
    val = Char2Hex(s[i]);
    if (val == 255)
      return FALSE;
    lpGuid->Data2 = (lpGuid->Data2 << 4) | val;
  }
  if (s[14] != _T('-'))
    return FALSE;
  for (i=15; i<19; i++)
  {
    val = Char2Hex(s[i]);
    if (val == 255)
      return FALSE;
    lpGuid->Data3 = (lpGuid->Data3 << 4) | val;
  }
  if (s[19] != _T('-'))
    return FALSE;
  s2 = s+20;
  for (i=0; i<8; i++)
  {
    if (i == 2)
    {
      if (*s2 != _T('-'))
        return FALSE;
      s2++;
    }
    val = Char2Hex(s2[0]);
    if (val == 255)
      return FALSE;
    val2 = Char2Hex(s2[1]);
    if (val2 == 255)
      return FALSE;
    lpGuid->Data4[i] = (val << 4) | val2;
    s2 += 2;
  }
  if (s[37] != _T('}') && s[38] == 0)
    return FALSE;
  return TRUE;
}
/*********************************************************************
 * @fn          StrConvert2Hex
 * @brief       To change a string type into hex .
 * @param       str - A string which will be converted to the corresponding hex. 
 * @param       arr - An array which will save the hex. 
 * @param       length - length of the string.
 * @return      null.
 */
void StrConvert2Hex (const unsigned char *str, uint8 *arr ,int length)
{
  for(int i =0;i<length;++i)
  {
    if(i%2 ==0){
      arr[i/2]=Char2Hex(str[i]);
      arr[i/2] <<=4;
    }
    else
      arr[i/2] |= Char2Hex(str[i]);
  }
}
Esempio n. 3
0
CStdString GameJoltAPI::EncodeURL( CStdString inputString )
{

	CStdString escapedString = "";

	int max = inputString.length();
	for ( int i = 0; i < max; i++ )
	{

		if 
		( 
			(48 <= inputString[i] && inputString[i] <= 57) || //0-9
			(65 <= inputString[i] && inputString[i] <= 90) || //abc...xyz
			(97 <= inputString[i] && inputString[i] <= 122) || //ABC...XYZ
			(
				inputString[i] == '~' || inputString[i] == '!' || 
				inputString[i]=='*' || inputString[i] == '(' || 
				inputString[i] == ')' || inputString[i] == '\''
			)
		)
		{
			escapedString += inputString[i];
		}
		else
		{
			escapedString += _T("%");
			escapedString += Char2Hex( inputString[i] ); //converts char 255 to string "ff"
		}

	}

	return escapedString;

}
Esempio n. 4
0
void ConnPort::SendComData(char *szRevData,int iLen,BOOL bNewLine)
{
		CString			strMsg;
		CTime			tt;
		CString			strTime;
		CString			strTmp;
		TCHAR			m_revData[MAX_BUFFER_SIZE];
		char			szTrans[MAX_BUFFER_SIZE];
	
        CSSToolDlg		*dlg=(CSSToolDlg *)AfxGetApp()->GetMainWnd();
        memset(m_revData,0,MAX_BUFFER_SIZE);
		memset(szTrans,	 0,MAX_BUFFER_SIZE);

		if(m_bHexShow)
		{
			Char2Hex(szRevData,szTrans,iLen);
			MultiByteToWideChar(CP_ACP,0,szTrans,-1,m_revData,MAX_BUFFER_SIZE);	
		}
		else
		{
			MultiByteToWideChar(CP_ACP,0,szRevData,-1,m_revData,MAX_BUFFER_SIZE);
		}
		strTmp=m_revData;
        if(!strTmp.IsEmpty())
        {
			
			tt=CTime::GetCurrentTime();
			strTime=tt.Format(L"[%m/%d %H:%M:%S] ");
			if(bNewLine&& !m_bHexShow &&m_bTimeShow)
				strMsg=strTime+strTmp;
			else
				strMsg=strTmp;
        }
        dlg->OutMsg(strMsg);
}
Esempio n. 5
0
std::string CCharset::UrlEncode(const std::string& c)
{
    std::string escaped;
    uint32_t max = (uint32_t)c.length();
    for (uint32_t i = 0; i < max; i++)
    {
        if ((48 <= c[i] && c[i] <= 57) ||//0-9
            (65 <= c[i] && c[i] <= 90) ||//ABC...XYZ
            (97 <= c[i] && c[i] <= 122) || //abc...xyz
            (strchr("~-_./:()'", c[i])))
        {
            escaped.append(&c[i], 1);
        }
        else if (c[i] == ' ')
        {
            escaped.append("%20");
        }
        else
        {
            escaped.append("%");
            escaped.append(Char2Hex(c[i]));//converts char 255 to string "FF"
        }
    }
    return escaped;
}
Esempio n. 6
0
static void test_char2hex(void)
{
    /*
     * Even if this routine is pretty simple, this is a sanity
     * check. This routine does not detect errors so we only
     * try valid conversions.
     * This routine does not expect the '0x' in front of numbers,
     * so our tests will be:
     * 0 .. 9
     * a .. f
     * A .. F
     */
    char i = '0';
    for (i = '0'; i <= '9'; ++i)
        assert_int_equal(i - '0', Char2Hex(0, i));
    for (i = 'a'; i <= 'f'; ++i)
        assert_int_equal(i - 'a' + 0x0A, Char2Hex(0, i));
}
Esempio n. 7
0
/**
 * Covert object GUID to hex string.
 *
 * @param s Bytes to convert.
 * @param out Converted string.
 * @return 0 on success; error code on failure.
 */
DWORD Guid2Str(IN PVOID s, OUT PSTR *out)
{
    DWORD dwError = 0;
    INT i = 0, j;
    PUCHAR b = (PUCHAR) s;

    dwError = LwAllocateMemory(37 * sizeof(char), OUT_PPVOID(out));
    ADT_BAIL_ON_ALLOC_FAILURE_NP(!dwError);

    for(j = 3; j >= 0; --j, i += 2) {
        Char2Hex(b[j], *out + i);
    }

    *(*out + i) = '-'; ++i;

    for(j = 5; j >= 4; --j, i += 2) {
        Char2Hex(b[j], *out + i);
    }

    *(*out + i) = '-'; ++i;

    for(j = 7; j >= 6; --j, i += 2) {
        Char2Hex(b[j], *out + i);
    }

    *(*out + i) = '-'; ++i;

    for(j = 8; j <= 9; ++j, i += 2) {
        Char2Hex(b[j], *out + i);
    }

    *(*out + i) = '-'; ++i;

    for(j = 10; j <= 15; ++j, i += 2) {
        Char2Hex(b[j], *out + i);
    }

    cleanup:
       return dwError;

    error:
       LW_SAFE_FREE_MEMORY(*out);
       goto cleanup;
}
Esempio n. 8
0
void HexDisplay(unsigned char* data, char* str,int length){
	int i=0;
	for(;i<length;i++){
		char res[2]={0};
		Char2Hex(data[i],res);
		str[i*2]=res[1];
		str[i*2+1]=res[0];
	}
	str[i*2]='\0';
}
Esempio n. 9
0
// Function to convert string of unsigned chars to string of chars, eg 0x9E3D -> "9E3D"
// static
void Library::CharStr2HexStr(const unsigned char* pucCharStr, char* pszHexStr, int iBufsize, int iSize)
{
	int i;
	char szHex[3];
	pszHexStr[0] = 0;
	for(i=0; i<iSize; i++)
	{
		Char2Hex(pucCharStr[i], szHex);
		strcat_s(pszHexStr, iBufsize, szHex);
	}
}
Esempio n. 10
0
//Function to convert string of unsigned chars to string of chars
void CharStr2HexStr(const unsigned char* pucCharStr, char* pszHexStr, unsigned long lSize)
{
    unsigned long i;
    char szHex[2];
    pszHexStr[0] = 0;
    for(i=0; i<lSize; i++)
    {
        Char2Hex(pucCharStr[i], szHex);
        strcat(pszHexStr, szHex);
    }
}
Esempio n. 11
0
//Function to convert string of unsigned chars to string of chars
void CharStr2HexStr(unsigned char const* pucCharStr, char* pszHexStr, int iSize)
{
	int i;
	char szHex[3];
	pszHexStr[0] = 0;
	for(i=0; i<iSize; i++)
	{
		Char2Hex(pucCharStr[i], szHex);
		strcat(pszHexStr, szHex);
	}
}
Esempio n. 12
0
//---------------------------------------------------------------------------
std::string URL_Encoded_Decode (const std::string& URL)
{
    string Result;
    string::size_type Pos;
    for (Pos=0; Pos<URL.size(); Pos++)
    {
        if (URL[Pos]=='%' && Pos+2<URL.size()) //At least 3 chars
        {
            const unsigned char Char1 = Char2Hex(URL[Pos+1]);
            const unsigned char Char2 = Char2Hex(URL[Pos+2]);
            const unsigned char Char = (Char1<<4) | Char2;
            Result+=Char;
            Pos+=2; //3 chars are used
        }
        else if (URL[Pos]=='+')
            Result+=' ';
        else
            Result+=URL[Pos];
    }
    return Result;
}
Esempio n. 13
0
/*
 * This function parses the address and checks if it conforms to the
 * RFCs 2373, 2460 and 5952.
 * We do not support Microsoft UNC encoding, i.e.
 * hhhh-hhhh-hhhh-hhhh-hhhh-hhhh-hhhh-hhhh.ipv6-literal.net
 * Despite following RFC 5292 we do not signal errors derived from bad
 * zero compression although this might change on time, so please do not
 * trust that we will honor address with wrong zero compression.
 *
 * Returns 0 on success.
 */
static int IPV6_parser(const char *source, struct IPV6Address *address)
{
    /*
     * IPV6 parsing is more complex than IPV4 parsing. There are a few ground rules:
     * - Leading zeros can be omitted.
     * - Fields that are just zeros can be abbreviated to one zero or completely omitted.
     * In the later case the following notation is used: '::'.
     * - Port number is specified in a special way:
     * [hhhh:hhhh:hhhh:hhhh:hhhh:hhhh:hhhh:hhhh]:ppppp
     * Notice that it is possible to have the '[' and ']' without specifying a port.
     *
     * Simplified state machine:
     *
     * _h      _h      _h      _h      _h      _h      _h      _h              _d
     * |/ ':'  |/ ':'  |/ ':'  |/ ':'  |/ ':'  |/ ':'  |/ ':'  |/ ']'    ':'   |/ done
     * 0------>1------>2------>3------>4------>5------>6------>7------>8------>9------>11
     * |\                                                      | done  |
     * -'['                                                    9<------+
     *
     * This is a simplified state machine since I assume that we keep the square brackets inside
     * the same state as hexadecimal digits, which in practice is not true.
     */
    char *p = NULL;
    int sixteen = 0;
    int unsorted_sixteen[6];
    int unsorted_pointer = 0;
    int bracket_expected = 0;
    int port = 0;
    int char_counter = 0;
    bool is_start_bracket = 0;
    bool is_end_bracket = 0;
    bool is_colon = 0;
    bool is_hexdigit = 0;
    bool is_upper_hexdigit = 0;
    bool is_digit = 0;
    int zero_compression = 0;
    int already_compressed = 0;
    int state = 0;
    bool state_change = false;

    /*
     * Initialize our container for unknown numbers.
     */
    for (unsorted_pointer = 0; unsorted_pointer < 6; ++unsorted_pointer)
    {
        unsorted_sixteen[unsorted_pointer] = 0;
    }
    unsorted_pointer = 0;
    /*
     * For simplicity sake we initialize address (if not NULL).
     */
    if (address)
    {
        int i = 0;
        for (i = 0; i < 8; ++i)
        {
            address->sixteen[i] = 0;
        }
        address->port = 0;
    }

    for (p = (char *)source; *p != '\0'; ++p)
    {
        /*
         * Take a closer look at the character
         */
        is_start_bracket = (*p == '[') ? 1 : 0;
        is_end_bracket = (*p == ']') ? 1 : 0;
        is_hexdigit = isxdigit(*p);
        is_digit = isdigit(*p);
        is_colon = (*p == ':') ? 1 : 0;
        if (is_hexdigit)
        {
            if (isalpha(*p))
            {
                is_upper_hexdigit = isupper(*p);
            }
        }

        switch (state)
        {
        case 0:
            /*
             * This case is slightly different because of the possible presence of '['.
             * Notice that '[' is only valid as the first character, anything else is
             * an error!
             */
            if (is_start_bracket)
            {
                if (char_counter == 0)
                {
                    bracket_expected = 1;
                }
                else
                {
                    state = 11;
                    state_change = true;
                }
            }
            else if (is_hexdigit)
            {
                /*
                 * RFC 5952 forbids upper case hex digits
                 */
                if (is_upper_hexdigit)
                {
                    state = 11;
                    state_change = true;
                }
                else
                {
                    sixteen = Char2Hex(sixteen, *p);
                }
            }
            else if (is_colon)
            {
                if (address)
                {
                    address->sixteen[0] = sixteen;
                }
                state = 1;
                state_change = true;
            }
            else
            {
                state = 11;
                state_change = true;
            }
            break;
        case 1:
            /*
             * This state is special since it cannot have a ']' as in the next
             * states.
             */
            if (is_hexdigit)
            {
                /*
                 * RFC 5952 forbids upper case hex digits
                 */
                if (is_upper_hexdigit)
                {
                    state = 11;
                    state_change = true;
                }
                else
                {
                    sixteen = Char2Hex(sixteen, *p);
                }
            }
            else if (is_colon)
            {
                if (char_counter == 0)
                {
                    /*
                     * This means 'X::Y...' which means zero compression.
                     * Flag it!
                     */
                    zero_compression = 1;
                    already_compressed = 1;
                }
                else
                {
                    if (address)
                    {
                        address->sixteen[state] = sixteen;
                    }
                }
                ++state;
                state_change = true;
            }
            else
            {
                state = 11;
                state_change = true;
            }
            break;
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
            if (is_hexdigit)
            {
                /*
                 * RFC 5952 forbids upper case hex digits
                 */
                if (is_upper_hexdigit)
                {
                    state = 11;
                    state_change = true;
                }
                else
                {
                    sixteen = Char2Hex(sixteen, *p);
                }
            }
            else if (is_colon)
            {
                if (char_counter == 0)
                {
                    if (already_compressed)
                    {
                        /*
                         * The '::' symbol can only occur once in a given address.
                         */
                        state = 11;
                        state_change = true;
                    }
                    else
                    {
                        /*
                         * This means '...:X::Y...' which means zero compression.
                         * Flag it!
                         */
                        zero_compression = 1;
                        already_compressed = 1;
                    }
                }
                else
                {
                    if (zero_compression)
                    {
                        /*
                         * If zero compression is enabled, then we cannot trust the position
                         * since we might compressed several fields. We store the value and
                         * look at them afterwards.
                         */
                        unsorted_sixteen[unsorted_pointer] = sixteen;
                        ++unsorted_pointer;
                    }
                    else
                    {
                        /*
                         * No zero compression, just assign the address and keep moving.
                         */
                        if (address)
                        {
                            address->sixteen[state] = sixteen;
                        }
                    }
                    ++state;
                    state_change = true;
                }
            }
            else if (is_end_bracket)
            {
                if (bracket_expected && zero_compression)
                {
                    bracket_expected = 0;
                    /*
                     * RFC 5952 says that we can end an address at any point after
                     * the second position (consequence of the zero compression).
                     * Therefore if we find a ']' we just jump to state 8.
                     */
                    unsorted_sixteen[unsorted_pointer] = sixteen;
                    ++unsorted_pointer;
                    state = 8;
                    state_change = true;
                }
                else
                {
                    /*
                     * Funny stuff, we got a ']' that we were not expecting.
                     * Politely walk back and signal the error.
                     */
                    state = 11;
                    state_change = true;
                }
            }
            else
            {
                state = 11;
                state_change = true;
            }
            break;
        case 7:
            /*
             * This case is special.
             */
            if (is_hexdigit)
            {
                /*
                 * RFC 5952 forbids uppercase hex digits
                 */
                if (is_upper_hexdigit)
                {
                    state = 11;
                    state_change = true;
                }
                else
                {
                    sixteen = Char2Hex(sixteen, *p);
                }
            }
            else if (is_end_bracket)
            {
                if (bracket_expected)
                {
                    bracket_expected = 0;
                    if (address)
                    {
                        address->sixteen[state] = sixteen;
                    }
                    /*
                     * The last possible position for a sixteen is number 8.
                     */
                    ++state;
                    state_change = true;
                }
                else
                {
                    /*
                     * Funny stuff, we got a ']' that we were not expecting.
                     * Politely walk back and signal the error.
                     */
                    state = 11;
                    state_change = true;
                }
            }
            else
            {
                state = 11;
                state_change = true;
            }
            break;
        case 8:
            if (is_colon)
            {
                ++state;
                state_change = true;
            }
            else
            {
                state = 11;
                state_change = true;
            }
            break;
        case 9:
            if (is_digit)
            {
                port = Char2Dec(port, *p);
            }
            else
            {
                state = 11;
                state_change = true;
            }
            break;
        case 10:
            break;
        case 11:
        default:
            return -1;
            break;
        }
        char_counter++;
        if (sixteen > 0xFFFF)
        {
            return -1;
        }
        if (port > 65535)
        {
            return -1;
        }
        if (state_change)
        {
            sixteen = 0;
            port = 0;
            char_counter = 0;
            is_start_bracket = false;
            is_end_bracket = false;
            is_colon = false;
            is_hexdigit = false;
            is_upper_hexdigit = false;
            is_digit = 0;
            state_change = false;
        }
    }
    /*
     * Look at the end state and return accordingly.
     */
    if ((state == 0) || (state == 1))
    {
        /*
         * These states are not final states, so if we exited is because something went wrong.
         */
        return -1;
    }
    /*
     * Thanks to RFC5952 the final states can be intermediate states. This because of zero compression,
     * which means that the following address 1:0:0:0:0:0:0:1 can be written as
     * 1::1. Not to mention more exotic varieties such as 1:0:0:0:0:0:0:0 1:: or
     * 0:0:0:0:0:0:0:0 ::
     * The first intermediate state that can exit is 2, since even the smallest of all address('::')
     * will have at least two ':'.
     * Another exotic case is 'X:0:0:Y:0:0:0:Z' which becomes 'X::Y:0:0:0:Z' or X:0:0:Y::Z because the symbol '::'
     * can appear only once in a given address.
     */
    if ((state == 2) || (state == 3) || (state == 4) || (state == 5) || (state == 6))
    {
        /*
         * We check first for non-closed brackets.
         * Then we check if there is a number that has not been added to our array.
         * Finally we move to zero compression.
         */
        if (bracket_expected)
        {
            return -1;
        }
        unsorted_sixteen[unsorted_pointer] = sixteen;
        ++unsorted_pointer;
        if (zero_compression)
        {
            /*
             * If there is no address, then we can just return :-)
             */
            if (address)
            {
                /*
                 * We need to find the rightful positions for those numbers.
                 * We use a simple trick:
                 * We know how many unsorted addresses we have from unsorted pointer,
                 * and we know that once zero_compression is activated we do not fill
                 * any more numbers to the address structure. Therefore the right way
                 * to do this is to take the array of unsorted_sixteen and start assigning
                 * numbers backwards.
                 */
                int i = 0;
                for (i = 0; i < unsorted_pointer; ++i)
                {
                    address->sixteen[7 - i] = unsorted_sixteen[unsorted_pointer - i - 1];
                }
            }
        }
        else
        {
            /*
             * We cannot end up here without zero compression, or an error.
             */
            return -1;
        }
    }
    if (state == 7)
    {
        /*
         * This state corresponds to the final state of a simple ipv6 address, i.e.
         * xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx
         * We exited because we ran out of characters. Let's check that we have something
         * before assigning things.
         */
        if (char_counter == 0)
        {
            /*
             * The last field was empty, signal the error.
             */
            return -1;
        }
        if (bracket_expected)
        {
            /*
             * We were expecting a bracket but it never came, so this is an error.
             */
            return -1;
        }
        if (address)
        {
            address->sixteen[7] = sixteen;
        }
    }
    if (state == 8)
    {
        /*
         * This state corresponds to the final state if brackets were present.
         * We still need to check if zero compression was activated and copy
         * the values if so.
         */
        if (zero_compression)
        {
            /*
             * If there is no address, then we can just return :-)
             */
            if (address)
            {
                /*
                 * We need to find the rightful positions for those numbers.
                 * We use a simple trick:
                 * We know how many unsorted addresses we have from unsorted pointer,
                 * and we know that once zero_compression is activated we do not fill
                 * any more numbers to the address structure. Therefore the right way
                 * to do this is to take the array of unsorted_sixteen and start assigning
                 * numbers backwards.
                 */
                int i = 0;
                for (i = 0; i < unsorted_pointer; ++i)
                {
                    address->sixteen[7 - i] = unsorted_sixteen[i];
                }
            }
        }
    }
    if (state == 9)
    {
        /*
         * This state corresponds to the final state if we had brackets around us.
         * This is usually used to append a port number to the address, so we check
         * if we have a port and then assign it.
         */
        if (char_counter == 0)
        {
            /*
             * The last field was empty, signal the error.
             */
            return -1;
        }
        if (address)
        {
            address->port = port;
        }
    }
    if (state == 11)
    {
        /*
         * Error state
         */
        return -1;
    }
    return 0;
}