LINUX.ORG.RU

История изменений

Исправление mersinvald, (текущая версия) :

Класс Poly:

struct Poly {
    Poly()
        : length(0), _memory(nullptr) {}

    Poly(uint8_t id, uint16_t offset, uint8_t size) \
        : length(0), _id(id), _size(size), _offset(offset), _memory(nullptr) {}

    /* @brief Append number at the end of polynomial
     * @param num - number to append
     * @return false if polynomial can't be stretched */
    inline bool Append(uint8_t num) {
        #ifdef DEBUG
        assert(length+1 < _size);
        #endif
        ptr()[length++] = num;
        return true;
    }

    /* @brief Polynomial initialization */
    inline void Init(uint8_t id, uint16_t offset, uint8_t size, uint8_t** memory_ptr) {
        this->_id     = id;
        this->_offset = offset;
        this->_size   = size;
        this->length  = 0;
        this->_memory = memory_ptr;
    }

    /* @brief Polynomial memory zeroing */
    inline void Reset() {
        memset((void*)ptr(), 0, this->_size);
    }

    /* @brief Copy polynomial to memory
     * @param src    - source byte-sequence
     * @param size   - size of polynomial
     * @param offset - write offset */
    inline void Set(const uint8_t* src, uint8_t len, uint8_t offset = 0) {
        #ifdef DEBUG
        assert(src && len <= this->_size-offset);
        #endif
        memcpy(ptr()+offset, src, len * sizeof(uint8_t));
        length = len + offset;
    }

    #define max(a, b) ((a > b) ? (a) : (b))

    inline bool operator== (Poly &b) const {
        return memcmp(ptr(), b.ptr(), max(this->length, b.length)) == 0;
    }

    inline bool operator!= (Poly &b) const {
        return memcmp(ptr(), b.ptr(), max(this->length, b.length));
    }

    inline void operator=  (const Poly &b) {
        length = max(length, b.length);
        Set(b.ptr(), length);
    }

    inline uint8_t& operator[] (uint8_t i) const {
        #ifdef DEBUG
        assert(i < _size);
        #endif
        return ptr()[i];
    }

    inline uint8_t& at(uint8_t i) const {
        #ifdef DEBUG
        assert(i < _size);
        #endif
        return ptr()[i];
    }

    inline uint8_t id() const {
        return _id;
    }

    inline uint8_t size() const {
        return _size;
    }

    // Returns pointer to memory of this polynomial
    inline uint8_t* ptr() const {
        #ifdef DEBUG
        assert(_memory && *_memory);
        #endif
        return (*_memory) + _offset;
    }

    uint8_t length;

protected:

    uint8_t   _id;
    uint8_t   _size;    // Size of reserved memory for this polynomial
    uint16_t  _offset;  // Offset in memory
    uint8_t** _memory;  // Pointer to pointer to memory
};

Инициализируется массив этих объектов следующим образом:

const uint8_t   enc_len  = msg_length + ecc_length;
const uint8_t   poly_len = ecc_length * 2;
uint8_t** memptr   = &memory;
uint16_t  offset   = 0;

polynoms[0].Init(ID_MSG_IN, offset, enc_len, memptr);
offset += enc_len;

polynoms[1].Init(ID_MSG_OUT, offset, enc_len, memptr);
offset += enc_len;

for(uint8_t i = ID_GENERATOR; i < ID_MSG_E; i++) {
    polynoms[i].Init(i, offset, poly_len, memptr);
    offset += poly_len;
}

polynoms[5].Init(ID_MSG_E, offset, enc_len, memptr);
offset += enc_len;
for(uint8_t i = ID_TPOLY3; i < ID_ERR_EVAL+2; i++) {
    polynoms[i].Init(i, offset, poly_len, memptr);
    offset += poly_len;
}

P.S Буду признателен за критику любого кода, приведенного в треде

Исправление mersinvald, :

Класс Poly:

    
struct Poly {
    Poly()
        : length(0), _memory(nullptr) {}

    Poly(uint8_t id, uint16_t offset, uint8_t size) \
        : length(0), _id(id), _size(size), _offset(offset), _memory(nullptr) {}

    /* @brief Append number at the end of polynomial
     * @param num - number to append
     * @return false if polynomial can't be stretched */
    inline bool Append(uint8_t num) {
        #ifdef DEBUG
        assert(length+1 < _size);
        #endif
        ptr()[length++] = num;
        return true;
    }

    /* @brief Polynomial initialization */
    inline void Init(uint8_t id, uint16_t offset, uint8_t size, uint8_t** memory_ptr) {
        this->_id     = id;
        this->_offset = offset;
        this->_size   = size;
        this->length  = 0;
        this->_memory = memory_ptr;
    }

    /* @brief Polynomial memory zeroing */
    inline void Reset() {
        memset((void*)ptr(), 0, this->_size);
    }

    /* @brief Copy polynomial to memory
     * @param src    - source byte-sequence
     * @param size   - size of polynomial
     * @param offset - write offset */
    inline void Set(const uint8_t* src, uint8_t len, uint8_t offset = 0) {
        #ifdef DEBUG
        assert(src && len <= this->_size-offset);
        #endif
        memcpy(ptr()+offset, src, len * sizeof(uint8_t));
        length = len + offset;
    }

    #define max(a, b) ((a > b) ? (a) : (b))

    inline bool operator== (Poly &b) const {
        return memcmp(ptr(), b.ptr(), max(this->length, b.length)) == 0;
    }

    inline bool operator!= (Poly &b) const {
        return memcmp(ptr(), b.ptr(), max(this->length, b.length));
    }

    inline void operator=  (const Poly &b) {
        length = max(length, b.length);
        Set(b.ptr(), length);
    }

    inline uint8_t& operator[] (uint8_t i) const {
        #ifdef DEBUG
        assert(i < _size);
        #endif
        return ptr()[i];
    }

    inline uint8_t& at(uint8_t i) const {
        #ifdef DEBUG
        assert(i < _size);
        #endif
        return ptr()[i];
    }

    inline uint8_t id() const {
        return _id;
    }

    inline uint8_t size() const {
        return _size;
    }

    // Returns pointer to memory of this polynomial
    inline uint8_t* ptr() const {
        #ifdef DEBUG
        assert(_memory && *_memory);
        #endif
        return (*_memory) + _offset;
    }

    uint8_t length;

protected:

    uint8_t   _id;
    uint8_t   _size;    // Size of reserved memory for this polynomial
    uint16_t  _offset;  // Offset in memory
    uint8_t** _memory;  // Pointer to pointer to memory
};

Инициализируется массив этих объектов следующим образом:

const uint8_t   enc_len  = msg_length + ecc_length;
const uint8_t   poly_len = ecc_length * 2;
uint8_t** memptr   = &memory;
uint16_t  offset   = 0;

polynoms[0].Init(ID_MSG_IN, offset, enc_len, memptr);
offset += enc_len;

polynoms[1].Init(ID_MSG_OUT, offset, enc_len, memptr);
offset += enc_len;

for(uint8_t i = ID_GENERATOR; i < ID_MSG_E; i++) {
    polynoms[i].Init(i, offset, poly_len, memptr);
    offset += poly_len;
}

polynoms[5].Init(ID_MSG_E, offset, enc_len, memptr);
offset += enc_len;
for(uint8_t i = ID_TPOLY3; i < ID_ERR_EVAL+2; i++) {
    polynoms[i].Init(i, offset, poly_len, memptr);
    offset += poly_len;
}

P.S Буду признателен за критику любого кода, приведенного в треде

Исходная версия mersinvald, :

Класс Poly:
struct Poly { Poly() : length(0), _memory(nullptr) {}

Poly(uint8_t id, uint16_t offset, uint8_t size) \ : length(0), _id(id), _size(size), _offset(offset), _memory(nullptr) {}

/* @brief Append number at the end of polynomial * @param num - number to append * @return false if polynomial can't be stretched */ inline bool Append(uint8_t num) { #ifdef DEBUG assert(length+1 < _size); #endif ptr()[length++] = num; return true; }

/* @brief Polynomial initialization */ inline void Init(uint8_t id, uint16_t offset, uint8_t size, uint8_t** memory_ptr) { this->_id = id; this->_offset = offset; this->_size = size; this->length = 0; this->_memory = memory_ptr; }

/* @brief Polynomial memory zeroing */ inline void Reset() { memset((void*)ptr(), 0, this->_size); }

/* @brief Copy polynomial to memory * @param src - source byte-sequence * @param size - size of polynomial * @param offset - write offset */ inline void Set(const uint8_t* src, uint8_t len, uint8_t offset = 0) { #ifdef DEBUG assert(src && len <= this->_size-offset); #endif memcpy(ptr()+offset, src, len * sizeof(uint8_t)); length = len + offset; }

#define max(a, b) ((a > b) ? (a) : (b))

inline bool operator== (Poly &b) const { return memcmp(ptr(), b.ptr(), max(this->length, b.length)) == 0; }

inline bool operator!= (Poly &b) const { return memcmp(ptr(), b.ptr(), max(this->length, b.length)); }

inline void operator= (const Poly &b) { length = max(length, b.length); Set(b.ptr(), length); }

inline uint8_t& operator[] (uint8_t i) const { #ifdef DEBUG assert(i < _size); #endif return ptr(); }

inline uint8_t& at(uint8_t i) const { #ifdef DEBUG assert(i < _size); #endif return ptr(); }

inline uint8_t id() const { return _id; }

inline uint8_t size() const { return _size; }

// Returns pointer to memory of this polynomial inline uint8_t* ptr() const { #ifdef DEBUG assert(_memory && *_memory); #endif return (*_memory) + _offset; }

uint8_t length;

protected:

uint8_t _id; uint8_t _size; // Size of reserved memory for this polynomial uint16_t _offset; // Offset in memory uint8_t** _memory; // Pointer to pointer to memory };

Инициализируется массив этих объектов следующим образом:

const uint8_t   enc_len  = msg_length + ecc_length;
const uint8_t   poly_len = ecc_length * 2;
uint8_t** memptr   = &memory;
uint16_t  offset   = 0;

polynoms[0].Init(ID_MSG_IN, offset, enc_len, memptr);
offset += enc_len;

polynoms[1].Init(ID_MSG_OUT, offset, enc_len, memptr);
offset += enc_len;

for(uint8_t i = ID_GENERATOR; i < ID_MSG_E; i++) {
    polynoms[i].Init(i, offset, poly_len, memptr);
    offset += poly_len;
}

polynoms[5].Init(ID_MSG_E, offset, enc_len, memptr);
offset += enc_len;
for(uint8_t i = ID_TPOLY3; i < ID_ERR_EVAL+2; i++) {
    polynoms[i].Init(i, offset, poly_len, memptr);
    offset += poly_len;
}

P.S Буду признателен за критику любого кода, приведенного в треде