blob: 470bd00c42c5bea00fb9799081fe7a415f83c417 [file] [log] [blame]
deadbeeff137e972017-03-23 15:45:49 -07001/*
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 Anton10542f22019-01-11 09:11:00 -080011#ifndef RTC_BASE_CRYPT_STRING_H_
12#define RTC_BASE_CRYPT_STRING_H_
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020013
14#include <string.h>
15
16#include <memory>
17#include <string>
18#include <vector>
19
20namespace rtc {
21
22class CryptStringImpl {
Joachim Bauch5b32f232018-03-07 20:02:26 +010023 public:
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020024 virtual ~CryptStringImpl() {}
25 virtual size_t GetLength() const = 0;
Yves Gerey665174f2018-06-19 15:03:05 +020026 virtual void CopyTo(char* dest, bool nullterminate) const = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020027 virtual std::string UrlEncode() const = 0;
Yves Gerey665174f2018-06-19 15:03:05 +020028 virtual CryptStringImpl* Copy() const = 0;
29 virtual void CopyRawTo(std::vector<unsigned char>* dest) const = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020030};
31
32class EmptyCryptStringImpl : public CryptStringImpl {
Joachim Bauch5b32f232018-03-07 20:02:26 +010033 public:
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020034 ~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
42class CryptString {
43 public:
44 CryptString();
45 size_t GetLength() const { return impl_->GetLength(); }
Joachim Bauch5b32f232018-03-07 20:02:26 +010046 void CopyTo(char* dest, bool nullterminate) const {
47 impl_->CopyTo(dest, nullterminate);
48 }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020049 CryptString(const CryptString& other);
50 explicit CryptString(const CryptStringImpl& impl);
51 ~CryptString();
Yves Gerey665174f2018-06-19 15:03:05 +020052 CryptString& operator=(const CryptString& other) {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020053 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 Gerey665174f2018-06-19 15:03:05 +020060 void CopyRawTo(std::vector<unsigned char>* dest) const {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020061 return impl_->CopyRawTo(dest);
62 }
63
64 private:
65 std::unique_ptr<const CryptStringImpl> impl_;
66};
deadbeeff137e972017-03-23 15:45:49 -070067
Joachim Bauch5b32f232018-03-07 20:02:26 +010068} // namespace rtc
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020069
Steve Anton10542f22019-01-11 09:11:00 -080070#endif // RTC_BASE_CRYPT_STRING_H_