Пример #1
0
size_t cmnDataDeSerializeBinary_size_t(size_t & data, const char * buffer, size_t bufferSize,
                                       const cmnDataFormat & localFormat,
                                       const cmnDataFormat & remoteFormat)
{
    // first case, both use same size
    if (remoteFormat.GetSizeTSize() == localFormat.GetSizeTSize()) {
        const size_t sizeOfData = sizeof(size_t);
        if (bufferSize < sizeOfData) {
            return 0;
        }
        data = *(reinterpret_cast<const size_t *>(buffer));
        bufferSize -= sizeOfData;
        return sizeOfData;
    }
    // different sizes
    // local is 64, hence remote is 32
    if (localFormat.GetSizeTSize() == cmnDataFormat::CMN_DATA_SIZE_T_SIZE_64) {
        unsigned int tmp;
        const size_t sizeOfData = sizeof(unsigned int);
        if (bufferSize < sizeOfData) {
            return 0;
        }
        tmp = *(reinterpret_cast<const unsigned int *>(buffer));
        bufferSize -= sizeOfData;
        data = tmp;
        return sizeOfData;
    }
    // local is 32, hence remote is 64
    if (localFormat.GetSizeTSize() == cmnDataFormat::CMN_DATA_SIZE_T_SIZE_32) {
        unsigned long long int tmp;
        const size_t sizeOfData = sizeof(unsigned long long int);
        if (bufferSize < sizeOfData) {
            return 0;
        }
        tmp = *(reinterpret_cast<const unsigned long long int *>(buffer));
        bufferSize -= sizeOfData;
        if (tmp > std::numeric_limits<size_t>::max()) {
            cmnThrow("cmnDataDeSerializeBinary(size_t): received a size_t larger than what can be handled on 32 bits");
        }
        data = static_cast<size_t>(tmp); // this should be safe now
        return sizeOfData;
    }
    return 0;
}
Пример #2
0
void cmnDataDeSerializeBinary_size_t(size_t & data,
                                     std::istream & inputStream,
                                     const cmnDataFormat & localFormat,
                                     const cmnDataFormat & remoteFormat)
    throw (std::runtime_error)
{
    // first case, both use same size
    if (remoteFormat.GetSizeTSize() == localFormat.GetSizeTSize()) {
        inputStream.read(reinterpret_cast<char *>(&data),
                         sizeof(size_t));
        if (inputStream.fail()) {
            cmnThrow("cmnDataDeSerializeBinary(size_t): error occured with std::istream::read");
        }
        return;
    }
    // different sizes
    // local is 64, hence remote is 32
    if (localFormat.GetSizeTSize() == cmnDataFormat::CMN_DATA_SIZE_T_SIZE_64) {
        unsigned int tmp;
        inputStream.read(reinterpret_cast<char *>(&tmp),
                         sizeof(tmp));
        if (inputStream.fail()) {
            cmnThrow("cmnDataDeSerializeBinary(size_t): error occured with std::istream::read");
        }
        data = tmp;
        return;
    }
    // local is 32, hence remote is 64
    if (localFormat.GetSizeTSize() == cmnDataFormat::CMN_DATA_SIZE_T_SIZE_32) {
        unsigned long long int tmp;
        inputStream.read(reinterpret_cast<char *>(&tmp),
                         sizeof(tmp));
        if (inputStream.fail()) {
            cmnThrow("cmnDataDeSerializeBinary(size_t): error occured with std::istream::read");
        }
        if (tmp > std::numeric_limits<size_t>::max()) {
            cmnThrow("cmnDataDeSerializeBinary(size_t): received a size_t larger than what can be handled on 32 bits");
        }
        data = static_cast<size_t>(tmp); // this should be safe now
        return;
    }
}