blob: 470bd00c42c5bea00fb9799081fe7a415f83c417 [file] [log] [blame]
deadbeeff137e972017-03-23 22:45:491/*
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 17:11:0011#ifndef RTC_BASE_CRYPT_STRING_H_
12#define RTC_BASE_CRYPT_STRING_H_
Henrik Kjellanderec78f1c2017-06-29 05:52:5013
14#include <string.h>
15
16#include <memory>
17#include <string>
18#include <vector>
19
20namespace rtc {
21
22class CryptStringImpl {
Joachim Bauch5b32f232018-03-07 19:02:2623 public:
Henrik Kjellanderec78f1c2017-06-29 05:52:5024 virtual ~CryptStringImpl() {}
25 virtual size_t GetLength() const = 0;
Yves Gerey665174f2018-06-19 13:03:0526 virtual void CopyTo(char* dest, bool nullterminate) const = 0;
Henrik Kjellanderec78f1c2017-06-29 05:52:5027 virtual std::string UrlEncode() const = 0;
Yves Gerey665174f2018-06-19 13:03:0528 virtual CryptStringImpl* Copy() const = 0;
29 virtual void CopyRawTo(std::vector<unsigned char>* dest) const = 0;
Henrik Kjellanderec78f1c2017-06-29 05:52:5030};
31
32class EmptyCryptStringImpl : public CryptStringImpl {
Joachim Bauch5b32f232018-03-07 19:02:2633 public:
Henrik Kjellanderec78f1c2017-06-29 05:52:5034 ~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 19:02:2646 void CopyTo(char* dest, bool nullterminate) const {
47 impl_->CopyTo(dest, nullterminate);
48 }
Henrik Kjellanderec78f1c2017-06-29 05:52:5049 CryptString(const CryptString& other);
50 explicit CryptString(const CryptStringImpl& impl);
51 ~CryptString();
Yves Gerey665174f2018-06-19 13:03:0552 CryptString& operator=(const CryptString& other) {
Henrik Kjellanderec78f1c2017-06-29 05:52:5053 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 13:03:0560 void CopyRawTo(std::vector<unsigned char>* dest) const {
Henrik Kjellanderec78f1c2017-06-29 05:52:5061 return impl_->CopyRawTo(dest);
62 }
63
64 private:
65 std::unique_ptr<const CryptStringImpl> impl_;
66};
deadbeeff137e972017-03-23 22:45:4967
Joachim Bauch5b32f232018-03-07 19:02:2668} // namespace rtc
Henrik Kjellanderec78f1c2017-06-29 05:52:5069
Steve Anton10542f22019-01-11 17:11:0070#endif // RTC_BASE_CRYPT_STRING_H_