crypt_string.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright 2004 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef RTC_BASE_CRYPT_STRING_H_
  11. #define RTC_BASE_CRYPT_STRING_H_
  12. #include <string.h>
  13. #include <memory>
  14. #include <string>
  15. #include <vector>
  16. namespace rtc {
  17. class CryptStringImpl {
  18. public:
  19. virtual ~CryptStringImpl() {}
  20. virtual size_t GetLength() const = 0;
  21. virtual void CopyTo(char* dest, bool nullterminate) const = 0;
  22. virtual std::string UrlEncode() const = 0;
  23. virtual CryptStringImpl* Copy() const = 0;
  24. virtual void CopyRawTo(std::vector<unsigned char>* dest) const = 0;
  25. };
  26. class EmptyCryptStringImpl : public CryptStringImpl {
  27. public:
  28. ~EmptyCryptStringImpl() override {}
  29. size_t GetLength() const override;
  30. void CopyTo(char* dest, bool nullterminate) const override;
  31. std::string UrlEncode() const override;
  32. CryptStringImpl* Copy() const override;
  33. void CopyRawTo(std::vector<unsigned char>* dest) const override;
  34. };
  35. class CryptString {
  36. public:
  37. CryptString();
  38. size_t GetLength() const { return impl_->GetLength(); }
  39. void CopyTo(char* dest, bool nullterminate) const {
  40. impl_->CopyTo(dest, nullterminate);
  41. }
  42. CryptString(const CryptString& other);
  43. explicit CryptString(const CryptStringImpl& impl);
  44. ~CryptString();
  45. CryptString& operator=(const CryptString& other) {
  46. if (this != &other) {
  47. impl_.reset(other.impl_->Copy());
  48. }
  49. return *this;
  50. }
  51. void Clear() { impl_.reset(new EmptyCryptStringImpl()); }
  52. std::string UrlEncode() const { return impl_->UrlEncode(); }
  53. void CopyRawTo(std::vector<unsigned char>* dest) const {
  54. return impl_->CopyRawTo(dest);
  55. }
  56. private:
  57. std::unique_ptr<const CryptStringImpl> impl_;
  58. };
  59. } // namespace rtc
  60. #endif // RTC_BASE_CRYPT_STRING_H_