Fix build
[LibreOffice.git] / sc / inc / tabprotection.hxx
bloba799012fbcd41d2bb200d8a17a855c3f537ea341
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 .
20 #pragma once
22 #include <sal/types.h>
24 #include "global.hxx"
25 #include "rangelst.hxx"
26 #include <memory>
28 class ScDocument;
29 class ScTableProtectionImpl;
31 enum ScPasswordHash
33 PASSHASH_SHA1 = 0,
34 PASSHASH_SHA1_UTF8, // tdf#115483 this is UTF8, previous one is wrong UTF16
35 PASSHASH_SHA256,
36 PASSHASH_XL,
37 PASSHASH_UNSPECIFIED
40 /// OOXML password definitions: algorithmName, hashValue, saltValue, spinCount
41 struct ScOoxPasswordHash
43 OUString maAlgorithmName; /// "SHA-512", ...
44 OUString maHashValue; /// base64 encoded hash value
45 OUString maSaltValue; /// base64 encoded salt value
46 sal_uInt32 mnSpinCount; /// spin count, iteration runs
48 ScOoxPasswordHash() : mnSpinCount(0) {}
49 bool hasPassword() const { return !maHashValue.isEmpty(); }
50 void clear()
52 // Keep algorithm and spin count.
53 maHashValue.clear();
54 maSaltValue.clear();
56 bool verifyPassword( const OUString& aPassText ) const;
59 namespace ScPassHashHelper
61 /** Check for the compatibility of all password hashes. If there is at
62 * least one hash that needs to be regenerated, it returns true. If all
63 * hash values are compatible with the specified hash type, then it
64 * returns false. */
65 bool needsPassHashRegen(const ScDocument& rDoc, ScPasswordHash eHash1, ScPasswordHash eHash2 = PASSHASH_UNSPECIFIED);
67 const OUString & getHashURI(ScPasswordHash eHash);
69 ScPasswordHash getHashTypeFromURI(std::u16string_view rURI);
72 class SAL_NO_VTABLE ScPassHashProtectable
74 public:
75 virtual ~ScPassHashProtectable() = 0;
77 virtual bool isProtected() const = 0;
78 virtual bool isProtectedWithPass() const = 0;
79 virtual void setProtected(bool bProtected) = 0;
81 virtual bool isPasswordEmpty() const = 0;
82 virtual bool hasPasswordHash(ScPasswordHash eHash, ScPasswordHash eHash2 = PASSHASH_UNSPECIFIED) const = 0;
83 virtual void setPassword(const OUString& aPassText) = 0;
84 virtual css::uno::Sequence<sal_Int8> getPasswordHash(
85 ScPasswordHash eHash, ScPasswordHash eHas2 = PASSHASH_UNSPECIFIED) const = 0;
86 virtual const ScOoxPasswordHash& getPasswordHash() const = 0;
87 virtual void setPasswordHash(
88 const css::uno::Sequence<sal_Int8>& aPassword,
89 ScPasswordHash eHash, ScPasswordHash eHash2 = PASSHASH_UNSPECIFIED) = 0;
90 virtual void setPasswordHash( const OUString& rAlgorithmName, const OUString& rHashValue,
91 const OUString& rSaltValue, sal_uInt32 nSpinCount ) = 0;
92 virtual bool verifyPassword(const OUString& aPassText) const = 0;
95 class SAL_DLLPUBLIC_RTTI ScDocProtection final : public ScPassHashProtectable
97 public:
98 enum Option
100 STRUCTURE = 0,
101 WINDOWS,
102 NONE ///< last item - used to resize the vector
105 SC_DLLPUBLIC explicit ScDocProtection();
106 explicit ScDocProtection(const ScDocProtection& r);
107 SC_DLLPUBLIC virtual ~ScDocProtection() override;
109 SC_DLLPUBLIC virtual bool isProtected() const override;
110 virtual bool isProtectedWithPass() const override;
111 SC_DLLPUBLIC virtual void setProtected(bool bProtected) override;
113 virtual bool isPasswordEmpty() const override;
114 virtual bool hasPasswordHash(ScPasswordHash eHash, ScPasswordHash eHash2 = PASSHASH_UNSPECIFIED) const override;
115 virtual void setPassword(const OUString& aPassText) override;
116 SC_DLLPUBLIC virtual css::uno::Sequence<sal_Int8> getPasswordHash(
117 ScPasswordHash eHash, ScPasswordHash eHash2 = PASSHASH_UNSPECIFIED) const override;
118 virtual const ScOoxPasswordHash& getPasswordHash() const override;
119 SC_DLLPUBLIC virtual void setPasswordHash(
120 const css::uno::Sequence<sal_Int8>& aPassword,
121 ScPasswordHash eHash, ScPasswordHash eHash2 = PASSHASH_UNSPECIFIED) override;
122 virtual void setPasswordHash( const OUString& rAlgorithmName, const OUString& rHashValue,
123 const OUString& rSaltValue, sal_uInt32 nSpinCount ) override;
124 SC_DLLPUBLIC virtual bool verifyPassword(const OUString& aPassText) const override;
126 SC_DLLPUBLIC bool isOptionEnabled(Option eOption) const;
127 SC_DLLPUBLIC void setOption(Option eOption, bool bEnabled);
129 private:
130 std::unique_ptr<ScTableProtectionImpl> mpImpl;
133 /** Container for the Excel EnhancedProtection feature.
135 struct ScEnhancedProtection
137 ScRangeListRef maRangeList;
138 sal_uInt32 mnAreserved;
139 sal_uInt32 mnPasswordVerifier;
140 OUString maTitle;
141 ::std::vector< sal_uInt8 > maSecurityDescriptor; // imported as raw BIFF data
142 OUString maSecurityDescriptorXML; // imported from OOXML
143 ScOoxPasswordHash maPasswordHash;
145 ScEnhancedProtection() : mnAreserved(0), mnPasswordVerifier(0) {}
147 bool hasSecurityDescriptor() const
149 return !maSecurityDescriptor.empty() || !maSecurityDescriptorXML.isEmpty();
152 bool hasPassword() const
154 return mnPasswordVerifier != 0 || maPasswordHash.hasPassword();
158 /** sheet protection state container
160 * This class stores sheet's protection state: 1) whether the protection
161 * is on, 2) password and/or password hash, and 3) any associated
162 * protection options. This class is also used as a protection state
163 * container for the undo/redo stack, in which case the password, hash and
164 * the options need to be preserved even when the protection flag is
165 * off. */
166 class SC_DLLPUBLIC ScTableProtection final : public ScPassHashProtectable
168 public:
169 enum Option
171 AUTOFILTER = 0,
172 DELETE_COLUMNS,
173 DELETE_ROWS,
174 FORMAT_CELLS,
175 FORMAT_COLUMNS,
176 FORMAT_ROWS,
177 INSERT_COLUMNS,
178 INSERT_HYPERLINKS,
179 INSERT_ROWS,
180 OBJECTS,
181 PIVOT_TABLES,
182 SCENARIOS,
183 SELECT_LOCKED_CELLS,
184 SELECT_UNLOCKED_CELLS,
185 SORT,
186 NONE ///< last item - used to resize the vector
189 explicit ScTableProtection();
190 explicit ScTableProtection(const ScTableProtection& r);
191 virtual ~ScTableProtection() override;
193 virtual bool isProtected() const override;
194 virtual bool isProtectedWithPass() const override;
195 virtual void setProtected(bool bProtected) override;
197 virtual bool isPasswordEmpty() const override;
198 virtual bool hasPasswordHash(ScPasswordHash eHash, ScPasswordHash eHash2 = PASSHASH_UNSPECIFIED) const override;
199 virtual void setPassword(const OUString& aPassText) override;
200 virtual css::uno::Sequence<sal_Int8> getPasswordHash(
201 ScPasswordHash eHash, ScPasswordHash eHash2 = PASSHASH_UNSPECIFIED) const override;
202 virtual const ScOoxPasswordHash& getPasswordHash() const override;
203 virtual void setPasswordHash(
204 const css::uno::Sequence<sal_Int8>& aPassword,
205 ScPasswordHash eHash, ScPasswordHash eHash2 = PASSHASH_UNSPECIFIED) override;
206 virtual void setPasswordHash( const OUString& rAlgorithmName, const OUString& rHashValue,
207 const OUString& rSaltValue, sal_uInt32 nSpinCount ) override;
208 virtual bool verifyPassword(const OUString& aPassText) const override;
210 bool isOptionEnabled(Option eOption) const;
211 void setOption(Option eOption, bool bEnabled);
213 void setEnhancedProtection( ::std::vector< ScEnhancedProtection > && rProt );
214 const ::std::vector< ScEnhancedProtection > & getEnhancedProtection() const;
215 bool updateReference( UpdateRefMode, const ScDocument&, const ScRange& rWhere, SCCOL nDx, SCROW nDy, SCTAB nDz );
216 bool isBlockEditable( const ScRange& rRange ) const;
217 bool isSelectionEditable( const ScRangeList& rRangeList ) const;
219 private:
220 std::unique_ptr<ScTableProtectionImpl> mpImpl;
223 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */