/src/libwpd/src/lib/WPXEncryption.cpp
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ |
2 | | /* libwpd |
3 | | * Version: MPL 2.0 / LGPLv2.1+ |
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 | | * Major Contributor(s): |
10 | | * Copyright (C) 2007 Fridrich Strba (fridrich.strba@bluewin.ch) |
11 | | * |
12 | | * For minor contributions see the git repository. |
13 | | * |
14 | | * Alternatively, the contents of this file may be used under the terms |
15 | | * of the GNU Lesser General Public License Version 2.1 or later |
16 | | * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are |
17 | | * applicable instead of those above. |
18 | | * |
19 | | * For further information visit http://libwpd.sourceforge.net |
20 | | */ |
21 | | |
22 | | /* "This product is not manufactured, approved, or supported by |
23 | | * Corel Corporation or Corel Corporation Limited." |
24 | | */ |
25 | | |
26 | | #include "WPXEncryption.h" |
27 | | #include "libwpd_internal.h" |
28 | | #include <string.h> |
29 | | |
30 | | WPXEncryption::WPXEncryption(const char *password, const unsigned long encryptionStartOffset) : |
31 | 36.1k | m_buffer(nullptr), |
32 | 36.1k | m_password(), |
33 | 36.1k | m_encryptionStartOffset(encryptionStartOffset), |
34 | 36.1k | m_encryptionMaskBase(0) |
35 | 36.1k | { |
36 | 36.1k | if (password) |
37 | 36.1k | { |
38 | 36.1k | for (unsigned long i = 0; i < strlen(password); i++) |
39 | 0 | if (password[i] >= 'a' && password[i] <= 'z') |
40 | 0 | m_password.append((char)(password[i] - 'a' + 'A')); |
41 | 0 | else |
42 | 0 | m_password.append(password[i]); |
43 | 36.1k | m_encryptionMaskBase = (unsigned char)(m_password.len() + 1); |
44 | 36.1k | } |
45 | 36.1k | } |
46 | | |
47 | | WPXEncryption::~WPXEncryption() |
48 | 36.1k | { |
49 | 36.1k | } |
50 | | |
51 | | |
52 | | unsigned short WPXEncryption::getCheckSum() const |
53 | 20.8k | { |
54 | 20.8k | if (m_password.len() <= 0) |
55 | 20.8k | return 0; |
56 | 0 | librevenge::RVNGString::Iter i(m_password); |
57 | 0 | unsigned short checkSum = 0; |
58 | 0 | for (i.rewind(); i.next();) |
59 | 0 | checkSum = (unsigned short)(((checkSum >> 1) | (checkSum << 15)) ^ (((unsigned short)*(i())) << 8)); |
60 | 0 | WPD_DEBUG_MSG(("CheckSum: 0x%.4x\n", checkSum)); |
61 | 0 | return checkSum; |
62 | 20.8k | } |
63 | | |
64 | | const unsigned char *WPXEncryption::readAndDecrypt(librevenge::RVNGInputStream *input, unsigned long numBytes, unsigned long &numBytesRead) |
65 | 79.4M | { |
66 | 79.4M | if ((m_password.len() <= 0) || (m_encryptionStartOffset > input->tell() + numBytes)) |
67 | 79.4M | return input->read(numBytes, numBytesRead); |
68 | | |
69 | 0 | unsigned long readStartPosition = input->tell(); |
70 | 0 | if (readStartPosition == (unsigned long)-1) |
71 | 0 | return nullptr; |
72 | 0 | const unsigned char *encryptedBuffer = input->read(numBytes, numBytesRead); |
73 | 0 | m_buffer.reset(new unsigned char[numBytesRead]); |
74 | 0 | for (unsigned long i=0; i<numBytesRead; i++) |
75 | 0 | { |
76 | 0 | if (readStartPosition + i < m_encryptionStartOffset) |
77 | 0 | m_buffer[i] = encryptedBuffer[i]; |
78 | 0 | else |
79 | 0 | { |
80 | 0 | unsigned long passwordOffset = (readStartPosition + i - m_encryptionStartOffset) % m_password.len(); |
81 | 0 | auto encryptionMask = (unsigned char)(m_encryptionMaskBase + readStartPosition + i - m_encryptionStartOffset); |
82 | 0 | m_buffer[i] = (unsigned char)(encryptedBuffer[i] ^ (m_password.cstr()[passwordOffset] ^ encryptionMask)); |
83 | 0 | } |
84 | 0 | } |
85 | 0 | return m_buffer.get(); |
86 | 0 | } |
87 | | /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */ |