LibreOffice Module sdext (master)  1
imagecontainer.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 
21 #include <imagecontainer.hxx>
22 #include <genericelements.hxx>
23 #include <xmlemitter.hxx>
24 
25 #include <rtl/ustrbuf.hxx>
26 #include <sal/log.hxx>
27 #include <o3tl/safeint.hxx>
28 #include <osl/diagnose.h>
29 
30 #include <com/sun/star/beans/PropertyValue.hpp>
31 
32 using namespace com::sun::star;
33 
34 namespace pdfi
35 {
36 
37 namespace
38 {
39 
40 const char aBase64EncodeTable[] =
41  { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
42  'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
43  'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
44  'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
45  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };
46 
47 OUString encodeBase64( const sal_Int8* i_pBuffer, const sal_Int32 i_nBufferLength )
48 {
49  OUStringBuffer aBuf( (i_nBufferLength+1) * 4 / 3 );
50  const sal_Int32 nRemain(i_nBufferLength%3);
51  const sal_Int32 nFullTripleLength( i_nBufferLength - (i_nBufferLength%3));
52  sal_Int32 nBufPos( 0 );
53  for( sal_Int32 i = 0; i < nFullTripleLength; i += 3, nBufPos += 4 )
54  {
55  const sal_Int32 nBinary = (static_cast<sal_uInt8>(i_pBuffer[i + 0]) << 16) +
56  (static_cast<sal_uInt8>(i_pBuffer[i + 1]) << 8) +
57  static_cast<sal_uInt8>(i_pBuffer[i + 2]);
58 
59  aBuf.append("====");
60 
61  sal_uInt8 nIndex (static_cast<sal_uInt8>((nBinary & 0xFC0000) >> 18));
62  aBuf[nBufPos] = aBase64EncodeTable [nIndex];
63 
64  nIndex = static_cast<sal_uInt8>((nBinary & 0x3F000) >> 12);
65  aBuf[nBufPos+1] = aBase64EncodeTable [nIndex];
66 
67  nIndex = static_cast<sal_uInt8>((nBinary & 0xFC0) >> 6);
68  aBuf[nBufPos+2] = aBase64EncodeTable [nIndex];
69 
70  nIndex = static_cast<sal_uInt8>((nBinary & 0x3F));
71  aBuf[nBufPos+3] = aBase64EncodeTable [nIndex];
72  }
73  if( nRemain > 0 )
74  {
75  aBuf.append("====");
76  sal_Int32 nBinary( 0 );
77  const sal_Int32 nStart(i_nBufferLength-nRemain);
78  switch(nRemain)
79  {
80  case 1: nBinary = static_cast<sal_uInt8>(i_pBuffer[nStart + 0]) << 16;
81  break;
82  case 2: nBinary = (static_cast<sal_uInt8>(i_pBuffer[nStart + 0]) << 16) +
83  (static_cast<sal_uInt8>(i_pBuffer[nStart + 1]) << 8);
84  break;
85  }
86  sal_uInt8 nIndex (static_cast<sal_uInt8>((nBinary & 0xFC0000) >> 18));
87  aBuf[nBufPos] = aBase64EncodeTable [nIndex];
88 
89  nIndex = static_cast<sal_uInt8>((nBinary & 0x3F000) >> 12);
90  aBuf[nBufPos+1] = aBase64EncodeTable [nIndex];
91 
92  if( nRemain == 2 )
93  {
94  nIndex = static_cast<sal_uInt8>((nBinary & 0xFC0) >> 6);
95  aBuf[nBufPos+2] = aBase64EncodeTable [nIndex];
96  }
97  }
98 
99  return aBuf.makeStringAndClear();
100 }
101 
102 } // namespace
103 
104 ImageContainer::ImageContainer()
105 {}
106 
107 ImageId ImageContainer::addImage( const uno::Sequence<beans::PropertyValue>& xBitmap )
108 {
109  m_aImages.push_back( xBitmap );
110  return m_aImages.size()-1;
111 }
112 
113 void ImageContainer::writeBase64EncodedStream( ImageId nId, EmitContext& rContext )
114 {
115  OSL_ASSERT( nId >= 0 && o3tl::make_unsigned(nId) < m_aImages.size() );
116 
117  const uno::Sequence<beans::PropertyValue>& rEntry( m_aImages[nId] );
118 
119  // find "InputSequence" property
120  const beans::PropertyValue* pAry(rEntry.getConstArray());
121  const sal_Int32 nLen(rEntry.getLength());
122  const beans::PropertyValue* pValue(
123  std::find_if(pAry, pAry+nLen,
124  [] (beans::PropertyValue const& v) -> bool {
125  return v.Name == "InputSequence";
126  }));
127 
128  if (pValue == pAry + nLen )
129  {
130  SAL_WARN("sdext.pdfimport", "InputSequence not found");
131  return;
132  }
133 
134  uno::Sequence<sal_Int8> aData;
135  if( !(pValue->Value >>= aData) )
136  {
137  SAL_WARN("sdext.pdfimport", "Wrong data type");
138  return;
139  }
140 
141  rContext.rEmitter.write( encodeBase64( aData.getConstArray(), aData.getLength() ));
142 }
143 
144 }
145 
146 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int32 nIndex
signed char sal_Int8
aBuf
sal_Int32 ImageId
Definition: pdfihelper.hxx:45
constexpr OUStringLiteral aData
int i
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
float v
const char aBase64EncodeTable[]
unsigned char sal_uInt8
XmlEmitter & rEmitter
#define SAL_WARN(area, stream)
virtual void write(const OUString &rString)=0
Write PCTEXT as-is to output.