Example #1
0
/*
========================
idDxtDecoder::DecomposeColorBlock
========================
*/
void idDxtDecoder::DecomposeColorBlock( byte colors[2][4], byte colorIndices[16], bool noBlack )
{
	int i;
	unsigned int indices;
	unsigned short color0, color1;
	int colorRemap1[] = { 3, 0, 2, 1 };
	int colorRemap2[] = { 1, 3, 2, 0 };
	int* crm;
	
	color0 = ReadUShort();
	color1 = ReadUShort();
	
	ColorFrom565( color0, colors[0] );
	ColorFrom565( color1, colors[1] );
	
	if( noBlack || color0 > color1 )
	{
		crm = colorRemap1;
	}
	else
	{
		crm = colorRemap2;
	}
	
	indices = ReadUInt();
	for( i = 0; i < 16; i++ )
	{
		colorIndices[i] = ( byte )crm[ indices & 3 ];
		indices >>= 2;
	}
}
Example #2
0
//unsigned short
void NifStream( unsigned short & val, istream& in, const NifInfo & info ) {
	if ( info.endian == sys_endian ) {
		val = ReadUShort( in );
	} else {
		val = SwapEndian( ReadUShort( in ) );
	}
}
Example #3
0
/*
========================
idDxtDecoder::DecodeNormalYValues
========================
*/
void idDxtDecoder::DecodeNormalYValues( byte* normalBlock, const int offsetY, byte& c0, byte& c1 )
{
	int i;
	unsigned int indexes;
	unsigned short normal0, normal1;
	byte normalsY[4];
	
	normal0 = ReadUShort();
	normal1 = ReadUShort();
	
	assert( normal0 >= normal1 );
	
	normalsY[0] = NormalYFrom565( normal0 );
	normalsY[1] = NormalYFrom565( normal1 );
	normalsY[2] = ( 2 * normalsY[0] + 1 * normalsY[1] ) / 3;
	normalsY[3] = ( 1 * normalsY[0] + 2 * normalsY[1] ) / 3;
	
	c0 = NormalBiasFrom565( normal0 );
	c1 = NormalScaleFrom565( normal0 );
	
	byte* normalYPtr = normalBlock + offsetY;
	
	indexes = ReadUInt();
	for( i = 0; i < 16; i++ )
	{
		normalYPtr[i * 4] = normalsY[indexes & 3];
		indexes >>= 2;
	}
}
Example #4
0
/*
========================
idDxtDecoder::DecodeColorValues
========================
*/
void idDxtDecoder::DecodeColorValues( byte* colorBlock, bool noBlack, bool writeAlpha )
{
	byte colors[4][4];
	
	unsigned short color0 = ReadUShort();
	unsigned short color1 = ReadUShort();
	
	ColorFrom565( color0, colors[0] );
	ColorFrom565( color1, colors[1] );
	
	colors[0][3] = 255;
	colors[1][3] = 255;
	
	if( noBlack || color0 > color1 )
	{
		colors[2][0] = ( 2 * colors[0][0] + 1 * colors[1][0] ) / 3;
		colors[2][1] = ( 2 * colors[0][1] + 1 * colors[1][1] ) / 3;
		colors[2][2] = ( 2 * colors[0][2] + 1 * colors[1][2] ) / 3;
		colors[2][3] = 255;
		
		colors[3][0] = ( 1 * colors[0][0] + 2 * colors[1][0] ) / 3;
		colors[3][1] = ( 1 * colors[0][1] + 2 * colors[1][1] ) / 3;
		colors[3][2] = ( 1 * colors[0][2] + 2 * colors[1][2] ) / 3;
		colors[3][3] = 255;
	}
	else
	{
		colors[2][0] = ( 1 * colors[0][0] + 1 * colors[1][0] ) / 2;
		colors[2][1] = ( 1 * colors[0][1] + 1 * colors[1][1] ) / 2;
		colors[2][2] = ( 1 * colors[0][2] + 1 * colors[1][2] ) / 2;
		colors[2][3] = 255;
		
		colors[3][0] = 0;
		colors[3][1] = 0;
		colors[3][2] = 0;
		colors[3][3] = 0;
	}
	
	unsigned int indexes = ReadUInt();
	for( int i = 0; i < 16; i++ )
	{
		colorBlock[i * 4 + 0] = colors[indexes & 3][0];
		colorBlock[i * 4 + 1] = colors[indexes & 3][1];
		colorBlock[i * 4 + 2] = colors[indexes & 3][2];
		if( writeAlpha )
		{
			colorBlock[i * 4 + 3] = colors[indexes & 3][3];
		}
		indexes >>= 2;
	}
}
Example #5
0
File: ISmb.cpp Project: FEI17N/Lgi
	bool Read()
	{
		Conn->Read((char*) Protocol, sizeof(Protocol), 0);
		Command = ReadUShort();
		Status.Status = ReadULong();
		Flags = ReadUChar();
		Flags2 = ReadUShort();
		Conn->Read((char*) Pad, sizeof(Pad), 0);
		Tid = ReadUShort();
		Pid = ReadUShort();
		Uid = ReadUShort();
		Mid = ReadUShort();
		
		WordCount = ReadUChar();
		DeleteArray(ParameterWords);
		ParameterWords = new ushort[WordCount];
		if (ParameterWords)
		{
			Conn->Read((char*) ParameterWords, sizeof(ushort)*WordCount, 0);
		}

		ByteCount = ReadUChar();
		DeleteArray(Buffer);
		Buffer = new uchar[ByteCount];
		if (Buffer)
		{
			Conn->Read((char*) Buffer, sizeof(uchar)*ByteCount, 0);
		}

		return true;
	}
Example #6
0
/**
 * @name ParseHeader
 * Parse a Patch file header
 * @note The current implementation is far from complete!
 *
 * @param Patch
 * Buffer pointing to the raw patch data
 *
 * @param Header
 * The result of the parsed header
 *
 * @return STATUS_SUCCESS on success, an error code otherwise
 */
DWORD ParseHeader(SAFE_READ* Patch, PATCH_HEADER* Header)
{
    DWORD Crc, Unknown;
    int Delta;

    ZeroMemory(Header, sizeof(*Header));

    /* Validate the patch */
    Crc = RtlComputeCrc32(0, Patch->Root, Patch->Size);
    if (Crc != ~0)
        return ERROR_PATCH_CORRUPT;

    if (ReadDWord(Patch) != '91AP')
        return ERROR_PATCH_DECODE_FAILURE;

    /* Read the flags, warn about an unknown combination */
    Header->Flags = ReadDWord(Patch);
    if (Header->Flags ^ UNKNOWN_FLAGS_COMBINATION)
        ERR("Unknown flags: 0x%x, patch will most likely fail\n", Header->Flags ^ UNKNOWN_FLAGS_COMBINATION);

    /* 0x5bb3284e, 0x5bb33562, 0x5bb357b1 */
    Unknown = ReadDWord(Patch);
    TRACE("Unknown: 0x%x\n", Unknown);

    Header->OutputSize = DecodeDWord(Patch);
    Header->OutputCrc = ReadDWord(Patch);

    Unknown = ReadByte(Patch);
    if (Unknown != 1)
        ERR("Field after CRC is not 1 but %u\n", Unknown);

    Delta = DecodeInt(Patch);
    Header->OldSize = Header->OutputSize + Delta;
    Header->OldCrc = ReadDWord(Patch);

    Unknown = ReadUShort(Patch);
    if (Unknown != 0)
        ERR("Field1 after OldCrc is not 0 but %u\n", Unknown);

    Unknown = DecodeDWord(Patch);
    if (Unknown != 0)
        ERR("Field2 after OldCrc is not 0 but %u\n", Unknown);

    Header->DataSize = DecodeDWord(Patch);
                            /* Remaining data, minus the CRC appended */
    if (Header->DataSize != (Patch->Size - (Patch->Ptr - Patch->Root) - sizeof(DWORD)))
    {
        ERR("Unable to read header, check previous logging!\n");
        return ERROR_PATCH_DECODE_FAILURE;
    }
    return STATUS_SUCCESS;
}
static AudErrorCode MDReadAudibleSpecificHeader( IAudibleInputStream *pFile, CAudibleSpecificHeader *pSpecificHead, long sectionSize )

{ BOOL readRes =  
    ReadUShort( pFile, &pSpecificHead->hashTableVersion )                                                             &&
    AUD_NO_ERROR == pFile->pVTable->Read( pFile, pSpecificHead->szTitleID,        sizeof( pSpecificHead->szTitleID ), NULL        ) &&
    AUD_NO_ERROR == pFile->pVTable->Read( pFile, pSpecificHead->digitalSignature, sizeof( pSpecificHead->digitalSignature ), NULL ) &&
    ReadULong( pFile, &pSpecificHead->audioSizeBytes )                                                                &&
    ReadULong( pFile, &pSpecificHead->audioTimeSeconds )                                                              &&
    ReadULong( pFile, &pSpecificHead->hashBlockSize    )                                                              &&
    ReadULong( pFile, &pSpecificHead->unused );
//----------------  
  if ( !readRes )
  { return AUD_FILE_READ_FAIL;
  }
  return AUD_NO_ERROR;
}
Example #8
0
/**
 * @name CreateNewFileFromPatch
 * Using the input @param Header and @param Patch, create a new file on @param new_file
 *
 * @param Header
 * Parsed / preprocessed patch header
 *
 * @param Patch
 * Memory buffer pointing to the patch payload
 *
 * @param new_file
 * A handle to the output file. This file will be resized
 *
 * @return STATUS_SUCCESS on success, an error code otherwise
 */
DWORD CreateNewFileFromPatch(PATCH_HEADER* Header, SAFE_READ* Patch, HANDLE new_file)
{
    SAFE_READ NewFile;
    HANDLE hMap;
    USHORT BlockSize;
    DWORD dwStatus;
    struct LZXstate* state;
    int lzxResult;

    hMap = CreateFileMappingW(new_file, NULL, PAGE_READWRITE, 0, Header->OutputSize, NULL);
    if (hMap == INVALID_HANDLE_VALUE)
        return ERROR_PATCH_NOT_AVAILABLE;

    NewFile.Root = NewFile.Ptr = MapViewOfFile(hMap, FILE_MAP_WRITE, 0, 0, 0);
    CloseHandle(hMap);
    NewFile.Size = Header->OutputSize;

    if (!NewFile.Root)
        return ERROR_PATCH_NOT_AVAILABLE;

    /* At this point Patch->Ptr should point to the payload */
    BlockSize = ReadUShort(Patch);

    /* This window size does not work on all files (for example, MS SQL Express 2008 setup) */
    state = LZXinit(17);
    if (state)
    {
        lzxResult = LZXdecompress(state, Patch->Ptr, NewFile.Ptr, BlockSize, NewFile.Size);
        LZXteardown(state);

        if (lzxResult == DECR_OK)
            dwStatus = STATUS_SUCCESS;
        else
            dwStatus = ERROR_PATCH_DECODE_FAILURE;
    }
    else
    {
        dwStatus = ERROR_INSUFFICIENT_BUFFER;
    }

    UnmapViewOfFile(NewFile.Root);
    return dwStatus;
}
Example #9
0
//Triangle
void NifStream( Triangle & val, istream& in, const NifInfo & info ) {
	val.v1 = ReadUShort( in );
	val.v2 = ReadUShort( in );
	val.v3 = ReadUShort( in );
};
static AudErrorCode MDReadAudibleTableOfContent( IAudibleInputStream *pFile, CAudibleAllChapters *pAudChapters, long sectionSize )

{
  int	i;

  //---- AA file consist of multiple sections, each section - multiple blocks.
  BOOL readRes = ReadULong( pFile, &pAudChapters->audioChapetsCount ); // Read count of audio chapters.
  if ( !readRes )
  { return AUD_FILE_READ_FAIL;
  }

  //---- Check for valid chapter count
  if ( pAudChapters->audioChapetsCount > AUDIO_CHAPTERS_MAX_COUNT )
  { return AUD_AA_FILE_BAD_FORMAT;
  } 

  //----------- Now allocate memory for audio chapters.
  pAudChapters->pAudioChap = (CAudibleOneAudioChapter *)Audible_malloc( pAudChapters->audioChapetsCount * sizeof( CAudibleOneAudioChapter ) );
  if ( pAudChapters->pAudioChap == NULL )
  { return AUD_MEM_ALLOC_FAILED;
  }

  //----------- Good, now iterate through all sections.
  for ( i = 0; i < pAudChapters->audioChapetsCount; i++ )
  { char unUsed[8];
    long entriesCount;
	CAudibleOneAudioChapter *p = &pAudChapters->pAudioChap[i];
    readRes = 
      ReadLong( pFile,  &p->blockCount )                                &&
      AUD_NO_ERROR == pFile->pVTable->Read( pFile, unUsed, sizeof( unUsed ), NULL )  &&
      ReadLong( pFile, &p->audioSize  )                                &&
      ReadLong( pFile,  &p->audioTimeSec )                              &&
      ReadUShort(pFile, &p->codecID )                                   &&
      ReadLong( pFile,  &entriesCount );
    if ( !readRes )
    { return AUD_FILE_READ_FAIL;
    }
	//---- For metadata, just assume one block
    p->pAudioBlockInfo = (CAudibleBlockInfo *)Audible_malloc( sizeof( CAudibleBlockInfo ) );	//p->audBlockInfo;
	if ( p->pAudioBlockInfo == NULL )
	  return AUD_MEM_ALLOC_FAILED;
    //--------------------- Now we skip time stamp table, as we know the bitrate. 
    { unsigned long curPos = pFile->pVTable->GetCurrentPos( pFile );
      curPos += entriesCount * 8;
      if ( AUD_NO_ERROR != pFile->pVTable->SetCurrentPos( pFile, curPos ) )
      { return AUD_FILE_SEEK_FAIL;
      }
    }   
  }

/*
  //---- Read one chapter to get the codecID. First skip some data we don't need right now...
  pAudChapters->pAudioChap = &tempChapter;
  if ( pFile->pVTable->Read( pFile, unUsed, 4*5, NULL ) != AUD_NO_ERROR )
    return AUD_FILE_READ_FAIL;
*/
/*
  pAudChapters->pAudioChap = &tempChapter;
  for ( i = 0; i < 5; i++ )
    if ( !ReadULong( pFile, &dummy ) )
    { return AUD_FILE_READ_FAIL;
    }
*/
/*
  //---- Read codec ID
  if ( !ReadUShort( pFile, &pAudChapters->pAudioChap[0].codecID ) )
  { return AUD_FILE_READ_FAIL;
  }
*/
/*
//----------- Now allocate memory for audio chapters.
//  pAudChapters->audioChapetsCount = minimum( AUDIO_CHAPTERS_MAX_COUNT, pAudChapters->audioChapetsCount );
  pAudChapters->pAudioChap = (CAudibleOneAudioChapter *)Audible_malloc( pAudChapters->audioChapetsCount * sizeof( CAudibleOneAudioChapter ) );
  if ( pAudChapters->pAudioChap == NULL )
  { return AUD_MEM_ALLOC_FAILED;
  }
//----------- Good, now iterates through all sections.
  { unsigned long i = 0;
    for ( i = 0; i < pAudChapters->audioChapetsCount; i++ )
    { short unUsed[4];
      long entriesCount = 0;
      readRes = 
        ReadLong( pFile,  &pAudChapters->pAudioChap[i].blockCount )                                &&
        AUD_NO_ERROR == pFile->pVTable->Read( pFile, unUsed, 2 * sizeof( unUsed ) / sizeof( unUsed[0] ), NULL )  &&
        ReadLong( pFile, &pAudChapters->pAudioChap[i].audioSize  )                                &&
        ReadLong( pFile,  &pAudChapters->pAudioChap[i].audioTimeSec )                              &&
        ReadUShort(pFile, &pAudChapters->pAudioChap[i].codecID )                                   &&
        ReadLong( pFile,  &entriesCount );
      if ( !readRes )
      { return AUD_FILE_READ_FAIL;
      }
  //---------------------  If count of blocks more than 4, we allocate memory dynamically
      if ( pAudChapters->pAudioChap[i].blockCount <= MAX_DEFAULD_AUDIOBLOCK_COUNT )
      { pAudChapters->pAudioChap[i].pAudioBlockInfo = pAudChapters->pAudioChap[i].audBlockInfo;
      }
      else
      { pAudChapters->pAudioChap[i].pAudioBlockInfo = 
          ( CAudibleBlockInfo *)Audible_malloc( pAudChapters->pAudioChap[i].blockCount * sizeof( CAudibleBlockInfo ) );
      }      
  //--------------------- Now we skip time stamp table, as we know the bitrate. 
  //    { unsigned long curPos = pFile->pVTable->GetCurrentPos( pFile );
  //      curPos += entriesCount * 8;
  //      if ( AUD_NO_ERROR != pFile->pVTable->SetCurrentPos( pFile, curPos ) )
  //      { return AUD_FILE_SEEK_FAIL;
  //      }
  //    }   
    }
    return AUD_NO_ERROR;
  }
*/

  return AUD_NO_ERROR;
}
Example #11
0
ShortStringHash Deserializer::ReadShortStringHash()
{
    return ShortStringHash(ReadUShort());
}
Example #12
0
void loadImage(char *filename)
{
   int i,j;
   int gotindex = FALSE;
   unsigned char grey,r,g,b;
   HEADER header;
   INFOHEADER infoheader;
   FILE *fptr;
   gMem = 300;

   /* Open file */
   if ((fptr = fopen(filename,"r")) == NULL) {
      fprintf(stderr,"Unable to open BMP file \"%s\"\n",filename);
      exit(-1);
   }
   gMem = 301;

   /* Read and check the header */

   ReadUShort(fptr,&header.type,FALSE);
#ifdef DEBUG
   fprintf(stderr,"ID is: %d, should be %d\n",header.type,'M'*256+'B');
#endif
   ReadUInt(fptr,&header.size,FALSE);
#ifdef DEBUG
   fprintf(stderr,"File size is %d bytes\n",header.size);
#endif
   ReadUShort(fptr,&header.reserved1,FALSE);
   ReadUShort(fptr,&header.reserved2,FALSE);
   ReadUInt(fptr,&header.offset,FALSE);
#ifdef DEBUG
   fprintf(stderr,"Offset to image data is %d bytes\n",header.offset);
#endif

   /* Read and check the information header */
   if (fread(&infoheader,sizeof(INFOHEADER),1,fptr) != 1) {
      fprintf(stderr,"Failed to read BMP info header\n");
      exit(-1);
   }
#ifdef DEBUG
   fprintf(stderr,"Image size = %d x %d\n",infoheader.width,infoheader.height);
   fprintf(stderr,"Number of colour planes is %d\n",infoheader.planes);
   fprintf(stderr,"Bits per pixel is %d\n",infoheader.bits);
   fprintf(stderr,"Compression type is %d\n",infoheader.compression);
   fprintf(stderr,"Number of colours is %d\n",infoheader.ncolours);
   fprintf(stderr,"Number of required colours is %d\n",
      infoheader.importantcolours);
#endif

   /* Read the lookup table if there is one */
   for (i=0;i<255;i++) {
     colourindex[i].r = 0;
     colourindex[i].g = 0;
     colourindex[i].b = 0;
     colourindex[i].junk = 0;
   }

   if (infoheader.ncolours > 0) {
#ifdef DEBUG
     fprintf(stderr, "There is a color index table\n");
#endif
      for (i=0;i<infoheader.ncolours;i++) {
         if (fread(&colourindex[i].b,sizeof(unsigned char),1,fptr) != 1) {
            fprintf(stderr,"Image read failed\n");
            exit(-1);
         }
         if (fread(&colourindex[i].g,sizeof(unsigned char),1,fptr) != 1) {
            fprintf(stderr,"Image read failed\n");
            exit(-1);
         }
         if (fread(&colourindex[i].r,sizeof(unsigned char),1,fptr) != 1) {
            fprintf(stderr,"Image read failed\n");
            exit(-1);
         }
         if (fread(&colourindex[i].junk,sizeof(unsigned char),1,fptr) != 1) {
            fprintf(stderr,"Image read failed\n");
            exit(-1);
         }
	 /*
        fprintf(stderr,"%3d\t%3d\t%3d\t%3d\n",i,
            colourindex[i].r,colourindex[i].g,colourindex[i].b);
	 */
      }
      gotindex = TRUE;
   }
   else
     {
       printf("No color index table\n");
       exit(0);
     }
   gMem = 302;

   /* Free and/or allocate memory for the bitmap */
   if (image != (char *)NULL) {
#ifdef DEBUG
     fprintf(stderr, "Freeing previous image array\n");
#endif
     free(image);
   }
   gMem = 303;
   /* Set the *global* dimensions for future reference   */
   /* width (w) may not be a multiple of 4, but dataw is */

   h = infoheader.height;
   dataw = w = infoheader.width;
   dataw += (w%4) ? 4-(w%4) : 0;

   //   printf("height = %d dataw=%d\n", h,dataw);
   image = (unsigned char *)malloc((h+1)*(dataw+1));
   //   printf("H %d W %d HXW %d\n", h, w, h*dataw);
   //   printf("IMAGE %x (%d) %x\n", image, (h+1)*(dataw+1), image+((h+1)*(dataw+1)));
   gMem = 304;

   /* Seek to the start of the image data */
   fseek(fptr,header.offset,SEEK_SET);

   /* Read each image line, and set each byte to white(0) or black(1) */
   for (j=0;j<h;j++) {
     gMem = 10000;
     unsigned char *line = &image[j*dataw];
     if (fread(line,sizeof(unsigned char),dataw,fptr) != dataw)
       {
	 fprintf(stderr,"Image read failed scanning line %d of %d\n",j,h);
	 exit(-1);
       }
#ifdef A1

#else
     for (i=0;i<w;i++)
       {
	 int d = line[i];
	 if (TABLEBLACK(d))
	   line[i] = 1;
	 else
	   line[i] = 0;
       }
#endif
   }
   fclose(fptr);
   gMem = 30000;
}