OSDN Git Service

Merge from feature_merge branch. Build TortoiseMerge successfully.
[tortoisegit/TortoiseGitJp.git] / src / TortoiseMerge / libsvn_diff / zutil.c
1 /* zutil.c -- target dependent utility functions for the compression library\r
2  * Copyright (C) 1995-2002 Jean-loup Gailly.\r
3  * For conditions of distribution and use, see copyright notice in zlib.h \r
4  */\r
5 \r
6 /* @(#) $Id: zutil.c,v 1.1 2003/05/23 21:05:30 steveking Exp $ */\r
7 \r
8 #include "zutil.h"\r
9 \r
10 struct internal_state      {int dummy;}; /* for buggy compilers */\r
11 \r
12 #ifndef STDC\r
13 extern void exit OF((int));\r
14 #endif\r
15 \r
16 const char *z_errmsg[10] = {\r
17 "need dictionary",     /* Z_NEED_DICT       2  */\r
18 "stream end",          /* Z_STREAM_END      1  */\r
19 "",                    /* Z_OK              0  */\r
20 "file error",          /* Z_ERRNO         (-1) */\r
21 "stream error",        /* Z_STREAM_ERROR  (-2) */\r
22 "data error",          /* Z_DATA_ERROR    (-3) */\r
23 "insufficient memory", /* Z_MEM_ERROR     (-4) */\r
24 "buffer error",        /* Z_BUF_ERROR     (-5) */\r
25 "incompatible version",/* Z_VERSION_ERROR (-6) */\r
26 ""};\r
27 \r
28 \r
29 const char * ZEXPORT zlibVersion()\r
30 {\r
31     return ZLIB_VERSION;\r
32 }\r
33 \r
34 #ifdef DEBUG\r
35 \r
36 #  ifndef verbose\r
37 #    define verbose 0\r
38 #  endif\r
39 int z_verbose = verbose;\r
40 \r
41 void z_error (m)\r
42     char *m;\r
43 {\r
44     fprintf(stderr, "%s\n", m);\r
45     exit(1);\r
46 }\r
47 #endif\r
48 \r
49 /* exported to allow conversion of error code to string for compress() and\r
50  * uncompress()\r
51  */\r
52 const char * ZEXPORT zError(err)\r
53     int err;\r
54 {\r
55     return ERR_MSG(err);\r
56 }\r
57 \r
58 \r
59 #ifndef HAVE_MEMCPY\r
60 \r
61 void zmemcpy(dest, source, len)\r
62     Bytef* dest;\r
63     const Bytef* source;\r
64     uInt  len;\r
65 {\r
66     if (len == 0) return;\r
67     do {\r
68         *dest++ = *source++; /* ??? to be unrolled */\r
69     } while (--len != 0);\r
70 }\r
71 \r
72 int zmemcmp(s1, s2, len)\r
73     const Bytef* s1;\r
74     const Bytef* s2;\r
75     uInt  len;\r
76 {\r
77     uInt j;\r
78 \r
79     for (j = 0; j < len; j++) {\r
80         if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;\r
81     }\r
82     return 0;\r
83 }\r
84 \r
85 void zmemzero(dest, len)\r
86     Bytef* dest;\r
87     uInt  len;\r
88 {\r
89     if (len == 0) return;\r
90     do {\r
91         *dest++ = 0;  /* ??? to be unrolled */\r
92     } while (--len != 0);\r
93 }\r
94 #endif\r
95 \r
96 #ifdef __TURBOC__\r
97 #if (defined( __BORLANDC__) || !defined(SMALL_MEDIUM)) && !defined(__32BIT__)\r
98 /* Small and medium model in Turbo C are for now limited to near allocation\r
99  * with reduced MAX_WBITS and MAX_MEM_LEVEL\r
100  */\r
101 #  define MY_ZCALLOC\r
102 \r
103 /* Turbo C malloc() does not allow dynamic allocation of 64K bytes\r
104  * and farmalloc(64K) returns a pointer with an offset of 8, so we\r
105  * must fix the pointer. Warning: the pointer must be put back to its\r
106  * original form in order to free it, use zcfree().\r
107  */\r
108 \r
109 #define MAX_PTR 10\r
110 /* 10*64K = 640K */\r
111 \r
112 local int next_ptr = 0;\r
113 \r
114 typedef struct ptr_table_s {\r
115     voidpf org_ptr;\r
116     voidpf new_ptr;\r
117 } ptr_table;\r
118 \r
119 local ptr_table table[MAX_PTR];\r
120 /* This table is used to remember the original form of pointers\r
121  * to large buffers (64K). Such pointers are normalized with a zero offset.\r
122  * Since MSDOS is not a preemptive multitasking OS, this table is not\r
123  * protected from concurrent access. This hack doesn't work anyway on\r
124  * a protected system like OS/2. Use Microsoft C instead.\r
125  */\r
126 \r
127 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)\r
128 {\r
129     voidpf buf = opaque; /* just to make some compilers happy */\r
130     ulg bsize = (ulg)items*size;\r
131 \r
132     /* If we allocate less than 65520 bytes, we assume that farmalloc\r
133      * will return a usable pointer which doesn't have to be normalized.\r
134      */\r
135     if (bsize < 65520L) {\r
136         buf = farmalloc(bsize);\r
137         if (*(ush*)&buf != 0) return buf;\r
138     } else {\r
139         buf = farmalloc(bsize + 16L);\r
140     }\r
141     if (buf == NULL || next_ptr >= MAX_PTR) return NULL;\r
142     table[next_ptr].org_ptr = buf;\r
143 \r
144     /* Normalize the pointer to seg:0 */\r
145     *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;\r
146     *(ush*)&buf = 0;\r
147     table[next_ptr++].new_ptr = buf;\r
148     return buf;\r
149 }\r
150 \r
151 void  zcfree (voidpf opaque, voidpf ptr)\r
152 {\r
153     int n;\r
154     if (*(ush*)&ptr != 0) { /* object < 64K */\r
155         farfree(ptr);\r
156         return;\r
157     }\r
158     /* Find the original pointer */\r
159     for (n = 0; n < next_ptr; n++) {\r
160         if (ptr != table[n].new_ptr) continue;\r
161 \r
162         farfree(table[n].org_ptr);\r
163         while (++n < next_ptr) {\r
164             table[n-1] = table[n];\r
165         }\r
166         next_ptr--;\r
167         return;\r
168     }\r
169     ptr = opaque; /* just to make some compilers happy */\r
170     Assert(0, "zcfree: ptr not found");\r
171 }\r
172 #endif\r
173 #endif /* __TURBOC__ */\r
174 \r
175 \r
176 #if defined(M_I86) && !defined(__32BIT__)\r
177 /* Microsoft C in 16-bit mode */\r
178 \r
179 #  define MY_ZCALLOC\r
180 \r
181 #if (!defined(_MSC_VER) || (_MSC_VER <= 600))\r
182 #  define _halloc  halloc\r
183 #  define _hfree   hfree\r
184 #endif\r
185 \r
186 voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)\r
187 {\r
188     if (opaque) opaque = 0; /* to make compiler happy */\r
189     return _halloc((long)items, size);\r
190 }\r
191 \r
192 void  zcfree (voidpf opaque, voidpf ptr)\r
193 {\r
194     if (opaque) opaque = 0; /* to make compiler happy */\r
195     _hfree(ptr);\r
196 }\r
197 \r
198 #endif /* MSC */\r
199 \r
200 \r
201 #ifndef MY_ZCALLOC /* Any system without a special alloc function */\r
202 \r
203 #ifndef STDC\r
204 extern voidp  calloc OF((uInt items, uInt size));\r
205 extern void   free   OF((voidpf ptr));\r
206 #endif\r
207 \r
208 voidpf zcalloc (opaque, items, size)\r
209     voidpf opaque;\r
210     unsigned items;\r
211     unsigned size;\r
212 {\r
213     if (opaque) items += size - size; /* make compiler happy */\r
214     return (voidpf)calloc(items, size);\r
215 }\r
216 \r
217 void  zcfree (opaque, ptr)\r
218     voidpf opaque;\r
219     voidpf ptr;\r
220 {\r
221     free(ptr);\r
222     if (opaque) return; /* make compiler happy */\r
223 }\r
224 \r
225 #endif /* MY_ZCALLOC */\r