1*c142477cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*c142477cSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*c142477cSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*c142477cSAndrew Rist  * distributed with this work for additional information
6*c142477cSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*c142477cSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*c142477cSAndrew Rist  * "License"); you may not use this file except in compliance
9*c142477cSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*c142477cSAndrew Rist  *
11*c142477cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*c142477cSAndrew Rist  *
13*c142477cSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*c142477cSAndrew Rist  * software distributed under the License is distributed on an
15*c142477cSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*c142477cSAndrew Rist  * KIND, either express or implied.  See the License for the
17*c142477cSAndrew Rist  * specific language governing permissions and limitations
18*c142477cSAndrew Rist  * under the License.
19*c142477cSAndrew Rist  *
20*c142477cSAndrew Rist  *************************************************************/
21*c142477cSAndrew Rist 
22*c142477cSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sdext.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "imagecontainer.hxx"
28cdf0e10cSrcweir #include "genericelements.hxx"
29cdf0e10cSrcweir #include "xmlemitter.hxx"
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
32cdf0e10cSrcweir #include <osl/file.h>
33cdf0e10cSrcweir #include <rtl/crc.h>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #include <com/sun/star/graphic/XGraphicProvider.hpp>
36cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
37cdf0e10cSrcweir 
38cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
39cdf0e10cSrcweir #include <comphelper/stl_types.hxx>
40cdf0e10cSrcweir 
41cdf0e10cSrcweir #include <boost/bind.hpp>
42cdf0e10cSrcweir 
43cdf0e10cSrcweir using namespace com::sun::star;
44cdf0e10cSrcweir 
45cdf0e10cSrcweir namespace pdfi
46cdf0e10cSrcweir {
47cdf0e10cSrcweir 
48cdf0e10cSrcweir namespace
49cdf0e10cSrcweir {
50cdf0e10cSrcweir 
51cdf0e10cSrcweir static const sal_Char aBase64EncodeTable[] =
52cdf0e10cSrcweir     { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
53cdf0e10cSrcweir       'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
54cdf0e10cSrcweir       'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
55cdf0e10cSrcweir       'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
56cdf0e10cSrcweir       '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };
57cdf0e10cSrcweir 
encodeBase64(const sal_Int8 * i_pBuffer,const sal_uInt32 i_nBufferLength)58cdf0e10cSrcweir rtl::OUString encodeBase64( const sal_Int8* i_pBuffer, const sal_uInt32 i_nBufferLength )
59cdf0e10cSrcweir {
60cdf0e10cSrcweir     rtl::OUStringBuffer aBuf( (i_nBufferLength+1) * 4 / 3 );
61cdf0e10cSrcweir     const sal_Int32 nRemain(i_nBufferLength%3);
62cdf0e10cSrcweir     const sal_Int32 nFullTripleLength( i_nBufferLength - (i_nBufferLength%3));
63cdf0e10cSrcweir     sal_Int32 nBufPos( 0 );
64cdf0e10cSrcweir     for( sal_Int32 i = 0; i < nFullTripleLength; i += 3, nBufPos += 4 )
65cdf0e10cSrcweir 	{
66cdf0e10cSrcweir         const sal_Int32 nBinary = (((sal_uInt8)i_pBuffer[i + 0]) << 16) +
67cdf0e10cSrcweir                                   (((sal_uInt8)i_pBuffer[i + 1]) <<  8) +
68cdf0e10cSrcweir                                   ((sal_uInt8)i_pBuffer[i + 2]);
69cdf0e10cSrcweir 
70cdf0e10cSrcweir         aBuf.appendAscii("====");
71cdf0e10cSrcweir 
72cdf0e10cSrcweir         sal_uInt8 nIndex (static_cast<sal_uInt8>((nBinary & 0xFC0000) >> 18));
73cdf0e10cSrcweir         aBuf.setCharAt(nBufPos, aBase64EncodeTable [nIndex]);
74cdf0e10cSrcweir 
75cdf0e10cSrcweir         nIndex = static_cast<sal_uInt8>((nBinary & 0x3F000) >> 12);
76cdf0e10cSrcweir         aBuf.setCharAt(nBufPos+1, aBase64EncodeTable [nIndex]);
77cdf0e10cSrcweir 
78cdf0e10cSrcweir         nIndex = static_cast<sal_uInt8>((nBinary & 0xFC0) >> 6);
79cdf0e10cSrcweir         aBuf.setCharAt(nBufPos+2, aBase64EncodeTable [nIndex]);
80cdf0e10cSrcweir 
81cdf0e10cSrcweir         nIndex = static_cast<sal_uInt8>((nBinary & 0x3F));
82cdf0e10cSrcweir         aBuf.setCharAt(nBufPos+3, aBase64EncodeTable [nIndex]);
83cdf0e10cSrcweir 	}
84cdf0e10cSrcweir     if( nRemain > 0 )
85cdf0e10cSrcweir     {
86cdf0e10cSrcweir         aBuf.appendAscii("====");
87cdf0e10cSrcweir         sal_Int32 nBinary( 0 );
88cdf0e10cSrcweir         const sal_Int32 nStart(i_nBufferLength-nRemain);
89cdf0e10cSrcweir         switch(nRemain)
90cdf0e10cSrcweir         {
91cdf0e10cSrcweir             case 1: nBinary = ((sal_uInt8)i_pBuffer[nStart + 0]) << 16;
92cdf0e10cSrcweir                 break;
93cdf0e10cSrcweir             case 2: nBinary = (((sal_uInt8)i_pBuffer[nStart + 0]) << 16) +
94cdf0e10cSrcweir                               (((sal_uInt8)i_pBuffer[nStart + 1]) <<  8);
95cdf0e10cSrcweir                 break;
96cdf0e10cSrcweir         }
97cdf0e10cSrcweir         sal_uInt8 nIndex (static_cast<sal_uInt8>((nBinary & 0xFC0000) >> 18));
98cdf0e10cSrcweir         aBuf.setCharAt(nBufPos, aBase64EncodeTable [nIndex]);
99cdf0e10cSrcweir 
100cdf0e10cSrcweir         nIndex = static_cast<sal_uInt8>((nBinary & 0x3F000) >> 12);
101cdf0e10cSrcweir         aBuf.setCharAt(nBufPos+1, aBase64EncodeTable [nIndex]);
102cdf0e10cSrcweir 
103cdf0e10cSrcweir         if( nRemain == 2 )
104cdf0e10cSrcweir         {
105cdf0e10cSrcweir             nIndex = static_cast<sal_uInt8>((nBinary & 0xFC0) >> 6);
106cdf0e10cSrcweir             aBuf.setCharAt(nBufPos+2, aBase64EncodeTable [nIndex]);
107cdf0e10cSrcweir         }
108cdf0e10cSrcweir     }
109cdf0e10cSrcweir 
110cdf0e10cSrcweir     return aBuf.makeStringAndClear();
111cdf0e10cSrcweir }
112cdf0e10cSrcweir 
113cdf0e10cSrcweir } // namespace
114cdf0e10cSrcweir 
ImageContainer()115cdf0e10cSrcweir ImageContainer::ImageContainer() :
116cdf0e10cSrcweir     m_aImages()
117cdf0e10cSrcweir {}
118cdf0e10cSrcweir 
addImage(const uno::Sequence<beans::PropertyValue> & xBitmap)119cdf0e10cSrcweir ImageId ImageContainer::addImage( const uno::Sequence<beans::PropertyValue>& xBitmap )
120cdf0e10cSrcweir {
121cdf0e10cSrcweir     m_aImages.push_back( xBitmap );
122cdf0e10cSrcweir     return m_aImages.size()-1;
123cdf0e10cSrcweir }
124cdf0e10cSrcweir 
writeBase64EncodedStream(ImageId nId,EmitContext & rContext)125cdf0e10cSrcweir void ImageContainer::writeBase64EncodedStream( ImageId nId, EmitContext& rContext )
126cdf0e10cSrcweir {
127cdf0e10cSrcweir     OSL_ASSERT( nId >= 0 && nId < ImageId( m_aImages.size()) );
128cdf0e10cSrcweir 
129cdf0e10cSrcweir     const uno::Sequence<beans::PropertyValue>& rEntry( m_aImages[nId] );
130cdf0e10cSrcweir 
131cdf0e10cSrcweir     // find "InputSequence" property
132cdf0e10cSrcweir     const beans::PropertyValue* pAry(rEntry.getConstArray());
133cdf0e10cSrcweir     const sal_Int32             nLen(rEntry.getLength());
134cdf0e10cSrcweir     const beans::PropertyValue* pValue(
135cdf0e10cSrcweir         std::find_if(pAry,pAry+nLen,
136cdf0e10cSrcweir                      boost::bind(comphelper::TPropertyValueEqualFunctor(),
137cdf0e10cSrcweir                                  _1,
138cdf0e10cSrcweir                                  rtl::OUString::createFromAscii("InputSequence"))));
139cdf0e10cSrcweir     OSL_ENSURE( pValue != pAry+nLen,
140cdf0e10cSrcweir                 "InputSequence not found" );
141cdf0e10cSrcweir 
142cdf0e10cSrcweir     uno::Sequence<sal_Int8> aData;
143cdf0e10cSrcweir     if( !(pValue->Value >>= aData) )
144cdf0e10cSrcweir         OSL_ENSURE(false,"Wrong data type");
145cdf0e10cSrcweir 
146cdf0e10cSrcweir     rContext.rEmitter.write( encodeBase64( aData.getConstArray(), aData.getLength() ));
147cdf0e10cSrcweir }
148cdf0e10cSrcweir 
149cdf0e10cSrcweir }
150