Beispiel #1
0
bool CCBReader::readHeader()
{
    /* If no bytes loaded, don't crash about it. */
    if(this->_bytes == nullptr) {
        return false;
    }

    /* Read magic bytes */
    int magicBytes = *((int*)(this->_bytes + this->_currentByte));
    this->_currentByte += 4;

    if(CC_SWAP_INT32_BIG_TO_HOST(magicBytes) != (*reinterpret_cast<const int*>("ccbi"))) {
        return false;
    }

    /* Read version. */
    int version = this->readInt(false);
    if(version != CCB_VERSION) {
        log("WARNING! Incompatible ccbi file version (file: %d reader: %d)", version, CCB_VERSION);
        return false;
    }

    // Read JS check
    _jsControlled = this->readBool();
    _animationManager->_jsControlled = _jsControlled;

    return true;
}
Beispiel #2
0
int ZipUtils::ccInflateCCZFile(const char *path, unsigned char **out)
{
     CCAssert(out, "");
     CCAssert(&*out, "");

     // load file into memory
     unsigned char* compressed = NULL;
    
     unsigned long fileLen = 0;
     compressed = CCFileUtils::getFileData(path, "rb", &fileLen);

     if(NULL == compressed || 0 == fileLen) 
     {
         CCLOG("cocos2d: Error loading CCZ compressed file");
         return -1;
     }

     struct CCZHeader *header = (struct CCZHeader*) compressed;

     // verify header
     if( header->sig[0] != 'C' || header->sig[1] != 'C' || header->sig[2] != 'Z' || header->sig[3] != '!' ) 
     {
         CCLOG("cocos2d: Invalid CCZ file");
         delete [] compressed;
         return -1;
     }

     // verify header version
     unsigned int version = CC_SWAP_INT16_BIG_TO_HOST( header->version );
     if( version > 2 ) 
     {
         CCLOG("cocos2d: Unsupported CCZ header format");
         delete [] compressed;
         return -1;
     }

     // verify compression format
     if( CC_SWAP_INT16_BIG_TO_HOST(header->compression_type) != CCZ_COMPRESSION_ZLIB ) 
     {
         CCLOG("cocos2d: CCZ Unsupported compression method");
         delete [] compressed;
         return -1;
     }

     unsigned int len = CC_SWAP_INT32_BIG_TO_HOST( header->len );

     *out = (unsigned char*)malloc( len );
     if(! *out )
     {
         CCLOG("cocos2d: CCZ: Failed to allocate memory for texture");
         delete [] compressed;
         return -1;
     }


     unsigned long destlen = len;
     unsigned long source = (unsigned long) compressed + sizeof(*header);
     int ret = uncompress(*out, &destlen, (Bytef*)source, fileLen - sizeof(*header) );

     delete [] compressed;

     if( ret != Z_OK )
     {
         CCLOG("cocos2d: CCZ: Failed to uncompress data");
         free( *out );
         *out = NULL;
         return -1;
     }

     return len;
}
KDint ZipUtils::ccInflateCCZFile ( const KDchar* szFilePath, KDubyte** ppDst )
{
	CCAssert ( ppDst  , "" );
	CCAssert ( &*ppDst, "" );

	// load file into memory
	KDubyte*  pCompressed = KD_NULL;
        
    KDint  nLenFile = 0;
    pCompressed = CCFileUtils::sharedFileUtils ( )->getFileData ( szFilePath, "rb", (KDsize *) ( &nLenFile ) );

	if ( KD_NULL == pCompressed || nLenFile == 0 ) 
	{
		//CCLOG ( "XMCocos2D : Error loading CCZ compressed file" );
        return -1;
	}
 
	struct CCZHeader*  pHeader = (struct CCZHeader*) pCompressed;

	// verify header
	if ( pHeader->sig[0] != 'C' || pHeader->sig[1] != 'C' || pHeader->sig[2] != 'Z' || pHeader->sig[3] != '!' ) 
	{
		// CCLOG ( "XMCocos2D : Invalid CCZ file" );
		CC_SAFE_DELETE_ARRAY ( pCompressed );
		return -1;
	}
 
	// verify header version
	KDuint  uVersion = CC_SWAP_INT16_BIG_TO_HOST ( pHeader->version );
	if ( uVersion > 2 ) 
	{
		CCLOG ( "XMCocos2D : Unsupported CCZ header format" );
		CC_SAFE_DELETE_ARRAY ( pCompressed );
		return -1;
	}
 
	// verify compression format
	if ( CC_SWAP_INT16_BIG_TO_HOST ( pHeader->compression_type ) != CCZ_COMPRESSION_ZLIB ) 
	{
		CCLOG ( "XMCocos2D : CCZ Unsupported compression method" );
		CC_SAFE_DELETE_ARRAY ( pCompressed );
		return -1;
	}

	KDuint  uLen = CC_SWAP_INT32_BIG_TO_HOST ( pHeader->len );
 
	*ppDst = (KDubyte*) kdMalloc ( uLen );
	if ( !*ppDst )
	{
		CCLOG ( "XMCocos2D : CCZ - Failed to allocate memory for texture" );
		CC_SAFE_DELETE_ARRAY ( pCompressed );
		return -1;
	}

	KDsize  uLenDst = uLen;
	KDsize  uSizeSrc = (KDsize) pCompressed + sizeof ( *pHeader );
	KDint   nRet = uncompress ( *ppDst, (uLongf *) &uLenDst, (Bytef *) uSizeSrc, nLenFile - sizeof ( *pHeader ) );

	CC_SAFE_DELETE_ARRAY ( pCompressed );

	if ( nRet != Z_OK )
	{
		CCLOG ( "XMCocos2D : CCZ - Failed to uncompress data" );
		CC_SAFE_FREE ( *ppDst );
		return -1;
	}

	return uLen;
}