1
/* Copyright (C) 2005 PrimeBase Technologies GmbH
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
* 2004-01-03 Paul McCullagh
24
#include "xt_config.h"
30
#include <sys/param.h>
34
#include "strutil_xt.h"
35
#include "memory_xt.h"
37
xtPublic int xt_comp_log_pos(xtLogID id1, off_t off1, xtLogID id2, off_t off2)
51
* This function returns the current time in micorsonds since
52
* 00:00:00 UTC, January 1, 1970.
53
* Currently it is accurate to the second :(
55
xtPublic xtWord8 xt_time_now(void)
59
ms = (xtWord8) time(NULL);
64
xtPublic void xt_free_nothing(struct XTThread *XT_UNUSED(thread), void *XT_UNUSED(x))
69
* A file name has the form:
70
* <text>-<number>[.<ext>]
71
* This function return the number part as a
74
xtPublic xtWord4 xt_file_name_to_id(char *file_name)
79
char *num = file_name + strlen(file_name) - 1;
81
while (num >= file_name && *num != '-')
85
sscanf(num, "%lu", &value);
87
return (xtWord4) value;
91
* now is moving forward. then is a static time in the
92
* future. What is the time difference?
94
* These variables can overflow.
96
xtPublic int xt_time_difference(register xtWord4 now, register xtWord4 then)
98
/* now is after then, so the now time has passed
99
* then. So we return a negative difference.
102
/* now has gone past then. If the difference is
103
* great, then we assume an overflow, and reverse!
105
if ((now - then) > (xtWord4) 0xFFFFFFFF/2)
106
return (int) (0xFFFFFFFF - (now - then));
108
return (int) now - (int) then;
110
/* If now is before then, we check the difference.
111
* If the difference is very large, then we assume
112
* that now has gone past then, and overflowed.
114
if ((then - now) > (xtWord4) 0xFFFFFFFF/2)
115
return - (int) (0xFFFFFFFF - (then - now));
119
xtPublic xtWord2 xt_get_checksum(xtWord1 *data, size_t len, u_int interval)
121
register xtWord4 sum = 0, g;
124
chk = data + len - 1;
126
sum = (sum << 4) + *chk;
127
if ((g = sum & 0xF0000000)) {
128
sum = sum ^ (g >> 24);
133
return (xtWord2) (sum ^ (sum >> 16));
136
xtPublic xtWord1 xt_get_checksum1(xtWord1 *data, size_t len)
138
register xtWord4 sum = 0, g;
141
chk = data + len - 1;
143
sum = (sum << 4) + *chk;
144
if ((g = sum & 0xF0000000)) {
145
sum = sum ^ (g >> 24);
150
return (xtWord1) (sum ^ (sum >> 24) ^ (sum >> 16) ^ (sum >> 8));
153
xtPublic xtWord4 xt_get_checksum4(xtWord1 *data, size_t len)
155
register xtWord4 sum = 0, g;
158
chk = data + len - 1;
160
sum = (sum << 4) + *chk;
161
if ((g = sum & 0xF0000000)) {
162
sum = sum ^ (g >> 24);
171
* --------------- Data Buffer ------------------
174
xtPublic xtBool xt_db_set_size(struct XTThread *self, XTDataBufferPtr dbuf, size_t size)
176
if (dbuf->db_size < size) {
177
if (!xt_realloc(self, (void **) &dbuf->db_data, size))
179
dbuf->db_size = size;
183
xt_free(self, dbuf->db_data);
184
dbuf->db_data = NULL;
191
* --------------- Data Buffer ------------------
194
xtPublic xtBool xt_ib_alloc(struct XTThread *self, XTInfoBufferPtr ib, size_t size)
197
ib->ib_db.db_size = 0;
198
ib->ib_db.db_data = NULL;
200
if (size <= ib->ib_db.db_size)
203
if (size <= XT_IB_DEFAULT_SIZE) {
204
ib->ib_db.db_size = XT_IB_DEFAULT_SIZE;
205
ib->ib_db.db_data = ib->ib_data;
209
if (ib->ib_db.db_data == ib->ib_data) {
210
ib->ib_db.db_size = 0;
211
ib->ib_db.db_data = NULL;
215
return xt_db_set_size(self, &ib->ib_db, size);
218
void xt_ib_free(struct XTThread *self, XTInfoBufferPtr ib)
221
xt_db_set_size(self, &ib->ib_db, 0);
227
* --------------- Basic List ------------------
230
xtPublic xtBool xt_bl_set_size(struct XTThread *self, XTBasicListPtr bl, size_t size)
232
if (bl->bl_size < size) {
233
if (!xt_realloc(self, (void **) &bl->bl_data, size * bl->bl_item_size))
239
xt_free(self, bl->bl_data);
247
xtPublic xtBool xt_bl_dup(struct XTThread *self, XTBasicListPtr from_bl, XTBasicListPtr to_bl)
249
to_bl->bl_item_size = from_bl->bl_item_size;
251
to_bl->bl_count = from_bl->bl_count;
252
to_bl->bl_data = NULL;
253
if (!xt_bl_set_size(self, to_bl, from_bl->bl_count))
255
memcpy(to_bl->bl_data, from_bl->bl_data, to_bl->bl_count * to_bl->bl_item_size);
259
xtPublic xtBool xt_bl_append(struct XTThread *self, XTBasicListPtr bl, void *value)
261
if (bl->bl_count == bl->bl_size) {
262
if (!xt_bl_set_size(self, bl, bl->bl_count+1))
265
memcpy(&bl->bl_data[bl->bl_count * bl->bl_item_size], value, bl->bl_item_size);
270
xtPublic void *xt_bl_last_item(XTBasicListPtr bl)
274
return &bl->bl_data[(bl->bl_count-1) * bl->bl_item_size];
277
xtPublic void *xt_bl_item_at(XTBasicListPtr bl, u_int i)
279
if (i >= bl->bl_count)
281
return &bl->bl_data[i * bl->bl_item_size];
284
xtPublic void xt_bl_free(struct XTThread *self, XTBasicListPtr wl)
286
xt_bl_set_size(self, wl, 0);
290
* --------------- Basic Queue ------------------
293
xtPublic xtBool xt_bq_set_size(struct XTThread *self, XTBasicQueuePtr bq, size_t size)
295
if (bq->bq_size < size) {
296
if (!xt_realloc(self, (void **) &bq->bq_data, size * bq->bq_item_size))
302
xt_free(self, bq->bq_data);
311
xtPublic void *xt_bq_get(XTBasicQueuePtr bq)
313
if (bq->bq_back == bq->bq_front)
315
return &bq->bq_data[bq->bq_back * bq->bq_item_size];
318
xtPublic void xt_bq_next(XTBasicQueuePtr bq)
320
if (bq->bq_back < bq->bq_front) {
322
if (bq->bq_front == bq->bq_back) {
329
xtPublic xtBool xt_bq_add(struct XTThread *self, XTBasicQueuePtr bq, void *value)
331
if (bq->bq_front == bq->bq_size) {
332
if (bq->bq_back >= bq->bq_max_waste) {
333
bq->bq_front -= bq->bq_back;
334
memmove(bq->bq_data, &bq->bq_data[bq->bq_back * bq->bq_item_size], bq->bq_front * bq->bq_item_size);
338
if (!xt_bq_set_size(self, bq, bq->bq_front+bq->bq_item_inc))
342
memcpy(&bq->bq_data[bq->bq_front * bq->bq_item_size], value, bq->bq_item_size);
347
xtPublic void xt_sb_free(struct XTThread *self, XTStringBufferPtr dbuf)
349
xt_sb_set_size(self, dbuf, 0);
352
xtPublic xtBool xt_sb_set_size(struct XTThread *self, XTStringBufferPtr dbuf, size_t size)
354
if (dbuf->sb_size < size) {
355
if (!xt_realloc(self, (void **) &dbuf->sb_cstring, size))
357
dbuf->sb_size = size;
360
if (dbuf->sb_cstring)
361
xt_free(self, dbuf->sb_cstring);
362
dbuf->sb_cstring = NULL;
369
xtPublic xtBool xt_sb_concat_len(struct XTThread *self, XTStringBufferPtr dbuf, c_char *str, size_t len)
371
if (!xt_sb_set_size(self, dbuf, dbuf->sb_len + len + 1))
373
memcpy(dbuf->sb_cstring + dbuf->sb_len, str, len);
375
dbuf->sb_cstring[dbuf->sb_len] = 0;
379
xtPublic xtBool xt_sb_concat(struct XTThread *self, XTStringBufferPtr dbuf, c_char *str)
381
return xt_sb_concat_len(self, dbuf, str, strlen(str));
384
xtPublic xtBool xt_sb_concat_char(struct XTThread *self, XTStringBufferPtr dbuf, int ch)
386
if (!xt_sb_set_size(self, dbuf, dbuf->sb_len + 1 + 1))
388
dbuf->sb_cstring[dbuf->sb_len] = (char) ch;
390
dbuf->sb_cstring[dbuf->sb_len] = 0;
394
xtPublic xtBool xt_sb_concat_int8(struct XTThread *self, XTStringBufferPtr dbuf, xtInt8 val)
398
sprintf(buffer, "%"PRId64, val);
399
return xt_sb_concat(self, dbuf, buffer);
402
xtPublic char *xt_sb_take_cstring(XTStringBufferPtr sbuf)
404
char *str = sbuf->sb_cstring;
406
sbuf->sb_cstring = NULL;
412
xtPublic xtBool xt_sb_concat_url_len(struct XTThread *self, XTStringBufferPtr dbuf, c_char *from, size_t len_from)
414
if (!xt_sb_set_size(self, dbuf, dbuf->sb_len + len_from + 1))
417
if (*from == '%' && len_from >= 2 && isxdigit(*(from+1)) && isxdigit(*(from+2))) {
418
unsigned char a = xt_hex_digit(*(from+1));
419
unsigned char b = xt_hex_digit(*(from+2));
420
dbuf->sb_cstring[dbuf->sb_len] = a << 4 | b;
424
dbuf->sb_cstring[dbuf->sb_len] = *from++;
427
dbuf->sb_cstring[dbuf->sb_len] = 0;