android: Update app-specific/MIME type icons
[LibreOffice.git] / svl / source / misc / strmadpt.cxx
blob7a755d924986057348cf6e61c91bced865a0f8a0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <algorithm>
22 #include <limits>
23 #include <set>
24 #include <string.h>
26 #include <com/sun/star/io/IOException.hpp>
27 #include <com/sun/star/io/XInputStream.hpp>
28 #include <com/sun/star/io/XOutputStream.hpp>
29 #include <com/sun/star/io/XSeekable.hpp>
30 #include <o3tl/safeint.hxx>
31 #include <osl/diagnose.h>
32 #include <svl/instrm.hxx>
33 #include <svl/outstrm.hxx>
34 #include <utility>
36 using namespace com::sun::star;
38 class SvDataPipe_Impl
40 public:
41 enum SeekResult { SEEK_BEFORE_MARKED, SEEK_OK, SEEK_PAST_END };
43 private:
44 struct Page
46 Page * m_pPrev;
47 Page * m_pNext;
48 sal_Int8 * m_pStart;
49 sal_Int8 * m_pRead;
50 sal_Int8 * m_pEnd;
51 sal_uInt32 m_nOffset;
52 sal_Int8 m_aBuffer[1];
54 static const sal_uInt32 m_nPageSize = 1000;
56 std::multiset< sal_uInt32 > m_aMarks;
57 Page * m_pFirstPage;
58 Page * m_pReadPage;
59 Page * m_pWritePage;
60 sal_Int8 * m_pReadBuffer;
61 sal_uInt32 m_nReadBufferSize;
62 sal_uInt32 m_nReadBufferFilled;
63 sal_uInt32 m_nPages;
64 bool m_bEOF;
66 void remove(Page * pPage);
68 public:
69 inline SvDataPipe_Impl();
71 ~SvDataPipe_Impl();
73 inline void setReadBuffer(sal_Int8 * pBuffer, sal_uInt32 nSize);
75 sal_uInt32 read();
77 void clearReadBuffer() { m_pReadBuffer = nullptr; }
79 void write(sal_Int8 const * pBuffer, sal_uInt32 nSize);
81 void setEOF() { m_bEOF = true; }
83 inline bool isEOF() const;
85 SeekResult setReadPosition(sal_uInt32 nPosition);
88 SvDataPipe_Impl::SvDataPipe_Impl()
89 : m_pFirstPage( nullptr )
90 , m_pReadPage( nullptr )
91 , m_pWritePage( nullptr )
92 , m_pReadBuffer( nullptr )
93 , m_nReadBufferSize( 0 )
94 , m_nReadBufferFilled( 0 )
95 , m_nPages( 0 )
96 , m_bEOF( false )
99 inline void SvDataPipe_Impl::setReadBuffer(sal_Int8 * pBuffer,
100 sal_uInt32 nSize)
102 m_pReadBuffer = pBuffer;
103 m_nReadBufferSize = nSize;
104 m_nReadBufferFilled = 0;
107 inline bool SvDataPipe_Impl::isEOF() const
109 return m_bEOF && m_pReadPage == m_pWritePage
110 && (!m_pReadPage || m_pReadPage->m_pRead == m_pReadPage->m_pEnd);
115 // SvInputStream
117 bool SvInputStream::open()
119 if (GetError() != ERRCODE_NONE)
120 return false;
121 if (!(m_xSeekable.is() || m_pPipe))
123 if (!m_xStream.is())
125 SetError(ERRCODE_IO_INVALIDDEVICE);
126 return false;
128 m_xSeekable.set(m_xStream, uno::UNO_QUERY);
129 if (!m_xSeekable.is())
130 m_pPipe.reset( new SvDataPipe_Impl );
132 return true;
135 // virtual
136 std::size_t SvInputStream::GetData(void * pData, std::size_t const nSize)
138 if (!open())
140 SetError(ERRCODE_IO_CANTREAD);
141 return 0;
143 // check if a truncated STREAM_SEEK_TO_END was passed
144 assert(m_nSeekedFrom != SAL_MAX_UINT32);
145 sal_uInt32 nRead = 0;
146 if (m_xSeekable.is())
148 if (m_nSeekedFrom != STREAM_SEEK_TO_END)
152 m_xSeekable->seek(m_nSeekedFrom);
154 catch (const io::IOException&)
156 SetError(ERRCODE_IO_CANTREAD);
157 return 0;
159 m_nSeekedFrom = STREAM_SEEK_TO_END;
161 for (;;)
163 sal_Int32 nRemain
164 = sal_Int32(
165 std::min(std::size_t(nSize - nRead),
166 std::size_t(std::numeric_limits<sal_Int32>::max())));
167 if (nRemain == 0)
168 break;
169 uno::Sequence< sal_Int8 > aBuffer;
170 sal_Int32 nCount;
173 nCount = m_xStream->readBytes(aBuffer, nRemain);
175 catch (const io::IOException&)
177 SetError(ERRCODE_IO_CANTREAD);
178 return nRead;
180 memcpy(static_cast< sal_Int8 * >(pData) + nRead,
181 aBuffer.getConstArray(), sal_uInt32(nCount));
182 nRead += nCount;
183 if (nCount < nRemain)
184 break;
187 else
189 if (m_nSeekedFrom != STREAM_SEEK_TO_END)
191 SetError(ERRCODE_IO_CANTREAD);
192 return 0;
194 m_pPipe->setReadBuffer(static_cast< sal_Int8 * >(pData), nSize);
195 nRead = m_pPipe->read();
196 if (nRead < nSize && !m_pPipe->isEOF())
197 for (;;)
199 sal_Int32 nRemain
200 = sal_Int32(
201 std::min(
202 std::size_t(nSize - nRead),
203 std::size_t(std::numeric_limits<sal_Int32>::max())));
204 if (nRemain == 0)
205 break;
206 uno::Sequence< sal_Int8 > aBuffer;
207 sal_Int32 nCount;
210 nCount = m_xStream->readBytes(aBuffer, nRemain);
212 catch (const io::IOException&)
214 SetError(ERRCODE_IO_CANTREAD);
215 break;
217 m_pPipe->write(aBuffer.getConstArray(), sal_uInt32(nCount));
218 nRead += m_pPipe->read();
219 if (nCount < nRemain)
221 m_xStream->closeInput();
222 m_pPipe->setEOF();
223 break;
226 m_pPipe->clearReadBuffer();
228 return nRead;
231 // virtual
232 std::size_t SvInputStream::PutData(void const *, std::size_t)
234 SetError(ERRCODE_IO_NOTSUPPORTED);
235 return 0;
238 // virtual
239 void SvInputStream::FlushData()
242 // virtual
243 sal_uInt64 SvInputStream::SeekPos(sal_uInt64 const nPos)
245 // check if a truncated STREAM_SEEK_TO_END was passed
246 assert(nPos != SAL_MAX_UINT32);
247 if (open())
249 if (nPos == STREAM_SEEK_TO_END)
251 if (m_nSeekedFrom == STREAM_SEEK_TO_END)
253 if (m_xSeekable.is())
256 sal_Int64 nLength = m_xSeekable->getLength();
257 OSL_ASSERT(nLength >= 0);
258 if (o3tl::make_unsigned(nLength)
259 < STREAM_SEEK_TO_END)
261 m_nSeekedFrom = Tell();
262 return sal_uInt64(nLength);
265 catch (const io::IOException&)
268 else
269 return Tell(); //@@@
271 else
272 return Tell();
274 else if (nPos == m_nSeekedFrom)
276 m_nSeekedFrom = STREAM_SEEK_TO_END;
277 return nPos;
279 else if (m_xSeekable.is())
283 m_xSeekable->seek(nPos);
284 m_nSeekedFrom = STREAM_SEEK_TO_END;
285 return nPos;
287 catch (const io::IOException&)
291 else if (m_pPipe->setReadPosition(nPos) == SvDataPipe_Impl::SEEK_OK)
293 m_nSeekedFrom = STREAM_SEEK_TO_END;
294 return nPos;
296 else if ( nPos > Tell() )
298 // Read out the bytes
299 sal_Int32 nRead = nPos - Tell();
300 uno::Sequence< sal_Int8 > aBuffer;
301 m_xStream->readBytes( aBuffer, nRead );
302 return nPos;
304 else if ( nPos == Tell() )
305 return nPos;
307 SetError(ERRCODE_IO_CANTSEEK);
308 return Tell();
311 // virtual
312 void SvInputStream::SetSize(sal_uInt64)
314 SetError(ERRCODE_IO_NOTSUPPORTED);
317 SvInputStream::SvInputStream( css::uno::Reference< css::io::XInputStream > xTheStream):
318 m_xStream(std::move(xTheStream)),
319 m_nSeekedFrom(STREAM_SEEK_TO_END)
321 SetBufferSize(0);
324 // virtual
325 SvInputStream::~SvInputStream()
327 if (m_xStream.is())
331 m_xStream->closeInput();
333 catch (const io::IOException&)
339 // SvOutputStream
341 // virtual
342 std::size_t SvOutputStream::GetData(void *, std::size_t)
344 SetError(ERRCODE_IO_NOTSUPPORTED);
345 return 0;
348 // virtual
349 std::size_t SvOutputStream::PutData(void const * pData, std::size_t nSize)
351 if (!m_xStream.is())
353 SetError(ERRCODE_IO_CANTWRITE);
354 return 0;
356 std::size_t nWritten = 0;
357 for (;;)
359 sal_Int32 nRemain
360 = sal_Int32(
361 std::min(std::size_t(nSize - nWritten),
362 std::size_t(std::numeric_limits<sal_Int32>::max())));
363 if (nRemain == 0)
364 break;
367 m_xStream->writeBytes(uno::Sequence< sal_Int8 >(
368 static_cast<const sal_Int8 * >(pData)
369 + nWritten,
370 nRemain));
372 catch (const io::IOException&)
374 SetError(ERRCODE_IO_CANTWRITE);
375 break;
377 nWritten += nRemain;
379 return nWritten;
382 // virtual
383 sal_uInt64 SvOutputStream::SeekPos(sal_uInt64)
385 SetError(ERRCODE_IO_NOTSUPPORTED);
386 return 0;
389 // virtual
390 void SvOutputStream::FlushData()
392 if (!m_xStream.is())
394 SetError(ERRCODE_IO_INVALIDDEVICE);
395 return;
399 m_xStream->flush();
401 catch (const io::IOException&)
406 // virtual
407 void SvOutputStream::SetSize(sal_uInt64)
409 SetError(ERRCODE_IO_NOTSUPPORTED);
412 SvOutputStream::SvOutputStream(uno::Reference< io::XOutputStream > xTheStream):
413 m_xStream(std::move(xTheStream))
415 SetBufferSize(0);
418 // virtual
419 SvOutputStream::~SvOutputStream()
421 if (m_xStream.is())
425 m_xStream->closeOutput();
427 catch (const io::IOException&)
434 // SvDataPipe_Impl
437 void SvDataPipe_Impl::remove(Page * pPage)
439 if (
440 pPage != m_pFirstPage ||
441 m_pReadPage == m_pFirstPage ||
443 !m_aMarks.empty() &&
444 *m_aMarks.begin() < m_pFirstPage->m_nOffset + m_nPageSize
448 return;
451 m_pFirstPage = m_pFirstPage->m_pNext;
453 if (m_nPages <= 100) // min pages
454 return;
456 pPage->m_pPrev->m_pNext = pPage->m_pNext;
457 pPage->m_pNext->m_pPrev = pPage->m_pPrev;
458 std::free(pPage);
459 --m_nPages;
462 SvDataPipe_Impl::~SvDataPipe_Impl()
464 if (m_pFirstPage != nullptr)
465 for (Page * pPage = m_pFirstPage;;)
467 Page * pNext = pPage->m_pNext;
468 std::free(pPage);
469 if (pNext == m_pFirstPage)
470 break;
471 pPage = pNext;
475 sal_uInt32 SvDataPipe_Impl::read()
477 if (m_pReadBuffer == nullptr || m_nReadBufferSize == 0 || m_pReadPage == nullptr)
478 return 0;
480 sal_uInt32 nSize = m_nReadBufferSize;
481 sal_uInt32 nRemain = m_nReadBufferSize - m_nReadBufferFilled;
483 m_pReadBuffer += m_nReadBufferFilled;
484 m_nReadBufferSize -= m_nReadBufferFilled;
485 m_nReadBufferFilled = 0;
487 while (nRemain > 0)
489 sal_uInt32 nBlock = std::min(sal_uInt32(m_pReadPage->m_pEnd
490 - m_pReadPage->m_pRead),
491 nRemain);
492 memcpy(m_pReadBuffer, m_pReadPage->m_pRead, nBlock);
493 m_pReadPage->m_pRead += nBlock;
494 m_pReadBuffer += nBlock;
495 m_nReadBufferSize -= nBlock;
496 m_nReadBufferFilled = 0;
497 nRemain -= nBlock;
499 if (m_pReadPage == m_pWritePage)
500 break;
502 if (m_pReadPage->m_pRead == m_pReadPage->m_pEnd)
504 Page * pRemove = m_pReadPage;
505 m_pReadPage = pRemove->m_pNext;
506 remove(pRemove);
510 return nSize - nRemain;
513 void SvDataPipe_Impl::write(sal_Int8 const * pBuffer, sal_uInt32 nSize)
515 if (nSize == 0)
516 return;
518 if (m_pWritePage == nullptr)
520 m_pFirstPage
521 = static_cast< Page * >(std::malloc(sizeof (Page)
522 + m_nPageSize
523 - 1));
524 m_pFirstPage->m_pPrev = m_pFirstPage;
525 m_pFirstPage->m_pNext = m_pFirstPage;
526 m_pFirstPage->m_pStart = m_pFirstPage->m_aBuffer;
527 m_pFirstPage->m_pRead = m_pFirstPage->m_aBuffer;
528 m_pFirstPage->m_pEnd = m_pFirstPage->m_aBuffer;
529 m_pFirstPage->m_nOffset = 0;
530 m_pReadPage = m_pFirstPage;
531 m_pWritePage = m_pFirstPage;
532 ++m_nPages;
535 sal_uInt32 nRemain = nSize;
537 if (m_pReadBuffer != nullptr && m_pReadPage == m_pWritePage
538 && m_pReadPage->m_pRead == m_pWritePage->m_pEnd)
540 sal_uInt32 nBlock = std::min(nRemain,
541 sal_uInt32(m_nReadBufferSize
542 - m_nReadBufferFilled));
543 sal_uInt32 nPosition = m_pWritePage->m_nOffset
544 + (m_pWritePage->m_pEnd
545 - m_pWritePage->m_aBuffer);
546 if (!m_aMarks.empty())
547 nBlock = *m_aMarks.begin() > nPosition ?
548 std::min(nBlock, sal_uInt32(*m_aMarks.begin()
549 - nPosition)) :
552 if (nBlock > 0)
554 memcpy(m_pReadBuffer + m_nReadBufferFilled, pBuffer,
555 nBlock);
556 m_nReadBufferFilled += nBlock;
557 nRemain -= nBlock;
559 nPosition += nBlock;
560 m_pWritePage->m_nOffset = (nPosition / m_nPageSize) * m_nPageSize;
561 m_pWritePage->m_pStart = m_pWritePage->m_aBuffer
562 + nPosition % m_nPageSize;
563 m_pWritePage->m_pRead = m_pWritePage->m_pStart;
564 m_pWritePage->m_pEnd = m_pWritePage->m_pStart;
568 if (nRemain <= 0)
569 return;
571 for (;;)
573 sal_uInt32 nBlock
574 = std::min(sal_uInt32(m_pWritePage->m_aBuffer + m_nPageSize
575 - m_pWritePage->m_pEnd),
576 nRemain);
577 memcpy(m_pWritePage->m_pEnd, pBuffer, nBlock);
578 m_pWritePage->m_pEnd += nBlock;
579 pBuffer += nBlock;
580 nRemain -= nBlock;
582 if (nRemain == 0)
583 break;
585 if (m_pWritePage->m_pNext == m_pFirstPage)
587 if (m_nPages == std::numeric_limits< sal_uInt32 >::max())
588 break;
590 Page * pNew
591 = static_cast< Page * >(std::malloc(
592 sizeof (Page) + m_nPageSize
593 - 1));
594 pNew->m_pPrev = m_pWritePage;
595 pNew->m_pNext = m_pWritePage->m_pNext;
597 m_pWritePage->m_pNext->m_pPrev = pNew;
598 m_pWritePage->m_pNext = pNew;
599 ++m_nPages;
602 m_pWritePage->m_pNext->m_nOffset = m_pWritePage->m_nOffset
603 + m_nPageSize;
604 m_pWritePage = m_pWritePage->m_pNext;
605 m_pWritePage->m_pStart = m_pWritePage->m_aBuffer;
606 m_pWritePage->m_pRead = m_pWritePage->m_aBuffer;
607 m_pWritePage->m_pEnd = m_pWritePage->m_aBuffer;
611 SvDataPipe_Impl::SeekResult SvDataPipe_Impl::setReadPosition(sal_uInt32
612 nPosition)
614 if (m_pFirstPage == nullptr)
615 return nPosition == 0 ? SEEK_OK : SEEK_PAST_END;
617 if (nPosition
618 <= m_pReadPage->m_nOffset
619 + (m_pReadPage->m_pRead - m_pReadPage->m_aBuffer))
621 if (nPosition
622 < m_pFirstPage->m_nOffset
623 + (m_pFirstPage->m_pStart - m_pFirstPage->m_aBuffer))
624 return SEEK_BEFORE_MARKED;
626 while (nPosition < m_pReadPage->m_nOffset)
628 m_pReadPage->m_pRead = m_pReadPage->m_pStart;
629 m_pReadPage = m_pReadPage->m_pPrev;
632 else
634 if (nPosition
635 > m_pWritePage->m_nOffset
636 + (m_pWritePage->m_pEnd - m_pWritePage->m_aBuffer))
637 return SEEK_PAST_END;
639 while (m_pReadPage != m_pWritePage
640 && nPosition >= m_pReadPage->m_nOffset + m_nPageSize)
642 Page * pRemove = m_pReadPage;
643 m_pReadPage = pRemove->m_pNext;
644 remove(pRemove);
648 m_pReadPage->m_pRead = m_pReadPage->m_aBuffer
649 + (nPosition - m_pReadPage->m_nOffset);
650 return SEEK_OK;
653 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */