mirror of
https://gitlab.xiph.org/xiph/icecast-server.git
synced 2025-02-02 15:07:36 -05:00
Feature: Added abstract refobject for digest calculation
This commit is contained in:
parent
2ef043decf
commit
bbddb556ef
@ -28,6 +28,7 @@ noinst_HEADERS = \
|
||||
xslt.h \
|
||||
yp.h \
|
||||
md5.h \
|
||||
digest.h \
|
||||
matchfile.h \
|
||||
tls.h \
|
||||
refobject.h \
|
||||
@ -77,6 +78,7 @@ icecast_SOURCES = \
|
||||
admin.c \
|
||||
resourcematch.c \
|
||||
md5.c \
|
||||
digest.c \
|
||||
matchfile.c \
|
||||
tls.c \
|
||||
refobject.c \
|
||||
|
118
src/digest.c
Normal file
118
src/digest.c
Normal file
@ -0,0 +1,118 @@
|
||||
/* Icecast
|
||||
*
|
||||
* This program is distributed under the GNU General Public License, version 2.
|
||||
* A copy of this license is included with this source.
|
||||
*
|
||||
* Copyright 2020, Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "digest.h"
|
||||
#include "md5.h"
|
||||
|
||||
#include "logging.h"
|
||||
#define CATMODULE "digest"
|
||||
|
||||
struct digest_tag {
|
||||
/* base object */
|
||||
refobject_base_t __base;
|
||||
|
||||
/* metadata */
|
||||
digest_algo_t algo;
|
||||
|
||||
/* state */
|
||||
int done;
|
||||
union {
|
||||
struct MD5Context md5;
|
||||
} state;
|
||||
};
|
||||
|
||||
REFOBJECT_DEFINE_TYPE(digest_t);
|
||||
|
||||
digest_t * digest_new(digest_algo_t algo)
|
||||
{
|
||||
digest_t *digest = refobject_new__new(digest_t, NULL, NULL, NULL);
|
||||
|
||||
if (!digest)
|
||||
return NULL;
|
||||
|
||||
digest->algo = algo;
|
||||
switch (algo) {
|
||||
case DIGEST_ALGO_MD5:
|
||||
MD5Init(&(digest->state.md5));
|
||||
break;
|
||||
default:
|
||||
refobject_unref(digest);
|
||||
return NULL;
|
||||
break;
|
||||
}
|
||||
|
||||
return digest;
|
||||
}
|
||||
|
||||
ssize_t digest_write(digest_t *digest, const void *data, size_t len)
|
||||
{
|
||||
if (!digest || !data)
|
||||
return -1;
|
||||
|
||||
if (digest->done)
|
||||
return -1;
|
||||
|
||||
switch (digest->algo) {
|
||||
case DIGEST_ALGO_MD5:
|
||||
MD5Update(&(digest->state.md5), (const unsigned char *)data, len);
|
||||
return len;
|
||||
break;
|
||||
default:
|
||||
return -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ssize_t digest_read(digest_t *digest, void *buf, size_t len)
|
||||
{
|
||||
if (!digest || !buf)
|
||||
return -1;
|
||||
|
||||
if (digest->done)
|
||||
return -1;
|
||||
|
||||
digest->done = 1;
|
||||
|
||||
switch (digest->algo) {
|
||||
case DIGEST_ALGO_MD5:
|
||||
if (len < HASH_LEN) {
|
||||
unsigned char buffer[HASH_LEN];
|
||||
MD5Final(buffer, &(digest->state.md5));
|
||||
memcpy(buf, buffer, len);
|
||||
return len;
|
||||
} else {
|
||||
MD5Final((unsigned char*)buf, &(digest->state.md5));
|
||||
return HASH_LEN;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ssize_t digest_length_bytes(digest_t *digest)
|
||||
{
|
||||
if (!digest)
|
||||
return -1;
|
||||
|
||||
switch (digest->algo) {
|
||||
case DIGEST_ALGO_MD5:
|
||||
return 16;
|
||||
break;
|
||||
default:
|
||||
return -1;
|
||||
break;
|
||||
}
|
||||
}
|
27
src/digest.h
Normal file
27
src/digest.h
Normal file
@ -0,0 +1,27 @@
|
||||
/* Icecast
|
||||
*
|
||||
* This program is distributed under the GNU General Public License, version 2.
|
||||
* A copy of this license is included with this source.
|
||||
*
|
||||
* Copyright 2020, Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>
|
||||
*/
|
||||
|
||||
#ifndef __DIGEST_H__
|
||||
#define __DIGEST_H__
|
||||
|
||||
#include "refobject.h"
|
||||
|
||||
REFOBJECT_FORWARD_TYPE(digest_t);
|
||||
|
||||
typedef enum {
|
||||
DIGEST_ALGO_MD5
|
||||
} digest_algo_t;
|
||||
|
||||
digest_t * digest_new(digest_algo_t algo);
|
||||
ssize_t digest_write(digest_t *digest, const void *data, size_t len);
|
||||
ssize_t digest_read(digest_t *digest, void *buf, size_t len);
|
||||
|
||||
/* Returns the digest size in bytes */
|
||||
ssize_t digest_length_bytes(digest_t *digest);
|
||||
|
||||
#endif
|
@ -122,6 +122,10 @@ typedef struct reportxml_database_tag reportxml_database_t;
|
||||
typedef struct listensocket_container_tag listensocket_container_t;
|
||||
typedef struct listensocket_tag listensocket_t;
|
||||
|
||||
/* ---[ digest.[ch] ]--- */
|
||||
|
||||
typedef struct digest_tag digest_t;
|
||||
|
||||
/* ---[ refobject.[ch] ]--- */
|
||||
|
||||
typedef struct refobject_base_tag refobject_base_t;
|
||||
@ -137,6 +141,7 @@ typedef union __attribute__ ((__transparent_union__)) {
|
||||
reportxml_database_t *reportxml_database;
|
||||
listensocket_container_t *listensocket_container;
|
||||
listensocket_t *listensocket;
|
||||
digest_t *digest;
|
||||
} refobject_t;
|
||||
#else
|
||||
typedef void * refobject_t;
|
||||
|
Loading…
x
Reference in New Issue
Block a user