Shmem::Shmem(IHadBetterBeIPDLCodeCallingThis_OtherwiseIAmADoodyhead, SharedMemory* aSegment, id_t aId) : mSegment(aSegment), mData(0), mSize(0) { NS_ABORT_IF_FALSE(mSegment, "NULL segment"); NS_ABORT_IF_FALSE(aId != 0, "invalid ID"); Unprotect(mSegment); Header* header; char* frontSentinel; char* data; char* backSentinel; GetSections(aSegment, &header, &frontSentinel, &data, &backSentinel); // do a quick validity check to avoid weird-looking crashes in libc char check = *frontSentinel; (void)check; NS_ABORT_IF_FALSE(!strncmp(header->mMagic, sMagic, sizeof(sMagic)), "invalid segment"); mSize = static_cast<size_t>(header->mSize); size_t pageSize = SharedMemory::SystemPageSize(); // transition into the "mapped" state by protecting the front and // back sentinels (which guard against buffer under/overflows) mSegment->Protect(frontSentinel, pageSize, RightsNone); mSegment->Protect(backSentinel, pageSize, RightsNone); // don't set these until we know they're valid mData = data; mId = aId; }
bool CIniFile::Sort(string FileName, bool Descending) { vector<CIniFile::Record> content; // Used to hold the sorted content vector<CIniFile::Record> sections = GetSections(FileName); // Get a list of Sections if(!sections.empty()) // Is there anything to process? { if(Descending) // Descending or Ascending? std::sort(sections.begin(), sections.end(), DescendingSectionSort()); else // Sort the Sections std::sort(sections.begin(), sections.end(), AscendingSectionSort()); for(vector<Record>::iterator iter = sections.begin(); iter < sections.end(); iter++) // For each Section { content.push_back(*iter); // Add the sorted Section to the content vector<CIniFile::Record> records = GetSection(iter->Section ,FileName); // Get a list of Records for this section if(Descending) // Descending or Ascending? std::sort(records.begin(), records.end(), DescendingRecordSort()); else // Sort the Records std::sort(records.begin(), records.end(), AscendingRecordSort()); for(vector<Record>::iterator it = records.begin(); it < records.end(); it++) // For each Record content.push_back(*it); // Add the sorted Record to the content } return Save(FileName,content); // Save } return false; // There were no sections }
static Header* GetHeader(Shmem::SharedMemory* aSegment) { Header* header; char* dontcare; GetSections(aSegment, &header, &dontcare, &dontcare, &dontcare); return header; }
std::shared_ptr<CSettingSection> CGUIDialogAddonSettings::GetSection() { const auto settingsManager = GetSettingsManager(); if (settingsManager == nullptr) return nullptr; const auto sections = settingsManager->GetSections(); if (!sections.empty()) return sections.front(); return nullptr; }
// static already_AddRefed<Shmem::SharedMemory> Shmem::Alloc(IHadBetterBeIPDLCodeCallingThis_OtherwiseIAmADoodyhead, size_t aNBytes, SharedMemoryType aType, bool aUnsafe, bool aProtect) { NS_ASSERTION(aNBytes <= UINT32_MAX, "Will truncate shmem segment size!"); MOZ_ASSERT(!aProtect || !aUnsafe, "protect => !unsafe"); size_t pageSize = SharedMemory::SystemPageSize(); nsRefPtr<SharedMemory> segment; // |2*pageSize| is for the front and back sentinel size_t segmentSize = SharedMemory::PageAlignedSize(aNBytes + 2*pageSize); if (aType == SharedMemory::TYPE_BASIC) segment = CreateSegment(segmentSize, SharedMemoryBasic::NULLHandle()); #ifdef MOZ_HAVE_SHAREDMEMORYSYSV else if (aType == SharedMemory::TYPE_SYSV) segment = CreateSegment(segmentSize, SharedMemorySysV::NULLHandle()); #endif else { NS_ERROR("unknown shmem type"); return nullptr; } if (!segment) return nullptr; Header* header; char *frontSentinel; char *data; char *backSentinel; GetSections(segment, &header, &frontSentinel, &data, &backSentinel); // initialize the segment with Shmem-internal information // NB: this can't be a static assert because technically pageSize // isn't known at compile time, event though in practice it's always // going to be 4KiB MOZ_ASSERT(sizeof(Header) <= pageSize, "Shmem::Header has gotten too big"); memcpy(header->mMagic, sMagic, sizeof(sMagic)); header->mSize = static_cast<uint32_t>(aNBytes); header->mUnsafe = aUnsafe; if (aProtect) Protect(segment); return segment.forget(); }
// static void Shmem::Dealloc(IHadBetterBeIPDLCodeCallingThis_OtherwiseIAmADoodyhead, SharedMemory* aSegment) { if (!aSegment) return; size_t pageSize = SharedMemory::SystemPageSize(); char *frontSentinel; char *data; char *backSentinel; GetSections(aSegment, &frontSentinel, &data, &backSentinel); aSegment->Protect(frontSentinel, pageSize, RightsWrite | RightsRead); Header* header = reinterpret_cast<Header*>(frontSentinel); memset(header->mMagic, 0, sizeof(sMagic)); header->mSize = 0; DestroySegment(aSegment); }
// static void Shmem::Dealloc(IHadBetterBeIPDLCodeCallingThis_OtherwiseIAmADoodyhead, SharedMemory* aSegment) { if (!aSegment) return; size_t pageSize = SharedMemory::SystemPageSize(); Header* header; char *frontSentinel; char *data; char *backSentinel; GetSections(aSegment, &header, &frontSentinel, &data, &backSentinel); aSegment->Protect(frontSentinel, pageSize, RightsWrite | RightsRead); memset(header->mMagic, 0, sizeof(sMagic)); header->mSize = 0; header->mUnsafe = false; // make it "safe" so as to catch errors DestroySegment(aSegment); }
// static Shmem::SharedMemory* Shmem::Alloc(IHadBetterBeIPDLCodeCallingThis_OtherwiseIAmADoodyhead, size_t aNBytes, SharedMemoryType aType, bool aProtect) { size_t pageSize = SharedMemory::SystemPageSize(); SharedMemory* segment = nsnull; // |2*pageSize| is for the front and back sentinel size_t segmentSize = PageAlignedSize(aNBytes + 2*pageSize); if (aType == SharedMemory::TYPE_BASIC) segment = CreateSegment(segmentSize, SharedMemoryBasic::NULLHandle()); #ifdef MOZ_HAVE_SHAREDMEMORYSYSV else if (aType == SharedMemory::TYPE_SYSV) segment = CreateSegment(segmentSize, SharedMemorySysV::NULLHandle()); #endif else NS_RUNTIMEABORT("unknown shmem type"); if (!segment) return 0; char *frontSentinel; char *data; char *backSentinel; GetSections(segment, &frontSentinel, &data, &backSentinel); // initialize the segment with Shmem-internal information Header* header = reinterpret_cast<Header*>(frontSentinel); memcpy(header->mMagic, sMagic, sizeof(sMagic)); header->mSize = aNBytes; if (aProtect) Protect(segment); return segment; }
BOOL DumpProcess(HANDLE hp) { ULONG64 base; ULONG64 next; MEMORY_BASIC_INFORMATION mbi; printf(" %12s %8s %8s: %3s %3s %4s %3s : %8s\n", "Address", "Offset", "Size", "Typ", "Sta", "Prot", "Ini", "Contents"); printf(" %12s %8s %8s: %3s %3s %4s %3s : %8s\n", "------------", "--------", "--------", "---", "---", "----", "---", "-----------------"); for (next = 0;;) { base = next; ZeroMemory(&mbi, sizeof(mbi)); if (VirtualQueryEx(hp, (PVOID)base, &mbi, sizeof(mbi)) == 0) { break; } if ((mbi.RegionSize & 0xfff) == 0xfff) { break; } next = (ULONG64)mbi.BaseAddress + mbi.RegionSize; if (mbi.State == MEM_FREE) { continue; } CHAR szType[16]; TypeToString(mbi.Type, szType, ARRAYSIZE(szType)); CHAR szState[16]; StateToString(mbi.State, szState, ARRAYSIZE(szState)); CHAR szProtect[16]; ProtectToString(mbi.Protect, szProtect, ARRAYSIZE(szProtect)); CHAR szAllocProtect[16]; ProtectToString(mbi.AllocationProtect, szAllocProtect, ARRAYSIZE(szAllocProtect)); CHAR szFile[MAX_PATH]; szFile[0] = '\0'; DWORD cb = 0; PCHAR pszFile = szFile; if (base == (ULONG64)mbi.AllocationBase) { #if 0 cb = pfGetMappedFileName(hp, (PVOID)mbi.AllocationBase, szFile, ARRAYSIZE(szFile)); #endif if (GetSections(hp, (PBYTE)mbi.AllocationBase)) { next = base + 0x1000; sprintf_s(szFile, ARRAYSIZE(szFile), "%d-bit PE", Bitness); } } if (cb > 0) { for (DWORD c = 0; c < cb; c++) { szFile[c] = (szFile[c] >= 'a' && szFile[c] <= 'z') ? szFile[c] - 'a' + 'A' : szFile[c]; } szFile[cb] = '\0'; } if ((pszFile = strrchr(szFile, '\\')) == NULL) { pszFile = szFile; } else { pszFile++; } PBYTE pbEnd; PCHAR pszSect = FindSectionName((PBYTE)base, pbEnd); if (pszSect != NULL) { pszFile = pszSect; if (next > (ULONG64)pbEnd) { next = (ULONG64)pbEnd; } } CHAR szDesc[128]; ZeroMemory(&szDesc, ARRAYSIZE(szDesc)); if (base == (ULONG64)mbi.AllocationBase) { sprintf_s(szDesc, ARRAYSIZE(szDesc), " %12I64x %8I64x %8I64x: %3s %3s %4s %3s : %s", (ULONG64)base, (ULONG64)base - (ULONG64)mbi.AllocationBase, (ULONG64)next - (ULONG64)base, szType, szState, szProtect, szAllocProtect, pszFile); } else { sprintf_s(szDesc, ARRAYSIZE(szDesc), " %12s %8I64x %8I64x: %3s %3s %4s %3s : %s", "-", (ULONG64)base - (ULONG64)mbi.AllocationBase, (ULONG64)next - (ULONG64)base, szType, szState, szProtect, szAllocProtect, pszFile); } printf("%s\n", szDesc); } return TRUE; }
BOOL SQLReadFileDSN( LPCSTR pszFileName, LPCSTR pszAppName, LPCSTR pszKeyName, LPSTR pszString, WORD nString, WORD *pnString ) { HINI hIni; int nBufPos = 0; char szValue[INI_MAX_PROPERTY_VALUE+1]; char szFileName[ODBC_FILENAME_MAX+1]; inst_logClear(); /* SANITY CHECKS */ if ( pszString == NULL || nString < 1 ) { inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_INVALID_BUFF_LEN, "" ); return FALSE; } if ( pszFileName == NULL && pszAppName == NULL && pszKeyName == NULL ) { inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "" ); return FALSE; } if ( pszAppName == NULL && pszKeyName != NULL ) { inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_INVALID_REQUEST_TYPE, "" ); return FALSE; } if ( pszFileName && strlen( pszFileName ) > ODBC_FILENAME_MAX ) { inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_INVALID_PATH, "" ); return FALSE; } *pszString = '\0'; /***************************************************** * GATHER ALL RELEVANT DSN INFORMATION INTO AN hIni *****************************************************/ if ( pszFileName && pszFileName[0] == '/' ) { strcpy( szFileName, pszFileName ); if ( strlen( szFileName ) < 4 || strcmp( szFileName + strlen( szFileName ) - 4, ".dsn" )) { strcat( szFileName, ".dsn" ); } /* on OS/2 the file DSN is a text file */ #ifdef __OS2__ if ( iniOpen( &hIni, (char*)szFileName, "#;", '[', ']', '=', TRUE, 0L ) != INI_SUCCESS ) #else if ( iniOpen( &hIni, (char*)szFileName, "#;", '[', ']', '=', TRUE ) != INI_SUCCESS ) #endif { inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_INVALID_PATH, "" ); return FALSE; } } else if ( pszFileName ) { char szPath[ODBC_FILENAME_MAX+1]; *szPath = '\0'; _odbcinst_FileINI( szPath ); sprintf( szFileName, "%s/%s", szPath, pszFileName ); if ( strlen( szFileName ) < 4 || strcmp( szFileName + strlen( szFileName ) - 4, ".dsn" )) { strcat( szFileName, ".dsn" ); } /* on OS/2 the file DSN is a text file */ #ifdef __OS2__ if ( iniOpen( &hIni, (char*) szFileName, "#;", '[', ']', '=', TRUE, 0L ) != INI_SUCCESS ) #else if ( iniOpen( &hIni, (char*) szFileName, "#;", '[', ']', '=', TRUE ) != INI_SUCCESS ) #endif { inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_INVALID_PATH, "" ); return FALSE; } } if ( pszAppName == NULL && pszKeyName == NULL ) { GetSections( hIni, pszString, nString ); } else if ( pszAppName != NULL && pszKeyName == NULL ) { GetEntries( hIni, pszAppName, pszString, nString ); } else { /* TRY TO GET THE ONE ITEM MATCHING Section & Entry */ if ( iniPropertySeek( hIni, (char *)pszAppName, (char *)pszKeyName, "" ) != INI_SUCCESS ) { inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_REQUEST_FAILED, "" ); return FALSE; } else { iniValue( hIni, szValue ); strncpy( pszString, szValue, nString ); pszString[ nString - 1 ] = '\0'; nBufPos = strlen( szValue ); } } if ( pszFileName ) { iniClose( hIni ); } if ( pnString ) { *pnString = strlen( pszString ); } return TRUE; }
ADDON_STATUS CAddonDll::TransferSettings() { bool restart = false; ADDON_STATUS reportStatus = ADDON_STATUS_OK; CLog::Log(LOGDEBUG, "Calling TransferSettings for: %s", Name().c_str()); LoadSettings(false); auto settings = GetSettings(); if (settings != nullptr) { for (auto section : settings->GetSections()) { for (auto category : section->GetCategories()) { for (auto group : category->GetGroups()) { for (auto setting : group->GetSettings()) { ADDON_STATUS status = ADDON_STATUS_OK; const char* id = setting->GetId().c_str(); switch (setting->GetType()) { case SettingType::Boolean: { bool tmp = std::static_pointer_cast<CSettingBool>(setting)->GetValue(); status = m_pDll->SetSetting(id, &tmp); break; } case SettingType::Integer: { int tmp = std::static_pointer_cast<CSettingInt>(setting)->GetValue(); status = m_pDll->SetSetting(id, &tmp); break; } case SettingType::Number: { float tmpf = static_cast<float>(std::static_pointer_cast<CSettingNumber>(setting)->GetValue()); status = m_pDll->SetSetting(id, &tmpf); break; } case SettingType::String: status = m_pDll->SetSetting(id, std::static_pointer_cast<CSettingString>(setting)->GetValue().c_str()); break; default: // log unknowns as an error, but go ahead and transfer the string CLog::Log(LOGERROR, "Unknown setting type of '%s' for %s", id, Name().c_str()); status = m_pDll->SetSetting(id, setting->ToString().c_str()); break; } if (status == ADDON_STATUS_NEED_RESTART) restart = true; else if (status != ADDON_STATUS_OK) reportStatus = status; } } } } } if (restart || reportStatus != ADDON_STATUS_OK) { new CAddonStatusHandler(ID(), restart ? ADDON_STATUS_NEED_RESTART : reportStatus, "", true); } return ADDON_STATUS_OK; }