librazor/razor-internal.h
author J. Ali Harlow <ali@juiblex.co.uk>
Wed Apr 29 17:00:01 2009 +0100 (2009-04-29)
changeset 361 2523d03a840e
parent 352 4866573c6944
child 364 66ec30bde5e5
permissions -rw-r--r--
Add support for preloading lua modules. This is useful both when
providing lua bindings to applications based on librazor and when
producing static binaries using librazor (where otherwise the lua
POSIX library would need to be included as an additional dynamic
object).
richard@300
     1
/*
richard@300
     2
 * Copyright (C) 2008  Kristian Høgsberg <krh@redhat.com>
richard@300
     3
 * Copyright (C) 2008  Red Hat, Inc
richard@300
     4
 *
richard@300
     5
 * This program is free software; you can redistribute it and/or modify
richard@300
     6
 * it under the terms of the GNU General Public License as published by
richard@300
     7
 * the Free Software Foundation; either version 2 of the License, or
richard@300
     8
 * (at your option) any later version.
richard@300
     9
 *
richard@300
    10
 * This program is distributed in the hope that it will be useful,
richard@300
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
richard@300
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
richard@300
    13
 * GNU General Public License for more details.
richard@300
    14
 *
richard@300
    15
 * You should have received a copy of the GNU General Public License along
richard@300
    16
 * with this program; if not, write to the Free Software Foundation, Inc.,
richard@300
    17
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
richard@300
    18
 */
richard@300
    19
rhughes@241
    20
#ifndef _RAZOR_INTERNAL_H_
rhughes@241
    21
#define _RAZOR_INTERNAL_H_
rhughes@241
    22
krh@248
    23
#include <stdlib.h>
krh@248
    24
#include <stdint.h>
richard@302
    25
#include <stdarg.h>
ali@360
    26
#include <unistd.h>
richard@302
    27
richard@302
    28
#include "razor.h"
krh@248
    29
krh@269
    30
/* GCC visibility */
krh@269
    31
#if defined(__GNUC__) && __GNUC__ >= 4
krh@269
    32
#define RAZOR_EXPORT __attribute__ ((visibility("default")))
krh@269
    33
#else
krh@269
    34
#define RAZOR_EXPORT
krh@269
    35
#endif
krh@269
    36
krh@271
    37
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
krh@271
    38
#define ALIGN(value, base) (((value) + (base - 1)) & ~((base) - 1))
krh@269
    39
krh@248
    40
void *zalloc(size_t size);
krh@248
    41
krh@248
    42
struct array {
krh@248
    43
	void *data;
krh@248
    44
	int size, alloc;
krh@248
    45
};
krh@248
    46
krh@248
    47
void array_init(struct array *array);
krh@248
    48
void array_release(struct array *array);
krh@248
    49
void *array_add(struct array *array, int size);
krh@248
    50
krh@248
    51
krh@248
    52
struct list_head {
krh@248
    53
	uint32_t list_ptr : 24;
krh@248
    54
	uint32_t flags    : 8;
krh@248
    55
};
krh@248
    56
krh@248
    57
struct list {
krh@248
    58
	uint32_t data  : 24;
krh@248
    59
	uint32_t flags : 8;
krh@248
    60
};
krh@248
    61
krh@248
    62
void list_set_empty(struct list_head *head);
krh@248
    63
void list_set_ptr(struct list_head *head, uint32_t ptr);
krh@248
    64
void list_set_array(struct list_head *head, struct array *pool, struct array *items, int force_indirect);
krh@248
    65
krh@248
    66
struct list *list_first(struct list_head *head, struct array *pool);
krh@248
    67
struct list *list_next(struct list *list);
krh@248
    68
krh@248
    69
void list_remap_pool(struct array *pool, uint32_t *map);
krh@248
    70
void list_remap_head(struct list_head *list, uint32_t *map);
krh@248
    71
krh@248
    72
krh@248
    73
struct hashtable {
krh@248
    74
	struct array buckets;
krh@248
    75
	struct array *pool;
krh@248
    76
};
krh@248
    77
krh@248
    78
void hashtable_init(struct hashtable *table, struct array *pool);
krh@248
    79
void hashtable_release(struct hashtable *table);
krh@248
    80
uint32_t hashtable_insert(struct hashtable *table, const char *key);
krh@248
    81
uint32_t hashtable_lookup(struct hashtable *table, const char *key);
krh@248
    82
uint32_t hashtable_tokenize(struct hashtable *table, const char *string);
krh@248
    83
krh@248
    84
krh@248
    85
struct razor_set_section {
jbowes@318
    86
	uint32_t name;
krh@248
    87
	uint32_t offset;
krh@248
    88
	uint32_t size;
krh@248
    89
};
krh@248
    90
krh@248
    91
struct razor_set_header {
krh@248
    92
	uint32_t magic;
krh@248
    93
	uint32_t version;
jbowes@318
    94
	uint32_t num_sections;
krh@248
    95
};
krh@248
    96
jbowes@318
    97
#define RAZOR_MAGIC 	0x525a4442
jbowes@318
    98
#define RAZOR_VERSION	1
krh@248
    99
jbowes@318
   100
#define RAZOR_STRING_POOL		"string_pool"
jbowes@318
   101
#define RAZOR_PACKAGES			"packages"
jbowes@318
   102
#define RAZOR_PROPERTIES		"properties"
jbowes@318
   103
#define RAZOR_PACKAGE_POOL		"package_pool"
jbowes@318
   104
#define RAZOR_PROPERTY_POOL		"property_pool"
jbowes@258
   105
jbowes@318
   106
#define RAZOR_DETAILS_STRING_POOL	"details_string_pool"
jbowes@258
   107
jbowes@318
   108
#define RAZOR_FILES			"files"
jbowes@318
   109
#define RAZOR_FILE_POOL			"file_pool"
jbowes@318
   110
#define RAZOR_FILE_STRING_POOL		"file_string_pool"
krh@248
   111
krh@248
   112
struct razor_package {
ali@324
   113
	unsigned int name  : 24;
ali@324
   114
	unsigned int flags : 8;
krh@248
   115
	uint32_t version;
krh@248
   116
	uint32_t arch;
jbowes@258
   117
	uint32_t summary;
jbowes@258
   118
	uint32_t description;
jbowes@258
   119
	uint32_t url;
jbowes@258
   120
	uint32_t license;
krh@248
   121
	struct list_head properties;
krh@248
   122
	struct list_head files;
krh@248
   123
};
krh@248
   124
jbowes@258
   125
krh@248
   126
struct razor_property {
krh@248
   127
	uint32_t name;
krh@248
   128
	uint32_t flags;
krh@248
   129
	uint32_t version;
krh@248
   130
	struct list_head packages;
krh@248
   131
};
krh@248
   132
krh@248
   133
struct razor_entry {
krh@248
   134
	uint32_t name  : 24;
krh@248
   135
	uint32_t flags : 8;
krh@248
   136
	uint32_t start;
krh@248
   137
	struct list_head packages;
krh@248
   138
};
krh@248
   139
krh@248
   140
#define RAZOR_ENTRY_LAST	0x80
krh@248
   141
krh@248
   142
struct razor_set {
krh@248
   143
	struct array string_pool;
krh@248
   144
 	struct array packages;
krh@248
   145
 	struct array properties;
krh@248
   146
 	struct array files;
krh@248
   147
	struct array package_pool;
krh@248
   148
 	struct array property_pool;
krh@248
   149
 	struct array file_pool;
jbowes@258
   150
	struct array file_string_pool;
jbowes@258
   151
	struct array details_string_pool;
jbowes@318
   152
krh@248
   153
	struct razor_set_header *header;
jbowes@318
   154
	size_t header_size;
jbowes@318
   155
jbowes@258
   156
	struct razor_set_header *details_header;
jbowes@318
   157
	size_t details_header_size;
jbowes@318
   158
jbowes@258
   159
	struct razor_set_header *files_header;
jbowes@318
   160
	size_t files_header_size;
krh@248
   161
};
krh@248
   162
krh@248
   163
struct import_entry {
krh@248
   164
	uint32_t package;
krh@248
   165
	char *name;
krh@248
   166
};
krh@248
   167
krh@248
   168
struct import_directory {
krh@248
   169
	uint32_t name, count;
krh@248
   170
	struct array files;
krh@248
   171
	struct array packages;
krh@248
   172
	struct import_directory *last;
krh@248
   173
};
krh@248
   174
krh@248
   175
struct razor_importer {
krh@248
   176
	struct razor_set *set;
krh@248
   177
	struct hashtable table;
jbowes@258
   178
	struct hashtable file_table;
jbowes@258
   179
	struct hashtable details_table;
krh@248
   180
	struct razor_package *package;
krh@248
   181
	struct array properties;
krh@248
   182
	struct array files;
krh@248
   183
	struct array file_requires;
krh@248
   184
};
krh@248
   185
krh@248
   186
struct razor_package_iterator {
krh@248
   187
	struct razor_set *set;
krh@248
   188
	struct razor_package *package, *end;
krh@248
   189
	struct list *index;
krh@248
   190
	int free_index;
krh@248
   191
};
krh@248
   192
krh@248
   193
void
krh@248
   194
razor_package_iterator_init_for_property(struct razor_package_iterator *pi,
krh@248
   195
					 struct razor_set *set,
krh@248
   196
					 struct razor_property *property);
krh@248
   197
krh@248
   198
struct razor_property_iterator {
krh@248
   199
	struct razor_set *set;
krh@248
   200
	struct razor_property *property, *end;
krh@248
   201
	struct list *index;
krh@248
   202
};
krh@248
   203
ali@351
   204
struct razor_file_iterator {
ali@351
   205
	struct razor_set *set;
ali@351
   206
	struct array path;
ali@351
   207
	struct list *index;
ali@351
   208
};
ali@351
   209
krh@248
   210
struct razor_entry *
krh@248
   211
razor_set_find_entry(struct razor_set *set,
krh@248
   212
		     struct razor_entry *dir, const char *pattern);
krh@248
   213
krh@248
   214
struct razor_merger *
krh@248
   215
razor_merger_create(struct razor_set *set1, struct razor_set *set2);
krh@248
   216
void
krh@248
   217
razor_merger_add_package(struct razor_merger *merger,
krh@248
   218
			 struct razor_package *package);
krh@248
   219
struct razor_set *
krh@248
   220
razor_merger_finish(struct razor_merger *merger);
krh@248
   221
ali@352
   222
int run_lua_script(const char *root, const char *name, const char *body,
ali@352
   223
		   ssize_t len);
ali@352
   224
rhughes@241
   225
/* Utility functions */
rhughes@241
   226
richard@302
   227
void
richard@302
   228
razor_package_get_details_varg(struct razor_set *set,
richard@302
   229
			       struct razor_package *package,
richard@302
   230
			       va_list args);
richard@302
   231
rhughes@241
   232
int razor_create_dir(const char *root, const char *path);
rhughes@241
   233
int razor_write(int fd, const void *data, size_t size);
rhughes@241
   234
ali@322
   235
void *razor_file_get_contents(const char *filename, size_t *length);
ali@322
   236
int razor_file_free_contents(void *addr, size_t length);
ali@322
   237
rhughes@241
   238
rhughes@241
   239
typedef int (*razor_compare_with_data_func_t)(const void *p1,
rhughes@241
   240
					      const void *p,
rhughes@241
   241
					      void *data);
rhughes@241
   242
uint32_t *
rhughes@241
   243
razor_qsort_with_data(void *base, size_t nelem, size_t size,
rhughes@241
   244
		      razor_compare_with_data_func_t compare, void *data);
rhughes@241
   245
rhughes@241
   246
#endif /* _RAZOR_INTERNAL_H_ */