bool InsertUrlW(wchar_t * def, wchar_t * pBuf, size_t & index, size_t & filelength, size_t maxlength, const wchar_t * pUrl) { // Search for first occurrence of def in the buffer, starting at index. if (!FindPlaceholderW(def, pBuf, index, filelength)) { // No more matches found. return false; } // Replace the $WCURL$ string with the actual URL wchar_t * pBuild = pBuf + index; ptrdiff_t Expansion = wcslen(pUrl) - wcslen(def); if (Expansion < 0) { memmove(pBuild, pBuild - Expansion, (filelength - ((pBuild - Expansion) - pBuf))); } else if (Expansion > 0) { // Check for buffer overflow if (maxlength < Expansion + filelength) return false; memmove(pBuild + Expansion, pBuild, (filelength - (pBuild - pBuf))); } memmove(pBuild, pUrl, wcslen(pUrl)*sizeof(wchar_t)); filelength += Expansion*sizeof(wchar_t); return true; }
bool InsertTextW(wchar_t* def, wchar_t* pBuf, size_t& index, size_t& filelength, size_t maxlength, const std::string& Value) { // Search for first occurrence of def in the buffer, starting at index. if (!FindPlaceholderW(def, pBuf, index, filelength)) { // No more matches found. return false; } // Replace the $WCxxx$ string with the actual revision number wchar_t* pBuild = pBuf + index; ptrdiff_t Expansion = Value.length() - wcslen(def); if (Expansion < 0) memmove(pBuild, pBuild - Expansion, (filelength - ((pBuild - Expansion) - pBuf) * sizeof(wchar_t))); else if (Expansion > 0) { // Check for buffer overflow if (maxlength < Expansion * sizeof(wchar_t) + filelength) return false; memmove(pBuild + Expansion, pBuild, (filelength - (pBuild - pBuf) * sizeof(wchar_t))); } std::wstring wValue = CUnicodeUtils::StdGetUnicode(Value); memmove(pBuild, wValue.c_str(), wValue.length() * sizeof(wchar_t)); filelength += (Expansion * sizeof(wchar_t)); return true; }
bool InsertBooleanW(wchar_t* def, wchar_t* pBuf, size_t& index, size_t& filelength, BOOL isTrue) { // Search for first occurrence of def in the buffer, starting at index. if (!FindPlaceholderW(def, pBuf, index, filelength)) { // No more matches found. return false; } // Look for the terminating '$' character wchar_t* pBuild = pBuf + index; wchar_t* pEnd = pBuild + 1; while (*pEnd != L'$') { ++pEnd; if (pEnd - pBuf >= (__int64)filelength) return false; // No terminator - malformed so give up. } // Look for the ':' dividing TrueText from FalseText wchar_t *pSplit = pBuild + 1; // this loop is guaranteed to terminate due to test above. while (*pSplit != L':' && *pSplit != L'$') pSplit++; if (*pSplit == L'$') return false; // No split - malformed so give up. if (isTrue) { // Replace $WCxxx?TrueText:FalseText$ with TrueText // Remove :FalseText$ memmove(pSplit, pEnd + 1, (filelength - (pEnd + 1 - pBuf) * sizeof(wchar_t))); filelength -= ((pEnd + 1 - pSplit) * sizeof(wchar_t)); // Remove $WCxxx? size_t deflen = wcslen(def); memmove(pBuild, pBuild + deflen, (filelength - (pBuild + deflen - pBuf) * sizeof(wchar_t))); filelength -= (deflen * sizeof(wchar_t)); } else { // Replace $WCxxx?TrueText:FalseText$ with FalseText // Remove terminating $ memmove(pEnd, pEnd + 1, (filelength - (pEnd + 1 - pBuf) * sizeof(wchar_t))); filelength -= sizeof(wchar_t); // Remove $WCxxx?TrueText: memmove(pBuild, pSplit + 1, (filelength - (pSplit + 1 - pBuf) * sizeof(wchar_t))); filelength -= ((pSplit + 1 - pBuild) * sizeof(wchar_t)); } return true; }
bool InsertRevisionW(wchar_t* def, wchar_t* pBuf, size_t& index, size_t& filelength, size_t maxlength, GitWCRev_t* GitStat) { // Search for first occurrence of def in the buffer, starting at index. if (!FindPlaceholderW(def, pBuf, index, filelength)) { // No more matches found. return false; } size_t hashlen = strlen(GitStat->HeadHashReadable); ptrdiff_t exp = 0; if ((wcscmp(def, TEXT(VERDEFSHORT)) == 0)) { wchar_t format[1024] = { 0 }; wchar_t* pStart = pBuf + index + wcslen(def); wchar_t* pEnd = pStart; while (*pEnd != '$') { ++pEnd; if (((__int64)(pEnd - pBuf)) * ((__int64)sizeof(wchar_t)) >= (__int64)filelength) return false; // No terminator - malformed so give up. } if ((pEnd - pStart) > 1024) return false; // Format specifier too big exp = pEnd - pStart + 1; memcpy(format, pStart, (pEnd - pStart) * sizeof(wchar_t)); unsigned long number = wcstoul(format, nullptr, 0); if (wcscmp(def, TEXT(VERDEFSHORT)) == 0 && number < hashlen) hashlen = number; } // Replace the $WCxxx$ string with the actual revision number wchar_t* pBuild = pBuf + index; ptrdiff_t Expansion = hashlen - exp - wcslen(def); if (Expansion < 0) memmove(pBuild, pBuild - Expansion, (filelength - ((pBuild - Expansion) - pBuf) * sizeof(wchar_t))); else if (Expansion > 0) { // Check for buffer overflow if (maxlength < Expansion * sizeof(wchar_t) + filelength) return false; memmove(pBuild + Expansion, pBuild, (filelength - (pBuild - pBuf) * sizeof(wchar_t))); } std::wstring hash = CUnicodeUtils::StdGetUnicode(GitStat->HeadHashReadable); memmove(pBuild, hash.c_str(), hashlen * sizeof(wchar_t)); filelength += (Expansion * sizeof(wchar_t)); return true; }
bool InsertDateW(wchar_t* def, wchar_t* pBuf, size_t& index, size_t& filelength, size_t maxlength, __time64_t ttime) { // Search for first occurrence of def in the buffer, starting at index. if (!FindPlaceholderW(def, pBuf, index, filelength)) { // No more matches found. return false; } // Format the text to insert at the placeholder if (ttime == USE_TIME_NOW) _time64(&ttime); struct tm newtime; if (wcsstr(def, L"UTC")) { if (_gmtime64_s(&newtime, &ttime)) return false; } else { if (_localtime64_s(&newtime, &ttime)) return false; } wchar_t destbuf[1024]; wchar_t* pBuild = pBuf + index; ptrdiff_t Expansion; if ((wcscmp(def,TEXT(DATEWFMTDEF)) == 0) || (wcscmp(def,TEXT(NOWWFMTDEF)) == 0) || (wcscmp(def,TEXT(DATEWFMTDEFUTC)) == 0) || (wcscmp(def,TEXT(NOWWFMTDEFUTC)) == 0)) { // Format the date/time according to the supplied strftime format string wchar_t format[1024] = { 0 }; wchar_t* pStart = pBuf + index + wcslen(def); wchar_t* pEnd = pStart; while (*pEnd != '$') { ++pEnd; if (((__int64)(pEnd - pBuf))*((__int64)sizeof(wchar_t)) >= (__int64)filelength) return false; // No terminator - malformed so give up. } if ((pEnd - pStart) > 1024) return false; // Format specifier too big memcpy(format, pStart, (pEnd - pStart) * sizeof(wchar_t)); // to avoid wcsftime aborting if the user specified an invalid time format, // we set a custom invalid parameter handler that does nothing at all: // that makes wcsftime do nothing and set errno to EINVAL. // we restore the invalid parameter handler right after _invalid_parameter_handler oldHandler = _set_invalid_parameter_handler(_invalid_parameter_donothing); if (wcsftime(destbuf,1024,format,&newtime) == 0) { if (errno == EINVAL) wcscpy_s(destbuf, L"Invalid Time Format Specified"); } _set_invalid_parameter_handler(oldHandler); Expansion = wcslen(destbuf) - (wcslen(def) + pEnd - pStart + 1); } else { // Format the date/time in international format as yyyy/mm/dd hh:mm:ss swprintf_s(destbuf, L"%04d/%02d/%02d %02d:%02d:%02d", newtime.tm_year + 1900, newtime.tm_mon + 1, newtime.tm_mday, newtime.tm_hour, newtime.tm_min, newtime.tm_sec); Expansion = wcslen(destbuf) - wcslen(def); } // Replace the def string with the actual commit date if (Expansion < 0) memmove(pBuild, pBuild - Expansion, (filelength - ((pBuild - Expansion) - pBuf) * sizeof(wchar_t))); else if (Expansion > 0) { // Check for buffer overflow if (maxlength < Expansion * sizeof(wchar_t) + filelength) return false; memmove(pBuild + Expansion, pBuild, (filelength - (pBuild - pBuf) * sizeof(wchar_t))); } memmove(pBuild, destbuf, wcslen(destbuf) * sizeof(wchar_t)); filelength += Expansion * sizeof(wchar_t); return true; }
bool InsertNumberW(wchar_t* def, wchar_t* pBuf, size_t& index, size_t& filelength, size_t maxlength, size_t Value) { // Search for first occurrence of def in the buffer, starting at index. if (!FindPlaceholderW(def, pBuf, index, filelength)) { // No more matches found. return false; } ptrdiff_t exp = 0; if ((wcscmp(def, TEXT(VALDEFAND)) == 0) || (wcscmp(def, TEXT(VALDEFOFFSET1)) == 0) || (wcscmp(def, TEXT(VALDEFOFFSET2)) == 0)) { wchar_t format[1024] = { 0 }; wchar_t* pStart = pBuf + index + wcslen(def); wchar_t* pEnd = pStart; while (*pEnd != '$') { ++pEnd; if (((__int64)(pEnd - pBuf)) * ((__int64)sizeof(wchar_t)) >= (__int64)filelength) return false; // No terminator - malformed so give up. } if ((pEnd - pStart) > 1024) return false; // Format specifier too big exp = pEnd - pStart + 1; memcpy(format, pStart, (pEnd - pStart) * sizeof(wchar_t)); unsigned long number = wcstoul(format, NULL, 0); if (wcscmp(def, TEXT(VALDEFAND)) == 0) { if (Value != -1) Value &= number; } if (wcscmp(def, TEXT(VALDEFOFFSET1)) == 0) { if (Value != -1) Value -= number; } if (wcscmp(def, TEXT(VALDEFOFFSET2)) == 0) { if (Value != -1) Value += number; } } // Format the text to insert at the placeholder wchar_t destbuf[40]; swprintf_s(destbuf, L"%lld", Value); // Replace the $WCxxx$ string with the actual revision number wchar_t* pBuild = pBuf + index; ptrdiff_t Expansion = wcslen(destbuf) - exp - wcslen(def); if (Expansion < 0) memmove(pBuild, pBuild - Expansion, (filelength - ((pBuild - Expansion) - pBuf) * sizeof(wchar_t))); else if (Expansion > 0) { // Check for buffer overflow if (maxlength < Expansion * sizeof(wchar_t) + filelength) return false; memmove(pBuild + Expansion, pBuild, (filelength - (pBuild - pBuf) * sizeof(wchar_t))); } memmove(pBuild, destbuf, wcslen(destbuf) * sizeof(wchar_t)); filelength += (Expansion * sizeof(wchar_t)); return true; }
bool InsertRevisionW(wchar_t * def, wchar_t * pBuf, size_t & index, size_t & filelength, size_t maxlength, long MinRev, long MaxRev, SubWCRev_t * SubStat) { // Search for first occurrence of def in the buffer, starting at index. if (!FindPlaceholderW(def, pBuf, index, filelength)) { // No more matches found. return false; } ptrdiff_t exp = 0; if ((wcscmp(def,TEXT(VERDEFAND)) == 0) || (wcscmp(def,TEXT(VERDEFOFFSET1)) == 0) || (wcscmp(def,TEXT(VERDEFOFFSET2)) == 0)) { wchar_t format[1024]; wchar_t * pStart = pBuf + index + wcslen(def); wchar_t * pEnd = pStart; while (*pEnd != '$') { pEnd++; if (((__int64)(pEnd - pBuf))*((__int64)sizeof(wchar_t)) >= (__int64)filelength) return false; // No terminator - malformed so give up. } if ((pEnd - pStart) > 1024) { return false; // Format specifier too big } exp = pEnd - pStart + 1; SecureZeroMemory(format, sizeof(format)); memcpy(format,pStart,(pEnd - pStart)*sizeof(wchar_t)); unsigned long number = wcstoul(format, NULL, 0); if (wcscmp(def,TEXT(VERDEFAND)) == 0) { if (MinRev != -1) MinRev &= number; MaxRev &= number; } if (wcscmp(def,TEXT(VERDEFOFFSET1)) == 0) { if (MinRev != -1) MinRev -= number; MaxRev -= number; } if (wcscmp(def,TEXT(VERDEFOFFSET2)) == 0) { if (MinRev != -1) MinRev += number; MaxRev += number; } } // Format the text to insert at the placeholder wchar_t destbuf[40]; if (MinRev == -1 || MinRev == MaxRev) { if ((SubStat)&&(SubStat->bHexPlain)) swprintf_s(destbuf, L"%lX", MaxRev); else if ((SubStat)&&(SubStat->bHexX)) swprintf_s(destbuf, L"%#lX", MaxRev); else swprintf_s(destbuf, L"%ld", MaxRev); } else { if ((SubStat)&&(SubStat->bHexPlain)) swprintf_s(destbuf, L"%lX:%lX", MinRev, MaxRev); else if ((SubStat)&&(SubStat->bHexX)) swprintf_s(destbuf, L"%#lX:%#lX", MinRev, MaxRev); else swprintf_s(destbuf, L"%ld:%ld", MinRev, MaxRev); } // Replace the $WCxxx$ string with the actual revision number wchar_t * pBuild = pBuf + index; ptrdiff_t Expansion = wcslen(destbuf) - exp - wcslen(def); if (Expansion < 0) { memmove(pBuild, pBuild - Expansion, (filelength - ((pBuild - Expansion) - pBuf))); } else if (Expansion > 0) { // Check for buffer overflow if (maxlength < Expansion + filelength) return false; memmove(pBuild + Expansion, pBuild, (filelength - (pBuild - pBuf))); } memmove(pBuild, destbuf, wcslen(destbuf)*sizeof(wchar_t)); filelength += (Expansion*sizeof(wchar_t)); return true; }