DWORD NextLong(void) { BYTE uc0, uc1, uc2, uc3; uc0 = NextByte(); uc1 = NextByte(); uc2 = NextByte(); uc3 = NextByte(); return uc0 + (uc1 << 8) + (uc2 << 16) + (uc3 << 24); }
void OutputStream::PutChar(WCHAR c) { if(m_bof) { m_bof = false; switch(m_encoding) { case utf8: case utf16le: case utf16be: PutChar(0xfeff); break; } } switch(m_encoding) { case utf8: if(0 <= c && c < 0x80) // 0xxxxxxx { NextByte(c); } else if(0x80 <= c && c < 0x800) // 110xxxxx 10xxxxxx { NextByte(0xc0 | ((c << 2) & 0x1f)); NextByte(0x80 | ((c << 0) & 0x3f)); } else if(0x800 <= c && c < 0xFFFF) // 1110xxxx 10xxxxxx 10xxxxxx { NextByte(0xe0 | ((c << 4) & 0x0f)); NextByte(0x80 | ((c << 2) & 0x3f)); NextByte(0x80 | ((c << 0) & 0x3f)); } else { NextByte('?'); } break; case utf16le: NextByte(c & 0xff); NextByte((c >> 8) & 0xff); break; case utf16be: NextByte((c >> 8) & 0xff); NextByte(c & 0xff); break; case wchar: NextByte(c); break; } }
void Random::NextBytes(MemoryByteData& outData) { byte* buffer = outData.MutableData(); size_t size = outData.Size(); FOR_EACH_SIZE(i, size) { buffer[i] = NextByte(); } }
ERRORCODE PSDDriver::ReadAChannel(LPBYTE pBuffer, int nChannel, int nChannels) { ERRORCODE error = ERRORCODE_None; ASSERT(m_ChannelFiles.GetSize() > nChannel); ASSERT(m_ChannelPositions.GetSize() > nChannel); ASSERT(m_ChannelLowPositions.GetSize() > nChannel); ReadOnlyFile* pFile = (ReadOnlyFile*)m_ChannelFiles.GetAt(nChannel); ST_DEV_POSITION lPosition = m_ChannelPositions.GetAt(nChannel); ST_DEV_POSITION lLowPosition = m_ChannelLowPositions.GetAt(nChannel); // If there is more than one file, we are potentially jumping around. // We need to establish the context for this data stream. if (lPosition == 0) { // Special value. Go to start. if ((error = pFile->seek(0, ST_DEV_SEEK_SET)) != ERRORCODE_None) { return error; } } else { // We need to seek. if (nChannels > 1) { // We have multiple channels going. The low-level file will have // moved. // Seek absolutely in the low-level file to restore what the // based file believes to be the current position. // Then we can seek in the higher-level file without confusion. if ((error = file.seek(lLowPosition, ST_DEV_SEEK_SET)) != ERRORCODE_None) { return error; } } // Seek to the right position. if ((error = pFile->seek(lPosition, ST_DEV_SEEK_SET)) != ERRORCODE_None) { return error; } } // Now we can read the data. int nBytes = m_nRowBytes; if (m_wCompression) { // We inline the PackBits code here since we need to do some special // stuff during the read. while (nBytes > 0) { char count; BYTE data; // Read this byte. if ((error = NextByte(pFile, (LPBYTE)&count)) != ERRORCODE_None) { return error; } if (count < 0) { /* Repeat count */ if ((error = NextByte(pFile, &data)) != ERRORCODE_None) { return error; } count = -count; while ((count >= 0) && (nBytes > 0)) { *pBuffer = data; pBuffer += nChannels; nBytes--; count--; } } else { /* Replace count. */ while ((count >= 0) && (nBytes > 0)) { if ((error = NextByte(pFile, &data)) != ERRORCODE_None) { return error; } *pBuffer = data; pBuffer += nChannels; nBytes--; count--; } } } } else { BYTE data; while (nBytes > 0) { if ((error = NextByte(pFile, &data)) != ERRORCODE_None) { return error; } *pBuffer = data; pBuffer += nChannels; nBytes--; } } // Remember our positions. pFile->tell(&lPosition); m_ChannelPositions.SetAt(nChannel, lPosition); if (nChannels > 1) { // Remember the low-level file position also. file.tell(&lLowPosition); m_ChannelLowPositions.SetAt(nChannel, lLowPosition); } return ERRORCODE_None; }
static int MatchText(char *t, int tlen, char *p, int plen, pg_locale_t locale, bool locale_is_c) { /* Fast path for match-everything pattern */ if (plen == 1 && *p == '%') return LIKE_TRUE; /* * In this loop, we advance by char when matching wildcards (and thus on * recursive entry to this function we are properly char-synced). On other * occasions it is safe to advance by byte, as the text and pattern will * be in lockstep. This allows us to perform all comparisons between the * text and pattern on a byte by byte basis, even for multi-byte * encodings. */ while (tlen > 0 && plen > 0) { if (*p == '\\') { /* Next pattern byte must match literally, whatever it is */ NextByte(p, plen); /* ... and there had better be one, per SQL standard */ if (plen <= 0) ereport(ERROR, (errcode(ERRCODE_INVALID_ESCAPE_SEQUENCE), errmsg("LIKE pattern must not end with escape character"))); if (GETCHAR(*p) != GETCHAR(*t)) return LIKE_FALSE; } else if (*p == '%') { char firstpat; /* * % processing is essentially a search for a text position at * which the remainder of the text matches the remainder of the * pattern, using a recursive call to check each potential match. * * If there are wildcards immediately following the %, we can skip * over them first, using the idea that any sequence of N _'s and * one or more %'s is equivalent to N _'s and one % (ie, it will * match any sequence of at least N text characters). In this way * we will always run the recursive search loop using a pattern * fragment that begins with a literal character-to-match, thereby * not recursing more than we have to. */ NextByte(p, plen); while (plen > 0) { if (*p == '%') NextByte(p, plen); else if (*p == '_') { /* If not enough text left to match the pattern, ABORT */ if (tlen <= 0) return LIKE_ABORT; NextChar(t, tlen); NextByte(p, plen); } else break; /* Reached a non-wildcard pattern char */ } /* * If we're at end of pattern, match: we have a trailing % which * matches any remaining text string. */ if (plen <= 0) return LIKE_TRUE; /* * Otherwise, scan for a text position at which we can match the * rest of the pattern. The first remaining pattern char is known * to be a regular or escaped literal character, so we can compare * the first pattern byte to each text byte to avoid recursing * more than we have to. This fact also guarantees that we don't * have to consider a match to the zero-length substring at the * end of the text. */ if (*p == '\\') { if (plen < 2) ereport(ERROR, (errcode(ERRCODE_INVALID_ESCAPE_SEQUENCE), errmsg("LIKE pattern must not end with escape character"))); firstpat = GETCHAR(p[1]); } else firstpat = GETCHAR(*p); while (tlen > 0) { if (GETCHAR(*t) == firstpat) { int matched = MatchText(t, tlen, p, plen, locale, locale_is_c); if (matched != LIKE_FALSE) return matched; /* TRUE or ABORT */ } NextChar(t, tlen); } /* * End of text with no match, so no point in trying later places * to start matching this pattern. */ return LIKE_ABORT; } else if (*p == '_') { /* _ matches any single character, and we know there is one */ NextChar(t, tlen); NextByte(p, plen); continue; } else if (GETCHAR(*p) != GETCHAR(*t)) { /* non-wildcard pattern char fails to match text char */ return LIKE_FALSE; } /* * Pattern and text match, so advance. * * It is safe to use NextByte instead of NextChar here, even for * multi-byte character sets, because we are not following immediately * after a wildcard character. If we are in the middle of a multibyte * character, we must already have matched at least one byte of the * character from both text and pattern; so we cannot get out-of-sync * on character boundaries. And we know that no backend-legal * encoding allows ASCII characters such as '%' to appear as non-first * bytes of characters, so we won't mistakenly detect a new___ wildcard. */ NextByte(t, tlen); NextByte(p, plen); } if (tlen > 0) return LIKE_FALSE; /* end of pattern, but not of text */ /* * End of text, but perhaps not of pattern. Match iff the remaining * pattern can match a zero-length string, ie, it's zero or more %'s. */ while (plen > 0 && *p == '%') NextByte(p, plen); if (plen <= 0) return LIKE_TRUE; /* * End of text with no match, so no point in trying later places to start * matching this pattern. */ return LIKE_ABORT; } /* MatchText() */
int InputStream::NextChar() { if(m_encoding == none) { m_encoding = unknown; switch(NextByte()) { case 0xef: if(NextByte() == 0xbb && NextByte() == 0xbf) m_encoding = utf8; break; case 0xff: if(NextByte() == 0xfe) m_encoding = utf16le; break; case 0xfe: if(NextByte() == 0xff) m_encoding = utf16be; break; } } if(m_encoding == unknown) { throw Exception(_T("unknown character encoding, missing BOM")); } int i, c; int cur = NextByte(); switch(m_encoding) { case utf8: for(i = 7; i >= 0 && (cur & (1 << i)); i--); cur &= (1 << i) - 1; while(++i < 7) { c = NextByte(); if(c == EOS) { cur = EOS; break; } cur = (cur << 6) | (c & 0x3f); } break; case utf16le: c = NextByte(); if(c == EOS) { cur = EOS; break; } cur = (c << 8) | cur; break; case utf16be: c = NextByte(); if(c == EOS) { cur = EOS; break; } cur = cur | (c << 8); break; case wchar: break; } return cur; }
static int MatchText(char *t, int tlen, char *p, int plen) { /* Fast path for match-everything pattern */ if ((plen == 1) && (*p == '%')) return LIKE_TRUE; /* * In this loop, we advance by char when matching wildcards (and thus on * recursive entry to this function we are properly char-synced). On other * occasions it is safe to advance by byte, as the text and pattern will * be in lockstep. This allows us to perform all comparisons between the * text and pattern on a byte by byte basis, even for multi-byte * encodings. */ while ((tlen > 0) && (plen > 0)) { if (*p == '\\') { /* Next byte must match literally, whatever it is */ NextByte(p, plen); if ((plen <= 0) || TCHAR(*p) != TCHAR(*t)) return LIKE_FALSE; } else if (*p == '%') { /* * % processing is essentially a search for a match for what * follows the %, plus a recursive match of the remainder. We * succeed if and only if both conditions are met. */ /* %% is the same as % according to the SQL standard */ /* Advance past all %'s */ while ((plen > 0) && (*p == '%')) NextByte(p, plen); /* Trailing percent matches everything. */ if (plen <= 0) return LIKE_TRUE; /* * Otherwise, scan for a text position at which we can match the * rest of the pattern. */ if (*p == '_') { /* %_ is the same as _% - avoid matching _ repeatedly */ NextChar(t, tlen); NextByte(p, plen); if (tlen <= 0) { return (plen <= 0) ? LIKE_TRUE : LIKE_ABORT; } else if (plen <= 0) { return LIKE_FALSE; } while (tlen > 0) { int matched = MatchText(t, tlen, p, plen); if (matched != LIKE_FALSE) return matched; /* TRUE or ABORT */ NextChar(t, tlen); } } else { char firstpat = TCHAR(*p); if (*p == '\\') { if (plen < 2) return LIKE_FALSE; firstpat = TCHAR(p[1]); } while (tlen > 0) { /* * Optimization to prevent most recursion: don't recurse * unless first pattern byte matches first text byte. */ if (TCHAR(*t) == firstpat) { int matched = MatchText(t, tlen, p, plen); if (matched != LIKE_FALSE) return matched; /* TRUE or ABORT */ } NextChar(t, tlen); } } /* * End of text with no match, so no point in trying later places * to start matching this pattern. */ return LIKE_ABORT; } else if (*p == '_') { NextChar(t, tlen); NextByte(p, plen); continue; } else if (TCHAR(*t) != TCHAR(*p)) { /* * Not the single-character wildcard and no explicit match? Then * time to quit... */ return LIKE_FALSE; } /* * It is safe to use NextByte instead of NextChar here, even for * multi-byte character sets, because we are not following immediately * after a wildcard character. If we are in the middle of a multibyte * character, we must already have matched at least one byte of the * character from both text and pattern; so we cannot get out-of-sync * on character boundaries. And we know that no backend-legal * encoding allows ASCII characters such as '%' to appear as non-first * bytes of characters, so we won't mistakenly detect a new wildcard. */ NextByte(t, tlen); NextByte(p, plen); } if (tlen > 0) return LIKE_FALSE; /* end of pattern, but not of text */ /* End of input string. Do we have matching pattern remaining? */ while ((plen > 0) && (*p == '%')) /* allow multiple %'s at end of * pattern */ NextByte(p, plen); if (plen <= 0) return LIKE_TRUE; /* * End of text with no match, so no point in trying later places to start * matching this pattern. */ return LIKE_ABORT; } /* MatchText() */
static int MatchText(const char *t, size_t tlen, const char *p, size_t plen, List **params) { while (tlen > 0 && plen > 0) { if (plen < 2 || *p != '%') { /* non-wildcard pattern char fails to match text char */ if (*p != *t) return LIKE_FALSE; } else if (p[1] == '%') { /* %% is % */ NextByte(p, plen); if (*p != *t) return LIKE_FALSE; } else { const char *begin = p; const char *w = t; char firstpat; /* Skip until the type specifer */ p = strpbrk(begin + 1, "diouxXeEfFgGaAcspm"); if (p == NULL) return LIKE_FALSE; /* bad format */ p++; plen -= p - begin; if (plen <= 0) { if (params) *params = lcons(strdup_with_len(t, tlen), *params); return LIKE_TRUE; /* matches everything. */ } /* * Otherwise, scan for a text position at which we can match the * rest of the pattern. */ firstpat = *p; while (tlen > 0) { /* * Optimization to prevent most recursion: don't recurse * unless first pattern byte matches first text byte. */ if (*t == firstpat) { int matched = MatchText(t, tlen, p, plen, params); if (matched == LIKE_TRUE && params) *params = lcons(strdup_with_len(w, t - w), *params); if (matched != LIKE_FALSE) return matched; /* TRUE or ABORT */ } NextChar(t, tlen); } /* * End of text with no match, so no point in trying later places * to start matching this pattern. */ return LIKE_ABORT; } NextByte(t, tlen); NextByte(p, plen); } if (tlen > 0) return LIKE_FALSE; /* end of pattern, but not of text */ /* End of text string. Do we have matching pattern remaining? */ while (plen > 0 && *p == '%') { const char *begin = p; p = strpbrk(begin + 1, "diouxXeEfFgGaAcspm"); if (p == NULL) return LIKE_FALSE; /* bad format */ p++; plen -= p - begin; } if (plen <= 0) return LIKE_TRUE; /* * End of text with no match, so no point in trying later places to start * matching this pattern. */ return LIKE_ABORT; }
gcc_hot static void ForVertical(pointer_type p, unsigned pitch, unsigned n, F f) { for (; n > 0; --n, p = NextByte(p, pitch)) f(p); }
static constexpr const_pointer_type NextRow(const_pointer_type p, unsigned pitch, int delta) { return NextByte(p, int(pitch) * delta); }
bool StringFunctions::Like(UNUSED_ATTRIBUTE executor::ExecutorContext &ctx, const char *t, uint32_t tlen, const char *p, uint32_t plen) { PELOTON_ASSERT(t != nullptr); PELOTON_ASSERT(p != nullptr); if (plen == 1 && *p == '%') return true; while (tlen > 0 && plen > 0) { if (*p == '\\') { NextByte(p, plen); if (plen <= 0) return false; if (tolower(*p) != tolower(*t)) return false; } else if (*p == '%') { char firstpat; NextByte(p, plen); while (plen > 0) { if (*p == '%') NextByte(p, plen); else if (*p == '_') { if (tlen <= 0) return false; NextByte(t, tlen); NextByte(p, plen); } else break; } if (plen <= 0) return true; if (*p == '\\') { if (plen < 2) return false; firstpat = tolower(p[1]); } else firstpat = tolower(*p); while (tlen > 0) { if (tolower(*t) == firstpat) { int matched = Like(ctx, t, tlen, p, plen); if (matched != false) return matched; } NextByte(t, tlen); } return false; } else if (*p == '_') { NextByte(t, tlen); NextByte(p, plen); continue; } else if (tolower(*p) != tolower(*t)) { return false; } NextByte(t, tlen); NextByte(p, plen); } if (tlen > 0) return false; while (plen > 0 && *p == '%') NextByte(p, plen); if (plen <= 0) return true; return false; }