xref: /plan9/sys/src/cmd/gs/src/szlibc.c (revision 593dc095aefb2a85c828727bbfa9da139a49bdf4)
1 /* Copyright (C) 1995, 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
2 
3   This software is provided AS-IS with no warranty, either express or
4   implied.
5 
6   This software is distributed under license and may not be copied,
7   modified or distributed except as expressly authorized under the terms
8   of the license contained in the file LICENSE in this distribution.
9 
10   For more information about licensing, please refer to
11   http://www.ghostscript.com/licensing/. For information on
12   commercial licensing, go to http://www.artifex.com/licensing/ or
13   contact Artifex Software, Inc., 101 Lucas Valley Road #110,
14   San Rafael, CA  94903, U.S.A., +1(415)492-9861.
15 */
16 
17 /* $Id: szlibc.c,v 1.6 2004/08/04 19:36:13 stefan Exp $ */
18 /* Code common to zlib encoding and decoding streams */
19 #include "std.h"
20 #include "gserror.h"
21 #include "gserrors.h"
22 #include "gstypes.h"
23 #include "gsmemory.h"
24 #include "gsmalloc.h"
25 #include "gsstruct.h"
26 #include "strimpl.h"
27 #include "szlibxx.h"
28 #include "zconf.h"
29 
30 private_st_zlib_block();
31 private_st_zlib_dynamic_state();
32 public_st_zlib_state();
33 
34 /* Set defaults for stream parameters. */
35 void
s_zlib_set_defaults(stream_state * st)36 s_zlib_set_defaults(stream_state * st)
37 {
38     stream_zlib_state *const ss = (stream_zlib_state *)st;
39 
40     ss->windowBits = MAX_WBITS;
41     ss->no_wrapper = false;
42     ss->level = Z_DEFAULT_COMPRESSION;
43     ss->method = Z_DEFLATED;
44     /* DEF_MEM_LEVEL should be in zlib.h or zconf.h, but it isn't. */
45     ss->memLevel = min(MAX_MEM_LEVEL, 8);
46     ss->strategy = Z_DEFAULT_STRATEGY;
47     /* Clear pointers */
48     ss->dynamic = 0;
49 }
50 
51 /* Allocate the dynamic state. */
52 int
s_zlib_alloc_dynamic_state(stream_zlib_state * ss)53 s_zlib_alloc_dynamic_state(stream_zlib_state *ss)
54 {
55     gs_memory_t *mem = ss->memory;
56     zlib_dynamic_state_t *zds =
57 	gs_alloc_struct_immovable(mem, zlib_dynamic_state_t,
58 				  &st_zlib_dynamic_state,
59 				  "s_zlib_alloc_dynamic_state");
60 
61     ss->dynamic = zds;
62     if (zds == 0)
63 	return_error(gs_error_VMerror);
64     zds->blocks = 0;
65     zds->memory = mem;
66     zds->zstate.zalloc = (alloc_func)s_zlib_alloc;
67     zds->zstate.zfree = (free_func)s_zlib_free;
68     zds->zstate.opaque = (voidpf)zds;
69     return 0;
70 }
71 
72 /* Free the dynamic state. */
73 void
s_zlib_free_dynamic_state(stream_zlib_state * ss)74 s_zlib_free_dynamic_state(stream_zlib_state *ss)
75 {
76     if (ss->dynamic)
77 	gs_free_object(ss->dynamic->memory, ss->dynamic,
78 		       "s_zlib_free_dynamic_state");
79 }
80 
81 /* Provide zlib-compatible allocation and freeing functions. */
82 void *
s_zlib_alloc(void * zmem,uint items,uint size)83 s_zlib_alloc(void *zmem, uint items, uint size)
84 {
85     zlib_dynamic_state_t *const zds = zmem;
86     gs_memory_t *mem = zds->memory->stable_memory;
87     zlib_block_t *block =
88 	gs_alloc_struct(mem, zlib_block_t, &st_zlib_block,
89 			"s_zlib_alloc(block)");
90     void *data =
91 	gs_alloc_byte_array_immovable(mem, items, size, "s_zlib_alloc(data)");
92 
93     if (block == 0 || data == 0) {
94 	gs_free_object(mem, data, "s_zlib_alloc(data)");
95 	gs_free_object(mem, block, "s_zlib_alloc(block)");
96 	return Z_NULL;
97     }
98     block->data = data;
99     block->next = zds->blocks;
100     block->prev = 0;
101     if (zds->blocks)
102 	zds->blocks->prev = block;
103     zds->blocks = block;
104     return data;
105 }
106 void
s_zlib_free(void * zmem,void * data)107 s_zlib_free(void *zmem, void *data)
108 {
109     zlib_dynamic_state_t *const zds = zmem;
110     gs_memory_t *mem = zds->memory->stable_memory;
111     zlib_block_t *block = zds->blocks;
112 
113     gs_free_object(mem, data, "s_zlib_free(data)");
114     for (; ; block = block->next) {
115 	if (block == 0) {
116 	    lprintf1("Freeing unrecorded data 0x%lx!\n", (ulong)data);
117 	    return;
118 	}
119 	if (block->data == data)
120 	    break;
121     }
122     if (block->next)
123 	block->next->prev = block->prev;
124     if (block->prev)
125 	block->prev->next = block->next;
126     else
127 	zds->blocks = block->next;
128     gs_free_object(mem, block, "s_zlib_free(block)");
129 }
130