LibreOffice Module sw (master)  1
swdetect.cxx
Go to the documentation of this file.
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 "swdetect.hxx"
21 
23 #include <com/sun/star/container/XNameAccess.hpp>
24 #include <com/sun/star/io/XInputStream.hpp>
25 #include <com/sun/star/uno/XComponentContext.hpp>
26 #include <sfx2/docfile.hxx>
27 #include <sot/storage.hxx>
29 
30 using namespace ::com::sun::star;
31 using namespace ::com::sun::star::uno;
32 using namespace ::com::sun::star::io;
33 using namespace ::com::sun::star::task;
34 using namespace ::com::sun::star::beans;
35 using namespace ::com::sun::star::lang;
37 
39 {
40 }
41 
43 {
44 }
45 
46 OUString SAL_CALL SwFilterDetect::detect( Sequence< PropertyValue >& lDescriptor )
47 {
48  MediaDescriptor aMediaDesc( lDescriptor );
49  OUString aTypeName = aMediaDesc.getUnpackedValueOrDefault( MediaDescriptor::PROP_TYPENAME(), OUString() );
50  uno::Reference< io::XInputStream > xInStream ( aMediaDesc[MediaDescriptor::PROP_INPUTSTREAM()], uno::UNO_QUERY );
51  if ( !xInStream.is() )
52  return OUString();
53 
54  SfxMedium aMedium;
55  aMedium.UseInteractionHandler( false );
56  aMedium.setStreamToLoadFrom( xInStream, true );
57 
58  SvStream *pInStrm = aMedium.GetInStream();
59  if ( !pInStrm || pInStrm->GetError() )
60  return OUString();
61 
62  bool bIsDetected = false;
63 
64  if ( aTypeName == "writer_Rich_Text_Format" )
65  {
66  pInStrm->Seek( STREAM_SEEK_TO_BEGIN );
67  bIsDetected = ( read_uInt8s_ToOString( *pInStrm, 5 ) == "{\\rtf" );
68  }
69  else if ( aTypeName == "writer_MS_WinWord_5" )
70  {
71  pInStrm->Seek( STREAM_SEEK_TO_BEGIN );
72  const sal_uInt8 nBufSize = 3;
73  sal_uInt8 nBuffer[ nBufSize ];
74  if (pInStrm->ReadBytes(nBuffer, nBufSize) < nBufSize)
75  return OUString();
76 
77  bIsDetected = (nBuffer[0] == 0x9B && nBuffer[1] == 0xA5 && nBuffer[2] == 0x21) // WinWord 1
78  || (nBuffer[0] == 0x9C && nBuffer[1] == 0xA5 && nBuffer[2] == 0x21) // PMWord 1
79  || (nBuffer[0] == 0xDB && nBuffer[1] == 0xA5 && nBuffer[2] == 0x2D) // WinWord 2
80  || (nBuffer[0] == 0xDC && nBuffer[1] == 0xA5 && nBuffer[2] == 0x65); // WinWord 6.0/95, as a single stream file
81  }
82  else
83  {
84  // Do not attempt to create an SotStorage on a
85  // 0-length stream as that would create the compound
86  // document header on the stream and effectively write to
87  // disk!
88  pInStrm->Seek( STREAM_SEEK_TO_BEGIN );
89  if ( pInStrm->remainingSize() == 0 )
90  return OUString();
91 
92  try
93  {
94  tools::SvRef<SotStorage> aStorage = new SotStorage ( pInStrm, false );
95  if ( !aStorage->GetError() )
96  {
97  bIsDetected = aStorage->IsContained( "WordDocument" );
98  if ( bIsDetected && aTypeName.startsWith( "writer_MS_Word_97" ) )
99  {
100  bIsDetected = ( aStorage->IsContained("0Table") || aStorage->IsContained("1Table") );
101 
102  // If we are checking the template type, and the document is not a .dot, don't
103  // mis-detect it.
104  if ( bIsDetected && aTypeName == "writer_MS_Word_97_Vorlage" )
105  {
106  // Super ugly hack, but we don't want to use the whole WW8Fib thing here in
107  // the swd library, apparently. We know (do we?) that the "aBits1" byte, as
108  // the variable is called in WW8Fib::WW8Fib(SvStream&,sal_uInt8,sal_uInt32),
109  // is at offset 10 in the WordDocument stream. The fDot bit is bit 0x01 of
110  // that byte.
111  tools::SvRef<SotStorageStream> xWordDocument = aStorage->OpenSotStream("WordDocument", StreamMode::STD_READ);
112  xWordDocument->Seek( 10 );
113  if ( xWordDocument->Tell() == 10 )
114  {
115  sal_uInt8 aBits1;
116  xWordDocument->ReadUChar( aBits1 );
117  // Check fDot bit
118  bIsDetected = ((aBits1 & 0x01) == 0x01);
119  }
120  }
121  }
122  }
123  }
124  catch (...)
125  {
126  bIsDetected = false;
127  }
128  }
129 
130  if ( bIsDetected )
131  return aTypeName;
132 
133  return OUString();
134 }
135 
136 /* XServiceInfo */
138 {
139  return "com.sun.star.comp.writer.FormatDetector";
140 }
141 
142 /* XServiceInfo */
144 {
145  return cppu::supportsService(this, sServiceName);
146 }
147 
148 /* XServiceInfo */
150 {
151  return { "com.sun.star.frame.ExtendedTypeDetection", "com.sun.star.text.FormatDetector", "com.sun.star.text.W4WFormatDetector" };
152 }
153 
154 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
156  css::uno::Sequence<css::uno::Any> const &)
157 {
158  return cppu::acquire(new SwFilterDetect());
159 }
160 
161 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
ErrCode GetError() const
sal_uInt64 Seek(sal_uInt64 nPos)
bool IsContained(const OUString &rEleName) const
ErrCode GetError() const
virtual ~SwFilterDetect() override
Definition: swdetect.cxx:42
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
sal_uInt64 remainingSize()
SotStorageStream * OpenSotStream(const OUString &rEleName, StreamMode=StreamMode::STD_READWRITE)
#define STREAM_SEEK_TO_BEGIN
void UseInteractionHandler(bool)
virtual OUString SAL_CALL getImplementationName() override
Definition: swdetect.cxx:137
unsigned char sal_Bool
std::size_t ReadBytes(void *pData, std::size_t nSize)
SvStream * GetInStream()
unsigned char sal_uInt8
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_writer_FormatDetector_get_implementation(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
Definition: swdetect.cxx:155
virtual sal_Bool SAL_CALL supportsService(const OUString &sServiceName) override
Definition: swdetect.cxx:143
virtual OUString SAL_CALL detect(css::uno::Sequence< css::beans::PropertyValue > &lDescriptor) override
Definition: swdetect.cxx:46
OString read_uInt8s_ToOString(SvStream &rStrm, std::size_t nLen)
const sal_Char sServiceName[]
Definition: accdoc.cxx:61
void setStreamToLoadFrom(const css::uno::Reference< css::io::XInputStream > &xInputStream, bool bIsReadOnly)
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: swdetect.cxx:149