deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 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 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #ifndef RTC_BASE_CRYPT_STRING_H_ |
| 12 | #define RTC_BASE_CRYPT_STRING_H_ |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 13 | |
| 14 | #include <string.h> |
| 15 | |
| 16 | #include <memory> |
| 17 | #include <string> |
| 18 | #include <vector> |
| 19 | |
| 20 | namespace rtc { |
| 21 | |
| 22 | class CryptStringImpl { |
Joachim Bauch | 5b32f23 | 2018-03-07 19:02:26 | [diff] [blame] | 23 | public: |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 24 | virtual ~CryptStringImpl() {} |
| 25 | virtual size_t GetLength() const = 0; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 26 | virtual void CopyTo(char* dest, bool nullterminate) const = 0; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 27 | virtual std::string UrlEncode() const = 0; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 28 | virtual CryptStringImpl* Copy() const = 0; |
| 29 | virtual void CopyRawTo(std::vector<unsigned char>* dest) const = 0; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | class EmptyCryptStringImpl : public CryptStringImpl { |
Joachim Bauch | 5b32f23 | 2018-03-07 19:02:26 | [diff] [blame] | 33 | public: |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 34 | ~EmptyCryptStringImpl() override {} |
| 35 | size_t GetLength() const override; |
| 36 | void CopyTo(char* dest, bool nullterminate) const override; |
| 37 | std::string UrlEncode() const override; |
| 38 | CryptStringImpl* Copy() const override; |
| 39 | void CopyRawTo(std::vector<unsigned char>* dest) const override; |
| 40 | }; |
| 41 | |
| 42 | class CryptString { |
| 43 | public: |
| 44 | CryptString(); |
| 45 | size_t GetLength() const { return impl_->GetLength(); } |
Joachim Bauch | 5b32f23 | 2018-03-07 19:02:26 | [diff] [blame] | 46 | void CopyTo(char* dest, bool nullterminate) const { |
| 47 | impl_->CopyTo(dest, nullterminate); |
| 48 | } |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 49 | CryptString(const CryptString& other); |
| 50 | explicit CryptString(const CryptStringImpl& impl); |
| 51 | ~CryptString(); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 52 | CryptString& operator=(const CryptString& other) { |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 53 | if (this != &other) { |
| 54 | impl_.reset(other.impl_->Copy()); |
| 55 | } |
| 56 | return *this; |
| 57 | } |
| 58 | void Clear() { impl_.reset(new EmptyCryptStringImpl()); } |
| 59 | std::string UrlEncode() const { return impl_->UrlEncode(); } |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 60 | void CopyRawTo(std::vector<unsigned char>* dest) const { |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 61 | return impl_->CopyRawTo(dest); |
| 62 | } |
| 63 | |
| 64 | private: |
| 65 | std::unique_ptr<const CryptStringImpl> impl_; |
| 66 | }; |
deadbeef | f137e97 | 2017-03-23 22:45:49 | [diff] [blame] | 67 | |
Joachim Bauch | 5b32f23 | 2018-03-07 19:02:26 | [diff] [blame] | 68 | } // namespace rtc |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 69 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 70 | #endif // RTC_BASE_CRYPT_STRING_H_ |