llxorcipher.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * @file llxorcipher.cpp
  3. * @brief Implementation of LLXORCipher
  4. *
  5. * $LicenseInfo:firstyear=2003&license=viewergpl$
  6. *
  7. * Copyright (c) 2003-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "linden_common.h"
  33. #include "llxorcipher.h"
  34. LLXORCipher::LLXORCipher(const U8* pad, U32 pad_len)
  35. : mPad(NULL),
  36. mHead(NULL),
  37. mPadLen(0)
  38. {
  39. init(pad, pad_len);
  40. }
  41. LLXORCipher::LLXORCipher(const std::string& pad)
  42. : mPad(NULL),
  43. mHead(NULL),
  44. mPadLen(0)
  45. {
  46. init((const U8*)pad.data(), (U32)pad.size());
  47. }
  48. LLXORCipher::LLXORCipher(const LLXORCipher& cipher)
  49. : mPad(NULL),
  50. mHead(NULL),
  51. mPadLen(0)
  52. {
  53. init(cipher.mPad, cipher.mPadLen);
  54. }
  55. LLXORCipher::~LLXORCipher()
  56. {
  57. if (mPad)
  58. {
  59. delete[] mPad;
  60. mPad = NULL;
  61. mPadLen = 0;
  62. }
  63. }
  64. void LLXORCipher::init(const U8* pad, U32 pad_len)
  65. {
  66. if (mPad)
  67. {
  68. delete[] mPad;
  69. mPad = NULL;
  70. mPadLen = 0;
  71. }
  72. if (pad && pad_len)
  73. {
  74. mPadLen = pad_len;
  75. mPad = new U8[mPadLen];
  76. if (mPad != NULL)
  77. {
  78. memcpy(mPad, pad, mPadLen);
  79. }
  80. }
  81. mHead = mPad;
  82. }
  83. LLXORCipher& LLXORCipher::operator=(const LLXORCipher& cipher)
  84. {
  85. if (this == &cipher) return *this;
  86. init(cipher.mPad, cipher.mPadLen);
  87. return *this;
  88. }
  89. U32 LLXORCipher::encrypt(const U8* src, U32 src_len, U8* dst)
  90. {
  91. if (!src || !src_len || !dst || !mPad)
  92. {
  93. return 0;
  94. }
  95. U8* pad_end = mPad + mPadLen;
  96. U32 count = src_len;
  97. while (count--)
  98. {
  99. *dst++ = *src++ ^ *mHead++;
  100. if (mHead >= pad_end)
  101. {
  102. mHead = mPad;
  103. }
  104. }
  105. return src_len;
  106. }
  107. U32 LLXORCipher::encrypt(const std::string& src, U8* dst)
  108. {
  109. if (src.empty() || !dst || !mPad)
  110. {
  111. return 0;
  112. }
  113. U8* pad_end = mPad + mPadLen;
  114. U32 count = src.size();
  115. for (U32 i = 0; i < count; ++i)
  116. {
  117. *dst++ = U8(src[i]) ^ *mHead++;
  118. if (mHead >= pad_end)
  119. {
  120. mHead = mPad;
  121. }
  122. }
  123. return count;
  124. }