Coverage Report

Created: 2026-09-01 07:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vlc/contrib/contrib-build/libkate/src/kate_decode.c
Line
Count
Source
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_fp.h"
22
#include "kate_rle.h"
23
24
typedef struct kate_memory_guard {
25
  size_t size;
26
  void **pointers;
27
} kate_memory_guard;
28
29
static void *kate_memory_guard_malloc(kate_memory_guard *kmg,size_t size)
30
0
{
31
0
  void **new_pointers;
32
0
  void *ptr;
33
0
  int ret;
34
35
0
  ret=kate_check_add_overflow(kmg->size,1,NULL);
36
0
  if (ret<0) return NULL;
37
38
0
  ptr=kate_malloc(size);
39
0
  if (!ptr) return NULL;
40
0
  new_pointers=(void**)kate_checked_realloc(kmg->pointers,(kmg->size+1),sizeof(void*));
41
0
  if (!new_pointers) {
42
0
    kate_free(ptr);
43
0
    return NULL;
44
0
  }
45
0
  kmg->pointers=new_pointers;
46
0
  kmg->pointers[kmg->size++]=ptr;
47
0
  return ptr;
48
0
}
49
50
static void *kate_memory_guard_checked_malloc(kate_memory_guard *kmg,size_t size1,size_t size2)
51
0
{
52
0
  size_t size;
53
0
  int ret=kate_check_mul_overflow(size1,size2,&size);
54
0
  if (ret<0) return NULL;
55
0
  return kate_memory_guard_malloc(kmg,size);
56
0
}
57
58
static void kate_memory_guard_flush(kate_memory_guard *kmg,int free_pointers)
59
0
{
60
0
  size_t n;
61
0
  if (free_pointers) {
62
0
    for (n=0;n<kmg->size;++n) kate_free(kmg->pointers[n]);
63
0
  }
64
0
  kmg->size=0;
65
0
}
66
67
static void kate_memory_guard_destroy(kate_memory_guard *kmg,int free_pointers)
68
0
{
69
0
  kate_memory_guard_flush(kmg,free_pointers);
70
0
  if (kmg->pointers) kate_free(kmg->pointers);
71
0
}
72
73
static int kate_memory_guard_merge(kate_memory_guard *kmg,kate_memory_guard *parent_kmg)
74
0
{
75
0
  void **new_pointers;
76
0
  int ret;
77
0
  size_t new_size;
78
79
0
  ret=kate_check_add_overflow(parent_kmg->size,kmg->size,&new_size);
80
0
  if (ret<0) return ret;
81
82
0
  new_pointers=(void**)kate_checked_realloc(parent_kmg->pointers,new_size,sizeof(void*));
83
0
  if (!new_pointers) {
84
0
    kate_memory_guard_destroy(kmg,1);
85
0
    return KATE_E_OUT_OF_MEMORY;
86
0
  }
87
0
  parent_kmg->pointers=new_pointers;
88
0
  memcpy(parent_kmg->pointers+parent_kmg->size,kmg->pointers,kmg->size*sizeof(void*));
89
0
  parent_kmg->size=new_size;
90
0
  kate_memory_guard_destroy(kmg,0);
91
0
  return 0;
92
0
}
93
94
0
#define KMG_GUARD() kate_memory_guard kmg={0,NULL}
95
0
#define KMG_MALLOC(size) kate_memory_guard_malloc(&kmg,size)
96
0
#define KMG_CHECKED_MALLOC(size1,size2) kate_memory_guard_checked_malloc(&kmg,size1,size2)
97
0
#define KMG_MERGE(parent_kmg) kate_memory_guard_merge(&kmg,parent_kmg)
98
0
#define KMG_ERROR(ret) (kate_memory_guard_destroy(&kmg,1),ret)
99
0
#define KMG_OK() (kate_memory_guard_destroy(&kmg,0),0)
100
0
#define KMG_FLUSH(free_pointers) kate_memory_guard_flush(&kmg,free_pointers)
101
102
103
104
static int kate_readbuf(kate_pack_buffer *kpb,char *s,int len)
105
0
{
106
0
  if (len<0) return KATE_E_INVALID_PARAMETER;
107
0
  if ((kate_pack_readable_bits(kpb)+7)/8<len) return KATE_E_BAD_PACKET;
108
0
  while (len--) *s++=kate_pack_read(kpb,8);
109
0
  return 0;
110
0
}
111
112
static kate_int32_t kate_read32(kate_pack_buffer *kpb)
113
0
{
114
0
  kate_int32_t v=0;
115
0
  v|=kate_pack_read(kpb,8);
116
0
  v|=(kate_pack_read(kpb,8)<<8);
117
0
  v|=(kate_pack_read(kpb,8)<<16);
118
0
  v|=(kate_pack_read(kpb,8)<<24);
119
0
  return v;
120
0
}
121
122
static kate_int32_t kate_read32v(kate_pack_buffer *kpb)
123
0
{
124
  /* an error (negative) will be returned as negative, propagating the error
125
     to the caller even though the expected range for 4 bits is 0-15 */
126
0
  int smallv=kate_pack_read(kpb,4);
127
0
  if (smallv==15) {
128
0
    int sign=kate_pack_read1(kpb);
129
0
    int bits=kate_pack_read(kpb,5)+1;
130
0
    kate_int32_t v=kate_pack_read(kpb,bits);
131
0
    if (sign) v=-v;
132
0
    return v;
133
0
  }
134
0
  else return smallv;
135
0
}
136
137
static kate_int64_t kate_read64(kate_pack_buffer *kpb)
138
0
{
139
0
  kate_int32_t vl=kate_read32(kpb);
140
0
  kate_int32_t vh=kate_read32(kpb);
141
0
  return (0xffffffff&(kate_int64_t)vl)|(((kate_int64_t)vh)<<32);
142
0
}
143
144
static int kate_overread(kate_pack_buffer *kpb)
145
0
{
146
0
  return kate_pack_look(kpb,0)<0;
147
0
}
148
149
static int kate_warp(kate_pack_buffer *kpb)
150
0
{
151
0
  while (1) {
152
0
    kate_int32_t bits=kate_read32v(kpb);
153
0
    if (!bits) break;
154
0
    if (bits<0) return KATE_E_BAD_PACKET;
155
0
    kate_pack_adv(kpb,bits);
156
0
  }
157
0
  return 0;
158
0
}
159
160
static int kate_read_metadata(kate_pack_buffer *kpb,kate_meta **km)
161
0
{
162
0
  KMG_GUARD();
163
0
  kate_meta *tmp;
164
0
  size_t n,nmeta;
165
0
  char *tag,*value;
166
0
  int len;
167
0
  int ret;
168
169
0
  *km=NULL;
170
0
  if (!kate_pack_read1(kpb)) return 0;
171
172
0
  ret=kate_meta_create(&tmp);
173
0
  if (ret<0) return ret;
174
0
  nmeta=kate_read32v(kpb);
175
0
  for (n=0;n<nmeta;++n) {
176
0
    len=kate_read32v(kpb);
177
0
    if (len<=0) goto error_bad_packet;
178
0
    tag=(char*)KMG_MALLOC(len+1);
179
0
    if (!tag) goto error_out_of_memory;
180
0
    kate_readbuf(kpb,tag,len);
181
0
    tag[len]=0;
182
183
0
    len=kate_read32v(kpb);
184
0
    if (len<0) goto error_bad_packet;
185
0
    value=(char*)KMG_MALLOC(len+1);
186
0
    if (!value) goto error_out_of_memory;
187
0
    kate_readbuf(kpb,value,len);
188
0
    value[len]=0;
189
190
0
    kate_warp(kpb);
191
192
0
    ret=kate_meta_add(tmp,tag,value,len);
193
0
    if (ret<0) goto error;
194
195
0
    KMG_FLUSH(1); /* TODO: unnecessary temporary allocations */
196
0
  }
197
198
0
  kate_warp(kpb);
199
200
0
  *km=tmp;
201
0
  return KMG_OK();
202
203
0
error_bad_packet:
204
0
  ret=KATE_E_BAD_PACKET;
205
0
  goto error;
206
207
0
error_out_of_memory:
208
0
  ret=KATE_E_OUT_OF_MEMORY;
209
0
  goto error;
210
211
0
error:
212
0
  kate_meta_destroy(tmp);
213
0
  return KMG_ERROR(ret);
214
0
}
215
216
static int kate_decode_check_magic(kate_pack_buffer *kpb)
217
0
{
218
0
  char magic[8];
219
0
  int ret;
220
221
0
  if (!kpb) return KATE_E_INVALID_PARAMETER;
222
223
0
  ret=kate_readbuf(kpb,magic,7);
224
0
  if (ret<0) return KATE_E_NOT_KATE;
225
0
  if (memcmp(magic,"kate\0\0\0",7)) return KATE_E_NOT_KATE;
226
227
0
  return 0;
228
0
}
229
230
/**
231
  \ingroup decoding
232
  Checks if a packet is a Kate ID header
233
  \param kp the packet to inspect
234
  \returns 0 the packet is not an ID header
235
  \returns !=0 the packet is an ID header
236
  */
237
int kate_decode_is_idheader(const kate_packet *kp)
238
0
{
239
0
  kate_pack_buffer kpb;
240
0
  unsigned char headerid;
241
242
0
  if (!kp) return 0;
243
244
0
  kate_pack_readinit(&kpb,kp->data,kp->nbytes);
245
0
  headerid=kate_pack_read(&kpb,8);
246
0
  if (headerid!=0x80) return 0;
247
248
0
  return !kate_decode_check_magic(&kpb);
249
0
}
250
251
static int kate_check_eop(kate_pack_buffer *kpb)
252
0
{
253
0
  int bits;
254
255
0
  if (!kpb) return KATE_E_INVALID_PARAMETER;
256
257
  /* ensure any remaining bits in the current byte are zero (reading 0 bytes yields zero) */
258
0
  bits=7&(8-(kate_pack_bits(kpb)&7));
259
0
  if (bits>0) {
260
0
    if (kate_pack_read(kpb,bits)) return KATE_E_BAD_PACKET;
261
0
  }
262
0
  if (kate_pack_look1(kpb)>=0) return KATE_E_BAD_PACKET; /* no more data expected */
263
264
0
  return 0;
265
0
}
266
267
static int kate_decode_read_canvas_size(kate_pack_buffer *kpb)
268
0
{
269
0
  size_t base,shift;
270
271
0
  if (!kpb) return KATE_E_INVALID_PARAMETER;
272
273
0
  shift=kate_pack_read(kpb,4);
274
0
  base=kate_pack_read(kpb,4);
275
0
  base|=(kate_pack_read(kpb,8)<<4);
276
277
0
  return base<<shift;
278
0
}
279
280
#define KATE_IS_BITSTREAM_LOE(major,minor) (major)
281
282
static int kate_decode_info_header(kate_info *ki,kate_pack_buffer *kpb)
283
0
{
284
0
  KMG_GUARD();
285
0
  int len;
286
0
  int ret;
287
0
  char *language,*category;
288
0
  int reserved;
289
290
0
  if (!ki || !kpb) return KMG_ERROR(KATE_E_INVALID_PARAMETER);
291
292
0
  ki->bitstream_version_major=kate_pack_read(kpb,8);
293
0
  ki->bitstream_version_minor=kate_pack_read(kpb,8);
294
0
  if (ki->bitstream_version_major>KATE_BITSTREAM_VERSION_MAJOR) return KMG_ERROR(KATE_E_VERSION);
295
296
0
  ki->num_headers=kate_pack_read(kpb,8);
297
0
  if (ki->num_headers<1) return KMG_ERROR(KATE_E_BAD_PACKET);
298
0
  ki->text_encoding=kate_pack_read(kpb,8);
299
0
  ki->text_directionality=kate_pack_read(kpb,8);
300
0
  reserved=kate_pack_read(kpb,8);
301
0
  if (ki->bitstream_version_major==0 && ki->bitstream_version_minor<3) {
302
0
    if (reserved!=0) return KMG_ERROR(KATE_E_BAD_PACKET); /* reserved - 0 */
303
0
  }
304
0
  ki->granule_shift=kate_pack_read(kpb,8);
305
306
0
  ret=kate_decode_read_canvas_size(kpb);
307
0
  if (ret<0) return KMG_ERROR(KATE_E_BAD_PACKET);
308
0
  ki->original_canvas_width=ret;
309
0
  ret=kate_decode_read_canvas_size(kpb);
310
0
  if (ret<0) return KMG_ERROR(KATE_E_BAD_PACKET);
311
0
  ki->original_canvas_height=ret;
312
313
0
  reserved=kate_read32(kpb);
314
0
  if (ki->bitstream_version_major==0 && ki->bitstream_version_minor<3) {
315
0
    if (reserved!=0) return KMG_ERROR(KATE_E_BAD_PACKET); /* reserved - 0 */
316
0
  }
317
318
0
  ki->gps_numerator=kate_read32(kpb);
319
0
  ki->gps_denominator=kate_read32(kpb);
320
321
0
  if (ki->granule_shift>=64) return KMG_ERROR(KATE_E_BAD_PACKET);
322
0
  if (ki->gps_numerator==0) return KMG_ERROR(KATE_E_BAD_PACKET);
323
0
  if (ki->gps_denominator==0) return KMG_ERROR(KATE_E_BAD_PACKET);
324
  /*
325
  should not be needed anymore, calculations on these should be ok with full 32 bit range
326
  if ((ki->gps_numerator^ki->gps_denominator)<0) return KMG_ERROR(KATE_E_BAD_PACKET);
327
  */
328
329
0
  len=16;
330
0
  language=(char*)KMG_MALLOC(len);
331
0
  if (!language) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
332
0
  ret=kate_readbuf(kpb,language,len); /* a terminating null is included */
333
0
  if (ret<0) return KMG_ERROR(ret);
334
0
  if (language[len-1]) return KMG_ERROR(KATE_E_BAD_PACKET); /* but do check it to be sure */
335
336
0
  len=16;
337
0
  category=(char*)KMG_MALLOC(len);
338
0
  if (!category) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
339
0
  ret=kate_readbuf(kpb,category,len); /* a terminating null is included */
340
0
  if (ret<0) return KMG_ERROR(ret);
341
0
  if (category[len-1]) return KMG_ERROR(KATE_E_BAD_PACKET); /* but do check it to be sure */
342
343
0
  ret=kate_check_eop(kpb);
344
0
  if (ret<0) return KMG_ERROR(ret);
345
346
0
  ki->language=language;
347
0
  ki->category=category;
348
349
0
  return KMG_OK();
350
0
}
351
352
static int kate_decode_comment_packet(const kate_info *ki,kate_comment *kc,kate_pack_buffer *kpb)
353
0
{
354
0
  KMG_GUARD();
355
0
  int ret,len,nc;
356
0
  char **user_comments,*vendor;
357
0
  int comments,*comment_lengths;
358
359
0
  if (!ki || !kc || !kpb) return KMG_ERROR(KATE_E_INVALID_PARAMETER);
360
361
0
  len=kate_read32(kpb);
362
0
  if (len<0) return KMG_ERROR(KATE_E_BAD_PACKET);
363
0
  if (!ki->no_limits && len>KATE_LIMIT_COMMENT_LENGTH) return KMG_ERROR(KATE_E_LIMIT);
364
0
  vendor=(char*)KMG_MALLOC(len+1);
365
0
  if (!vendor) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
366
0
  ret=kate_readbuf(kpb,vendor,len);
367
0
  if (ret<0) return KMG_ERROR(ret);
368
0
  vendor[len]=0;
369
370
0
  comments=kate_read32(kpb);
371
0
  if (comments<0) return KMG_ERROR(KATE_E_BAD_PACKET);
372
0
  if (!ki->no_limits && len>KATE_LIMIT_COMMENTS) return KMG_ERROR(KATE_E_LIMIT);
373
0
  user_comments=(char**)KMG_CHECKED_MALLOC(comments,sizeof(char*));
374
0
  comment_lengths=(int*)KMG_CHECKED_MALLOC(comments,sizeof(int));
375
0
  if (!user_comments || !comment_lengths) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
376
377
0
  for (nc=0;nc<comments;++nc) user_comments[nc]=NULL;
378
0
  for (nc=0;nc<comments;++nc) {
379
0
    len=kate_read32(kpb);
380
0
    if (len<0) return KMG_ERROR(KATE_E_BAD_PACKET);
381
0
    if (!ki->no_limits && len>KATE_LIMIT_COMMENT_LENGTH) return KMG_ERROR(KATE_E_LIMIT);
382
0
    user_comments[nc]=(char*)KMG_MALLOC(len+1);
383
0
    if (!user_comments[nc]) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
384
0
    if (len) {
385
0
      ret=kate_readbuf(kpb,user_comments[nc],len);
386
0
      if (ret<0) return KMG_ERROR(ret);
387
0
    }
388
0
    user_comments[nc][len]=0;
389
0
    comment_lengths[nc]=len;
390
0
  }
391
392
0
  ret=kate_check_eop(kpb);
393
0
  if (ret<0) return KMG_ERROR(ret);
394
395
0
  kc->user_comments=user_comments;
396
0
  kc->comment_lengths=comment_lengths;
397
0
  kc->comments=comments;
398
0
  kc->vendor=vendor;
399
400
0
  return KMG_OK();
401
0
}
402
403
static int kate_decode_region(const kate_info *ki,kate_region *kr,kate_pack_buffer *kpb)
404
0
{
405
0
  int ret;
406
407
0
  if (!kr || !kpb) return KATE_E_INVALID_PARAMETER;
408
409
0
  kr->metric=kate_pack_read(kpb,8);
410
0
  kr->x=kate_read32v(kpb);
411
0
  kr->y=kate_read32v(kpb);
412
0
  kr->w=kate_read32v(kpb);
413
0
  kr->h=kate_read32v(kpb);
414
0
  kr->style=kate_read32v(kpb);
415
416
0
  if (((ki->bitstream_version_major<<8)|ki->bitstream_version_minor)>=0x0002) {
417
    /* 0.2 adds a warp for clip */
418
0
    kate_read32v(kpb); /* the size of the warp */
419
0
    kr->clip=kate_pack_read1(kpb);
420
0
  }
421
0
  else {
422
0
    kr->clip=0;
423
0
  }
424
425
0
  if (((ki->bitstream_version_major<<8)|ki->bitstream_version_minor)>=0x0006) {
426
    /* 0.6 adds a warp for metadata */
427
0
    kate_read32v(kpb); /* the size of the warp */
428
0
    ret=kate_read_metadata(kpb,&kr->meta);
429
0
    if (ret<0) return ret;
430
0
  }
431
0
  else {
432
0
    kr->meta=NULL;
433
0
  }
434
435
0
  return kate_warp(kpb);
436
0
}
437
438
static int kate_decode_regions_packet(kate_info *ki,kate_pack_buffer *kpb)
439
0
{
440
0
  KMG_GUARD();
441
0
  int ret,n,nregions;
442
0
  kate_region **regions;
443
444
0
  if (!ki || !kpb) return KMG_ERROR(KATE_E_INVALID_PARAMETER);
445
446
0
  nregions=kate_read32v(kpb);
447
0
  if (nregions<0) return KMG_ERROR(KATE_E_BAD_PACKET);
448
0
  if (!ki->no_limits && nregions>KATE_LIMIT_REGIONS) return KMG_ERROR(KATE_E_LIMIT);
449
0
  regions=(kate_region**)KMG_CHECKED_MALLOC(nregions,sizeof(kate_region*));
450
0
  if (!regions) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
451
0
  for (n=0;n<nregions;++n) {
452
0
    regions[n]=(kate_region*)KMG_MALLOC(sizeof(kate_region));
453
0
    if (!regions[n]) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
454
0
    ret=kate_decode_region(ki,regions[n],kpb);
455
0
    if (kate_overread(kpb)) return KMG_ERROR(KATE_E_BAD_PACKET);
456
0
    if (ret<0) return KMG_ERROR(ret);
457
0
  }
458
459
0
  ret=kate_warp(kpb);
460
0
  if (ret<0) return KMG_ERROR(ret);
461
462
0
  ret=kate_check_eop(kpb);
463
0
  if (ret<0) return KMG_ERROR(ret);
464
465
0
  ki->nregions=nregions;
466
0
  ki->regions=regions;
467
468
0
  return KMG_OK();
469
0
}
470
471
static int kate_decode_color(kate_color *kc,kate_pack_buffer *kpb)
472
0
{
473
0
  if (!kc || !kpb) return KATE_E_INVALID_PARAMETER;
474
0
  kc->r=kate_pack_read(kpb,8);
475
0
  kc->g=kate_pack_read(kpb,8);
476
0
  kc->b=kate_pack_read(kpb,8);
477
0
  kc->a=kate_pack_read(kpb,8);
478
0
  return 0;
479
0
}
480
481
static int kate_decode_style(const kate_info *ki,kate_style *ks,kate_pack_buffer *kpb,kate_memory_guard *parent_kmg)
482
0
{
483
0
  KMG_GUARD();
484
0
  int ret;
485
0
  kate_float d[8];
486
0
  size_t idx;
487
0
  int len;
488
489
0
  if (!ks || !kpb) return KMG_ERROR(KATE_E_INVALID_PARAMETER);
490
491
0
  ret=kate_fp_decode_kate_float(sizeof(d)/sizeof(d[0]),d,1,kpb);
492
0
  if (ret<0) return KMG_ERROR(ret);
493
494
0
  idx=0;
495
0
  ks->halign=d[idx++];
496
0
  ks->valign=d[idx++];
497
0
  ks->font_width=d[idx++];
498
0
  ks->font_height=d[idx++];
499
0
  ks->left_margin=d[idx++];
500
0
  ks->top_margin=d[idx++];
501
0
  ks->right_margin=d[idx++];
502
0
  ks->bottom_margin=d[idx++];
503
0
  ret=kate_decode_color(&ks->text_color,kpb);
504
0
  if (ret<0) return KMG_ERROR(ret);
505
0
  ret=kate_decode_color(&ks->background_color,kpb);
506
0
  if (ret<0) return KMG_ERROR(ret);
507
0
  ret=kate_decode_color(&ks->draw_color,kpb);
508
0
  if (ret<0) return KMG_ERROR(ret);
509
0
  ks->font_metric=kate_pack_read(kpb,8);
510
0
  ks->margin_metric=kate_pack_read(kpb,8);
511
0
  ks->bold=kate_pack_read1(kpb);
512
0
  ks->italics=kate_pack_read1(kpb);
513
0
  ks->underline=kate_pack_read1(kpb);
514
0
  ks->strike=kate_pack_read1(kpb);
515
516
0
  if (((ki->bitstream_version_major<<8)|ki->bitstream_version_minor)>=0x0002) {
517
    /* 0.2 adds a warp for justify and font */
518
0
    kate_read32v(kpb); /* the size of the warp */
519
0
    ks->justify=kate_pack_read1(kpb);
520
0
    len=kate_read32v(kpb);
521
0
    if (len<0) {
522
0
      return KMG_ERROR(KATE_E_BAD_PACKET);
523
0
    }
524
0
    else if (len>0) {
525
0
      ks->font=(char*)KMG_MALLOC(len+1);
526
0
      if (!ks->font) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
527
0
      ret=kate_readbuf(kpb,ks->font,len);
528
0
      if (ret<0) return KMG_ERROR(ret);
529
0
      ks->font[len]=0;
530
0
    }
531
0
    else {
532
0
      ks->font=NULL;
533
0
    }
534
0
  }
535
0
  else {
536
0
    ks->justify=0;
537
0
    ks->font=NULL;
538
0
  }
539
540
0
  if (((ki->bitstream_version_major<<8)|ki->bitstream_version_minor)>=0x0004) {
541
    /* 0.4 adds a warp for wrap mode */
542
0
    kate_read32v(kpb); /* the size of the warp */
543
0
    ks->wrap_mode=kate_read32v(kpb);
544
0
  }
545
0
  else {
546
0
    ks->wrap_mode=kate_wrap_word;
547
0
  }
548
549
0
  if (((ki->bitstream_version_major<<8)|ki->bitstream_version_minor)>=0x0006) {
550
    /* 0.6 adds a warp for metadata */
551
0
    kate_read32v(kpb); /* the size of the warp */
552
0
    ret=kate_read_metadata(kpb,&ks->meta);
553
0
    if (ret<0) return KMG_ERROR(ret);
554
0
  }
555
0
  else {
556
0
    ks->meta=NULL;
557
0
  }
558
559
0
  ret=kate_warp(kpb);
560
0
  if (ret<0) return KMG_ERROR(ret);
561
562
0
  return KMG_MERGE(parent_kmg);
563
0
}
564
565
static int kate_decode_styles_packet(kate_info *ki,kate_pack_buffer *kpb)
566
0
{
567
0
  KMG_GUARD();
568
0
  int ret,n,nstyles;
569
0
  kate_style **styles;
570
571
0
  if (!ki || !kpb) return KMG_ERROR(KATE_E_INVALID_PARAMETER);
572
573
0
  nstyles=kate_read32v(kpb);
574
0
  if (nstyles<0) return KMG_ERROR(KATE_E_BAD_PACKET);
575
0
  if (!ki->no_limits && nstyles>KATE_LIMIT_STYLES) return KMG_ERROR(KATE_E_LIMIT);
576
0
  styles=(kate_style**)KMG_CHECKED_MALLOC(nstyles,sizeof(kate_style*));
577
0
  if (!styles) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
578
0
  for (n=0;n<nstyles;++n) {
579
0
    styles[n]=(kate_style*)KMG_MALLOC(sizeof(kate_style));
580
0
    if (!styles[n]) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
581
0
    ret=kate_decode_style(ki,styles[n],kpb,&kmg);
582
0
    if (kate_overread(kpb)) return KMG_ERROR(KATE_E_BAD_PACKET);
583
0
    if (ret<0) return KMG_ERROR(ret);
584
0
  }
585
586
0
  ret=kate_warp(kpb);
587
0
  if (ret<0) return KMG_ERROR(ret);
588
589
0
  ret=kate_check_eop(kpb);
590
0
  if (ret<0) return KMG_ERROR(ret);
591
592
0
  ki->nstyles=nstyles;
593
0
  ki->styles=styles;
594
595
0
  return KMG_OK();
596
0
}
597
598
static int kate_decode_curve(const kate_info *ki,kate_curve *kc,kate_pack_buffer *kpb,kate_memory_guard *parent_kmg)
599
0
{
600
0
  KMG_GUARD();
601
0
  int ret;
602
603
0
  if (!ki || !kc || !kpb) return KMG_ERROR(KATE_E_INVALID_PARAMETER);
604
605
0
  kc->type=kate_pack_read(kpb,8);
606
0
  kc->npts=kate_read32v(kpb);
607
0
  ret=kate_warp(kpb);
608
0
  if (ret<0) return KMG_ERROR(ret);
609
0
  if (!ki->no_limits && kc->npts>KATE_LIMIT_CURVE_POINTS) return KMG_ERROR(KATE_E_LIMIT);
610
0
  kc->pts=(kate_float*)KMG_CHECKED_MALLOC(kc->npts,2*sizeof(kate_float));
611
0
  if (!kc->pts) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
612
613
0
  ret=kate_fp_decode_kate_float(kc->npts,kc->pts,2,kpb);
614
0
  if (ret<0) return KMG_ERROR(ret);
615
616
  /* on success, add our memory to the parent guard */
617
0
  return KMG_MERGE(parent_kmg);
618
0
}
619
620
static int kate_decode_curves_packet(kate_info *ki,kate_pack_buffer *kpb)
621
0
{
622
0
  KMG_GUARD();
623
0
  int ret,n,ncurves;
624
0
  kate_curve **curves=NULL;
625
626
0
  if (!ki || !kpb) return KMG_ERROR(KATE_E_INVALID_PARAMETER);
627
628
0
  ncurves=kate_read32v(kpb);
629
0
  if (ncurves<0) return KMG_ERROR(KATE_E_BAD_PACKET);
630
0
  if (!ki->no_limits && ncurves>KATE_LIMIT_CURVES) return KMG_ERROR(KATE_E_LIMIT);
631
632
0
  if (ncurves>0) {
633
0
    curves=(kate_curve**)KMG_CHECKED_MALLOC(ncurves,sizeof(kate_curve*));
634
0
    if (!curves) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
635
0
    for (n=0;n<ncurves;++n) {
636
0
      curves[n]=(kate_curve*)KMG_MALLOC(sizeof(kate_curve));
637
0
      if (!curves[n]) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
638
0
      ret=kate_decode_curve(ki,curves[n],kpb,&kmg);
639
0
      if (kate_overread(kpb)) return KMG_ERROR(KATE_E_BAD_PACKET);
640
0
      if (ret<0) return KMG_ERROR(ret);
641
0
    }
642
0
  }
643
644
0
  ret=kate_warp(kpb);
645
0
  if (ret<0) return KMG_ERROR(ret);
646
647
0
  ret=kate_check_eop(kpb);
648
0
  if (ret<0) return KMG_ERROR(ret);
649
650
0
  ki->ncurves=ncurves;
651
0
  ki->curves=curves;
652
653
0
  return KMG_OK();
654
0
}
655
656
static int kate_decode_motion(const kate_info *ki,kate_motion *km,kate_pack_buffer *kpb,kate_memory_guard *parent_kmg)
657
0
{
658
0
  KMG_GUARD();
659
0
  size_t n;
660
0
  int ret;
661
662
0
  if (!ki || !km || !kpb) return KMG_ERROR(KATE_E_INVALID_PARAMETER);
663
664
0
  km->ncurves=kate_read32v(kpb);
665
0
  if (!ki->no_limits && km->ncurves>KATE_LIMIT_MOTION_CURVES) return KMG_ERROR(KATE_E_LIMIT);
666
0
  km->curves=(kate_curve**)KMG_CHECKED_MALLOC(km->ncurves,sizeof(kate_curve*));
667
0
  if (!km->curves) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
668
0
  km->durations=(kate_float*)KMG_CHECKED_MALLOC(km->ncurves,sizeof(kate_float));
669
0
  if (!km->durations) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
670
671
0
  for (n=0;n<km->ncurves;++n) {
672
0
    if (kate_pack_read1(kpb)) {
673
0
      size_t idx=kate_read32v(kpb);
674
0
      if (idx>=ki->ncurves) return KMG_ERROR(KATE_E_BAD_PACKET);
675
0
      km->curves[n]=ki->curves[idx];
676
0
    }
677
0
    else {
678
0
      km->curves[n]=(kate_curve*)KMG_MALLOC(sizeof(kate_curve));
679
0
      if (!km->curves[n]) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
680
0
      ret=kate_decode_curve(ki,km->curves[n],kpb,&kmg);
681
0
      if (kate_overread(kpb)) return KMG_ERROR(KATE_E_BAD_PACKET);
682
0
      if (ret<0) return KMG_ERROR(ret);
683
0
    }
684
0
  }
685
0
  ret=kate_fp_decode_kate_float(km->ncurves,km->durations,1,kpb);
686
0
  if (ret<0) return KMG_ERROR(ret);
687
0
  km->x_mapping=kate_pack_read(kpb,8);
688
0
  km->y_mapping=kate_pack_read(kpb,8);
689
0
  km->semantics=kate_pack_read(kpb,8);
690
0
  km->periodic=kate_pack_read1(kpb);
691
692
0
  if (((ki->bitstream_version_major<<8)|ki->bitstream_version_minor)>=0x0006) {
693
    /* 0.6 adds a warp for metadata */
694
0
    kate_read32v(kpb); /* the size of the warp */
695
0
    ret=kate_read_metadata(kpb,&km->meta);
696
0
    if (ret<0) return KMG_ERROR(ret);
697
0
  }
698
0
  else {
699
0
    km->meta=NULL;
700
0
  }
701
702
0
  ret=kate_warp(kpb);
703
0
  if (ret<0) return KMG_ERROR(ret);
704
705
0
  return KMG_MERGE(parent_kmg);
706
0
}
707
708
static int kate_decode_motions_packet(kate_info *ki,kate_pack_buffer *kpb)
709
0
{
710
0
  KMG_GUARD();
711
0
  int ret,n,nmotions;
712
0
  kate_motion **motions=NULL;
713
714
0
  if (!ki || !kpb) return KMG_ERROR(KATE_E_INVALID_PARAMETER);
715
716
0
  nmotions=kate_read32v(kpb);
717
0
  if (nmotions<0) return KMG_ERROR(KATE_E_BAD_PACKET);
718
0
  if (!ki->no_limits && nmotions>KATE_LIMIT_MOTIONS) return KMG_ERROR(KATE_E_LIMIT);
719
720
0
  if (nmotions>0) {
721
0
    motions=(kate_motion**)KMG_CHECKED_MALLOC(nmotions,sizeof(kate_motion*));
722
0
    if (!motions) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
723
0
    for (n=0;n<nmotions;++n) {
724
0
      motions[n]=(kate_motion*)KMG_MALLOC(sizeof(kate_motion));
725
0
      if (!motions[n]) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
726
0
      ret=kate_decode_motion(ki,motions[n],kpb,&kmg);
727
0
      if (kate_overread(kpb)) return KMG_ERROR(KATE_E_BAD_PACKET);
728
0
      if (ret<0) return KMG_ERROR(ret);
729
0
    }
730
0
  }
731
732
0
  ret=kate_warp(kpb);
733
0
  if (ret<0) return KMG_ERROR(ret);
734
735
0
  ret=kate_check_eop(kpb);
736
0
  if (ret<0) return KMG_ERROR(ret);
737
738
0
  ki->nmotions=nmotions;
739
0
  ki->motions=motions;
740
741
0
  return KMG_OK();
742
0
}
743
744
static int kate_decode_palette(const kate_info *ki,kate_palette *kp,kate_pack_buffer *kpb)
745
0
{
746
0
  kate_color *colors;
747
0
  size_t n;
748
0
  int ret;
749
750
0
  if (!ki || !kp || !kpb) return KATE_E_INVALID_PARAMETER;
751
752
0
  kp->ncolors=kate_pack_read(kpb,8)+1;
753
754
0
  colors=(kate_color*)kate_checked_malloc(kp->ncolors,sizeof(kate_color));
755
0
  if (!colors) return KATE_E_OUT_OF_MEMORY;
756
757
0
  for (n=0;n<kp->ncolors;++n) {
758
0
    ret=kate_decode_color(colors+n,kpb);
759
0
    if (kate_overread(kpb)) {
760
0
      kate_free(colors);
761
0
      return ret;
762
0
    }
763
0
    if (ret<0) {
764
0
      kate_free(colors);
765
0
      return ret;
766
0
    }
767
0
  }
768
769
0
  if (((ki->bitstream_version_major<<8)|ki->bitstream_version_minor)>=0x0006) {
770
    /* 0.6 adds a warp for metadata */
771
0
    kate_read32v(kpb); /* the size of the warp */
772
0
    ret=kate_read_metadata(kpb,&kp->meta);
773
0
    if (ret<0) return ret;
774
0
  }
775
0
  else {
776
0
    kp->meta=NULL;
777
0
  }
778
779
0
  ret=kate_warp(kpb);
780
0
  if (ret<0) return ret;
781
782
0
  kp->colors=colors;
783
784
0
  return 0;
785
0
}
786
787
static int kate_decode_palettes_packet(kate_info *ki,kate_pack_buffer *kpb)
788
0
{
789
0
  KMG_GUARD();
790
0
  int ret,n,npalettes;
791
0
  kate_palette **palettes=NULL;
792
793
0
  if (!ki || !kpb) return KMG_ERROR(KATE_E_INVALID_PARAMETER);
794
795
0
  npalettes=kate_read32v(kpb);
796
0
  if (npalettes<0) return KMG_ERROR(KATE_E_BAD_PACKET);
797
0
  if (!ki->no_limits && npalettes>KATE_LIMIT_PALETTES) return KMG_ERROR(KATE_E_LIMIT);
798
799
0
  if (npalettes>0) {
800
0
    palettes=(kate_palette**)KMG_CHECKED_MALLOC(npalettes,sizeof(kate_palette*));
801
0
    if (!palettes) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
802
0
    for (n=0;n<npalettes;++n) {
803
0
      palettes[n]=(kate_palette*)KMG_MALLOC(sizeof(kate_palette));
804
0
      if (!palettes[n]) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
805
0
      ret=kate_decode_palette(ki,palettes[n],kpb);
806
0
      if (kate_overread(kpb)) return KMG_ERROR(ret);
807
0
      if (ret<0) return KMG_ERROR(ret);
808
0
    }
809
0
  }
810
811
0
  ret=kate_warp(kpb);
812
0
  if (ret<0) return KMG_ERROR(ret);
813
814
0
  ret=kate_check_eop(kpb);
815
0
  if (ret<0) return KMG_ERROR(ret);
816
817
0
  ki->npalettes=npalettes;
818
0
  ki->palettes=palettes;
819
820
0
  return KMG_OK();
821
0
}
822
823
static int kate_decode_bitmap(const kate_info *ki,kate_bitmap *kb,kate_pack_buffer *kpb)
824
0
{
825
0
  size_t n,npixels;
826
0
  unsigned char *pixels;
827
0
  int ret,encoding;
828
829
0
  if (!ki || !kb || !kpb) return KATE_E_INVALID_PARAMETER;
830
831
0
  kb->width=kate_read32v(kpb);
832
0
  kb->height=kate_read32v(kpb);
833
0
  kb->bpp=kate_pack_read(kpb,8);
834
0
  if (kb->width<=0 || kb->height<=0 || kb->bpp>8) return KATE_E_BAD_PACKET;
835
0
  if (!ki->no_limits && (kb->width>KATE_LIMIT_BITMAP_SIZE || kb->height>KATE_LIMIT_BITMAP_SIZE)) return KATE_E_LIMIT;
836
837
0
  if (kb->bpp==0) {
838
    /* raw bitmap */
839
0
    kb->type=kate_pack_read(kpb,8);
840
0
    kb->palette=-1;
841
0
    switch (kb->type) {
842
0
      case kate_bitmap_type_paletted:
843
0
        encoding=kate_pack_read(kpb,8);
844
0
        switch (encoding) {
845
0
          case 1: /* RLE encoded */
846
0
            kb->bpp=kate_read32v(kpb);
847
0
            kb->palette=kate_read32v(kpb);
848
0
            pixels=(unsigned char*)kate_checked_malloc(kb->width,kb->height);
849
0
            if (!pixels) return KATE_E_OUT_OF_MEMORY;
850
0
            ret=kate_rle_decode(kb->width,kb->height,pixels,kb->bpp,kpb);
851
0
            if (ret<0) return ret;
852
0
            break;
853
0
          default:
854
0
            return KATE_E_BAD_PACKET;
855
0
        }
856
0
        break;
857
0
      case kate_bitmap_type_png:
858
0
        kb->size=kate_read32(kpb);
859
0
        if (!ki->no_limits && kb->size>KATE_LIMIT_BITMAP_RAW_SIZE) return KATE_E_LIMIT;
860
0
        pixels=(unsigned char*)kate_malloc(kb->size);
861
0
        if (!pixels) return KATE_E_OUT_OF_MEMORY;
862
0
        ret=kate_readbuf(kpb,(char*)pixels,kb->size);
863
0
        if (ret<0) {
864
0
          kate_free(pixels);
865
0
          return ret;
866
0
        }
867
0
        break;
868
0
      default:
869
0
        return KATE_E_BAD_PACKET;
870
0
    }
871
0
  }
872
0
  else {
873
    /* paletted bitmap */
874
0
    kb->type=kate_bitmap_type_paletted;
875
0
    kb->palette=kate_read32v(kpb);
876
877
0
    ret=kate_check_mul_overflow(kb->width,kb->height,&npixels);
878
0
    if (ret<0) return ret;
879
0
    pixels=(unsigned char*)kate_malloc(npixels);
880
0
    if (!pixels) return KATE_E_OUT_OF_MEMORY;
881
0
    if ((size_t)kate_pack_bits(kpb)<npixels*kb->bpp) {
882
0
      kate_free(pixels);
883
0
      return KATE_E_BAD_PACKET;
884
0
    }
885
886
0
    for (n=0;n<npixels;++n) {
887
0
      pixels[n]=kate_pack_read(kpb,kb->bpp);
888
0
    }
889
0
    if (kate_overread(kpb)) {
890
0
      kate_free(pixels);
891
0
      return KATE_E_BAD_PACKET;
892
0
    }
893
0
  }
894
895
0
  if (((ki->bitstream_version_major<<8)|ki->bitstream_version_minor)>=0x0004) {
896
    /* 0.4 adds a warp for x/y offset */
897
0
    kate_read32v(kpb); /* the size of the warp */
898
0
    kb->x_offset=kate_read32v(kpb);
899
0
    kb->y_offset=kate_read32v(kpb);
900
0
  }
901
0
  else {
902
0
    kb->x_offset=0;
903
0
    kb->y_offset=0;
904
0
  }
905
906
0
  if (((ki->bitstream_version_major<<8)|ki->bitstream_version_minor)>=0x0006) {
907
    /* 0.6 adds a warp for metadata */
908
0
    kate_read32v(kpb); /* the size of the warp */
909
0
    ret=kate_read_metadata(kpb,&kb->meta);
910
0
    if (ret<0) return ret;
911
0
  }
912
0
  else {
913
0
    kb->meta=NULL;
914
0
  }
915
0
  kb->internal=1;
916
917
0
  ret=kate_warp(kpb);
918
0
  if (ret<0) return ret;
919
920
0
  kb->pixels=pixels;
921
922
0
  return 0;
923
0
}
924
925
static int kate_decode_bitmaps_packet(kate_info *ki,kate_pack_buffer *kpb)
926
0
{
927
0
  KMG_GUARD();
928
0
  int ret,n,nbitmaps;
929
0
  kate_bitmap **bitmaps=NULL;
930
931
0
  if (!ki || !kpb) return KMG_ERROR(KATE_E_INVALID_PARAMETER);
932
933
0
  nbitmaps=kate_read32v(kpb);
934
0
  if (nbitmaps<0) return KMG_ERROR(KATE_E_BAD_PACKET);
935
0
  if (!ki->no_limits && nbitmaps>KATE_LIMIT_BITMAPS) return KMG_ERROR(KATE_E_LIMIT);
936
937
0
  if (nbitmaps>0) {
938
0
    bitmaps=(kate_bitmap**)KMG_CHECKED_MALLOC(nbitmaps,sizeof(kate_bitmap*));
939
0
    if (!bitmaps) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
940
0
    for (n=0;n<nbitmaps;++n) {
941
0
      bitmaps[n]=(kate_bitmap*)KMG_MALLOC(sizeof(kate_bitmap));
942
0
      if (!bitmaps[n]) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
943
0
      ret=kate_decode_bitmap(ki,bitmaps[n],kpb);
944
0
      if (kate_overread(kpb)) return KMG_ERROR(KATE_E_BAD_PACKET);
945
0
      if (ret<0) return KMG_ERROR(ret);
946
0
    }
947
0
  }
948
949
0
  ret=kate_warp(kpb);
950
0
  if (ret<0) return KMG_ERROR(ret);
951
952
0
  ret=kate_check_eop(kpb);
953
0
  if (ret<0) return KMG_ERROR(ret);
954
955
0
  ki->nbitmaps=nbitmaps;
956
0
  ki->bitmaps=bitmaps;
957
958
0
  return KMG_OK();
959
0
}
960
961
static int kate_decode_font_range(const kate_info *ki,kate_font_range *kfr,kate_pack_buffer *kpb)
962
0
{
963
0
  if (!ki || !kfr || !kpb) return KATE_E_INVALID_PARAMETER;
964
965
0
  kfr->first_code_point=kate_read32v(kpb);
966
0
  kfr->last_code_point=kate_read32v(kpb);
967
0
  kfr->first_bitmap=kate_read32v(kpb);
968
969
0
  return kate_warp(kpb);
970
0
}
971
972
static int kate_decode_font_ranges_packet(kate_info *ki,kate_pack_buffer *kpb)
973
0
{
974
0
  KMG_GUARD();
975
0
  int l,n,ret;
976
0
  int nfont_ranges,nfont_mappings;
977
0
  kate_font_range **font_ranges=NULL;
978
0
  kate_font_mapping **font_mappings=NULL;
979
980
0
  if (!ki || !kpb) return KMG_ERROR(KATE_E_INVALID_PARAMETER);
981
982
0
  nfont_ranges=kate_read32v(kpb);
983
0
  if (nfont_ranges<0) return KMG_ERROR(KATE_E_BAD_PACKET);
984
0
  if (!ki->no_limits && nfont_ranges>KATE_LIMIT_FONT_RANGES) return KMG_ERROR(KATE_E_LIMIT);
985
986
0
  if (nfont_ranges>0) {
987
0
    font_ranges=(kate_font_range**)KMG_CHECKED_MALLOC(nfont_ranges,sizeof(kate_font_range*));
988
0
    if (!font_ranges) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
989
0
    for (n=0;n<nfont_ranges;++n) {
990
0
      font_ranges[n]=(kate_font_range*)KMG_MALLOC(sizeof(kate_font_range));
991
0
      if (!font_ranges[n]) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
992
0
      ret=kate_decode_font_range(ki,font_ranges[n],kpb);
993
0
      if (kate_overread(kpb)) return KMG_ERROR(KATE_E_BAD_PACKET);
994
0
      if (ret<0) return KMG_ERROR(ret);
995
0
    }
996
0
  }
997
998
  /* these may now be used by the mappings */
999
0
  ki->nfont_ranges=nfont_ranges;
1000
0
  ki->font_ranges=font_ranges;
1001
1002
  /* now, the mappings */
1003
0
  nfont_mappings=kate_read32v(kpb);
1004
0
  if (nfont_mappings<0) return KMG_ERROR(KATE_E_BAD_PACKET);
1005
0
  if (!ki->no_limits && nfont_mappings>KATE_LIMIT_FONT_MAPPINGS) return KMG_ERROR(KATE_E_LIMIT);
1006
1007
0
  if (nfont_mappings>0) {
1008
0
    font_mappings=(kate_font_mapping**)KMG_CHECKED_MALLOC(nfont_mappings,sizeof(kate_font_mapping*));
1009
0
    if (!font_mappings) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
1010
0
    for (n=0;n<nfont_mappings;++n) {
1011
0
      font_mappings[n]=(kate_font_mapping*)KMG_MALLOC(sizeof(kate_font_mapping));
1012
0
      if (!font_mappings[n]) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
1013
1014
0
      nfont_ranges=kate_read32v(kpb);
1015
0
      if (nfont_ranges<0) return KMG_ERROR(KATE_E_BAD_PACKET);
1016
0
      if (!ki->no_limits && nfont_ranges>KATE_LIMIT_FONT_MAPPING_RANGES) return KMG_ERROR(KATE_E_LIMIT);
1017
1018
0
      if (nfont_ranges>0) {
1019
0
        font_ranges=(kate_font_range**)KMG_CHECKED_MALLOC(nfont_ranges,sizeof(kate_font_range*));
1020
0
        if (!font_ranges) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
1021
1022
0
        for (l=0;l<nfont_ranges;++l) {
1023
0
          if (kate_pack_read1(kpb)) {
1024
0
            size_t idx=kate_read32v(kpb);
1025
0
            if (idx>=ki->nfont_ranges) return KMG_ERROR(KATE_E_BAD_PACKET);
1026
0
            font_ranges[l]=ki->font_ranges[idx];
1027
0
          }
1028
0
          else {
1029
0
            font_ranges[l]=(kate_font_range*)KMG_MALLOC(sizeof(kate_font_range));
1030
0
            if (!font_ranges[l]) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
1031
0
            ret=kate_decode_font_range(ki,font_ranges[l],kpb);
1032
0
            if (kate_overread(kpb)) return KMG_ERROR(KATE_E_BAD_PACKET);
1033
0
            if (ret<0) return KMG_ERROR(ret);
1034
0
          }
1035
0
        }
1036
0
        font_mappings[n]->nranges=nfont_ranges;
1037
0
        font_mappings[n]->ranges=font_ranges;
1038
0
      }
1039
0
      else {
1040
0
        font_mappings[n]->nranges=0;
1041
0
        font_mappings[n]->ranges=NULL;
1042
0
      }
1043
0
    }
1044
0
  }
1045
1046
0
  ret=kate_warp(kpb);
1047
0
  if (ret<0) return KMG_ERROR(ret);
1048
1049
0
  ret=kate_check_eop(kpb);
1050
0
  if (ret<0) return KMG_ERROR(ret);
1051
1052
0
  ki->nfont_mappings=nfont_mappings;
1053
0
  ki->font_mappings=font_mappings;
1054
1055
0
  return KMG_OK();
1056
0
}
1057
1058
/**
1059
  \ingroup decoding
1060
  Decodes a header packet, and updates the kate_info and kate_comment structures
1061
  from the decoded data.
1062
  \param ki the kate_info structure to update
1063
  \param kc the kate_comment structure to update
1064
  \param kp the packet to decode
1065
  \returns 0 success
1066
  \returns 1 success, and all headers have been decoded
1067
  \returns KATE_E_* error
1068
  */
1069
int kate_decode_headerin(kate_info *ki,kate_comment *kc,kate_packet *kp)
1070
0
{
1071
0
  kate_pack_buffer kpb;
1072
0
  unsigned char headerid;
1073
0
  int ret;
1074
0
  int packetno;
1075
1076
0
  if (!ki || !kc || !kp) return KATE_E_INVALID_PARAMETER;
1077
1078
0
  kate_pack_readinit(&kpb,kp->data,kp->nbytes);
1079
0
  headerid=kate_pack_read(&kpb,8);
1080
1081
0
  ret=kate_decode_check_magic(&kpb);
1082
0
  if (ret<0) return ret;
1083
1084
0
  if (!(headerid&0x80)) return KATE_E_BAD_PACKET;
1085
0
  packetno=headerid&~0x80;
1086
0
  if (packetno<ki->num_headers) {
1087
0
    if (ki->probe!=packetno) return KATE_E_BAD_PACKET;
1088
0
  }
1089
1090
  /* starting with 0.1.4 (bitstream version 0.2, unchanged), there
1091
     was a change in the interpretation of the Kate magic, at the
1092
     request of Xiph, to limit signature length to 8 bytes. So the
1093
     signature (from byte offset 1, after the packet type byte) is
1094
     now only 7 bytes rather than 8, but all headers packets have
1095
     a reserved 0 byte after the signature, so the actual bitstream
1096
     format is left unchanged */
1097
0
  if (kate_pack_read(&kpb,8)!=0) return KATE_E_BAD_PACKET;
1098
1099
0
  switch (packetno) {
1100
0
    case 0: /* this is the info packet */
1101
0
      ret=kate_decode_info_header(ki,&kpb);
1102
0
      break;
1103
1104
0
    case 1: /* this is the comments packet */
1105
0
      ret=kate_decode_comment_packet(ki,kc,&kpb);
1106
0
      break;
1107
1108
0
    case 2: /* this is the region list packet */
1109
0
      ret=kate_decode_regions_packet(ki,&kpb);
1110
0
      break;
1111
1112
0
    case 3: /* this is the style list packet */
1113
0
      ret=kate_decode_styles_packet(ki,&kpb);
1114
0
      break;
1115
1116
0
    case 4: /* this is the curve list packet */
1117
0
      ret=kate_decode_curves_packet(ki,&kpb);
1118
0
      break;
1119
1120
0
    case 5: /* this is the motion list packet */
1121
0
      ret=kate_decode_motions_packet(ki,&kpb);
1122
0
      break;
1123
1124
0
    case 6: /* this is the palette list packet */
1125
0
      ret=kate_decode_palettes_packet(ki,&kpb);
1126
0
      break;
1127
1128
0
    case 7: /* this is the bitmap list packet */
1129
0
      ret=kate_decode_bitmaps_packet(ki,&kpb);
1130
0
      break;
1131
1132
0
    case 8: /* this is the font ranges list packet */
1133
0
      ret=kate_decode_font_ranges_packet(ki,&kpb);
1134
0
      if (ret==0) ret=1; /* we're done, we know of no more headers to come */
1135
0
      break;
1136
1137
0
    default:
1138
      /* we ignore extra header packets for future proofing */
1139
0
      ret=0;
1140
0
      break;
1141
0
  }
1142
1143
0
  if (ret>=0) {
1144
    /* if the header was parsed successfully, we're ready for the next one */
1145
0
    ki->probe++;
1146
0
  }
1147
1148
0
  return ret;
1149
0
}
1150
1151
/**
1152
  \ingroup decoding
1153
  Initializes a kate_state structure for decoding using the supplied kate_info structure.
1154
  When done, the kate_state should be cleared using kate_clear.
1155
  \param k the kate_state to initialize for decoding
1156
  \param ki the kate_info structure initialized from the decoded headers
1157
  \returns 0 success
1158
  \returns KATE_E_* error
1159
  */
1160
int kate_decode_init(kate_state *k,kate_info *ki)
1161
0
{
1162
0
  if (!k || !ki) return KATE_E_INVALID_PARAMETER;
1163
1164
0
  k->ki=ki;
1165
0
  k->kes=NULL;
1166
0
  k->kds=kate_decode_state_create();
1167
0
  if (!k->kds) return KATE_E_OUT_OF_MEMORY;
1168
1169
0
  return 0;
1170
0
}
1171
1172
#define READ_OVERRIDE(read) \
1173
0
  do { \
1174
0
    if (kate_pack_read1(kpb)) { read; } \
1175
0
  } while(0)
1176
1177
#if 0 && defined DEBUG
1178
#include <unistd.h>
1179
#include <sys/types.h>
1180
#include <time.h>
1181
#include <stdlib.h>
1182
#define RNDERR(label) \
1183
  do { \
1184
    static int seed=0; \
1185
    if (!seed) { \
1186
      const char *env=getenv("KATE_RAND_SEED"); \
1187
      if (env) srand(atoi(env)); else srand(time(NULL)^getpid()); \
1188
      seed=1; \
1189
    } \
1190
    if (((rand()>>8)&0xff)==0) { \
1191
      ret=KATE_E_OUT_OF_MEMORY; \
1192
      goto label; \
1193
    } \
1194
  } while(0)
1195
#else
1196
0
#define RNDERR(label) ((void)0)
1197
#endif
1198
1199
static int kate_decode_text_packet(kate_state *k,kate_pack_buffer *kpb,int repeat)
1200
0
{
1201
0
  KMG_GUARD();
1202
0
  int ret,n;
1203
0
  int len;
1204
0
  char *text;
1205
0
  kate_decode_state *kds;
1206
0
  kate_event *ev;
1207
1208
0
  if (!k || !kpb) return KATE_E_INVALID_PARAMETER;
1209
0
  if (!k->kds) return KATE_E_INIT;
1210
1211
0
  ret=kate_decode_state_clear(k->kds,k->ki,1);
1212
0
  if (ret<0) return ret;
1213
1214
0
  kds=k->kds;
1215
0
  ev=kds->event;
1216
1217
0
  ev->start=kate_read64(kpb);
1218
0
  ev->duration=kate_read64(kpb);
1219
0
  ev->backlink=kate_read64(kpb);
1220
0
  if (ev->start<0 || ev->duration<0) goto error_bad_packet;
1221
0
  if (ev->backlink<0 || ev->backlink>ev->start) goto error_bad_packet;
1222
0
  RNDERR(error_bad_packet);
1223
1224
0
  ev->start_time=kate_granule_duration(k->ki,ev->start);
1225
0
  ev->end_time=ev->start_time+kate_granule_duration(k->ki,ev->duration);
1226
1227
0
  len=kate_read32(kpb);
1228
0
  if (len<0) goto error_bad_packet;
1229
0
  RNDERR(error_bad_packet);
1230
0
  if (!k->ki->no_limits && len>KATE_LIMIT_TEXT_LENGTH) goto error_limit;
1231
0
  RNDERR(error_limit);
1232
1233
  /* read the text, and null terminate it - 4 characters for UTF-32 */
1234
0
  if (kate_check_add_overflow(len,4,NULL)) goto error_out_of_memory;
1235
0
  text=(char*)KMG_MALLOC(len+4);
1236
0
  if (!text) goto error_out_of_memory;
1237
0
  RNDERR(error_out_of_memory);
1238
0
  ret=kate_readbuf(kpb,text,len);
1239
0
  if (ret<0) goto error_bad_packet;
1240
0
  text[len]=0;
1241
0
  text[len+1]=0;
1242
0
  text[len+2]=0;
1243
0
  text[len+3]=0;
1244
  /* we can't validate the text yet, as we don't know whether there's a text encoding override later,
1245
     so we delay validation till we've read the overrides */
1246
1247
0
  ev->text=text;
1248
0
  ev->len=len;
1249
0
  ev->len0=len+4;
1250
1251
0
  if (kate_pack_read1(kpb)) {
1252
0
    ev->id=kate_read32v(kpb);
1253
0
  }
1254
1255
0
  if (repeat && ev->id>=0) {
1256
    /* if this is a repeat, check if we have this event already */
1257
0
    ret=kate_decode_state_find_event(k->kds,ev->id);
1258
0
    if (ret<0 && ret!=KATE_E_NOT_FOUND) goto error;
1259
0
    if (ret>=0) goto ignore;
1260
0
  }
1261
1262
0
  if (kate_pack_read1(kpb)) {
1263
0
    kate_motion **motions=NULL;
1264
0
    size_t nmotions=0;
1265
1266
0
    len=kate_read32v(kpb);
1267
0
    if (len<=0) goto error_bad_packet; /* if the flag was set, there's at least one */
1268
0
    if (!k->ki->no_limits && len>KATE_LIMIT_TEXT_MOTIONS) goto error_limit;
1269
0
    RNDERR(error_limit);
1270
0
    motions=(kate_motion**)KMG_CHECKED_MALLOC(len,sizeof(kate_motion*));
1271
0
    if (!motions) goto error_out_of_memory;
1272
0
    RNDERR(error_out_of_memory);
1273
0
    nmotions=0;
1274
0
    for (n=0;n<len;++n) {
1275
0
      if (kate_pack_read1(kpb)) {
1276
0
        size_t idx=kate_read32v(kpb);
1277
0
        if (idx>=k->ki->nmotions) goto error_bad_packet;
1278
0
        RNDERR(error_bad_packet);
1279
0
        motions[n]=k->ki->motions[idx];
1280
0
      }
1281
0
      else {
1282
0
        motions[n]=KMG_MALLOC(sizeof(kate_motion));
1283
0
        if (!motions[n]) goto error_out_of_memory;
1284
0
        RNDERR(error_out_of_memory);
1285
0
        ret=kate_decode_motion(k->ki,motions[n],kpb,&kmg);
1286
0
        if (kate_overread(kpb)) goto error_bad_packet;
1287
0
        if (ret<0) goto error;
1288
0
        RNDERR(error);
1289
0
      }
1290
0
      ++nmotions;
1291
0
    }
1292
1293
0
    ev->motions=motions;
1294
0
    ev->nmotions=nmotions;
1295
0
  }
1296
1297
0
  if (kate_pack_read1(kpb)) {
1298
0
    READ_OVERRIDE(ev->text_encoding=kate_pack_read(kpb,8));
1299
0
    READ_OVERRIDE(ev->text_directionality=kate_pack_read(kpb,8));
1300
0
    READ_OVERRIDE(
1301
0
      do {
1302
0
        len=kate_read32v(kpb);
1303
0
        if (len<0) goto error_bad_packet;
1304
0
        RNDERR(error_bad_packet);
1305
0
        if (!k->ki->no_limits && len>KATE_LIMIT_LANGUAGE_LENGTH) goto error_limit;
1306
0
        RNDERR(error_limit);
1307
0
        if (len>0) {
1308
0
          ev->language=(char*)KMG_MALLOC(len+1);
1309
0
          if (!ev->language) goto error_out_of_memory;
1310
0
        RNDERR(error_out_of_memory);
1311
0
          ret=kate_readbuf(kpb,ev->language,len);
1312
0
          if (ret<0) goto error_bad_packet;
1313
0
          ev->language[len]=0;
1314
0
        }
1315
0
      } while(0)
1316
0
    );
1317
0
    READ_OVERRIDE(
1318
0
      size_t idx=kate_read32v(kpb);
1319
0
      if (idx>=k->ki->nregions) goto error_bad_packet;
1320
0
      RNDERR(error_bad_packet);
1321
0
      ev->region=k->ki->regions[idx];
1322
0
    );
1323
0
    READ_OVERRIDE(
1324
0
      ev->region=KMG_MALLOC(sizeof(kate_region));
1325
0
      if (!ev->region) goto error_out_of_memory;
1326
0
      RNDERR(error_out_of_memory);
1327
0
      ret=kate_decode_region(k->ki,ev->region,kpb);
1328
0
      if (ret<0) goto error;
1329
0
    );
1330
0
    READ_OVERRIDE(
1331
0
      size_t idx=kate_read32v(kpb);
1332
0
      if (idx>=k->ki->nstyles) goto error_bad_packet;
1333
0
      RNDERR(error_bad_packet);
1334
0
      ev->style=k->ki->styles[idx]
1335
0
    );
1336
0
    READ_OVERRIDE(
1337
0
      ev->style=KMG_MALLOC(sizeof(kate_style));
1338
0
      if (!ev->style) goto error_out_of_memory;
1339
0
      RNDERR(error_out_of_memory);
1340
0
      ret=kate_decode_style(k->ki,ev->style,kpb,&kmg);
1341
0
      if (ret<0) goto error;
1342
0
    );
1343
0
    READ_OVERRIDE(
1344
0
      size_t idx=kate_read32v(kpb);
1345
0
      if (idx>=k->ki->nstyles) goto error_bad_packet;
1346
0
      RNDERR(error_bad_packet);
1347
0
      ev->secondary_style=k->ki->styles[idx]
1348
0
    );
1349
0
    READ_OVERRIDE(
1350
0
      ev->secondary_style=KMG_MALLOC(sizeof(kate_style));
1351
0
      if (!ev->secondary_style) goto error_out_of_memory;
1352
0
      RNDERR(error_out_of_memory);
1353
0
      ret=kate_decode_style(k->ki,ev->secondary_style,kpb,&kmg);
1354
0
      if (ret<0) goto error;
1355
0
      RNDERR(error);
1356
0
    );
1357
0
    READ_OVERRIDE(
1358
0
      size_t idx=kate_read32v(kpb);
1359
0
      if (idx>=k->ki->nfont_mappings) goto error_bad_packet;
1360
0
      RNDERR(error_bad_packet);
1361
0
      ev->font_mapping=k->ki->font_mappings[idx]
1362
0
    );
1363
0
  }
1364
1365
0
  if (((k->ki->bitstream_version_major<<8)|k->ki->bitstream_version_minor)>=0x0002) {
1366
    /* 0.2 adds a warp for palette, bitmap, markup type */
1367
0
    kate_read32v(kpb); /* the size of the warp */
1368
0
    if (kate_pack_read1(kpb)) {
1369
0
      READ_OVERRIDE(
1370
0
        size_t idx=kate_read32v(kpb);
1371
0
        if (idx>=k->ki->npalettes) goto error_bad_packet;
1372
0
        RNDERR(error_bad_packet);
1373
0
        ev->palette=k->ki->palettes[idx];
1374
0
      );
1375
0
      READ_OVERRIDE(
1376
0
        ev->palette=KMG_MALLOC(sizeof(kate_palette));
1377
0
        if (!ev->palette) goto error_out_of_memory;
1378
0
        ret=kate_decode_palette(k->ki,ev->palette,kpb);
1379
0
        if (ret<0) goto error;
1380
0
        RNDERR(error);
1381
0
      );
1382
0
      READ_OVERRIDE(
1383
0
        size_t idx=kate_read32v(kpb);
1384
0
        if (idx>=k->ki->nbitmaps) goto error_bad_packet;
1385
0
        ev->bitmap=k->ki->bitmaps[idx];
1386
0
      );
1387
0
      READ_OVERRIDE(
1388
0
        ev->bitmap=KMG_MALLOC(sizeof(kate_bitmap));
1389
0
        if (!ev->bitmap) goto error_out_of_memory;
1390
0
        RNDERR(error_out_of_memory);
1391
0
        ret=kate_decode_bitmap(k->ki,ev->bitmap,kpb);
1392
0
        if (ret<0) goto error;
1393
0
        RNDERR(error);
1394
0
      );
1395
0
      READ_OVERRIDE(ev->text_markup_type=kate_pack_read(kpb,8));
1396
0
    }
1397
0
  }
1398
1399
0
  if (((k->ki->bitstream_version_major<<8)|k->ki->bitstream_version_minor)>=0x0004) {
1400
    /* 0.4 adds a warp for bitmaps */
1401
0
    kate_bitmap **bitmaps=NULL;
1402
0
    size_t nbitmaps=0;
1403
1404
0
    kate_read32v(kpb); /* the size of the warp */
1405
1406
0
    len=kate_read32v(kpb);
1407
0
    if (len<0) goto error_bad_packet;
1408
0
    RNDERR(error_bad_packet);
1409
0
    if (len>0) {
1410
0
      if (!k->ki->no_limits && len>KATE_LIMIT_BITMAPS) goto error_limit;
1411
0
      bitmaps=(kate_bitmap**)KMG_CHECKED_MALLOC(len,sizeof(kate_bitmap*));
1412
0
      if (!bitmaps) goto error_out_of_memory;
1413
0
      RNDERR(error_out_of_memory);
1414
0
      nbitmaps=0;
1415
0
      for (n=0;n<len;++n) {
1416
0
        if (kate_pack_read1(kpb)) {
1417
0
          size_t idx=kate_read32v(kpb);
1418
0
          if (idx>=k->ki->nbitmaps) goto error_bad_packet;
1419
0
          RNDERR(error_bad_packet);
1420
0
          bitmaps[n]=k->ki->bitmaps[idx];
1421
0
        }
1422
0
        else {
1423
0
          bitmaps[n]=KMG_MALLOC(sizeof(kate_bitmap));
1424
0
          if (!bitmaps[n]) goto error_out_of_memory;
1425
0
          RNDERR(error_out_of_memory);
1426
0
          ret=kate_decode_bitmap(k->ki,bitmaps[n],kpb);
1427
0
          if (kate_overread(kpb)) goto error_bad_packet;
1428
0
          if (ret<0) goto error;
1429
0
        }
1430
0
        ++nbitmaps;
1431
0
      }
1432
0
    }
1433
1434
0
    ev->bitmaps=bitmaps;
1435
0
    ev->nbitmaps=nbitmaps;
1436
0
  }
1437
0
  else {
1438
0
    ev->bitmaps=NULL;
1439
0
    ev->nbitmaps=0;
1440
0
  }
1441
1442
0
  if (((k->ki->bitstream_version_major<<8)|k->ki->bitstream_version_minor)>=0x0006) {
1443
    /* 0.6 adds a warp for metadata */
1444
0
    kate_read32v(kpb); /* the size of the warp */
1445
1446
0
    ret=kate_read_metadata(kpb,&ev->meta);
1447
0
    if (ret<0) goto error;
1448
0
  }
1449
1450
0
  ret=kate_warp(kpb);
1451
0
  if (ret<0) goto error;
1452
0
  RNDERR(error);
1453
1454
  /* validate text, and reject invalid encodings */
1455
0
  ret=kate_text_validate(ev->text_encoding,ev->text,ev->len0);
1456
0
  RNDERR(error);
1457
0
  if (ret<0) goto error;
1458
1459
0
  if (ev->text_markup_type!=kate_markup_none && k->ki->remove_markup) {
1460
0
    size_t zero_size=ev->len0-ev->len;
1461
0
    ret=kate_text_remove_markup(ev->text_encoding,ev->text,&ev->len0);
1462
0
    if (ret<0) goto error;
1463
0
    if (ev->len0<zero_size) goto error_bad_packet; /* can't happen, but let's be safe */
1464
0
    ev->len=ev->len0-zero_size;
1465
0
    RNDERR(error);
1466
0
    ev->text_markup_type=kate_markup_none;
1467
0
  }
1468
1469
  /* if no style, use the region (if any) default style */
1470
0
  if (!ev->style && ev->region) {
1471
0
    if (ev->region->style>=0) {
1472
0
      size_t idx=ev->region->style;
1473
0
      if (idx<k->ki->nstyles) {
1474
0
        ev->style=k->ki->styles[idx];
1475
0
      }
1476
0
    }
1477
0
  }
1478
1479
0
  if (ev->id>=0) {
1480
    /* register the event as active */
1481
0
    ret=kate_decode_state_add_event(k->kds,ev);
1482
0
    RNDERR(error);
1483
0
    if (ret<0) goto error;
1484
0
  }
1485
1486
0
  ret=kate_decode_state_flush_events(k->kds,ev->start);
1487
0
  RNDERR(error);
1488
0
  if (ret<0) goto error;
1489
1490
0
  return KMG_OK();
1491
1492
0
error_limit:
1493
0
  ret=KATE_E_LIMIT;
1494
0
  goto error;
1495
1496
0
error_bad_packet:
1497
0
  ret=KATE_E_BAD_PACKET;
1498
0
  goto error;
1499
1500
0
error_out_of_memory:
1501
0
  ret=KATE_E_OUT_OF_MEMORY;
1502
0
  goto error;
1503
1504
0
ignore:
1505
  /* special case where we need to destroy the event as we're not interested in it,
1506
     but we do not want an error to be raised */
1507
0
  ret=0;
1508
0
  goto error;
1509
1510
0
error:
1511
  /* all the memory allocated for the event should be freed by the kate_memory_guard,
1512
     so we just unlink and jettison it */
1513
0
  kate_free(kds->event);
1514
0
  kds->event=NULL;
1515
0
  return KMG_ERROR(ret);
1516
0
}
1517
1518
static int kate_decode_repeat_packet(kate_state *k,kate_pack_buffer *kpb)
1519
0
{
1520
0
  return kate_decode_text_packet(k,kpb,1);
1521
0
}
1522
1523
static int kate_decode_keepalive_packet(kate_state *k,kate_pack_buffer *kpb)
1524
0
{
1525
0
  if (!k || !kpb) return KATE_E_INVALID_PARAMETER;
1526
0
  if (!k->kds) return KATE_E_INIT;
1527
1528
0
  return 0;
1529
0
}
1530
1531
static int kate_decode_end_packet(kate_state *k,kate_pack_buffer *kpb)
1532
0
{
1533
0
  if (!k || !kpb) return KATE_E_INVALID_PARAMETER;
1534
0
  if (!k->kds) return KATE_E_INIT;
1535
1536
0
  return 1;
1537
0
}
1538
1539
/**
1540
  \ingroup decoding
1541
  Decodes a data packet.
1542
  \param k the kate_state to decode to
1543
  \param kp the packet to decode
1544
  \returns 0 success
1545
  \returns 1 success, and we're at end of stream
1546
  \returns KATE_E_* error
1547
  */
1548
int kate_decode_packetin(kate_state *k,kate_packet *kp)
1549
0
{
1550
0
  kate_pack_buffer kpb;
1551
0
  int ret,id;
1552
1553
0
  if (!k || !kp) return KATE_E_INVALID_PARAMETER;
1554
0
  if (!k->ki) return KATE_E_INIT;
1555
0
  if (!k->kds) return KATE_E_INIT;
1556
1557
0
  ret=kate_decode_state_clear(k->kds,k->ki,0);
1558
0
  if (ret<0) return ret;
1559
1560
0
  kate_pack_readinit(&kpb,kp->data,kp->nbytes);
1561
0
  id=kate_pack_read(&kpb,8);
1562
0
  if (id&0x80) {
1563
    /* we have a header - we'll ignore it
1564
       it may have happened either because we seeked to the beginning of the stream
1565
       or because we're parsing a stream that has more headers than we know about */
1566
0
    return 0;
1567
0
  }
1568
1569
0
  switch (id) {
1570
0
    case 0x00: return kate_decode_text_packet(k,&kpb,0);
1571
0
    case 0x01: return kate_decode_keepalive_packet(k,&kpb);
1572
0
    case 0x02: return kate_decode_repeat_packet(k,&kpb);
1573
0
    case 0x7f: return kate_decode_end_packet(k,&kpb);
1574
0
    default: return 0; /* unknown data packets are ignored, for future proofing */
1575
0
  }
1576
1577
0
  return 0;
1578
0
}
1579
1580
/**
1581
  \ingroup decoding
1582
  Requests a pointer to the event decoded by the last packet, if there is one.
1583
  \param k the kate_state to get the event from
1584
  \param event a pointer to where to place the pointer to the event, if any
1585
  \returns 0 success, an event was returned
1586
  \returns 1 success, but there was no event to return
1587
  \returns KATE_E_* error
1588
  */
1589
int kate_decode_eventout(kate_state *k,kate_const kate_event **event)
1590
0
{
1591
0
  if (!k) return KATE_E_INVALID_PARAMETER;
1592
0
  if (!k->kds) return KATE_E_INIT;
1593
1594
0
  if (!k->kds->event) return 1;
1595
1596
0
  if (event) *event=k->kds->event;
1597
1598
0
  return 0;
1599
0
}
1600
1601
/**
1602
  \ingroup decoding
1603
  Informs the Kate decoder that seeking has occured.
1604
  This will cause the decoder to use any future repeat packets to recover
1605
  data from past events which are still active, but which original packets
1606
  were before the seek point, and therefore unavailable.
1607
  \param k the kate_state for which a seek occured
1608
  \returns 0 success
1609
  \returns KATE_E_* error
1610
  */
1611
int kate_decode_seek(kate_state *k)
1612
0
{
1613
0
  if (!k) return KATE_E_INVALID_PARAMETER;
1614
0
  if (!k->kds) return KATE_E_INIT;
1615
1616
0
  return kate_decode_state_flush_events(k->kds,-1);
1617
0
}
1618