1 /* Copyright (C) 2008, 2009 Vincent Penquerc'h.
2 This file is part of the Kate codec library.
3 Written by Vincent Penquerc'h.
4
5 Use, distribution and reproduction of this library is governed
6 by a BSD style source license included with this source in the
7 file 'COPYING'. Please read these terms before distributing. */
8
9
10 #define KATE_INTERNAL
11 #include "kate_internal.h"
12
13 #ifdef HAVE_STDLIB_H
14 #include <stdlib.h>
15 #endif
16 #ifdef HAVE_STRING_H
17 #include <string.h>
18 #endif
19 #include "kate/kate.h"
20 #include "kate_decode_state.h"
21 #include "kate_encode_state.h"
22
23 #define MKVER1(x) #x
24 #define MKVER2(x,y) MKVER1(x) "." MKVER1(y)
25 #define MKVER3(x,y,z) MKVER1(x)"."MKVER1(y)"."MKVER1(z)
26
27 /**
28 \ingroup version
29 Returns the version number of this version of libkate, in 8.8.8 major.minor.patch format
30 \returns the version number of libkate
31 */
kate_get_version(void)32 int kate_get_version(void)
33 {
34 return (KATE_VERSION_MAJOR<<16)|(KATE_VERSION_MINOR<<8)|KATE_VERSION_PATCH;
35 }
36
37 /**
38 \ingroup version
39 Returns a human readable string representing the version number of this version of libkate
40 \returns the version number of libkate as a string
41 */
kate_get_version_string(void)42 const char *kate_get_version_string(void)
43 {
44 return "libkate " MKVER3(KATE_VERSION_MAJOR,KATE_VERSION_MINOR,KATE_VERSION_PATCH) " (Tiger)";
45 }
46
47 /**
48 \ingroup version
49 Returns the highest bitstream version number that this version of libkate supports, in 8.8 major.minor format
50 \returns the bitstream version number
51 */
kate_get_bitstream_version(void)52 int kate_get_bitstream_version(void)
53 {
54 return (KATE_BITSTREAM_VERSION_MAJOR<<8)|(KATE_BITSTREAM_VERSION_MINOR);
55 }
56
57 /**
58 \ingroup version
59 Returns a string representing the highest bitstream version number that this version of libkate supports
60 \returns the bitstream version number as a string
61 */
kate_get_bitstream_version_string(void)62 const char *kate_get_bitstream_version_string(void)
63 {
64 return "kate bitstream " MKVER2(KATE_BITSTREAM_VERSION_MAJOR,KATE_BITSTREAM_VERSION_MINOR);
65 }
66
67 /**
68 Destroys a kate_state structure.
69 The kate_state structure should have been initialized with kate_decode_init or kate_encode_init.
70 \param k the kate_state structure to clear
71 \returns 0 success
72 \returns KATE_E_* error
73 */
kate_clear(kate_state * k)74 int kate_clear(kate_state *k)
75 {
76 if (!k) return KATE_E_INVALID_PARAMETER;
77
78 if (k->kds) {
79 kate_decode_state_destroy(k->kds);
80 k->kds=NULL;
81 }
82 if (k->kes) {
83 kate_encode_state_destroy(k->kes);
84 k->kes=NULL;
85 }
86
87 return 0;
88 }
89
90 static inline int kate_ascii_tolower(int c) __attribute__((const));
kate_ascii_tolower(int c)91 static inline int kate_ascii_tolower(int c)
92 {
93 if (c>='A' && c<='Z') return c|32;
94 return c;
95 }
96
kate_ascii_strncasecmp(const char * s0,const char * s1,size_t n)97 int kate_ascii_strncasecmp(const char *s0,const char *s1,size_t n)
98 {
99 while (n--) {
100 int c0=kate_ascii_tolower(*s0++);
101 int c1=kate_ascii_tolower(*s1++);
102 if (c0!=c1) return c0-c1;
103 if (!c0) return 0;
104 }
105 return 0;
106 }
107
kate_ascii_strcasecmp(const char * s0,const char * s1)108 int kate_ascii_strcasecmp(const char *s0,const char *s1)
109 {
110 return kate_ascii_strncasecmp(s0,s1,(size_t)-1);
111 }
112
113 /**
114 \ingroup misc
115 Initializes a kate region to sensible defaults
116 \param kr the region to initialize
117 \returns 0 success
118 \returns KATE_E_* error
119 */
kate_region_init(kate_region * kr)120 int kate_region_init(kate_region *kr)
121 {
122 static const kate_region default_region={
123 kate_percentage,
124 10,80,80,10,
125 -1,
126 0,
127 0,
128 NULL,
129 {0,0,0,0,0}
130 };
131
132 if (!kr) return KATE_E_INVALID_PARAMETER;
133 memcpy(kr,&default_region,sizeof(kate_region));
134 return 0;
135 }
136
137 /**
138 \ingroup misc
139 Initializes a kate style to sensible defaults
140 \param ks the style to initialize
141 \returns 0 success
142 \returns KATE_E_* error
143 */
kate_style_init(kate_style * ks)144 int kate_style_init(kate_style *ks)
145 {
146 static const kate_style default_style={
147 0,0,
148 {255,255,255,255},
149 {0,0,0,0},
150 {255,255,255,255},
151 kate_pixel,
152 -1,-1,
153 kate_pixel,
154 0,0,0,0,
155 0,
156 0,
157 0,
158 0,
159 0,
160 kate_wrap_word,
161 0,
162 NULL,
163 NULL,
164 {0,0,0,0,0,0,0,0}
165 };
166
167 if (!ks) return KATE_E_INVALID_PARAMETER;
168 memcpy(ks,&default_style,sizeof(kate_style));
169 return 0;
170 }
171
172 /**
173 \ingroup misc
174 Initializes a kate palette to sensible defaults
175 \param kp the palette to initialize
176 \returns 0 success
177 \returns KATE_E_* error
178 */
kate_palette_init(kate_palette * kp)179 int kate_palette_init(kate_palette *kp)
180 {
181 static const kate_palette default_palette={
182 0,
183 NULL,
184 NULL,
185 {0}
186 };
187
188 if (!kp) return KATE_E_INVALID_PARAMETER;
189 memcpy(kp,&default_palette,sizeof(kate_palette));
190 return 0;
191 }
192
193 /**
194 \ingroup misc
195 Initializes a kate bitmap to sensible defaults. Obsolete, use kate_bitmap_init_new in new code.
196 \param kb the bitmap to initialize
197 \returns 0 success
198 \returns KATE_E_* error
199 */
kate_bitmap_init(kate_bitmap * kb)200 int kate_bitmap_init(kate_bitmap *kb)
201 {
202 typedef struct kate_bitmap_old_equivalent {
203 size_t width; /**< width in pixels */
204 size_t height; /**< height in pixels */
205 unsigned char bpp; /**< bits per pixel, from 1 to 8, or 0 for a raw PNG bitmap */
206 kate_bitmap_type type; /**< the type of this bitmap */
207 unsigned char pad0[1];
208 unsigned char internal;
209 int palette; /**< index of the default palette to use */
210 unsigned char *pixels; /**< pixels, rows first, one byte per pixel regardless of bpp */
211 size_t size; /**< for raw bitmaps, number of bytes in pixels */
212 int x_offset; /**< the horizontal offset to the logical origin of the bitmap */
213 int y_offset; /**< the vertical offset to the logical origin of the bitmap */
214 } kate_bitmap_old_equivalent;
215 static const kate_bitmap_old_equivalent default_bitmap={
216 0,0,
217 0,
218 kate_bitmap_type_png,
219 {0},
220 0, /* internal flag - meta and later not initialized */
221 -1,
222 NULL,
223 0,
224 0,0,
225 };
226
227 if (!kb) return KATE_E_INVALID_PARAMETER;
228 memcpy(kb,&default_bitmap,sizeof(kate_bitmap_old_equivalent));
229 return 0;
230 }
231
232 /**
233 \ingroup misc
234 Initializes a kate bitmap to sensible defaults
235 \param kb the bitmap to initialize
236 \returns 0 success
237 \returns KATE_E_* error
238 */
kate_bitmap_init_new(kate_bitmap * kb)239 int kate_bitmap_init_new(kate_bitmap *kb)
240 {
241 static const kate_bitmap default_bitmap={
242 0,0,
243 0,
244 kate_bitmap_type_png,
245 {0},
246 1, /* internal flag - all initialized */
247 -1,
248 NULL,
249 0,
250 0,0,
251 NULL,
252 {0,0,0,0,0,0,0,0,0,0,0,0,0,0}
253 };
254
255 if (!kb) return KATE_E_INVALID_PARAMETER;
256 memcpy(kb,&default_bitmap,sizeof(kate_bitmap));
257 return 0;
258 }
259
260 /**
261 \ingroup misc
262 Initializes a kate curve to sensible defaults
263 \param kc the curve to initialize
264 \returns 0 success
265 \returns KATE_E_* error
266 */
kate_curve_init(kate_curve * kc)267 int kate_curve_init(kate_curve *kc)
268 {
269 static const kate_curve default_curve={
270 kate_curve_none,
271 0,
272 NULL,
273 {0,0,0,0,0}
274 };
275
276 if (!kc) return KATE_E_INVALID_PARAMETER;
277 memcpy(kc,&default_curve,sizeof(kate_curve));
278 return 0;
279 }
280
281 /**
282 \ingroup misc
283 Initializes a kate motion to sensible defaults
284 \param km the motion to initialize
285 \returns 0 success
286 \returns KATE_E_* error
287 */
kate_motion_init(kate_motion * km)288 int kate_motion_init(kate_motion *km)
289 {
290 static const kate_motion default_motion={
291 0,
292 NULL,
293 NULL,
294 kate_motion_mapping_none,
295 kate_motion_mapping_none,
296 kate_motion_semantics_time,
297 0,
298 0,
299 NULL,
300 {0,0,0,0}
301 };
302
303 if (!km) return KATE_E_INVALID_PARAMETER;
304 memcpy(km,&default_motion,sizeof(kate_motion));
305 return 0;
306 }
307
kate_checked_malloc(size_t n,size_t sz)308 void *kate_checked_malloc(size_t n,size_t sz)
309 {
310 size_t mul;
311 if (kate_check_mul_overflow(n,sz,&mul)) return NULL;
312 return kate_malloc(mul);
313 }
314
kate_checked_realloc(void * ptr,size_t n,size_t sz)315 void *kate_checked_realloc(void *ptr,size_t n,size_t sz)
316 {
317 size_t mul;
318 if (kate_check_mul_overflow(n,sz,&mul)) return NULL;
319 return kate_realloc(ptr,mul);
320 }
321
322