misc/libfreetype/src/gzip/zutil.c
author nemo
Sun, 11 Sep 2011 10:46:53 -0400
changeset 5856 ed97138dc414
parent 5172 88f2e05288ba
permissions -rw-r--r--
Should prevent a crasher when drowning while firing
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5172
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     1
/* zutil.c -- target dependent utility functions for the compression library
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     2
 * Copyright (C) 1995-2002 Jean-loup Gailly.
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     3
 * For conditions of distribution and use, see copyright notice in zlib.h
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     4
 */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     5
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     6
/* @(#) $Id$ */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     7
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     8
#include "zutil.h"
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     9
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    10
#ifndef STDC
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    11
extern void exit OF((int));
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    12
#endif
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    13
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    14
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    15
#ifndef HAVE_MEMCPY
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    16
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    17
void zmemcpy(dest, source, len)
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    18
    Bytef* dest;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    19
    const Bytef* source;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    20
    uInt  len;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    21
{
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    22
    if (len == 0) return;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    23
    do {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    24
        *dest++ = *source++; /* ??? to be unrolled */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    25
    } while (--len != 0);
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    26
}
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    27
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    28
int zmemcmp(s1, s2, len)
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    29
    const Bytef* s1;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    30
    const Bytef* s2;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    31
    uInt  len;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    32
{
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    33
    uInt j;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    34
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    35
    for (j = 0; j < len; j++) {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    36
        if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    37
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    38
    return 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    39
}
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    40
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    41
void zmemzero(dest, len)
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    42
    Bytef* dest;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    43
    uInt  len;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    44
{
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    45
    if (len == 0) return;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    46
    do {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    47
        *dest++ = 0;  /* ??? to be unrolled */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    48
    } while (--len != 0);
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    49
}
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    50
#endif
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    51
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    52
#if defined( MSDOS ) && defined( __TURBOC__ ) && !defined( MY_ZCALLOC )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    53
#if (defined( __BORLANDC__) || !defined(SMALL_MEDIUM)) && !defined(__32BIT__)
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    54
/* Small and medium model in Turbo C are for now limited to near allocation
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    55
 * with reduced MAX_WBITS and MAX_MEM_LEVEL
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    56
 */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    57
#  define MY_ZCALLOC
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    58
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    59
/* Turbo C malloc() does not allow dynamic allocation of 64K bytes
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    60
 * and farmalloc(64K) returns a pointer with an offset of 8, so we
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    61
 * must fix the pointer. Warning: the pointer must be put back to its
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    62
 * original form in order to free it, use zcfree().
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    63
 */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    64
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    65
#define MAX_PTR 10
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    66
/* 10*64K = 640K */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    67
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    68
local int next_ptr = 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    69
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    70
typedef struct ptr_table_s {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    71
    voidpf org_ptr;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    72
    voidpf new_ptr;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    73
} ptr_table;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    74
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    75
local ptr_table table[MAX_PTR];
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    76
/* This table is used to remember the original form of pointers
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    77
 * to large buffers (64K). Such pointers are normalized with a zero offset.
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    78
 * Since MSDOS is not a preemptive multitasking OS, this table is not
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    79
 * protected from concurrent access. This hack doesn't work anyway on
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    80
 * a protected system like OS/2. Use Microsoft C instead.
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    81
 */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    82
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    83
voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    84
{
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    85
    voidpf buf = opaque; /* just to make some compilers happy */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    86
    ulg bsize = (ulg)items*size;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    87
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    88
    /* If we allocate less than 65520 bytes, we assume that farmalloc
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    89
     * will return a usable pointer which doesn't have to be normalized.
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    90
     */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    91
    if (bsize < 65520L) {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    92
        buf = farmalloc(bsize);
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    93
        if (*(ush*)&buf != 0) return buf;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    94
    } else {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    95
        buf = farmalloc(bsize + 16L);
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    96
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    97
    if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    98
    table[next_ptr].org_ptr = buf;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    99
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   100
    /* Normalize the pointer to seg:0 */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   101
    *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   102
    *(ush*)&buf = 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   103
    table[next_ptr++].new_ptr = buf;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   104
    return buf;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   105
}
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   106
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   107
void  zcfree (voidpf opaque, voidpf ptr)
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   108
{
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   109
    int n;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   110
    if (*(ush*)&ptr != 0) { /* object < 64K */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   111
        farfree(ptr);
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   112
        return;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   113
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   114
    /* Find the original pointer */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   115
    for (n = 0; n < next_ptr; n++) {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   116
        if (ptr != table[n].new_ptr) continue;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   117
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   118
        farfree(table[n].org_ptr);
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   119
        while (++n < next_ptr) {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   120
            table[n-1] = table[n];
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   121
        }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   122
        next_ptr--;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   123
        return;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   124
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   125
    ptr = opaque; /* just to make some compilers happy */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   126
    Assert(0, "zcfree: ptr not found");
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   127
}
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   128
#endif
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   129
#endif /* MSDOS && __TURBOC__ */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   130
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   131
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   132
#if defined(M_I86) && !defined(__32BIT__) && !defined( MY_ZCALLOC )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   133
/* Microsoft C in 16-bit mode */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   134
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   135
#  define MY_ZCALLOC
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   136
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   137
#if (!defined(_MSC_VER) || (_MSC_VER <= 600))
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   138
#  define _halloc  halloc
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   139
#  define _hfree   hfree
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   140
#endif
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   141
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   142
voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   143
{
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   144
    if (opaque) opaque = 0; /* to make compiler happy */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   145
    return _halloc((long)items, size);
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   146
}
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   147
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   148
void  zcfree (voidpf opaque, voidpf ptr)
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   149
{
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   150
    if (opaque) opaque = 0; /* to make compiler happy */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   151
    _hfree(ptr);
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   152
}
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   153
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   154
#endif /* MSC */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   155
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   156
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   157
#ifndef MY_ZCALLOC /* Any system without a special alloc function */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   158
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   159
#ifndef STDC
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   160
extern voidp  ft_scalloc OF((uInt items, uInt size));
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   161
extern void   ft_sfree   OF((voidpf ptr));
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   162
#endif
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   163
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   164
voidpf zcalloc (opaque, items, size)
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   165
    voidpf opaque;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   166
    unsigned items;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   167
    unsigned size;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   168
{
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   169
    if (opaque) items += size - size; /* make compiler happy */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   170
    return (voidpf)ft_scalloc(items, size);
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   171
}
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   172
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   173
void  zcfree (opaque, ptr)
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   174
    voidpf opaque;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   175
    voidpf ptr;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   176
{
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   177
    ft_sfree(ptr);
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   178
    if (opaque) return; /* make compiler happy */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   179
}
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   180
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   181
#endif /* MY_ZCALLOC */