- Main Page
- Related Pages
- Data Structures
- Files
- Examples

- File List

# /home/v/Kate/WORK_TREES/kate-sorceress-backup/v/src/kate/src/kate_decode.c

    00001 /* Copyright (C) 2008, 2009 Vincent Penquerc'h.
    00002    This file is part of the Kate codec library.
    00003    Written by Vincent Penquerc'h.
    00004
    00005    Use, distribution and reproduction of this library is governed
    00006    by a BSD style source license included with this source in the
    00007    file 'COPYING'. Please read these terms before distributing. */
    00008
    00009
    00010 #define KATE_INTERNAL
    00011 #include "kate_internal.h"
    00012
    00013 #ifdef HAVE_STDLIB_H
    00014 #include <stdlib.h>
    00015 #endif
    00016 #ifdef HAVE_STRING_H
    00017 #include <string.h>
    00018 #endif
    00019 #include "kate/kate.h"
    00020 #include "kate_decode_state.h"
    00021 #include "kate_fp.h"
    00022 #include "kate_rle.h"
    00023
    00025 typedef struct kate_memory_guard {
    00026   size_t size;
    00027   void **pointers;
    00028 } kate_memory_guard;
    00029
    00030 static void *kate_memory_guard_malloc(kate_memory_guard *kmg,size_t size)
    00031 {
    00032   void **new_pointers;
    00033   void *ptr;
    00034   int ret;
    00035
    00036   ret=kate_check_add_overflow(kmg->size,1,NULL);
    00037   if (ret<0) return NULL;
    00038
    00039   ptr=kate_malloc(size);
    00040   if (!ptr) return NULL;
    00041   new_pointers=(void**)kate_checked_realloc(kmg->pointers,(kmg->size+1),sizeof(void*));
    00042   if (!new_pointers) {
    00043     kate_free(ptr);
    00044     return NULL;
    00045   }
    00046   kmg->pointers=new_pointers;
    00047   kmg->pointers[kmg->size++]=ptr;
    00048   return ptr;
    00049 }
    00050
    00051 static void *kate_memory_guard_checked_malloc(kate_memory_guard *kmg,size_t size1,size_t size2)
    00052 {
    00053   size_t size;
    00054   int ret=kate_check_mul_overflow(size1,size2,&size);
    00055   if (ret<0) return NULL;
    00056   return kate_memory_guard_malloc(kmg,size);
    00057 }
    00058
    00059 static void kate_memory_guard_destroy(kate_memory_guard *kmg,int free_pointers)
    00060 {
    00061   size_t n;
    00062   if (free_pointers) {
    00063     for (n=0;n<kmg->size;++n) kate_free(kmg->pointers[n]);
    00064   }
    00065   if (kmg->pointers) kate_free(kmg->pointers);
    00066 }
    00067
    00068 static int kate_memory_guard_merge(kate_memory_guard *kmg,kate_memory_guard *parent_kmg)
    00069 {
    00070   void **new_pointers;
    00071   int ret;
    00072   size_t new_size;
    00073
    00074   ret=kate_check_add_overflow(parent_kmg->size,kmg->size,&new_size);
    00075   if (ret<0) return ret;
    00076
    00077   new_pointers=(void**)kate_checked_realloc(parent_kmg->pointers,new_size,sizeof(void*));
    00078   if (!new_pointers) {
    00079     kate_memory_guard_destroy(kmg,1);
    00080     return KATE_E_OUT_OF_MEMORY;
    00081   }
    00082   parent_kmg->pointers=new_pointers;
    00083   memcpy(parent_kmg->pointers+parent_kmg->size,kmg->pointers,kmg->size*sizeof(void*));
    00084   parent_kmg->size=new_size;
    00085   kate_memory_guard_destroy(kmg,0);
    00086   return 0;
    00087 }
    00088
    00089 #define KMG_GUARD() kate_memory_guard kmg={0,NULL}
    00090 #define KMG_MALLOC(size) kate_memory_guard_malloc(&kmg,size)
    00091 #define KMG_CHECKED_MALLOC(size1,size2) kate_memory_guard_checked_malloc(&kmg,size1,size2)
    00092 #define KMG_MERGE(parent_kmg) kate_memory_guard_merge(&kmg,parent_kmg)
    00093 #define KMG_ERROR(ret) (kate_memory_guard_destroy(&kmg,1),ret)
    00094 #define KMG_OK() (kate_memory_guard_destroy(&kmg,0),0)
    00095
    00096
    00097
    00098 static int kate_readbuf(kate_pack_buffer *kpb,char *s,int len)
    00099 {
    00100   if (len<0) return KATE_E_INVALID_PARAMETER;
    00101   if ((kate_pack_readable_bits(kpb)+7)/8<len) return KATE_E_BAD_PACKET;
    00102   while (len--) *s++=kate_pack_read(kpb,8);
    00103   return 0;
    00104 }
    00105
    00106 static kate_int32_t kate_read32(kate_pack_buffer *kpb)
    00107 {
    00108   kate_int32_t v=0;
    00109   v|=kate_pack_read(kpb,8);
    00110   v|=(kate_pack_read(kpb,8)<<8);
    00111   v|=(kate_pack_read(kpb,8)<<16);
    00112   v|=(kate_pack_read(kpb,8)<<24);
    00113   return v;
    00114 }
    00115
    00116 static kate_int32_t kate_read32v(kate_pack_buffer *kpb)
    00117 {
    00118   /* an error (negative) will be returned as negative, propagating the error
    00119      to the caller even though the expected range for 4 bits is 0-15 */
    00120   int smallv=kate_pack_read(kpb,4);
    00121   if (smallv==15) {
    00122     int sign=kate_pack_read1(kpb);
    00123     int bits=kate_pack_read(kpb,5)+1;
    00124     kate_int32_t v=kate_pack_read(kpb,bits);
    00125     if (sign) v=-v;
    00126     return v;
    00127   }
    00128   else return smallv;
    00129 }
    00130
    00131 static kate_int64_t kate_read64(kate_pack_buffer *kpb)
    00132 {
    00133   kate_int32_t vl=kate_read32(kpb);
    00134   kate_int32_t vh=kate_read32(kpb);
    00135   return (0xffffffff&(kate_int64_t)vl)|(((kate_int64_t)vh)<<32);
    00136 }
    00137
    00138 static int kate_overread(kate_pack_buffer *kpb)
    00139 {
    00140   return kate_pack_look(kpb,0)<0;
    00141 }
    00142
    00143 static int kate_warp(kate_pack_buffer *kpb)
    00144 {
    00145   while (1) {
    00146     kate_int32_t bits=kate_read32v(kpb);
    00147     if (!bits) break;
    00148     if (bits<0) return KATE_E_BAD_PACKET;
    00149     kate_pack_adv(kpb,bits);
    00150   }
    00151   return 0;
    00152 }
    00153
    00154 static int kate_decode_check_magic(kate_pack_buffer *kpb)
    00155 {
    00156   char magic[8];
    00157   int ret;
    00158
    00159   if (!kpb) return KATE_E_INVALID_PARAMETER;
    00160
    00161   ret=kate_readbuf(kpb,magic,7);
    00162   if (ret<0) return KATE_E_NOT_KATE;
    00163   if (memcmp(magic,"kate\0\0\0",7)) return KATE_E_NOT_KATE;
    00164
    00165   return 0;
    00166 }
    00167
    00175 int kate_decode_is_idheader(const kate_packet *kp)
    00176 {
    00177   kate_pack_buffer kpb;
    00178   unsigned char headerid;
    00179
    00180   if (!kp) return 0;
    00181
    00182   kate_pack_readinit(&kpb,kp->data,kp->nbytes);
    00183   headerid=kate_pack_read(&kpb,8);
    00184   if (headerid!=0x80) return 0;
    00185
    00186   return !kate_decode_check_magic(&kpb);
    00187 }
    00188
    00189 static int kate_check_eop(kate_pack_buffer *kpb)
    00190 {
    00191   int bits;
    00192
    00193   if (!kpb) return KATE_E_INVALID_PARAMETER;
    00194
    00195   /* ensure any remaining bits in the current byte are zero (reading 0 bytes yields zero) */
    00196   bits=7&(8-(kate_pack_bits(kpb)&7));
    00197   if (bits>0) {
    00198     if (kate_pack_read(kpb,bits)) return KATE_E_BAD_PACKET;
    00199   }
    00200   if (kate_pack_look1(kpb)>=0) return KATE_E_BAD_PACKET; /* no more data expected */
    00201
    00202   return 0;
    00203 }
    00204
    00205 static int kate_decode_read_canvas_size(kate_pack_buffer *kpb)
    00206 {
    00207   size_t base,shift;
    00208
    00209   if (!kpb) return KATE_E_INVALID_PARAMETER;
    00210
    00211   shift=kate_pack_read(kpb,4);
    00212   base=kate_pack_read(kpb,4);
    00213   base|=(kate_pack_read(kpb,8)<<4);
    00214
    00215   return base<<shift;
    00216 }
    00217
    00218 #define KATE_IS_BITSTREAM_LOE(major,minor) (major)
    00219
    00220 static int kate_decode_info_header(kate_info *ki,kate_pack_buffer *kpb)
    00221 {
    00222   KMG_GUARD();
    00223   int len;
    00224   int ret;
    00225   char *language,*category;
    00226   int reserved;
    00227
    00228   if (!ki || !kpb) return KMG_ERROR(KATE_E_INVALID_PARAMETER);
    00229
    00230   ki->bitstream_version_major=kate_pack_read(kpb,8);
    00231   ki->bitstream_version_minor=kate_pack_read(kpb,8);
    00232   if (ki->bitstream_version_major>KATE_BITSTREAM_VERSION_MAJOR) return KMG_ERROR(KATE_E_VERSION);
    00233
    00234   ki->num_headers=kate_pack_read(kpb,8);
    00235   if (ki->num_headers<1) return KMG_ERROR(KATE_E_BAD_PACKET);
    00236   ki->text_encoding=kate_pack_read(kpb,8);
    00237   ki->text_directionality=kate_pack_read(kpb,8);
    00238   reserved=kate_pack_read(kpb,8);
    00239   if (ki->bitstream_version_major==0 && ki->bitstream_version_minor<3) {
    00240     if (reserved!=0) return KMG_ERROR(KATE_E_BAD_PACKET); /* reserved - 0 */
    00241   }
    00242   ki->granule_shift=kate_pack_read(kpb,8);
    00243
    00244   ret=kate_decode_read_canvas_size(kpb);
    00245   if (ret<0) return KMG_ERROR(KATE_E_BAD_PACKET);
    00246   ki->original_canvas_width=ret;
    00247   ret=kate_decode_read_canvas_size(kpb);
    00248   if (ret<0) return KMG_ERROR(KATE_E_BAD_PACKET);
    00249   ki->original_canvas_height=ret;
    00250
    00251   reserved=kate_read32(kpb);
    00252   if (ki->bitstream_version_major==0 && ki->bitstream_version_minor<3) {
    00253     if (reserved!=0) return KMG_ERROR(KATE_E_BAD_PACKET); /* reserved - 0 */
    00254   }
    00255
    00256   ki->gps_numerator=kate_read32(kpb);
    00257   ki->gps_denominator=kate_read32(kpb);
    00258
    00259   if (ki->granule_shift>=64) return KMG_ERROR(KATE_E_BAD_PACKET);
    00260   if (ki->gps_numerator==0) return KMG_ERROR(KATE_E_BAD_PACKET);
    00261   if (ki->gps_denominator==0) return KMG_ERROR(KATE_E_BAD_PACKET);
    00262   /*
    00263   should not be needed anymore, calculations on these should be ok with full 32 bit range
    00264   if ((ki->gps_numerator^ki->gps_denominator)<0) return KMG_ERROR(KATE_E_BAD_PACKET);
    00265   */
    00266
    00267   len=16;
    00268   language=(char*)KMG_MALLOC(len);
    00269   if (!language) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
    00270   ret=kate_readbuf(kpb,language,len); /* a terminating null is included */
    00271   if (ret<0) return KMG_ERROR(ret);
    00272   if (language[len-1]) return KMG_ERROR(KATE_E_BAD_PACKET); /* but do check it to be sure */
    00273
    00274   len=16;
    00275   category=(char*)KMG_MALLOC(len);
    00276   if (!category) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
    00277   ret=kate_readbuf(kpb,category,len); /* a terminating null is included */
    00278   if (ret<0) return KMG_ERROR(ret);
    00279   if (category[len-1]) return KMG_ERROR(KATE_E_BAD_PACKET); /* but do check it to be sure */
    00280
    00281   ret=kate_check_eop(kpb);
    00282   if (ret<0) return KMG_ERROR(ret);
    00283
    00284   ki->language=language;
    00285   ki->category=category;
    00286
    00287   return KMG_OK();
    00288 }
    00289
    00290 static int kate_decode_comment_packet(const kate_info *ki,kate_comment *kc,kate_pack_buffer *kpb)
    00291 {
    00292   KMG_GUARD();
    00293   int ret,len,nc;
    00294   char **user_comments,*vendor;
    00295   int comments,*comment_lengths;
    00296
    00297   if (!ki || !kc || !kpb) return KMG_ERROR(KATE_E_INVALID_PARAMETER);
    00298
    00299   len=kate_read32(kpb);
    00300   if (len<0) return KMG_ERROR(KATE_E_BAD_PACKET);
    00301   if (!ki->no_limits && len>KATE_LIMIT_COMMENT_LENGTH) return KMG_ERROR(KATE_E_LIMIT);
    00302   vendor=(char*)KMG_MALLOC(len+1);
    00303   if (!vendor) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
    00304   ret=kate_readbuf(kpb,vendor,len);
    00305   if (ret<0) return KMG_ERROR(ret);
    00306   vendor[len]=0;
    00307
    00308   comments=kate_read32(kpb);
    00309   if (comments<0) return KMG_ERROR(KATE_E_BAD_PACKET);
    00310   if (!ki->no_limits && len>KATE_LIMIT_COMMENTS) return KMG_ERROR(KATE_E_LIMIT);
    00311   user_comments=(char**)KMG_CHECKED_MALLOC(comments,sizeof(char*));
    00312   comment_lengths=(int*)KMG_CHECKED_MALLOC(comments,sizeof(int));
    00313   if (!user_comments || !comment_lengths) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
    00314
    00315   for (nc=0;nc<comments;++nc) user_comments[nc]=NULL;
    00316   for (nc=0;nc<comments;++nc) {
    00317     len=kate_read32(kpb);
    00318     if (len<0) return KMG_ERROR(KATE_E_BAD_PACKET);
    00319     if (!ki->no_limits && len>KATE_LIMIT_COMMENT_LENGTH) return KMG_ERROR(KATE_E_LIMIT);
    00320     user_comments[nc]=(char*)KMG_MALLOC(len+1);
    00321     if (!user_comments[nc]) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
    00322     if (len) {
    00323       ret=kate_readbuf(kpb,user_comments[nc],len);
    00324       if (ret<0) return KMG_ERROR(ret);
    00325     }
    00326     user_comments[nc][len]=0;
    00327     comment_lengths[nc]=len;
    00328   }
    00329
    00330   ret=kate_check_eop(kpb);
    00331   if (ret<0) return KMG_ERROR(ret);
    00332
    00333   kc->user_comments=user_comments;
    00334   kc->comment_lengths=comment_lengths;
    00335   kc->comments=comments;
    00336   kc->vendor=vendor;
    00337
    00338   return KMG_OK();
    00339 }
    00340
    00341 static int kate_decode_region(const kate_info *ki,kate_region *kr,kate_pack_buffer *kpb)
    00342 {
    00343   if (!kr || !kpb) return KATE_E_INVALID_PARAMETER;
    00344   kr->metric=kate_pack_read(kpb,8);
    00345   kr->x=kate_read32v(kpb);
    00346   kr->y=kate_read32v(kpb);
    00347   kr->w=kate_read32v(kpb);
    00348   kr->h=kate_read32v(kpb);
    00349   kr->style=kate_read32v(kpb);
    00350
    00351   if (((ki->bitstream_version_major<<8)|ki->bitstream_version_minor)>=0x0002) {
    00352     /* 0.2 adds a warp for clip */
    00353     kate_read32v(kpb); /* the size of the warp */
    00354     kr->clip=kate_pack_read1(kpb);
    00355   }
    00356   else {
    00357     kr->clip=0;
    00358   }
    00359
    00360   return kate_warp(kpb);
    00361 }
    00362
    00363 static int kate_decode_regions_packet(kate_info *ki,kate_pack_buffer *kpb)
    00364 {
    00365   KMG_GUARD();
    00366   int ret,n,nregions;
    00367   kate_region **regions;
    00368
    00369   if (!ki || !kpb) return KMG_ERROR(KATE_E_INVALID_PARAMETER);
    00370
    00371   nregions=kate_read32v(kpb);
    00372   if (nregions<0) return KMG_ERROR(KATE_E_BAD_PACKET);
    00373   if (!ki->no_limits && nregions>KATE_LIMIT_REGIONS) return KMG_ERROR(KATE_E_LIMIT);
    00374   regions=(kate_region**)KMG_CHECKED_MALLOC(nregions,sizeof(kate_region*));
    00375   if (!regions) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
    00376   for (n=0;n<nregions;++n) {
    00377     regions[n]=(kate_region*)KMG_MALLOC(sizeof(kate_region));
    00378     if (!regions[n]) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
    00379     ret=kate_decode_region(ki,regions[n],kpb);
    00380     if (kate_overread(kpb)) return KMG_ERROR(KATE_E_BAD_PACKET);
    00381     if (ret<0) return KMG_ERROR(ret);
    00382   }
    00383
    00384   ret=kate_warp(kpb);
    00385   if (ret<0) return KMG_ERROR(ret);
    00386
    00387   ret=kate_check_eop(kpb);
    00388   if (ret<0) return KMG_ERROR(ret);
    00389
    00390   ki->nregions=nregions;
    00391   ki->regions=regions;
    00392
    00393   return KMG_OK();
    00394 }
    00395
    00396 static int kate_decode_color(kate_color *kc,kate_pack_buffer *kpb)
    00397 {
    00398   if (!kc || !kpb) return KATE_E_INVALID_PARAMETER;
    00399   kc->r=kate_pack_read(kpb,8);
    00400   kc->g=kate_pack_read(kpb,8);
    00401   kc->b=kate_pack_read(kpb,8);
    00402   kc->a=kate_pack_read(kpb,8);
    00403   return 0;
    00404 }
    00405
    00406 static int kate_decode_style(const kate_info *ki,kate_style *ks,kate_pack_buffer *kpb,kate_memory_guard *parent_kmg)
    00407 {
    00408   KMG_GUARD();
    00409   int ret;
    00410   kate_float d[8];
    00411   size_t idx;
    00412   int len;
    00413
    00414   if (!ks || !kpb) return KMG_ERROR(KATE_E_INVALID_PARAMETER);
    00415
    00416   ret=kate_fp_decode_kate_float(sizeof(d)/sizeof(d[0]),d,1,kpb);
    00417   if (ret<0) return KMG_ERROR(ret);
    00418
    00419   idx=0;
    00420   ks->halign=d[idx++];
    00421   ks->valign=d[idx++];
    00422   ks->font_width=d[idx++];
    00423   ks->font_height=d[idx++];
    00424   ks->left_margin=d[idx++];
    00425   ks->top_margin=d[idx++];
    00426   ks->right_margin=d[idx++];
    00427   ks->bottom_margin=d[idx++];
    00428   ret=kate_decode_color(&ks->text_color,kpb);
    00429   if (ret<0) return KMG_ERROR(ret);
    00430   ret=kate_decode_color(&ks->background_color,kpb);
    00431   if (ret<0) return KMG_ERROR(ret);
    00432   ret=kate_decode_color(&ks->draw_color,kpb);
    00433   if (ret<0) return KMG_ERROR(ret);
    00434   ks->font_metric=kate_pack_read(kpb,8);
    00435   ks->margin_metric=kate_pack_read(kpb,8);
    00436   ks->bold=kate_pack_read1(kpb);
    00437   ks->italics=kate_pack_read1(kpb);
    00438   ks->underline=kate_pack_read1(kpb);
    00439   ks->strike=kate_pack_read1(kpb);
    00440
    00441   if (((ki->bitstream_version_major<<8)|ki->bitstream_version_minor)>=0x0002) {
    00442     /* 0.2 adds a warp for justify and font */
    00443     kate_read32v(kpb); /* the size of the warp */
    00444     ks->justify=kate_pack_read1(kpb);
    00445     len=kate_read32v(kpb);
    00446     if (len<0) {
    00447       return KMG_ERROR(KATE_E_BAD_PACKET);
    00448     }
    00449     else if (len>0) {
    00450       ks->font=(char*)KMG_MALLOC(len+1);
    00451       if (!ks->font) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
    00452       ret=kate_readbuf(kpb,ks->font,len);
    00453       if (ret<0) return KMG_ERROR(ret);
    00454       ks->font[len]=0;
    00455     }
    00456     else {
    00457       ks->font=NULL;
    00458     }
    00459   }
    00460   else {
    00461     ks->justify=0;
    00462     ks->font=NULL;
    00463   }
    00464
    00465   if (((ki->bitstream_version_major<<8)|ki->bitstream_version_minor)>=0x0004) {
    00466     /* 0.4 adds a warp for wrap mode */
    00467     kate_read32v(kpb); /* the size of the warp */
    00468     ks->wrap_mode=kate_read32v(kpb);
    00469   }
    00470   else {
    00471     ks->wrap_mode=kate_wrap_word;
    00472   }
    00473
    00474   ret=kate_warp(kpb);
    00475   if (ret<0) return KMG_ERROR(ret);
    00476
    00477   return KMG_MERGE(parent_kmg);
    00478 }
    00479
    00480 static int kate_decode_styles_packet(kate_info *ki,kate_pack_buffer *kpb)
    00481 {
    00482   KMG_GUARD();
    00483   int ret,n,nstyles;
    00484   kate_style **styles;
    00485
    00486   if (!ki || !kpb) return KMG_ERROR(KATE_E_INVALID_PARAMETER);
    00487
    00488   nstyles=kate_read32v(kpb);
    00489   if (nstyles<0) return KMG_ERROR(KATE_E_BAD_PACKET);
    00490   if (!ki->no_limits && nstyles>KATE_LIMIT_STYLES) return KMG_ERROR(KATE_E_LIMIT);
    00491   styles=(kate_style**)KMG_CHECKED_MALLOC(nstyles,sizeof(kate_style*));
    00492   if (!styles) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
    00493   for (n=0;n<nstyles;++n) {
    00494     styles[n]=(kate_style*)KMG_MALLOC(sizeof(kate_style));
    00495     if (!styles[n]) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
    00496     ret=kate_decode_style(ki,styles[n],kpb,&kmg);
    00497     if (kate_overread(kpb)) return KMG_ERROR(KATE_E_BAD_PACKET);
    00498     if (ret<0) return KMG_ERROR(ret);
    00499   }
    00500
    00501   ret=kate_warp(kpb);
    00502   if (ret<0) return KMG_ERROR(ret);
    00503
    00504   ret=kate_check_eop(kpb);
    00505   if (ret<0) return KMG_ERROR(ret);
    00506
    00507   ki->nstyles=nstyles;
    00508   ki->styles=styles;
    00509
    00510   return KMG_OK();
    00511 }
    00512
    00513 static int kate_decode_curve(const kate_info *ki,kate_curve *kc,kate_pack_buffer *kpb,kate_memory_guard *parent_kmg)
    00514 {
    00515   KMG_GUARD();
    00516   int ret;
    00517
    00518   if (!ki || !kc || !kpb) return KMG_ERROR(KATE_E_INVALID_PARAMETER);
    00519
    00520   kc->type=kate_pack_read(kpb,8);
    00521   kc->npts=kate_read32v(kpb);
    00522   ret=kate_warp(kpb);
    00523   if (ret<0) return KMG_ERROR(ret);
    00524   if (!ki->no_limits && kc->npts>KATE_LIMIT_CURVE_POINTS) return KMG_ERROR(KATE_E_LIMIT);
    00525   kc->pts=(kate_float*)KMG_CHECKED_MALLOC(kc->npts,2*sizeof(kate_float));
    00526   if (!kc->pts) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
    00527
    00528   ret=kate_fp_decode_kate_float(kc->npts,kc->pts,2,kpb);
    00529   if (ret<0) return KMG_ERROR(ret);
    00530
    00531   /* on success, add our memory to the parent guard */
    00532   return KMG_MERGE(parent_kmg);
    00533 }
    00534
    00535 static int kate_decode_curves_packet(kate_info *ki,kate_pack_buffer *kpb)
    00536 {
    00537   KMG_GUARD();
    00538   int ret,n,ncurves;
    00539   kate_curve **curves=NULL;
    00540
    00541   if (!ki || !kpb) return KMG_ERROR(KATE_E_INVALID_PARAMETER);
    00542
    00543   ncurves=kate_read32v(kpb);
    00544   if (ncurves<0) return KMG_ERROR(KATE_E_BAD_PACKET);
    00545   if (!ki->no_limits && ncurves>KATE_LIMIT_CURVES) return KMG_ERROR(KATE_E_LIMIT);
    00546
    00547   if (ncurves>0) {
    00548     curves=(kate_curve**)KMG_CHECKED_MALLOC(ncurves,sizeof(kate_curve*));
    00549     if (!curves) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
    00550     for (n=0;n<ncurves;++n) {
    00551       curves[n]=(kate_curve*)KMG_MALLOC(sizeof(kate_curve));
    00552       if (!curves[n]) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
    00553       ret=kate_decode_curve(ki,curves[n],kpb,&kmg);
    00554       if (kate_overread(kpb)) return KMG_ERROR(KATE_E_BAD_PACKET);
    00555       if (ret<0) return KMG_ERROR(ret);
    00556     }
    00557   }
    00558
    00559   ret=kate_warp(kpb);
    00560   if (ret<0) return KMG_ERROR(ret);
    00561
    00562   ret=kate_check_eop(kpb);
    00563   if (ret<0) return KMG_ERROR(ret);
    00564
    00565   ki->ncurves=ncurves;
    00566   ki->curves=curves;
    00567
    00568   return KMG_OK();
    00569 }
    00570
    00571 static int kate_decode_motion(const kate_info *ki,kate_motion *km,kate_pack_buffer *kpb,kate_memory_guard *parent_kmg)
    00572 {
    00573   KMG_GUARD();
    00574   size_t n;
    00575   int ret;
    00576
    00577   if (!ki || !km || !kpb) return KMG_ERROR(KATE_E_INVALID_PARAMETER);
    00578
    00579   km->ncurves=kate_read32v(kpb);
    00580   if (!ki->no_limits && km->ncurves>KATE_LIMIT_MOTION_CURVES) return KMG_ERROR(KATE_E_LIMIT);
    00581   km->curves=(kate_curve**)KMG_CHECKED_MALLOC(km->ncurves,sizeof(kate_curve*));
    00582   if (!km->curves) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
    00583   km->durations=(kate_float*)KMG_CHECKED_MALLOC(km->ncurves,sizeof(kate_float));
    00584   if (!km->durations) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
    00585
    00586   for (n=0;n<km->ncurves;++n) {
    00587     if (kate_pack_read1(kpb)) {
    00588       size_t idx=kate_read32v(kpb);
    00589       if (idx>=ki->ncurves) return KMG_ERROR(KATE_E_BAD_PACKET);
    00590       km->curves[n]=ki->curves[idx];
    00591     }
    00592     else {
    00593       km->curves[n]=(kate_curve*)KMG_MALLOC(sizeof(kate_curve));
    00594       if (!km->curves[n]) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
    00595       ret=kate_decode_curve(ki,km->curves[n],kpb,&kmg);
    00596       if (kate_overread(kpb)) return KMG_ERROR(KATE_E_BAD_PACKET);
    00597       if (ret<0) return KMG_ERROR(ret);
    00598     }
    00599   }
    00600   ret=kate_fp_decode_kate_float(km->ncurves,km->durations,1,kpb);
    00601   if (ret<0) return KMG_ERROR(ret);
    00602   km->x_mapping=kate_pack_read(kpb,8);
    00603   km->y_mapping=kate_pack_read(kpb,8);
    00604   km->semantics=kate_pack_read(kpb,8);
    00605   km->periodic=kate_pack_read1(kpb);
    00606   ret=kate_warp(kpb);
    00607   if (ret<0) return KMG_ERROR(ret);
    00608
    00609   return KMG_MERGE(parent_kmg);
    00610 }
    00611
    00612 static int kate_decode_motions_packet(kate_info *ki,kate_pack_buffer *kpb)
    00613 {
    00614   KMG_GUARD();
    00615   int ret,n,nmotions;
    00616   kate_motion **motions=NULL;
    00617
    00618   if (!ki || !kpb) return KMG_ERROR(KATE_E_INVALID_PARAMETER);
    00619
    00620   nmotions=kate_read32v(kpb);
    00621   if (nmotions<0) return KMG_ERROR(KATE_E_BAD_PACKET);
    00622   if (!ki->no_limits && nmotions>KATE_LIMIT_MOTIONS) return KMG_ERROR(KATE_E_LIMIT);
    00623
    00624   if (nmotions>0) {
    00625     motions=(kate_motion**)KMG_CHECKED_MALLOC(nmotions,sizeof(kate_motion*));
    00626     if (!motions) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
    00627     for (n=0;n<nmotions;++n) {
    00628       motions[n]=(kate_motion*)KMG_MALLOC(sizeof(kate_motion));
    00629       if (!motions[n]) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
    00630       ret=kate_decode_motion(ki,motions[n],kpb,&kmg);
    00631       if (kate_overread(kpb)) return KMG_ERROR(KATE_E_BAD_PACKET);
    00632       if (ret<0) return KMG_ERROR(ret);
    00633     }
    00634   }
    00635
    00636   ret=kate_warp(kpb);
    00637   if (ret<0) return KMG_ERROR(ret);
    00638
    00639   ret=kate_check_eop(kpb);
    00640   if (ret<0) return KMG_ERROR(ret);
    00641
    00642   ki->nmotions=nmotions;
    00643   ki->motions=motions;
    00644
    00645   return KMG_OK();
    00646 }
    00647
    00648 static int kate_decode_palette(const kate_info *ki,kate_palette *kp,kate_pack_buffer *kpb)
    00649 {
    00650   kate_color *colors;
    00651   size_t n;
    00652   int ret;
    00653
    00654   if (!ki || !kp || !kpb) return KATE_E_INVALID_PARAMETER;
    00655
    00656   kp->ncolors=kate_pack_read(kpb,8)+1;
    00657
    00658   colors=(kate_color*)kate_checked_malloc(kp->ncolors,sizeof(kate_color));
    00659   if (!colors) return KATE_E_OUT_OF_MEMORY;
    00660
    00661   for (n=0;n<kp->ncolors;++n) {
    00662     ret=kate_decode_color(colors+n,kpb);
    00663     if (kate_overread(kpb)) {
    00664       kate_free(colors);
    00665       return ret;
    00666     }
    00667     if (ret<0) {
    00668       kate_free(colors);
    00669       return ret;
    00670     }
    00671   }
    00672   ret=kate_warp(kpb);
    00673   if (ret<0) return ret;
    00674
    00675   kp->colors=colors;
    00676
    00677   return 0;
    00678 }
    00679
    00680 static int kate_decode_palettes_packet(kate_info *ki,kate_pack_buffer *kpb)
    00681 {
    00682   KMG_GUARD();
    00683   int ret,n,npalettes;
    00684   kate_palette **palettes=NULL;
    00685
    00686   if (!ki || !kpb) return KMG_ERROR(KATE_E_INVALID_PARAMETER);
    00687
    00688   npalettes=kate_read32v(kpb);
    00689   if (npalettes<0) return KMG_ERROR(KATE_E_BAD_PACKET);
    00690   if (!ki->no_limits && npalettes>KATE_LIMIT_PALETTES) return KMG_ERROR(KATE_E_LIMIT);
    00691
    00692   if (npalettes>0) {
    00693     palettes=(kate_palette**)KMG_CHECKED_MALLOC(npalettes,sizeof(kate_palette*));
    00694     if (!palettes) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
    00695     for (n=0;n<npalettes;++n) {
    00696       palettes[n]=(kate_palette*)KMG_MALLOC(sizeof(kate_palette));
    00697       if (!palettes[n]) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
    00698       ret=kate_decode_palette(ki,palettes[n],kpb);
    00699       if (kate_overread(kpb)) return KMG_ERROR(ret);
    00700       if (ret<0) return KMG_ERROR(ret);
    00701     }
    00702   }
    00703
    00704   ret=kate_warp(kpb);
    00705   if (ret<0) return KMG_ERROR(ret);
    00706
    00707   ret=kate_check_eop(kpb);
    00708   if (ret<0) return KMG_ERROR(ret);
    00709
    00710   ki->npalettes=npalettes;
    00711   ki->palettes=palettes;
    00712
    00713   return KMG_OK();
    00714 }
    00715
    00716 static int kate_decode_bitmap(const kate_info *ki,kate_bitmap *kb,kate_pack_buffer *kpb)
    00717 {
    00718   size_t n,npixels;
    00719   unsigned char *pixels;
    00720   int ret,encoding;
    00721
    00722   if (!ki || !kb || !kpb) return KATE_E_INVALID_PARAMETER;
    00723
    00724   kb->width=kate_read32v(kpb);
    00725   kb->height=kate_read32v(kpb);
    00726   kb->bpp=kate_pack_read(kpb,8);
    00727   if (kb->width<=0 || kb->height<=0 || kb->bpp>8) return KATE_E_BAD_PACKET;
    00728   if (!ki->no_limits && (kb->width>KATE_LIMIT_BITMAP_SIZE || kb->height>KATE_LIMIT_BITMAP_SIZE)) return KATE_E_LIMIT;
    00729
    00730   if (kb->bpp==0) {
    00731     /* raw bitmap */
    00732     kb->type=kate_pack_read(kpb,8);
    00733     kb->palette=-1;
    00734     switch (kb->type) {
    00735       case kate_bitmap_type_paletted:
    00736         encoding=kate_pack_read(kpb,8);
    00737         switch (encoding) {
    00738           case 1: /* RLE encoded */
    00739             kb->bpp=kate_read32v(kpb);
    00740             kb->palette=kate_read32v(kpb);
    00741             pixels=(unsigned char*)kate_checked_malloc(kb->width,kb->height);
    00742             if (!pixels) return KATE_E_OUT_OF_MEMORY;
    00743             ret=kate_rle_decode(kb->width,kb->height,pixels,kb->bpp,kpb);
    00744             if (ret<0) return ret;
    00745             break;
    00746           default:
    00747             return KATE_E_BAD_PACKET;
    00748         }
    00749         break;
    00750       case kate_bitmap_type_png:
    00751         kb->size=kate_read32(kpb);
    00752         if (!ki->no_limits && kb->size>KATE_LIMIT_BITMAP_RAW_SIZE) return KATE_E_LIMIT;
    00753         pixels=(unsigned char*)kate_malloc(kb->size);
    00754         if (!pixels) return KATE_E_OUT_OF_MEMORY;
    00755         ret=kate_readbuf(kpb,(char*)pixels,kb->size);
    00756         if (ret<0) {
    00757           kate_free(pixels);
    00758           return ret;
    00759         }
    00760         break;
    00761       default:
    00762         return KATE_E_BAD_PACKET;
    00763     }
    00764   }
    00765   else {
    00766     /* paletted bitmap */
    00767     kb->type=kate_bitmap_type_paletted;
    00768     kb->palette=kate_read32v(kpb);
    00769
    00770     ret=kate_check_mul_overflow(kb->width,kb->height,&npixels);
    00771     if (ret<0) return ret;
    00772     pixels=(unsigned char*)kate_malloc(npixels);
    00773     if (!pixels) return KATE_E_OUT_OF_MEMORY;
    00774     if ((size_t)kate_pack_bits(kpb)<npixels*kb->bpp) {
    00775       kate_free(pixels);
    00776       return KATE_E_BAD_PACKET;
    00777     }
    00778
    00779     for (n=0;n<npixels;++n) {
    00780       pixels[n]=kate_pack_read(kpb,kb->bpp);
    00781     }
    00782     if (kate_overread(kpb)) {
    00783       kate_free(pixels);
    00784       return KATE_E_BAD_PACKET;
    00785     }
    00786   }
    00787
    00788   if (((ki->bitstream_version_major<<8)|ki->bitstream_version_minor)>=0x0004) {
    00789     /* 0.4 adds a warp for x/y offset */
    00790     kate_read32v(kpb); /* the size of the warp */
    00791     kb->x_offset=kate_read32v(kpb);
    00792     kb->y_offset=kate_read32v(kpb);
    00793   }
    00794   else {
    00795     kb->x_offset=0;
    00796     kb->y_offset=0;
    00797   }
    00798
    00799   ret=kate_warp(kpb);
    00800   if (ret<0) return ret;
    00801
    00802   kb->pixels=pixels;
    00803
    00804   return 0;
    00805 }
    00806
    00807 static int kate_decode_bitmaps_packet(kate_info *ki,kate_pack_buffer *kpb)
    00808 {
    00809   KMG_GUARD();
    00810   int ret,n,nbitmaps;
    00811   kate_bitmap **bitmaps=NULL;
    00812
    00813   if (!ki || !kpb) return KMG_ERROR(KATE_E_INVALID_PARAMETER);
    00814
    00815   nbitmaps=kate_read32v(kpb);
    00816   if (nbitmaps<0) return KMG_ERROR(KATE_E_BAD_PACKET);
    00817   if (!ki->no_limits && nbitmaps>KATE_LIMIT_BITMAPS) return KMG_ERROR(KATE_E_LIMIT);
    00818
    00819   if (nbitmaps>0) {
    00820     bitmaps=(kate_bitmap**)KMG_CHECKED_MALLOC(nbitmaps,sizeof(kate_bitmap*));
    00821     if (!bitmaps) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
    00822     for (n=0;n<nbitmaps;++n) {
    00823       bitmaps[n]=(kate_bitmap*)KMG_MALLOC(sizeof(kate_bitmap));
    00824       if (!bitmaps[n]) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
    00825       ret=kate_decode_bitmap(ki,bitmaps[n],kpb);
    00826       if (kate_overread(kpb)) return KMG_ERROR(KATE_E_BAD_PACKET);
    00827       if (ret<0) return KMG_ERROR(ret);
    00828     }
    00829   }
    00830
    00831   ret=kate_warp(kpb);
    00832   if (ret<0) return KMG_ERROR(ret);
    00833
    00834   ret=kate_check_eop(kpb);
    00835   if (ret<0) return KMG_ERROR(ret);
    00836
    00837   ki->nbitmaps=nbitmaps;
    00838   ki->bitmaps=bitmaps;
    00839
    00840   return KMG_OK();
    00841 }
    00842
    00843 static int kate_decode_font_range(const kate_info *ki,kate_font_range *kfr,kate_pack_buffer *kpb)
    00844 {
    00845   if (!ki || !kfr || !kpb) return KATE_E_INVALID_PARAMETER;
    00846
    00847   kfr->first_code_point=kate_read32v(kpb);
    00848   kfr->last_code_point=kate_read32v(kpb);
    00849   kfr->first_bitmap=kate_read32v(kpb);
    00850
    00851   return kate_warp(kpb);
    00852 }
    00853
    00854 static int kate_decode_font_ranges_packet(kate_info *ki,kate_pack_buffer *kpb)
    00855 {
    00856   KMG_GUARD();
    00857   int l,n,ret;
    00858   int nfont_ranges,nfont_mappings;
    00859   kate_font_range **font_ranges=NULL;
    00860   kate_font_mapping **font_mappings=NULL;
    00861
    00862   if (!ki || !kpb) return KMG_ERROR(KATE_E_INVALID_PARAMETER);
    00863
    00864   nfont_ranges=kate_read32v(kpb);
    00865   if (nfont_ranges<0) return KMG_ERROR(KATE_E_BAD_PACKET);
    00866   if (!ki->no_limits && nfont_ranges>KATE_LIMIT_FONT_RANGES) return KMG_ERROR(KATE_E_LIMIT);
    00867
    00868   if (nfont_ranges>0) {
    00869     font_ranges=(kate_font_range**)KMG_CHECKED_MALLOC(nfont_ranges,sizeof(kate_font_range*));
    00870     if (!font_ranges) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
    00871     for (n=0;n<nfont_ranges;++n) {
    00872       font_ranges[n]=(kate_font_range*)KMG_MALLOC(sizeof(kate_font_range));
    00873       if (!font_ranges[n]) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
    00874       ret=kate_decode_font_range(ki,font_ranges[n],kpb);
    00875       if (kate_overread(kpb)) return KMG_ERROR(KATE_E_BAD_PACKET);
    00876       if (ret<0) return KMG_ERROR(ret);
    00877     }
    00878   }
    00879
    00880   /* these may now be used by the mappings */
    00881   ki->nfont_ranges=nfont_ranges;
    00882   ki->font_ranges=font_ranges;
    00883
    00884   /* now, the mappings */
    00885   nfont_mappings=kate_read32v(kpb);
    00886   if (nfont_mappings<0) return KMG_ERROR(KATE_E_BAD_PACKET);
    00887   if (!ki->no_limits && nfont_mappings>KATE_LIMIT_FONT_MAPPINGS) return KMG_ERROR(KATE_E_LIMIT);
    00888
    00889   if (nfont_mappings>0) {
    00890     font_mappings=(kate_font_mapping**)KMG_CHECKED_MALLOC(nfont_mappings,sizeof(kate_font_mapping*));
    00891     if (!font_mappings) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
    00892     for (n=0;n<nfont_mappings;++n) {
    00893       font_mappings[n]=(kate_font_mapping*)KMG_MALLOC(sizeof(kate_font_mapping));
    00894       if (!font_mappings[n]) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
    00895
    00896       nfont_ranges=kate_read32v(kpb);
    00897       if (nfont_ranges<0) return KMG_ERROR(KATE_E_BAD_PACKET);
    00898       if (!ki->no_limits && nfont_ranges>KATE_LIMIT_FONT_MAPPING_RANGES) return KMG_ERROR(KATE_E_LIMIT);
    00899
    00900       if (nfont_ranges>0) {
    00901         font_ranges=(kate_font_range**)KMG_CHECKED_MALLOC(nfont_ranges,sizeof(kate_font_range*));
    00902         if (!font_ranges) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
    00903
    00904         for (l=0;l<nfont_ranges;++l) {
    00905           if (kate_pack_read1(kpb)) {
    00906             size_t idx=kate_read32v(kpb);
    00907             if (idx>=ki->nfont_ranges) return KMG_ERROR(KATE_E_BAD_PACKET);
    00908             font_ranges[l]=ki->font_ranges[idx];
    00909           }
    00910           else {
    00911             font_ranges[l]=(kate_font_range*)KMG_MALLOC(sizeof(kate_font_range));
    00912             if (!font_ranges[l]) return KMG_ERROR(KATE_E_OUT_OF_MEMORY);
    00913             ret=kate_decode_font_range(ki,font_ranges[l],kpb);
    00914             if (kate_overread(kpb)) return KMG_ERROR(KATE_E_BAD_PACKET);
    00915             if (ret<0) return KMG_ERROR(ret);
    00916           }
    00917         }
    00918         font_mappings[n]->nranges=nfont_ranges;
    00919         font_mappings[n]->ranges=font_ranges;
    00920       }
    00921       else {
    00922         font_mappings[n]->nranges=0;
    00923         font_mappings[n]->ranges=NULL;
    00924       }
    00925     }
    00926   }
    00927
    00928   ret=kate_warp(kpb);
    00929   if (ret<0) return KMG_ERROR(ret);
    00930
    00931   ret=kate_check_eop(kpb);
    00932   if (ret<0) return KMG_ERROR(ret);
    00933
    00934   ki->nfont_mappings=nfont_mappings;
    00935   ki->font_mappings=font_mappings;
    00936
    00937   return KMG_OK();
    00938 }
    00939
    00951 int kate_decode_headerin(kate_info *ki,kate_comment *kc,kate_packet *kp)
    00952 {
    00953   kate_pack_buffer kpb;
    00954   unsigned char headerid;
    00955   int ret;
    00956   int packetno;
    00957
    00958   if (!ki || !kc || !kp) return KATE_E_INVALID_PARAMETER;
    00959
    00960   kate_pack_readinit(&kpb,kp->data,kp->nbytes);
    00961   headerid=kate_pack_read(&kpb,8);
    00962
    00963   ret=kate_decode_check_magic(&kpb);
    00964   if (ret<0) return ret;
    00965
    00966   if (!(headerid&0x80)) return KATE_E_BAD_PACKET;
    00967   packetno=headerid&~0x80;
    00968   if (packetno<ki->num_headers) {
    00969     if (ki->probe!=packetno) return KATE_E_BAD_PACKET;
    00970   }
    00971
    00972   /* starting with 0.1.4 (bitstream version 0.2, unchanged), there
    00973      was a change in the interpretation of the Kate magic, at the
    00974      request of Xiph, to limit signature length to 8 bytes. So the
    00975      signature (from byte offset 1, after the packet type byte) is
    00976      now only 7 bytes rather than 8, but all headers packets have
    00977      a reserved 0 byte after the signature, so the actual bitstream
    00978      format is left unchanged */
    00979   if (kate_pack_read(&kpb,8)!=0) return KATE_E_BAD_PACKET;
    00980
    00981   switch (packetno) {
    00982     case 0: /* this is the info packet */
    00983       ret=kate_decode_info_header(ki,&kpb);
    00984       break;
    00985
    00986     case 1: /* this is the comments packet */
    00987       ret=kate_decode_comment_packet(ki,kc,&kpb);
    00988       break;
    00989
    00990     case 2: /* this is the region list packet */
    00991       ret=kate_decode_regions_packet(ki,&kpb);
    00992       break;
    00993
    00994     case 3: /* this is the style list packet */
    00995       ret=kate_decode_styles_packet(ki,&kpb);
    00996       break;
    00997
    00998     case 4: /* this is the curve list packet */
    00999       ret=kate_decode_curves_packet(ki,&kpb);
    01000       break;
    01001
    01002     case 5: /* this is the motion list packet */
    01003       ret=kate_decode_motions_packet(ki,&kpb);
    01004       break;
    01005
    01006     case 6: /* this is the palette list packet */
    01007       ret=kate_decode_palettes_packet(ki,&kpb);
    01008       break;
    01009
    01010     case 7: /* this is the bitmap list packet */
    01011       ret=kate_decode_bitmaps_packet(ki,&kpb);
    01012       break;
    01013
    01014     case 8: /* this is the font ranges list packet */
    01015       ret=kate_decode_font_ranges_packet(ki,&kpb);
    01016       if (ret==0) ret=1; /* we're done, we know of no more headers to come */
    01017       break;
    01018
    01019     default:
    01020       /* we ignore extra header packets for future proofing */
    01021       ret=0;
    01022       break;
    01023   }
    01024
    01025   if (ret>=0) {
    01026     /* if the header was parsed successfully, we're ready for the next one */
    01027     ki->probe++;
    01028   }
    01029
    01030   return ret;
    01031 }
    01032
    01042 int kate_decode_init(kate_state *k,kate_info *ki)
    01043 {
    01044   if (!k || !ki) return KATE_E_INVALID_PARAMETER;
    01045
    01046   k->ki=ki;
    01047   k->kes=NULL;
    01048   k->kds=kate_decode_state_create();
    01049   if (!k->kds) return KATE_E_OUT_OF_MEMORY;
    01050
    01051   return 0;
    01052 }
    01053
    01054 #define READ_OVERRIDE(read) \
    01055   do { \
    01056     if (kate_pack_read1(kpb)) { read; } \
    01057   } while(0)
    01058
    01059 #if 0 && defined DEBUG
    01060 #include <unistd.h>
    01061 #include <sys/types.h>
    01062 #include <time.h>
    01063 #include <stdlib.h>
    01064 #define RNDERR(label) \
    01065   do { \
    01066     static int seed=0; \
    01067     if (!seed) { \
    01068       const char *env=getenv("KATE_RAND_SEED"); \
    01069       if (env) srand(atoi(env)); else srand(time(NULL)^getpid()); \
    01070       seed=1; \
    01071     } \
    01072     if (((rand()>>8)&0xff)==0) { \
    01073       ret=KATE_E_OUT_OF_MEMORY; \
    01074       goto label; \
    01075     } \
    01076   } while(0)
    01077 #else
    01078 #define RNDERR(label) ((void)0)
    01079 #endif
    01080
    01081 static int kate_decode_text_packet(kate_state *k,kate_pack_buffer *kpb,int repeat)
    01082 {
    01083   KMG_GUARD();
    01084   int ret,n;
    01085   int len;
    01086   char *text;
    01087   kate_decode_state *kds;
    01088   kate_event *ev;
    01089
    01090   if (!k || !kpb) return KATE_E_INVALID_PARAMETER;
    01091   if (!k->kds) return KATE_E_INIT;
    01092
    01093   ret=kate_decode_state_clear(k->kds,k->ki,1);
    01094   if (ret<0) return ret;
    01095
    01096   kds=k->kds;
    01097   ev=kds->event;
    01098
    01099   ev->start=kate_read64(kpb);
    01100   ev->duration=kate_read64(kpb);
    01101   ev->backlink=kate_read64(kpb);
    01102   if (ev->start<0 || ev->duration<0) goto error_bad_packet;
    01103   if (ev->backlink<0 || ev->backlink>ev->start) goto error_bad_packet;
    01104   RNDERR(error_bad_packet);
    01105
    01106   ev->start_time=kate_granule_duration(k->ki,ev->start);
    01107   ev->end_time=ev->start_time+kate_granule_duration(k->ki,ev->duration);
    01108
    01109   len=kate_read32(kpb);
    01110   if (len<0) goto error_bad_packet;
    01111   RNDERR(error_bad_packet);
    01112   if (!k->ki->no_limits && len>KATE_LIMIT_TEXT_LENGTH) goto error_limit;
    01113   RNDERR(error_limit);
    01114
    01115   /* read the text, and null terminate it - 4 characters for UTF-32 */
    01116   if (kate_check_add_overflow(len,4,NULL)) goto error_out_of_memory;
    01117   text=(char*)KMG_MALLOC(len+4);
    01118   if (!text) goto error_out_of_memory;
    01119   RNDERR(error_out_of_memory);
    01120   ret=kate_readbuf(kpb,text,len);
    01121   if (ret<0) goto error_bad_packet;
    01122   text[len]=0;
    01123   text[len+1]=0;
    01124   text[len+2]=0;
    01125   text[len+3]=0;
    01126   /* we can't validate the text yet, as we don't know whether there's a text encoding override later,
    01127      so we delay validatation till we've read the overrides */
    01128
    01129   ev->text=text;
    01130   ev->len=len;
    01131   ev->len0=len+4;
    01132
    01133   if (kate_pack_read1(kpb)) {
    01134     ev->id=kate_read32v(kpb);
    01135   }
    01136
    01137   if (repeat && ev->id>=0) {
    01138     /* if this is a repeat, check if we have this event already */
    01139     ret=kate_decode_state_find_event(k->kds,ev->id);
    01140     if (ret<0 && ret!=KATE_E_NOT_FOUND) goto error;
    01141     if (ret>=0) goto ignore;
    01142   }
    01143
    01144   if (kate_pack_read1(kpb)) {
    01145     kate_motion **motions=NULL;
    01146     size_t nmotions=0;
    01147
    01148     len=kate_read32v(kpb);
    01149     if (len<=0) goto error_bad_packet; /* if the flag was set, there's at least one */
    01150     if (!k->ki->no_limits && len>KATE_LIMIT_TEXT_MOTIONS) goto error_limit;
    01151     RNDERR(error_limit);
    01152     motions=(kate_motion**)KMG_CHECKED_MALLOC(len,sizeof(kate_motion*));
    01153     if (!motions) goto error_out_of_memory;
    01154     RNDERR(error_out_of_memory);
    01155     nmotions=0;
    01156     for (n=0;n<len;++n) {
    01157       if (kate_pack_read1(kpb)) {
    01158         size_t idx=kate_read32v(kpb);
    01159         if (idx>=k->ki->nmotions) goto error_bad_packet;
    01160         RNDERR(error_bad_packet);
    01161         motions[n]=k->ki->motions[idx];
    01162       }
    01163       else {
    01164         motions[n]=KMG_MALLOC(sizeof(kate_motion));
    01165         if (!motions[n]) goto error_out_of_memory;
    01166         RNDERR(error_out_of_memory);
    01167         ret=kate_decode_motion(k->ki,motions[n],kpb,&kmg);
    01168         if (kate_overread(kpb)) goto error_bad_packet;
    01169         if (ret<0) goto error;
    01170         RNDERR(error);
    01171       }
    01172       ++nmotions;
    01173     }
    01174
    01175     ev->motions=motions;
    01176     ev->nmotions=nmotions;
    01177   }
    01178
    01179   if (kate_pack_read1(kpb)) {
    01180     READ_OVERRIDE(ev->text_encoding=kate_pack_read(kpb,8));
    01181     READ_OVERRIDE(ev->text_directionality=kate_pack_read(kpb,8));
    01182     READ_OVERRIDE(
    01183       do {
    01184         len=kate_read32v(kpb);
    01185         if (len<0) goto error_bad_packet;
    01186         RNDERR(error_bad_packet);
    01187         if (!k->ki->no_limits && len>KATE_LIMIT_LANGUAGE_LENGTH) goto error_limit;
    01188         RNDERR(error_limit);
    01189         if (len>0) {
    01190           ev->language=(char*)KMG_MALLOC(len+1);
    01191           if (!ev->language) goto error_out_of_memory;
    01192         RNDERR(error_out_of_memory);
    01193           ret=kate_readbuf(kpb,ev->language,len);
    01194           if (ret<0) goto error_bad_packet;
    01195           ev->language[len]=0;
    01196         }
    01197       } while(0)
    01198     );
    01199     READ_OVERRIDE(
    01200       size_t idx=kate_read32v(kpb);
    01201       if (idx>=k->ki->nregions) goto error_bad_packet;
    01202       RNDERR(error_bad_packet);
    01203       ev->region=k->ki->regions[idx];
    01204     );
    01205     READ_OVERRIDE(
    01206       ev->region=KMG_MALLOC(sizeof(kate_region));
    01207       if (!ev->region) goto error_out_of_memory;
    01208       RNDERR(error_out_of_memory);
    01209       ret=kate_decode_region(k->ki,ev->region,kpb);
    01210       if (ret<0) goto error;
    01211     );
    01212     READ_OVERRIDE(
    01213       size_t idx=kate_read32v(kpb);
    01214       if (idx>=k->ki->nstyles) goto error_bad_packet;
    01215       RNDERR(error_bad_packet);
    01216       ev->style=k->ki->styles[idx]
    01217     );
    01218     READ_OVERRIDE(
    01219       ev->style=KMG_MALLOC(sizeof(kate_style));
    01220       if (!ev->style) goto error_out_of_memory;
    01221       RNDERR(error_out_of_memory);
    01222       ret=kate_decode_style(k->ki,ev->style,kpb,&kmg);
    01223       if (ret<0) goto error;
    01224     );
    01225     READ_OVERRIDE(
    01226       size_t idx=kate_read32v(kpb);
    01227       if (idx>=k->ki->nstyles) goto error_bad_packet;
    01228       RNDERR(error_bad_packet);
    01229       ev->secondary_style=k->ki->styles[idx]
    01230     );
    01231     READ_OVERRIDE(
    01232       ev->secondary_style=KMG_MALLOC(sizeof(kate_style));
    01233       if (!ev->secondary_style) goto error_out_of_memory;
    01234       RNDERR(error_out_of_memory);
    01235       ret=kate_decode_style(k->ki,ev->secondary_style,kpb,&kmg);
    01236       if (ret<0) goto error;
    01237       RNDERR(error);
    01238     );
    01239     READ_OVERRIDE(
    01240       size_t idx=kate_read32v(kpb);
    01241       if (idx>=k->ki->nfont_mappings) goto error_bad_packet;
    01242       RNDERR(error_bad_packet);
    01243       ev->font_mapping=k->ki->font_mappings[idx]
    01244     );
    01245   }
    01246
    01247   if (((k->ki->bitstream_version_major<<8)|k->ki->bitstream_version_minor)>=0x0002) {
    01248     /* 0.2 adds a warp for palette, bitmap, markup type */
    01249     kate_read32v(kpb); /* the size of the warp */
    01250     if (kate_pack_read1(kpb)) {
    01251       READ_OVERRIDE(
    01252         size_t idx=kate_read32v(kpb);
    01253         if (idx>=k->ki->npalettes) goto error_bad_packet;
    01254         RNDERR(error_bad_packet);
    01255         ev->palette=k->ki->palettes[idx];
    01256       );
    01257       READ_OVERRIDE(
    01258         ev->palette=KMG_MALLOC(sizeof(kate_palette));
    01259         if (!ev->palette) goto error_out_of_memory;
    01260         ret=kate_decode_palette(k->ki,ev->palette,kpb);
    01261         if (ret<0) goto error;
    01262         RNDERR(error);
    01263       );
    01264       READ_OVERRIDE(
    01265         size_t idx=kate_read32v(kpb);
    01266         if (idx>=k->ki->nbitmaps) goto error_bad_packet;
    01267         ev->bitmap=k->ki->bitmaps[idx];
    01268       );
    01269       READ_OVERRIDE(
    01270         ev->bitmap=KMG_MALLOC(sizeof(kate_bitmap));
    01271         if (!ev->bitmap) goto error_out_of_memory;
    01272         RNDERR(error_out_of_memory);
    01273         ret=kate_decode_bitmap(k->ki,ev->bitmap,kpb);
    01274         if (ret<0) goto error;
    01275         RNDERR(error);
    01276       );
    01277       READ_OVERRIDE(ev->text_markup_type=kate_pack_read(kpb,8));
    01278     }
    01279   }
    01280
    01281   if (((k->ki->bitstream_version_major<<8)|k->ki->bitstream_version_minor)>=0x0004) {
    01282     /* 0.4 adds a warp for bitmaps */
    01283     kate_bitmap **bitmaps=NULL;
    01284     size_t nbitmaps=0;
    01285
    01286     kate_read32v(kpb); /* the size of the warp */
    01287
    01288     len=kate_read32v(kpb);
    01289     if (len<0) goto error_bad_packet;
    01290     RNDERR(error_bad_packet);
    01291     if (len>0) {
    01292       if (!k->ki->no_limits && len>KATE_LIMIT_BITMAPS) goto error_limit;
    01293       bitmaps=(kate_bitmap**)KMG_CHECKED_MALLOC(len,sizeof(kate_bitmap*));
    01294       if (!bitmaps) goto error_out_of_memory;
    01295       RNDERR(error_out_of_memory);
    01296       nbitmaps=0;
    01297       for (n=0;n<len;++n) {
    01298         if (kate_pack_read1(kpb)) {
    01299           size_t idx=kate_read32v(kpb);
    01300           if (idx>=k->ki->nbitmaps) goto error_bad_packet;
    01301           RNDERR(error_bad_packet);
    01302           bitmaps[n]=k->ki->bitmaps[idx];
    01303         }
    01304         else {
    01305           bitmaps[n]=KMG_MALLOC(sizeof(kate_bitmap));
    01306           if (!bitmaps[n]) goto error_out_of_memory;
    01307           RNDERR(error_out_of_memory);
    01308           ret=kate_decode_bitmap(k->ki,bitmaps[n],kpb);
    01309           if (kate_overread(kpb)) goto error_bad_packet;
    01310           if (ret<0) goto error;
    01311         }
    01312         ++nbitmaps;
    01313       }
    01314     }
    01315
    01316     ev->bitmaps=bitmaps;
    01317     ev->nbitmaps=nbitmaps;
    01318   }
    01319   else {
    01320     ev->bitmaps=NULL;
    01321     ev->nbitmaps=0;
    01322   }
    01323
    01324   ret=kate_warp(kpb);
    01325   if (ret<0) goto error;
    01326   RNDERR(error);
    01327
    01328   /* validate text, and reject invalid encodings */
    01329   ret=kate_text_validate(ev->text_encoding,ev->text,ev->len0);
    01330   RNDERR(error);
    01331   if (ret<0) goto error;
    01332
    01333   if (ev->text_markup_type!=kate_markup_none && k->ki->remove_markup) {
    01334     size_t zero_size=ev->len0-ev->len;
    01335     ret=kate_text_remove_markup(ev->text_encoding,ev->text,&ev->len0);
    01336     if (ret<0) goto error;
    01337     if (ev->len0<zero_size) goto error_bad_packet; /* can't happen, but let's be safe */
    01338     ev->len=ev->len0-zero_size;
    01339     RNDERR(error);
    01340     ev->text_markup_type=kate_markup_none;
    01341   }
    01342
    01343   /* if no style, use the region (if any) default style */
    01344   if (!ev->style && ev->region) {
    01345     if (ev->region->style>=0) {
    01346       size_t idx=ev->region->style;
    01347       if (idx<k->ki->nstyles) {
    01348         ev->style=k->ki->styles[idx];
    01349       }
    01350     }
    01351   }
    01352
    01353   if (ev->id>=0) {
    01354     /* register the event as active */
    01355     ret=kate_decode_state_add_event(k->kds,ev);
    01356     RNDERR(error);
    01357     if (ret<0) goto error;
    01358   }
    01359
    01360   ret=kate_decode_state_flush_events(k->kds,ev->start);
    01361   RNDERR(error);
    01362   if (ret<0) goto error;
    01363
    01364   return KMG_OK();
    01365
    01366 error_limit:
    01367   ret=KATE_E_LIMIT;
    01368   goto error;
    01369
    01370 error_bad_packet:
    01371   ret=KATE_E_BAD_PACKET;
    01372   goto error;
    01373
    01374 error_out_of_memory:
    01375   ret=KATE_E_OUT_OF_MEMORY;
    01376   goto error;
    01377
    01378 ignore:
    01379   /* special case where we need to destroy the event as we're not interested in it,
    01380      but we do not want an error to be raised */
    01381   ret=0;
    01382   goto error;
    01383
    01384 error:
    01385   /* all the memory allocated for the event should be freed by the kate_memory_guard,
    01386      so we just unlink and jettison it */
    01387   kate_free(kds->event);
    01388   kds->event=NULL;
    01389   return KMG_ERROR(ret);
    01390 }
    01391
    01392 static int kate_decode_repeat_packet(kate_state *k,kate_pack_buffer *kpb)
    01393 {
    01394   return kate_decode_text_packet(k,kpb,1);
    01395 }
    01396
    01397 static int kate_decode_keepalive_packet(kate_state *k,kate_pack_buffer *kpb)
    01398 {
    01399   if (!k || !kpb) return KATE_E_INVALID_PARAMETER;
    01400   if (!k->kds) return KATE_E_INIT;
    01401
    01402   return 0;
    01403 }
    01404
    01405 static int kate_decode_end_packet(kate_state *k,kate_pack_buffer *kpb)
    01406 {
    01407   if (!k || !kpb) return KATE_E_INVALID_PARAMETER;
    01408   if (!k->kds) return KATE_E_INIT;
    01409
    01410   return 1;
    01411 }
    01412
    01422 int kate_decode_packetin(kate_state *k,kate_packet *kp)
    01423 {
    01424   kate_pack_buffer kpb;
    01425   int ret,id;
    01426
    01427   if (!k || !kp) return KATE_E_INVALID_PARAMETER;
    01428   if (!k->ki) return KATE_E_INIT;
    01429   if (!k->kds) return KATE_E_INIT;
    01430
    01431   ret=kate_decode_state_clear(k->kds,k->ki,0);
    01432   if (ret<0) return ret;
    01433
    01434   kate_pack_readinit(&kpb,kp->data,kp->nbytes);
    01435   id=kate_pack_read(&kpb,8);
    01436   if (id&0x80) {
    01437     /* we have a header - we'll ignore it
    01438        it may have happened either because we seeked to the beginning of the stream
    01439        or because we're parsing a stream that has more headers than we know about */
    01440     return 0;
    01441   }
    01442
    01443   switch (id) {
    01444     case 0x00: return kate_decode_text_packet(k,&kpb,0);
    01445     case 0x01: return kate_decode_keepalive_packet(k,&kpb);
    01446     case 0x02: return kate_decode_repeat_packet(k,&kpb);
    01447     case 0x7f: return kate_decode_end_packet(k,&kpb);
    01448     default: return 0; /* unknown data packets are ignored, for future proofing */
    01449   }
    01450
    01451   return 0;
    01452 }
    01453
    01463 int kate_decode_eventout(kate_state *k,kate_const kate_event **event)
    01464 {
    01465   if (!k) return KATE_E_INVALID_PARAMETER;
    01466   if (!k->kds) return KATE_E_INIT;
    01467
    01468   if (!k->kds->event) return 1;
    01469
    01470   if (event) *event=k->kds->event;
    01471
    01472   return 0;
    01473 }
    01474
    01485 int kate_decode_seek(kate_state *k)
    01486 {
    01487   if (!k) return KATE_E_INVALID_PARAMETER;
    01488   if (!k->kds) return KATE_E_INIT;
    01489
    01490   return kate_decode_state_flush_events(k->kds,-1);
    01491 }
    01492