Exemplo n.º 1
0
//read header from file
u32 SFileHeader::read(FILE* f)
{
    if (!f)
        return XP3_INVALIDARGUMENT;
    READ_ARRAY(f,magic);
    if (magic[0] != 'X' ||
        magic[1] != 'P' ||
        magic[2] != '3')
    {
        return XP3_INVALIDARCHIVE;
    }

    READ_ARRAY(f,mark1);
    READ_ARRAY(f,mark2);
    READ_VALUE(f,structureVersion);
    READ_VALUE(f,padding1);
    READ_VALUE(f,headerVersion);
    READ_VALUE(f,padding2);
    READ_VALUE(f,indexSize);
    READ_VALUE(f,indexHeaderPos);
    if (indexHeaderPos == 0)
        return XP3_INVALIDARCHIVE;

    return XP3_OK;
}
Exemplo n.º 2
0
struct rbug_proto_shader_info_reply * rbug_demarshal_shader_info_reply(struct rbug_proto_header *header)
{
	uint32_t len = 0;
	uint32_t pos = 0;
	uint8_t *data =  NULL;
	struct rbug_proto_shader_info_reply *ret;

	if (!header)
		return NULL;
	if (header->opcode != (int32_t)RBUG_OP_SHADER_INFO_REPLY)
		return NULL;

	pos = 0;
	len = header->length * 4;
	data = (uint8_t*)&header[1];
	ret = MALLOC(sizeof(*ret));
	if (!ret)
		return NULL;

	ret->header.__message = header;
	ret->header.opcode = header->opcode;

	READ(4, uint32_t, serial); /* serial */
	READ_ARRAY(4, uint32_t, original); /* original */
	READ_ARRAY(4, uint32_t, replaced); /* replaced */
	READ(1, uint8_t, disabled); /* disabled */

	return ret;
}
Exemplo n.º 3
0
struct rbug_proto_context_info_reply * rbug_demarshal_context_info_reply(struct rbug_proto_header *header)
{
	uint32_t len = 0;
	uint32_t pos = 0;
	uint8_t *data =  NULL;
	struct rbug_proto_context_info_reply *ret;

	if (!header)
		return NULL;
	if (header->opcode != (int16_t)RBUG_OP_CONTEXT_INFO_REPLY)
		return NULL;

	pos = 0;
	len = header->length * 4;
	data = (uint8_t*)&header[1];
	ret = MALLOC(sizeof(*ret));
	if (!ret)
		return NULL;

	ret->header.__message = header;
	ret->header.opcode = header->opcode;

	READ(4, uint32_t, serial); /* serial */
	READ(8, rbug_shader_t, vertex); /* vertex */
	READ(8, rbug_shader_t, fragment); /* fragment */
	READ_ARRAY(8, rbug_texture_t, texs); /* texs */
	READ_ARRAY(8, rbug_texture_t, cbufs); /* cbufs */
	READ(8, rbug_texture_t, zsbuf); /* zsbuf */
	READ(4, rbug_block_t, blocker); /* blocker */
	READ(4, rbug_block_t, blocked); /* blocked */

	return ret;
}
Exemplo n.º 4
0
/** 
    reads partition information from the byte file.
 */
void readPartitions(ByteFile *bf)
{
  int i ; 
  
  seekPos(bf, ALN_PARTITIONS); 
  
  assert(bf->partitions == (pInfo **)NULL); 
  bf->partitions = (pInfo **)calloc((size_t)bf->numPartitions, sizeof(pInfo*) );

  for(i = 0; i < bf->numPartitions; ++i)
    {
      bf->partitions[i] = (pInfo*)calloc(1,sizeof(pInfo));
      pInfo* p = bf->partitions[i];

      p->frequencies = (double*)NULL;
      p->partitionName = (char *)NULL;

      READ_VAR(bf->fh, p->states);     
      READ_VAR(bf->fh, p->maxTipStates);     
      READ_VAR(bf->fh, p->lower);
      READ_VAR(bf->fh, p->upper);
    
      /* DONT use this value! */
      READ_VAR(bf->fh, p->width);
      p->width = 0; 

      READ_VAR(bf->fh, p->dataType);
      
      READ_VAR(bf->fh, p->protModels);
      //READ_VAR(bf->fh, p->autoProtModels);
      READ_VAR(bf->fh, p->protFreqs);
      READ_VAR(bf->fh, p->nonGTR);
      READ_VAR(bf->fh, p->optimizeBaseFrequencies);
      //      READ_VAR(bf->fh, p->numberOfCategories);

      /* read string */
      unsigned int len = 0; 
      READ_VAR(bf->fh, len); 
      p->partitionName = (char*)calloc(len,sizeof(char));
      READ_ARRAY(bf->fh, p->partitionName, len, sizeof(char)); 

      p->frequencies = (double*)calloc((size_t)p->states, sizeof(double)); 
      READ_ARRAY(bf->fh, p->frequencies, (size_t)p->states , sizeof(double)); 

     
    }
  
  bf->hasRead |= ALN_PARTITIONS; 
}
Exemplo n.º 5
0
struct rbug_proto_shader_list_reply * rbug_demarshal_shader_list_reply(struct rbug_proto_header *header)
{
	uint32_t len = 0;
	uint32_t pos = 0;
	uint8_t *data =  NULL;
	struct rbug_proto_shader_list_reply *ret;

	if (!header)
		return NULL;
	if (header->opcode != (int32_t)RBUG_OP_SHADER_LIST_REPLY)
		return NULL;

	pos = 0;
	len = header->length * 4;
	data = (uint8_t*)&header[1];
	ret = MALLOC(sizeof(*ret));
	if (!ret)
		return NULL;

	ret->header.__message = header;
	ret->header.opcode = header->opcode;

	READ(4, uint32_t, serial); /* serial */
	READ_ARRAY(8, rbug_shader_t, shaders); /* shaders */

	return ret;
}
Exemplo n.º 6
0
struct rbug_proto_shader_replace * rbug_demarshal_shader_replace(struct rbug_proto_header *header)
{
	uint32_t len = 0;
	uint32_t pos = 0;
	uint8_t *data =  NULL;
	struct rbug_proto_shader_replace *ret;

	if (!header)
		return NULL;
	if (header->opcode != (int32_t)RBUG_OP_SHADER_REPLACE)
		return NULL;

	pos = 0;
	len = header->length * 4;
	data = (uint8_t*)&header[1];
	ret = MALLOC(sizeof(*ret));
	if (!ret)
		return NULL;

	ret->header.__message = header;
	ret->header.opcode = header->opcode;

	READ(8, rbug_context_t, context); /* context */
	READ(8, rbug_shader_t, shader); /* shader */
	READ_ARRAY(4, uint32_t, tokens); /* tokens */

	return ret;
}
Exemplo n.º 7
0
struct rbug_proto_texture_read_reply * rbug_demarshal_texture_read_reply(struct rbug_proto_header *header)
{
	uint32_t len = 0;
	uint32_t pos = 0;
	uint8_t *data =  NULL;
	struct rbug_proto_texture_read_reply *ret;

	if (!header)
		return NULL;
	if (header->opcode != (int16_t)RBUG_OP_TEXTURE_READ_REPLY)
		return NULL;

	pos = 0;
	len = header->length * 4;
	data = (uint8_t*)&header[1];
	ret = MALLOC(sizeof(*ret));
	if (!ret)
		return NULL;

	ret->header.__message = header;
	ret->header.opcode = header->opcode;

	READ(4, uint32_t, serial); /* serial */
	READ(4, uint32_t, format); /* format */
	READ(4, uint32_t, blockw); /* blockw */
	READ(4, uint32_t, blockh); /* blockh */
	READ(4, uint32_t, blocksize); /* blocksize */
	READ_ARRAY(1, uint8_t, data); /* data */
	READ(4, uint32_t, stride); /* stride */

	return ret;
}
Exemplo n.º 8
0
struct rbug_proto_texture_write * rbug_demarshal_texture_write(struct rbug_proto_header *header)
{
	uint32_t len = 0;
	uint32_t pos = 0;
	uint8_t *data =  NULL;
	struct rbug_proto_texture_write *ret;

	if (!header)
		return NULL;
	if (header->opcode != (int16_t)RBUG_OP_TEXTURE_WRITE)
		return NULL;

	pos = 0;
	len = header->length * 4;
	data = (uint8_t*)&header[1];
	ret = MALLOC(sizeof(*ret));
	if (!ret)
		return NULL;

	ret->header.__message = header;
	ret->header.opcode = header->opcode;

	READ(8, rbug_texture_t, texture); /* texture */
	READ(4, uint32_t, face); /* face */
	READ(4, uint32_t, level); /* level */
	READ(4, uint32_t, zslice); /* zslice */
	READ(4, uint32_t, x); /* x */
	READ(4, uint32_t, y); /* y */
	READ(4, uint32_t, w); /* w */
	READ(4, uint32_t, h); /* h */
	READ_ARRAY(1, uint8_t, data); /* data */
	READ(4, uint32_t, stride); /* stride */

	return ret;
}
Exemplo n.º 9
0
bool Disasm::ReadArray(uint8 type, uint32 length, struct ATTRIBUTE* attr) { 
	switch (type) {
		case  HP_UByteArray: READ_ARRAY(ubyte_array, uint8, UByte);
			break;
		case  HP_UInt16Array: READ_ARRAY(uint16_array, uint16, UInt16);
			break;
		case  HP_UInt32Array: READ_ARRAY(uint32_array, uint32, UInt32);
			break;
		case  HP_SInt16Array: READ_ARRAY(sint16_array, int16, SInt16);
			break;
		case  HP_SInt32Array: READ_ARRAY(sint32_array, int32, SInt32);
			break;
		case  HP_Real32Array: READ_ARRAY(real32_array, float, Real32);
			break;
	}
	return true;
}
Exemplo n.º 10
0
struct rbug_proto_texture_info_reply * rbug_demarshal_texture_info_reply(struct rbug_proto_header *header)
{
	uint32_t len = 0;
	uint32_t pos = 0;
	uint8_t *data =  NULL;
	struct rbug_proto_texture_info_reply *ret;

	if (!header)
		return NULL;
	if (header->opcode != (int16_t)RBUG_OP_TEXTURE_INFO_REPLY)
		return NULL;

	pos = 0;
	len = header->length * 4;
	data = (uint8_t*)&header[1];
	ret = MALLOC(sizeof(*ret));
	if (!ret)
		return NULL;

	ret->header.__message = header;
	ret->header.opcode = header->opcode;

	READ(4, uint32_t, serial); /* serial */
	READ(4, uint32_t, target); /* target */
	READ(4, uint32_t, format); /* format */
	READ_ARRAY(4, uint32_t, width); /* width */
	READ_ARRAY(4, uint32_t, height); /* height */
	READ_ARRAY(4, uint32_t, depth); /* depth */
	READ(4, uint32_t, blockw); /* blockw */
	READ(4, uint32_t, blockh); /* blockh */
	READ(4, uint32_t, blocksize); /* blocksize */
	READ(4, uint32_t, last_level); /* last_level */
	READ(4, uint32_t, nr_samples); /* nr_samples */
	READ(4, uint32_t, tex_usage); /* tex_usage */

	return ret;
}
Exemplo n.º 11
0
/** 
    reads the taxon names from the byte file  
 */ 
void readTaxa(ByteFile *bf)
{
  int i; 

  assert(bf->taxaNames == (char **)NULL);
  seekPos(bf,  ALN_TAXA); 

  bf->taxaNames = (char **)calloc(bf->numTax, sizeof(char*));
  for(i = 0; i < bf->numTax; ++i)
    {
      int len = 0; 
      READ_VAR(bf->fh, len ); 
      bf->taxaNames[i] = (char*)calloc(len, sizeof(char)); 
      READ_ARRAY(bf->fh, bf->taxaNames[i], len, sizeof(char)); 
    }

  bf->hasRead |= ALN_TAXA; 
}
Exemplo n.º 12
0
/** 
    uses the information in the PartitionAssignment to only extract
    data relevant to this process (weights and alignment characters).
 */ 
void readMyData(ByteFile *bf, PartitionAssignment *pa, int procId)
{
  seekPos(bf, ALN_ALIGNMENT); 

  exa_off_t
    alnPos = exa_ftell(bf->fh); 

  size_t 
    len; 

  int numAssign = pa->numAssignPerProc[procId];
  Assignment *myAssigns = pa->assignPerProc[procId];

  /* first read aln characters   */
  int i,j ; 
  for(i = 0; i < numAssign; ++i )
    {
      Assignment a = myAssigns[i]; 
      /* printf("reading for: ") ;  */
      /* printAssignment(a, procId);  */

      pInfo 
	*partition = bf->partitions[a.partId];     

      partition->width = a.width; 
      partition->offset = a.offset; 
      len = (size_t)bf->numTax * a.width; 

      if(isPomo(partition->dataType))
	{	  
	  double 
	    *xTip =  (double *)malloc_aligned(len * (size_t)partition->states * sizeof(double));
	  
	  partition->xResource = (double *)malloc_aligned(len * (size_t)partition->states * sizeof(double)); 
	  
	  memset(partition->xResource, 0, len * (size_t)partition->states * sizeof(double));  
	  memset(xTip,                 0, len * (size_t)partition->states * sizeof(double)); 

	  partition->xTipCLV    = (double **)calloc((size_t)bf->numTax + 1 , sizeof(double *)); 
	  partition->xTipVector = (double **)calloc((size_t)bf->numTax + 1 , sizeof(double *)); 

	  for(j = 1; j <= bf->numTax; ++j)
	    {
	      partition->xTipCLV[j]    = partition->xResource + (size_t)(j-1) * a.width * (size_t)partition->states; 
	      partition->xTipVector[j] = xTip                 + (size_t)(j-1) * a.width * (size_t)partition->states;	      
	    }
	}
      else
	{
	  partition->yResource = (unsigned char*)malloc_aligned( len * sizeof(unsigned char)); 
	  memset(partition->yResource,0,(size_t)len * sizeof(unsigned char)); 
	  partition->yVector = (unsigned char**) calloc((size_t)bf->numTax + 1 , sizeof(unsigned char*)); 
	  for(j = 1; j <= bf->numTax; ++j)
	    partition->yVector[j] = partition->yResource + (size_t)(j-1) * a.width; 
	}

#ifdef OLD_LAYOUT
      for(j = 1; j <= bf->numTax; ++j )
	{
	  exa_off_t pos = alnPos + (  bf->numPattern * (j-1)    +  partition->lower + a.offset ) * sizeof(unsigned char); 
	  assert(alnPos <= pos); 
	  exa_fseek(bf->fh, pos, SEEK_SET); 
	  READ_ARRAY(bf->fh, partition->yVector[j], a.width, sizeof(unsigned char));
	}
#else 
      /*  if the entire partition is assigned to this process, read it
          in one go. Otherwise, several seeks are necessary.  */
      if( a.width == (partition->upper - partition->lower ) )
        { 
	  if(isPomo(partition->dataType))
	    {
	      exa_off_t
		pos = alnPos +  (exa_off_t)partition->lower * (exa_off_t)bf->numTax * (exa_off_t)partition->states * (exa_off_t)sizeof(double); 
	      
	      assert(alnPos <= pos); 
	      exa_fseek(bf->fh, pos, SEEK_SET); 
	      READ_ARRAY(bf->fh, partition->xResource, a.width * (size_t)bf->numTax * (size_t)partition->states, sizeof(double));
	    }
	  else
	    {
	      exa_off_t
		pos = alnPos + ((exa_off_t)partition->lower * (exa_off_t)bf->numTax) * (exa_off_t)sizeof(unsigned char); 
	      
	      assert(alnPos <= pos); 
	      exa_fseek(bf->fh, pos, SEEK_SET); 
	      READ_ARRAY(bf->fh, partition->yResource, a.width * (size_t)bf->numTax, sizeof(unsigned char));
	    }
        }
      else 
        {
          for(j = 1; j <= bf->numTax; ++j )
            {
	      if(isPomo(partition->dataType))
		{
		  exa_off_t 
		    pos = alnPos + (exa_off_t)sizeof(double) * (exa_off_t)partition->states 
		    * ( 
		       ((exa_off_t)partition->lower * (exa_off_t)bf->numTax ) /* until start of partition  */
		       + ((exa_off_t)(j-1) * ((exa_off_t)partition->upper - (exa_off_t)partition->lower) ) /* until start of sequence of taxon within partition */
		       + (exa_off_t)a.offset )  ; 
		  
		  assert(alnPos <= pos); 
		  exa_fseek(bf->fh, pos, SEEK_SET); 
		  READ_ARRAY(bf->fh, partition->xTipCLV[j], a.width * (size_t)partition->states, sizeof(double));
		}
	      else
		{
		  exa_off_t 
		    pos = alnPos + (exa_off_t)sizeof(unsigned char) 
		    * ( 
		       ((exa_off_t)partition->lower * (exa_off_t)bf->numTax ) /* until start of partition  */
		       + ((exa_off_t)(j-1) * ((exa_off_t)partition->upper - (exa_off_t)partition->lower) ) /* until start of sequence of taxon within partition */
		       + (exa_off_t)a.offset )  ; 
		  
		  assert(alnPos <= pos); 
		  exa_fseek(bf->fh, pos, SEEK_SET); 
		  READ_ARRAY(bf->fh, partition->yVector[j], a.width, sizeof(unsigned char));
		}
            }
        }
#endif
    }

  
  /* now read weights  */
  seekPos(bf, ALN_WEIGHTS); 

  exa_off_t
    wgtPos = exa_ftell(bf->fh); 
  assert( ! (wgtPos <  0) );

  for(i = 0; i < numAssign; ++i)
    {
      Assignment a = myAssigns[i]; 
      pInfo *partition = bf->partitions[a.partId];

#ifdef __MIC_NATIVE
     /* for Xeon Phi, wgt must be padded to the multiple of 8 (because of site blocking in kernels) */
     const int padded_width = GET_PADDED_WIDTH(a.width);
     len = padded_width * sizeof(int);
#else
     len = a.width * sizeof(int);
#endif

      partition->wgt = (int*)malloc_aligned( len); 
      memset(partition->wgt, 0, len); 

      exa_off_t pos = wgtPos +  ((exa_off_t)partition->lower  + (exa_off_t)a.offset) * (exa_off_t)sizeof(int); 
      assert(wgtPos <= pos );
      
      exa_fseek(bf->fh, pos, SEEK_SET); 
      READ_ARRAY(bf->fh, partition->wgt, a.width, sizeof(int)); 

    }

  bf->hasRead |= ALN_ALIGNMENT; 
  bf->hasRead |= ALN_WEIGHTS; 
} 
Exemplo n.º 13
0
int CDVDOverlayCodecTX3G::Decode(BYTE* data, int size, double pts, double duration)
{
  if (m_pOverlay)
    SAFE_RELEASE(m_pOverlay);

  m_pOverlay = new CDVDOverlayText();
  m_pOverlay->iPTSStartTime = pts;
  m_pOverlay->iPTSStopTime  = pts + duration;

  // do not move this. READ_XXXX macros modify pos.
  uint8_t  *pos = data;
  uint8_t  *end = pos + size;

  // Parse the packet as a TX3G TextSample.
  // Look for a single StyleBox ('styl') and 
  // read all contained StyleRecords.
  // Ignore all other box types.
  // NOTE: Buffer overflows on read are not checked.
  // ALSO: READ_XXXX/SKIP_XXXX macros will modify pos.
  uint16_t textLength = READ_U16();
  uint8_t *text = READ_ARRAY(textLength);

  int numStyleRecords = 0;
  uint8_t *bgnStyle   = (uint8_t*)calloc(textLength, 1);
  uint8_t *endStyle   = (uint8_t*)calloc(textLength, 1);
  int bgnColorIndex = 0, endColorIndex = 0;
  uint32_t textColorRGBA = m_textColor;
  while (pos < end)
  {
    // Read TextSampleModifierBox
    uint32_t size = READ_U32();
    if (size == 0)
      size = pos - end;   // extends to end of packet
    if (size == 1)
    {
      CLog::Log(LOGDEBUG, "CDVDOverlayCodecTX3G: TextSampleModifierBox has unsupported large size" );
      break;
    }
    uint32_t type = READ_U32();
    if (type == FOURCC("uuid"))
    {
      CLog::Log(LOGDEBUG, "CDVDOverlayCodecTX3G: TextSampleModifierBox has unsupported extended type" );
      break;
    }

    if (type == FOURCC("styl"))
    {
      // Found a StyleBox. Parse the contained StyleRecords
      if ( numStyleRecords != 0 )
      {
        CLog::Log(LOGDEBUG, "CDVDOverlayCodecTX3G: found additional StyleBoxes on subtitle; skipping" );
        SKIP_ARRAY(size);
        continue;
      }

      numStyleRecords = READ_U16();
      for (int i = 0; i < numStyleRecords; i++)
      {
        StyleRecord curRecord;
        curRecord.bgnChar         = READ_U16();
        curRecord.endChar         = READ_U16();
        curRecord.fontID          = READ_U16();
        curRecord.faceStyleFlags  = READ_U8();
        curRecord.fontSize        = READ_U8();
        curRecord.textColorRGBA   = READ_U32();

        bgnStyle[curRecord.bgnChar] |= curRecord.faceStyleFlags;
        endStyle[curRecord.endChar] |= curRecord.faceStyleFlags;
        bgnColorIndex = curRecord.bgnChar;
        endColorIndex = curRecord.endChar;
        textColorRGBA = curRecord.textColorRGBA;
      }
    }
    else
    {
      // Found some other kind of TextSampleModifierBox. Skip it.
      SKIP_ARRAY(size);
    }
  }

  // Copy text to out and add HTML markup for the style records
  int charIndex = 0;
  CStdStringA strUTF8;
  for (pos = text, end = text + textLength; pos < end; pos++)
  {
    if ((*pos & 0xC0) == 0x80)
    {
      // Is a non-first byte of a multi-byte UTF-8 character
      strUTF8.append((const char*)pos, 1);
      continue;   // ...without incrementing 'charIndex'
    }

    uint8_t bgnStyles = bgnStyle[charIndex];
    uint8_t endStyles = endStyle[charIndex];

    // [B] or [/B] -> toggle bold on and off
    // [I] or [/I] -> toggle italics on and off
    // [COLOR ffab007f] or [/COLOR] -> toggle color on and off
    // [CAPS <option>]  or [/CAPS]  -> toggle capatilization on and off

    if (endStyles & BOLD)
      strUTF8.append("[/B]");
    if (endStyles & ITALIC)
      strUTF8.append("[/I]");
    // we do not support underline
    //if (endStyles & UNDERLINE)
    //  strUTF8.append("[/U]");
    if (endColorIndex == charIndex && textColorRGBA != m_textColor)
      strUTF8.append("[/COLOR]");

    // invert the order from above so we bracket the text correctly.
    if (bgnColorIndex == charIndex && textColorRGBA != m_textColor)
      strUTF8.AppendFormat("[COLOR %8x]", textColorRGBA);
    // we do not support underline
    //if (bgnStyles & UNDERLINE)
    //  strUTF8.append("[U]");
    if (bgnStyles & ITALIC)
      strUTF8.append("[I]");
    if (bgnStyles & BOLD)
      strUTF8.append("[B]");

    // stuff the UTF8 char
    strUTF8.append((const char*)pos, 1);

    // this is a char index, not a byte index.
    charIndex++;
  }
  
  free(bgnStyle);
  free(endStyle);
    
  if (strUTF8.IsEmpty())
    return OC_BUFFER;

  if (strUTF8[strUTF8.size()-1] == '\n')
    strUTF8.Delete(strUTF8.size()-1);

  // add a new text element to our container
  m_pOverlay->AddElement(new CDVDOverlayText::CElementText(strUTF8.c_str()));

  return OC_OVERLAY;
}
Exemplo n.º 14
0
int CDVDOverlayCodecTX3G::Decode(DemuxPacket *pPacket)
{
  if (m_pOverlay)
    SAFE_RELEASE(m_pOverlay);

  m_pOverlay = new CDVDOverlayText();
  CDVDOverlayCodec::GetAbsoluteTimes(m_pOverlay->iPTSStartTime, m_pOverlay->iPTSStopTime, pPacket, m_pOverlay->replace);

  // do not move this. READ_XXXX macros modify pos.
  uint8_t  *pos = pPacket->pData;
  uint8_t  *end = pPacket->pData + pPacket->iSize;

  // Parse the packet as a TX3G TextSample.
  // Look for a single StyleBox ('styl') and 
  // read all contained StyleRecords.
  // Ignore all other box types.
  // NOTE: Buffer overflows on read are not checked.
  // ALSO: READ_XXXX/SKIP_XXXX macros will modify pos.
  LEN_CHECK(2);
  uint16_t textLength = READ_U16();
  LEN_CHECK(textLength);
  uint8_t *text = READ_ARRAY(textLength);

  int numStyleRecords = 0;
  // reserve one more style slot for broken encoders

  XUTILS::auto_buffer bgnStyle(textLength+1);
  XUTILS::auto_buffer endStyle(textLength+1);

  memset(bgnStyle.get(), 0, textLength+1);
  memset(endStyle.get(), 0, textLength+1);

  int bgnColorIndex = 0, endColorIndex = 0;
  uint32_t textColorRGBA = m_textColor;
  while (pos < end)
  {
    // Read TextSampleModifierBox
    LEN_CHECK(4);
    uint32_t size = READ_U32();
    if (size == 0)
      size = pos - end;   // extends to end of packet
    if (size == 1)
    {
      CLog::Log(LOGDEBUG, "CDVDOverlayCodecTX3G: TextSampleModifierBox has unsupported large size" );
      break;
    }
    LEN_CHECK(4);
    uint32_t type = READ_U32();
    if (type == FOURCC("uuid"))
    {
      CLog::Log(LOGDEBUG, "CDVDOverlayCodecTX3G: TextSampleModifierBox has unsupported extended type" );
      break;
    }

    if (type == FOURCC("styl"))
    {
      // Found a StyleBox. Parse the contained StyleRecords
      if ( numStyleRecords != 0 )
      {
        CLog::Log(LOGDEBUG, "CDVDOverlayCodecTX3G: found additional StyleBoxes on subtitle; skipping" );
        LEN_CHECK(size);
        SKIP_ARRAY(size);
        continue;
      }

      LEN_CHECK(2);
      numStyleRecords = READ_U16();
      for (int i = 0; i < numStyleRecords; i++)
      {
        StyleRecord curRecord;
        LEN_CHECK(12);
        curRecord.bgnChar         = READ_U16();
        curRecord.endChar         = READ_U16();
        curRecord.fontID          = READ_U16();
        curRecord.faceStyleFlags  = READ_U8();
        curRecord.fontSize        = READ_U8();
        curRecord.textColorRGBA   = READ_U32();
        // clamp bgnChar/bgnChar to textLength,
        // we alloc enough space above and this
        // fixes borken encoders that do not handle
        // endChar correctly.
        if (curRecord.bgnChar > textLength)
          curRecord.bgnChar = textLength;
        if (curRecord.endChar > textLength)
          curRecord.endChar = textLength;

        bgnStyle.get()[curRecord.bgnChar] |= curRecord.faceStyleFlags;
        endStyle.get()[curRecord.endChar] |= curRecord.faceStyleFlags;
        bgnColorIndex = curRecord.bgnChar;
        endColorIndex = curRecord.endChar;
        textColorRGBA = curRecord.textColorRGBA;
      }
    }
    else
    {
      // Found some other kind of TextSampleModifierBox. Skip it.
      LEN_CHECK(size);
      SKIP_ARRAY(size);
    }
  }

  // Copy text to out and add HTML markup for the style records
  int charIndex = 0;
  std::string strUTF8;
  // index over textLength chars to include broken encoders,
  // so we pickup closing styles on broken encoders
  for (pos = text, end = text + textLength; pos <= end; pos++)
  {
    if ((*pos & 0xC0) == 0x80)
    {
      // Is a non-first byte of a multi-byte UTF-8 character
      strUTF8.append((const char*)pos, 1);
      continue;   // ...without incrementing 'charIndex'
    }

    uint8_t bgnStyles = bgnStyle.get()[charIndex];
    uint8_t endStyles = endStyle.get()[charIndex];

    // [B] or [/B] -> toggle bold on and off
    // [I] or [/I] -> toggle italics on and off
    // [COLOR ffab007f] or [/COLOR] -> toggle color on and off
    // [CAPS <option>]  or [/CAPS]  -> toggle capatilization on and off

    if (endStyles & BOLD)
      strUTF8.append("[/B]");
    if (endStyles & ITALIC)
      strUTF8.append("[/I]");
    // we do not support underline
    //if (endStyles & UNDERLINE)
    //  strUTF8.append("[/U]");
    if (endColorIndex == charIndex && textColorRGBA != m_textColor)
      strUTF8.append("[/COLOR]");

    // invert the order from above so we bracket the text correctly.
    if (bgnColorIndex == charIndex && textColorRGBA != m_textColor)
      strUTF8 += StringUtils::Format("[COLOR %8x]", textColorRGBA);
    // we do not support underline
    //if (bgnStyles & UNDERLINE)
    //  strUTF8.append("[U]");
    if (bgnStyles & ITALIC)
      strUTF8.append("[I]");
    if (bgnStyles & BOLD)
      strUTF8.append("[B]");

    // stuff the UTF8 char
    strUTF8.append((const char*)pos, 1);

    // this is a char index, not a byte index.
    charIndex++;
  }
  
  if (strUTF8.empty())
    return OC_BUFFER;

  if (strUTF8[strUTF8.size()-1] == '\n')
    strUTF8.erase(strUTF8.size()-1);

  // add a new text element to our container
  m_pOverlay->AddElement(new CDVDOverlayText::CElementText(strUTF8.c_str()));

  return OC_OVERLAY;
}
Exemplo n.º 15
0
Function* bas_load_compiled_function(Basic* kernel, DataReader* reader)
{
    Function* function = NULL;
    uint16_t function_size;
    uint32_t n;
    uint16_t* global_table = NULL;
    uint16_t function_globals;
    uint8_t name_size;
    char* name = NULL;
    int success = 0;
    
    READ_VAR(function_size);
    //printf( "Function size %d\n", function_size );
    
    function = (Function*)mem_malloc(sizeof(Function)+function_size);
    
    if (function == 0)
        return NULL;
    
    function->header.ref_count = 1;
    function->header.type = kTypeFunction;
    
    READ_VAR(function->locals);
    READ_VAR(function->arguments);
    READ_VAR(function->stack_usage);
    READ_ARRAY( (char*)(function+1), function_size );
    
    READ_VAR(function_globals);
    
    global_table = (uint16_t*)mem_malloc(sizeof(uint16_t)*function_globals);
    if (global_table == NULL)
        goto end;

    for(n=0; n<function_globals; ++n)
    {
        READ_VAR(name_size);
        
        name = mem_malloc(name_size+1);
        if (name == NULL)
            goto end;
        READ_ARRAY(name, name_size);
        
        name[name_size] = '\0';
        
        global_table[n] = bas_find_or_create_global(kernel, name);
        if (global_table[n] == 0xFFFF)
            goto end;
        
        mem_free(name);
        name = NULL;
    }
    
    // patch the bytecode to fix the global indices
    uint8_t* bc_ptr = (uint8_t*)(function+1);
    uint8_t* bc_end = bc_ptr + function_size;
    
    while (bc_ptr < bc_end)
    {
        switch (*bc_ptr)
        {
            case BC_GET_GLOBAL:
            case BC_SET_GLOBAL:
            {
                bc_ptr ++;
                uint16_t index;
                memcpy( &index, bc_ptr, 2 ); 
                index = global_table[index];
                memcpy( bc_ptr, &index, 2 );
                bc_ptr += 2;
                break;
            }
            case BC_ADD:
            case BC_SUBTRACT:
            case BC_MULTIPLY:
            case BC_DIVIDE:
            case BC_RETURN_ZERO: 
            case BC_POP:
            case BC_COMPARE_LT:
            case BC_COMPARE_LE:
            case BC_COMPARE_GT:
            case BC_COMPARE_GE:
            case BC_COMPARE_EQ:
            case BC_COMPARE_NE:
            case BC_NOT:
            case BC_NEGATE:
                bc_ptr++;
                break; // do nothing
            case BC_CALL:
                bc_ptr += 2; // skip number of arguments
                break;
            case BC_PUSH_INT32:
                bc_ptr += 5; // skip literal
                break;
            case BC_PUSH_FLOAT:
                bc_ptr += sizeof(float)+1; // skip literal
                break;
            case BC_PUSH_STRING:
            {
                // skip string size and characters
                bc_ptr ++;
                uint16_t stringSize;
                memcpy( &stringSize, bc_ptr, 2 ); bc_ptr += 2;
                bc_ptr += stringSize;
            }
            break;
            case BC_BRANCH_TRUE:
            case BC_BRANCH_FALSE:
            {
                bc_ptr += 3; // skip branch offset
            }
            break;
            default:
                goto end;
        }
    }
    
    success = 1;
end:
    if (!success)
    {
        if (function != NULL) 
            mem_free(function);
        function = NULL;
    }
    if (name != NULL) mem_free(name);
    if (global_table != NULL) mem_free(global_table);
    
    return function;
}
Exemplo n.º 16
0
static hb_buffer_t *tx3g_decode_to_utf8( hb_buffer_t *in )
{
    uint8_t *pos = in->data;
    uint8_t *end = in->data + in->size;
    
    uint16_t numStyleRecords = 0;
    
    uint8_t *startStyle;
    uint8_t *endStyle;
    
    /*
     * Parse the packet as a TX3G TextSample.
     * 
     * Look for a single StyleBox ('styl') and read all contained StyleRecords.
     * Ignore all other box types.
     * 
     * NOTE: Buffer overflows on read are not checked.
     */
    uint16_t textLength = READ_U16();
    uint8_t *text = READ_ARRAY(textLength);
    startStyle = calloc( textLength, 1 );
    endStyle = calloc( textLength, 1 );
    while ( pos < end ) {
        /*
         * Read TextSampleModifierBox
         */
        uint32_t size = READ_U32();
        if ( size == 0 ) {
            size = pos - end;   // extends to end of packet
        }
        if ( size == 1 ) {
            hb_log( "dectx3gsub: TextSampleModifierBox has unsupported large size" );
            break;
        }
        uint32_t type = READ_U32();
        if ( type == FOURCC("uuid") ) {
            hb_log( "dectx3gsub: TextSampleModifierBox has unsupported extended type" );
            break;
        }
        
        if ( type == FOURCC("styl") ) {
            // Found a StyleBox. Parse the contained StyleRecords
            
            if ( numStyleRecords != 0 ) {
                hb_log( "dectx3gsub: found additional StyleBoxes on subtitle; skipping" );
                SKIP_ARRAY(size);
                continue;
            }
            
            numStyleRecords = READ_U16();
            
            int i;
            for (i=0; i<numStyleRecords; i++) {
                StyleRecord curRecord;
                curRecord.startChar         = READ_U16();
                curRecord.endChar           = READ_U16();
                curRecord.fontID            = READ_U16();
                curRecord.faceStyleFlags    = READ_U8();
                curRecord.fontSize          = READ_U8();
                curRecord.textColorRGBA     = READ_U32();
                
                startStyle[curRecord.startChar] |= curRecord.faceStyleFlags;
                endStyle[curRecord.endChar]     |= curRecord.faceStyleFlags;
            }
        } else {
            // Found some other kind of TextSampleModifierBox. Skip it.
            SKIP_ARRAY(size);
        }
    }
    
    /*
     * Copy text to output buffer, and add HTML markup for the style records
     */
    int maxOutputSize = textLength + (numStyleRecords * NUM_FACE_STYLE_FLAGS * (MAX_OPEN_TAG_SIZE + MAX_CLOSE_TAG_SIZE));
    hb_buffer_t *out = hb_buffer_init( maxOutputSize );
    if ( out == NULL )
        goto fail;
    uint8_t *dst = out->data;
    int charIndex = 0;
    for ( pos = text, end = text + textLength; pos < end; pos++ ) {
        if (IS_10xxxxxx(*pos)) {
            // Is a non-first byte of a multi-byte UTF-8 character
            WRITE_CHAR(*pos);
            continue;   // ...without incrementing 'charIndex'
        }
        
        uint8_t plusStyles = startStyle[charIndex];
        uint8_t minusStyles = endStyle[charIndex];
        
        if (minusStyles & UNDERLINE)
            WRITE_END_TAG('u');
        if (minusStyles & ITALIC)
            WRITE_END_TAG('i');
        if (minusStyles & BOLD)
            WRITE_END_TAG('b');
        
        if (plusStyles & BOLD)
            WRITE_START_TAG('b');
        if (plusStyles & ITALIC)
            WRITE_START_TAG('i');
        if (plusStyles & UNDERLINE)
            WRITE_START_TAG('u');
        
        WRITE_CHAR(*pos);
        charIndex++;
    }
    
    // Trim output buffer to the actual amount of data written
    out->size = dst - out->data;
    
    // Copy metadata from the input packet to the output packet
    out->s.start = in->s.start;
    out->s.stop = in->s.stop;
    
fail:
    free( startStyle );
    free( endStyle );
    
    return out;
}