Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
3.6 KiB

  1. // Copyright (c) 2001 Microsoft Corporation
  2. //
  3. // run-encoded string class
  4. //
  5. // 2001-02-08 sburns
  6. #ifndef ENCODEDSTRING_HPP_INCLUDED
  7. #define ENCODEDSTRING_HPP_INCLUDED
  8. // A class that has a similar public interface as class Burnslib::String, but
  9. // is represented as a run-encoded unicode string, using the Rtl functions.
  10. // This class is used to represent password strings in-memory, instead of
  11. // holding them as cleartext, which is a security hole if the memory pages
  12. // are swapped to disk.
  13. class EncodedString
  14. {
  15. typedef String::size_type size_type;
  16. public:
  17. // constucts an empty string.
  18. explicit
  19. EncodedString();
  20. // constructs a copy of an existing, already encoded string
  21. EncodedString(const EncodedString& rhs);
  22. // scribbles out the text, and deletes it.
  23. ~EncodedString()
  24. {
  25. Reset();
  26. }
  27. // Extracts the decoded cleartext representation of the text, including
  28. // null terminator. The caller must free the result with delete[], and
  29. // should scribble it out, too.
  30. //
  31. // Example:
  32. // WCHAR* cleartext = encoded.GetDecodedCopy();
  33. // // use the cleartext
  34. // ::ZeroMemory(cleartext, encoded.GetLength() * sizeof(WCHAR));
  35. // delete[] cleartext;
  36. WCHAR*
  37. EncodedString::GetDecodedCopy() const;
  38. // Returns true if the string is zero-length, false if not.
  39. bool
  40. IsEmpty() const
  41. {
  42. return (GetLength() == 0);
  43. }
  44. // Sets the contents of self to the encoded representation of the
  45. // cleartext, replacing the previous value of self. The encoded
  46. // representation will be the same length, in characters, as the
  47. // cleartext.
  48. //
  49. // clearText - in, un-encoded text to be encoded. May be empty string, but
  50. // not a null pointer.
  51. void
  52. Encode(const WCHAR* cleartext);
  53. // Returns the length, in unicode characters, of the text.
  54. size_type
  55. GetLength() const
  56. {
  57. return cypherText.Length;
  58. }
  59. // Replaces the contents of self with a copy of the contents of rhs.
  60. // Returns *this.
  61. const EncodedString&
  62. operator= (const EncodedString& rhs);
  63. // Compares the cleartext representations of self and rhs, and returns
  64. // true if they are lexicographically the same: the lengths are the same
  65. // and all the characters are the same.
  66. bool
  67. operator== (const EncodedString& rhs) const;
  68. bool
  69. operator!= (const EncodedString& rhs) const
  70. {
  71. return !(operator==(rhs));
  72. }
  73. private:
  74. // scribbles out and frees the internal string.
  75. void
  76. Reset();
  77. // builds the internal encoded representation from the cleartext.
  78. //
  79. // clearText - in, un-encoded text to be encoded. May be empty string, but
  80. // not a null pointer.
  81. void
  82. Init(const WCHAR* clearText);
  83. // We deliberately do not implement conversion to or from wchar_t* or
  84. // String. This is to force the user of the class to be very deliberate
  85. // about decoding the string. class String is a copy-on-write shared
  86. // reference implementation, and we don't want to make it easy to create
  87. // "hidden" copies of cleartext, or move from one representation to
  88. // another, or accidentally get a String filled with encoded text.
  89. // deliberately commented out
  90. // explicit
  91. // EncodedString(const String& cleartext);
  92. operator WCHAR* ();
  93. operator String ();
  94. // In the course of encoding, decoding, and assigning to the instance,
  95. // we may create and destroy these, but logically the string is "const"
  96. mutable UCHAR seed;
  97. mutable UNICODE_STRING cypherText;
  98. };
  99. #endif // ENCODEDSTRING_HPP_INCLUDED