bool CWebServer::LoadCert(std::string &skey, std::string &scert) { XFILE::CFile file; XFILE::auto_buffer buf; const char* keyFile = "special://userdata/server.key"; const char* certFile = "special://userdata/server.pem"; if (!file.Exists(keyFile) || !file.Exists(certFile)) return false; if (file.LoadFile(keyFile, buf) > 0) { skey.resize(buf.length()); skey.assign(buf.get()); file.Close(); } else CLog::Log(LOGDEBUG, "WebServer %s: Error loading: %s", __FUNCTION__, keyFile); if (file.LoadFile(certFile, buf) > 0) { scert.resize(buf.length()); scert.assign(buf.get()); file.Close(); } else CLog::Log(LOGDEBUG, "WebServer %s: Error loading: %s", __FUNCTION__, certFile); if (!skey.empty() && !scert.empty()) { CLog::Log(LOGERROR, "WebServer %s: found server key: %s, certificate: %s, HTTPS support enabled", __FUNCTION__, keyFile, certFile); return true; } return false; }
bool CPODocument::LoadFile(const std::string &pofilename) { CURL poFileUrl(pofilename); if (!XFILE::CFile::Exists(poFileUrl)) return false; XFILE::CFile file; XFILE::auto_buffer buf; if (file.LoadFile(poFileUrl, buf) < 18) // at least a size of a minimalistic header { CLog::Log(LOGERROR, "%s: can't load file \"%s\" or file is too small", __FUNCTION__, pofilename.c_str()); return false; } m_strBuffer = '\n'; m_strBuffer.append(buf.get(), buf.size()); buf.clear(); ConvertLineEnds(pofilename); // we make sure, to have an LF at the end of buffer if (*m_strBuffer.rbegin() != '\n') { m_strBuffer += "\n"; } m_POfilelength = m_strBuffer.size(); if (GetNextEntry() && m_Entry.Type == MSGID_FOUND) return true; CLog::Log(LOGERROR, "POParser: unable to read PO file header from file: %s", pofilename.c_str()); return false; }
bool CXBMCTinyXML::LoadFile(const std::string& _filename, TiXmlEncoding encoding) { value = _filename.c_str(); XFILE::CFile file; XFILE::auto_buffer buffer; if (file.LoadFile(value, buffer) <= 0) { SetError(TIXML_ERROR_OPENING_FILE, NULL, NULL, TIXML_ENCODING_UNKNOWN); return false; } // Delete the existing data: Clear(); location.Clear(); std::string data(buffer.get(), buffer.length()); buffer.clear(); // free memory early if (encoding == TIXML_ENCODING_UNKNOWN) Parse(data, file.GetContentCharset()); else Parse(data, encoding); if (Error()) return false; return true; }
bool CBaseTexture::LoadFromFileInternal(const std::string& texturePath, unsigned int maxWidth, unsigned int maxHeight, bool autoRotate, bool requirePixels, const std::string& strMimeType) { if (URIUtils::HasExtension(texturePath, ".dds")) { // special case for DDS images CDDSImage image; if (image.ReadFile(texturePath)) { Update(image.GetWidth(), image.GetHeight(), 0, image.GetFormat(), image.GetData(), false); return true; } return false; } unsigned int width = maxWidth ? std::min(maxWidth, g_Windowing.GetMaxTextureSize()) : g_Windowing.GetMaxTextureSize(); unsigned int height = maxHeight ? std::min(maxHeight, g_Windowing.GetMaxTextureSize()) : g_Windowing.GetMaxTextureSize(); // Read image into memory to use our vfs XFILE::CFile file; XFILE::auto_buffer buf; if (file.LoadFile(texturePath, buf) <= 0) return false; CURL url(texturePath); // make sure resource:// paths are properly resolved if (url.IsProtocol("resource")) { std::string translatedPath; if (XFILE::CResourceFile::TranslatePath(url, translatedPath)) url.Parse(translatedPath); } // handle xbt:// paths differently because it allows loading the texture directly from memory if (url.IsProtocol("xbt")) { XFILE::CXbtFile xbtFile; if (!xbtFile.Open(url)) return false; return LoadFromMemory(xbtFile.GetImageWidth(), xbtFile.GetImageHeight(), 0, xbtFile.GetImageFormat(), xbtFile.HasImageAlpha(), reinterpret_cast<unsigned char*>(buf.get())); } IImage* pImage; if(strMimeType.empty()) pImage = ImageFactory::CreateLoader(texturePath); else pImage = ImageFactory::CreateLoaderFromMimeType(strMimeType); if (!LoadIImage(pImage, (unsigned char *)buf.get(), buf.size(), width, height, autoRotate)) { CLog::Log(LOGDEBUG, "%s - Load of %s failed.", __FUNCTION__, texturePath.c_str()); delete pImage; return false; } delete pImage; return true; }
bool CKaraokeLyricsTextKAR::Load() { XFILE::CFile file; bool succeed = true; m_reportedInvalidVarField = false; // Clear the lyrics array clearLyrics(); if (file.LoadFile(m_midiFile, m_midiData) <= 0) return false; file.Close(); // Parse MIDI try { parseMIDI(); } catch ( const char * p ) { CLog::Log( LOGERROR, "KAR lyrics loader: cannot load file: %s", p ); succeed = false; } m_midiData.clear(); return succeed; }
unsigned int CFileUtils::LoadFile(const std::string &filename, void* &outputBuffer) { XFILE::auto_buffer buffer; XFILE::CFile file; const unsigned int total_read = file.LoadFile(filename, buffer); outputBuffer = buffer.detach(); return total_read; }
FT_Face GetFont(const std::string &filename, float size, float aspect, XUTILS::auto_buffer& memoryBuf) { // don't have it yet - create it if (!m_library) FT_Init_FreeType(&m_library); if (!m_library) { CLog::Log(LOGERROR, "Unable to initialize freetype library"); return NULL; } FT_Face face; // ok, now load the font face CURL realFile(CSpecialProtocol::TranslatePath(filename)); if (realFile.GetFileName().empty()) return NULL; memoryBuf.clear(); #ifndef TARGET_WINDOWS if (!realFile.GetProtocol().empty()) #endif // ! TARGET_WINDOWS { // load file into memory if it is not on local drive // in case of win32: always load file into memory as filename is in UTF-8, // but freetype expect filename in ANSI encoding XFILE::CFile f; if (f.LoadFile(realFile, memoryBuf) <= 0) return NULL; if (FT_New_Memory_Face(m_library, (const FT_Byte*)memoryBuf.get(), memoryBuf.size(), 0, &face) != 0) return NULL; } #ifndef TARGET_WINDOWS else if (FT_New_Face( m_library, realFile.GetFileName().c_str(), 0, &face )) return NULL; #endif // ! TARGET_WINDOWS unsigned int ydpi = 72; // 72 points to the inch is the freetype default unsigned int xdpi = (unsigned int)MathUtils::round_int(ydpi * aspect); // we set our screen res currently to 96dpi in both directions (windows default) // we cache our characters (for rendering speed) so it's probably // not a good idea to allow free scaling of fonts - rather, just // scaling to pixel ratio on screen perhaps? if (FT_Set_Char_Size( face, 0, (int)(size*64 + 0.5f), xdpi, ydpi )) { FT_Done_Face(face); return NULL; } return face; };
int CNfoFile::Load(const std::string& strFile) { Close(); XFILE::CFile file; XFILE::auto_buffer buf; if (file.LoadFile(strFile, buf) > 0) { m_doc.assign(buf.get(), buf.size()); m_headPos = 0; return 0; } m_doc.clear(); return 1; }
bool CJpegIO::Open(const CStdString &texturePath, unsigned int minx, unsigned int miny, bool read) { Close(); m_texturePath = texturePath; XFILE::CFile file; XFILE::auto_buffer buf; if (file.LoadFile(texturePath, buf) <= 0) return false; m_inputBuffSize = buf.size(); m_inputBuff = (unsigned char*)buf.detach(); return Read(m_inputBuff, m_inputBuffSize, minx, miny); }
bool CBaseTexture::LoadFromFileInternal(const std::string& texturePath, unsigned int maxWidth, unsigned int maxHeight, bool autoRotate, bool requirePixels, const std::string& strMimeType) { if (URIUtils::HasExtension(texturePath, ".dds")) { // special case for DDS images CDDSImage image; if (image.ReadFile(texturePath)) { Update(image.GetWidth(), image.GetHeight(), 0, image.GetFormat(), image.GetData(), false); return true; } return false; } unsigned int width = maxWidth ? std::min(maxWidth, g_Windowing.GetMaxTextureSize()) : g_Windowing.GetMaxTextureSize(); unsigned int height = maxHeight ? std::min(maxHeight, g_Windowing.GetMaxTextureSize()) : g_Windowing.GetMaxTextureSize(); // Read image into memory to use our vfs XFILE::CFile file; XFILE::auto_buffer buf; if (file.LoadFile(texturePath, buf) <= 0) return false; IImage* pImage; if(strMimeType.empty()) pImage = ImageFactory::CreateLoader(texturePath); else pImage = ImageFactory::CreateLoaderFromMimeType(strMimeType); if (!LoadIImage(pImage, (unsigned char *)buf.get(), buf.size(), width, height, autoRotate)) { delete pImage; pImage = ImageFactory::CreateFallbackLoader(texturePath); if (!LoadIImage(pImage, (unsigned char *)buf.get(), buf.size(), width, height)) { CLog::Log(LOGDEBUG, "%s - Load of %s failed.", __FUNCTION__, texturePath.c_str()); delete pImage; return false; } } delete pImage; return true; }
std::vector< CStdString > CKaraokeLyricsTextUStar::readFile(const CStdString & lyricsFile, bool report_errors ) { std::vector< CStdString > lines; XFILE::CFile file; XFILE::auto_buffer buf; if (file.LoadFile(lyricsFile, buf) <= 0) { if (report_errors) CLog::Log(LOGERROR, "%s: can't load \"%s\" file", __FUNCTION__, lyricsFile.c_str()); return std::vector< CStdString >(); } file.Close(); const size_t lyricSize = buf.size(); // Parse into the string array size_t offset = 0; size_t lineoffset = 0; while ( offset < lyricSize ) { // End of line? if (buf.get()[offset] == 0x0D || buf.get()[offset] == 0x0A) { // An empty line? if ( lineoffset != offset ) lines.push_back(std::string(buf.get() + lineoffset, offset - lineoffset)); // Point to the next symbol lineoffset = offset + 1; } offset++; } // Last line, if any if ( lineoffset < lyricSize ) lines.push_back(std::string(buf.get() + lineoffset, buf.size() - lineoffset)); return lines; }
bool CKaraokeLyricsTextLRC::Load() { XFILE::CFile file; // Clear the lyrics array clearLyrics(); XFILE::auto_buffer buf; if (file.LoadFile(m_lyricsFile, buf) <= 0) { CLog::Log(LOGERROR, "%s: can't load \"%s\" file", __FUNCTION__, m_lyricsFile.c_str()); return false; } file.Close(); // Parse the correction value int timing_correction = MathUtils::round_int( g_advancedSettings.m_karaokeSyncDelayLRC * 10 ); unsigned int offset = 0; CStdString songfilename = getSongFile(); // Skip windoze UTF8 file prefix, if any, and reject UTF16 files if (buf.size() > 3) { if ((unsigned char)buf.get()[0] == 0xFF && (unsigned char)buf.get()[1] == 0xFE) { CLog::Log( LOGERROR, "LRC lyric loader: lyrics file is in UTF16 encoding, must be in UTF8" ); return false; } // UTF8 prefix added by some windoze apps if ((unsigned char)buf.get()[0] == 0xEF && (unsigned char)buf.get()[1] == 0xBB && (unsigned char)buf.get()[2] == 0xBF) offset = 3; } if (checkMultiTime(buf.get() + offset, buf.size() - offset)) return ParserMultiTime(buf.get() + offset, buf.size() - offset, timing_correction); else return ParserNormal(buf.get() + offset, buf.size() - offset, timing_correction); }
bool CScraperUrl::Get(const SUrlEntry& scrURL, std::string& strHTML, XFILE::CCurlFile& http, const CStdString& cacheContext) { CURL url(scrURL.m_url); http.SetReferer(scrURL.m_spoof); CStdString strCachePath; if (scrURL.m_isgz) http.SetContentEncoding("gzip"); if (!scrURL.m_cache.empty()) { strCachePath = URIUtils::AddFileToFolder(g_advancedSettings.m_cachePath, "scrapers/" + cacheContext + "/" + scrURL.m_cache); if (XFILE::CFile::Exists(strCachePath)) { XFILE::CFile file; XFILE::auto_buffer buffer; if (file.LoadFile(strCachePath, buffer)) { strHTML.assign(buffer.get(), buffer.length()); return true; } } } CStdString strHTML1(strHTML); if (scrURL.m_post) { CStdString strOptions = url.GetOptions(); strOptions = strOptions.substr(1); url.SetOptions(""); if (!http.Post(url.Get(), strOptions, strHTML1)) return false; } else if (!http.Get(url.Get(), strHTML1)) return false; strHTML = strHTML1; std::string mimeType(http.GetMimeType()); CMime::EFileType ftype = CMime::GetFileTypeFromMime(mimeType); if (ftype == CMime::FileTypeUnknown) ftype = CMime::GetFileTypeFromContent(strHTML); if (ftype == CMime::FileTypeZip || ftype == CMime::FileTypeGZip) { XFILE::CZipFile file; std::string strBuffer; int iSize = file.UnpackFromMemory(strBuffer,strHTML,scrURL.m_isgz); // FIXME: use FileTypeGZip instead of scrURL.m_isgz? if (iSize > 0) { strHTML = strBuffer; CLog::Log(LOGDEBUG, "%s: Archive \"%s\" was unpacked in memory", __FUNCTION__, scrURL.m_url.c_str()); } else CLog::Log(LOGWARNING, "%s: \"%s\" looks like archive, but cannot be unpacked", __FUNCTION__, scrURL.m_url.c_str()); } std::string reportedCharset(http.GetServerReportedCharset()); if (ftype == CMime::FileTypeHtml) { std::string realHtmlCharset, converted; if (!CCharsetDetection::ConvertHtmlToUtf8(strHTML, converted, reportedCharset, realHtmlCharset)) CLog::Log(LOGWARNING, "%s: Can't find precise charset for HTML \"%s\", using \"%s\" as fallback", __FUNCTION__, scrURL.m_url.c_str(), realHtmlCharset.c_str()); else CLog::Log(LOGDEBUG, "%s: Using \"%s\" charset for HTML \"%s\"", __FUNCTION__, realHtmlCharset.c_str(), scrURL.m_url.c_str()); strHTML = converted; } else if (ftype == CMime::FileTypeXml) { CXBMCTinyXML xmlDoc; xmlDoc.Parse(strHTML, reportedCharset); std::string realXmlCharset(xmlDoc.GetUsedCharset()); if (!realXmlCharset.empty()) { CLog::Log(LOGDEBUG, "%s: Using \"%s\" charset for XML \"%s\"", __FUNCTION__, realXmlCharset.c_str(), scrURL.m_url.c_str()); std::string converted; g_charsetConverter.ToUtf8(realXmlCharset, strHTML, converted); strHTML = converted; } } else if (ftype == CMime::FileTypePlainText || StringUtils::CompareNoCase(mimeType.substr(0, 5), "text/") == 0) { std::string realTextCharset, converted; CCharsetDetection::ConvertPlainTextToUtf8(strHTML, converted, reportedCharset, realTextCharset); strHTML = converted; if (reportedCharset != realTextCharset) CLog::Log(LOGWARNING, "%s: Using \"%s\" charset for plain text \"%s\" instead of server reported \"%s\" charset", __FUNCTION__, realTextCharset.c_str(), scrURL.m_url.c_str(), reportedCharset.c_str()); else CLog::Log(LOGDEBUG, "%s: Using \"%s\" charset for plain text \"%s\"", __FUNCTION__, realTextCharset.c_str(), scrURL.m_url.c_str()); } else if (!reportedCharset.empty()) { CLog::Log(LOGDEBUG, "%s: Using \"%s\" charset for \"%s\"", __FUNCTION__, reportedCharset.c_str(), scrURL.m_url.c_str()); if (reportedCharset != "UTF-8") { std::string converted; g_charsetConverter.ToUtf8(reportedCharset, strHTML, converted); strHTML = converted; } } else CLog::Log(LOGDEBUG, "%s: Using content of \"%s\" as binary or text with \"UTF-8\" charset", __FUNCTION__, scrURL.m_url.c_str()); if (!scrURL.m_cache.empty()) { CStdString strCachePath = URIUtils::AddFileToFolder(g_advancedSettings.m_cachePath, "scrapers/" + cacheContext + "/" + scrURL.m_cache); XFILE::CFile file; if (file.OpenForWrite(strCachePath,true)) file.Write(strHTML.data(),strHTML.size()); file.Close(); } return true; }
const CTextureArray& CGUITextureManager::Load(const std::string& strTextureName, bool checkBundleOnly /*= false */) { std::string strPath; static CTextureArray emptyTexture; int bundle = -1; int size = 0; if (!HasTexture(strTextureName, &strPath, &bundle, &size)) return emptyTexture; if (size) // we found the texture { for (int i = 0; i < (int)m_vecTextures.size(); ++i) { CTextureMap *pMap = m_vecTextures[i]; if (pMap->GetName() == strTextureName) { //CLog::Log(LOGDEBUG, "Total memusage %u", GetMemoryUsage()); return pMap->GetTexture(); } } // Whoops, not there. return emptyTexture; } for (ilistUnused i = m_unusedTextures.begin(); i != m_unusedTextures.end(); ++i) { CTextureMap* pMap = i->first; if (pMap->GetName() == strTextureName && i->second > 0) { m_vecTextures.push_back(pMap); m_unusedTextures.erase(i); return pMap->GetTexture(); } } if (checkBundleOnly && bundle == -1) return emptyTexture; //Lock here, we will do stuff that could break rendering CSingleLock lock(g_graphicsContext); #ifdef _DEBUG_TEXTURES int64_t start; start = CurrentHostCounter(); #endif if (bundle >= 0 && StringUtils::EndsWithNoCase(strPath, ".gif")) { CTextureMap* pMap = nullptr; CBaseTexture **pTextures = nullptr; int nLoops = 0, width = 0, height = 0; int* Delay = nullptr; int nImages = m_TexBundle[bundle].LoadAnim(strTextureName, &pTextures, width, height, nLoops, &Delay); if (!nImages) { CLog::Log(LOGERROR, "Texture manager unable to load bundled file: %s", strTextureName.c_str()); delete[] pTextures; delete[] Delay; return emptyTexture; } unsigned int maxWidth = 0; unsigned int maxHeight = 0; pMap = new CTextureMap(strTextureName, width, height, nLoops); for (int iImage = 0; iImage < nImages; ++iImage) { pMap->Add(pTextures[iImage], Delay[iImage]); maxWidth = std::max(maxWidth, pTextures[iImage]->GetWidth()); maxHeight = std::max(maxHeight, pTextures[iImage]->GetHeight()); } pMap->SetWidth((int)maxWidth); pMap->SetHeight((int)maxHeight); delete[] pTextures; delete[] Delay; if (pMap) { m_vecTextures.push_back(pMap); return pMap->GetTexture(); } } else if (StringUtils::EndsWithNoCase(strPath, ".gif") || StringUtils::EndsWithNoCase(strPath, ".apng")) { CTextureMap* pMap = nullptr; std::string mimeType; if (StringUtils::EndsWithNoCase(strPath, ".gif")) mimeType = "image/gif"; else if (StringUtils::EndsWithNoCase(strPath, ".apng")) mimeType = "image/apng"; XFILE::CFile file; XFILE::auto_buffer buf; CFFmpegImage anim(mimeType); pMap = new CTextureMap(strTextureName, 0, 0, 0); if (file.LoadFile(strPath, buf) <= 0 || !anim.Initialize((uint8_t*)buf.get(), buf.size()) || !pMap) { CLog::Log(LOGERROR, "Texture manager unable to load file: %s", CURL::GetRedacted(strPath).c_str()); file.Close(); return emptyTexture; } unsigned int maxWidth = 0; unsigned int maxHeight = 0; uint64_t maxMemoryUsage = 91238400;// 1920*1080*4*11 bytes, i.e, a total of approx. 12 full hd frames auto frame = anim.ReadFrame(); while (frame) { CTexture *glTexture = new CTexture(); if (glTexture) { glTexture->LoadFromMemory(anim.Width(), anim.Height(), frame->GetPitch(), XB_FMT_A8R8G8B8, true, frame->m_pImage); pMap->Add(glTexture, frame->m_delay); maxWidth = std::max(maxWidth, glTexture->GetWidth()); maxHeight = std::max(maxHeight, glTexture->GetHeight()); } if (pMap->GetMemoryUsage() <= maxMemoryUsage) { frame = anim.ReadFrame(); } else { CLog::Log(LOGDEBUG, "Memory limit (%" PRIu64 " bytes) exceeded, %i frames extracted from file : %s", (maxMemoryUsage/11)*12,pMap->GetTexture().size(), CURL::GetRedacted(strPath).c_str()); break; } } pMap->SetWidth((int)maxWidth); pMap->SetHeight((int)maxHeight); file.Close(); if (pMap) { m_vecTextures.push_back(pMap); return pMap->GetTexture(); } } CBaseTexture *pTexture = NULL; int width = 0, height = 0; if (bundle >= 0) { if (!m_TexBundle[bundle].LoadTexture(strTextureName, &pTexture, width, height)) { CLog::Log(LOGERROR, "Texture manager unable to load bundled file: %s", strTextureName.c_str()); return emptyTexture; } } else { pTexture = CBaseTexture::LoadFromFile(strPath); if (!pTexture) return emptyTexture; width = pTexture->GetWidth(); height = pTexture->GetHeight(); } if (!pTexture) return emptyTexture; CTextureMap* pMap = new CTextureMap(strTextureName, width, height, 0); pMap->Add(pTexture, 100); m_vecTextures.push_back(pMap); #ifdef _DEBUG_TEXTURES int64_t end, freq; end = CurrentHostCounter(); freq = CurrentHostFrequency(); char temp[200]; sprintf(temp, "Load %s: %.1fms%s\n", strPath.c_str(), 1000.f * (end - start) / freq, (bundle >= 0) ? " (bundled)" : ""); OutputDebugString(temp); #endif return pMap->GetTexture(); }
bool CScraperUrl::Get(const SUrlEntry& scrURL, std::string& strHTML, XFILE::CCurlFile& http, const CStdString& cacheContext) { CURL url(scrURL.m_url); http.SetReferer(scrURL.m_spoof); CStdString strCachePath; if (scrURL.m_isgz) http.SetContentEncoding("gzip"); if (!scrURL.m_cache.IsEmpty()) { strCachePath = URIUtils::AddFileToFolder(g_advancedSettings.m_cachePath, "scrapers/" + cacheContext + "/" + scrURL.m_cache); if (XFILE::CFile::Exists(strCachePath)) { XFILE::CFile file; XFILE::auto_buffer buffer; if (file.LoadFile(strCachePath, buffer)) { strHTML.assign(buffer.get(), buffer.length()); return true; } } } CStdString strHTML1(strHTML); if (scrURL.m_post) { CStdString strOptions = url.GetOptions(); strOptions = strOptions.substr(1); url.SetOptions(""); if (!http.Post(url.Get(), strOptions, strHTML1)) return false; } else if (!http.Get(url.Get(), strHTML1)) return false; strHTML = strHTML1; std::string fileCharset(http.GetServerReportedCharset()); if (scrURL.m_url.Find(".zip") > -1 ) { XFILE::CZipFile file; CStdString strBuffer; int iSize = file.UnpackFromMemory(strBuffer,strHTML,scrURL.m_isgz); if (iSize) { fileCharset.clear(); strHTML.clear(); strHTML.append(strBuffer.c_str(),strBuffer.data()+iSize); } } if (!fileCharset.empty() && fileCharset != "UTF-8") { std::string converted; if (g_charsetConverter.ToUtf8(fileCharset, strHTML, converted) && !converted.empty()) strHTML = converted; } if (!scrURL.m_cache.IsEmpty()) { CStdString strCachePath = URIUtils::AddFileToFolder(g_advancedSettings.m_cachePath, "scrapers/" + cacheContext + "/" + scrURL.m_cache); XFILE::CFile file; if (file.OpenForWrite(strCachePath,true)) file.Write(strHTML.data(),strHTML.size()); file.Close(); } return true; }