poppler/Decrypt.cc | 134 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 99 insertions(+), 35 deletions(-)
New commits: commit e0ef346c0f669140076c4cf443f07ea0770996da Author: Albert Astals Cid <[email protected]> Date: Fri Sep 21 00:32:24 2018 +0200 Make the mul tables be calculated at compile time with constexpr diff --git a/poppler/Decrypt.cc b/poppler/Decrypt.cc index db66786c..ed405856 100644 --- a/poppler/Decrypt.cc +++ b/poppler/Decrypt.cc @@ -762,55 +762,119 @@ static inline void invShiftRows(unsigned char *state) { } // {02} \cdot s -static inline unsigned char mul02(unsigned char s) { - return (s & 0x80) ? ((s << 1) ^ 0x1b) : (s << 1); -} +struct Mul02Table +{ + constexpr Mul02Table() : values() + { + for(int s = 0; s < 256; s++) { + values[s] = (s & 0x80) ? ((s << 1) ^ 0x1b) : (s << 1); + } + } + + constexpr unsigned char operator()(uint8_t i) const { return values[i]; } + + unsigned char values[256]; +}; + +static constexpr Mul02Table mul02; // {03} \cdot s -static inline unsigned char mul03(unsigned char s) { - unsigned char s2 = (s & 0x80) ? ((s << 1) ^ 0x1b) : (s << 1); - return s ^ s2; -} +struct Mul03Table +{ + constexpr Mul03Table() : values() + { + for(int s=0; s<256; s++) { + const unsigned char s2 = (s & 0x80) ? ((s << 1) ^ 0x1b) : (s << 1); + values[s] = s ^ s2; + } + } + + constexpr unsigned char operator()(uint8_t i) const { return values[i]; } + + unsigned char values[256]; +}; + +static constexpr Mul03Table mul03; // {09} \cdot s -static inline unsigned char mul09(unsigned char s) { - unsigned char s2, s4, s8; +struct Mul09Table +{ + constexpr Mul09Table() : values() + { + for(int s=0; s<256; s++) { + const unsigned char s2 = (s & 0x80) ? ((s << 1) ^ 0x1b) : (s << 1); + const unsigned char s4 = (s2 & 0x80) ? ((s2 << 1) ^ 0x1b) : (s2 << 1); + const unsigned char s8 = (s4 & 0x80) ? ((s4 << 1) ^ 0x1b) : (s4 << 1); + values[s] = s ^ s8; + } + } - s2 = (s & 0x80) ? ((s << 1) ^ 0x1b) : (s << 1); - s4 = (s2 & 0x80) ? ((s2 << 1) ^ 0x1b) : (s2 << 1); - s8 = (s4 & 0x80) ? ((s4 << 1) ^ 0x1b) : (s4 << 1); - return s ^ s8; -} + constexpr unsigned char operator()(uint8_t i) const { return values[i]; } + + unsigned char values[256]; +}; + +static constexpr Mul09Table mul09; // {0b} \cdot s -static inline unsigned char mul0b(unsigned char s) { - unsigned char s2, s4, s8; +struct Mul0bTable +{ + constexpr Mul0bTable() : values() + { + for(int s=0; s<256; s++) { + const unsigned char s2 = (s & 0x80) ? ((s << 1) ^ 0x1b) : (s << 1); + const unsigned char s4 = (s2 & 0x80) ? ((s2 << 1) ^ 0x1b) : (s2 << 1); + const unsigned char s8 = (s4 & 0x80) ? ((s4 << 1) ^ 0x1b) : (s4 << 1); + values[s] = s ^ s2 ^ s8; + } + } - s2 = (s & 0x80) ? ((s << 1) ^ 0x1b) : (s << 1); - s4 = (s2 & 0x80) ? ((s2 << 1) ^ 0x1b) : (s2 << 1); - s8 = (s4 & 0x80) ? ((s4 << 1) ^ 0x1b) : (s4 << 1); - return s ^ s2 ^ s8; -} + constexpr unsigned char operator()(uint8_t i) const { return values[i]; } + + unsigned char values[256]; +}; + +static constexpr Mul0bTable mul0b; // {0d} \cdot s -static inline unsigned char mul0d(unsigned char s) { - unsigned char s2, s4, s8; +struct Mul0dTable +{ + constexpr Mul0dTable() : values() + { + for(int s=0; s<256; s++) { + const unsigned char s2 = (s & 0x80) ? ((s << 1) ^ 0x1b) : (s << 1); + const unsigned char s4 = (s2 & 0x80) ? ((s2 << 1) ^ 0x1b) : (s2 << 1); + const unsigned char s8 = (s4 & 0x80) ? ((s4 << 1) ^ 0x1b) : (s4 << 1); + values[s] = s ^ s4 ^ s8; + } + } - s2 = (s & 0x80) ? ((s << 1) ^ 0x1b) : (s << 1); - s4 = (s2 & 0x80) ? ((s2 << 1) ^ 0x1b) : (s2 << 1); - s8 = (s4 & 0x80) ? ((s4 << 1) ^ 0x1b) : (s4 << 1); - return s ^ s4 ^ s8; -} + constexpr unsigned char operator()(uint8_t i) const { return values[i]; } + + unsigned char values[256]; +}; + +static constexpr Mul0dTable mul0d; // {0e} \cdot s -static inline unsigned char mul0e(unsigned char s) { - unsigned char s2, s4, s8; +struct Mul0eTable +{ + constexpr Mul0eTable() : values() + { + for(int s=0; s<256; s++) { + const unsigned char s2 = (s & 0x80) ? ((s << 1) ^ 0x1b) : (s << 1); + const unsigned char s4 = (s2 & 0x80) ? ((s2 << 1) ^ 0x1b) : (s2 << 1); + const unsigned char s8 = (s4 & 0x80) ? ((s4 << 1) ^ 0x1b) : (s4 << 1); + values[s] = s2 ^ s4 ^ s8; + } + } - s2 = (s & 0x80) ? ((s << 1) ^ 0x1b) : (s << 1); - s4 = (s2 & 0x80) ? ((s2 << 1) ^ 0x1b) : (s2 << 1); - s8 = (s4 & 0x80) ? ((s4 << 1) ^ 0x1b) : (s4 << 1); - return s2 ^ s4 ^ s8; -} + constexpr unsigned char operator()(uint8_t i) const { return values[i]; } + + unsigned char values[256]; +}; + +static constexpr Mul0eTable mul0e; static inline void mixColumns(unsigned char *state) { int c; _______________________________________________ poppler mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/poppler
