使用跨越具有不同字节顺序不同平台的memcpy

问题描述 投票:-3回答:1

我写这从uint32_t的copys数据到一个std :: vector的功能。这一计划将跨越具有不同字节序不同的平台上使用(主要是LE但是一些BE)

目前我使用这样的:

std:vector<uint8_t> Decryption::GenerateIvForBlock(uint32_t blockNumber)
{
    std:vector<uint8_t> ivInput(AES128_KEY_BYTE_LENGTH, 0);

// Some code
if (///////)
{
    memcpy(&ivInput[0], &blockNumber, sizeof(blockNumber));
}
} 

目前的行为是在不同的输入端平台的不同,即使是blockNumber跨平台相同。不幸的是,我不能很容易测试大端系统,所以我努力制定出一个解决方案。

c++ endianness memcpy
1个回答
1
投票

作为πάνταῥεῖ建议的,ntohx() / htonx()功能可以帮助您从网络字节顺序到主机字节顺序和背部转换16位和32位无符号的积分。

还有的是为包括ntoh()hton()在C ++ <net>头,但据我所知,没有suceess模板一些草稿。你可能会发现在<Winsock2.h><arpa/inet.h>的C函数,所以你可以自己创建模板。例:

#include <cstdint>
#ifdef _WIN32
 #include <Winsock2.h>
#else
 #include <arpa/inet.h>
#endif

template <class T>
T hton(T host) noexcept = delete;

template <>
uint8_t hton(uint8_t host) noexcept {
    return host;
}

template <>
uint16_t hton(uint16_t host) noexcept {
    return htons(host);
}

template <>
uint32_t hton(uint32_t host) noexcept {
    return htonl(host);
}

template <class T>
T ntoh(T network) noexcept = delete;

template <>
uint8_t ntoh(uint8_t network) noexcept {
    return network;
}

template <>
uint16_t ntoh(uint16_t network) noexcept {
    return ntohs(network);
}

template <>
uint32_t ntoh(uint32_t network) noexcept {
    return ntohl(network);
}

有了这些之后,就可以创建与运营商流的载体,以保存和恢复数据的包装类模板。下面是一个非常简单的例子。根据需要添加运营商等。

template <typename T>
class Data {
    std::vector<T> m_data;
public:
    Data() : m_data{} {}

    template< class... Args >
    auto emplace_back( Args&&... args ) {
        return m_data.emplace_back(std::forward<Args>(args)...);
    }
    void reserve(typename std::vector<T>::size_type size) {
        m_data.reserve(size);
    }
    bool operator==(const Data& o) {
        return m_data == o.m_data;
    }

    template <typename V>
    friend std::ostream& operator<<(std::ostream&, const Data<V>&);
    template <typename V>
    friend std::istream& operator>>(std::istream&, Data<V>&);
};

template <typename T>
std::ostream& operator<<(std::ostream& os, const Data<T>& d) {
    // write the number of entries first
    const uint32_t count = hton(static_cast<const uint32_t>(d.m_data.size()));
    os.write(reinterpret_cast<const char*>(&count), sizeof(count));
    // write all the entries, converted to network byte order
    for(auto v : d.m_data) {
        v = hton(v);
        os.write(reinterpret_cast<const char*>(&v), sizeof(v));
    }
    return os;
}

template <typename T>
std::istream& operator>>(std::istream& is, Data<T>& d) {
    // read the number of entries first
    uint32_t count;
    is.read(reinterpret_cast<char*>(&count), sizeof(count));
    d.m_data.resize(ntoh(count));
    // read all the entries and convert to host byte order
    for(auto& v : d.m_data) {
        is.read(reinterpret_cast<char*>(&v), sizeof(v));
        v = ntoh(v);
    }
    return is;
}

测试:

#include <iostream>
#include <vector>
#include <sstream>

int main() {
    Data<unsigned> orig;
    Data<unsigned> copy;
    std::stringstream ss;
    orig.reserve(1024*1024);
    for(unsigned i=0; i<1024*1024; ++i) orig.emplace_back(i);
    ss << orig; // save to stream
    // the data in 'ss' is now in network byte order
    ss >> copy; // restore from stream
    if(orig==copy) std::cout << "happy\n";
}
© www.soinside.com 2019 - 2024. All rights reserved.