File: | home/maarten/src/libreoffice/core/starmath/source/smdetect.cxx |
Warning: | line 92, column 10 Potential leak of memory pointed to by 'aStorage.pObj' |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ | |||
2 | /* | |||
3 | * This file is part of the LibreOffice project. | |||
4 | * | |||
5 | * This Source Code Form is subject to the terms of the Mozilla Public | |||
6 | * License, v. 2.0. If a copy of the MPL was not distributed with this | |||
7 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. | |||
8 | * | |||
9 | * This file incorporates work covered by the following license notice: | |||
10 | * | |||
11 | * Licensed to the Apache Software Foundation (ASF) under one or more | |||
12 | * contributor license agreements. See the NOTICE file distributed | |||
13 | * with this work for additional information regarding copyright | |||
14 | * ownership. The ASF licenses this file to you under the Apache | |||
15 | * License, Version 2.0 (the "License"); you may not use this file | |||
16 | * except in compliance with the License. You may obtain a copy of | |||
17 | * the License at http://www.apache.org/licenses/LICENSE-2.0 . | |||
18 | */ | |||
19 | ||||
20 | #include "smdetect.hxx" | |||
21 | #include <cppuhelper/supportsservice.hxx> | |||
22 | #include <com/sun/star/io/XInputStream.hpp> | |||
23 | #include <com/sun/star/ucb/ContentCreationException.hpp> | |||
24 | #include <com/sun/star/uno/XComponentContext.hpp> | |||
25 | #include <sfx2/docfile.hxx> | |||
26 | #include <unotools/mediadescriptor.hxx> | |||
27 | #include <sal/log.hxx> | |||
28 | #include <sot/storage.hxx> | |||
29 | #include <tools/diagnose_ex.h> | |||
30 | ||||
31 | #include "eqnolefilehdr.hxx" | |||
32 | ||||
33 | using namespace ::com::sun::star; | |||
34 | using namespace ::com::sun::star::uno; | |||
35 | using namespace ::com::sun::star::io; | |||
36 | using namespace ::com::sun::star::task; | |||
37 | using namespace ::com::sun::star::beans; | |||
38 | using namespace ::com::sun::star::lang; | |||
39 | using utl::MediaDescriptor; | |||
40 | ||||
41 | SmFilterDetect::SmFilterDetect() | |||
42 | { | |||
43 | } | |||
44 | ||||
45 | SmFilterDetect::~SmFilterDetect() | |||
46 | { | |||
47 | } | |||
48 | ||||
49 | OUString SAL_CALL SmFilterDetect::detect( Sequence< PropertyValue >& lDescriptor ) | |||
50 | { | |||
51 | MediaDescriptor aMediaDesc( lDescriptor ); | |||
52 | uno::Reference< io::XInputStream > xInStream ( aMediaDesc[MediaDescriptor::PROP_INPUTSTREAM()], uno::UNO_QUERY ); | |||
53 | if ( !xInStream.is() ) | |||
| ||||
54 | return OUString(); | |||
55 | ||||
56 | SfxMedium aMedium; | |||
57 | aMedium.UseInteractionHandler( false ); | |||
58 | aMedium.setStreamToLoadFrom( xInStream, true ); | |||
59 | ||||
60 | SvStream *pInStrm = aMedium.GetInStream(); | |||
61 | if ( !pInStrm || pInStrm->GetError() ) | |||
62 | return OUString(); | |||
63 | ||||
64 | // Do not attempt to create an SotStorage on a | |||
65 | // 0-length stream as that would create the compound | |||
66 | // document header on the stream and effectively write to | |||
67 | // disk! | |||
68 | pInStrm->Seek( STREAM_SEEK_TO_BEGIN0L ); | |||
69 | if ( pInStrm->remainingSize() == 0 ) | |||
70 | return OUString(); | |||
71 | ||||
72 | bool bStorageOk = false; | |||
73 | try | |||
74 | { | |||
75 | tools::SvRef<SotStorage> aStorage = new SotStorage( pInStrm, false ); | |||
76 | bStorageOk = !aStorage->GetError(); | |||
77 | if (bStorageOk
| |||
78 | { | |||
79 | if ( aStorage->IsStream("Equation Native") ) | |||
80 | { | |||
81 | sal_uInt8 nVersion; | |||
82 | if ( GetMathTypeVersion( aStorage.get(), nVersion ) && nVersion <=3 ) | |||
83 | return "math_MathType_3x"; | |||
84 | } | |||
85 | } | |||
86 | } | |||
87 | catch (const css::ucb::ContentCreationException &) | |||
88 | { | |||
89 | TOOLS_WARN_EXCEPTION("starmath", "SmFilterDetect::detect caught" )do { css::uno::Any tools_warn_exception( DbgGetCaughtException () ); do { if (true) { switch (sal_detail_log_report(::SAL_DETAIL_LOG_LEVEL_WARN , "starmath")) { case SAL_DETAIL_LOG_ACTION_IGNORE: break; case SAL_DETAIL_LOG_ACTION_LOG: if (sizeof ::sal::detail::getResult ( ::sal::detail::StreamStart() << "SmFilterDetect::detect caught" << " " << exceptionToString(tools_warn_exception )) == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN), ( "starmath"), ("/home/maarten/src/libreoffice/core/starmath/source/smdetect.cxx" ":" "89" ": "), ::sal::detail::unwrapStream( ::sal::detail:: StreamStart() << "SmFilterDetect::detect caught" << " " << exceptionToString(tools_warn_exception)), 0); } else { ::std::ostringstream sal_detail_stream; sal_detail_stream << "SmFilterDetect::detect caught" << " " << exceptionToString(tools_warn_exception); ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("starmath"), ("/home/maarten/src/libreoffice/core/starmath/source/smdetect.cxx" ":" "89" ": "), sal_detail_stream, 0); }; break; case SAL_DETAIL_LOG_ACTION_FATAL : if (sizeof ::sal::detail::getResult( ::sal::detail::StreamStart () << "SmFilterDetect::detect caught" << " " << exceptionToString(tools_warn_exception)) == 1) { ::sal_detail_log ( (::SAL_DETAIL_LOG_LEVEL_WARN), ("starmath"), ("/home/maarten/src/libreoffice/core/starmath/source/smdetect.cxx" ":" "89" ": "), ::sal::detail::unwrapStream( ::sal::detail:: StreamStart() << "SmFilterDetect::detect caught" << " " << exceptionToString(tools_warn_exception)), 0); } else { ::std::ostringstream sal_detail_stream; sal_detail_stream << "SmFilterDetect::detect caught" << " " << exceptionToString(tools_warn_exception); ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("starmath"), ("/home/maarten/src/libreoffice/core/starmath/source/smdetect.cxx" ":" "89" ": "), sal_detail_stream, 0); }; std::abort(); break ; } } } while (false); } while (false); | |||
90 | } | |||
91 | ||||
92 | if (!bStorageOk) | |||
| ||||
93 | { | |||
94 | // 200 should be enough for the XML | |||
95 | // version, encoding and !DOCTYPE | |||
96 | // stuff I hope? | |||
97 | static const sal_uInt16 nBufferSize = 200; | |||
98 | char aBuffer[nBufferSize+1]; | |||
99 | pInStrm->Seek( STREAM_SEEK_TO_BEGIN0L ); | |||
100 | pInStrm->StartReadingUnicodeText( RTL_TEXTENCODING_DONTKNOW(((rtl_TextEncoding) 0)) ); // avoid BOM marker | |||
101 | auto nBytesRead = pInStrm->ReadBytes( aBuffer, nBufferSize ); | |||
102 | if (nBytesRead >= 6) | |||
103 | { | |||
104 | aBuffer[nBytesRead] = 0; | |||
105 | bool bIsMathType = false; | |||
106 | if (0 == strncmp( "<?xml", aBuffer, 5)) | |||
107 | bIsMathType = (strstr( aBuffer, "<math>" ) || | |||
108 | strstr( aBuffer, "<math " ) || | |||
109 | strstr( aBuffer, "<math:math " )); | |||
110 | else | |||
111 | // this is the old <math tag to MathML in the beginning of the XML file | |||
112 | bIsMathType = (0 == strncmp( "<math ", aBuffer, 6) || | |||
113 | 0 == strncmp( "<math> ", aBuffer, 7) || | |||
114 | 0 == strncmp( "<math:math> ", aBuffer, 12)); | |||
115 | ||||
116 | if ( bIsMathType ) | |||
117 | return "math_MathML_XML_Math"; | |||
118 | } | |||
119 | } | |||
120 | ||||
121 | return OUString(); | |||
122 | } | |||
123 | ||||
124 | /* XServiceInfo */ | |||
125 | OUString SAL_CALL SmFilterDetect::getImplementationName() | |||
126 | { | |||
127 | return "com.sun.star.comp.math.FormatDetector"; | |||
128 | } | |||
129 | ||||
130 | /* XServiceInfo */ | |||
131 | sal_Bool SAL_CALL SmFilterDetect::supportsService( const OUString& sServiceName ) | |||
132 | { | |||
133 | return cppu::supportsService(this, sServiceName); | |||
134 | } | |||
135 | ||||
136 | /* XServiceInfo */ | |||
137 | Sequence< OUString > SAL_CALL SmFilterDetect::getSupportedServiceNames() | |||
138 | { | |||
139 | return { "com.sun.star.frame.ExtendedTypeDetection" }; | |||
140 | } | |||
141 | ||||
142 | extern "C" SAL_DLLPUBLIC_EXPORT__attribute__ ((visibility("default"))) uno::XInterface* | |||
143 | math_FormatDetector_get_implementation(uno::XComponentContext* /*pCtx*/, | |||
144 | uno::Sequence<uno::Any> const& /*rSeq*/) | |||
145 | { | |||
146 | return cppu::acquire(new SmFilterDetect); | |||
147 | } | |||
148 | ||||
149 | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |