Skip to content

Commit

Permalink
Remove libxml2 dependency in favor of custom XML parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
nikias committed Oct 22, 2016
1 parent a3263ad commit 392135c
Show file tree
Hide file tree
Showing 10 changed files with 798 additions and 407 deletions.
27 changes: 18 additions & 9 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ LIBPLIST_SO_VERSION=3:0:0

AC_SUBST(LIBPLIST_SO_VERSION)

dnl Minimum package versions
LIBXML2_VERSION=2.7.8

AC_SUBST(LIBXML2_VERSION)

# Checks for programs.
AC_PROG_CC
AC_PROG_CXX
Expand All @@ -40,9 +35,6 @@ AC_LANG_POP
AM_PROG_CC_C_O
AC_PROG_LIBTOOL

# Checks for libraries.
PKG_CHECK_MODULES(libxml2, libxml-2.0 >= $LIBXML2_VERSION)

# Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS([stdint.h stdlib.h string.h])
Expand Down Expand Up @@ -139,6 +131,22 @@ AM_CONDITIONAL([HAVE_CYTHON],[test "x$CYTHON_SUB" = "xcython"])
AC_SUBST([CYTHON_SUB])

AS_COMPILER_FLAGS(GLOBAL_CFLAGS, "-Wall -Wextra -Wmissing-declarations -Wredundant-decls -Wshadow -Wpointer-arith -Wwrite-strings -Wswitch-default -Wno-unused-parameter -fvisibility=hidden")

AC_ARG_ENABLE(debug,
AS_HELP_STRING([--enable-debug],
[enable debugging, default: no]),
[case "${enableval}" in
yes) debug=yes ;;
no) debug=no ;;
*) AC_MSG_ERROR([bad value ${enableval} for --enable-debug]) ;;
esac],
[debug=no])

if (test "x$debug" = "xyes"); then
AC_DEFINE(DEBUG, 1, [Define if debug code should be enabled.])
GLOBAL_CFLAGS+=" -g"
fi

AC_SUBST(GLOBAL_CFLAGS)

case "$GLOBAL_CFLAGS" in
Expand All @@ -165,7 +173,8 @@ echo "
Configuration for $PACKAGE $VERSION:
-------------------------------------------

Install prefix: .........: $prefix
Install prefix ..........: $prefix
Debug code ..............: $debug
Python bindings .........: $cython_python_bindings

Now type 'make' to build $PACKAGE $VERSION,
Expand Down
13 changes: 0 additions & 13 deletions include/plist/plist.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,6 @@ extern "C"
} plist_type;


/********************************************
* *
* Library Initialization & Cleanup *
* *
********************************************/

/**
* Frees memory used globally by listplist, in
* particular the libxml parser
*/

void plist_cleanup(void);

/********************************************
* *
* Creation & Destruction *
Expand Down
5 changes: 3 additions & 2 deletions src/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_srcdir) -I$(top_srcdir)/libcnary/include

AM_CFLAGS = $(GLOBAL_CFLAGS) $(libxml2_CFLAGS)
AM_LDFLAGS = $(libxml2_LIBS)
AM_CFLAGS = $(GLOBAL_CFLAGS)
AM_LDFLAGS =

lib_LTLIBRARIES = libplist.la libplist++.la
libplist_la_LIBADD = $(top_builddir)/libcnary/libcnary.la
libplist_la_LDFLAGS = $(AM_LDFLAGS) -version-info $(LIBPLIST_SO_VERSION) -no-undefined
libplist_la_SOURCES = base64.c base64.h \
bytearray.c bytearray.h \
strbuf.h \
hashtable.c hashtable.h \
ptrarray.c ptrarray.h \
time64.c time64.h time64_limits.h \
Expand Down
22 changes: 11 additions & 11 deletions src/base64.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,32 +43,32 @@ static const signed char base64_table[256] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
};

char *base64encode(const unsigned char *buf, size_t *size)
size_t base64encode(char *outbuf, const unsigned char *buf, size_t size)
{
if (!buf || !size || !(*size > 0)) return NULL;
int outlen = (*size / 3) * 4;
char *outbuf = (char*)malloc(outlen+5); // 4 spare bytes + 1 for '\0'
if (!outbuf || !buf || (size <= 0)) {
return 0;
}

size_t n = 0;
size_t m = 0;
unsigned char input[3];
unsigned int output[4];
while (n < *size) {
while (n < size) {
input[0] = buf[n];
input[1] = (n+1 < *size) ? buf[n+1] : 0;
input[2] = (n+2 < *size) ? buf[n+2] : 0;
input[1] = (n+1 < size) ? buf[n+1] : 0;
input[2] = (n+2 < size) ? buf[n+2] : 0;
output[0] = input[0] >> 2;
output[1] = ((input[0] & 3) << 4) + (input[1] >> 4);
output[2] = ((input[1] & 15) << 2) + (input[2] >> 6);
output[3] = input[2] & 63;
outbuf[m++] = base64_str[(int)output[0]];
outbuf[m++] = base64_str[(int)output[1]];
outbuf[m++] = (n+1 < *size) ? base64_str[(int)output[2]] : base64_pad;
outbuf[m++] = (n+2 < *size) ? base64_str[(int)output[3]] : base64_pad;
outbuf[m++] = (n+1 < size) ? base64_str[(int)output[2]] : base64_pad;
outbuf[m++] = (n+2 < size) ? base64_str[(int)output[3]] : base64_pad;
n+=3;
}
outbuf[m] = 0; // 0-termination!
*size = m;
return outbuf;
return m;
}

static int base64decode_block(unsigned char *target, const char *data, size_t data_size)
Expand Down
2 changes: 1 addition & 1 deletion src/base64.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#define BASE64_H
#include <stdlib.h>

char *base64encode(const unsigned char *buf, size_t *size);
size_t base64encode(char *outbuf, const unsigned char *buf, size_t size);
unsigned char *base64decode(const char *buf, size_t *size);

#endif
3 changes: 1 addition & 2 deletions src/bplist.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* bplist.c
* Binary plist implementation
*
* Copyright (c) 2011-2015 Nikias Bassen, All Rights Reserved.
* Copyright (c) 2011-2016 Nikias Bassen, All Rights Reserved.
* Copyright (c) 2008-2010 Jonathan Beck, All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
Expand All @@ -28,7 +28,6 @@
#include <stdio.h>
#include <string.h>

#include <libxml/encoding.h>
#include <ctype.h>

#include <plist/plist.h>
Expand Down
12 changes: 8 additions & 4 deletions src/bytearray.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
#include <string.h>
#include "bytearray.h"

#define PAGE_SIZE 4096

bytearray_t *byte_array_new()
{
bytearray_t *a = (bytearray_t*)malloc(sizeof(bytearray_t));
a->data = malloc(256);
a->capacity = PAGE_SIZE * 8;
a->data = malloc(a->capacity);
a->len = 0;
a->capacity = 256;
return a;
}

Expand All @@ -44,8 +46,10 @@ void byte_array_append(bytearray_t *ba, void *buf, size_t len)
if (!ba || !ba->data || (len <= 0)) return;
size_t remaining = ba->capacity-ba->len;
if (len > remaining) {
ba->data = realloc(ba->data, ba->capacity + (len - remaining));
ba->capacity += (len - remaining);
size_t needed = len - remaining;
size_t increase = (needed > PAGE_SIZE) ? (needed+(PAGE_SIZE-1)) & (~(PAGE_SIZE-1)) : PAGE_SIZE;
ba->data = realloc(ba->data, ba->capacity + increase);
ba->capacity += increase;
}
memcpy(((char*)ba->data) + ba->len, buf, len);
ba->len += len;
Expand Down
86 changes: 71 additions & 15 deletions src/plist.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* plist.c
* Builds plist XML structures
*
* Copyright (c) 2009-2016 Nikias Bassen All Rights Reserved.
* Copyright (c) 2010-2015 Martin Szulecki All Rights Reserved.
* Copyright (c) 2009-2014 Nikias Bassen All Rights Reserved.
* Copyright (c) 2008 Zach C. All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
Expand All @@ -29,27 +29,83 @@
#include <stdio.h>
#include <math.h>

#ifdef WIN32
#include <windows.h>
#else
#include <pthread.h>
#endif

#include <node.h>
#include <node_iterator.h>

#include <libxml/encoding.h>
#include <libxml/dict.h>
#include <libxml/xmlerror.h>
#include <libxml/globals.h>
#include <libxml/threads.h>
#include <libxml/xmlmemory.h>
extern void plist_xml_init(void);
extern void plist_xml_deinit(void);

static void internal_plist_init(void)
{
plist_xml_init();
}

static void internal_plist_deinit(void)
{
plist_xml_deinit();
}

#ifdef WIN32

typedef volatile struct {
LONG lock;
int state;
} thread_once_t;

static thread_once_t init_once = {0, 0};
static thread_once_t deinit_once = {0, 0};

void thread_once(thread_once_t *once_control, void (*init_routine)(void))
{
while (InterlockedExchange(&(once_control->lock), 1) != 0) {
Sleep(1);
}
if (!once_control->state) {
once_control->state = 1;
init_routine();
}
InterlockedExchange(&(once_control->lock), 0);
}

BOOL WINAPI DllMain(HINSTANCE hModule, DWORD dwReason, LPVOID lpReserved)
{
switch (dwReason) {
case DLL_PROCESS_ATTACH:
thread_once(&init_once, internal_plist_init);
break;
case DLL_PROCESS_DETACH:
thread_once(&deinit_once, internal_plist_deinit);
break;
default:
break;
}
return 1;
}

PLIST_API void plist_cleanup(void)
#else

static pthread_once_t init_once = PTHREAD_ONCE_INIT;
static pthread_once_t deinit_once = PTHREAD_ONCE_INIT;

static void __attribute__((constructor)) libplist_initialize(void)
{
/* free memory from parser initialization */
xmlCleanupCharEncodingHandlers();
xmlDictCleanup();
xmlResetLastError();
xmlCleanupGlobals();
xmlCleanupThreads();
xmlCleanupMemory();
pthread_once(&init_once, internal_plist_init);
}

static void __attribute__((destructor)) libplist_deinitialize(void)
{
pthread_once(&deinit_once, internal_plist_deinit);
}

#endif


PLIST_API int plist_is_binary(const char *plist_data, uint32_t length)
{
if (length < 8) {
Expand Down
33 changes: 33 additions & 0 deletions src/strbuf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* strbuf.h
* header file for simple string buffer, using the bytearray as underlying
* structure.
*
* Copyright (c) 2016 Nikias Bassen, All Rights Reserved.
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef STRBUF_H
#define STRBUF_H
#include <stdlib.h>
#include "bytearray.h"

typedef struct bytearray_t strbuf_t;

#define str_buf_new() byte_array_new()
#define str_buf_free(__ba) byte_array_free(__ba)
#define str_buf_append(__ba, __str, __len) byte_array_append(__ba, (void*)(__str), __len)

#endif

0 comments on commit 392135c

Please sign in to comment.