vector<Frame> HexDiff::computeScore() { vector<Frame> frameSet; DNA tmpSet; DNAseq tmpSeq; vector<HexValue> hexValue; list<HexValue>::iterator it; vector<DNAseq>::iterator it2; double occ = 0.0; for (it2 = posSet.chain.begin(); it2 != posSet.chain.end(); it2++) { for (it = Hd.begin(); it != Hd.end(); ++it) { occ = computeOccurencies((*it2), it->getHex()); hexValue.push_back(HexValue(it->getHex(),(occ * it->getValue()))); } frameSet.push_back(Frame((*it2),hexValue,1)); hexValue.clear(); } for (it2 = negSet.chain.begin(); it2 != negSet.chain.end(); it2++) { for (it = Hd.begin(); it != Hd.end(); ++it) { occ = computeOccurencies((*it2), it->getHex()); hexValue.push_back(HexValue(it->getHex(),(occ * it->getValue()))); } frameSet.push_back(Frame(*(it2),hexValue,-1)); hexValue.clear(); } return frameSet; }
static BYTE GetByte(CString message, int index) { if (index*2+1 > message.GetLength() ) throw CPException(); TCHAR c1 = toupper(message.GetAt(index*2)); TCHAR c2 = toupper(message.GetAt(index*2+1)); return (HexValue(c1)*16) + HexValue(c2); }
int HexValue(char c0, char c1) { int nResult = -1; int n0 = HexValue(c0); if (n0 >= 0) { int n1 = HexValue(c0); if (n1 >= 0) { nResult = (n0<<4)+n1; } } return nResult; }
static int ValidateType (StrBuf* Type) /* Check if the given type is valid and if so, return a string id for it. If ** the type isn't valid, return -1. Type is overwritten when checking. */ { unsigned I; const char* A; char* B; /* The length must not be zero and divideable by two */ unsigned Length = SB_GetLen (Type); if (Length < 2 || (Length & 0x01) != 0) { ErrorSkip ("Type value has invalid length"); return -1; } /* The string must consist completely of hex digit chars */ A = SB_GetConstBuf (Type); for (I = 0; I < Length; ++I) { if (!IsXDigit (A[I])) { ErrorSkip ("Type value contains invalid characters"); return -1; } } /* Convert the type to binary */ B = SB_GetBuf (Type); while (A < SB_GetConstBuf (Type) + Length) { /* Since we know, there are only hex digits, there can't be any errors */ *B++ = (HexValue (A[0]) << 4) | HexValue (A[1]); A += 2; } Type->Len = (Length /= 2); /* Allocate the type and return it */ return GetStrBufId (Type); }
NMEA0183_BOOLEAN SENTENCE::IsChecksumBad( int checksum_field_number ) const { // ASSERT_VALID( this ); /* ** Checksums are optional, return TRUE if an existing checksum is known to be bad */ wxString checksum_in_sentence = Field( checksum_field_number ); if ( checksum_in_sentence == _T("") ) { return( Unknown0183 ); } if ( ComputeChecksum() != HexValue( checksum_in_sentence ) ) { return( NTrue ); } return( NFalse ); }
/// <summary> /// After having read a \ character in the input, decode the escape code following it. /// We recognize the following special C-style forms: /// /// \a - 7 /// \b - 8 /// \t - 9 /// \n - 10 /// \v - 11 /// \f - 12 /// \r - 13 /// \e - 27 /// \x** or \X** - Hex-encoded value, exactly one byte (one or two hex chars). /// \u** or \U** - Unicode value, multiple hex bytes, variable length (mod 2^32). If followed by a ';' the ';' will also be consumed. /// \### - Decimal-encoded value (and NOT octal-encoded), exactly one byte (at most three digits). /// \\ or \' or \" or \... - Special escape codes /// /// Among the "special" escape codes, you may potentially escape any character, if 'allowUnknowns' /// is enabled; however, under no circumstances may you escape control codes in the range of 0-31, /// and a backslash before a high-valued character escapes the full UTF-8 code point (potentially /// up to four bytes). /// </summary> /// <param name="input">A pointer to a pointer to the current source bytes.</param> /// <param name="end">A pointer to the end of the input (one past the last valid character).</param> /// <param name="allowUnknowns">Whether to allow unknown escape characters as legitimate input that /// yield themselves (True), or whether to reject unknown escape codes as garbage (False).</param> /// <returns>Returns a decoded character.</returns> SMILE_API_FUNC Int Lexer_DecodeEscapeCode(const Byte **input, const Byte *end, Bool allowUnknowns) { const Byte *src = *input; Byte ch; Int b1, b2; UInt value; // If there's no input, give up. if (src >= end) return -1; switch (ch = *src++) { // Handle the common named escapes: a, b, t, n, v, f, r, and e. case 'a': *input = src; return '\x07'; case 'b': *input = src; return '\x08'; case 't': *input = src; return '\x09'; case 'n': *input = src; return '\x0A'; case 'v': *input = src; return '\x0B'; case 'f': *input = src; return '\x0C'; case 'r': *input = src; return '\x0D'; case 'e': *input = src; return '\x1E'; // Handle the special escapes: ', ", and \, among others. case '\'': case '\"': case '\\': *input = src; return ch; // Handle hex forms, which start with \x or \X followed by one or two hexadecimal characters. case 'x': case 'X': { // At least one hex digit is required. if (src >= end || (b1 = HexValue(*src)) == -1) { *input = src; return -1; } src++; // One more digit is optional. if (src < end && (b2 = HexValue(*src)) >= 0) { src++; value = (UInt)((b1 << 4) | b2); } else { value = (UInt)b1; } *input = src; return value; } // Handle Unicode forms, which start with \u or \U followed by one or more hexadecimal // characters. If the hex characters are followed by ';', it will also be consumed, allowing // the character to be safely separated from any succeeding characters. Any value larger // than 2^32 will be treated as mod 2^32. Any resulting value larger than 0x110000 (the largest // representable Unicode character) will be returned as -1. case 'u': case 'U': { // At least one hex digit is required. if (src >= end || (b1 = HexValue(*src)) == -1) { *input = src; return -1; } src++; // Consume hex digits until we run out of them. value = b1; while (src < end && (b2 = HexValue(*src)) >= 0) { src++; value <<= 4; value |= b2; } // If there is a trailing ';', consume that too. if (src < end && *src == ';') { src++; } *input = src; // Obey mod-2^32 semantics, as per the formal defintion. value &= 0xFFFFFFFF; // Disallow anything above 0x110000, as per the formal definition. return value < 0x110000 ? (Int)(UInt)value : (Int)-1; } // Handle decimal forms, which start with any decimal digit followed by at most two more decimal digits. // Values greater than 255 will be discarded (returned as -1). case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': { UInt value; // Consume the first digit. value = ch - '0'; // Consume an optional second digit. if (src < end && (ch = *src) >= '0' && ch <= '9') { src++; value *= 10; value += (ch - '0'); } // Consume an optional third digit. if (src < end && (ch = *src) >= '0' && ch <= '9') { src++; value *= 10; value += (ch - '0'); } *input = src; return value <= 255 ? (Int)value : (Int)-1; } // Everything else is (maybe) illegal, depending on the caller's opinion. default: if (!allowUnknowns || ch < 32) return -1; if (ch < 128) { // Escaped character in the ASCII range. *input = src; return ch; } else { // Escaped character in the Unicode higher-than-ASCII range. value = String_ExtractUnicodeCharacterInternal(&src, end); *input = src; return ch; } } }
PPDERROR BUFOBJ_CopyStringHex( PBUFOBJ pBufObj, PSTR pTo ) /*++ Routine Description: Copy string out of a buffer object and treat its contents as a mix of normal characters and hex-decimal digits. Arguments: pBufObj - pointer to buffer object pTo - pointer to destination character buffer Return Value: PPDERR_NONE - string was successfully copied PPDERR_SYNTAX - invalid hex-decimal string [Note:] Since we use the null character to as a string terminator, it cannot appear as embedded hex string. Otherwise, the string will be terminated prematurely. --*/ { PSTR pFrom = pBufObj->pBuffer; char ch; BOOL bHexMode = FALSE; // Go through the source string one character at a time while ((ch = *pFrom++) != '\0') { if (bHexMode) { // Currently in hex mode if (ch == HEXEND_CHAR) { // Get out of hex mode when we see a '>' bHexMode = FALSE; } else if (IsHexChar(ch) && IsHexChar(*pFrom)) { // Convert two hex digits into a single byte ASSERT(ch != '0' || *pFrom != '0'); *pTo++ = (HexValue(ch) << 4) | HexValue(*pFrom); pFrom++; } else { // illegal hex-decimal digits DBGMSG(DBG_LEVEL_ERROR, "Invalid hex digits.\n"); return PPDERR_SYNTAX; } } else { // Currently not in hex mode if (ch == HEXBEGIN_CHAR) { // Get into hex mode when we see a '<' bHexMode = TRUE; } else { // Copy normal character *pTo++ = ch; } } } // Null-terminate the destination buffer *pTo = '\0'; // Return success status code return PPDERR_NONE; }
WCHAR CLexer::ScanUnicodeEscape (PCWSTR &p, __out PWCH pchSurrogate, BOOL fPeek) { ASSERT(pchSurrogate); *pchSurrogate = 0; PCWSTR pszStart = p - 1; // Back-up to the '\' ASSERT(*pszStart == '\\'); WCHAR ch = *p++; if (ch == 'U') { unsigned int uChar = 0; if (!IsHexDigit (*p)) { if (!fPeek) ErrorAtPosition (m_iCurLine, (long)(pszStart - m_pszCurLine), (long)(p - pszStart), ERR_IllegalEscape); } else { for (int i=0; i<8; i++) { if (!IsHexDigit (*p)) { if (!fPeek) ErrorAtPosition (m_iCurLine, (long)(pszStart - m_pszCurLine), (long)(p - pszStart), ERR_IllegalEscape); break; } uChar = (uChar << 4) + HexValue (*p++); } if (uChar < 0x00010000) { ch = (WCHAR)uChar; *pchSurrogate = L'\0'; } else if (uChar > 0x0010FFFF) { if (!fPeek) ErrorAtPosition (m_iCurLine, (long)(pszStart - m_pszCurLine), (long)(p - pszStart), ERR_IllegalEscape); } else { ASSERT(uChar > 0x0000FFFF && uChar <= 0x0010FFFF); ch = (WCHAR)((uChar - 0x00010000) / 0x0400 + 0xD800); *pchSurrogate = (WCHAR)((uChar - 0x00010000) % 0x0400 + 0xDC00); } } } else { ASSERT(ch == L'u' || ch == L'x'); int iChar = 0; if (!IsHexDigit (*p)) { if (!fPeek) ErrorAtPosition (m_iCurLine, (long)(pszStart - m_pszCurLine), (long)(p - pszStart), ERR_IllegalEscape); } else { for (int i=0; i<4; i++) { if (!IsHexDigit (*p)) { if (ch == 'u' && !fPeek) ErrorAtPosition (m_iCurLine, (long)(pszStart - m_pszCurLine), (long)(p - pszStart), ERR_IllegalEscape); break; } iChar = (iChar << 4) + HexValue (*p++); } ch = (WCHAR)iChar; } } return ch; }
BOOL CMlsPropertyString::AsColor(int nIndex, COLORREF& clColor, COLORREF clDefault /*=RGB(0,0,0)*/) { BOOL fResult = FALSE; clColor = clDefault; CString csColor; int i; TRY { if ((nIndex >= 0) && (nIndex < Count())) { // default is black csColor = m_PropertyStrings[nIndex]; if (!csColor.IsEmpty()) { // check for specific color: $RRGGBB if (csColor[0] == '$') { if (csColor.GetLength() == 7) { int nRed = HexValue(csColor[1], csColor[2]); int nGreen = HexValue(csColor[3], csColor[4]); int nBlue = HexValue(csColor[5], csColor[6]); if ((nRed >= 0) && (nGreen >= 0) && (nBlue >= 0)) { clColor = RGB(nRed, nGreen, nBlue); fResult = TRUE; } } } else { // check for named color typedef struct { LPCSTR pszName; COLORREF clColor; } NamedColor; static NamedColor NamedColors[] = { { "Black", RGB( 0, 0, 0) }, { "Maroon", RGB(128, 0, 0) }, { "Green", RGB( 0,128, 0) }, { "Olive", RGB(128, 0, 0) }, { "Navy", RGB( 0, 0,128) }, { "Purple", RGB(128, 0,128) }, { "Teal", RGB( 0,128,128) }, { "Gray", RGB(128,128,128) }, { "Silver", RGB(192,192,192) }, { "Red", RGB(255, 0, 0) }, { "Lime", RGB( 0,255, 0) }, { "Yellow", RGB(255,255, 0) }, { "Blue", RGB( 0, 0,255) }, { "Fuchsia", RGB(255, 0,255) }, { "Aqua", RGB( 0,255,255) }, { "LtGray", RGB(192,192,192) }, { "DkGray", RGB(128,128,128) }, { "White", RGB(255,255,255) } }; for (i = 0; i < sizeof(NamedColors)/sizeof(NamedColors[0]); i++) { if (stricmp(NamedColors[i].pszName, csColor) == 0) { clColor = NamedColors[i].clColor; fResult = TRUE; break; } } if (!fResult) { // check for system color typedef struct { LPCSTR pszName; int nColor; } SystemColor; static SystemColor SystemColors[] = { { "ScrollBar", COLOR_SCROLLBAR }, { "Background", COLOR_BACKGROUND }, { "ActiveCaption", COLOR_ACTIVECAPTION }, { "InactiveCaption", COLOR_INACTIVECAPTION }, { "Menu", COLOR_MENU }, { "Window", COLOR_WINDOW }, { "WindowFrame", COLOR_WINDOWFRAME }, { "MenuText", COLOR_MENUTEXT }, { "WindowText", COLOR_WINDOWTEXT }, { "CaptionText", COLOR_CAPTIONTEXT }, { "ActiveBorder", COLOR_ACTIVEBORDER }, { "InactiveBorder", COLOR_INACTIVEBORDER }, { "AppWorkSpace", COLOR_APPWORKSPACE }, { "Highlight", COLOR_HIGHLIGHT }, { "HighlightText", COLOR_HIGHLIGHTTEXT }, { "BtnFace", COLOR_BTNFACE }, { "BtnShadow", COLOR_BTNSHADOW }, { "GrayText", COLOR_GRAYTEXT }, { "BtnText", COLOR_BTNTEXT }, { "InactiveCaptionText", COLOR_INACTIVECAPTIONTEXT }, { "BtnHighlight", COLOR_BTNHIGHLIGHT } }; for (i = 0; i < sizeof(SystemColors)/sizeof(SystemColors[0]); i++) { if (stricmp(SystemColors[i].pszName, csColor) == 0) { clColor = GetSysColor(SystemColors[i].nColor); fResult = TRUE; break; } } } } } } } CATCH_ALL(e) { clColor = clDefault; fResult = FALSE; } END_CATCH_ALL return fResult; }
// Similar to wcstoul, but returns a 64-bit number, and always assumes base is 0 // static ULONGLONG ConsoleArgs::wcstoul64( LPCWSTR nptr, LPCWSTR * endptr) { unsigned __int64 val = 0; // accumulator unsigned __int64 maxval, maxdigval; // some limits const WCHAR *p = nptr; // scanning/peek pointer unsigned ibase; unsigned digval; WCHAR c; // current char bool fNegated = false; bool fHadDigits = false; bool fOverflow = false; if (endptr != NULL) *endptr = (WCHAR*)nptr; errno = 0; c = *p++; while(iswspace(c)) c = *p++; if (c == L'+') c = *p++; else if (*nptr == L'-') { fNegated = true; c = *p++; } if (c == L'0') { if (*p == L'x' || *p == L'X') { // Hex ++p; c = *p++; ibase = 16; maxval = UI64(0xFFFFFFFFFFFFFFFF) / 16; } else { // Octal ibase = 8; maxval = UI64(0xFFFFFFFFFFFFFFFF) / 8; } } else { // Decimal ibase = 10; } maxval = UI64(0xFFFFFFFFFFFFFFFF) / ibase; maxdigval = UI64(0xFFFFFFFFFFFFFFFF) % ibase; for (;;) { if (c > 0xFF || !isxdigit((char)c) || (digval = HexValue(c)) >= ibase) break; fHadDigits = true; if (!fOverflow && (val < maxval || (val == maxval && digval <= maxdigval))) val = val * ibase + digval; else { fOverflow = true; if (endptr == NULL) { /* no need to keep on parsing if we don't have to return the endptr. */ break; } } c = *p++; } --p; /* point to place that stopped scan */ if (!fHadDigits) { /* no number there; return 0 and point to beginning of string */ if (endptr) /* store beginning of string in endptr later on */ p = nptr; ASSERT(val == 0); } else if ( fOverflow ) { /* overflow occurred */ errno = ERANGE; val = UI64(0xFFFFFFFFFFFFFFFF); } if (endptr != NULL) /* store pointer to char that stopped the scan */ *endptr = (WCHAR*)p; if (fNegated) /* negate result if there was a neg sign */ val = (unsigned __int64)(-(__int64)val); return val; }