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 
11 #ifndef RTC_BASE_CRYPT_STRING_H_
12 #define RTC_BASE_CRYPT_STRING_H_
13 
14 #include <string.h>
15 
16 #include <memory>
17 #include <string>
18 #include <vector>
19 
20 namespace rtc {
21 
22 class CryptStringImpl {
23  public:
~CryptStringImpl()24   virtual ~CryptStringImpl() {}
25   virtual size_t GetLength() const = 0;
26   virtual void CopyTo(char* dest, bool nullterminate) const = 0;
27   virtual std::string UrlEncode() const = 0;
28   virtual CryptStringImpl* Copy() const = 0;
29   virtual void CopyRawTo(std::vector<unsigned char>* dest) const = 0;
30 };
31 
32 class EmptyCryptStringImpl : public CryptStringImpl {
33  public:
~EmptyCryptStringImpl()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();
GetLength()45   size_t GetLength() const { return impl_->GetLength(); }
CopyTo(char * dest,bool nullterminate)46   void CopyTo(char* dest, bool nullterminate) const {
47     impl_->CopyTo(dest, nullterminate);
48   }
49   CryptString(const CryptString& other);
50   explicit CryptString(const CryptStringImpl& impl);
51   ~CryptString();
52   CryptString& operator=(const CryptString& other) {
53     if (this != &other) {
54       impl_.reset(other.impl_->Copy());
55     }
56     return *this;
57   }
Clear()58   void Clear() { impl_.reset(new EmptyCryptStringImpl()); }
UrlEncode()59   std::string UrlEncode() const { return impl_->UrlEncode(); }
CopyRawTo(std::vector<unsigned char> * dest)60   void CopyRawTo(std::vector<unsigned char>* dest) const {
61     return impl_->CopyRawTo(dest);
62   }
63 
64  private:
65   std::unique_ptr<const CryptStringImpl> impl_;
66 };
67 
68 }  // namespace rtc
69 
70 #endif  // RTC_BASE_CRYPT_STRING_H_
71