Avoid potential negative array index access to cached text.
[LibreOffice.git] / include / rtl / byteseq.hxx
blob33118cd8b4e388667c9e86597bb54c8298c65da5
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 * This file is part of LibreOffice published API.
23 #ifndef INCLUDED_RTL_BYTESEQ_HXX
24 #define INCLUDED_RTL_BYTESEQ_HXX
26 #include "rtl/byteseq.h"
28 #include <cstddef>
29 #include <new>
31 namespace rtl
35 inline ByteSequence::ByteSequence()
36 : _pSequence( NULL )
38 ::rtl_byte_sequence_construct( &_pSequence, 0 );
41 inline ByteSequence::ByteSequence( const ByteSequence & rSeq )
42 : _pSequence( NULL )
44 ::rtl_byte_sequence_assign( &_pSequence, rSeq._pSequence );
47 #if defined LIBO_INTERNAL_ONLY
48 inline ByteSequence::ByteSequence( ByteSequence && rSeq ) noexcept
49 : _pSequence(rSeq._pSequence)
51 rSeq._pSequence = nullptr;
53 #endif
55 inline ByteSequence::ByteSequence( sal_Sequence *pSequence)
56 : _pSequence( pSequence )
58 ::rtl_byte_sequence_acquire( pSequence );
61 inline ByteSequence::ByteSequence( const sal_Int8 * pElements, sal_Int32 len )
62 : _pSequence( NULL )
64 ::rtl_byte_sequence_constructFromArray( &_pSequence, pElements, len );
65 if (_pSequence == NULL)
66 throw ::std::bad_alloc();
69 inline ByteSequence::ByteSequence( sal_Int32 len, enum __ByteSequence_NoDefault )
70 : _pSequence( NULL )
72 ::rtl_byte_sequence_constructNoDefault( &_pSequence, len );
73 if (_pSequence == NULL)
74 throw ::std::bad_alloc();
77 inline ByteSequence::ByteSequence( sal_Sequence *pSequence, enum __ByteSequence_NoAcquire )
78 : _pSequence( pSequence )
82 inline ByteSequence::ByteSequence( sal_Int32 len )
83 : _pSequence( NULL )
85 ::rtl_byte_sequence_construct( &_pSequence, len );
86 if (_pSequence == NULL)
87 throw ::std::bad_alloc();
90 inline ByteSequence::~ByteSequence()
92 ::rtl_byte_sequence_release( _pSequence );
95 inline ByteSequence & ByteSequence::operator = ( const ByteSequence & rSeq )
97 ::rtl_byte_sequence_assign( &_pSequence, rSeq._pSequence );
98 return *this;
101 #if defined LIBO_INTERNAL_ONLY
102 inline ByteSequence & ByteSequence::operator = ( ByteSequence && rSeq ) noexcept
104 ::rtl_byte_sequence_release(_pSequence);
105 _pSequence = rSeq._pSequence;
106 rSeq._pSequence = nullptr;
107 return *this;
109 #endif
111 inline bool ByteSequence::operator == ( const ByteSequence & rSeq ) const
113 return ::rtl_byte_sequence_equals( _pSequence, rSeq._pSequence );
116 inline sal_Int8 * ByteSequence::getArray()
118 ::rtl_byte_sequence_reference2One( &_pSequence );
119 if (_pSequence == NULL)
120 throw ::std::bad_alloc();
121 return reinterpret_cast<sal_Int8 *>(_pSequence->elements);
124 inline void ByteSequence::realloc( sal_Int32 nSize )
126 ::rtl_byte_sequence_realloc( &_pSequence, nSize );
127 if (_pSequence == NULL)
128 throw ::std::bad_alloc();
131 inline sal_Int8 & ByteSequence::operator [] ( sal_Int32 nIndex )
133 return getArray()[ nIndex ];
136 inline bool ByteSequence::operator != ( const ByteSequence & rSeq ) const
138 return (! operator == ( rSeq ));
142 #endif
144 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */