llxorcipher.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * @file llxorcipher.h
  3. *
  4. * $LicenseInfo:firstyear=2003&license=viewergpl$
  5. *
  6. * Copyright (c) 2003-2009, Linden Research, Inc.
  7. *
  8. * Second Life Viewer Source Code
  9. * The source code in this file ("Source Code") is provided by Linden Lab
  10. * to you under the terms of the GNU General Public License, version 2.0
  11. * ("GPL"), unless you have obtained a separate licensing agreement
  12. * ("Other License"), formally executed by you and Linden Lab. Terms of
  13. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  14. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  15. *
  16. * There are special exceptions to the terms and conditions of the GPL as
  17. * it is applied to this Source Code. View the full text of the exception
  18. * in the file doc/FLOSS-exception.txt in this software distribution, or
  19. * online at
  20. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  21. *
  22. * By copying, modifying or distributing this software, you acknowledge
  23. * that you have read and understood your obligations described above,
  24. * and agree to abide by those obligations.
  25. *
  26. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  27. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  28. * COMPLETENESS OR PERFORMANCE.
  29. * $/LicenseInfo$
  30. */
  31. #ifndef LLXORCIPHER_H
  32. #define LLXORCIPHER_H
  33. #include "llpreprocessor.h"
  34. #include "stdtypes.h"
  35. class LLXORCipher
  36. {
  37. public:
  38. LLXORCipher(const U8* pad, U32 pad_len);
  39. LLXORCipher(const std::string& pad);
  40. LLXORCipher(const LLXORCipher& cipher);
  41. virtual ~LLXORCipher();
  42. LLXORCipher& operator=(const LLXORCipher& cipher);
  43. // Cipher methods
  44. U32 encrypt(const U8* src, U32 src_len, U8* dst);
  45. U32 encrypt(const std::string& src, U8* dst);
  46. LL_INLINE U32 decrypt(const U8* src, U32 src_len, U8* dst)
  47. {
  48. // Since XOR is a symetric cipher, just call the encrypt() method.
  49. return encrypt(src, src_len, dst);
  50. }
  51. LL_INLINE U32 decrypt(const std::string& src, U8* dst)
  52. {
  53. // Since XOR is a symetric cipher, just call the encrypt() method.
  54. return encrypt(src, dst);
  55. }
  56. // Special syntactic-sugar since xor can be performed in place.
  57. // *BUG: THIS MEANS THAT THE COMPILER GETS FOOLED ABOUT THE CONSTNESS OF
  58. // THE INPUT BUFFER: DO MAKE SURE TO COPY THE CONST INPUT STRING INTO THE
  59. // DESTINATION BEFORE HAND, OR YOUR CONST INPUT SOURCE WILL GET CORRUPTED !
  60. // *TODO: change to fix the above bug.
  61. LL_INLINE U32 encrypt(U8* buf, U32 len)
  62. {
  63. return encrypt((const U8*)buf, len, buf);
  64. }
  65. LL_INLINE U32 decrypt(U8* buf, U32 len)
  66. {
  67. return encrypt((const U8*)buf, len, buf);
  68. }
  69. LL_INLINE U32 encrypt(std::string& src)
  70. {
  71. return encrypt(src, (U8*)src.data());
  72. }
  73. LL_INLINE U32 decrypt(std::string& src)
  74. {
  75. return encrypt(src, (U8*)src.data());
  76. }
  77. protected:
  78. void init(const U8* pad, U32 pad_len);
  79. protected:
  80. U8* mPad;
  81. U8* mHead;
  82. U32 mPadLen;
  83. };
  84. #endif