/* Copyright (C) 2002 The gtkmm Development Team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

_DEFS(glibmm,glib)

#include <glibmm/value.h>
#include <glib.h>
#include <string>

#ifndef DOXYGEN_SHOULD_SKIP_THIS
extern "C" { typedef struct _GChecksum GChecksum; }
#endif

//TODO: When we can change API, make Checksum a _CLASS_BOXEDTYPE.

namespace Glib
{

/** Computes the checksum for data.
 * This is a generic API for computing checksums (or "digests") for a sequence of arbitrary bytes,
 * using various hashing algorithms like MD5, SHA-1 and SHA-256. Checksums are commonly used in various environments and specifications.
 *
 * glibmm supports incremental checksums by calling update() as long as there's data available and then using get_string()
 * or get_digest() to compute the checksum and return it either as a string in hexadecimal form, or as a raw sequence of bytes.
 * To compute the checksum for binary blobs and NULL-terminated strings in one go, use the static compute_checksum() convenience functions().
 *
 * @newin{2,16}
 */
class GLIBMM_API Checksum
{
  _CLASS_OPAQUE_COPYABLE(Checksum, GChecksum, NONE, g_checksum_copy, g_checksum_free, GLIBMM_API)
  _IGNORE(g_checksum_copy, g_checksum_free)

public:
  _WRAP_ENUM(ChecksumType, GChecksumType, NO_GTYPE, decl_prefix GLIBMM_API)

#m4 _CONVERSION(`ChecksumType', `GChecksumType', `(($2)$3)')

  /** Creates a new Checksum, using the checksum algorithm @a checksum_type.
   * If the checksum_type is not known, then operator bool() will return false.
   *
   * @param checksum_type Checksum type, one of defined above.
   */
  explicit Checksum(ChecksumType checksum_type);

  /** Returns true if the Checksum object is valid.
   * This will return false, for instance, if an unsupported checksum type was provided to the constructor.
   */
  explicit operator bool() const;

  _WRAP_METHOD(void reset(), g_checksum_reset)

  //TODO: length should really be gssize, not gsize, when we can break ABI:
#m4 _CONVERSION(`gsize',`gssize',`(gssize)($3)')
  _WRAP_METHOD(void update(const guchar* data, gsize length), g_checksum_update)

  /** Feeds data into an existing Checksum.
   * The checksum must still be open, that is get_string() or get_digest() must not have been called on the checksum.
   *
   * @param data Buffer used to compute the checksum
   */
  void update(const std::string& data);

  _WRAP_METHOD(void get_digest(guint8 *buffer, gsize *digest_len) const, g_checksum_get_digest)

  _WRAP_METHOD(std::string get_string() const, g_checksum_get_string)


  _WRAP_METHOD(static std::string compute_checksum(ChecksumType checksum_type, const guchar* data, gsize length), g_compute_checksum_for_data)

  /** Computes the checksum of a string.
   *
   * @param checksum_type A ChecksumType
   * @param str The string to compute the checksum of.
   * @result The checksum as a hexadecimal string.
   */
  static std::string compute_checksum(ChecksumType checksum_type, const std::string& str);
  _IGNORE(g_compute_checksum_for_string)


  //We don't use _WRAP_METHOD because this is not really a GCheckSum function:
  /** Gets the length in bytes of digests of type @a checksum_type.
   *
   * @param checksum_type A ChecksumType.
   * @result The checksum length, or -1 if @a checksum_type is not supported.
   */
  static gssize get_length(ChecksumType checksum_type);
};

#ifndef DOXYGEN_SHOULD_SKIP_THIS
// This is needed so Glib::Checksum can be used with Glib::Value and _WRAP_PROPERTY.
template <>
class GLIBMM_API Value<Glib::Checksum> : public ValueBase_Boxed
{
public:
  using CppType = Glib::Checksum;
  using CType = GChecksum*;

  static GType value_type();

  void set(const CppType& data);
  CppType get() const;
};
#endif /* DOXYGEN_SHOULD_SKIP_THIS */

} //namespace Glib

