view AES/Adler16.h @ 3:4ace58a7c03c

Send disconnect command before closing the connection The old BT module transmitted a notification in case a connection were closed which cause the ostc to exit the uart loop. The new one doesn't do this => send disconnect command to avoid waiting in the installation loop till timeout or button press.
author Ideenmodellierer
date Fri, 28 Nov 2025 20:00:02 +0100
parents 0b3630a29ad8
children
line wrap: on
line source

//////////////////////////////////////////////////////////////////////////////
/// \file   Adler16.h
/// \brief Adler checksum, on 2x8 bits.
/// \author JD Gascuel.
/// \copyright (c) 2011-2016 JD Gascuel. All rights reserved.
/// $Id$
//////////////////////////////////////////////////////////////////////////////
//
// BSD 2-Clause License:
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
//    this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
//    this list of conditions and the following disclaimer in the documentation
//    and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//
//////////////////////////////////////////////////////////////////////////////
// HISTORY
//  2015-07-31 Creation.
//  2016-05-24  jDG: BSD-2 version.

#ifndef ADLER16_H
#define ADLER16_H

#include <QByteArray>

//////////////////////////////////////////////////////////////////////////////
/// \brief Addler checksum, on 2xN bits.
template <typename T>
class AdlerTemplate
{
    T _a, _b;

public:
    //------------------------------------------------------------------------
    /// \brief Initialize checksum with a given seed.
    inline AdlerTemplate(T a, T b)
      : _a(a), _b(b)
    {}

    //------------------------------------------------------------------------
    /// \brief Re-initialize checksum with a given seed.
    inline void reset(T a, T b)
    {   _a = a;
        _b = b;
    }

    //------------------------------------------------------------------------
    /// \brief Hash a single byte/word
    inline void add(int c)
    {
        _a += (T)(c & 0xFF);
        _b += _a;
    }

    //------------------------------------------------------------------------
    /// \brief Hash a block of bytes.
    /// \note \a size is always in BYTE units.
    inline void add(const void* buffer, size_t size)
    {
        for(size_t i = 0; i<size; ++i)
            add( ((const unsigned char*)buffer)[i] );
    }

    //------------------------------------------------------------------------
    /// \brief Hash all the byte/words for a Qt buffer.
    inline void add(const QByteArray& buffer)
    {
        add(buffer.data(), buffer.size());
    }

    //------------------------------------------------------------------------
    /// \brief Returns hash result.
    inline T a() const { return _a; }
    inline T b() const { return _b; }

    //------------------------------------------------------------------------
    /// Check Hash result.
    /// \returns TRUE if has do match the given values.
    inline bool check(T a, T b)
    {
        return (_a == a) && (_b == b);
    }
};

/// \brief 2x8 bits implementation.
typedef AdlerTemplate<unsigned char>  Adler16;

/// \brief 2x16 bits implementation.
typedef AdlerTemplate<unsigned short> Adler32;

#endif // ADDLER16_H