Commit 520612ff authored by Ralf S. Engelschall's avatar Ralf S. Engelschall
Browse files

Port ssl_util_table.[ch] to Apache 2.0 by just removing all platform

depended code (table_read, table_write). This is possible because this
table library is local to mod_ssl and inside mod_ssl this library is
used for manipulating hash tables inside shared memory segments only. So
we can just get rid of the unportable parts at all.


git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@89030 13f79535-47bb-0310-9956-ffa450edef68
parent 23d110fa
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -54,8 +54,8 @@
 - ssl_util.c .............. utility functions
 # ssl_util_ssl.c .......... the OpenSSL companion source
 # ssl_util_ssl.h .......... the OpenSSL companion header
 - ssl_util_table.c ........ the hash table library source
 - ssl_util_table.h ........ the hash table library header
 # ssl_util_table.c ........ the hash table library source
 # ssl_util_table.h ........ the hash table library header

 Legend: # = already ported to Apache 2.0
         - = port still not finished
+0 −353
Original line number Diff line number Diff line
@@ -87,18 +87,8 @@
 *   o added support for MM library via ta_{malloc,calloc,realloc,free}
 */

#if 0 /* XXX */

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
#include <io.h>
#include <errno.h>
#else
#include <unistd.h>
#endif

/* forward definitions for table.h */
typedef struct table_st table_t;
@@ -2352,347 +2342,6 @@ int table_this_r(table_t * table_p, table_linear_t * linear_p,
    return TABLE_ERROR_NONE;
}

/******************************* file routines *******************************/

/*
 * int table_read
 *
 * DESCRIPTION:
 *
 * Read in a table from a file that had been written to disk earlier
 * via table_write.
 *
 * RETURNS:
 *
 * Success - Pointer to the new table structure which must be passed
 * to table_free to be deallocated.
 *
 * Failure - NULL
 *
 * ARGUMENTS:
 *
 * path - Table file to read in.
 *
 * error_p - Pointer to an integer which, if not NULL, will contain a
 * table error code.
 */
table_t *table_read(const char *path, int *error_p,
                    void *(*malloc_f)(size_t size),
                    void *(*calloc_f)(size_t number, size_t size),
                    void *(*realloc_f)(void *ptr, size_t size),
                    void (*free_f)(void *ptr))
{
    unsigned int size;
    int fd, ent_size;
    FILE *infile;
    table_entry_t entry, **bucket_p, *entry_p = NULL, *last_p;
    unsigned long pos;
    table_t *table_p;

    /* open the file */
    fd = open(path, O_RDONLY, 0);
    if (fd < 0) {
        if (error_p != NULL)
            *error_p = TABLE_ERROR_OPEN;
        return NULL;
    }

    /* allocate a table structure */
    if (malloc_f != NULL)
        table_p = malloc_f(sizeof(table_t));
    else
        table_p = malloc(sizeof(table_t));
    if (table_p == NULL) {
        if (error_p != NULL)
            *error_p = TABLE_ERROR_ALLOC;
        return NULL;
    }

    /* now open the fd to get buffered i/o */
    infile = fdopen(fd, "r");
    if (infile == NULL) {
        if (error_p != NULL)
            *error_p = TABLE_ERROR_OPEN;
        return NULL;
    }

    /* read the main table struct */
    if (fread(table_p, sizeof(table_t), 1, infile) != 1) {
        if (error_p != NULL)
            *error_p = TABLE_ERROR_READ;
        if (free_f != NULL)
            free_f(table_p);
        else
            free(table_p);
        return NULL;
    }
    table_p->ta_file_size = 0;

    table_p->ta_malloc  = malloc_f  != NULL ? malloc_f  : malloc;
    table_p->ta_calloc  = calloc_f  != NULL ? calloc_f  : calloc;
    table_p->ta_realloc = realloc_f != NULL ? realloc_f : realloc;
    table_p->ta_free    = free_f    != NULL ? free_f    : free;

    /* is the file contain bad info or maybe another system type? */
    if (table_p->ta_magic != TABLE_MAGIC) {
        if (error_p != NULL)
            *error_p = TABLE_ERROR_PNT;
        return NULL;
    }

    /* allocate the buckets */
    table_p->ta_buckets = (table_entry_t **)table_p->ta_calloc(table_p->ta_bucket_n, sizeof(table_entry_t *));
    if (table_p->ta_buckets == NULL) {
        if (error_p != NULL)
            *error_p = TABLE_ERROR_ALLOC;
        table_p->ta_free(table_p);
        return NULL;
    }

    if (fread(table_p->ta_buckets, sizeof(table_entry_t *), table_p->ta_bucket_n,
              infile) != (size_t) table_p->ta_bucket_n) {
        if (error_p != NULL)
            *error_p = TABLE_ERROR_READ;
        table_p->ta_free(table_p->ta_buckets);
        table_p->ta_free(table_p);
        return NULL;
    }

    /* read in the entries */
    for (bucket_p = table_p->ta_buckets;
         bucket_p < table_p->ta_buckets + table_p->ta_bucket_n;
         bucket_p++) {

        /* skip null buckets */
        if (*bucket_p == NULL)
            continue;
        /* run through the entry list */
        last_p = NULL;
        for (pos = *(unsigned long *) bucket_p;;
             pos = (unsigned long) entry_p->te_next_p) {

            /* read in the entry */
            if (fseek(infile, pos, SEEK_SET) != 0) {
                if (error_p != NULL)
                    *error_p = TABLE_ERROR_SEEK;
                table_p->ta_free(table_p->ta_buckets);
                if (entry_p != NULL)
                    table_p->ta_free(entry_p);
                table_p->ta_free(table_p);
                /* the other table elements will not be freed */
                return NULL;
            }
            if (fread(&entry, sizeof(struct table_shell_st), 1, infile) != 1) {
                if (error_p != NULL)
                    *error_p = TABLE_ERROR_READ;
                table_p->ta_free(table_p->ta_buckets);
                if (entry_p != NULL)
                    table_p->ta_free(entry_p);
                table_p->ta_free(table_p);
                /* the other table elements will not be freed */
                return NULL;
            }

            /* make a new entry */
            ent_size = entry_size(table_p, entry.te_key_size, entry.te_data_size);
            entry_p = (table_entry_t *)table_p->ta_malloc(ent_size);
            if (entry_p == NULL) {
                if (error_p != NULL)
                    *error_p = TABLE_ERROR_ALLOC;
                table_p->ta_free(table_p->ta_buckets);
                table_p->ta_free(table_p);
                /* the other table elements will not be freed */
                return NULL;
            }
            entry_p->te_key_size = entry.te_key_size;
            entry_p->te_data_size = entry.te_data_size;
            entry_p->te_next_p = entry.te_next_p;

            if (last_p == NULL)
                *bucket_p = entry_p;
            else
                last_p->te_next_p = entry_p;
            /* determine how much more we have to read */
            size = ent_size - sizeof(struct table_shell_st);
            if (fread(ENTRY_KEY_BUF(entry_p), sizeof(char), size, infile) != size) {
                if (error_p != NULL)
                    *error_p = TABLE_ERROR_READ;
                table_p->ta_free(table_p->ta_buckets);
                table_p->ta_free(entry_p);
                table_p->ta_free(table_p);
                /* the other table elements will not be freed */
                return NULL;
            }

            /* we are done if the next pointer is null */
            if (entry_p->te_next_p == (unsigned long) 0)
                break;
            last_p = entry_p;
        }
    }

    (void) fclose(infile);

    if (error_p != NULL)
        *error_p = TABLE_ERROR_NONE;
    return table_p;
}

/*
 * int table_write
 *
 * DESCRIPTION:
 *
 * Write a table from memory to file.
 *
 * RETURNS:
 *
 * Success - TABLE_ERROR_NONE
 *
 * Failure - Table error code.
 *
 * ARGUMENTS:
 *
 * table_p - Pointer to the table that we are writing to the file.
 *
 * path - Table file to write out to.
 *
 * mode - Mode of the file.  This argument is passed on to open when
 * the file is created.
 */
int table_write(const table_t * table_p, const char *path, const int mode)
{
    int fd, rem, ent_size;
    unsigned int bucket_c;
    unsigned long size;
    table_entry_t *entry_p, **buckets, **bucket_p, *next_p;
    table_t tmain;
    FILE *outfile;

    if (table_p == NULL)
        return TABLE_ERROR_ARG_NULL;
    if (table_p->ta_magic != TABLE_MAGIC)
        return TABLE_ERROR_PNT;
    fd = open(path, O_WRONLY | O_CREAT, mode);
    if (fd < 0)
        return TABLE_ERROR_OPEN;
    outfile = fdopen(fd, "w");
    if (outfile == NULL)
        return TABLE_ERROR_OPEN;
    /* allocate a block of sizes for each bucket */
    buckets = (table_entry_t **) table_p->ta_malloc(sizeof(table_entry_t *) *
                                        table_p->ta_bucket_n);
    if (buckets == NULL)
        return TABLE_ERROR_ALLOC;
    /* make a copy of the tmain struct */
    tmain = *table_p;

    /* start counting the bytes */
    size = 0;
    size += sizeof(table_t);

    /* buckets go right after tmain struct */
    tmain.ta_buckets = (table_entry_t **) size;
    size += sizeof(table_entry_t *) * table_p->ta_bucket_n;

    /* run through and count the buckets */
    for (bucket_c = 0; bucket_c < table_p->ta_bucket_n; bucket_c++) {
        bucket_p = table_p->ta_buckets + bucket_c;
        if (*bucket_p == NULL) {
            buckets[bucket_c] = NULL;
            continue;
        }
        buckets[bucket_c] = (table_entry_t *) size;
        for (entry_p = *bucket_p; entry_p != NULL; entry_p = entry_p->te_next_p) {
            size += entry_size(table_p, entry_p->te_key_size, entry_p->te_data_size);
            /*
             * We now have to round the file to the nearest long so the
             * mmaping of the longs in the entry structs will work.
             */
            rem = size & (sizeof(long) - 1);
            if (rem > 0)
                size += sizeof(long) - rem;
        }
    }
    /* add a \0 at the end to fill the last section */
    size++;

    /* set the tmain fields */
    tmain.ta_linear.tl_magic = 0;
    tmain.ta_linear.tl_bucket_c = 0;
    tmain.ta_linear.tl_entry_c = 0;
    tmain.ta_file_size = size;

    /*
     * Now we can start the writing because we got the bucket offsets.
     */

    /* write the tmain table struct */
    size = 0;
    if (fwrite(&tmain, sizeof(table_t), 1, outfile) != 1) {
        table_p->ta_free(buckets);
        return TABLE_ERROR_WRITE;
    }
    size += sizeof(table_t);
    if (fwrite(buckets, sizeof(table_entry_t *), table_p->ta_bucket_n,
               outfile) != (size_t) table_p->ta_bucket_n) {
        table_p->ta_free(buckets);
        return TABLE_ERROR_WRITE;
    }
    size += sizeof(table_entry_t *) * table_p->ta_bucket_n;

    /* write out the entries */
    for (bucket_p = table_p->ta_buckets;
         bucket_p < table_p->ta_buckets + table_p->ta_bucket_n;
         bucket_p++) {
        for (entry_p = *bucket_p; entry_p != NULL; entry_p = entry_p->te_next_p) {

            ent_size = entry_size(table_p, entry_p->te_key_size,
                                  entry_p->te_data_size);
            size += ent_size;
            /* round to nearest long here so we can write copy */
            rem = size & (sizeof(long) - 1);
            if (rem > 0)
                size += sizeof(long) - rem;
            next_p = entry_p->te_next_p;
            if (next_p != NULL)
                entry_p->te_next_p = (table_entry_t *) size;
            /* now write to disk */
            if (fwrite(entry_p, ent_size, 1, outfile) != 1) {
                table_p->ta_free(buckets);
                return TABLE_ERROR_WRITE;
            }

            /* restore the next pointer */
            if (next_p != NULL)
                entry_p->te_next_p = next_p;
            /* now write the padding information */
            if (rem > 0) {
                rem = sizeof(long) - rem;
                /*
                 * NOTE: this won't leave fseek'd space at the end but we
                 * don't care there because there is no accessed memory
                 * afterwards.  We write 1 \0 at the end to make sure.
                 */
                if (fseek(outfile, rem, SEEK_CUR) != 0) {
                    table_p->ta_free(buckets);
                    return TABLE_ERROR_SEEK;
                }
            }
        }
    }
    /*
     * Write a \0 at the end of the file to make sure that the last
     * fseek filled with nulls.
     */
    (void) fputc('\0', outfile);

    (void) fclose(outfile);
    table_p->ta_free(buckets);

    return TABLE_ERROR_NONE;
}

/******************************** table order ********************************/

/*
@@ -2867,5 +2516,3 @@ int table_entry_info(table_t * table_p, table_entry_t * entry_p,
    return TABLE_ERROR_NONE;
}
#endif /* XXX */
+0 −6
Original line number Diff line number Diff line
@@ -83,8 +83,6 @@
#ifndef __SSL_UTIL_TABLE_H__
#define __SSL_UTIL_TABLE_H__

#if 0 /* XXX */

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
@@ -177,8 +175,6 @@ extern int table_this(table_t *table_p, void **key_buf_p, int *key_s
extern int             table_first_r(table_t *table_p, table_linear_t *linear_p, void **key_buf_p, int *key_size_p, void **data_buf_p, int *data_size_p);
extern int             table_next_r(table_t *table_p, table_linear_t *linear_p, void **key_buf_p, int *key_size_p, void **data_buf_p, int *data_size_p);
extern int             table_this_r(table_t *table_p, table_linear_t *linear_p, void **key_buf_p, int *key_size_p, void **data_buf_p, int *data_size_p);
extern table_t        *table_read(const char *path, int *error_p, void *(*malloc_f)(size_t size), void *(*calloc_f)(size_t number, size_t size), void *(*realloc_f)(void *ptr, size_t size), void (*free_f)(void *ptr));
extern int             table_write(const table_t *table_p, const char *path, const int mode);
extern table_entry_t **table_order(table_t *table_p, table_compare_t compare, int *num_entries_p, int *error_p);
extern int             table_entry_info(table_t *table_p, table_entry_t *entry_p, void **key_buf_p, int *key_size_p, void **data_buf_p, int *data_size_p);

@@ -186,6 +182,4 @@ extern int table_entry_info(table_t *table_p, table_entry_t *entry_p
}
#endif /* __cplusplus */

#endif /* XXX */

#endif /* __SSL_UTIL_TABLE_H__ */