Remove C support.

I'm not willing to invest the time to make it good.
This commit is contained in:
tastytea 2019-09-13 16:45:40 +02:00
parent 34eedfe6d1
commit 4afee87285
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
8 changed files with 2 additions and 407 deletions

View File

@ -28,12 +28,9 @@ add_subdirectory(src)
configure_file("${PROJECT_SOURCE_DIR}/${PROJECT_NAME}.pc.in"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc" @ONLY)
configure_file("${PROJECT_SOURCE_DIR}/${PROJECT_NAME}_c.pc.in"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}_c.pc" @ONLY)
install(FILES
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}_c.pc"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
if(WITH_TESTS)

View File

@ -1,4 +1,4 @@
**identiconpp** is a library to generate identicons for C++ and C.
**identiconpp** is a library to generate identicons for C++.
You get the images as `Magick::Image`. This allows you to make all kinds of
modifications.
@ -57,15 +57,6 @@ int main()
}
```
### C interface
This is somewhat experimental. Have a look at [example.c]
(https://schlomp.space/tastytea/identiconpp/src/branch/master/example.c) and
[identiconpp_c.h](https://doc.schlomp.space/identiconpp/identiconpp__c_8h.html).
It seems to be impossible to use `Magick++` and `MagickWand` in the same
library, so the images are returned as base64-encoded strings.
## Install
### Gentoo

104
example.c
View File

@ -1,104 +0,0 @@
/* This file is part of identiconpp.
* Public Domain / CC-0
*
* Compile with gcc --std=c99 \
* $(pkg-config --cflags --libs identiconpp_c.pc MagickWand openssl)
*/
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include "identiconpp_c.h"
#include <MagickWand/MagickWand.h>
#include <openssl/evp.h>
#include <openssl/bio.h>
char *b64decode(char *input, int length)
{
BIO *b64, *bmem;
char *buffer = (char *)malloc(length);
memset(buffer, 0, length);
b64 = BIO_new(BIO_f_base64());
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
bmem = BIO_new_mem_buf(input, length);
bmem = BIO_push(b64, bmem);
BIO_read(bmem, buffer, length);
BIO_free_all(bmem);
return buffer;
}
int main(int argc, char *argv[])
{
char digest[65] = "973dfe463ec85785f5f95af5ba3906ee"
"db2d931c24e69824a89ea65dba4e813b";
const char colors[6][9] =
{
"800000ff",
"008000ff",
"000080ff",
"808000ff",
"008080ff",
"800080ff"
};
if (argc > 1)
{
strncpy(digest, argv[1], 64);
}
MagickWand *mw = NULL;
MagickWandGenesis();
mw = NewMagickWand();
{
const uint8_t padding[2] = { 20, 20 };
identiconpp_setup(4, 4, identiconpp_ltr_symmetric,
"ffffffff", colors, 6, padding);
uint64_t len = identiconpp_generate("png", digest, 200);
char base64[len];
strcpy(base64, identiconpp_base64());
MagickReadImageBlob(mw, b64decode(base64, len), len);
MagickWriteImage(mw, "identicon1.png");
}
{
const uint8_t padding[2] = { 0, 0 };
identiconpp_setup(5, 5, identiconpp_sigil,
"00000080", colors, 6, padding);
uint64_t len = identiconpp_generate("png", digest, 200);
char base64[len];
strcpy(base64, identiconpp_base64());
MagickReadImageBlob(mw, b64decode(base64, len), len);
MagickWriteImage(mw, "identicon2.png");
}
{
const uint8_t padding[2] = { 0, 0 };
identiconpp_setup(5, 5, identiconpp_ltr_asymmetric,
"000000ff", colors, 6, padding);
uint64_t len = identiconpp_generate("png", digest, 200);
char base64[len];
strcpy(base64, identiconpp_base64());
MagickReadImageBlob(mw, b64decode(base64, len), len);
MagickWriteImage(mw, "identicon3.png");
}
{
const uint8_t padding[2] = { 10, 10 };
identiconpp_setup(6, 4, identiconpp_ltr_symmetric,
"000000c0", colors, 6, padding);
uint64_t len = identiconpp_generate("png", digest, 200);
char base64[len];
strcpy(base64, identiconpp_base64());
MagickReadImageBlob(mw, b64decode(base64, len), len);
MagickWriteImage(mw, "identicon4.png");
}
DestroyMagickWand(mw);
MagickWandTerminus();
}

View File

@ -1,11 +0,0 @@
name=@PROJECT_NAME@
prefix=@CMAKE_INSTALL_PREFIX@
exec_prefix=${prefix}
libdir=${prefix}/@CMAKE_INSTALL_LIBDIR@
includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@
Name: ${name}
Description: Library to generate identicons for C++ and C.
Version: @PROJECT_VERSION@
Libs: -L${libdir} -l${name}
Cflags: -I${includedir}

View File

@ -14,5 +14,5 @@ set_target_properties(${PROJECT_NAME} PROPERTIES
SOVERSION ${${PROJECT_NAME}_VERSION_MAJOR})
install(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(FILES ${CMAKE_PROJECT_NAME}.hpp ${CMAKE_PROJECT_NAME}_c.h
install(FILES ${CMAKE_PROJECT_NAME}.hpp
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})

View File

@ -1,103 +0,0 @@
/* This file is part of identiconpp.
* Copyright © 2019 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <vector>
#include <memory>
#include <exception>
#include <iostream>
#include <Magick++/Image.h>
#include <Magick++/Blob.h>
#include "identiconpp.hpp"
#include "debug.hpp"
#include "identiconpp_c.h"
using std::cerr;
using std::endl;
static std::unique_ptr<Identiconpp> identicon;
static string base64;
bool identiconpp_setup(const uint8_t columns, const uint8_t rows,
identiconpp_algorithm type,
const char background[9],
const char foreground[][9],
const uint8_t foreground_len,
const uint8_t padding[2])
{
Identiconpp::algorithm algo = Identiconpp::algorithm::ltr_symmetric;
switch (type)
{
case identiconpp_ltr_symmetric:
{
algo = Identiconpp::algorithm::ltr_symmetric;
break;
}
case identiconpp_ltr_asymmetric:
{
algo = Identiconpp::algorithm::ltr_asymmetric;
break;
}
case identiconpp_sigil:
{
algo = Identiconpp::algorithm::sigil;
break;
}
}
std::vector<string> vforeground;
for (uint8_t i = 0; i < foreground_len; ++i)
{
vforeground.push_back(foreground[i]);
}
try
{
identicon = std::make_unique<Identiconpp>(
Identiconpp(columns, rows, algo, background, vforeground,
{ padding[0], padding[1] }));
}
catch (const std::exception &e)
{
cerr << e.what() << endl;
return false;
}
return true;
}
uint64_t identiconpp_generate(const char magick[],
const char digest[], const uint16_t width)
{
try
{
Magick::Image img = identicon->generate(digest, width);
Magick::Blob blob;
img.magick(magick);
img.write(&blob);
base64 = blob.base64();
return blob.base64().length();
}
catch (const std::exception &e)
{
cerr << e.what() << endl;
return 0;
}
}
const char *identiconpp_base64()
{
return base64.c_str();
}

View File

@ -1,99 +0,0 @@
/* This file is part of identiconpp.
* Copyright © 2019 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef IDENTICONPP_C_H
#define IDENTICONPP_C_H
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
/*!
* @brief C interface for identiconpp.
* @file identiconpp_c.h
* @example example.c
*/
/*!
* @brief List of identicon algorithms
*/
typedef enum
{
//! Generates symmetric (vertically mirrored) identicons.
identiconpp_ltr_symmetric,
//! Generates asymmetric identicons.
identiconpp_ltr_asymmetric,
/*!
* Generates the same results as
* [sigil](https://github.com/cupcake/sigil/) and
* [pydenticon](https://github.com/azaghal/pydenticon/).
*/
identiconpp_sigil
} identiconpp_algorithm;
/*!
* @brief Setup identicon parameters.
*
* @param columns Number of columns
* @param rows Number of rows
* @param type The algorithm to use
* @param background Background color, hexadecimal, rrggbbaa
* @param foreground Array of foreground colors
* @param foreground_len Length of the array of foreground colors
* @param padding Padding in pixels { left & right, top & down }
*
* @return false on error, true otherwise.
*
* @since before 0.5.0
*/
bool identiconpp_setup(const uint8_t columns, const uint8_t rows,
identiconpp_algorithm type,
const char background[9],
const char foreground[][9],
const uint8_t foreground_len,
const uint8_t padding[2]);
/*!
* @brief Generates identicon from digest.
*
* @param magick See http://imagemagick.org/script/formats.php
* @param digest The pre-computed digest
* @param width The width of the identicon
*
* @return Length of the generated base64-string, or 0 on error.
*
* @since before 0.5.0
*/
uint64_t identiconpp_generate(const char magick[],
const char digest[], const uint16_t width);
/*!
* @brief Return base64-encoded string of the image generated with
* identiconpp_generate().
*
* @since before 0.5.0
*/
const char *identiconpp_base64();
#ifdef __cplusplus
}
#endif
#endif // IDENTICONPP_C_H

View File

@ -1,76 +0,0 @@
#include <catch.hpp>
#include <string>
#include <exception>
#include <vector>
#include <cstdint>
#include <Magick++/Image.h>
#include "identiconpp_c.h"
using std::string;
using std::uint8_t;
using std::uint64_t;
SCENARIO("C interface", "[C]")
{
GIVEN("The digest: sha256(test@example.com)")
{
char digest[65] = "973dfe463ec85785f5f95af5ba3906ee"
"db2d931c24e69824a89ea65dba4e813b";
const uint8_t padding[2] = { 20, 40 };
bool success = false;
WHEN("256 bits of entropy is required")
{
const char colors[15][9] =
{
"000000ff", "000000ff", "000000ff", "000000ff", "000000ff",
"000000ff", "000000ff", "000000ff", "000000ff", "000000ff",
"000000ff", "000000ff", "000000ff", "000000ff", "000000ff"
};
success = identiconpp_setup(18, 28,
identiconpp_ltr_symmetric,
"ffffffff",
colors, 15, padding);
uint64_t len = identiconpp_generate("png", digest, 50);
if (len == 0)
{
success = false;
}
if (string(identiconpp_base64()).length() != len)
{
success = false;
}
THEN("Does not crash")
{
REQUIRE(success == true);
}
}
WHEN("257 bits of entropy is required")
{
const char colors[16][9] =
{
"000000ff", "000000ff", "000000ff", "000000ff", "000000ff",
"000000ff", "000000ff", "000000ff", "000000ff", "000000ff",
"000000ff", "000000ff", "000000ff", "000000ff", "000000ff",
"000000ff"
};
success = identiconpp_setup(18, 28,
identiconpp_ltr_symmetric,
"ffffffff",
colors, 16, padding);
if (identiconpp_generate("png", digest, 50) == 0)
{
success = false;
}
THEN("Crashes")
{
REQUIRE(success == false);
}
}
}
}