2012-09-20 07:51:34 +04:00
|
|
|
/**
|
|
|
|
* xrdp: A Remote Desktop Protocol server.
|
|
|
|
*
|
2014-03-02 11:26:40 +04:00
|
|
|
* Copyright (C) Jay Sorg 2004-2014
|
2012-09-20 07:51:34 +04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
* simple list
|
|
|
|
*/
|
2005-06-28 07:04:36 +04:00
|
|
|
|
2017-03-03 07:33:23 +03:00
|
|
|
#if defined(HAVE_CONFIG_H)
|
|
|
|
#include <config_ac.h>
|
|
|
|
#endif
|
|
|
|
|
2023-02-08 14:16:03 +03:00
|
|
|
#include <stdlib.h>
|
2023-03-06 16:54:22 +03:00
|
|
|
#include <stdarg.h>
|
2023-02-08 14:16:03 +03:00
|
|
|
|
2005-06-28 07:04:36 +04:00
|
|
|
#include "arch.h"
|
|
|
|
#include "os_calls.h"
|
2020-12-21 15:36:00 +03:00
|
|
|
#include "string_calls.h"
|
2005-06-28 07:04:36 +04:00
|
|
|
#include "list.h"
|
2020-08-02 07:34:31 +03:00
|
|
|
#include "log.h"
|
2005-06-28 07:04:36 +04:00
|
|
|
|
2023-02-08 14:16:03 +03:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
DEFAULT_LIST_SIZE = 10,
|
|
|
|
DEFAULT_GROW_BY_SIZE = 10
|
|
|
|
};
|
|
|
|
|
2023-02-15 08:50:28 +03:00
|
|
|
/******************************************************************************/
|
2017-03-12 19:35:00 +03:00
|
|
|
struct list *
|
2023-02-08 14:16:03 +03:00
|
|
|
list_create_sized(unsigned int size)
|
2005-06-28 07:04:36 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
struct list *self;
|
2005-06-28 07:04:36 +04:00
|
|
|
|
2023-02-08 14:16:03 +03:00
|
|
|
if (size < DEFAULT_LIST_SIZE)
|
|
|
|
{
|
|
|
|
size = DEFAULT_LIST_SIZE;
|
|
|
|
}
|
|
|
|
self = (struct list *)calloc(sizeof(struct list), 1);
|
|
|
|
if (self != NULL)
|
|
|
|
{
|
|
|
|
self->items = (tbus *)malloc(sizeof(tbus) * size);
|
|
|
|
if (self->items == NULL)
|
|
|
|
{
|
|
|
|
free(self);
|
|
|
|
self = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
self->grow_by = DEFAULT_GROW_BY_SIZE;
|
|
|
|
self->alloc_size = size;
|
|
|
|
}
|
|
|
|
}
|
2012-09-20 07:51:34 +04:00
|
|
|
return self;
|
2005-06-28 07:04:36 +04:00
|
|
|
}
|
|
|
|
|
2023-02-15 08:50:28 +03:00
|
|
|
/******************************************************************************/
|
2023-02-08 14:16:03 +03:00
|
|
|
struct list *
|
|
|
|
list_create(void)
|
|
|
|
{
|
|
|
|
return list_create_sized(DEFAULT_LIST_SIZE);
|
|
|
|
}
|
|
|
|
|
2023-02-15 08:50:28 +03:00
|
|
|
/******************************************************************************/
|
2017-03-12 19:35:00 +03:00
|
|
|
void
|
2012-09-20 07:51:34 +04:00
|
|
|
list_delete(struct list *self)
|
2005-06-28 07:04:36 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (self == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self->auto_free)
|
2005-06-28 07:04:36 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
for (i = 0; i < self->count; i++)
|
|
|
|
{
|
2023-02-08 14:16:03 +03:00
|
|
|
free((void *)self->items[i]);
|
2012-09-20 07:51:34 +04:00
|
|
|
self->items[i] = 0;
|
|
|
|
}
|
2005-06-28 07:04:36 +04:00
|
|
|
}
|
2012-09-20 07:51:34 +04:00
|
|
|
|
2023-02-08 14:16:03 +03:00
|
|
|
free(self->items);
|
|
|
|
free(self);
|
2005-06-28 07:04:36 +04:00
|
|
|
}
|
|
|
|
|
2023-02-15 08:50:28 +03:00
|
|
|
/******************************************************************************/
|
2023-02-08 14:16:03 +03:00
|
|
|
static int
|
|
|
|
grow_list(struct list *self)
|
2005-06-28 07:04:36 +04:00
|
|
|
{
|
2023-02-08 14:16:03 +03:00
|
|
|
int rv = 1;
|
|
|
|
unsigned int new_alloc_size = self->alloc_size + self->grow_by;
|
|
|
|
tbus *p = (tbus *)realloc(self->items, sizeof(tbus) * new_alloc_size);
|
|
|
|
if (p == NULL)
|
|
|
|
{
|
|
|
|
rv = 0;
|
|
|
|
}
|
|
|
|
else
|
2012-09-20 07:51:34 +04:00
|
|
|
{
|
2023-02-08 14:16:03 +03:00
|
|
|
self->alloc_size = new_alloc_size;
|
2012-09-20 07:51:34 +04:00
|
|
|
self->items = p;
|
|
|
|
}
|
2023-02-08 14:16:03 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
int
|
|
|
|
list_add_item(struct list *self, tbus item)
|
|
|
|
{
|
|
|
|
if (self->count == self->alloc_size && !grow_list(self))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2012-09-20 07:51:34 +04:00
|
|
|
|
|
|
|
self->items[self->count] = item;
|
|
|
|
self->count++;
|
2023-02-08 14:16:03 +03:00
|
|
|
return 1;
|
2005-06-28 07:04:36 +04:00
|
|
|
}
|
|
|
|
|
2023-02-15 08:50:28 +03:00
|
|
|
/******************************************************************************/
|
2017-03-12 19:35:00 +03:00
|
|
|
tbus
|
2016-10-17 09:14:06 +03:00
|
|
|
list_get_item(const struct list *self, int index)
|
2005-06-28 07:04:36 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
if (index < 0 || index >= self->count)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self->items[index];
|
2005-06-28 07:04:36 +04:00
|
|
|
}
|
|
|
|
|
2023-02-15 08:50:28 +03:00
|
|
|
/******************************************************************************/
|
2017-03-12 19:35:00 +03:00
|
|
|
void
|
2012-09-20 07:51:34 +04:00
|
|
|
list_clear(struct list *self)
|
2005-06-28 07:04:36 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
int i;
|
2005-06-28 07:04:36 +04:00
|
|
|
|
2012-09-20 07:51:34 +04:00
|
|
|
if (self->auto_free)
|
2005-06-28 07:04:36 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
for (i = 0; i < self->count; i++)
|
|
|
|
{
|
2023-02-08 14:16:03 +03:00
|
|
|
free((void *)self->items[i]);
|
2012-09-20 07:51:34 +04:00
|
|
|
self->items[i] = 0;
|
|
|
|
}
|
2005-06-28 07:04:36 +04:00
|
|
|
}
|
2012-09-20 07:51:34 +04:00
|
|
|
|
|
|
|
self->count = 0;
|
2023-02-08 14:16:03 +03:00
|
|
|
self->grow_by = DEFAULT_GROW_BY_SIZE;
|
|
|
|
self->alloc_size = DEFAULT_LIST_SIZE;
|
|
|
|
self->items = (tbus *)realloc(self->items, sizeof(tbus) * self->alloc_size);
|
2005-06-28 07:04:36 +04:00
|
|
|
}
|
|
|
|
|
2023-02-15 08:50:28 +03:00
|
|
|
/******************************************************************************/
|
2017-03-12 19:35:00 +03:00
|
|
|
int
|
2012-09-20 07:51:34 +04:00
|
|
|
list_index_of(struct list *self, tbus item)
|
2005-06-28 07:04:36 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
int i;
|
2005-06-28 07:04:36 +04:00
|
|
|
|
2012-09-20 07:51:34 +04:00
|
|
|
for (i = 0; i < self->count; i++)
|
2005-06-28 07:04:36 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
if (self->items[i] == item)
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
2005-06-28 07:04:36 +04:00
|
|
|
}
|
2012-09-20 07:51:34 +04:00
|
|
|
|
|
|
|
return -1;
|
2005-06-28 07:04:36 +04:00
|
|
|
}
|
|
|
|
|
2023-02-15 08:50:28 +03:00
|
|
|
/******************************************************************************/
|
2017-03-12 19:35:00 +03:00
|
|
|
void
|
2012-09-20 07:51:34 +04:00
|
|
|
list_remove_item(struct list *self, int index)
|
2005-06-28 07:04:36 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
int i;
|
2005-06-28 07:04:36 +04:00
|
|
|
|
2012-09-20 07:51:34 +04:00
|
|
|
if (index >= 0 && index < self->count)
|
2005-06-28 07:04:36 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
if (self->auto_free)
|
|
|
|
{
|
2023-02-08 14:16:03 +03:00
|
|
|
free((void *)self->items[index]);
|
2012-09-20 07:51:34 +04:00
|
|
|
self->items[index] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = index; i < (self->count - 1); i++)
|
|
|
|
{
|
|
|
|
self->items[i] = self->items[i + 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
self->count--;
|
2005-06-28 07:04:36 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-08 14:16:03 +03:00
|
|
|
int
|
2012-09-20 07:51:34 +04:00
|
|
|
list_insert_item(struct list *self, int index, tbus item)
|
2005-06-28 07:04:36 +04:00
|
|
|
{
|
2023-03-06 18:42:33 +03:00
|
|
|
int i;
|
2012-09-20 07:51:34 +04:00
|
|
|
|
2023-02-08 14:16:03 +03:00
|
|
|
if (index > self->count)
|
2005-06-28 07:04:36 +04:00
|
|
|
{
|
2023-02-08 14:16:03 +03:00
|
|
|
index = self->count;
|
2005-06-28 07:04:36 +04:00
|
|
|
}
|
2023-02-08 14:16:03 +03:00
|
|
|
else if (index < 0)
|
2005-06-28 07:04:36 +04:00
|
|
|
{
|
2023-02-08 14:16:03 +03:00
|
|
|
index = 0;
|
|
|
|
}
|
2012-09-20 07:51:34 +04:00
|
|
|
|
2023-02-08 14:16:03 +03:00
|
|
|
if (self->count == self->alloc_size && !grow_list(self))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2012-09-20 07:51:34 +04:00
|
|
|
|
2023-03-06 18:42:33 +03:00
|
|
|
// Move all the items above this location up one
|
|
|
|
for (i = self->count ; i > index ; --i)
|
2023-02-08 14:16:03 +03:00
|
|
|
{
|
2023-03-06 18:42:33 +03:00
|
|
|
self->items[i] = self->items[i - 1];
|
2005-06-28 07:04:36 +04:00
|
|
|
}
|
2023-02-08 14:16:03 +03:00
|
|
|
|
2023-03-06 18:42:33 +03:00
|
|
|
self->count++;
|
|
|
|
|
2023-02-08 14:16:03 +03:00
|
|
|
self->items[index] = item;
|
|
|
|
return 1;
|
2005-06-28 07:04:36 +04:00
|
|
|
}
|
2006-09-18 08:35:10 +04:00
|
|
|
|
2023-03-06 16:54:22 +03:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
int
|
|
|
|
list_add_strdup(struct list *self, const char *str)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
char *dup;
|
|
|
|
|
|
|
|
if (str == NULL)
|
|
|
|
{
|
|
|
|
rv = list_add_item(self, (tintptr)str);
|
|
|
|
}
|
|
|
|
else if ((dup = g_strdup(str)) == NULL)
|
|
|
|
{
|
|
|
|
rv = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rv = list_add_item(self, (tintptr)dup);
|
|
|
|
if (!rv)
|
|
|
|
{
|
|
|
|
g_free(dup);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
int
|
|
|
|
list_add_strdup_multi(struct list *self, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int entry_count = self->count;
|
|
|
|
const char *s;
|
|
|
|
int rv = 1;
|
|
|
|
|
|
|
|
va_start(ap, self);
|
|
|
|
while ((s = va_arg(ap, const char *)) != NULL)
|
|
|
|
{
|
|
|
|
if (!list_add_strdup(self, s))
|
|
|
|
{
|
|
|
|
rv = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
if (rv == 0)
|
|
|
|
{
|
|
|
|
// Remove the additional items we added
|
|
|
|
while (self->count > entry_count)
|
|
|
|
{
|
|
|
|
list_remove_item(self, self->count - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
2023-02-15 08:50:28 +03:00
|
|
|
/******************************************************************************/
|
2006-09-18 08:35:10 +04:00
|
|
|
/* append one list to another using strdup for each item in the list */
|
2016-02-14 07:41:07 +03:00
|
|
|
/* begins copy at start_index, a zero based index on the source list */
|
2023-02-08 14:16:03 +03:00
|
|
|
int
|
2012-09-20 07:51:34 +04:00
|
|
|
list_append_list_strdup(struct list *self, struct list *dest, int start_index)
|
2006-09-18 08:35:10 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
int index;
|
2023-02-08 14:16:03 +03:00
|
|
|
int rv = 1;
|
|
|
|
int entry_dest_count = dest->count;
|
2012-09-20 07:51:34 +04:00
|
|
|
|
|
|
|
for (index = start_index; index < self->count; index++)
|
|
|
|
{
|
2023-02-08 14:16:03 +03:00
|
|
|
const char *item = (const char *)list_get_item(self, index);
|
2023-03-06 16:54:22 +03:00
|
|
|
if (!list_add_strdup(dest, item))
|
2023-02-08 14:16:03 +03:00
|
|
|
{
|
|
|
|
rv = 0;
|
|
|
|
break;
|
|
|
|
}
|
2012-09-20 07:51:34 +04:00
|
|
|
}
|
2023-02-08 14:16:03 +03:00
|
|
|
|
|
|
|
if (rv == 0)
|
|
|
|
{
|
|
|
|
// Remove the additional items we added
|
|
|
|
while (dest->count > entry_dest_count)
|
|
|
|
{
|
|
|
|
list_remove_item(dest, dest->count - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2006-09-18 08:35:10 +04:00
|
|
|
}
|
2009-08-05 10:16:05 +04:00
|
|
|
|
2023-02-15 08:50:28 +03:00
|
|
|
/******************************************************************************/
|
2017-03-12 19:35:00 +03:00
|
|
|
void
|
2012-09-20 07:51:34 +04:00
|
|
|
list_dump_items(struct list *self)
|
2009-08-05 10:16:05 +04:00
|
|
|
{
|
2012-09-20 07:51:34 +04:00
|
|
|
int index;
|
|
|
|
|
|
|
|
if (self->count == 0)
|
|
|
|
{
|
2020-08-02 07:34:31 +03:00
|
|
|
LOG_DEVEL(LOG_LEVEL_TRACE, "List is empty");
|
2012-09-20 07:51:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
for (index = 0; index < self->count; index++)
|
|
|
|
{
|
2020-08-02 07:34:31 +03:00
|
|
|
LOG_DEVEL(LOG_LEVEL_TRACE, "%d: %p", index, (void *) list_get_item(self, index));
|
2012-09-20 07:51:34 +04:00
|
|
|
}
|
2009-08-05 10:16:05 +04:00
|
|
|
}
|
2023-02-15 08:50:28 +03:00
|
|
|
|
|
|
|
int
|
|
|
|
split_string_append_fragment(const char **start, const char *end,
|
|
|
|
struct list *list)
|
|
|
|
{
|
2024-05-07 14:01:50 +03:00
|
|
|
unsigned int len = end - *start;
|
|
|
|
// Check for an unexpected terminator in the string
|
|
|
|
const char *term = (const char *)memchr(*start, '\0', len);
|
|
|
|
if (term != NULL)
|
|
|
|
{
|
|
|
|
end = term;
|
|
|
|
len = end - *start;
|
|
|
|
}
|
2023-02-15 08:50:28 +03:00
|
|
|
char *copy = (char *)malloc(len + 1);
|
|
|
|
if (copy == NULL)
|
|
|
|
{
|
|
|
|
list_delete(list);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
g_memcpy(copy, *start, len);
|
|
|
|
copy[len] = '\0';
|
|
|
|
if (!list_add_item(list, (tintptr)copy))
|
|
|
|
{
|
|
|
|
g_free(copy);
|
|
|
|
list_delete(list);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*start = end + 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
struct list *
|
|
|
|
split_string_into_list(const char *str, char character)
|
|
|
|
{
|
|
|
|
struct list *result = list_create();
|
|
|
|
if (result == NULL)
|
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
result->auto_free = 1;
|
|
|
|
|
|
|
|
if (str == NULL)
|
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *p;
|
|
|
|
while ((p = g_strchr(str, character)) != NULL)
|
|
|
|
{
|
|
|
|
if (!split_string_append_fragment(&str, p, result))
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*str != '\0')
|
|
|
|
{
|
|
|
|
if (!split_string_append_fragment(&str, str + g_strlen(str), result))
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|