import Unicorn2
This commit is contained in:
2
glib_compat/README
Normal file
2
glib_compat/README
Normal file
@@ -0,0 +1,2 @@
|
||||
This is a compatible glib library, customized for Unicorn.
|
||||
Based on glib 2.64.4.
|
||||
1649
glib_compat/garray.c
Normal file
1649
glib_compat/garray.c
Normal file
File diff suppressed because it is too large
Load Diff
99
glib_compat/garray.h
Normal file
99
glib_compat/garray.h
Normal file
@@ -0,0 +1,99 @@
|
||||
/* GLIB - Library of useful routines for C programming
|
||||
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Modified by the GLib Team and others 1997-2000. See the AUTHORS
|
||||
* file for a list of people on the GLib Team. See the ChangeLog
|
||||
* files for a list of changes. These files are distributed with
|
||||
* GLib at ftp://ftp.gtk.org/pub/gtk/.
|
||||
*/
|
||||
|
||||
#ifndef __G_ARRAY_H__
|
||||
#define __G_ARRAY_H__
|
||||
|
||||
#include "gtypes.h"
|
||||
|
||||
typedef struct _GBytes GBytes;
|
||||
typedef struct _GArray GArray;
|
||||
typedef struct _GByteArray GByteArray;
|
||||
typedef struct _GPtrArray GPtrArray;
|
||||
|
||||
struct _GArray
|
||||
{
|
||||
gchar *data;
|
||||
guint len;
|
||||
};
|
||||
|
||||
struct _GByteArray
|
||||
{
|
||||
guint8 *data;
|
||||
guint len;
|
||||
};
|
||||
|
||||
struct _GPtrArray
|
||||
{
|
||||
gpointer *pdata;
|
||||
guint len;
|
||||
};
|
||||
|
||||
/* Resizable arrays. remove fills any cleared spot and shortens the
|
||||
* array, while preserving the order. remove_fast will distort the
|
||||
* order by moving the last element to the position of the removed.
|
||||
*/
|
||||
|
||||
#define g_array_append_val(a,v) g_array_append_vals (a, &(v), 1)
|
||||
#define g_array_index(a,t,i) (((t*) (void *) (a)->data) [(i)])
|
||||
|
||||
GArray* g_array_append_vals (GArray *array,
|
||||
gconstpointer data,
|
||||
guint len);
|
||||
|
||||
GArray* g_array_new (gboolean zero_terminated, gboolean clear_, guint element_size);
|
||||
GArray* g_array_sized_new (gboolean zero_terminated,
|
||||
gboolean clear_,
|
||||
guint element_size,
|
||||
guint reserved_size);
|
||||
|
||||
gchar* g_array_free(GArray *array, gboolean free_segment);
|
||||
GArray* g_array_set_size(GArray *array, guint length);
|
||||
GArray*
|
||||
g_array_remove_range (GArray *farray,
|
||||
guint index_,
|
||||
guint length);
|
||||
|
||||
void g_ptr_array_set_free_func (GPtrArray *array,
|
||||
GDestroyNotify element_free_func);
|
||||
|
||||
/* Resizable pointer array. This interface is much less complicated
|
||||
* than the above. Add appends a pointer. Remove fills any cleared
|
||||
* spot and shortens the array. remove_fast will again distort order.
|
||||
*/
|
||||
#define g_ptr_array_index(array,index_) ((array)->pdata)[index_]
|
||||
GPtrArray* g_ptr_array_new_with_free_func (GDestroyNotify element_free_func);
|
||||
void g_ptr_array_add(GPtrArray *array, gpointer data);
|
||||
GPtrArray* g_ptr_array_sized_new (guint reserved_size);
|
||||
GPtrArray* g_ptr_array_remove_range (GPtrArray *array, guint index_, guint length);
|
||||
|
||||
/* Byte arrays, an array of guint8. Implemented as a GArray,
|
||||
* but type-safe.
|
||||
*/
|
||||
GByteArray* g_byte_array_sized_new(guint reserved_size);
|
||||
guint8* g_byte_array_free(GByteArray *array, gboolean free_segment);
|
||||
GByteArray* g_byte_array_append(GByteArray *array, const guint8 *data, guint len);
|
||||
GByteArray* g_byte_array_set_size(GByteArray *array, guint length);
|
||||
|
||||
#endif /* __G_ARRAY_H__ */
|
||||
77
glib_compat/ghash.h
Normal file
77
glib_compat/ghash.h
Normal file
@@ -0,0 +1,77 @@
|
||||
/* GLIB - Library of useful routines for C programming
|
||||
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Modified by the GLib Team and others 1997-2000. See the AUTHORS
|
||||
* file for a list of people on the GLib Team. See the ChangeLog
|
||||
* files for a list of changes. These files are distributed with
|
||||
* GLib at ftp://ftp.gtk.org/pub/gtk/.
|
||||
*/
|
||||
|
||||
#ifndef __G_HASH_H__
|
||||
#define __G_HASH_H__
|
||||
|
||||
#include "gtypes.h"
|
||||
|
||||
typedef struct _GHashTable GHashTable;
|
||||
|
||||
typedef gboolean (*GHRFunc) (gpointer key, gpointer value, gpointer user_data);
|
||||
|
||||
struct _GHashTableIter
|
||||
{
|
||||
/*< private >*/
|
||||
gpointer dummy1;
|
||||
gpointer dummy2;
|
||||
gpointer dummy3;
|
||||
int dummy4;
|
||||
gboolean dummy5;
|
||||
gpointer dummy6;
|
||||
};
|
||||
|
||||
GHashTable* g_hash_table_new (GHashFunc hash_func, GEqualFunc key_equal_func);
|
||||
|
||||
GHashTable* g_hash_table_new_full (GHashFunc hash_func,
|
||||
GEqualFunc key_equal_func,
|
||||
GDestroyNotify key_destroy_func,
|
||||
GDestroyNotify value_destroy_func);
|
||||
|
||||
void g_hash_table_destroy (GHashTable *hash_table);
|
||||
|
||||
gboolean g_hash_table_insert (GHashTable *hash_table, gpointer key, gpointer value);
|
||||
|
||||
void g_hash_table_replace (GHashTable *hash_table, gpointer key, gpointer value);
|
||||
|
||||
gboolean g_hash_table_remove (GHashTable *hash_table, gconstpointer key);
|
||||
|
||||
void g_hash_table_remove_all (GHashTable *hash_table);
|
||||
|
||||
gpointer g_hash_table_lookup (GHashTable *hash_table, gconstpointer key);
|
||||
|
||||
void g_hash_table_foreach (GHashTable *hash_table, GHFunc func, gpointer user_data);
|
||||
|
||||
guint g_hash_table_size (GHashTable *hash_table);
|
||||
|
||||
GHashTable* g_hash_table_ref (GHashTable *hash_table);
|
||||
|
||||
void g_hash_table_unref (GHashTable *hash_table);
|
||||
|
||||
/* Hash Functions
|
||||
*/
|
||||
gboolean g_int_equal (gconstpointer v1, gconstpointer v2);
|
||||
guint g_int_hash (gconstpointer v);
|
||||
|
||||
#endif /* __G_HASH_H__ */
|
||||
1492
glib_compat/glib_compat.c
Normal file
1492
glib_compat/glib_compat.c
Normal file
File diff suppressed because it is too large
Load Diff
97
glib_compat/glib_compat.h
Normal file
97
glib_compat/glib_compat.h
Normal file
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
glib_compat.h replacement functionality for glib code used in qemu
|
||||
Copyright (C) 2016 Chris Eagle cseagle at gmail dot com
|
||||
|
||||
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; either version 2
|
||||
of the License, or (at your option) any later version.
|
||||
|
||||
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, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#ifndef __GLIB_COMPAT_H
|
||||
#define __GLIB_COMPAT_H
|
||||
|
||||
#include "unicorn/platform.h"
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
|
||||
#define G_MAXUINT UINT_MAX
|
||||
#define G_MAXINT INT_MAX
|
||||
|
||||
#include "gtestutils.h"
|
||||
#include "gtypes.h"
|
||||
#include "garray.h"
|
||||
#include "gtree.h"
|
||||
#include "ghash.h"
|
||||
#include "gmem.h"
|
||||
#include "gslice.h"
|
||||
#include "gmessages.h"
|
||||
#include "gpattern.h"
|
||||
#include "grand.h"
|
||||
#include "glist.h"
|
||||
#include "gnode.h"
|
||||
|
||||
typedef gint (*GCompareDataFunc)(gconstpointer a,
|
||||
gconstpointer b,
|
||||
gpointer user_data);
|
||||
typedef void (*GFunc)(gpointer data, gpointer user_data);
|
||||
typedef gint (*GCompareFunc)(gconstpointer v1, gconstpointer v2);
|
||||
|
||||
guint g_str_hash(gconstpointer v);
|
||||
gboolean g_str_equal(gconstpointer v1, gconstpointer v2);
|
||||
guint g_int_hash(gconstpointer v);
|
||||
|
||||
gboolean g_int_equal(gconstpointer v1, gconstpointer v2);
|
||||
|
||||
int g_strcmp0(const char *str1, const char *str2);
|
||||
|
||||
GList *g_list_first(GList *list);
|
||||
void g_list_foreach(GList *list, GFunc func, gpointer user_data);
|
||||
void g_list_free(GList *list);
|
||||
GList *g_list_insert_sorted(GList *list, gpointer data, GCompareFunc compare);
|
||||
#define g_list_next(list) (list->next)
|
||||
GList *g_list_prepend(GList *list, gpointer data);
|
||||
GList *g_list_remove_link(GList *list, GList *llink);
|
||||
GList *g_list_sort(GList *list, GCompareFunc compare);
|
||||
|
||||
typedef struct _GSList {
|
||||
gpointer data;
|
||||
struct _GSList *next;
|
||||
} GSList;
|
||||
|
||||
GSList *g_slist_append(GSList *list, gpointer data);
|
||||
void g_slist_foreach(GSList *list, GFunc func, gpointer user_data);
|
||||
void g_slist_free(GSList *list);
|
||||
GSList *g_slist_prepend(GSList *list, gpointer data);
|
||||
GSList *g_slist_sort(GSList *list, GCompareFunc compare);
|
||||
GSList *g_slist_find_custom(GSList *list, gconstpointer data, GCompareFunc func);
|
||||
|
||||
/* replacement for g_malloc dependency */
|
||||
void g_free(gpointer ptr);
|
||||
gpointer g_realloc(gpointer ptr, size_t size);
|
||||
|
||||
char *g_strdup(const char *str);
|
||||
char *g_strdup_printf(const char *format, ...);
|
||||
char *g_strdup_vprintf(const char *format, va_list ap);
|
||||
char *g_strndup(const char *str, size_t n);
|
||||
void g_strfreev(char **v);
|
||||
gpointer g_memdup(gconstpointer mem, size_t byte_size);
|
||||
gpointer g_new_(size_t sz, size_t n_structs);
|
||||
gpointer g_new0_(size_t sz, size_t n_structs);
|
||||
gpointer g_renew_(size_t sz, gpointer mem, size_t n_structs);
|
||||
|
||||
gchar** g_strsplit (const gchar *string,
|
||||
const gchar *delimiter,
|
||||
gint max_tokens);
|
||||
|
||||
#endif
|
||||
154
glib_compat/glist.c
Normal file
154
glib_compat/glist.c
Normal file
@@ -0,0 +1,154 @@
|
||||
/* GLIB - Library of useful routines for C programming
|
||||
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Modified by the GLib Team and others 1997-2000. See the AUTHORS
|
||||
* file for a list of people on the GLib Team. See the ChangeLog
|
||||
* files for a list of changes. These files are distributed with
|
||||
* GLib at ftp://ftp.gtk.org/pub/gtk/.
|
||||
*/
|
||||
|
||||
/*
|
||||
* MT safe
|
||||
*/
|
||||
|
||||
#include "gtypes.h"
|
||||
#include "glist.h"
|
||||
#include "gslice.h"
|
||||
#include "gmessages.h"
|
||||
|
||||
#define _g_list_alloc() g_slice_new (GList)
|
||||
#define _g_list_alloc0() g_slice_new0 (GList)
|
||||
#define _g_list_free1(list) g_slice_free (GList, list)
|
||||
|
||||
/**
|
||||
* g_list_alloc:
|
||||
*
|
||||
* Allocates space for one #GList element. It is called by
|
||||
* g_list_append(), g_list_prepend(), g_list_insert() and
|
||||
* g_list_insert_sorted() and so is rarely used on its own.
|
||||
*
|
||||
* Returns: a pointer to the newly-allocated #GList element
|
||||
**/
|
||||
GList *g_list_alloc (void)
|
||||
{
|
||||
return _g_list_alloc0 ();
|
||||
}
|
||||
|
||||
static inline GList *_g_list_remove_link (GList *list, GList *link)
|
||||
{
|
||||
if (link == NULL)
|
||||
return list;
|
||||
|
||||
if (link->prev)
|
||||
{
|
||||
if (link->prev->next == link)
|
||||
link->prev->next = link->next;
|
||||
//else
|
||||
// g_warning ("corrupted double-linked list detected");
|
||||
}
|
||||
if (link->next)
|
||||
{
|
||||
if (link->next->prev == link)
|
||||
link->next->prev = link->prev;
|
||||
//else
|
||||
// g_warning ("corrupted double-linked list detected");
|
||||
}
|
||||
|
||||
if (link == list)
|
||||
list = list->next;
|
||||
|
||||
link->next = NULL;
|
||||
link->prev = NULL;
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* g_list_delete_link:
|
||||
* @list: a #GList, this must point to the top of the list
|
||||
* @link_: node to delete from @list
|
||||
*
|
||||
* Removes the node link_ from the list and frees it.
|
||||
* Compare this to g_list_remove_link() which removes the node
|
||||
* without freeing it.
|
||||
*
|
||||
* Returns: the (possibly changed) start of the #GList
|
||||
*/
|
||||
GList *g_list_delete_link (GList *list, GList *link_)
|
||||
{
|
||||
list = _g_list_remove_link (list, link_);
|
||||
_g_list_free1 (link_);
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* g_list_insert_before:
|
||||
* @list: a pointer to a #GList, this must point to the top of the list
|
||||
* @sibling: the list element before which the new element
|
||||
* is inserted or %NULL to insert at the end of the list
|
||||
* @data: the data for the new element
|
||||
*
|
||||
* Inserts a new element into the list before the given position.
|
||||
*
|
||||
* Returns: the (possibly changed) start of the #GList
|
||||
*/
|
||||
GList *g_list_insert_before (GList *list, GList *sibling, gpointer data)
|
||||
{
|
||||
if (list == NULL)
|
||||
{
|
||||
list = g_list_alloc ();
|
||||
list->data = data;
|
||||
g_return_val_if_fail (sibling == NULL, list);
|
||||
return list;
|
||||
}
|
||||
else if (sibling != NULL)
|
||||
{
|
||||
GList *node;
|
||||
|
||||
node = _g_list_alloc ();
|
||||
node->data = data;
|
||||
node->prev = sibling->prev;
|
||||
node->next = sibling;
|
||||
sibling->prev = node;
|
||||
if (node->prev != NULL)
|
||||
{
|
||||
node->prev->next = node;
|
||||
return list;
|
||||
}
|
||||
else
|
||||
{
|
||||
g_return_val_if_fail (sibling == list, node);
|
||||
return node;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
GList *last;
|
||||
|
||||
for (last = list; last->next != NULL; last = last->next) {}
|
||||
|
||||
last->next = _g_list_alloc ();
|
||||
last->next->data = data;
|
||||
last->next->prev = last;
|
||||
last->next->next = NULL;
|
||||
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
||||
44
glib_compat/glist.h
Normal file
44
glib_compat/glist.h
Normal file
@@ -0,0 +1,44 @@
|
||||
/* GLIB - Library of useful routines for C programming
|
||||
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Modified by the GLib Team and others 1997-2000. See the AUTHORS
|
||||
* file for a list of people on the GLib Team. See the ChangeLog
|
||||
* files for a list of changes. These files are distributed with
|
||||
* GLib at ftp://ftp.gtk.org/pub/gtk/.
|
||||
*/
|
||||
|
||||
#ifndef __G_LIST_H__
|
||||
#define __G_LIST_H__
|
||||
|
||||
#include "gmem.h"
|
||||
|
||||
typedef struct _GList GList;
|
||||
|
||||
struct _GList
|
||||
{
|
||||
gpointer data;
|
||||
GList *next;
|
||||
GList *prev;
|
||||
};
|
||||
|
||||
|
||||
GList* g_list_insert_before (GList *list, GList *sibling, gpointer data);
|
||||
|
||||
GList* g_list_delete_link (GList *list, GList *link_);
|
||||
|
||||
#endif /* __G_LIST_H__ */
|
||||
59
glib_compat/gmacros.h
Normal file
59
glib_compat/gmacros.h
Normal file
@@ -0,0 +1,59 @@
|
||||
/* GLIB - Library of useful routines for C programming
|
||||
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Modified by the GLib Team and others 1997-2000. See the AUTHORS
|
||||
* file for a list of people on the GLib Team. See the ChangeLog
|
||||
* files for a list of changes. These files are distributed with
|
||||
* GLib at ftp://ftp.gtk.org/pub/gtk/.
|
||||
*/
|
||||
|
||||
/* This file must not include any other glib header file and must thus
|
||||
* not refer to variables from glibconfig.h
|
||||
*/
|
||||
|
||||
#ifndef __G_MACROS_H__
|
||||
#define __G_MACROS_H__
|
||||
|
||||
/* We include stddef.h to get the system's definition of NULL
|
||||
*/
|
||||
#include <stddef.h>
|
||||
|
||||
/* Here we provide G_GNUC_EXTENSION as an alias for __extension__,
|
||||
* where this is valid. This allows for warningless compilation of
|
||||
* "long long" types even in the presence of '-ansi -pedantic'.
|
||||
*/
|
||||
#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8)
|
||||
#define G_GNUC_EXTENSION __extension__
|
||||
#else
|
||||
#define G_GNUC_EXTENSION
|
||||
#endif
|
||||
|
||||
#if !(defined (G_STMT_START) && defined (G_STMT_END))
|
||||
#define G_STMT_START do
|
||||
#if defined (_MSC_VER) && (_MSC_VER >= 1500)
|
||||
#define G_STMT_END \
|
||||
__pragma(warning(push)) \
|
||||
__pragma(warning(disable:4127)) \
|
||||
while(0) \
|
||||
__pragma(warning(pop))
|
||||
#else
|
||||
#define G_STMT_END while (0)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#endif /* __G_MACROS_H__ */
|
||||
257
glib_compat/gmem.c
Normal file
257
glib_compat/gmem.c
Normal file
@@ -0,0 +1,257 @@
|
||||
/* GLIB - Library of useful routines for C programming
|
||||
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Modified by the GLib Team and others 1997-2000. See the AUTHORS
|
||||
* file for a list of people on the GLib Team. See the ChangeLog
|
||||
* files for a list of changes. These files are distributed with
|
||||
* GLib at ftp://ftp.gtk.org/pub/gtk/.
|
||||
*/
|
||||
|
||||
/*
|
||||
* MT safe
|
||||
*/
|
||||
|
||||
#include "gtypes.h"
|
||||
#include "gmem.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "gslice.h"
|
||||
|
||||
#define SIZE_OVERFLOWS(a,b) (((b) > 0 && (a) > G_MAXSIZE / (b)))
|
||||
|
||||
|
||||
/**
|
||||
* g_try_malloc:
|
||||
* @n_bytes: number of bytes to allocate.
|
||||
*
|
||||
* Attempts to allocate @n_bytes, and returns %NULL on failure.
|
||||
* Contrast with g_malloc(), which aborts the program on failure.
|
||||
*
|
||||
* Returns: the allocated memory, or %NULL.
|
||||
*/
|
||||
gpointer g_try_malloc (gsize n_bytes)
|
||||
{
|
||||
gpointer mem;
|
||||
|
||||
if (n_bytes)
|
||||
mem = malloc (n_bytes);
|
||||
else
|
||||
mem = NULL;
|
||||
|
||||
return mem;
|
||||
}
|
||||
|
||||
/**
|
||||
* g_try_malloc_n:
|
||||
* @n_blocks: the number of blocks to allocate
|
||||
* @n_block_bytes: the size of each block in bytes
|
||||
*
|
||||
* This function is similar to g_try_malloc(), allocating (@n_blocks * @n_block_bytes) bytes,
|
||||
* but care is taken to detect possible overflow during multiplication.
|
||||
*
|
||||
* Since: 2.24
|
||||
* Returns: the allocated memory, or %NULL.
|
||||
*/
|
||||
gpointer g_try_malloc_n (gsize n_blocks, gsize n_block_bytes)
|
||||
{
|
||||
if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
|
||||
return NULL;
|
||||
|
||||
return g_try_malloc (n_blocks * n_block_bytes);
|
||||
}
|
||||
|
||||
/**
|
||||
* g_malloc:
|
||||
* @n_bytes: the number of bytes to allocate
|
||||
*
|
||||
* Allocates @n_bytes bytes of memory.
|
||||
* If @n_bytes is 0 it returns %NULL.
|
||||
*
|
||||
* Returns: a pointer to the allocated memory
|
||||
*/
|
||||
gpointer g_malloc (gsize n_bytes)
|
||||
{
|
||||
if (n_bytes) {
|
||||
gpointer mem;
|
||||
|
||||
mem = malloc (n_bytes);
|
||||
if (mem)
|
||||
return mem;
|
||||
|
||||
//g_error ("%s: failed to allocate %"G_GSIZE_FORMAT" bytes",
|
||||
// G_STRLOC, n_bytes);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* g_malloc_n:
|
||||
* @n_blocks: the number of blocks to allocate
|
||||
* @n_block_bytes: the size of each block in bytes
|
||||
*
|
||||
* This function is similar to g_malloc(), allocating (@n_blocks * @n_block_bytes) bytes,
|
||||
* but care is taken to detect possible overflow during multiplication.
|
||||
*
|
||||
* Since: 2.24
|
||||
* Returns: a pointer to the allocated memory
|
||||
*/
|
||||
gpointer g_malloc_n (gsize n_blocks, gsize n_block_bytes)
|
||||
{
|
||||
if (SIZE_OVERFLOWS (n_blocks, n_block_bytes)) {
|
||||
//g_error ("%s: overflow allocating %"G_GSIZE_FORMAT"*%"G_GSIZE_FORMAT" bytes",
|
||||
// G_STRLOC, n_blocks, n_block_bytes);
|
||||
}
|
||||
|
||||
return g_malloc (n_blocks * n_block_bytes);
|
||||
}
|
||||
|
||||
/**
|
||||
* g_malloc0:
|
||||
* @n_bytes: the number of bytes to allocate
|
||||
*
|
||||
* Allocates @n_bytes bytes of memory, initialized to 0's.
|
||||
* If @n_bytes is 0 it returns %NULL.
|
||||
*
|
||||
* Returns: a pointer to the allocated memory
|
||||
*/
|
||||
gpointer g_malloc0 (gsize n_bytes)
|
||||
{
|
||||
if (n_bytes) {
|
||||
gpointer mem;
|
||||
|
||||
mem = calloc (1, n_bytes);
|
||||
if (mem)
|
||||
return mem;
|
||||
|
||||
//g_error ("%s: failed to allocate %"G_GSIZE_FORMAT" bytes",
|
||||
// G_STRLOC, n_bytes);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* g_malloc0_n:
|
||||
* @n_blocks: the number of blocks to allocate
|
||||
* @n_block_bytes: the size of each block in bytes
|
||||
*
|
||||
* This function is similar to g_malloc0(), allocating (@n_blocks * @n_block_bytes) bytes,
|
||||
* but care is taken to detect possible overflow during multiplication.
|
||||
*
|
||||
* Since: 2.24
|
||||
* Returns: a pointer to the allocated memory
|
||||
*/
|
||||
gpointer g_malloc0_n (gsize n_blocks, gsize n_block_bytes)
|
||||
{
|
||||
if (SIZE_OVERFLOWS (n_blocks, n_block_bytes)) {
|
||||
//g_error ("%s: overflow allocating %"G_GSIZE_FORMAT"*%"G_GSIZE_FORMAT" bytes",
|
||||
// G_STRLOC, n_blocks, n_block_bytes);
|
||||
}
|
||||
|
||||
return g_malloc0 (n_blocks * n_block_bytes);
|
||||
}
|
||||
|
||||
/**
|
||||
* g_try_malloc0:
|
||||
* @n_bytes: number of bytes to allocate
|
||||
*
|
||||
* Attempts to allocate @n_bytes, initialized to 0's, and returns %NULL on
|
||||
* failure. Contrast with g_malloc0(), which aborts the program on failure.
|
||||
*
|
||||
* Since: 2.8
|
||||
* Returns: the allocated memory, or %NULL
|
||||
*/
|
||||
gpointer g_try_malloc0 (gsize n_bytes)
|
||||
{
|
||||
gpointer mem;
|
||||
|
||||
if (n_bytes)
|
||||
mem = calloc (1, n_bytes);
|
||||
else
|
||||
mem = NULL;
|
||||
|
||||
return mem;
|
||||
}
|
||||
|
||||
/**
|
||||
* g_realloc:
|
||||
* @mem: (nullable): the memory to reallocate
|
||||
* @n_bytes: new size of the memory in bytes
|
||||
*
|
||||
* Reallocates the memory pointed to by @mem, so that it now has space for
|
||||
* @n_bytes bytes of memory. It returns the new address of the memory, which may
|
||||
* have been moved. @mem may be %NULL, in which case it's considered to
|
||||
* have zero-length. @n_bytes may be 0, in which case %NULL will be returned
|
||||
* and @mem will be freed unless it is %NULL.
|
||||
*
|
||||
* Returns: the new address of the allocated memory
|
||||
*/
|
||||
gpointer g_realloc (gpointer mem, gsize n_bytes)
|
||||
{
|
||||
gpointer newmem;
|
||||
|
||||
if (n_bytes) {
|
||||
newmem = realloc (mem, n_bytes);
|
||||
if (newmem)
|
||||
return newmem;
|
||||
|
||||
//g_error("%s: failed to allocate %"G_GSIZE_FORMAT" bytes", G_STRLOC, n_bytes);
|
||||
}
|
||||
|
||||
free (mem);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* g_realloc_n:
|
||||
* @mem: (nullable): the memory to reallocate
|
||||
* @n_blocks: the number of blocks to allocate
|
||||
* @n_block_bytes: the size of each block in bytes
|
||||
*
|
||||
* This function is similar to g_realloc(), allocating (@n_blocks * @n_block_bytes) bytes,
|
||||
* but care is taken to detect possible overflow during multiplication.
|
||||
*
|
||||
* Since: 2.24
|
||||
* Returns: the new address of the allocated memory
|
||||
*/
|
||||
gpointer g_realloc_n (gpointer mem, gsize n_blocks, gsize n_block_bytes)
|
||||
{
|
||||
if (SIZE_OVERFLOWS (n_blocks, n_block_bytes)) {
|
||||
//g_error ("%s: overflow allocating %"G_GSIZE_FORMAT"*%"G_GSIZE_FORMAT" bytes",
|
||||
// G_STRLOC, n_blocks, n_block_bytes);
|
||||
}
|
||||
|
||||
return g_realloc (mem, n_blocks * n_block_bytes);
|
||||
}
|
||||
|
||||
/**
|
||||
* g_free:
|
||||
* @mem: (nullable): the memory to free
|
||||
*
|
||||
* Frees the memory pointed to by @mem.
|
||||
*
|
||||
* If @mem is %NULL it simply returns, so there is no need to check @mem
|
||||
* against %NULL before calling this function.
|
||||
*/
|
||||
void g_free (gpointer mem)
|
||||
{
|
||||
free (mem);
|
||||
}
|
||||
111
glib_compat/gmem.h
Normal file
111
glib_compat/gmem.h
Normal file
@@ -0,0 +1,111 @@
|
||||
/* GLIB - Library of useful routines for C programming
|
||||
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Modified by the GLib Team and others 1997-2000. See the AUTHORS
|
||||
* file for a list of people on the GLib Team. See the ChangeLog
|
||||
* files for a list of changes. These files are distributed with
|
||||
* GLib at ftp://ftp.gtk.org/pub/gtk/.
|
||||
*/
|
||||
|
||||
#ifndef __G_MEM_H__
|
||||
#define __G_MEM_H__
|
||||
|
||||
#include <limits.h>
|
||||
#include "gmacros.h"
|
||||
|
||||
#define G_MAXSIZE ULONG_MAX
|
||||
|
||||
/* Optimise: avoid the call to the (slower) _n function if we can
|
||||
* determine at compile-time that no overflow happens.
|
||||
*/
|
||||
#if defined (__GNUC__) && (__GNUC__ >= 2) && defined (__OPTIMIZE__)
|
||||
# define _G_NEW(struct_type, n_structs, func) \
|
||||
(struct_type *) (G_GNUC_EXTENSION ({ \
|
||||
gsize __n = (gsize) (n_structs); \
|
||||
gsize __s = sizeof (struct_type); \
|
||||
gpointer __p; \
|
||||
if (__s == 1) \
|
||||
__p = g_##func (__n); \
|
||||
else if (__builtin_constant_p (__n) && \
|
||||
(__s == 0 || __n <= G_MAXSIZE / __s)) \
|
||||
__p = g_##func (__n * __s); \
|
||||
else \
|
||||
__p = g_##func##_n (__n, __s); \
|
||||
__p; \
|
||||
}))
|
||||
# define _G_RENEW(struct_type, mem, n_structs, func) \
|
||||
(struct_type *) (G_GNUC_EXTENSION ({ \
|
||||
gsize __n = (gsize) (n_structs); \
|
||||
gsize __s = sizeof (struct_type); \
|
||||
gpointer __p = (gpointer) (mem); \
|
||||
if (__s == 1) \
|
||||
__p = g_##func (__p, __n); \
|
||||
else if (__builtin_constant_p (__n) && \
|
||||
(__s == 0 || __n <= G_MAXSIZE / __s)) \
|
||||
__p = g_##func (__p, __n * __s); \
|
||||
else \
|
||||
__p = g_##func##_n (__p, __n, __s); \
|
||||
__p; \
|
||||
}))
|
||||
|
||||
#else
|
||||
/* Unoptimised version: always call the _n() function. */
|
||||
#define _G_NEW(struct_type, n_structs, func) \
|
||||
((struct_type *) g_##func##_n ((n_structs), sizeof (struct_type)))
|
||||
#define _G_RENEW(struct_type, mem, n_structs, func) \
|
||||
((struct_type *) g_##func##_n (mem, (n_structs), sizeof (struct_type)))
|
||||
|
||||
#endif
|
||||
|
||||
gpointer g_try_malloc (gsize n_bytes);
|
||||
|
||||
gpointer g_try_malloc0 (gsize n_bytes);
|
||||
|
||||
gpointer g_try_malloc_n (gsize n_blocks, gsize n_block_bytes);
|
||||
|
||||
gpointer g_malloc0_n (gsize n_blocks, gsize n_block_bytes);
|
||||
|
||||
gpointer g_realloc_n (gpointer mem, gsize n_blocks, gsize n_block_bytes);
|
||||
|
||||
gpointer g_malloc_n (gsize n_blocks, gsize n_block_bytes);
|
||||
|
||||
gpointer g_malloc0 (gsize n_bytes);
|
||||
|
||||
gpointer g_malloc (gsize n_bytes);
|
||||
|
||||
void g_free (gpointer mem);
|
||||
|
||||
/**
|
||||
* g_try_new:
|
||||
* @struct_type: the type of the elements to allocate
|
||||
* @n_structs: the number of elements to allocate
|
||||
*
|
||||
* Attempts to allocate @n_structs elements of type @struct_type, and returns
|
||||
* %NULL on failure. Contrast with g_new(), which aborts the program on failure.
|
||||
* The returned pointer is cast to a pointer to the given type.
|
||||
* The function returns %NULL when @n_structs is 0 of if an overflow occurs.
|
||||
*
|
||||
* Since: 2.8
|
||||
* Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
|
||||
*/
|
||||
#define g_try_new(struct_type, n_structs) _G_NEW (struct_type, n_structs, try_malloc)
|
||||
#define g_new0(struct_type, n_structs) _G_NEW (struct_type, n_structs, malloc0)
|
||||
#define g_new(struct_type, n_structs) _G_NEW (struct_type, n_structs, malloc)
|
||||
#define g_renew(struct_type, mem, n_structs) _G_RENEW (struct_type, mem, n_structs, realloc)
|
||||
|
||||
#endif /* __G_MEM_H__ */
|
||||
35
glib_compat/gmessages.h
Normal file
35
glib_compat/gmessages.h
Normal file
@@ -0,0 +1,35 @@
|
||||
/* GLIB - Library of useful routines for C programming
|
||||
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Modified by the GLib Team and others 1997-2000. See the AUTHORS
|
||||
* file for a list of people on the GLib Team. See the ChangeLog
|
||||
* files for a list of changes. These files are distributed with
|
||||
* GLib at ftp://ftp.gtk.org/pub/gtk/.
|
||||
*/
|
||||
|
||||
#ifndef __G_MESSAGES_H__
|
||||
#define __G_MESSAGES_H__
|
||||
|
||||
#include "gmacros.h"
|
||||
|
||||
#define g_return_val_if_fail(expr,val) G_STMT_START{ (void)0; }G_STMT_END
|
||||
#define g_return_if_fail(expr) G_STMT_START{ (void)0; }G_STMT_END
|
||||
#define g_return_if_reached() G_STMT_START{ return; }G_STMT_END
|
||||
#define g_return_val_if_reached(val) G_STMT_START{ return (val); }G_STMT_END
|
||||
|
||||
#endif /* __G_MESSAGES_H__ */
|
||||
39
glib_compat/gnode.h
Normal file
39
glib_compat/gnode.h
Normal file
@@ -0,0 +1,39 @@
|
||||
/* GLIB - Library of useful routines for C programming
|
||||
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Modified by the GLib Team and others 1997-2000. See the AUTHORS
|
||||
* file for a list of people on the GLib Team. See the ChangeLog
|
||||
* files for a list of changes. These files are distributed with
|
||||
* GLib at ftp://ftp.gtk.org/pub/gtk/.
|
||||
*/
|
||||
|
||||
#ifndef __G_NODE_H__
|
||||
#define __G_NODE_H__
|
||||
|
||||
#include "gmem.h"
|
||||
|
||||
/* Tree traverse orders */
|
||||
typedef enum
|
||||
{
|
||||
G_IN_ORDER,
|
||||
G_PRE_ORDER,
|
||||
G_POST_ORDER,
|
||||
G_LEVEL_ORDER
|
||||
} GTraverseType;
|
||||
|
||||
#endif /* __G_NODE_H__ */
|
||||
400
glib_compat/gpattern.c
Normal file
400
glib_compat/gpattern.c
Normal file
@@ -0,0 +1,400 @@
|
||||
/* GLIB - Library of useful routines for C programming
|
||||
* Copyright (C) 1995-1997, 1999 Peter Mattis, Red Hat, Inc.
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "gpattern.h"
|
||||
|
||||
#include "gmacros.h"
|
||||
#include "gmessages.h"
|
||||
#include "gmem.h"
|
||||
|
||||
/**
|
||||
* SECTION:patterns
|
||||
* @title: Glob-style pattern matching
|
||||
* @short_description: matches strings against patterns containing '*'
|
||||
* (wildcard) and '?' (joker)
|
||||
*
|
||||
* The g_pattern_match* functions match a string
|
||||
* against a pattern containing '*' and '?' wildcards with similar
|
||||
* semantics as the standard glob() function: '*' matches an arbitrary,
|
||||
* possibly empty, string, '?' matches an arbitrary character.
|
||||
*
|
||||
* Note that in contrast to glob(), the '/' character can be matched by
|
||||
* the wildcards, there are no '[...]' character ranges and '*' and '?'
|
||||
* can not be escaped to include them literally in a pattern.
|
||||
*
|
||||
* When multiple strings must be matched against the same pattern, it
|
||||
* is better to compile the pattern to a #GPatternSpec using
|
||||
* g_pattern_spec_new() and use g_pattern_match_string() instead of
|
||||
* g_pattern_match_simple(). This avoids the overhead of repeated
|
||||
* pattern compilation.
|
||||
**/
|
||||
|
||||
/**
|
||||
* GPatternSpec:
|
||||
*
|
||||
* A GPatternSpec struct is the 'compiled' form of a pattern. This
|
||||
* structure is opaque and its fields cannot be accessed directly.
|
||||
*/
|
||||
|
||||
/* keep enum and structure of gpattern.c and patterntest.c in sync */
|
||||
typedef enum
|
||||
{
|
||||
G_MATCH_ALL, /* "*A?A*" */
|
||||
G_MATCH_ALL_TAIL, /* "*A?AA" */
|
||||
G_MATCH_HEAD, /* "AAAA*" */
|
||||
G_MATCH_TAIL, /* "*AAAA" */
|
||||
G_MATCH_EXACT, /* "AAAAA" */
|
||||
G_MATCH_LAST
|
||||
} GMatchType;
|
||||
|
||||
struct _GPatternSpec
|
||||
{
|
||||
GMatchType match_type;
|
||||
guint pattern_length;
|
||||
guint min_length;
|
||||
guint max_length;
|
||||
gchar *pattern;
|
||||
};
|
||||
|
||||
|
||||
/* --- functions --- */
|
||||
static inline gboolean g_pattern_ph_match (const gchar *match_pattern,
|
||||
const gchar *match_string,
|
||||
gboolean *wildcard_reached_p)
|
||||
{
|
||||
const gchar *pattern, *string;
|
||||
gchar ch;
|
||||
|
||||
pattern = match_pattern;
|
||||
string = match_string;
|
||||
|
||||
ch = *pattern;
|
||||
pattern++;
|
||||
while (ch)
|
||||
{
|
||||
switch (ch)
|
||||
{
|
||||
case '?':
|
||||
if (!*string)
|
||||
return FALSE;
|
||||
string = string + 1;
|
||||
break;
|
||||
|
||||
case '*':
|
||||
*wildcard_reached_p = TRUE;
|
||||
do
|
||||
{
|
||||
ch = *pattern;
|
||||
pattern++;
|
||||
if (ch == '?')
|
||||
{
|
||||
if (!*string)
|
||||
return FALSE;
|
||||
string = string + 1;
|
||||
}
|
||||
}
|
||||
while (ch == '*' || ch == '?');
|
||||
if (!ch)
|
||||
return TRUE;
|
||||
do
|
||||
{
|
||||
gboolean next_wildcard_reached = FALSE;
|
||||
while (ch != *string)
|
||||
{
|
||||
if (!*string)
|
||||
return FALSE;
|
||||
string = string + 1;
|
||||
}
|
||||
string++;
|
||||
if (g_pattern_ph_match (pattern, string, &next_wildcard_reached))
|
||||
return TRUE;
|
||||
if (next_wildcard_reached)
|
||||
/* the forthcoming pattern substring up to the next wildcard has
|
||||
* been matched, but a mismatch occurred for the rest of the
|
||||
* pattern, following the next wildcard.
|
||||
* there's no need to advance the current match position any
|
||||
* further if the rest pattern will not match.
|
||||
*/
|
||||
return FALSE;
|
||||
}
|
||||
while (*string);
|
||||
break;
|
||||
|
||||
default:
|
||||
if (ch == *string)
|
||||
string++;
|
||||
else
|
||||
return FALSE;
|
||||
break;
|
||||
}
|
||||
|
||||
ch = *pattern;
|
||||
pattern++;
|
||||
}
|
||||
|
||||
return *string == 0;
|
||||
}
|
||||
|
||||
static gchar *string_reverse(const gchar *string, gint string_length)
|
||||
{
|
||||
gchar *new_string;
|
||||
gint i, j;
|
||||
if (string == NULL || string_length <= 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
new_string = g_new(gchar, string_length + 1);
|
||||
if (new_string) {
|
||||
for (i = 0; i < string_length; i++) {
|
||||
j = string_length - i - 1;
|
||||
new_string[j] = string[i];
|
||||
}
|
||||
new_string[string_length] = 0;
|
||||
}
|
||||
|
||||
return new_string;
|
||||
}
|
||||
|
||||
/**
|
||||
* g_pattern_match:
|
||||
* @pspec: a #GPatternSpec
|
||||
* @string_length: the length of @string (in bytes, i.e. strlen(),
|
||||
* not g_utf8_strlen())
|
||||
* @string: the UTF-8 encoded string to match
|
||||
* @string_reversed: (nullable): the reverse of @string or %NULL
|
||||
*
|
||||
* Matches a string against a compiled pattern. Passing the correct
|
||||
* length of the string given is mandatory. The reversed string can be
|
||||
* omitted by passing %NULL, this is more efficient if the reversed
|
||||
* version of the string to be matched is not at hand, as
|
||||
* g_pattern_match() will only construct it if the compiled pattern
|
||||
* requires reverse matches.
|
||||
*
|
||||
* Note that, if the user code will (possibly) match a string against a
|
||||
* multitude of patterns containing wildcards, chances are high that
|
||||
* some patterns will require a reversed string. In this case, it's
|
||||
* more efficient to provide the reversed string to avoid multiple
|
||||
* constructions thereof in the various calls to g_pattern_match().
|
||||
*
|
||||
* Note also that the reverse of a UTF-8 encoded string can in general
|
||||
* not be obtained by g_strreverse(). This works only if the string
|
||||
* does not contain any multibyte characters. GLib offers the
|
||||
* g_utf8_strreverse() function to reverse UTF-8 encoded strings.
|
||||
*
|
||||
* Returns: %TRUE if @string matches @pspec
|
||||
**/
|
||||
gboolean g_pattern_match (GPatternSpec *pspec,
|
||||
guint string_length,
|
||||
const gchar *string,
|
||||
const gchar *string_reversed)
|
||||
{
|
||||
g_return_val_if_fail (pspec != NULL, FALSE);
|
||||
g_return_val_if_fail (string != NULL, FALSE);
|
||||
|
||||
if (string_length < pspec->min_length ||
|
||||
string_length > pspec->max_length)
|
||||
return FALSE;
|
||||
|
||||
switch (pspec->match_type)
|
||||
{
|
||||
gboolean dummy;
|
||||
case G_MATCH_ALL:
|
||||
return g_pattern_ph_match (pspec->pattern, string, &dummy);
|
||||
case G_MATCH_ALL_TAIL:
|
||||
if (string_reversed)
|
||||
return g_pattern_ph_match (pspec->pattern, string_reversed, &dummy);
|
||||
else
|
||||
{
|
||||
gboolean result;
|
||||
gchar *tmp;
|
||||
tmp = string_reverse (string, string_length);
|
||||
result = g_pattern_ph_match (pspec->pattern, tmp, &dummy);
|
||||
g_free (tmp);
|
||||
return result;
|
||||
}
|
||||
case G_MATCH_HEAD:
|
||||
if (pspec->pattern_length == string_length)
|
||||
return strcmp (pspec->pattern, string) == 0;
|
||||
else if (pspec->pattern_length)
|
||||
return strncmp (pspec->pattern, string, pspec->pattern_length) == 0;
|
||||
else
|
||||
return TRUE;
|
||||
case G_MATCH_TAIL:
|
||||
if (pspec->pattern_length)
|
||||
return strcmp (pspec->pattern, string + (string_length - pspec->pattern_length)) == 0;
|
||||
else
|
||||
return TRUE;
|
||||
case G_MATCH_EXACT:
|
||||
if (pspec->pattern_length != string_length)
|
||||
return FALSE;
|
||||
else
|
||||
return strcmp (pspec->pattern, string) == 0;
|
||||
default:
|
||||
g_return_val_if_fail (pspec->match_type < G_MATCH_LAST, FALSE);
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* g_pattern_spec_new:
|
||||
* @pattern: a zero-terminated UTF-8 encoded string
|
||||
*
|
||||
* Compiles a pattern to a #GPatternSpec.
|
||||
*
|
||||
* Returns: a newly-allocated #GPatternSpec
|
||||
**/
|
||||
GPatternSpec* g_pattern_spec_new (const gchar *pattern)
|
||||
{
|
||||
GPatternSpec *pspec;
|
||||
gboolean seen_joker = FALSE, seen_wildcard = FALSE, more_wildcards = FALSE;
|
||||
gint hw_pos = -1, tw_pos = -1, hj_pos = -1, tj_pos = -1;
|
||||
gboolean follows_wildcard = FALSE;
|
||||
guint pending_jokers = 0;
|
||||
const gchar *s;
|
||||
gchar *d;
|
||||
guint i;
|
||||
|
||||
g_return_val_if_fail (pattern != NULL, NULL);
|
||||
|
||||
/* canonicalize pattern and collect necessary stats */
|
||||
pspec = g_new (GPatternSpec, 1);
|
||||
pspec->pattern_length = strlen (pattern);
|
||||
pspec->min_length = 0;
|
||||
pspec->max_length = 0;
|
||||
pspec->pattern = g_new (gchar, pspec->pattern_length + 1);
|
||||
d = pspec->pattern;
|
||||
for (i = 0, s = pattern; *s != 0; s++)
|
||||
{
|
||||
switch (*s)
|
||||
{
|
||||
case '*':
|
||||
if (follows_wildcard) /* compress multiple wildcards */
|
||||
{
|
||||
pspec->pattern_length--;
|
||||
continue;
|
||||
}
|
||||
follows_wildcard = TRUE;
|
||||
if (hw_pos < 0)
|
||||
hw_pos = i;
|
||||
tw_pos = i;
|
||||
break;
|
||||
case '?':
|
||||
pending_jokers++;
|
||||
pspec->min_length++;
|
||||
pspec->max_length += 4; /* maximum UTF-8 character length */
|
||||
continue;
|
||||
default:
|
||||
for (; pending_jokers; pending_jokers--, i++) {
|
||||
*d++ = '?';
|
||||
if (hj_pos < 0)
|
||||
hj_pos = i;
|
||||
tj_pos = i;
|
||||
}
|
||||
follows_wildcard = FALSE;
|
||||
pspec->min_length++;
|
||||
pspec->max_length++;
|
||||
break;
|
||||
}
|
||||
*d++ = *s;
|
||||
i++;
|
||||
}
|
||||
for (; pending_jokers; pending_jokers--) {
|
||||
*d++ = '?';
|
||||
if (hj_pos < 0)
|
||||
hj_pos = i;
|
||||
tj_pos = i;
|
||||
}
|
||||
*d++ = 0;
|
||||
seen_joker = hj_pos >= 0;
|
||||
seen_wildcard = hw_pos >= 0;
|
||||
more_wildcards = seen_wildcard && hw_pos != tw_pos;
|
||||
if (seen_wildcard)
|
||||
pspec->max_length = UINT_MAX;
|
||||
|
||||
/* special case sole head/tail wildcard or exact matches */
|
||||
if (!seen_joker && !more_wildcards)
|
||||
{
|
||||
if (pspec->pattern[0] == '*')
|
||||
{
|
||||
pspec->match_type = G_MATCH_TAIL;
|
||||
memmove (pspec->pattern, pspec->pattern + 1, --pspec->pattern_length);
|
||||
pspec->pattern[pspec->pattern_length] = 0;
|
||||
return pspec;
|
||||
}
|
||||
if (pspec->pattern_length > 0 &&
|
||||
pspec->pattern[pspec->pattern_length - 1] == '*')
|
||||
{
|
||||
pspec->match_type = G_MATCH_HEAD;
|
||||
pspec->pattern[--pspec->pattern_length] = 0;
|
||||
return pspec;
|
||||
}
|
||||
if (!seen_wildcard)
|
||||
{
|
||||
pspec->match_type = G_MATCH_EXACT;
|
||||
return pspec;
|
||||
}
|
||||
}
|
||||
|
||||
/* now just need to distinguish between head or tail match start */
|
||||
tw_pos = pspec->pattern_length - 1 - tw_pos; /* last pos to tail distance */
|
||||
tj_pos = pspec->pattern_length - 1 - tj_pos; /* last pos to tail distance */
|
||||
if (seen_wildcard)
|
||||
pspec->match_type = tw_pos > hw_pos ? G_MATCH_ALL_TAIL : G_MATCH_ALL;
|
||||
else /* seen_joker */
|
||||
pspec->match_type = tj_pos > hj_pos ? G_MATCH_ALL_TAIL : G_MATCH_ALL;
|
||||
if (pspec->match_type == G_MATCH_ALL_TAIL) {
|
||||
gchar *tmp = pspec->pattern;
|
||||
pspec->pattern = string_reverse (pspec->pattern, pspec->pattern_length);
|
||||
g_free (tmp);
|
||||
}
|
||||
return pspec;
|
||||
}
|
||||
|
||||
/**
|
||||
* g_pattern_spec_free:
|
||||
* @pspec: a #GPatternSpec
|
||||
*
|
||||
* Frees the memory allocated for the #GPatternSpec.
|
||||
**/
|
||||
void g_pattern_spec_free (GPatternSpec *pspec)
|
||||
{
|
||||
g_return_if_fail (pspec != NULL);
|
||||
|
||||
g_free (pspec->pattern);
|
||||
g_free (pspec);
|
||||
}
|
||||
|
||||
/**
|
||||
* g_pattern_match_string:
|
||||
* @pspec: a #GPatternSpec
|
||||
* @string: the UTF-8 encoded string to match
|
||||
*
|
||||
* Matches a string against a compiled pattern. If the string is to be
|
||||
* matched against more than one pattern, consider using
|
||||
* g_pattern_match() instead while supplying the reversed string.
|
||||
*
|
||||
* Returns: %TRUE if @string matches @pspec
|
||||
**/
|
||||
gboolean g_pattern_match_string (GPatternSpec *pspec, const gchar *string)
|
||||
{
|
||||
g_return_val_if_fail (pspec != NULL, FALSE);
|
||||
g_return_val_if_fail (string != NULL, FALSE);
|
||||
|
||||
return g_pattern_match (pspec, strlen (string), string, NULL);
|
||||
}
|
||||
34
glib_compat/gpattern.h
Normal file
34
glib_compat/gpattern.h
Normal file
@@ -0,0 +1,34 @@
|
||||
/* GLIB - Library of useful routines for C programming
|
||||
* Copyright (C) 1995-1997, 1999 Peter Mattis, Red Hat, Inc.
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#ifndef __G_PATTERN_H__
|
||||
#define __G_PATTERN_H__
|
||||
|
||||
#include "gtypes.h"
|
||||
|
||||
typedef struct _GPatternSpec GPatternSpec;
|
||||
|
||||
GPatternSpec* g_pattern_spec_new (const gchar *pattern);
|
||||
void g_pattern_spec_free (GPatternSpec *pspec);
|
||||
gboolean g_pattern_match (GPatternSpec *pspec,
|
||||
guint string_length,
|
||||
const gchar *string,
|
||||
const gchar *string_reversed);
|
||||
gboolean g_pattern_match_string (GPatternSpec *pspec,
|
||||
const gchar *string);
|
||||
|
||||
#endif /* __G_PATTERN_H__ */
|
||||
384
glib_compat/grand.c
Normal file
384
glib_compat/grand.c
Normal file
@@ -0,0 +1,384 @@
|
||||
/* GLIB - Library of useful routines for C programming
|
||||
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
/* Originally developed and coded by Makoto Matsumoto and Takuji
|
||||
* Nishimura. Please mail <matumoto@math.keio.ac.jp>, if you're using
|
||||
* code from this file in your own programs or libraries.
|
||||
* Further information on the Mersenne Twister can be found at
|
||||
* http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
|
||||
* This code was adapted to glib by Sebastian Wilhelmi.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Modified by the GLib Team and others 1997-2000. See the AUTHORS
|
||||
* file for a list of people on the GLib Team. See the ChangeLog
|
||||
* files for a list of changes. These files are distributed with
|
||||
* GLib at ftp://ftp.gtk.org/pub/gtk/.
|
||||
*/
|
||||
|
||||
/*
|
||||
* MT safe
|
||||
*/
|
||||
|
||||
#define _CRT_RAND_S
|
||||
|
||||
#include <math.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#ifndef _MSC_VER
|
||||
#include <unistd.h>
|
||||
#include <sys/time.h>
|
||||
#else
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
#include "grand.h"
|
||||
#include "gmem.h"
|
||||
#include "gmessages.h"
|
||||
|
||||
#define G_USEC_PER_SEC 1000000
|
||||
|
||||
#if defined(__MINGW64_VERSION_MAJOR) || defined(_WIN32)
|
||||
errno_t rand_s(unsigned int* randomValue);
|
||||
#endif
|
||||
|
||||
#define G_GINT64_CONSTANT(val) (val##L)
|
||||
|
||||
/* Period parameters */
|
||||
#define N 624
|
||||
#define M 397
|
||||
#define MATRIX_A 0x9908b0df /* constant vector a */
|
||||
#define UPPER_MASK 0x80000000 /* most significant w-r bits */
|
||||
#define LOWER_MASK 0x7fffffff /* least significant r bits */
|
||||
|
||||
/* Tempering parameters */
|
||||
#define TEMPERING_MASK_B 0x9d2c5680
|
||||
#define TEMPERING_MASK_C 0xefc60000
|
||||
#define TEMPERING_SHIFT_U(y) (y >> 11)
|
||||
#define TEMPERING_SHIFT_S(y) (y << 7)
|
||||
#define TEMPERING_SHIFT_T(y) (y << 15)
|
||||
#define TEMPERING_SHIFT_L(y) (y >> 18)
|
||||
|
||||
struct _GRand
|
||||
{
|
||||
guint32 mt[N]; /* the array for the state vector */
|
||||
guint mti;
|
||||
};
|
||||
|
||||
static guint get_random_version (void)
|
||||
{
|
||||
static gsize initialized = FALSE;
|
||||
static guint random_version;
|
||||
|
||||
if (!initialized)
|
||||
{
|
||||
// g_warning ("Unknown G_RANDOM_VERSION \"%s\". Using version 2.2.", version_string);
|
||||
random_version = 22;
|
||||
initialized = TRUE;
|
||||
}
|
||||
|
||||
return random_version;
|
||||
}
|
||||
|
||||
/**
|
||||
* g_rand_set_seed:
|
||||
* @rand_: a #GRand
|
||||
* @seed: a value to reinitialize the random number generator
|
||||
*
|
||||
* Sets the seed for the random number generator #GRand to @seed.
|
||||
*/
|
||||
void g_rand_set_seed (GRand *rand, guint32 seed)
|
||||
{
|
||||
g_return_if_fail (rand != NULL);
|
||||
|
||||
switch (get_random_version ())
|
||||
{
|
||||
case 20:
|
||||
/* setting initial seeds to mt[N] using */
|
||||
/* the generator Line 25 of Table 1 in */
|
||||
/* [KNUTH 1981, The Art of Computer Programming */
|
||||
/* Vol. 2 (2nd Ed.), pp102] */
|
||||
|
||||
if (seed == 0) /* This would make the PRNG produce only zeros */
|
||||
seed = 0x6b842128; /* Just set it to another number */
|
||||
|
||||
rand->mt[0]= seed;
|
||||
for (rand->mti=1; rand->mti<N; rand->mti++)
|
||||
rand->mt[rand->mti] = (69069 * rand->mt[rand->mti-1]);
|
||||
|
||||
break;
|
||||
case 22:
|
||||
/* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
|
||||
/* In the previous version (see above), MSBs of the */
|
||||
/* seed affect only MSBs of the array mt[]. */
|
||||
|
||||
rand->mt[0]= seed;
|
||||
for (rand->mti=1; rand->mti<N; rand->mti++)
|
||||
rand->mt[rand->mti] = 1812433253UL *
|
||||
(rand->mt[rand->mti-1] ^ (rand->mt[rand->mti-1] >> 30)) + rand->mti;
|
||||
break;
|
||||
default:
|
||||
// g_assert_not_reached ();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* g_rand_new_with_seed:
|
||||
* @seed: a value to initialize the random number generator
|
||||
*
|
||||
* Creates a new random number generator initialized with @seed.
|
||||
*
|
||||
* Returns: the new #GRand
|
||||
**/
|
||||
GRand* g_rand_new_with_seed (guint32 seed)
|
||||
{
|
||||
GRand *rand = g_new0 (GRand, 1);
|
||||
g_rand_set_seed (rand, seed);
|
||||
return rand;
|
||||
}
|
||||
|
||||
/**
|
||||
* g_rand_set_seed_array:
|
||||
* @rand_: a #GRand
|
||||
* @seed: array to initialize with
|
||||
* @seed_length: length of array
|
||||
*
|
||||
* Initializes the random number generator by an array of longs.
|
||||
* Array can be of arbitrary size, though only the first 624 values
|
||||
* are taken. This function is useful if you have many low entropy
|
||||
* seeds, or if you require more then 32 bits of actual entropy for
|
||||
* your application.
|
||||
*
|
||||
* Since: 2.4
|
||||
*/
|
||||
void g_rand_set_seed_array (GRand *rand, const guint32 *seed, guint seed_length)
|
||||
{
|
||||
guint i, j, k;
|
||||
|
||||
g_return_if_fail (rand != NULL);
|
||||
g_return_if_fail (seed_length >= 1);
|
||||
|
||||
g_rand_set_seed (rand, 19650218UL);
|
||||
|
||||
i=1; j=0;
|
||||
k = (N>seed_length ? N : seed_length);
|
||||
for (; k; k--)
|
||||
{
|
||||
rand->mt[i] = (rand->mt[i] ^
|
||||
((rand->mt[i-1] ^ (rand->mt[i-1] >> 30)) * 1664525UL))
|
||||
+ seed[j] + j; /* non linear */
|
||||
rand->mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
|
||||
i++; j++;
|
||||
if (i>=N)
|
||||
{
|
||||
rand->mt[0] = rand->mt[N-1];
|
||||
i=1;
|
||||
}
|
||||
if (j>=seed_length)
|
||||
j=0;
|
||||
}
|
||||
for (k=N-1; k; k--)
|
||||
{
|
||||
rand->mt[i] = (rand->mt[i] ^
|
||||
((rand->mt[i-1] ^ (rand->mt[i-1] >> 30)) * 1566083941UL))
|
||||
- i; /* non linear */
|
||||
rand->mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
|
||||
i++;
|
||||
if (i>=N)
|
||||
{
|
||||
rand->mt[0] = rand->mt[N-1];
|
||||
i=1;
|
||||
}
|
||||
}
|
||||
|
||||
rand->mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */
|
||||
}
|
||||
|
||||
/**
|
||||
* g_rand_new_with_seed_array:
|
||||
* @seed: an array of seeds to initialize the random number generator
|
||||
* @seed_length: an array of seeds to initialize the random number
|
||||
* generator
|
||||
*
|
||||
* Creates a new random number generator initialized with @seed.
|
||||
*
|
||||
* Returns: the new #GRand
|
||||
*
|
||||
* Since: 2.4
|
||||
*/
|
||||
GRand *g_rand_new_with_seed_array (const guint32 *seed, guint seed_length)
|
||||
{
|
||||
GRand *rand = g_new0 (GRand, 1);
|
||||
g_rand_set_seed_array (rand, seed, seed_length);
|
||||
return rand;
|
||||
}
|
||||
|
||||
gint64 g_get_real_time (void)
|
||||
{
|
||||
#if defined(unix) || defined(__unix__) || defined(__unix) || defined (__MINGW32__) || defined(__APPLE__)
|
||||
struct timeval r;
|
||||
|
||||
/* this is required on alpha, there the timeval structs are ints
|
||||
* not longs and a cast only would fail horribly */
|
||||
gettimeofday (&r, NULL);
|
||||
|
||||
return (((gint64) r.tv_sec) * 1000000) + r.tv_usec;
|
||||
#else
|
||||
FILETIME ft;
|
||||
guint64 time64;
|
||||
|
||||
GetSystemTimeAsFileTime (&ft);
|
||||
memmove (&time64, &ft, sizeof (FILETIME));
|
||||
|
||||
/* Convert from 100s of nanoseconds since 1601-01-01
|
||||
* to Unix epoch. This is Y2038 safe.
|
||||
*/
|
||||
time64 -= G_GINT64_CONSTANT (116444736000000000);
|
||||
time64 /= 10;
|
||||
|
||||
return time64;
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* g_rand_new:
|
||||
*
|
||||
* Creates a new random number generator initialized with a seed taken
|
||||
* either from `/dev/urandom` (if existing) or from the current time
|
||||
* (as a fallback).
|
||||
*
|
||||
* On Windows, the seed is taken from rand_s().
|
||||
*
|
||||
* Returns: the new #GRand
|
||||
*/
|
||||
GRand *g_rand_new (void)
|
||||
{
|
||||
guint32 seed[4];
|
||||
#if defined(unix) || defined(__unix__) || defined(__unix) || defined(__APPLE__)
|
||||
static gboolean dev_urandom_exists = TRUE;
|
||||
|
||||
if (dev_urandom_exists)
|
||||
{
|
||||
FILE* dev_urandom;
|
||||
|
||||
do
|
||||
{
|
||||
dev_urandom = fopen("/dev/urandom", "rb");
|
||||
}
|
||||
while (dev_urandom == NULL && errno == EINTR);
|
||||
|
||||
if (dev_urandom)
|
||||
{
|
||||
int r;
|
||||
|
||||
setvbuf (dev_urandom, NULL, _IONBF, 0);
|
||||
do
|
||||
{
|
||||
errno = 0;
|
||||
r = fread (seed, sizeof (seed), 1, dev_urandom);
|
||||
}
|
||||
while (errno == EINTR);
|
||||
|
||||
if (r != 1)
|
||||
dev_urandom_exists = FALSE;
|
||||
|
||||
fclose (dev_urandom);
|
||||
}
|
||||
else
|
||||
dev_urandom_exists = FALSE;
|
||||
}
|
||||
|
||||
if (!dev_urandom_exists)
|
||||
{
|
||||
gint64 now_us = g_get_real_time ();
|
||||
seed[0] = now_us / G_USEC_PER_SEC;
|
||||
seed[1] = now_us % G_USEC_PER_SEC;
|
||||
seed[2] = getpid ();
|
||||
seed[3] = getppid ();
|
||||
}
|
||||
#else /* G_OS_WIN32 */
|
||||
/* rand_s() is only available since Visual Studio 2005 and
|
||||
* MinGW-w64 has a wrapper that will emulate rand_s() if it's not in msvcrt
|
||||
*/
|
||||
#if (defined(_MSC_VER) && _MSC_VER >= 1400) || defined(__MINGW64_VERSION_MAJOR)
|
||||
gint i;
|
||||
|
||||
for (i = 0; i < 4;/* array size of seed */ i++) {
|
||||
rand_s(&seed[i]);
|
||||
}
|
||||
#else
|
||||
#warning Using insecure seed for random number generation because of missing rand_s() in Windows XP
|
||||
GTimeVal now;
|
||||
|
||||
g_get_current_time (&now);
|
||||
seed[0] = now.tv_sec;
|
||||
seed[1] = now.tv_usec;
|
||||
seed[2] = getpid ();
|
||||
seed[3] = 0;
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
return g_rand_new_with_seed_array (seed, 4);
|
||||
}
|
||||
|
||||
/**
|
||||
* g_rand_int:
|
||||
* @rand_: a #GRand
|
||||
*
|
||||
* Returns the next random #guint32 from @rand_ equally distributed over
|
||||
* the range [0..2^32-1].
|
||||
*
|
||||
* Returns: a random number
|
||||
*/
|
||||
guint32 g_rand_int (GRand *rand)
|
||||
{
|
||||
guint32 y;
|
||||
static const guint32 mag01[2]={0x0, MATRIX_A};
|
||||
/* mag01[x] = x * MATRIX_A for x=0,1 */
|
||||
|
||||
g_return_val_if_fail (rand != NULL, 0);
|
||||
|
||||
if (rand->mti >= N) { /* generate N words at one time */
|
||||
int kk;
|
||||
|
||||
for (kk = 0; kk < N - M; kk++) {
|
||||
y = (rand->mt[kk]&UPPER_MASK)|(rand->mt[kk+1]&LOWER_MASK);
|
||||
rand->mt[kk] = rand->mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1];
|
||||
}
|
||||
for (; kk < N - 1; kk++) {
|
||||
y = (rand->mt[kk]&UPPER_MASK)|(rand->mt[kk+1]&LOWER_MASK);
|
||||
rand->mt[kk] = rand->mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1];
|
||||
}
|
||||
y = (rand->mt[N-1]&UPPER_MASK)|(rand->mt[0]&LOWER_MASK);
|
||||
rand->mt[N-1] = rand->mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1];
|
||||
|
||||
rand->mti = 0;
|
||||
}
|
||||
|
||||
y = rand->mt[rand->mti++];
|
||||
y ^= TEMPERING_SHIFT_U(y);
|
||||
y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B;
|
||||
y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C;
|
||||
y ^= TEMPERING_SHIFT_L(y);
|
||||
|
||||
return y;
|
||||
}
|
||||
|
||||
37
glib_compat/grand.h
Normal file
37
glib_compat/grand.h
Normal file
@@ -0,0 +1,37 @@
|
||||
/* GLIB - Library of useful routines for C programming
|
||||
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Modified by the GLib Team and others 1997-2000. See the AUTHORS
|
||||
* file for a list of people on the GLib Team. See the ChangeLog
|
||||
* files for a list of changes. These files are distributed with
|
||||
* GLib at ftp://ftp.gtk.org/pub/gtk/.
|
||||
*/
|
||||
|
||||
#ifndef __G_RAND_H__
|
||||
#define __G_RAND_H__
|
||||
|
||||
#include "gtypes.h"
|
||||
|
||||
typedef struct _GRand GRand;
|
||||
|
||||
GRand *g_rand_new_with_seed(guint32 seed);
|
||||
GRand *g_rand_new_with_seed_array (const guint32 *seed, guint seed_length);
|
||||
GRand *g_rand_new(void);
|
||||
guint32 g_rand_int(GRand *rand_);
|
||||
|
||||
#endif /* __G_RAND_H__ */
|
||||
91
glib_compat/gslice.c
Normal file
91
glib_compat/gslice.c
Normal file
@@ -0,0 +1,91 @@
|
||||
/* GLIB sliced memory - fast concurrent memory chunk allocator
|
||||
* Copyright (C) 2005 Tim Janik
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
/* MT safe */
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "gtypes.h"
|
||||
#include "gslice.h"
|
||||
#include "gmem.h" /* gslice.h */
|
||||
|
||||
/**
|
||||
* g_slice_alloc:
|
||||
* @block_size: the number of bytes to allocate
|
||||
*
|
||||
* Allocates a block of memory from the slice allocator.
|
||||
* The block address handed out can be expected to be aligned
|
||||
* to at least 1 * sizeof (void*),
|
||||
* though in general slices are 2 * sizeof (void*) bytes aligned,
|
||||
* if a malloc() fallback implementation is used instead,
|
||||
* the alignment may be reduced in a libc dependent fashion.
|
||||
* Note that the underlying slice allocation mechanism can
|
||||
* be changed with the [`G_SLICE=always-malloc`][G_SLICE]
|
||||
* environment variable.
|
||||
*
|
||||
* Returns: a pointer to the allocated memory block, which will be %NULL if and
|
||||
* only if @mem_size is 0
|
||||
*
|
||||
* Since: 2.10
|
||||
*/
|
||||
gpointer g_slice_alloc (gsize mem_size)
|
||||
{
|
||||
return g_malloc (mem_size);
|
||||
}
|
||||
|
||||
/**
|
||||
* g_slice_alloc0:
|
||||
* @block_size: the number of bytes to allocate
|
||||
*
|
||||
* Allocates a block of memory via g_slice_alloc() and initializes
|
||||
* the returned memory to 0. Note that the underlying slice allocation
|
||||
* mechanism can be changed with the [`G_SLICE=always-malloc`][G_SLICE]
|
||||
* environment variable.
|
||||
*
|
||||
* Returns: a pointer to the allocated block, which will be %NULL if and only
|
||||
* if @mem_size is 0
|
||||
*
|
||||
* Since: 2.10
|
||||
*/
|
||||
gpointer g_slice_alloc0 (gsize mem_size)
|
||||
{
|
||||
gpointer mem = g_slice_alloc (mem_size);
|
||||
if (mem)
|
||||
memset (mem, 0, mem_size);
|
||||
return mem;
|
||||
}
|
||||
|
||||
/**
|
||||
* g_slice_free1:
|
||||
* @block_size: the size of the block
|
||||
* @mem_block: a pointer to the block to free
|
||||
*
|
||||
* Frees a block of memory.
|
||||
*
|
||||
* The memory must have been allocated via g_slice_alloc() or
|
||||
* g_slice_alloc0() and the @block_size has to match the size
|
||||
* specified upon allocation. Note that the exact release behaviour
|
||||
* can be changed with the [`G_DEBUG=gc-friendly`][G_DEBUG] environment
|
||||
* variable, also see [`G_SLICE`][G_SLICE] for related debugging options.
|
||||
*
|
||||
* If @mem_block is %NULL, this function does nothing.
|
||||
*
|
||||
* Since: 2.10
|
||||
*/
|
||||
void g_slice_free1 (gsize mem_size, gpointer mem_block)
|
||||
{
|
||||
g_free (mem_block);
|
||||
}
|
||||
36
glib_compat/gslice.h
Normal file
36
glib_compat/gslice.h
Normal file
@@ -0,0 +1,36 @@
|
||||
/* GLIB sliced memory - fast threaded memory chunk allocator
|
||||
* Copyright (C) 2005 Tim Janik
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#ifndef __G_SLICE_H__
|
||||
#define __G_SLICE_H__
|
||||
|
||||
#include "gtypes.h"
|
||||
|
||||
#define g_slice_new(type) ((type*) g_slice_alloc (sizeof (type)))
|
||||
#define g_slice_new0(type) ((type*) g_slice_alloc0 (sizeof (type)))
|
||||
|
||||
gpointer g_slice_alloc0 (gsize block_size);
|
||||
gpointer g_slice_alloc (gsize block_size);
|
||||
void g_slice_free1 (gsize block_size, gpointer mem_block);
|
||||
|
||||
#define g_slice_free(type, mem) \
|
||||
G_STMT_START { \
|
||||
if (1) g_slice_free1 (sizeof (type), (mem)); \
|
||||
else (void) ((type*) 0 == (mem)); \
|
||||
} G_STMT_END
|
||||
|
||||
#endif /* __G_SLICE_H__ */
|
||||
34
glib_compat/gtestutils.c
Normal file
34
glib_compat/gtestutils.c
Normal file
@@ -0,0 +1,34 @@
|
||||
/* GLib testing utilities
|
||||
* Copyright (C) 2007 Imendio AB
|
||||
* Authors: Tim Janik, Sven Herzberg
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#include "gtestutils.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
void
|
||||
g_assertion_message_expr (const char *file,
|
||||
int line,
|
||||
const char *expr)
|
||||
{
|
||||
if (!expr)
|
||||
printf("%s:%d code should not be reached", file, line);
|
||||
else
|
||||
printf("%s:%d assertion failed: %s", file, line, expr);
|
||||
|
||||
abort();
|
||||
}
|
||||
57
glib_compat/gtestutils.h
Normal file
57
glib_compat/gtestutils.h
Normal file
@@ -0,0 +1,57 @@
|
||||
/* GLib testing utilities
|
||||
* Copyright (C) 2007 Imendio AB
|
||||
* Authors: Tim Janik
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#ifndef __G_TEST_UTILS_H__
|
||||
#define __G_TEST_UTILS_H__
|
||||
|
||||
|
||||
#if !(defined (G_STMT_START) && defined (G_STMT_END))
|
||||
#define G_STMT_START do
|
||||
#if defined (_MSC_VER) && (_MSC_VER >= 1500)
|
||||
#define G_STMT_END \
|
||||
__pragma(warning(push)) \
|
||||
__pragma(warning(disable:4127)) \
|
||||
while(0) \
|
||||
__pragma(warning(pop))
|
||||
#else
|
||||
#define G_STMT_END while (0)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
|
||||
#define G_GNUC_NORETURN \
|
||||
__attribute__((__noreturn__))
|
||||
#else /* !__GNUC__ */
|
||||
/* NOTE: MSVC has __declspec(noreturn) but unlike GCC __attribute__,
|
||||
* __declspec can only be placed at the start of the function prototype
|
||||
* and not at the end, so we can't use it without breaking API.
|
||||
*/
|
||||
#define G_GNUC_NORETURN
|
||||
#endif /* !__GNUC__ */
|
||||
|
||||
void g_assertion_message_expr (const char *file,
|
||||
int line,
|
||||
const char *expr) G_GNUC_NORETURN;
|
||||
|
||||
#define g_assert_not_reached() G_STMT_START { g_assertion_message_expr (__FILE__, __LINE__, NULL); } G_STMT_END
|
||||
#define g_assert(expr) G_STMT_START { \
|
||||
if (expr) ; else \
|
||||
g_assertion_message_expr (__FILE__, __LINE__, #expr); \
|
||||
} G_STMT_END
|
||||
|
||||
#endif /* __G_TEST_UTILS_H__ */
|
||||
1255
glib_compat/gtree.c
Normal file
1255
glib_compat/gtree.c
Normal file
File diff suppressed because it is too large
Load Diff
55
glib_compat/gtree.h
Normal file
55
glib_compat/gtree.h
Normal file
@@ -0,0 +1,55 @@
|
||||
/* GLIB - Library of useful routines for C programming
|
||||
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Modified by the GLib Team and others 1997-2000. See the AUTHORS
|
||||
* file for a list of people on the GLib Team. See the ChangeLog
|
||||
* files for a list of changes. These files are distributed with
|
||||
* GLib at ftp://ftp.gtk.org/pub/gtk/.
|
||||
*/
|
||||
|
||||
#ifndef __G_TREE_H__
|
||||
#define __G_TREE_H__
|
||||
|
||||
typedef struct _GTree GTree;
|
||||
|
||||
typedef gboolean (*GTraverseFunc) (gpointer key, gpointer value, gpointer data);
|
||||
|
||||
/* Balanced binary trees
|
||||
*/
|
||||
GTree* g_tree_new (GCompareFunc key_compare_func);
|
||||
|
||||
GTree* g_tree_new_full (GCompareDataFunc key_compare_func,
|
||||
gpointer key_compare_data,
|
||||
GDestroyNotify key_destroy_func,
|
||||
GDestroyNotify value_destroy_func);
|
||||
|
||||
GTree* g_tree_ref (GTree *tree);
|
||||
|
||||
void g_tree_destroy (GTree *tree);
|
||||
|
||||
void g_tree_insert (GTree *tree, gpointer key, gpointer value);
|
||||
|
||||
gboolean g_tree_remove (GTree *tree, gconstpointer key);
|
||||
|
||||
gpointer g_tree_lookup (GTree *tree, gconstpointer key);
|
||||
|
||||
void g_tree_foreach (GTree *tree, GTraverseFunc func, gpointer user_data);
|
||||
|
||||
gint g_tree_nnodes (GTree *tree);
|
||||
|
||||
#endif /* __G_TREE_H__ */
|
||||
80
glib_compat/gtypes.h
Normal file
80
glib_compat/gtypes.h
Normal file
@@ -0,0 +1,80 @@
|
||||
/* GLIB - Library of useful routines for C programming
|
||||
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Modified by the GLib Team and others 1997-2000. See the AUTHORS
|
||||
* file for a list of people on the GLib Team. See the ChangeLog
|
||||
* files for a list of changes. These files are distributed with
|
||||
* GLib at ftp://ftp.gtk.org/pub/gtk/.
|
||||
*/
|
||||
|
||||
#ifndef __G_TYPES_H__
|
||||
#define __G_TYPES_H__
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <float.h>
|
||||
|
||||
#ifndef TRUE
|
||||
#define TRUE 1
|
||||
#endif
|
||||
|
||||
#ifndef FALSE
|
||||
#define FALSE 0
|
||||
#endif
|
||||
|
||||
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
|
||||
|
||||
/* typedefs for glib related types that may still be referenced */
|
||||
typedef void* gpointer;
|
||||
|
||||
typedef const void *gconstpointer;
|
||||
|
||||
typedef int gint;
|
||||
typedef uint8_t guint8;
|
||||
typedef int8_t gint8;
|
||||
typedef uint16_t guint16;
|
||||
typedef int16_t gint16;
|
||||
typedef uint32_t guint32;
|
||||
typedef int32_t gint32;
|
||||
typedef uint64_t guint64;
|
||||
typedef int64_t gint64;
|
||||
typedef unsigned int guint;
|
||||
typedef char gchar;
|
||||
typedef int gboolean;
|
||||
typedef unsigned long gulong;
|
||||
typedef unsigned long gsize;
|
||||
|
||||
typedef gint grefcount;
|
||||
|
||||
typedef volatile gint gatomicrefcount;
|
||||
|
||||
typedef void (*GDestroyNotify) (gpointer data);
|
||||
|
||||
typedef gint (*GCompareFunc) (gconstpointer a, gconstpointer b);
|
||||
|
||||
typedef gint (*GCompareDataFunc) (gconstpointer a, gconstpointer b, gpointer user_data);
|
||||
|
||||
typedef guint (*GHashFunc) (gconstpointer key);
|
||||
|
||||
typedef gboolean (*GEqualFunc) (gconstpointer a, gconstpointer b);
|
||||
|
||||
typedef void (*GHFunc) (gpointer key, gpointer value, gpointer user_data);
|
||||
|
||||
typedef gpointer (*GCopyFunc) (gconstpointer src, gpointer data);
|
||||
|
||||
#endif /* __G_TYPES_H__ */
|
||||
Reference in New Issue
Block a user