Commit b5bbf5a4 authored by anonymous's avatar anonymous
Browse files

Merge util changes from other libs

parent 1fdc4c74
......@@ -43,6 +43,7 @@ libbdplus_la_SOURCES=\
util/attributes.h \
util/macro.h \
util/mutex.h \
util/mutex.c \
util/strutl.c \
util/strutl.h \
util/logging.c \
......
......@@ -24,8 +24,10 @@
#ifdef _WIN32
# define DIR_SEP "\\"
# define DIR_SEP_CHAR '\\'
#else
# define DIR_SEP "/"
# define DIR_SEP_CHAR '/'
#endif
/*
......
......@@ -25,6 +25,7 @@
#include "util/macro.h"
#include "util/strutl.h"
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
......@@ -179,20 +180,20 @@ uint32_t diff_hashdb_load(uint8_t *hashname, uint8_t *fname, uint64_t offset,
char str[512];
DEBUG(DBG_BDPLUS,"[diff] namehash: %s\n",
print_hex(str, namehash, shalen));
str_print_hex(str, namehash, shalen));
// Hash it.
gcry_md_hash_buffer(GCRY_MD_SHA1, digest, namehash, shalen - 1);
memset(str, 0, sizeof(str));
DEBUG(DBG_BDPLUS,"[diff] find hashdb: %s\n",
print_hex(str, digest, sizeof(digest)));
str_print_hex(str, digest, sizeof(digest)));
while(fread(&sha_hdr, sizeof(sha_hdr), 1, fd) == 1) {
memset(str, 0, sizeof(str));
DEBUG(DBG_BDPLUS,"[diff] read hashdb: %s\n",
print_hex(str, sha_hdr.digest, sizeof(digest)));
str_print_hex(str, sha_hdr.digest, sizeof(digest)));
sha_hdr.next = FETCH4((uint8_t *)&sha_hdr.next);
......
......@@ -29,6 +29,7 @@
#include "util/logging.h"
#include "util/macro.h"
#include "util/strutl.h"
#include <stdlib.h>
#include <inttypes.h>
......@@ -594,9 +595,9 @@ int32_t segment_decrypt(conv_table_t *conv_tab, uint8_t *key, uint8_t *mask)
char str[128];
DEBUG(DBG_BDPLUS | DBG_CRIT,"[segment] Key %2u, %3u: %s\n", conv_tab->current_table,
conv_tab->current_segment,
print_hex(str, key, 16));
DEBUG(DBG_BDPLUS," mask: %s\n", print_hex(str, mask, 8));
DEBUG(DBG_BDPLUS,"Q: %s\n", print_hex(str, mask, 39));
str_print_hex(str, key, 16));
DEBUG(DBG_BDPLUS," mask: %s\n", str_print_hex(str, mask, 8));
DEBUG(DBG_BDPLUS,"Q: %s\n", str_print_hex(str, mask, 39));
if ( conv_tab->current_table >= conv_tab->numTables) {
DEBUG(DBG_BDPLUS | DBG_CRIT, "[segment] decrypt, current_table (%d) >= numTables! help?!\n",
......
......@@ -38,6 +38,7 @@
#include <inttypes.h>
#include <string.h>
#include <stdio.h> /* SEEK_* */
#if HAVE_TIME_H
#include <time.h>
#endif
......
......@@ -71,7 +71,7 @@ char *bdplus_disc_cache_file(bdplus_t *plus, const char *file)
char *base = file_get_cache_dir();
char vid_str[33];
char *result;
print_hex(vid_str, plus->volumeID, 16);
str_print_hex(vid_str, plus->volumeID, 16);
result = str_printf("%s/%s/%s", base ? base : "/tmp/", vid_str, file);
X_FREE(base);
file_mkpath(result);
......
......@@ -21,7 +21,11 @@
#define LIBBLURAY_ATTRIBUTES_H_
#if defined(__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3 ))
# define BD_ATTR_FORMAT_PRINTF(format,var) __attribute__((__format__(__printf__,format,var)))
# if defined(_WIN32)
# define BD_ATTR_FORMAT_PRINTF(format,var) __attribute__((__format__(__gnu_printf__,format,var)))
# else
# define BD_ATTR_FORMAT_PRINTF(format,var) __attribute__((__format__(__printf__,format,var)))
# endif
# define BD_ATTR_MALLOC __attribute__((__malloc__))
# define BD_ATTR_PACKED __attribute__((packed))
#else
......
......@@ -21,6 +21,8 @@
#include "logging.h"
#include "file/file.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
......@@ -28,16 +30,6 @@
uint32_t debug_mask = (uint32_t)-1; /* set all bits to make sure bd_debug() is called for initialization */
char *print_hex(char *out, const uint8_t *buf, int count)
{
int zz;
for(zz = 0; zz < count; zz++) {
sprintf(out + (zz * 2), "%02X", buf[zz]);
}
return out;
}
void bd_debug(const char *file, int line, uint32_t mask, const char *format, ...)
{
static int debug_init = 0;
......@@ -69,10 +61,11 @@ void bd_debug(const char *file, int line, uint32_t mask, const char *format, ...
}
if (mask & debug_mask) {
const char *f = strrchr(file, DIR_SEP_CHAR);
char buffer[4096], *pt = buffer;
va_list args;
pt += sprintf(buffer, "%s:%d: ", file, line);
pt += sprintf(buffer, "%s:%d: ", f ? f + 1 : file, line);
va_start(args, format);
vsnprintf(pt, sizeof(buffer) - (size_t)(intptr_t)(pt - buffer) - 1, format, args);
......
......@@ -29,16 +29,16 @@ enum debug_mask_enum {
DBG_RESERVED = 0x0001,
DBG_CONFIGFILE = 0x0002,
DBG_FILE = 0x0004,
DBG_AACS = 0x0008,
DBG_MKB = 0x0010,
DBG_MMC = 0x0020,
//DBG_AACS = 0x0008,
//DBG_MKB = 0x0010,
//DBG_MMC = 0x0020,
DBG_BLURAY = 0x0040,
DBG_DIR = 0x0080,
DBG_NAV = 0x0100,
//DBG_NAV = 0x0100,
DBG_BDPLUS = 0x0200,
DBG_DLX = 0x0400,
DBG_CRIT = 0x0800, // this is libbluray's default debug mask so use this if you want to display critical info
DBG_HDMV = 0x1000,
//DBG_HDMV = 0x1000,
DBG_BDPLUS_TRAP = 0x100000 | DBG_BDPLUS,
DBG_BDPLUS_EVENT = 0x200000 | DBG_BDPLUS,
......@@ -57,7 +57,6 @@ BD_PRIVATE extern uint32_t debug_mask;
#define BD_DEBUG DEBUG
BD_PRIVATE char *print_hex(char *out, const uint8_t *str, int count);
BD_PRIVATE void bd_debug(const char *file, int line, uint32_t mask, const char *format, ...) BD_ATTR_FORMAT_PRINTF(4,5);
......
......@@ -21,10 +21,8 @@
#ifndef MACRO_H_
#define MACRO_H_
#include <stdio.h> /* fprintf() */
#include <stdlib.h> /* free() */
#define HEX_PRINT(X,Y) { int zz; for(zz = 0; zz < Y; zz++) fprintf(stderr, "%02X", X[zz]); fprintf(stderr, "\n"); }
#define MKINT_BE16(X) ( (X)[0] << 8 | (X)[1] )
#define MKINT_BE24(X) ( (X)[0] << 16 | (X)[1] << 8 | (X)[2] )
#define MKINT_BE32(X) ( (X)[0] << 24 | (X)[1] << 16 | (X)[2] << 8 | (X)[3] )
......
......@@ -37,29 +37,40 @@
#include <errno.h>
typedef struct bd_mutex_s BD_MUTEX;
struct bd_mutex_s {
CRITICAL_SECTION cs;
};
typedef CRITICAL_SECTION BD_MUTEX;
#define bd_mutex_lock(m) \
(EnterCriticalSection(&m->cs), 0)
static inline int bd_mutex_lock(BD_MUTEX *p) {
EnterCriticalSection(p);
return 0;
}
#define bd_mutex_unlock(m) \
(LeaveCriticalSection(&m->cs), 0)
static inline int bd_mutex_unlock(BD_MUTEX *p) {
LeaveCriticalSection(p);
return 0;
}
#define bd_mutex_trylock(m) \
(TryEnterCriticalSection(&m->cs) ? 0 : EBUSY)
#if 0
static int bd_mutex_trylock(BD_MUTEX *p) {
return TryEnterCriticalSection(p) ? 0 : EBUSY;
}
#endif
#define bd_mutex_init(m) \
(InitializeCriticalSection(&m->cs), 0)
static inline int bd_mutex_init(BD_MUTEX *p) {
InitializeCriticalSection(p);
return 0;
}
#define bd_mutex_destroy(m) \
(DeleteCriticalSection(&m->cs), 0)
static inline int bd_mutex_destroy(BD_MUTEX *p) {
DeleteCriticalSection(p);
return 0;
}
#elif defined(HAVE_PTHREAD_H)
#include "logging.h"
#include "macro.h"
/*
* recursive mutex
*/
......@@ -71,27 +82,8 @@ struct bd_mutex_s {
pthread_mutex_t mutex;
};
static inline int bd_mutex_init(BD_MUTEX *p)
{
p->owner = (pthread_t)-1;
p->lock_count = 0;
if (pthread_mutex_init(&p->mutex, NULL)) {
BD_DEBUG(DBG_BLURAY|DBG_CRIT, "bd_mutex_init() failed !\n");
return -1;
}
return 0;
}
static inline int bd_mutex_destroy(BD_MUTEX *p)
{
if (pthread_mutex_destroy(&p->mutex)) {
BD_DEBUG(DBG_BLURAY|DBG_CRIT, "bd_mutex_destroy() failed !\n");
return -1;
}
return 0;
}
BD_PRIVATE int bd_mutex_init(BD_MUTEX *p);
BD_PRIVATE int bd_mutex_destroy(BD_MUTEX *p);
static int bd_mutex_lock(BD_MUTEX *p)
{
......
......@@ -24,12 +24,17 @@
#include "strutl.h"
#include "macro.h"
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#ifdef HAVE_REGEX_H
#include <regex.h>
#endif
#include <string.h>
char * str_dup(const char *str)
{
return str ? strcpy (malloc(strlen(str) + 1), str) : NULL;
}
char * str_printf(const char *fmt, ...)
{
......@@ -66,34 +71,6 @@ char * str_printf(const char *fmt, ...)
}
}
/*
* Regular expression function. Here a string is tested to see if it matches a
* particular regular expression. Setting icase to a non-zero value will set
* the regular expression to ignore case in match.
*/
#if 0
int str_match(const char *string, const char *regexp, int icase) {
#ifdef HAVE_REGEX_H
regex_t re;
int flags = REG_EXTENDED | REG_NOSUB;
if (icase) {
flags |= REG_ICASE;
}
if (!regcomp(&re, regexp, flags)) {
return 0;
}
int status = regexec(&re, string, 0, NULL, 0);
regfree(&re);
if (!status) {
return 0;
}
#else
#warning System does not have POSIX regex support.
#endif
return 1;
}
#endif
char *str_next_line(char *p)
{
while (*p && *p != '\r' && *p != '\n') {
......@@ -114,3 +91,13 @@ char *str_skip_white(char *p)
return p;
}
char *str_print_hex(char *out, const uint8_t *buf, int count)
{
int zz;
for (zz = 0; zz < count; zz++) {
sprintf(out + (zz * 2), "%02x", buf[zz]);
}
return out;
}
......@@ -23,12 +23,14 @@
#include "attributes.h"
#include <stdint.h>
BD_PRIVATE char * str_dup(const char *str) BD_ATTR_MALLOC;
BD_PRIVATE char * str_printf(const char *fmt, ...) BD_ATTR_FORMAT_PRINTF(1,2) BD_ATTR_MALLOC;
#if 0
BD_PRIVATE int str_match(const char *string, const char *regexp, int icase);
#endif
BD_PRIVATE char * str_skip_white(char *p);
BD_PRIVATE char * str_next_line(char *p);
BD_PRIVATE char * str_print_hex(char *out, const uint8_t *str, int count);
#endif // STRUTL_H_
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment