1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sdext.hxx"
30 
31 #include "imagecontainer.hxx"
32 #include "genericelements.hxx"
33 #include "xmlemitter.hxx"
34 
35 #include <rtl/ustrbuf.hxx>
36 #include <osl/file.h>
37 #include <rtl/crc.h>
38 
39 #include <com/sun/star/graphic/XGraphicProvider.hpp>
40 #include <com/sun/star/beans/PropertyValue.hpp>
41 
42 #include <cppuhelper/implbase1.hxx>
43 #include <comphelper/stl_types.hxx>
44 
45 #include <boost/bind.hpp>
46 
47 using namespace com::sun::star;
48 
49 namespace pdfi
50 {
51 
52 namespace
53 {
54 
55 static const sal_Char aBase64EncodeTable[] =
56     { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
57       'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
58       'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
59       'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
60       '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };
61 
62 rtl::OUString encodeBase64( const sal_Int8* i_pBuffer, const sal_uInt32 i_nBufferLength )
63 {
64     rtl::OUStringBuffer aBuf( (i_nBufferLength+1) * 4 / 3 );
65     const sal_Int32 nRemain(i_nBufferLength%3);
66     const sal_Int32 nFullTripleLength( i_nBufferLength - (i_nBufferLength%3));
67     sal_Int32 nBufPos( 0 );
68     for( sal_Int32 i = 0; i < nFullTripleLength; i += 3, nBufPos += 4 )
69 	{
70         const sal_Int32 nBinary = (((sal_uInt8)i_pBuffer[i + 0]) << 16) +
71                                   (((sal_uInt8)i_pBuffer[i + 1]) <<  8) +
72                                   ((sal_uInt8)i_pBuffer[i + 2]);
73 
74         aBuf.appendAscii("====");
75 
76         sal_uInt8 nIndex (static_cast<sal_uInt8>((nBinary & 0xFC0000) >> 18));
77         aBuf.setCharAt(nBufPos, aBase64EncodeTable [nIndex]);
78 
79         nIndex = static_cast<sal_uInt8>((nBinary & 0x3F000) >> 12);
80         aBuf.setCharAt(nBufPos+1, aBase64EncodeTable [nIndex]);
81 
82         nIndex = static_cast<sal_uInt8>((nBinary & 0xFC0) >> 6);
83         aBuf.setCharAt(nBufPos+2, aBase64EncodeTable [nIndex]);
84 
85         nIndex = static_cast<sal_uInt8>((nBinary & 0x3F));
86         aBuf.setCharAt(nBufPos+3, aBase64EncodeTable [nIndex]);
87 	}
88     if( nRemain > 0 )
89     {
90         aBuf.appendAscii("====");
91         sal_Int32 nBinary( 0 );
92         const sal_Int32 nStart(i_nBufferLength-nRemain);
93         switch(nRemain)
94         {
95             case 1: nBinary = ((sal_uInt8)i_pBuffer[nStart + 0]) << 16;
96                 break;
97             case 2: nBinary = (((sal_uInt8)i_pBuffer[nStart + 0]) << 16) +
98                               (((sal_uInt8)i_pBuffer[nStart + 1]) <<  8);
99                 break;
100         }
101         sal_uInt8 nIndex (static_cast<sal_uInt8>((nBinary & 0xFC0000) >> 18));
102         aBuf.setCharAt(nBufPos, aBase64EncodeTable [nIndex]);
103 
104         nIndex = static_cast<sal_uInt8>((nBinary & 0x3F000) >> 12);
105         aBuf.setCharAt(nBufPos+1, aBase64EncodeTable [nIndex]);
106 
107         if( nRemain == 2 )
108         {
109             nIndex = static_cast<sal_uInt8>((nBinary & 0xFC0) >> 6);
110             aBuf.setCharAt(nBufPos+2, aBase64EncodeTable [nIndex]);
111         }
112     }
113 
114     return aBuf.makeStringAndClear();
115 }
116 
117 } // namespace
118 
119 ImageContainer::ImageContainer() :
120     m_aImages()
121 {}
122 
123 ImageId ImageContainer::addImage( const uno::Sequence<beans::PropertyValue>& xBitmap )
124 {
125     m_aImages.push_back( xBitmap );
126     return m_aImages.size()-1;
127 }
128 
129 void ImageContainer::writeBase64EncodedStream( ImageId nId, EmitContext& rContext )
130 {
131     OSL_ASSERT( nId >= 0 && nId < ImageId( m_aImages.size()) );
132 
133     const uno::Sequence<beans::PropertyValue>& rEntry( m_aImages[nId] );
134 
135     // find "InputSequence" property
136     const beans::PropertyValue* pAry(rEntry.getConstArray());
137     const sal_Int32             nLen(rEntry.getLength());
138     const beans::PropertyValue* pValue(
139         std::find_if(pAry,pAry+nLen,
140                      boost::bind(comphelper::TPropertyValueEqualFunctor(),
141                                  _1,
142                                  rtl::OUString::createFromAscii("InputSequence"))));
143     OSL_ENSURE( pValue != pAry+nLen,
144                 "InputSequence not found" );
145 
146     uno::Sequence<sal_Int8> aData;
147     if( !(pValue->Value >>= aData) )
148         OSL_ENSURE(false,"Wrong data type");
149 
150     rContext.rEmitter.write( encodeBase64( aData.getConstArray(), aData.getLength() ));
151 }
152 
153 }
154