Skip to content

Commit

Permalink
use std::string_view instead const std::string&
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Jan 12, 2025
1 parent efd8e6e commit 634cece
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 52 deletions.
6 changes: 3 additions & 3 deletions libi2pd/Blinding.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013-2022, The PurpleI2P Project
* Copyright (c) 2013-2025, The PurpleI2P Project
*
* This file is part of Purple i2pd project and licensed under BSD3
*
Expand Down Expand Up @@ -152,11 +152,11 @@ namespace data
m_BlindedSigType = m_SigType;
}

BlindedPublicKey::BlindedPublicKey (const std::string& b33):
BlindedPublicKey::BlindedPublicKey (std::string_view b33):
m_SigType (0) // 0 means invalid, we can't blind DSA, set it later
{
uint8_t addr[40]; // TODO: define length from b33
size_t l = i2p::data::Base32ToByteStream (b33.c_str (), b33.length (), addr, 40);
size_t l = i2p::data::Base32ToByteStream (b33.data (), b33.length (), addr, 40);
if (l < 32)
{
LogPrint (eLogError, "Blinding: Malformed b33 ", b33);
Expand Down
5 changes: 3 additions & 2 deletions libi2pd/Blinding.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013-2020, The PurpleI2P Project
* Copyright (c) 2013-2025, The PurpleI2P Project
*
* This file is part of Purple i2pd project and licensed under BSD3
*
Expand All @@ -11,6 +11,7 @@

#include <inttypes.h>
#include <string>
#include <string_view>
#include <vector>
#include "Identity.h"

Expand All @@ -23,7 +24,7 @@ namespace data
public:

BlindedPublicKey (std::shared_ptr<const IdentityEx> identity, bool clientAuth = false);
BlindedPublicKey (const std::string& b33); // from b33 without .b32.i2p
BlindedPublicKey (std::string_view b33); // from b33 without .b32.i2p
std::string ToB33 () const;

const uint8_t * GetPublicKey () const { return m_PublicKey.data (); };
Expand Down
8 changes: 3 additions & 5 deletions libi2pd/Identity.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013-2024, The PurpleI2P Project
* Copyright (c) 2013-2025, The PurpleI2P Project
*
* This file is part of Purple i2pd project and licensed under BSD3
*
Expand Down Expand Up @@ -262,11 +262,11 @@ namespace data
return fullLen;
}

size_t IdentityEx::FromBase64(const std::string& s)
size_t IdentityEx::FromBase64(std::string_view s)
{
const size_t slen = s.length();
std::vector<uint8_t> buf(slen); // binary data can't exceed base64
const size_t len = Base64ToByteStream (s.c_str(), slen, buf.data(), slen);
const size_t len = Base64ToByteStream (s.data(), slen, buf.data(), slen);
return FromBuffer (buf.data(), len);
}

Expand Down Expand Up @@ -728,9 +728,7 @@ namespace data
case SIGNING_KEY_TYPE_RSA_SHA384_3072:
case SIGNING_KEY_TYPE_RSA_SHA512_4096:
LogPrint (eLogWarning, "Identity: RSA signature type is not supported. Creating EdDSA");
#if (__cplusplus >= 201703L) // C++ 17 or higher
[[fallthrough]];
#endif
// no break here
case SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519:
i2p::crypto::CreateEDDSA25519RandomKeys (priv, pub);
Expand Down
5 changes: 3 additions & 2 deletions libi2pd/Identity.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013-2024, The PurpleI2P Project
* Copyright (c) 2013-2025, The PurpleI2P Project
*
* This file is part of Purple i2pd project and licensed under BSD3
*
Expand All @@ -12,6 +12,7 @@
#include <inttypes.h>
#include <string.h>
#include <string>
#include <string_view>
#include <memory>
#include <vector>
#include "Base.h"
Expand Down Expand Up @@ -99,7 +100,7 @@ namespace data

size_t FromBuffer (const uint8_t * buf, size_t len);
size_t ToBuffer (uint8_t * buf, size_t len) const;
size_t FromBase64(const std::string& s);
size_t FromBase64(std::string_view s);
std::string ToBase64 () const;
const Identity& GetStandardIdentity () const { return m_StandardIdentity; };

Expand Down
18 changes: 11 additions & 7 deletions libi2pd/Tag.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013-2023, The PurpleI2P Project
* Copyright (c) 2013-2025, The PurpleI2P Project
*
* This file is part of Purple i2pd project and licensed under BSD3
*
Expand All @@ -12,10 +12,14 @@
#include <boost/static_assert.hpp>
#include <string.h>
#include <openssl/rand.h>
#include <string>
#include <string_view>
#include "Base.h"

namespace i2p {
namespace data {
namespace i2p
{
namespace data
{
template<size_t sz>
class Tag
{
Expand Down Expand Up @@ -70,14 +74,14 @@ namespace data {
return std::string (str, str + l);
}

size_t FromBase32 (const std::string& s)
size_t FromBase32 (std::string_view s)
{
return i2p::data::Base32ToByteStream (s.c_str (), s.length (), m_Buf, sz);
return i2p::data::Base32ToByteStream (s.data (), s.length (), m_Buf, sz);
}

size_t FromBase64 (const std::string& s)
size_t FromBase64 (std::string_view s)
{
return i2p::data::Base64ToByteStream (s.c_str (), s.length (), m_Buf, sz);
return i2p::data::Base64ToByteStream (s.data (), s.length (), m_Buf, sz);
}

uint8_t GetBit (int i) const
Expand Down
44 changes: 22 additions & 22 deletions libi2pd_client/AddressBook.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013-2024, The PurpleI2P Project
* Copyright (c) 2013-2025, The PurpleI2P Project
*
* This file is part of Purple i2pd project and licensed under BSD3
*
Expand Down Expand Up @@ -49,22 +49,22 @@ namespace client
if (m_IsPersist)
i2p::config::GetOption("addressbook.hostsfile", m_HostsFile);
}
std::shared_ptr<const i2p::data::IdentityEx> GetAddress (const i2p::data::IdentHash& ident) const;
void AddAddress (std::shared_ptr<const i2p::data::IdentityEx> address);
void RemoveAddress (const i2p::data::IdentHash& ident);
std::shared_ptr<const i2p::data::IdentityEx> GetAddress (const i2p::data::IdentHash& ident) const override;
void AddAddress (std::shared_ptr<const i2p::data::IdentityEx> address) override;
void RemoveAddress (const i2p::data::IdentHash& ident) override;

bool Init ();
int Load (std::map<std::string, std::shared_ptr<Address> > & addresses);
int LoadLocal (std::map<std::string, std::shared_ptr<Address> >& addresses);
int Save (const std::map<std::string, std::shared_ptr<Address> >& addresses);
bool Init () override;
int Load (Addresses& addresses) override;
int LoadLocal (Addresses& addresses) override;
int Save (const Addresses& addresses) override;

void SaveEtag (const i2p::data::IdentHash& subsciption, const std::string& etag, const std::string& lastModified);
bool GetEtag (const i2p::data::IdentHash& subscription, std::string& etag, std::string& lastModified);
void ResetEtags ();
void SaveEtag (const i2p::data::IdentHash& subsciption, const std::string& etag, const std::string& lastModified) override;
bool GetEtag (const i2p::data::IdentHash& subscription, std::string& etag, std::string& lastModified) override;
void ResetEtags () override;

private:

int LoadFromFile (const std::string& filename, std::map<std::string, std::shared_ptr<Address> >& addresses); // returns -1 if can't open file, otherwise number of records
int LoadFromFile (const std::string& filename, Addresses& addresses); // returns -1 if can't open file, otherwise number of records

private:

Expand Down Expand Up @@ -142,7 +142,7 @@ namespace client
storage.Remove( ident.ToBase32() );
}

int AddressBookFilesystemStorage::LoadFromFile (const std::string& filename, std::map<std::string, std::shared_ptr<Address> >& addresses)
int AddressBookFilesystemStorage::LoadFromFile (const std::string& filename, Addresses& addresses)
{
int num = 0;
std::ifstream f (filename, std::ifstream::in); // in text mode
Expand All @@ -168,7 +168,7 @@ namespace client
return num;
}

int AddressBookFilesystemStorage::Load (std::map<std::string, std::shared_ptr<Address> >& addresses)
int AddressBookFilesystemStorage::Load (Addresses& addresses)
{
int num = LoadFromFile (indexPath, addresses);
if (num < 0)
Expand All @@ -182,15 +182,15 @@ namespace client
return num;
}

int AddressBookFilesystemStorage::LoadLocal (std::map<std::string, std::shared_ptr<Address> >& addresses)
int AddressBookFilesystemStorage::LoadLocal (Addresses& addresses)
{
int num = LoadFromFile (localPath, addresses);
if (num < 0) return 0;
LogPrint (eLogInfo, "Addressbook: ", num, " local addresses loaded");
return num;
}

int AddressBookFilesystemStorage::Save (const std::map<std::string, std::shared_ptr<Address> >& addresses)
int AddressBookFilesystemStorage::Save (const Addresses& addresses)
{
if (addresses.empty())
{
Expand Down Expand Up @@ -283,7 +283,7 @@ namespace client

//---------------------------------------------------------------------

Address::Address (const std::string& b32):
Address::Address (std::string_view b32):
addressType (eAddressInvalid)
{
if (b32.length () <= B33_ADDRESS_THRESHOLD)
Expand Down Expand Up @@ -377,7 +377,7 @@ namespace client
m_Subscriptions.clear ();
}

std::shared_ptr<const Address> AddressBook::GetAddress (const std::string& address)
std::shared_ptr<const Address> AddressBook::GetAddress (std::string_view address)
{
auto pos = address.find(".b32.i2p");
if (pos != std::string::npos)
Expand All @@ -404,7 +404,7 @@ namespace client
return std::make_shared<const Address>(dest.GetIdentHash ());
}

std::shared_ptr<const Address> AddressBook::FindAddress (const std::string& address)
std::shared_ptr<const Address> AddressBook::FindAddress (std::string_view address)
{
auto it = m_Addresses.find (address);
if (it != m_Addresses.end ())
Expand Down Expand Up @@ -609,7 +609,7 @@ namespace client
void AddressBook::LoadLocal ()
{
if (!m_Storage) return;
std::map<std::string, std::shared_ptr<Address>> localAddresses;
AddressBookStorage::Addresses localAddresses;
m_Storage->LoadLocal (localAddresses);
for (const auto& it: localAddresses)
{
Expand Down Expand Up @@ -766,7 +766,7 @@ namespace client
}
}

void AddressBook::LookupAddress (const std::string& address)
void AddressBook::LookupAddress (std::string_view address)
{
std::shared_ptr<const Address> addr;
auto dot = address.find ('.');
Expand Down Expand Up @@ -796,7 +796,7 @@ namespace client
memset (buf, 0, 4);
htobe32buf (buf + 4, nonce);
buf[8] = address.length ();
memcpy (buf + 9, address.c_str (), address.length ());
memcpy (buf + 9, address.data (), address.length ());
datagram->SendDatagramTo (buf, len, addr->identHash, ADDRESS_RESPONSE_DATAGRAM_PORT, ADDRESS_RESOLVER_DATAGRAM_PORT);
delete[] buf;
}
Expand Down
22 changes: 12 additions & 10 deletions libi2pd_client/AddressBook.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013-2024, The PurpleI2P Project
* Copyright (c) 2013-2025, The PurpleI2P Project
*
* This file is part of Purple i2pd project and licensed under BSD3
*
Expand Down Expand Up @@ -47,7 +47,7 @@ namespace client
i2p::data::IdentHash identHash;
std::shared_ptr<i2p::data::BlindedPublicKey> blindedPublicKey;

Address (const std::string& b32);
Address (std::string_view b32);
Address (const i2p::data::IdentHash& hash);
bool IsIdentHash () const { return addressType == eAddressIndentHash; };
bool IsValid () const { return addressType != eAddressInvalid; };
Expand All @@ -59,15 +59,17 @@ namespace client
{
public:

typedef std::map<std::string, std::shared_ptr<Address>, std::less<> > Addresses;

virtual ~AddressBookStorage () {};
virtual std::shared_ptr<const i2p::data::IdentityEx> GetAddress (const i2p::data::IdentHash& ident) const = 0;
virtual void AddAddress (std::shared_ptr<const i2p::data::IdentityEx> address) = 0;
virtual void RemoveAddress (const i2p::data::IdentHash& ident) = 0;

virtual bool Init () = 0;
virtual int Load (std::map<std::string, std::shared_ptr<Address> >& addresses) = 0;
virtual int LoadLocal (std::map<std::string, std::shared_ptr<Address> >& addresses) = 0;
virtual int Save (const std::map<std::string, std::shared_ptr<Address> >& addresses) = 0;
virtual int Load (Addresses& addresses) = 0;
virtual int LoadLocal (Addresses& addresses) = 0;
virtual int Save (const Addresses& addresses) = 0;

virtual void SaveEtag (const i2p::data::IdentHash& subscription, const std::string& etag, const std::string& lastModified) = 0;
virtual bool GetEtag (const i2p::data::IdentHash& subscription, std::string& etag, std::string& lastModified) = 0;
Expand All @@ -79,16 +81,16 @@ namespace client
class AddressBook
{
public:

AddressBook ();
~AddressBook ();
void Start ();
void StartResolvers ();
void Stop ();
std::shared_ptr<const Address> GetAddress (const std::string& address);
std::shared_ptr<const Address> GetAddress (std::string_view address);
std::shared_ptr<const i2p::data::IdentityEx> GetFullAddress (const std::string& address);
std::shared_ptr<const Address> FindAddress (const std::string& address);
void LookupAddress (const std::string& address);
std::shared_ptr<const Address> FindAddress (std::string_view address);
void LookupAddress (std::string_view address);
void InsertAddress (const std::string& address, const std::string& jump); // for jump links
void InsertFullAddress (std::shared_ptr<const i2p::data::IdentityEx> address);

Expand Down Expand Up @@ -121,7 +123,7 @@ namespace client
private:

std::mutex m_AddressBookMutex;
std::map<std::string, std::shared_ptr<Address> > m_Addresses;
AddressBookStorage::Addresses m_Addresses;
std::map<i2p::data::IdentHash, std::shared_ptr<AddressResolver> > m_Resolvers; // local destination->resolver
std::mutex m_LookupsMutex;
std::map<uint32_t, std::string> m_Lookups; // nonce -> address
Expand Down
2 changes: 1 addition & 1 deletion libi2pd_client/I2CP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,7 @@ namespace client
case 1: // address
{
auto name = ExtractString (buf + 11, len - 11);
auto addr = i2p::client::context.GetAddressBook ().GetAddress (std::string (name)); // TODO: GetAddress should take string_view
auto addr = i2p::client::context.GetAddressBook ().GetAddress (name);
if (!addr || !addr->IsIdentHash ())
{
// TODO: handle blinded addresses
Expand Down

0 comments on commit 634cece

Please sign in to comment.