rEFIt_UEFI/libeg/lodepng.cpp
/*international text chunk (iTXt)*/
international text chunk (iTXt)
international text chunk ( iTXt )
static unsigned readChunk_iTXt(LodePNGInfo* info, const LodePNGDecompressSettings* zlibsettings,
                               const unsigned char* data, size_t chunkLength) {
  unsigned error = 0;
  unsigned i;
  unsigned length, begin, compressed;
  char *key = 0, *langtag = 0, *transkey = 0;
  ucvector decoded;
  ucvector_init(&decoded); 
  while(!error)  {
    if(chunkLength < 5) CERROR_BREAK(error, 30); 
    for(length = 0; length < chunkLength && data[length] != 0; ++length) ;
    if(length + 3 >= chunkLength) CERROR_BREAK(error, 75); 
    if(length < 1 || length > 79) CERROR_BREAK(error, 89); 
    key = (char*)lodepng_malloc(length + 1);
    if(!key) CERROR_BREAK(error, 83); 
    key[length] = 0;
    for(i = 0; i != length; ++i) key[i] = (char)data[i];
    compressed = data[length + 1];
    if(data[length + 2] != 0) CERROR_BREAK(error, 72); 
    begin = length + 3;
    length = 0;
    for(i = begin; i < chunkLength && data[i] != 0; ++i) ++length;
    langtag = (char*)lodepng_malloc(length + 1);
    if(!langtag) CERROR_BREAK(error, 83); 
    langtag[length] = 0;
    for(i = 0; i != length; ++i) langtag[i] = (char)data[begin + i];
    begin += length + 1;
    length = 0;
    for(i = begin; i < chunkLength && data[i] != 0; ++i) ++length;
    transkey = (char*)lodepng_malloc(length + 1);
    if(!transkey) CERROR_BREAK(error, 83); 
    transkey[length] = 0;
    for(i = 0; i != length; ++i) transkey[i] = (char)data[begin + i];
    begin += length + 1;
    length = (unsigned)chunkLength < begin ? 0 : (unsigned)chunkLength - begin;
    if(compressed) {
      error = zlib_decompress(&decoded.data, &decoded.size,
                              &data[begin],
                              length, zlibsettings);
      if(error) break;
      if(decoded.allocsize < decoded.size) decoded.allocsize = decoded.size;
      ucvector_push_back(&decoded, 0);
    } else {
      if(!ucvector_resize(&decoded, length + 1)) CERROR_BREAK(error, 83 );
      decoded.data[length] = 0;
      for(i = 0; i != length; ++i) decoded.data[i] = data[begin + i];
    }
    error = lodepng_add_itext(info, key, langtag, transkey, (char*)decoded.data);
    break;
  }
  lodepng_free(key);
  lodepng_free(langtag);
  lodepng_free(transkey);
  ucvector_cleanup(&decoded);
  return error;
}
static unsigned readChunk_iTXt ( LodePNGInfo * info , const LodePNGDecompressSettings * zlibsettings , const unsigned char * data , size_t chunkLength ) { unsigned error = 0 ; unsigned i ; unsigned length , begin , compressed ; char * key = 0 , * langtag = 0 , * transkey = 0 ; ucvector decoded ; ucvector_init ( & decoded ) ; while ( ! error ) { if ( chunkLength < 5 ) CERROR_BREAK ( error , 30 ) ; for ( length = 0 ; length < chunkLength && data [ length ] != 0 ; ++ length ) ; if ( length + 3 >= chunkLength ) CERROR_BREAK ( error , 75 ) ; if ( length < 1 || length > 79 ) CERROR_BREAK ( error , 89 ) ; key = ( char * ) lodepng_malloc ( length + 1 ) ; if ( ! key ) CERROR_BREAK ( error , 83 ) ; key [ length ] = 0 ; for ( i = 0 ; i != length ; ++ i ) key [ i ] = ( char ) data [ i ] ; compressed = data [ length + 1 ] ; if ( data [ length + 2 ] != 0 ) CERROR_BREAK ( error , 72 ) ; begin = length + 3 ; length = 0 ; for ( i = begin ; i < chunkLength && data [ i ] != 0 ; ++ i ) ++ length ; langtag = ( char * ) lodepng_malloc ( length + 1 ) ; if ( ! langtag ) CERROR_BREAK ( error , 83 ) ; langtag [ length ] = 0 ; for ( i = 0 ; i != length ; ++ i ) langtag [ i ] = ( char ) data [ begin + i ] ; begin += length + 1 ; length = 0 ; for ( i = begin ; i < chunkLength && data [ i ] != 0 ; ++ i ) ++ length ; transkey = ( char * ) lodepng_malloc ( length + 1 ) ; if ( ! transkey ) CERROR_BREAK ( error , 83 ) ; transkey [ length ] = 0 ; for ( i = 0 ; i != length ; ++ i ) transkey [ i ] = ( char ) data [ begin + i ] ; begin += length + 1 ; length = ( unsigned ) chunkLength < begin ? 0 : ( unsigned ) chunkLength - begin ; if ( compressed ) { error = zlib_decompress ( & decoded . data , & decoded . size , & data [ begin ] , length , zlibsettings ) ; if ( error ) break ; if ( decoded . allocsize < decoded . size ) decoded . allocsize = decoded . size ; ucvector_push_back ( & decoded , 0 ) ; } else { if ( ! ucvector_resize ( & decoded , length + 1 ) ) CERROR_BREAK ( error , 83 ) ; decoded . data [ length ] = 0 ; for ( i = 0 ; i != length ; ++ i ) decoded . data [ i ] = data [ begin + i ] ; } error = lodepng_add_itext ( info , key , langtag , transkey , ( char * ) decoded . data ) ; break ; } lodepng_free ( key ) ; lodepng_free ( langtag ) ; lodepng_free ( transkey ) ; ucvector_cleanup ( & decoded ) ; return error ; }
international text chunk (iTXt)
international text chunk ( iTXt )
/* TODO: only use in case of compressed text */ /*not really a while loop, only used to break on error*/ /*Quick check if the chunk length isn't too small. Even without check
    it'd still fail with other error checks below if it's too short. This just gives a different error code.*/ /*iTXt chunk too short*/ /*read the key*/ /*no null termination char, corrupt?*/ /*keyword too short or long*/ /*alloc fail*/ /*read the compression method*/ /*the 0 byte indicating compression must be 0*/ /*even though it's not allowed by the standard, no error is thrown if
    there's no null termination char, if the text is empty for the next 3 texts*/ /*read the langtag*/ /*alloc fail*/ /*read the transkey*/ /*alloc fail*/ /*read the actual text*/ /*will fail if zlib error, e.g. if length is too small*/ /*alloc fail*/