/src/libreoffice/io/source/stm/streamhelper.cxx
Line | Count | Source (jump to first uncovered line) |
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 <limits> |
21 | | #include <string.h> |
22 | | |
23 | | #include <com/sun/star/uno/Sequence.hxx> |
24 | | |
25 | | #include <com/sun/star/io/BufferSizeExceededException.hpp> |
26 | | |
27 | | using namespace ::com::sun::star::uno; |
28 | | |
29 | | #include "streamhelper.hxx" |
30 | | |
31 | | namespace io_stm { |
32 | | |
33 | | void MemFIFO::write( const Sequence< sal_Int8 > &seq ) |
34 | 0 | { |
35 | 0 | writeAt(getSize(), seq); |
36 | 0 | } |
37 | | |
38 | | void MemFIFO::read( Sequence<sal_Int8> &seq , sal_Int32 nBufferLen ) |
39 | 0 | { |
40 | 0 | readAt(0, seq , nBufferLen); |
41 | 0 | forgetFromStart( nBufferLen ); |
42 | 0 | } |
43 | | |
44 | | void MemFIFO::skip( sal_Int32 nBytesToSkip ) |
45 | 0 | { |
46 | 0 | forgetFromStart( nBytesToSkip ); |
47 | 0 | } |
48 | | |
49 | 0 | MemRingBuffer::MemRingBuffer() : m_p(nullptr), m_nBufferLen(0), m_nStart(0), m_nOccupiedBuffer(0) |
50 | 0 | { |
51 | 0 | } |
52 | | |
53 | | MemRingBuffer::~MemRingBuffer() |
54 | 0 | { |
55 | 0 | std::free( m_p ); |
56 | 0 | } |
57 | | |
58 | | void MemRingBuffer::resizeBuffer( sal_Int32 nMinSize ) |
59 | 0 | { |
60 | 0 | sal_Int32 nNewLen = 1; |
61 | |
|
62 | 0 | while( nMinSize > nNewLen ) { |
63 | 0 | nNewLen = nNewLen << 1; |
64 | 0 | } |
65 | | |
66 | | // buffer never shrinks ! |
67 | 0 | if( nNewLen < m_nBufferLen ) { |
68 | 0 | nNewLen = m_nBufferLen; |
69 | 0 | } |
70 | |
|
71 | 0 | if( nNewLen == m_nBufferLen ) |
72 | 0 | return; |
73 | | |
74 | 0 | auto p = static_cast<sal_Int8*>(std::realloc(m_p, nNewLen)); |
75 | 0 | if (!p) |
76 | 0 | throw css::io::BufferSizeExceededException( |
77 | 0 | u"MemRingBuffer::resizeBuffer BufferSizeExceededException"_ustr); |
78 | | |
79 | 0 | m_p = p; |
80 | | |
81 | |
|
82 | 0 | if( m_nStart + m_nOccupiedBuffer > m_nBufferLen ) { |
83 | 0 | memmove( &( m_p[m_nStart+(nNewLen-m_nBufferLen)]) , &(m_p[m_nStart]) , m_nBufferLen - m_nStart ); |
84 | 0 | m_nStart += nNewLen - m_nBufferLen; |
85 | 0 | } |
86 | 0 | m_nBufferLen = nNewLen; |
87 | 0 | } |
88 | | |
89 | | |
90 | | void MemRingBuffer::readAt( sal_Int32 nPos, Sequence<sal_Int8> &seq , sal_Int32 nBytesToRead ) const |
91 | 0 | { |
92 | 0 | if( nPos + nBytesToRead > m_nOccupiedBuffer ) { |
93 | 0 | throw css::io::BufferSizeExceededException( |
94 | 0 | u"MemRingBuffer::readAt BufferSizeExceededException"_ustr); |
95 | 0 | } |
96 | | |
97 | 0 | sal_Int32 nStartReadingPos = nPos + m_nStart; |
98 | 0 | if( nStartReadingPos >= m_nBufferLen ) { |
99 | 0 | nStartReadingPos -= m_nBufferLen; |
100 | 0 | } |
101 | |
|
102 | 0 | seq.realloc( nBytesToRead ); |
103 | |
|
104 | 0 | if( nStartReadingPos + nBytesToRead > m_nBufferLen ) { |
105 | 0 | sal_Int32 nDeltaLen = m_nBufferLen - nStartReadingPos; |
106 | 0 | memcpy( seq.getArray() , &(m_p[nStartReadingPos]) , nDeltaLen ); |
107 | 0 | memcpy( &(seq.getArray()[nDeltaLen]), m_p , nBytesToRead - nDeltaLen ); |
108 | 0 | } |
109 | 0 | else { |
110 | 0 | memcpy( seq.getArray() , &(m_p[nStartReadingPos]) , nBytesToRead ); |
111 | 0 | } |
112 | 0 | } |
113 | | |
114 | | |
115 | | void MemRingBuffer::writeAt( sal_Int32 nPos, const Sequence<sal_Int8> &seq ) |
116 | 0 | { |
117 | 0 | checkInvariants(); |
118 | 0 | const sal_Int32 nLen = seq.getLength(); |
119 | |
|
120 | 0 | if( nPos < 0 || nPos > std::numeric_limits< sal_Int32 >::max() - nLen ) |
121 | 0 | { |
122 | 0 | throw css::io::BufferSizeExceededException( |
123 | 0 | u"MemRingBuffer::writeAt BufferSizeExceededException"_ustr); |
124 | 0 | } |
125 | | |
126 | 0 | if( nPos + nLen - m_nOccupiedBuffer > 0 ) { |
127 | 0 | resizeBuffer( nPos + nLen ); |
128 | 0 | m_nOccupiedBuffer = nPos + nLen; |
129 | 0 | } |
130 | |
|
131 | 0 | sal_Int32 nStartWritingIndex = m_nStart + nPos; |
132 | 0 | if( nStartWritingIndex >= m_nBufferLen ) { |
133 | 0 | nStartWritingIndex -= m_nBufferLen; |
134 | 0 | } |
135 | |
|
136 | 0 | if( const sal_Int32 nBufferRestLen = m_nBufferLen-nStartWritingIndex; nLen > nBufferRestLen ) { |
137 | | // two area copy |
138 | 0 | memcpy( &(m_p[nStartWritingIndex]) , seq.getConstArray(), nBufferRestLen ); |
139 | 0 | memcpy( m_p , &( seq.getConstArray()[nBufferRestLen] ), nLen - nBufferRestLen ); |
140 | |
|
141 | 0 | } |
142 | 0 | else { |
143 | | // one area copy |
144 | 0 | memcpy( &( m_p[nStartWritingIndex]), seq.getConstArray() , nLen ); |
145 | 0 | } |
146 | 0 | checkInvariants(); |
147 | 0 | } |
148 | | |
149 | | |
150 | | sal_Int32 MemRingBuffer::getSize() const noexcept |
151 | 0 | { |
152 | 0 | return m_nOccupiedBuffer; |
153 | 0 | } |
154 | | |
155 | | void MemRingBuffer::forgetFromStart( sal_Int32 nBytesToForget ) |
156 | 0 | { |
157 | 0 | checkInvariants(); |
158 | 0 | if( nBytesToForget > m_nOccupiedBuffer ) { |
159 | 0 | throw css::io::BufferSizeExceededException( |
160 | 0 | u"MemRingBuffer::forgetFromStart BufferSizeExceededException"_ustr); |
161 | 0 | } |
162 | 0 | m_nStart += nBytesToForget; |
163 | 0 | if( m_nStart >= m_nBufferLen ) { |
164 | 0 | m_nStart = m_nStart - m_nBufferLen; |
165 | 0 | } |
166 | 0 | m_nOccupiedBuffer -= nBytesToForget; |
167 | 0 | checkInvariants(); |
168 | 0 | } |
169 | | |
170 | | |
171 | | } |
172 | | |
173 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |