bool DetectEpubFormat( LVStreamRef stream ) { LVContainerRef m_arc = LVOpenArchieve( stream ); if ( m_arc.isNull() ) return false; // not a ZIP archive //dumpZip( m_arc ); // read "mimetype" file contents from root of archive lString16 mimeType; { LVStreamRef mtStream = m_arc->OpenStream(L"mimetype", LVOM_READ ); if ( !mtStream.isNull() ) { int size = mtStream->GetSize(); if ( size>4 && size<100 ) { LVArray<char> buf( size+1, '\0' ); if ( mtStream->Read( buf.get(), size, NULL )==LVERR_OK ) { for ( int i=0; i<size; i++ ) if ( buf[i]<32 || ((unsigned char)buf[i])>127 ) buf[i] = 0; buf[size] = 0; if ( buf[0] ) mimeType = Utf8ToUnicode( lString8( buf.get() ) ); } } } } if ( mimeType != L"application/epub+zip" ) return false; return true; }
static bool GetEPUBBookProperties(const char *name, LVStreamRef stream, BookProperties * pBookProps) { LVContainerRef m_arc = LVOpenArchieve( stream ); if ( m_arc.isNull() ) return false; // not a ZIP archive // check root media type lString16 rootfilePath = EpubGetRootFilePath(m_arc); if ( rootfilePath.empty() ) return false; lString16 codeBase; codeBase=LVExtractPath(rootfilePath, false); LVStreamRef content_stream = m_arc->OpenStream(rootfilePath.c_str(), LVOM_READ); if ( content_stream.isNull() ) return false; ldomDocument * doc = LVParseXMLStream( content_stream ); if ( !doc ) return false; time_t t = (time_t)time(0); struct stat fs; if ( !stat( name, &fs ) ) { t = fs.st_mtime; } lString16 author = doc->textFromXPath( lString16("package/metadata/creator")).trim(); lString16 title = doc->textFromXPath( lString16("package/metadata/title")).trim(); lString16 language = doc->textFromXPath( lString16("package/metadata/language")).trim(); pBookProps->author = author; pBookProps->title = title; pBookProps->language = language; for ( int i=1; i<20; i++ ) { ldomNode * item = doc->nodeFromXPath( lString16("package/metadata/meta[") << fmt::decimal(i) << "]" ); if ( !item ) break; lString16 name = item->getAttributeValue("name"); lString16 content = item->getAttributeValue("content"); if (name == "calibre:series") pBookProps->series = content.trim(); else if (name == "calibre:series_index") pBookProps->seriesNumber = content.trim().atoi(); } pBookProps->filesize = (long)stream->GetSize(); pBookProps->filename = lString16(name); pBookProps->filedate = getDateTimeString( t ); delete doc; return true; }
lString8 readFileToString( const char * fname ) { lString8 buf; LVStreamRef stream = LVOpenFileStream(fname, LVOM_READ); if (!stream) return buf; int sz = stream->GetSize(); if (sz>0) { buf.insert( 0, sz, ' ' ); stream->Read( buf.modify(), sz, NULL ); } return buf; }
void MMapTest() { // write { LVStreamRef stream = LVMapFileStream( "c:\\cr3\\mapfile.dat", LVOM_APPEND, 1000000 ); LVStreamBufferRef buf = stream->GetWriteBuffer( 0, stream->GetSize() ); lUInt32 * p = (lUInt32*)buf->getReadWrite(); for ( lUInt32 i=0; i<1000000/4; i++ ) { p[i] = i; } } // read { LVStreamRef stream = LVMapFileStream( "c:\\cr3\\mapfile.dat", LVOM_READ, 1000000 ); LVStreamBufferRef buf = stream->GetReadBuffer( 0, stream->GetSize() ); const lUInt32 * p = (const lUInt32*)buf->getReadOnly(); for ( lUInt32 i=0; i<1000000/4; i++ ) { if ( p[i]!=i ) { CRLog::error("MMapTest failed!!!"); break; } } } }
jbyteArray CRJNIEnv::streamToJByteArray( LVStreamRef stream ) { if ( stream.isNull() ) return NULL; unsigned sz = stream->GetSize(); if ( sz<10 || sz>2000000 ) return NULL; jbyteArray array = env->NewByteArray(sz); lUInt8 * array_data = (lUInt8 *)env->GetByteArrayElements(array, 0); lvsize_t bytesRead = 0; stream->Read(array_data, sz, &bytesRead); env->ReleaseByteArrayElements(array, (jbyte*)array_data, 0); if (bytesRead != sz) return NULL; return array; }
static int getCoverPageImageData(lua_State *L) { CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument"); LVStreamRef stream = doc->text_view->getCoverPageImageStream(); if (!stream.isNull()) { unsigned size = stream->GetSize(); lvsize_t read_size = 0; void *buffer = (void *)malloc(size); if (buffer != NULL) { stream->Read(buffer, size, &read_size); if (read_size == size) { lua_pushlightuserdata(L, buffer); lua_pushinteger(L, size); return 2; } } } return 0; }
bool DetectWordFormat( LVStreamRef stream ) { AntiwordStreamGuard file(stream); setOptions(); lUInt32 lFilesize = (lUInt32)stream->GetSize(); int iWordVersion = iGuessVersionNumber(file, lFilesize); if (iWordVersion < 0 || iWordVersion == 3) { if (bIsRtfFile(file)) { // CRLog::trace("not a Word Document." // " It is probably a Rich Text Format file"); } if (bIsWordPerfectFile(file)) { // CRLog::trace("not a Word Document." // " It is probably a Word Perfect file"); } else { //CRLog::error("not a Word Document"); } return FALSE; } return true; }
void V3DocViewWin::showHelpDialog() { LVStreamRef stream = LVOpenFileStream( _helpFile.c_str(), LVOM_READ ); lString8 help; if ( stream.isNull() ) { // show warning lString8 body; body << "<title><p>" << _("No manual currently available for this language, sorry!") << "</p></title>"; help = CRViewDialog::makeFb2Xml( body ); } else { int len = stream->GetSize(); if ( len>100 && len <1000000 ) { help.append( len, ' ' ); stream->Read( help.modify(), len, NULL ); } } //lString8 help = UnicodeToUtf8( LVReadTextFile( _helpFile ) ); if ( !help.empty() ) { CRViewDialog * dlg = new CRViewDialog( _wm, lString16(_("Help")), help, lvRect(), true, true ); _wm->activateWindow( dlg ); } }
/* * Class: org_coolreader_crengine_Engine * Method: setHyphenationMethod * Signature: (I[B)Z */ JNIEXPORT jboolean JNICALL Java_org_coolreader_crengine_Engine_setHyphenationMethod (JNIEnv * _env, jobject _engine, jint method, jbyteArray data) { CRJNIEnv env(_env); if ( method==0 ) { CRLog::info("Selecting hyphenation method: Disabled"); return HyphMan::activateDictionary(lString16(HYPH_DICT_ID_NONE)); } else if ( method==1 ) { CRLog::info("Selecting hyphenation method: Algoryphmic"); return HyphMan::activateDictionary(lString16(HYPH_DICT_ID_ALGORITHM)); } else { CRLog::info("Selecting hyphenation method: Dictionary"); LVStreamRef stream = env.jbyteArrayToStream( data ); CRLog::debug("Stream is created from byte array, length=%d", (int)(stream.isNull()?0:stream->GetSize())); bool res = HyphMan::activateDictionaryFromStream(stream); if ( !res ) { CRLog::error("Dictionary activation is failed: disabling hyphenation"); HyphMan::activateDictionary(lString16(HYPH_DICT_ID_NONE)); return false; } return true; } }
static bool GetBookProperties(const char *name, BookProperties * pBookProps) { CRLog::trace("GetBookProperties( %s )", name); // check archieve lString16 arcPathName; lString16 arcItemPathName; bool isArchiveFile = LVSplitArcName( lString16(name), arcPathName, arcItemPathName ); // open stream LVStreamRef stream = LVOpenFileStream( (isArchiveFile ? arcPathName : Utf8ToUnicode(lString8(name))).c_str() , LVOM_READ); if (!stream) { CRLog::error("cannot open file %s", name); return false; } if ( DetectEpubFormat( stream ) ) { CRLog::trace("GetBookProperties() : epub format detected"); return GetEPUBBookProperties( name, stream, pBookProps ); } time_t t = (time_t)time(0); if ( isArchiveFile ) { int arcsize = (int)stream->GetSize(); LVContainerRef container = LVOpenArchieve(stream); if ( container.isNull() ) { CRLog::error( "Cannot read archive contents from %s", LCSTR(arcPathName) ); return false; } stream = container->OpenStream(arcItemPathName.c_str(), LVOM_READ); if ( stream.isNull() ) { CRLog::error( "Cannot open archive file item stream %s", LCSTR(lString16(name)) ); return false; } } struct stat fs; if ( !stat( name, &fs ) ) { t = fs.st_mtime; } // read document #if COMPACT_DOM==1 ldomDocument doc(stream, 0); #else ldomDocument doc; #endif ldomDocumentWriter writer(&doc, true); doc.setNodeTypes( fb2_elem_table ); doc.setAttributeTypes( fb2_attr_table ); doc.setNameSpaceTypes( fb2_ns_table ); LVXMLParser parser( stream, &writer ); CRLog::trace( "checking format..." ); if ( !parser.CheckFormat() ) { return false; } CRLog::trace( "parsing..." ); if ( !parser.Parse() ) { return false; } CRLog::trace( "parsed" ); #if 0 char ofname[512]; sprintf(ofname, "%s.xml", name); CRLog::trace(" writing to file %s", ofname); LVStreamRef out = LVOpenFileStream(ofname, LVOM_WRITE); doc.saveToStream(out, "utf16"); #endif lString16 authors = extractDocAuthors( &doc, lString16("|"), false ); lString16 title = extractDocTitle( &doc ); lString16 language = extractDocLanguage( &doc ).lowercase(); lString16 series = extractDocSeries( &doc, &pBookProps->seriesNumber ); #if SERIES_IN_AUTHORS==1 if ( !series.empty() ) authors << " " << series; #endif pBookProps->title = title; pBookProps->author = authors; pBookProps->series = series; pBookProps->filesize = (long)stream->GetSize(); pBookProps->filename = lString16(name); pBookProps->filedate = getDateTimeString( t ); pBookProps->language = language; return true; }
bool ImportWordDocument( LVStreamRef stream, ldomDocument * m_doc, LVDocViewCallback * progressCallback, CacheLoadingCallback * formatCallback ) { AntiwordStreamGuard file(stream); setOptions(); inside_p = false; inside_table = false; table_col_count = 0; inside_list = 0; // 0=none, 1=ul, 2=ol alignment = 0; inside_li = false; last_space_char = false; sLeftIndent = 0; /* Left indentation in twips */ sLeftIndent1 = 0; /* First line left indentation in twips */ sRightIndent = 0; /* Right indentation in twips */ usBeforeIndent = 0; /* Vertical indent before paragraph in twips */ usAfterIndent = 0; /* Vertical indent after paragraph in twips */ BOOL bResult = 0; diagram_type *pDiag; int iWordVersion; lUInt32 lFilesize = (lUInt32)stream->GetSize(); iWordVersion = iGuessVersionNumber(file, lFilesize); if (iWordVersion < 0 || iWordVersion == 3) { if (bIsRtfFile(file)) { CRLog::error("not a Word Document." " It is probably a Rich Text Format file"); } if (bIsWordPerfectFile(file)) { CRLog::error("not a Word Document." " It is probably a Word Perfect file"); } else { CRLog::error("not a Word Document"); } return FALSE; } /* Reset any reading done during file testing */ stream->SetPos(0); ldomDocumentWriter w(m_doc); writer = &w; doc = m_doc; image_index = 0; pDiag = pCreateDiagram("cr3", "filename.doc"); if (pDiag == NULL) { return false; } bResult = bWordDecryptor(file, lFilesize, pDiag); vDestroyDiagram(pDiag); doc = NULL; writer = NULL; #ifdef _DEBUG #define SAVE_COPY_OF_LOADED_DOCUMENT 1//def _DEBUG #endif if ( bResult!=0 ) { #ifdef SAVE_COPY_OF_LOADED_DOCUMENT //def _DEBUG LVStreamRef ostream = LVOpenFileStream( "/tmp/test_save_source.xml", LVOM_WRITE ); if ( !ostream.isNull() ) m_doc->saveToStream( ostream, "utf-16" ); #endif } return bResult!=0; }
int GetBookProperties(char *name, struct BookProperties* pBookProps, int localLanguage) { CRLog::trace("GetBookProperties( %s )", name); memset(pBookProps, 0, sizeof(BookProperties) ); // open stream LVStreamRef stream = LVOpenFileStream(name, LVOM_READ); if (!stream) { CRLog::error("cannot open file %s", name); return 0; } // check archieve #ifdef USE_ZLIB LVContainerRef arc; //printf("start opening arc\n"); //for ( int i=0; i<1000; i++ ) //for ( int kk=0; kk<1000; kk++) { arc = LVOpenArchieve( stream ); //printf("end opening arc\n"); if (!arc.isNull()) { CRLog::trace("%s is archive with %d items", name, arc->GetObjectCount()); // archieve const LVContainerItemInfo * bestitem = NULL; const LVContainerItemInfo * fb2item = NULL; const LVContainerItemInfo * fbditem = NULL; for (int i=0; i<arc->GetObjectCount(); i++) { const LVContainerItemInfo * item = arc->GetObjectInfo(i); if (item) { if ( !item->IsContainer() ) { lString16 name( item->GetName() ); if ( name.length() > 5 ) { name.lowercase(); const lChar16 * pext = name.c_str() + name.length() - 4; if ( pext[0]=='.' && pext[1]=='f' && pext[2]=='b' && pext[3]=='2') { fb2item = item; } else if ( pext[0]=='.' && pext[1]=='f' && pext[2]=='b' && pext[3]=='d') { fbditem = item; } } } } } bestitem = fb2item; if ( fbditem ) bestitem = fbditem; if ( !bestitem ) return 0; CRLog::trace( "opening item %s from archive", UnicodeToUtf8(bestitem->GetName()).c_str() ); //printf("start opening stream\n"); //for ( int k=0; k<1000; k++ ) { stream = arc->OpenStream( bestitem->GetName(), LVOM_READ ); char buf[8192]; stream->Read(buf, 8192, NULL ); //} //printf("end opening stream\n"); if ( stream.isNull() ) return 0; CRLog::trace( "stream created" ); // opened archieve stream } } #endif //USE_ZLIB // read document #if COMPACT_DOM==1 ldomDocument doc(stream, 0); #else ldomDocument doc; #endif ldomDocumentWriter writer(&doc, true); doc.setNodeTypes( fb2_elem_table ); doc.setAttributeTypes( fb2_attr_table ); doc.setNameSpaceTypes( fb2_ns_table ); LVXMLParser parser( stream, &writer ); CRLog::trace( "checking format..." ); if ( !parser.CheckFormat() ) { return 0; } CRLog::trace( "parsing..." ); if ( !parser.Parse() ) { return 0; } CRLog::trace( "parsed" ); #if 0 char ofname[512]; sprintf(ofname, "%s.xml", name); CRLog::trace(" writing to file %s", ofname); LVStreamRef out = LVOpenFileStream(ofname, LVOM_WRITE); doc.saveToStream(out, "utf16"); #endif lString16 authors = extractDocAuthors( &doc ); lString16 title = extractDocTitle( &doc ); lString16 series = extractDocSeriesReverse( &doc ); #if SERIES_IN_AUTHORS==1 if ( !series.empty() ) authors << L" " << series; #endif SetFieldValue( pBookProps->name, title ); if ( !authors.empty() ) SetFieldValue( pBookProps->author, authors ); if ( !series.empty() ) SetFieldValue( pBookProps->series, series ); pBookProps->filesize = (long)stream->GetSize(); strncpy( pBookProps->filename, name, MAX_PROPERTY_LEN-1 ); struct stat fs; time_t t; if ( stat( name, &fs ) ) { t = (time_t)time(0); } else { t = fs.st_mtime; } SetFieldValue( pBookProps->filedate, getDateTimeString( t, localLanguage ) ); return 1; }