Debian Codegraph @

src/lib/QXP4Deobfuscator.cpp

raw
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*
3 * This file is part of the libqxp 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
10 #include "QXP4Deobfuscator.h"
11
12 namespace libqxp
13 {
14
15 namespace
16 {
17
18 uint16_t fill(uint16_t value, uint16_t shift, uint16_t mask)
19 {
20 uint16_t r = shift;
21 uint16_t v = value;
22 while ((v & 1) == 0 && r > 0)
23 {
24 v >>= 1;
25 r--;
26 }
27 const uint16_t s = shift - r;
28 const uint16_t m = (0xffff >> s) << s;
29 return (value | m) & mask;
30 }
31
32 uint16_t shift(uint16_t value, uint16_t count)
33 {
34 const uint16_t mask = 0xffff >> (16 - count);
35 const uint16_t highinit = value & mask;
36 const uint16_t high = fill(highinit | (value >> 15), count, mask) << (16 - count);
37 return high | (value >> count);
38 }
39
40 }
41
42 QXP4Deobfuscator::QXP4Deobfuscator(uint16_t seed, uint16_t increment)
43 : QXPDeobfuscator(seed)
44 , m_increment(increment)
45 {
46 }
47
48 void QXP4Deobfuscator::next(uint16_t block)
49 {
50 m_seed += m_increment;
51 m_increment = shift(m_increment, block & 0xf);
52 }
53
54 void QXP4Deobfuscator::nextRev()
55 {
56 m_seed += 0xffff - m_increment;
57 }
58
59 void QXP4Deobfuscator::nextShift(uint16_t count)
60 {
61 m_seed = shift(m_seed, count & 0xf);
62 }
63
64 }
65
66 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */