Example #1
0
static char *try_uncompress_with_tzx(const char *name)
{
    char *tmp_name = NULL;
    size_t l = strlen(name);
    int exit_status;
    char *argv[4];

    /* Check whether the name sounds like a tzx file. */
    if (l < 4 || strcasecmp(name + l - 4, ".tzx") != 0) {
        return NULL;
    }

    /* `exec*()' does not want these to be constant...  */
    argv[0] = lib_stralloc("64tzxtap");
    argv[1] = archdep_filename_parameter(name);
    argv[2] = NULL;

    ZDEBUG(("try_uncompress_with_tzx: spawning 64tzxtap %s", name));
    exit_status = archdep_spawn("64tzxtap", argv, &tmp_name, NULL);

    lib_free(argv[0]);
    lib_free(argv[1]);

    if (exit_status == 0) {
        ZDEBUG(("try_uncompress_with_tzx: OK"));
        return tmp_name;
    } else {
        ZDEBUG(("try_uncompress_with_tzx: failed"));
        ioutil_remove(tmp_name);
        lib_free(tmp_name);
        return NULL;
    }
}
Example #2
0
/* Compress `src' into `dest' using bzip.  */
static int compress_with_bzip(const char *src, const char *dest)
{
    static char *argv[4];
    int exit_status;
    char *mdest;

    /* `exec*()' does not want these to be constant...  */
    argv[0] = lib_stralloc("bzip2");
    argv[1] = lib_stralloc("-c");
    argv[2] = lib_stralloc(src);
    argv[3] = NULL;

    mdest = lib_stralloc(dest);

    ZDEBUG(("compress_with_bzip: spawning bzip -c %s", src));
    exit_status = archdep_spawn("bzip2", argv, &mdest, NULL);

    lib_free(mdest);

    lib_free(argv[0]);
    lib_free(argv[1]);
    lib_free(argv[2]);

    if (exit_status == 0) {
        ZDEBUG(("compress_with_bzip: OK."));
        return 0;
    } else {
        ZDEBUG(("compress_with_bzip: failed."));
        return -1;
    }
}
Example #3
0
BOOLEAN DeauthReq(Signal_t *signal)
{
	FrmDesc_t *pfrmDesc;
	MacAddr_t Sta;
	ReasonCode Rsn = RC_UNSPEC_REASON;
	U8	vapId = 0;
	
	ZDEBUG("DeauthReq");	

	memcpy((U8 *)&Sta, (U8 *)&signal->frmInfo.Sta, 6);
	Rsn = signal->frmInfo.rCode;
	
	vapId = signal->vapId;
	UpdateStaStatus(&Sta, STATION_STATE_NOT_AUTH, vapId);
	
	pfrmDesc = allocFdesc();
	if(!pfrmDesc){
		sigEnque(pMgtQ, (signal));
		return FALSE;
	}
	
	mkDisAssoc_DeAuthFrm(pfrmDesc, ST_DEAUTH, &Sta, Rsn, vapId);
	sendMgtFrame(signal, pfrmDesc);
	
	return FALSE;
}
Example #4
0
BOOLEAN DisasocReq(Signal_t *signal)
{
	FrmDesc_t *pfrmDesc;
	MacAddr_t Sta;
	ReasonCode Rsn = RC_UNSPEC_REASON;
	U8 vapId = 0;

	ZDEBUG("DisasocReq");
	
	memcpy((U8 *)&Sta, (U8 *)&signal->frmInfo.Sta, 6);
	Rsn = signal->frmInfo.rCode;
	pdot11Obj->StatusNotify(STA_DISASSOCIATED, (U8 *)&Sta);

	vapId = signal->vapId;
	UpdateStaStatus(&Sta, STATION_STATE_DIS_ASOC, vapId);
	
	pfrmDesc = allocFdesc();
	if(!pfrmDesc){
		sigEnque(pMgtQ, (signal));
		return FALSE;
	}

	mkDisAssoc_DeAuthFrm(pfrmDesc, ST_DISASOC, &Sta, Rsn, vapId);
	sendMgtFrame(signal, pfrmDesc);
	
	return FALSE;
}
Example #5
0
BOOLEAN Disasoc(Signal_t *signal)
{
	FrmDesc_t *pfrmDesc;
	Frame_t *rdu;
	MacAddr_t Sta;
	ReasonCode Rsn;
	U8 vapId = 0;
	
	ZDEBUG("Disasoc");
	pfrmDesc	= signal->frmInfo.frmDesc;
	rdu = pfrmDesc->mpdu;
	memcpy((U8 *)&Sta, (U8 *)(addr2(rdu)), 6);
	Rsn = (ReasonCode)(reason(rdu));

	if(memcmp(addr1(rdu), (U8*)&mBssId, 6)){ //Not for this BSSID
		freeFdesc(pfrmDesc);
		return TRUE;
	}

	UpdateStaStatus(&Sta, STATION_STATE_DIS_ASOC, vapId);
	freeFdesc(pfrmDesc);
	
	//here to handle disassoc ind.
	pdot11Obj->StatusNotify(STA_DISASSOCIATED, (U8 *)&Sta);
	return TRUE;
}
Example #6
0
BOOLEAN ProbeReq(Signal_t *signal)
{
	FrmDesc_t *pfrmDesc;
	Frame_t *rdu;
	MacAddr_t sta;
	Element rSsid;
	Element *pWPA = NULL;
	U8 vapId = 0;

	ZDEBUG("ProbeReq");
	pfrmDesc = signal->frmInfo.frmDesc;
	rdu = pfrmDesc->mpdu;
	
	if (!getElem(rdu, EID_SSID, &rSsid))
		goto release;
	
	if (mHiddenSSID){ //discard broadcast ssid
		if (eLen(&rSsid) == 0){
			goto release;
		}
	}	
	memcpy((U8*)&sta, (U8*)addr2(rdu), 6);
	
	if (eLen(&rSsid) == 0){
		
		//WPA
		if (mDynKeyMode == DYN_KEY_TKIP)	
			pWPA = &mWPAIe;
		mkProbeRspFrm(pfrmDesc, &sta, mBeaconPeriod, mCap, &dot11DesiredSsid, &mBrates, &mPhpm, NULL, (Element *)pWPA, vapId);		
		return sendMgtFrame(signal, pfrmDesc);	
	}
	else{
	 	if (memcmp(&rSsid, &dot11DesiredSsid, eLen(&dot11DesiredSsid)+2) == 0){
			//WPA
			if (mDynKeyMode == DYN_KEY_TKIP)	
				pWPA = &mWPAIe;
			
			mkProbeRspFrm(pfrmDesc, &sta, mBeaconPeriod, mCap, &dot11DesiredSsid, &mBrates, &mPhpm, NULL, (Element *)pWPA, vapId);		
			return sendMgtFrame(signal, pfrmDesc);
		}	
	}
	
release:
   	ZDEBUG("goto release");
	freeFdesc(pfrmDesc);
	return TRUE;
}
Example #7
0
void QArchive::setOutDir(const QString &odir)
{
	Q_D(QArchive);
	d->outDir = odir;
	if(!QDir(d->outDir).exists()) {
		ZDEBUG("out dir %s doesn't exist. creating...",qPrintable(d->outDir));
		QDir().mkdir(d->outDir);
	}
}
Example #8
0
/* If `name' has a bzip-like extension, try to uncompress it into a temporary
   file using bzip.  If this succeeds, return the name of the temporary file;
   return NULL otherwise.  */
static char *try_uncompress_with_bzip(const char *name)
{
    char *tmp_name = NULL;
    size_t l = strlen(name);
    int exit_status;
    char *argv[4];

    /* Check whether the name sounds like a bzipped file by checking the
       extension.  MSDOS and UNIX variants of bzip v2 use the extension
       '.bz2'.  bzip v1 is obsolete.  */
    if (l < 5 || strcasecmp(name + l - 4, ".bz2") != 0) {
        return NULL;
    }

    /* `exec*()' does not want these to be constant...  */
    argv[0] = lib_stralloc("bzip2");
    argv[1] = lib_stralloc("-cd");
    argv[2] = archdep_filename_parameter(name);
    argv[3] = NULL;

    ZDEBUG(("try_uncompress_with_bzip: spawning bzip -cd %s", name));
    exit_status = archdep_spawn("bzip2", argv, &tmp_name, NULL);

    lib_free(argv[0]);
    lib_free(argv[1]);
    lib_free(argv[2]);

    if (exit_status == 0) {
        ZDEBUG(("try_uncompress_with_bzip: OK"));
        return tmp_name;
    } else {
        ZDEBUG(("try_uncompress_with_bzip: failed"));
        ioutil_remove(tmp_name);
        lib_free(tmp_name);
        return NULL;
    }
}
Example #9
0
/* Handle close of a (compressed file). `ptr' points to the zfile to close.  */
static int handle_close(zfile_t *ptr)
{
    ZDEBUG(("handle_close: closing `%s' (`%s'), write_mode = %d",
            ptr->tmp_name ? ptr->tmp_name : "(null)",
            ptr->orig_name, ptr->write_mode));

    if (ptr->tmp_name) {
        /* Recompress into the original file.  */
        if (ptr->orig_name
            && ptr->write_mode
            && zfile_compress(ptr->tmp_name, ptr->orig_name, ptr->type)) {
            return -1;
        }

        /* Remove temporary file.  */
        if (ioutil_remove(ptr->tmp_name) < 0) {
            log_error(zlog, "Cannot unlink `%s': %s", ptr->tmp_name, strerror(errno));
        }
    }

    handle_close_action(ptr);

    /* Remove item from list.  */
    if (ptr->prev != NULL) {
        ptr->prev->next = ptr->next;
    } else {
        zfile_list = ptr->next;
    }

    if (ptr->next != NULL) {
        ptr->next->prev = ptr->prev;
    }

    if (ptr->orig_name) {
        lib_free(ptr->orig_name);
    }
    if (ptr->tmp_name) {
        lib_free(ptr->tmp_name);
    }
    if (ptr->request_string) {
        lib_free(ptr->request_string);
    }

    lib_free(ptr);

    return 0;
}
Example #10
0
int main(int argc, char *argv[])
{
    appName = getFileName(argv[0]);
	qDebug("%s %s\nQt %s\n", APP_NAME, APP_VERSION_STR, qVersion());

	opts_t options=opts_parse(argc,argv);

    ZApplication a(argc, argv/*, QApplication::GuiClient*/);
#if CONFIG_QT4
	QApplication::setApplicationName(APP_NAME);
#if QT_VERSION >= QT_VERSION_CHECK(4, 4, 0)
	QApplication::setApplicationVersion(APP_VERSION_STR);
#endif
	QApplication::setOrganizationName("Wang Bin");

#endif

#if CONFIG_QT4
    QString dirname = QCoreApplication::applicationDirPath();
#else
	QString dirname=QFileInfo(argv[0]).dirPath();
	//QString dirname=getFileDir(argv[0]);//
#endif
	//QDir::setCurrent(dirname); //bad in windows cygwin
	ZDEBUG("dir: %s",qPrintable(dirname));

	QTranslator appTranslator(0);
#if CONFIG_EZX
	QString sysLang=ZLanguage::getSystemLanguageCode();
	appTranslator.load(APP_NAME "_" + sysLang, dirname + "/i18n");
	//QString(dirname)+"/i18n" will load fail, 乱码
#else
	QString sysLang=QLocale::system().name();
	appTranslator.load(APP_NAME "-" + sysLang, dirname + "/i18n");
#endif //CONFIG_EZX
	ZDEBUG("system language: %s",qPrintable(sysLang));
	a.installTranslator(&appTranslator);
	//Need QtTranslator

	//QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));

	Qop qop;
#if !CONFIG_QT4
	a.setMainWidget(qop.progress);
#endif //CONFIG_QT4
	ZDEBUG("Steps from options: %d",options->steps);
	qop.setTimeFormat(options->time_format);
	qop.setInterval(options->interval);
	qop.setUpdateAllMessage(options->all_msg);
	if(!options->hide)
		qop.progress->show();
	//order is important
	//qop.parser_type=options->parser_type;
	qop.setArchive(options->x_file);

	//internal method
	if(options->diy || argc<2)  {
		qop.extract(options->x_file,options->out_dir);
		//qDebug("%s", options->x_file);
	}
	else if(!options->cmd==0)
		qop.execute(QString::fromLocal8Bit(options->cmd));
	else {
		qop.parser_type=options->parser_type;
		ZDEBUG("steps %d",options->steps);
		qop.initParser();
		if(options->unit==0) qop.parser->setCountType(QCounterThread::Size);
		else if(options->unit==1) qop.parser->setCountType(QCounterThread::Num);
		if(options->steps>0) {
			qop.parser->setRecount(false);
			qop.parser->setTotalSize(options->steps);
			//qop.progress->setMaximum(options->steps);
		}
		if(qop.steps<=0) { //compress
			if(options->steps>0) qop.parser->setTotalSize(options->steps);
			QStringList files=QStringList();
		//why is optind?
			for(int i=options->optind;i<argc;++i) files<<argv[i];
			qop.parser->setFiles(files);
			qop.parser->setMultiThread(options->multi_thread);
			qop.parser->startCounterThread();
		}
#if NO_SOCKET
		qop.parser->start();
#endif
	}
#if CONFIG_EZX
	a.processEvents();
#endif
	//progress->exec();
	if(options->auto_close) exit(0);

	return a.exec();
}
Example #11
0
BOOLEAN Re_AsocReq(Signal_t *signal)
{
	FrmDesc_t *pfrmDesc;
	Frame_t *rdu;
	MacAddr_t Sta;
	U16 aid = 0;
	StatusCode asStatus;
	U8 lsInterval;
	Element WPA;
	Element asSsid;
	Element asRates;
	//Element extRates;
	U16 cap;
	U8 ZydasMode = 0;
	int i;
	U8 tmpMaxRate = 0x02;
	U8 MaxRate;
	U16 notifyStatus = STA_ASOC_REQ;
	U16 notifyStatus1 = STA_ASSOCIATED;
	TypeSubtype type = ST_ASOC_RSP;
	U8	Preamble = 0;
	U8	HigestBasicRate = 0;
	U8	vapId = 0;
	U8	Len;
	BOOLEAN bErpSta = FALSE;

	ZDEBUG("Re_AsocReq");
	pfrmDesc = signal->frmInfo.frmDesc;
	rdu = pfrmDesc->mpdu;
	lsInterval = listenInt(pfrmDesc->mpdu);
	//FPRINT_V("lsInterval", lsInterval);
	cap = cap(pfrmDesc->mpdu);
	memcpy((U8 *)&Sta, (U8 *)addr2(rdu), 6);
	
	if ((isGroup(addr2(rdu))) ||  (!getElem(rdu, EID_SSID, &asSsid))
		|| (!getElem(rdu, EID_SUPRATES, &asRates))){
		freeFdesc(pfrmDesc);
		return TRUE;
	}
	
	if ((eLen(&asSsid) != eLen(&dot11DesiredSsid) || 
		memcmp(&asSsid, &dot11DesiredSsid, eLen(&dot11DesiredSsid)+2) != 0)){
		freeFdesc(pfrmDesc);
		return TRUE;
	}		
	//chaeck capability
	if (cap & CAP_SHORT_PREAMBLE)
		Preamble = 1;
	else
		Preamble = 0;
		
	// Privacy not match
	if (cap & CAP_PRIVACY){
		if (!mPrivacyInvoked){
			freeFdesc(pfrmDesc);
			return TRUE;
		}	
	}
	else {
		if (mPrivacyInvoked){
			freeFdesc(pfrmDesc);
			return TRUE;
		}	
	}		
	Len = eLen(&asRates);	
	for (i=0; i<Len; i++){
		if ( (asRates.buf[2+i] & 0x7f) > tmpMaxRate ){
			tmpMaxRate = (asRates.buf[2+i] & 0x7f);
			if (asRates.buf[2+i] & 0x80)
				HigestBasicRate = asRates.buf[2+i];
		}	
				
		if (((asRates.buf[2+i] & 0x7f) == 0x21) && (!(cap & CAP_PBCC_ENABLE))){ //Zydas 16.5M
			void *reg = pdot11Obj->reg;
			
			ZydasMode = 1;
			mZyDasModeClient = TRUE;
			//FPRINT("ZydasMode");
		}	
	}	
	
	MaxRate = RateConvert((tmpMaxRate & 0x7f), cap);			
	
	if (signal->id == SIG_REASSOC_REQ)	
		notifyStatus = STA_REASOC_REQ;
		
	if (!pdot11Obj->StatusNotify(notifyStatus, (U8 *)&Sta)){ //Accept it
		if (mDynKeyMode == DYN_KEY_TKIP){	
			if (getElem(rdu, EID_WPA, &WPA)){
				//zd1205_OctetDump("AssocRequest = ", asRdu->body, asRdu->bodyLen);
				//zd1205_OctetDump("AssocRequest WPA_IE = ", &WPA.buf[2], WPA.buf[1]);
				
				if (pdot11Obj->AssocRequest((U8 *)&Sta, rdu->body, rdu->bodyLen)){ //reject
					asStatus = SC_UNSPEC_FAILURE;
					goto check_failed;
					//we need reason code here
				}	
			}
			else{
				asStatus = SC_UNSPEC_FAILURE;
				goto wpa_check_failed;		
		}		
		}		

wpa_check_ok:			
		if (!UpdateStaStatus(&Sta, STATION_STATE_ASOC, vapId)){
			asStatus = SC_AP_FULL;
		}
		else{
			AssocInfoUpdate(&Sta, MaxRate, lsInterval, ZydasMode, Preamble, bErpSta, vapId);
			aid = AIdLookup(&Sta);
			asStatus = SC_SUCCESSFUL;
			if (signal->id == SIG_REASSOC_REQ)	
				notifyStatus1 = STA_REASSOCIATED;
			pdot11Obj->StatusNotify(notifyStatus1, (U8 *)&Sta);
		}
	}
	else{
wpa_check_failed:	
		asStatus = SC_UNSPEC_FAILURE;
	}	
	
	aid |= 0xC000;
	if (aid != 0xC000){
		#ifdef B500_DEBUG
			FPRINT_V("Aid", aid);
			FPRINT_V("MaxRate", MaxRate);
		#endif

	}	
	
check_failed:
	if (signal->id == SIG_REASSOC_REQ)	
		type = ST_REASOC_RSP;	
	mkRe_AsocRspFrm(pfrmDesc, type, &Sta, mCap, asStatus, aid, &mBrates, NULL, vapId);
	sendMgtFrame(signal, pfrmDesc);

	return FALSE;
}
Example #12
0
void QArchive::terminate()
{
	ZDEBUG("terminated!");
	exit(0);
}
Example #13
0
/* Compress `src' into `dest' using algorithm `type'.  */
static int zfile_compress(const char *src, const char *dest,
                          enum compression_type type)
{
    char *dest_backup_name;
    int retval;

    /* This shouldn't happen */
    if (type == COMPR_ARCHIVE) {
        log_error(zlog, "compress: trying to compress archive-file.");
        return -1;
    }

    /* This shouldn't happen */
    if (type == COMPR_ZIPCODE) {
        log_error(zlog, "compress: trying to compress zipcode-file.");
        return -1;
    }

    /* This shouldn't happen */
    if (type == COMPR_LYNX) {
        log_error(zlog, "compress: trying to compress lynx-file.");
        return -1;
    }

    /* This shouldn't happen */
    if (type == COMPR_TZX) {
        log_error(zlog, "compress: trying to compress tzx-file.");
        return -1;
    }

    /* Check whether `compression_type' is a known one.  */
    if (type != COMPR_GZIP && type != COMPR_BZIP) {
        log_error(zlog, "compress: unknown compression type");
        return -1;
    }

    /* If we have no write permissions for `dest', give up.  */
    if (ioutil_access(dest, IOUTIL_ACCESS_W_OK) < 0) {
        ZDEBUG(("compress: no write permissions for `%s'",
                dest));
        return -1;
    }

    if (ioutil_access(dest, IOUTIL_ACCESS_R_OK) < 0) {
        ZDEBUG(("compress: no read permissions for `%s'", dest));
        dest_backup_name = NULL;
    } else {
        /* If `dest' exists, make a backup first.  */
        dest_backup_name = archdep_make_backup_filename(dest);
        if (dest_backup_name != NULL) {
            ZDEBUG(("compress: making backup %s... ", dest_backup_name));
        }
#ifdef WIN32
        if (dest_backup_name != NULL) {
            ioutil_remove(dest_backup_name);
        }
#endif
        if (dest_backup_name != NULL
            && ioutil_rename(dest, dest_backup_name) < 0) {
            ZDEBUG(("failed."));
            log_error(zlog, "Could not make pre-compression backup.");
            return -1;
        } else {
            ZDEBUG(("OK."));
        }
    }

    switch (type) {
        case COMPR_GZIP:
            retval = compress_with_gzip(src, dest);
            break;
        case COMPR_BZIP:
            retval = compress_with_bzip(src, dest);
            break;
        default:
            retval = -1;
    }

    if (retval == -1) {
        /* Compression failed: restore original file.  */
#ifdef WIN32
        if (dest_backup_name != NULL) {
            ioutil_remove(dest);
        }
#endif
        if (dest_backup_name != NULL
            && ioutil_rename(dest_backup_name, dest) < 0) {
            log_error(zlog,
                      "Could not restore backup file after failed compression.");
        }
    } else {
        /* Compression succeeded: remove backup file.  */
        if (dest_backup_name != NULL
            && ioutil_remove(dest_backup_name) < 0) {
            log_error(zlog, "Warning: could not remove backup file.");
            /* Do not return an error anyway (no data is lost).  */
        }
    }

    if (dest_backup_name) {
        lib_free(dest_backup_name);
    }
    return retval;
}
Example #14
0
/* Compress `src' into `dest' using gzip.  */
static int compress_with_gzip(const char *src, const char *dest)
{
#ifdef HAVE_ZLIB
    FILE *fdsrc;
    gzFile fddest;
    size_t len;

    fdsrc = fopen(dest, MODE_READ);
    if (fdsrc == NULL) {
        return -1;
    }

    fddest = gzopen(src, MODE_WRITE "9");
    if (fddest == NULL) {
        fclose(fdsrc);
        return -1;
    }

    do {
        char buf[256];
        len = fread((void *)buf, 256, 1, fdsrc);
        if (len > 0) {
            gzwrite(fddest, (void *)buf, (unsigned int)len);
        }
    } while (len > 0);

    gzclose(fddest);
    fclose(fdsrc);

    archdep_file_set_gzip(dest);

    ZDEBUG(("compress with zlib: OK."));

    return 0;
#else
    static char *argv[4];
    int exit_status;
    char *mdest;

    /* `exec*()' does not want these to be constant...  */
    argv[0] = lib_stralloc("gzip");
    argv[1] = lib_stralloc("-c");
    argv[2] = lib_stralloc(src);
    argv[3] = NULL;

    mdest = lib_stralloc(dest);

    ZDEBUG(("compress_with_gzip: spawning gzip -c %s", src));
    exit_status = archdep_spawn("gzip", argv, &mdest, NULL);

    lib_free(mdest);

    lib_free(argv[0]);
    lib_free(argv[1]);
    lib_free(argv[2]);

    if (exit_status == 0) {
        ZDEBUG(("compress_with_gzip: OK."));
        return 0;
    } else {
        ZDEBUG(("compress_with_gzip: failed."));
        return -1;
    }
#endif
}
Example #15
0
/* If `name' has a correct extension, try to list its contents and search for
   the first file with a proper extension; if found, extract it.  If this
   succeeds, return the name of the temporary file; if the archive file is
   valid but `write_mode' is non-zero, return a zero-length string; in all
   the other cases, return NULL.  */
static char *try_uncompress_archive(const char *name, int write_mode,
                                    const char *program,
                                    const char *listopts,
                                    const char *extractopts,
                                    const char *extension,
                                    const char *search)
{
    char *tmp_name = NULL;
    size_t l = strlen(name), len, nameoffset;
    int found = 0;
    int exit_status;
    char *argv[8];
    FILE *fd;
    char tmp[1024];

    /* Do we have correct extension?  */
    len = strlen(extension);
    if (l <= len || strcasecmp(name + l - len, extension) != 0) {
        return NULL;
    }

    /* First run listing and search for first recognizeable extension.  */
    argv[0] = lib_stralloc(program);
    argv[1] = lib_stralloc(listopts);
    argv[2] = archdep_filename_parameter(name);
    argv[3] = NULL;

    ZDEBUG(("try_uncompress_archive: spawning `%s %s %s'",
            program, listopts, name));
    exit_status = archdep_spawn(program, argv, &tmp_name, NULL);

    lib_free(argv[0]);
    lib_free(argv[1]);
    lib_free(argv[2]);

    /* No luck?  */
    if (exit_status != 0) {
        ZDEBUG(("try_uncompress_archive: `%s %s' failed.", program, listopts));
        ioutil_remove(tmp_name);
        lib_free(tmp_name);
        return NULL;
    }

    ZDEBUG(("try_uncompress_archive: `%s %s' successful.", program, listopts));

    fd = fopen(tmp_name, MODE_READ);
    if (!fd) {
        ZDEBUG(("try_uncompress_archive: cannot read `%s %s' output.",
                program, tmp_name));
        ioutil_remove(tmp_name);
        lib_free(tmp_name);
        return NULL;
    }

    ZDEBUG(("try_uncompress_archive: searching for the first valid file."));

    /* Search for `search' first (if any) to see the offset where
       filename begins, then search for first recognizeable file.  */
    nameoffset = search ? -1 : 0;
    len = search ? strlen(search) : 0;
    while (!feof(fd) && !found) {
        if (fgets(tmp, 1024, fd) == NULL) {
            break;
        }
        l = strlen(tmp);
        while (l > 0) {
            tmp[--l] = 0;
            if (((nameoffset < 0) || (nameoffset > 1024)) && l >= len &&
                !strcasecmp(tmp + l - len, search) != 0) {
                nameoffset = l - 4;
            }
            if (nameoffset >= 0 && nameoffset <= 1024 && is_valid_extension(tmp, l, nameoffset)) {
                ZDEBUG(("try_uncompress_archive: found `%s'.",
                        tmp + nameoffset));
                found = 1;
                break;
            }
        }
    }

    fclose(fd);
    ioutil_remove(tmp_name);
    if (!found) {
        ZDEBUG(("try_uncompress_archive: no valid file found."));
        lib_free(tmp_name);
        return NULL;
    }

    /* This would be a valid ZIP file, but we cannot handle ZIP files in
       write mode.  Return a null temporary file name to report this.  */
    if (write_mode) {
        ZDEBUG(("try_uncompress_archive: cannot open file in write mode."));
        lib_free(tmp_name);
        return "";
    }

    /* And then file inside zip.  If we have a zipcode extract all of them
       to the same file. */
    argv[0] = lib_stralloc(program);
    argv[1] = lib_stralloc(extractopts);
    argv[2] = archdep_filename_parameter(name);
    if (is_zipcode_name(tmp + nameoffset)) {
        argv[3] = lib_stralloc(tmp + nameoffset);
        argv[4] = lib_stralloc(tmp + nameoffset);
        argv[5] = lib_stralloc(tmp + nameoffset);
        argv[6] = lib_stralloc(tmp + nameoffset);
        argv[7] = NULL;
        argv[3][0] = '1';
        argv[4][0] = '2';
        argv[5][0] = '3';
        argv[6][0] = '4';
    } else {
        argv[3] = archdep_quote_parameter(tmp + nameoffset);
        argv[4] = NULL;
    }

    ZDEBUG(("try_uncompress_archive: spawning `%s %s %s %s'.",
            program, extractopts, name, tmp + nameoffset));
    exit_status = archdep_spawn(program, argv, &tmp_name, NULL);

    lib_free(argv[0]);
    lib_free(argv[1]);
    lib_free(argv[2]);
    lib_free(argv[3]);
    if (is_zipcode_name(tmp + nameoffset)) {
        lib_free(argv[4]);
        lib_free(argv[5]);
        lib_free(argv[6]);
    }

    if (exit_status != 0) {
        ZDEBUG(("try_uncompress_archive: `%s %s' failed.",
                program, extractopts));
        ioutil_remove(tmp_name);
        lib_free(tmp_name);
        return NULL;
    }

    ZDEBUG(("try_uncompress_archive: `%s %s' successful.", program, tmp_name));
    return tmp_name;
}
Example #16
0
/* If `name' has a gzip-like extension, try to uncompress it into a temporary
   file using gzip or zlib if available.  If this succeeds, return the name
   of the temporary file; return NULL otherwise.  */
static char *try_uncompress_with_gzip(const char *name)
{
#ifdef HAVE_ZLIB
    FILE *fddest;
    gzFile fdsrc;
    char *tmp_name = NULL;
    int len;

    if (!archdep_file_is_gzip(name)) {
        return NULL;
    }

    fddest = archdep_mkstemp_fd(&tmp_name, MODE_WRITE);

    if (fddest == NULL) {
        return NULL;
    }

    fdsrc = gzopen(name, MODE_READ);
    if (fdsrc == NULL) {
        fclose(fddest);
        ioutil_remove(tmp_name);
        lib_free(tmp_name);
        return NULL;
    }

    do {
        char buf[256];

        len = gzread(fdsrc, (void *)buf, 256);
        if (len > 0) {
            if (fwrite((void *)buf, 1, (size_t)len, fddest) < len) {
                gzclose(fdsrc);
                fclose(fddest);
                ioutil_remove(tmp_name);
                lib_free(tmp_name);
                return NULL;
            }
        }
    } while (len > 0);

    gzclose(fdsrc);
    fclose(fddest);

    return tmp_name;
#else
    char *tmp_name = NULL;
    int exit_status;
    char *argv[4];

    if (!archdep_file_is_gzip(name)) {
        return NULL;
    }

    /* `exec*()' does not want these to be constant...  */
    argv[0] = lib_stralloc("gzip");
    argv[1] = lib_stralloc("-cd");
    argv[2] = archdep_filename_parameter(name);
    argv[3] = NULL;

    ZDEBUG(("try_uncompress_with_gzip: spawning gzip -cd %s", name));
    exit_status = archdep_spawn("gzip", argv, &tmp_name, NULL);

    lib_free(argv[0]);
    lib_free(argv[1]);
    lib_free(argv[2]);

    if (exit_status == 0) {
        ZDEBUG(("try_uncompress_with_gzip: OK"));
        return tmp_name;
    } else {
        ZDEBUG(("try_uncompress_with_gzip: failed"));
        ioutil_remove(tmp_name);
        lib_free(tmp_name);
        return NULL;
    }
#endif
}