/** @brief Read's log data for defined timerange and stores the entries into entry_list struct * @param [out] entry_list returns a filled entry list of requested log data * @param [in] filter_list a list of filters of type logfilter struct * @param [out] error_text returns a error string in case of an error execpt on READLOG_ERROR_MEMORY * @param [in] search_string a string you are searching for * Set to NULL to disable search function * @param [in] reverse this bool defines which order the log entries should return * @param [in] ts_start defines the start timestamp for log entries * @arg >=0 means unix timestamp * @param [in] ts_end defines the end timestamp for log entries * @arg >=0 means unix timestamp * @return * @retval READLOG_OK * @retval READLOG_ERROR_WARNING * @retval READLOG_ERROR_FATAL * @retval READLOG_ERROR_MEMORY * @retval READLOG_ERROR_FILTER * @author Ricardo Bartels * * This functions reads a \c log_file and and try's (if set) to filter for a search string. * This search string uses regular expressions. The reverse option defines if you want * have your log entries returned in normal or revers order. Normal order for returning * would be from the newest entry to the oldest. You can also set a time "window". This * defines if you want to exclude entries which are outside of these "window". Then only * entries will be returned which are between start and end. Very useful if user has all * entries in one log file. **/ int get_log_entries(logentry **entry_list, logfilter **filter_list, char **error_text, char *search_string, int reverse, time_t ts_start, time_t ts_end) { char *input = NULL; char *temp_buffer = NULL; char *search_regex = NULL; char log_file_name[MAX_FILENAME_LENGTH]; char ts_buffer[16]; int type = 0; int regex_i = 0, i = 0, len = 0; int file_num = 1; int file = 0; int in_range = FALSE; int return_val = READLOG_OK; int data_found = FALSE; int open_read_failed = FALSE; // this is roughly 10 years of log files on hourly rotation int num_max_log_files = 87600; short keep_entry = TRUE; time_t timestamp = 0L; time_t last_timestamp = 0L; mmapfile *thefile = NULL; logentry *temp_entry = NULL; logentry *last_entry = NULL; regex_t preg; logfilter *temp_filter; DIR *dirp; struct dirent *dptr; struct file_data files[num_max_log_files + 1]; #ifdef HAVE_ZLIB_H gzFile gzfile = NULL; char gz_buffer[MAX_COMMAND_BUFFER * 2]; #else read_gzip_logs = FALSE; #endif /* empty error_text */ if (*error_text != NULL) my_free(*error_text); /* bail out if one timestamp is negative */ if (ts_start < 0 || ts_end < 0) { *error_text = strdup("start or end timestamp are invalid. Check submited date information"); return READLOG_ERROR_FATAL; } /* check if search_string is set */ if (search_string != NULL) { /* allocate for 3 extra chars, ^, $ and \0 */ search_regex = malloc(sizeof(char) * (strlen(search_string) * 2 + 3)); len = strlen(search_string); for (i = 0; i < len; i++, regex_i++) { if (search_string[i] == '*') { search_regex[regex_i++] = '.'; search_regex[regex_i] = '*'; } else search_regex[regex_i] = search_string[i]; } search_regex[regex_i] = '\0'; /* check and compile regex, return error on failure */ if (regcomp(&preg, search_regex, REG_ICASE | REG_NOSUB) != 0) { regfree(&preg); my_free(search_regex); *error_text = strdup("It seems like that reagular expressions don't like what you searched for. Please change your search string."); return READLOG_ERROR_FATAL; } my_free(search_regex); } /* initialize file data array */ for (i=0;i<=num_max_log_files;i++) { files[i].file_name = NULL; } /* try to open log_archive_path, return if it fails */ if ((dirp=opendir(log_archive_path)) == NULL){ if (search_string != NULL) regfree(&preg); asprintf(&temp_buffer, "Unable to open \"log_archive_path\" -> \"%s\"!!!", log_archive_path); *error_text = strdup(temp_buffer); my_free(temp_buffer); return READLOG_ERROR_FATAL; } else { /* read every dir entry */ while ((dptr=readdir(dirp)) != NULL) { /* filter dir for icinga / nagios log files */ if ((strncmp("icinga-",dptr->d_name,7) == 0 || strncmp("nagios-",dptr->d_name,7) == 0 ) && ((strstr(dptr->d_name, ".log") && strlen(dptr->d_name) == 24 ) || (read_gzip_logs == TRUE && strstr(dptr->d_name, ".log.gz") && strlen(dptr->d_name) == 27 ))) { if ( file_num <= num_max_log_files ) { files[file_num++].file_name = strdup(dptr->d_name); } else { asprintf(&temp_buffer, "The amount of log files in the archive directory exceeds the maximum of \"%d\" readable log files! Consider deleting old log files.", num_max_log_files); *error_text = strdup(temp_buffer); my_free(temp_buffer); return_val = READLOG_ERROR_WARNING; break; } } } closedir(dirp); } /* sort log files, newest first */ qsort((void *)files, file_num, sizeof(struct file_data), sort_icinga_logfiles_by_name); /* define which log files to use */ for (i=0; i< file_num; i++) { /* first log file is always the current log file */ if (i == 0) { strncpy(log_file_name, log_file, sizeof(log_file_name) -1); log_file_name[sizeof(log_file_name)-1] = '\x0'; /* return full path of logfile and store first timestamp of last file */ } else { snprintf(log_file_name, sizeof(log_file_name) -1, "%s%s",log_archive_path, files[i].file_name); log_file_name[sizeof(log_file_name)-1] = '\x0'; last_timestamp = timestamp; } /* free file entry and set to NULL. if valid file is found, entry gets refilled */ my_free(files[i].file_name); /* we found data and we are out of range again, file must be older then ts_start. stop checking files */ if (data_found == TRUE && in_range == FALSE) continue; /* try to open log file, or throw error and try next log file */ open_read_failed = FALSE; if (read_gzip_logs == TRUE && strstr(log_file_name, ".log.gz")) { #ifdef HAVE_ZLIB_H if ((gzfile = gzopen(log_file_name, "r")) == NULL) #endif open_read_failed = TRUE; } else { if((file=open(log_file_name, O_RDONLY)) < -1) open_read_failed = TRUE; } if(open_read_failed == TRUE) { if (*error_text == NULL) { asprintf(&temp_buffer, "Unable to open log file \"%s\" !!!", log_file_name); *error_text = strdup(temp_buffer); my_free(temp_buffer); } return_val = READLOG_ERROR_WARNING; continue; } /* read first 16 bytes to get first timestamp, or throw error if data is not 16 bytes log (empty file) */ open_read_failed = FALSE; if (read_gzip_logs == TRUE && strstr(log_file_name, ".log.gz")) { #ifdef HAVE_ZLIB_H if(gzread(gzfile,ts_buffer,16) != 16) #endif open_read_failed = TRUE; } else { if(read(file,ts_buffer,16) != 16) open_read_failed = TRUE; } if(open_read_failed == TRUE) { if (*error_text == NULL) { asprintf(&temp_buffer, "Log file \"%s\" invalid! No timestamp found within first 16 bytes!", log_file_name); *error_text = strdup(temp_buffer); my_free(temp_buffer); } return_val = READLOG_ERROR_WARNING; #ifdef HAVE_ZLIB_H if (read_gzip_logs == TRUE && strstr(log_file_name, ".log.gz")) gzclose(gzfile); else #endif close(file); continue; } #ifdef HAVE_ZLIB_H if (read_gzip_logs == TRUE && strstr(log_file_name, ".log.gz")) gzclose(gzfile); else #endif close(file); /* get first timestamp */ temp_buffer = strtok(ts_buffer, "]"); timestamp = (temp_buffer == NULL) ? 0L : strtoul(temp_buffer + 1, NULL, 10); /* if first (oldest) timestamp in file is newer then ts_end, skip file */ if (timestamp > ts_end) continue; in_range = TRUE; /* the priviouse file holds range for ts_start */ if (last_timestamp != 0L && last_timestamp < ts_start) in_range = FALSE; /* keep file if in range */ if(in_range == TRUE) { files[i].file_name = strdup(log_file_name); data_found = TRUE; } } /* read all log files we found earlier in reverse order, starting with the oldest */ for (i=file_num; i >= 0; i--) { /* if file name is empty try next file */ if (files[i].file_name == NULL) continue; /* try to open log file */ if (read_gzip_logs == TRUE && strstr(files[i].file_name, ".log.gz")) { #ifdef HAVE_ZLIB_H if ((gzfile = gzopen(files[i].file_name, "r")) == NULL) #endif continue; } else { if ((thefile = mmap_fopen(files[i].file_name)) == NULL) continue; } while (1) { /* free memory */ my_free(input); if (read_gzip_logs == TRUE && strstr(files[i].file_name, ".log.gz")) { #ifdef HAVE_ZLIB_H if ((gzgets(gzfile, gz_buffer, MAX_COMMAND_BUFFER * 2 + 1)) == NULL) #endif break; } else { if ((input = mmap_fgets(thefile)) == NULL) break; } #ifdef HAVE_ZLIB_H if (read_gzip_logs == TRUE && strstr(files[i].file_name, ".log.gz")) { gz_buffer[MAX_COMMAND_BUFFER * 2 - 1] = '\0'; input = strdup(gz_buffer); } #endif strip(input); if ((int)strlen(input) == 0) continue; /* get timestamp */ temp_buffer = strtok(input, "]"); if (temp_buffer == NULL) continue; timestamp = strtoul(temp_buffer + 1, NULL, 10); /* skip line if out of range */ if ((ts_end >= 0 && timestamp > ts_end) || (ts_start >= 0 && timestamp < ts_start)) continue; /* get log entry text */ temp_buffer = strtok(NULL, "\n"); if (temp_buffer == NULL) continue; /* if we search for something, check if it entry matches search_string */ if (search_string != NULL) { if (regexec(&preg, temp_buffer, 0, NULL, 0) == REG_NOMATCH) continue; } /* categorize log entry */ if (strstr(temp_buffer, " starting...")) type = LOGENTRY_STARTUP; else if (strstr(temp_buffer, " shutting down...")) type = LOGENTRY_SHUTDOWN; else if (strstr(temp_buffer, "Bailing out")) type = LOGENTRY_BAILOUT; else if (strstr(temp_buffer, " restarting...")) type = LOGENTRY_RESTART; else if (strstr(temp_buffer, "HOST ALERT:") && strstr(temp_buffer, ";DOWN;")) type = LOGENTRY_HOST_DOWN; else if (strstr(temp_buffer, "HOST ALERT:") && strstr(temp_buffer, ";UNREACHABLE;")) type = LOGENTRY_HOST_UNREACHABLE; else if (strstr(temp_buffer, "HOST ALERT:") && strstr(temp_buffer, ";RECOVERY;")) type = LOGENTRY_HOST_RECOVERY; else if (strstr(temp_buffer, "HOST ALERT:") && strstr(temp_buffer, ";UP;")) type = LOGENTRY_HOST_UP; else if (strstr(temp_buffer, "HOST NOTIFICATION:")) type = LOGENTRY_HOST_NOTIFICATION; else if (strstr(temp_buffer, "SERVICE ALERT:") && strstr(temp_buffer, ";CRITICAL;")) type = LOGENTRY_SERVICE_CRITICAL; else if (strstr(temp_buffer, "SERVICE ALERT:") && strstr(temp_buffer, ";WARNING;")) type = LOGENTRY_SERVICE_WARNING; else if (strstr(temp_buffer, "SERVICE ALERT:") && strstr(temp_buffer, ";UNKNOWN;")) type = LOGENTRY_SERVICE_UNKNOWN; else if (strstr(temp_buffer, "SERVICE ALERT:") && strstr(temp_buffer, ";RECOVERY;")) type = LOGENTRY_SERVICE_RECOVERY; else if (strstr(temp_buffer, "SERVICE ALERT:") && strstr(temp_buffer, ";OK;")) type = LOGENTRY_SERVICE_OK; else if (strstr(temp_buffer, "SERVICE NOTIFICATION:")) type = LOGENTRY_SERVICE_NOTIFICATION; else if (strstr(temp_buffer, "SERVICE EVENT HANDLER:")) type = LOGENTRY_SERVICE_EVENT_HANDLER; else if (strstr(temp_buffer, "HOST EVENT HANDLER:")) type = LOGENTRY_HOST_EVENT_HANDLER; else if (strstr(temp_buffer, "EXTERNAL COMMAND:")) type = LOGENTRY_EXTERNAL_COMMAND; else if (strstr(temp_buffer, "PASSIVE SERVICE CHECK:")) type = LOGENTRY_PASSIVE_SERVICE_CHECK; else if (strstr(temp_buffer, "PASSIVE HOST CHECK:")) type = LOGENTRY_PASSIVE_HOST_CHECK; else if (strstr(temp_buffer, "LOG ROTATION:")) type = LOGENTRY_LOG_ROTATION; else if (strstr(temp_buffer, "active mode...")) type = LOGENTRY_ACTIVE_MODE; else if (strstr(temp_buffer, "standby mode...")) type = LOGENTRY_STANDBY_MODE; else if (strstr(temp_buffer, "SERVICE FLAPPING ALERT:") && strstr(temp_buffer, ";STARTED;")) type = LOGENTRY_SERVICE_FLAPPING_STARTED; else if (strstr(temp_buffer, "SERVICE FLAPPING ALERT:") && strstr(temp_buffer, ";STOPPED;")) type = LOGENTRY_SERVICE_FLAPPING_STOPPED; else if (strstr(temp_buffer, "SERVICE FLAPPING ALERT:") && strstr(temp_buffer, ";DISABLED;")) type = LOGENTRY_SERVICE_FLAPPING_DISABLED; else if (strstr(temp_buffer, "HOST FLAPPING ALERT:") && strstr(temp_buffer, ";STARTED;")) type = LOGENTRY_HOST_FLAPPING_STARTED; else if (strstr(temp_buffer, "HOST FLAPPING ALERT:") && strstr(temp_buffer, ";STOPPED;")) type = LOGENTRY_HOST_FLAPPING_STOPPED; else if (strstr(temp_buffer, "HOST FLAPPING ALERT:") && strstr(temp_buffer, ";DISABLED;")) type = LOGENTRY_HOST_FLAPPING_DISABLED; else if (strstr(temp_buffer, "SERVICE DOWNTIME ALERT:") && strstr(temp_buffer, ";STARTED;")) type = LOGENTRY_SERVICE_DOWNTIME_STARTED; else if (strstr(temp_buffer, "SERVICE DOWNTIME ALERT:") && strstr(temp_buffer, ";STOPPED;")) type = LOGENTRY_SERVICE_DOWNTIME_STOPPED; else if (strstr(temp_buffer, "SERVICE DOWNTIME ALERT:") && strstr(temp_buffer, ";CANCELLED;")) type = LOGENTRY_SERVICE_DOWNTIME_CANCELLED; else if (strstr(temp_buffer, "HOST DOWNTIME ALERT:") && strstr(temp_buffer, ";STARTED;")) type = LOGENTRY_HOST_DOWNTIME_STARTED; else if (strstr(temp_buffer, "HOST DOWNTIME ALERT:") && strstr(temp_buffer, ";STOPPED;")) type = LOGENTRY_HOST_DOWNTIME_STOPPED; else if (strstr(temp_buffer, "HOST DOWNTIME ALERT:") && strstr(temp_buffer, ";CANCELLED;")) type = LOGENTRY_HOST_DOWNTIME_CANCELLED; else if (strstr(temp_buffer, "INITIAL SERVICE STATE:")) type = LOGENTRY_SERVICE_INITIAL_STATE; else if (strstr(temp_buffer, "INITIAL HOST STATE:")) type = LOGENTRY_HOST_INITIAL_STATE; else if (strstr(temp_buffer, "CURRENT SERVICE STATE:")) type = LOGENTRY_SERVICE_CURRENT_STATE; else if (strstr(temp_buffer, "CURRENT HOST STATE:")) type = LOGENTRY_HOST_CURRENT_STATE; else if (strstr(temp_buffer, "error executing command")) type = LOGENTRY_ERROR_COMMAND_EXECUTION; else if (strstr(temp_buffer, "idomod:")) type = LOGENTRY_IDOMOD; else if (strstr(temp_buffer, "npcdmod:")) type = LOGENTRY_NPCDMOD; else if (strstr(temp_buffer, "Auto-save of")) type = LOGENTRY_AUTOSAVE; else if (strstr(temp_buffer, "Warning:")) type = LOGENTRY_SYSTEM_WARNING; else type = LOGENTRY_UNDEFINED; /* apply filters */ if (*filter_list != NULL) { keep_entry = FALSE; for (temp_filter = *filter_list; temp_filter != NULL; temp_filter = temp_filter->next) { if (temp_filter->include != 0) { if (temp_filter->include == type) { keep_entry = TRUE; break; } } else if (temp_filter->exclude != 0) { if (temp_filter->exclude == type) { keep_entry = FALSE; break; } else keep_entry = TRUE; } } if (keep_entry == FALSE) continue; } /* initialzie */ /* allocate memory for a new log entry */ temp_entry = (logentry *)malloc(sizeof(logentry)); if (temp_entry == NULL) { mmap_fclose(thefile); return READLOG_ERROR_MEMORY; } temp_entry->timestamp = 0L; temp_entry->type = 0; temp_entry->entry_text = NULL; temp_entry->next = NULL; temp_entry->timestamp = timestamp; temp_entry->type = type; temp_entry->entry_text = strdup(temp_buffer); if (reverse == TRUE) { if (*entry_list == NULL) { *entry_list = temp_entry; last_entry = *entry_list; } else { last_entry->next = temp_entry; last_entry = temp_entry; } } else { temp_entry->next = *entry_list; *entry_list = temp_entry; } } #ifdef HAVE_ZLIB_H if (read_gzip_logs == TRUE && strstr(files[i].file_name, ".log.gz")) gzclose(gzfile); else #endif mmap_fclose(thefile); } for (i=0; i< file_num;i++) my_free(files[i].file_name); if (search_string != NULL) regfree(&preg); return return_val; }
//Reads up to len bytes of data from the input stream into an array of bytes. int GZInputStream::read(unsigned char* b, int off, int len) { return gzread(m_file,b+off,len); }
static int cli_tgzload(int fd, struct cl_engine *engine, unsigned int *signo, unsigned int options, struct cli_dbio *dbio, struct cli_dbinfo *dbinfo) { char osize[13], name[101]; char block[TAR_BLOCKSIZE]; int nread, fdd, ret; unsigned int type, size, pad, compr = 1; off_t off; struct cli_dbinfo *db; unsigned char hash[32]; cli_dbgmsg("in cli_tgzload()\n"); if(lseek(fd, 512, SEEK_SET) < 0) { return CL_ESEEK; } if(cli_readn(fd, block, 7) != 7) return CL_EFORMAT; /* truncated file? */ if(!strncmp(block, "COPYING", 7)) compr = 0; if(lseek(fd, 512, SEEK_SET) < 0) { return CL_ESEEK; } if((fdd = dup(fd)) == -1) { cli_errmsg("cli_tgzload: Can't duplicate descriptor %d\n", fd); return CL_EDUP; } if(compr) { if((dbio->gzs = gzdopen(fdd, "rb")) == NULL) { cli_errmsg("cli_tgzload: Can't gzdopen() descriptor %d, errno = %d\n", fdd, errno); if (fdd > -1) close(fdd); return CL_EOPEN; } dbio->fs = NULL; } else { if((dbio->fs = fdopen(fdd, "rb")) == NULL) { cli_errmsg("cli_tgzload: Can't fdopen() descriptor %d, errno = %d\n", fdd, errno); if (fdd > -1) close(fdd); return CL_EOPEN; } dbio->gzs = NULL; } dbio->bufsize = CLI_DEFAULT_DBIO_BUFSIZE; dbio->buf = cli_malloc(dbio->bufsize); if(!dbio->buf) { cli_errmsg("cli_tgzload: Can't allocate memory for dbio->buf\n"); cli_tgzload_cleanup(compr, dbio, fdd); return CL_EMALFDB; } dbio->bufpt = NULL; dbio->usebuf = 1; dbio->readpt = dbio->buf; while(1) { if(compr) nread = gzread(dbio->gzs, block, TAR_BLOCKSIZE); else nread = fread(block, 1, TAR_BLOCKSIZE, dbio->fs); if(!nread) break; if(nread != TAR_BLOCKSIZE) { cli_errmsg("cli_tgzload: Incomplete block read\n"); cli_tgzload_cleanup(compr, dbio, fdd); return CL_EMALFDB; } if(block[0] == '\0') /* We're done */ break; strncpy(name, block, 100); name[100] = '\0'; if(strchr(name, '/')) { cli_errmsg("cli_tgzload: Slash separators are not allowed in CVD\n"); cli_tgzload_cleanup(compr, dbio, fdd); return CL_EMALFDB; } type = block[156]; switch(type) { case '0': case '\0': break; case '5': cli_errmsg("cli_tgzload: Directories are not supported in CVD\n"); cli_tgzload_cleanup(compr, dbio, fdd); return CL_EMALFDB; default: cli_errmsg("cli_tgzload: Unknown type flag '%c'\n", type); cli_tgzload_cleanup(compr, dbio, fdd); return CL_EMALFDB; } strncpy(osize, block + 124, 12); osize[12] = '\0'; if((sscanf(osize, "%o", &size)) == 0) { cli_errmsg("cli_tgzload: Invalid size in header\n"); cli_tgzload_cleanup(compr, dbio, fdd); return CL_EMALFDB; } dbio->size = size; dbio->readsize = dbio->size < dbio->bufsize ? dbio->size : dbio->bufsize - 1; dbio->bufpt = NULL; dbio->readpt = dbio->buf; if (!(dbio->hashctx)) { dbio->hashctx = cl_hash_init("sha256"); if (!(dbio->hashctx)) { cli_tgzload_cleanup(compr, dbio, fdd); return CL_EMALFDB; } } dbio->bread = 0; /* cli_dbgmsg("cli_tgzload: Loading %s, size: %u\n", name, size); */ if(compr) off = (off_t) gzseek(dbio->gzs, 0, SEEK_CUR); else off = ftell(dbio->fs); if((!dbinfo && cli_strbcasestr(name, ".info")) || (dbinfo && (CLI_DBEXT(name) || cli_strbcasestr(name, ".ign") || cli_strbcasestr(name, ".ign2")))) { ret = cli_load(name, engine, signo, options, dbio); if(ret) { cli_errmsg("cli_tgzload: Can't load %s\n", name); cli_tgzload_cleanup(compr, dbio, fdd); return CL_EMALFDB; } if(!dbinfo) { cli_tgzload_cleanup(compr, dbio, fdd); return CL_SUCCESS; } else { db = dbinfo; while(db && strcmp(db->name, name)) db = db->next; if(!db) { cli_errmsg("cli_tgzload: File %s not found in .info\n", name); cli_tgzload_cleanup(compr, dbio, fdd); return CL_EMALFDB; } if(dbio->bread) { if(db->size != dbio->bread) { cli_errmsg("cli_tgzload: File %s not correctly loaded\n", name); cli_tgzload_cleanup(compr, dbio, fdd); return CL_EMALFDB; } cl_finish_hash(dbio->hashctx, hash); dbio->hashctx = cl_hash_init("sha256"); if (!(dbio->hashctx)) { cli_tgzload_cleanup(compr, dbio, fdd); return CL_EMALFDB; } if(memcmp(db->hash, hash, 32)) { cli_errmsg("cli_tgzload: Invalid checksum for file %s\n", name); cli_tgzload_cleanup(compr, dbio, fdd); return CL_EMALFDB; } } } } pad = size % TAR_BLOCKSIZE ? (TAR_BLOCKSIZE - (size % TAR_BLOCKSIZE)) : 0; if(compr) { if(off == gzseek(dbio->gzs, 0, SEEK_CUR)) gzseek(dbio->gzs, size + pad, SEEK_CUR); else if(pad) gzseek(dbio->gzs, pad, SEEK_CUR); } else { if(off == ftell(dbio->fs)) fseek(dbio->fs, size + pad, SEEK_CUR); else if(pad) fseek(dbio->fs, pad, SEEK_CUR); } } cli_tgzload_cleanup(compr, dbio, fdd); return CL_SUCCESS; }
int ADZ_libLoad_DiskFile(HXCFE_IMGLDR * imgldr_ctx,HXCFE_FLOPPY * floppydisk,char * imgfile,void * parameters) { unsigned int filesize; int32_t i,j; unsigned int file_offset; int32_t sectorsize; int32_t gap3len,skew,trackformat,interleave; unsigned char* flatimg; gzFile file; int err; HXCFE_CYLINDER* currentcylinder; imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"ADZ_libLoad_DiskFile %s",imgfile); file = gzopen(imgfile, "rb"); if (!file) { imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"gzopen: Error while reading the file!"); return -1; } i=0; filesize=0; flatimg=(unsigned char*)malloc(UNPACKBUFFER); do { err=gzread(file, flatimg+filesize,UNPACKBUFFER ); filesize=filesize+err; flatimg=(unsigned char *)realloc(flatimg,filesize+UNPACKBUFFER); i++; }while(err>0); gzclose(file); if(!flatimg) { imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"Unpack error!"); return HXCFE_BADFILE; } if(flatimg) { sectorsize = 512; interleave = 1; gap3len = 0; skew = 0; file_offset = 0; trackformat = AMIGAFORMAT_DD; floppydisk->floppySectorPerTrack=11; floppydisk->floppyNumberOfSide=2; floppydisk->floppyNumberOfTrack=(filesize/(512*2*floppydisk->floppySectorPerTrack)); floppydisk->floppyBitRate=DEFAULT_AMIGA_BITRATE; floppydisk->floppyiftype=AMIGA_DD_FLOPPYMODE; floppydisk->tracks=(HXCFE_CYLINDER**)malloc(sizeof(HXCFE_CYLINDER*)*(floppydisk->floppyNumberOfTrack+4)); for(j=0;j<floppydisk->floppyNumberOfTrack;j++) { floppydisk->tracks[j]=allocCylinderEntry(DEFAULT_AMIGA_RPM,floppydisk->floppyNumberOfSide); currentcylinder=floppydisk->tracks[j]; for(i=0;i<floppydisk->floppyNumberOfSide;i++) { hxcfe_imgCallProgressCallback(imgldr_ctx,(j<<1) + (i&1),floppydisk->floppyNumberOfTrack*2 ); file_offset=(sectorsize*(j*floppydisk->floppySectorPerTrack*floppydisk->floppyNumberOfSide))+ (sectorsize*(floppydisk->floppySectorPerTrack)*i); currentcylinder->sides[i]=tg_generateTrack(&flatimg[file_offset],sectorsize,floppydisk->floppySectorPerTrack,(unsigned char)j,(unsigned char)i,0,interleave,(unsigned char)(((j<<1)|(i&1))*skew),floppydisk->floppyBitRate,currentcylinder->floppyRPM,trackformat,gap3len,0,2500,-11150); } } // Add 4 empty tracks for(j=floppydisk->floppyNumberOfTrack;j<(floppydisk->floppyNumberOfTrack + 4);j++) { floppydisk->tracks[j]=allocCylinderEntry(DEFAULT_AMIGA_RPM,floppydisk->floppyNumberOfSide); currentcylinder=floppydisk->tracks[j]; for(i=0;i<floppydisk->floppyNumberOfSide;i++) { currentcylinder->sides[i]=tg_generateTrack(&flatimg[file_offset],sectorsize,0,(unsigned char)j,(unsigned char)i,0,interleave,(unsigned char)(((j<<1)|(i&1))*skew),floppydisk->floppyBitRate,currentcylinder->floppyRPM,trackformat,gap3len,0,2500,-11150); } } floppydisk->floppyNumberOfTrack += 4; imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_1,"ADZ Loader : tracks file successfully loaded and encoded!"); return 0; } return HXCFE_BADFILE; }
int64 ZipFile::readImpl(char *buffer, int64 length) { ASSERT(m_gzFile); return gzread(m_gzFile, buffer, length); }
static INT __untargzFile (CPCHAR pcTargzFile, CPCHAR pcDestPath) { INT iFdTar; gzFile gzTar; char *pcBuf; ssize_t sstN; char cFname[100]; char cLinkname[100]; int iSum; int iHdrChksum; int iRetVal; unsigned long ulI; unsigned long ulNblocks; unsigned long ulSize; unsigned char ucLinkflag; ULONG ulTotalFile = 0ul; ULONG ulTotalDir = 0ul; iFdTar = open(pcTargzFile, O_RDONLY); if (iFdTar < 0) { fprintf(stderr, "can not open : %s : %s\n", pcTargzFile, lib_strerror(errno)); return (PX_ERROR); } pcBuf = (char *)__SHEAP_ALLOC(512); if (pcBuf == LW_NULL) { close(iFdTar); fprintf(stderr, "system low memory.\n"); return (PX_ERROR); } gzTar = gzdopen(iFdTar, "rb"); if (!gzTar) { __SHEAP_FREE(pcBuf); close(iFdTar); fprintf(stderr, "zlib can not open : %s\n", pcTargzFile); return (PX_ERROR); } iRetVal = ERROR_NONE; while (1) { char cOutFile[PATH_MAX + 1]; mode_t mode; if ((sstN = gzread(gzTar, pcBuf, 512)) != 512) { break; } if (lib_strncmp(&pcBuf[257], TMAGIC, 5)) { break; } lib_strlcpy(cFname, pcBuf, 100); ucLinkflag = pcBuf[156]; ulSize = __untarOctal2Long(&pcBuf[124], 12); iHdrChksum = (int)__untarOctal2Long(&pcBuf[148], 8); iSum = __untarHeaderChksum(pcBuf); if (iSum != iHdrChksum) { fprintf(stderr, "tar file : %s chksum error.\n", pcTargzFile); iRetVal = PX_ERROR; break; } mode = (int)__untarOctal2Long(&pcBuf[100], 8); if (pcDestPath) { if (lib_strcmp(pcDestPath, PX_STR_ROOT) == 0) { if (cFname[0] == PX_ROOT) { lib_strlcpy(cOutFile, cFname, PATH_MAX + 1); } else { snprintf(cOutFile, PATH_MAX + 1, "%s%s", pcDestPath, cFname); } } else { snprintf(cOutFile, PATH_MAX + 1, "%s/%s", pcDestPath, cFname); } } else { lib_strlcpy(cOutFile, cFname, PATH_MAX + 1); } if (ucLinkflag == SYMTYPE) { printf("unpackage %s <LNK> ...\n", cOutFile); lib_strlcpy(cLinkname, &pcBuf[157], 100); symlink(cLinkname, cOutFile); ulTotalFile++; } else if (ucLinkflag == REGTYPE) { INT iFdOut; printf("unpackage %s size : %ld ...\n", cOutFile, ulSize); ulNblocks = (((ulSize) + 511) & ~511) / 512; if ((iFdOut = creat(cOutFile, mode)) < 0) { fprintf(stderr, "can not create : %s\n", cOutFile); gzseek(gzTar, (ulNblocks * 512), SEEK_CUR); } else { for (ulI = 0; ulI < ulNblocks; ulI++) { sstN = gzread(gzTar, pcBuf, 512); sstN = min(sstN, ulSize - (ulI * 512)); write(iFdOut, pcBuf, (size_t)sstN); } close(iFdOut); ulTotalFile++; } } else if (ucLinkflag == DIRTYPE) { printf("unpackage %s <DIR> ...\n", cOutFile); mkdir(cOutFile, mode); ulTotalDir++; } } __SHEAP_FREE(pcBuf); gzclose(gzTar); printf("unpackage total %lu files %lu directory.\n", ulTotalFile, ulTotalDir); return (iRetVal); }
/** If all files produced by dia were good XML files, we wouldn't have to do * this little gymnastic. Alas, during the libxml1 days, we were outputting * files with no encoding specification (which means UTF-8 if we're in an * asciish encoding) and strings encoded in local charset (so, we wrote * broken files). * * The following logic finds if we have a broken file, and attempts to fix * it if it's possible. If the file is correct or is unrecognisable, we pass * it untouched to libxml2. * @param filename The name of the file to check. * @param default_enc The default encoding to use if none is given. * @return The filename given if it seems ok, or the name of a new file * with fixed contents, or NULL if we couldn't read the file. The * caller should free this string and unlink the file if it is not * the same as `filename'. * @bug The many gzclose-g_free-return sequences should be refactored into * an "exception handle" (goto+label). At least for people who think goto is * better than this. I dont. --hb */ static const gchar * xml_file_check_encoding(const gchar *filename, const gchar *default_enc) { int fd = g_open (filename, O_RDONLY, 0); gzFile zf = gzdopen(fd,"rb"); gchar *buf; gchar *p,*pmax; int len; gchar *tmp,*res; int uf; gboolean well_formed_utf8; static char magic_xml[] = {0x3c,0x3f,0x78,0x6d,0x6c,0x00}; /* "<?xml" in ASCII */ if (!zf) { dia_log_message("%s can not be opened for encoding check (%s)", filename, fd > 0 ? "gzdopen" : "g_open"); /* XXX perhaps we can just chicken out to libxml ? -- CC */ return filename; } p = buf = g_malloc0(BUFLEN); len = gzread(zf,buf,BUFLEN); pmax = p + len; /* first, we expect the magic <?xml string */ if ((0 != strncmp(p,magic_xml,5)) || (len < 5)) { gzclose(zf); g_free(buf); return filename; /* let libxml figure out what this is. */ } /* now, we're sure we have some asciish XML file. */ p += 5; while (((*p == 0x20)||(*p == 0x09)||(*p == 0x0d)||(*p == 0x0a)) && (p<pmax)) p++; if (p>=pmax) { /* whoops ? */ gzclose(zf); g_free(buf); return filename; } if (0 != strncmp(p,"version=\"",9)) { gzclose(zf); /* chicken out. */ g_free(buf); return filename; } p += 9; /* The header is rather well formed. */ if (p>=pmax) { /* whoops ? */ gzclose(zf); g_free(buf); return filename; } while ((*p != '"') && (p < pmax)) p++; p++; while (((*p == 0x20)||(*p == 0x09)||(*p == 0x0d)||(*p == 0x0a)) && (p<pmax)) p++; if (p>=pmax) { /* whoops ? */ gzclose(zf); g_free(buf); return filename; } if (0 == strncmp(p,"encoding=\"",10)) { gzclose(zf); /* this file has an encoding string. Good. */ g_free(buf); return filename; } /* now let's read the whole file, to see if there are offending bits. * We can call it well formed UTF-8 if the highest isn't used */ well_formed_utf8 = TRUE; do { int i; for (i = 0; i < len; i++) if (buf[i] & 0x80 || buf[i] == '&') well_formed_utf8 = FALSE; len = gzread(zf,buf,BUFLEN); } while (len > 0 && well_formed_utf8); if (well_formed_utf8) { gzclose(zf); /* this file is utf-8 compatible */ g_free(buf); return filename; } else { gzclose(zf); /* poor man's fseek */ fd = g_open (filename, O_RDONLY, 0); zf = gzdopen(fd,"rb"); len = gzread(zf,buf,BUFLEN); } if (0 != strcmp(default_enc,"UTF-8")) { message_warning(_("The file %s has no encoding specification;\n" "assuming it is encoded in %s"), dia_message_filename(filename), default_enc); } else { gzclose(zf); /* we apply the standard here. */ g_free(buf); return filename; } tmp = getenv("TMP"); if (!tmp) tmp = getenv("TEMP"); if (!tmp) tmp = "/tmp"; res = g_strconcat(tmp,G_DIR_SEPARATOR_S,"dia-xml-fix-encodingXXXXXX",NULL); uf = g_mkstemp(res); write(uf,buf,p-buf); write(uf," encoding=\"",11); write(uf,default_enc,strlen(default_enc)); write(uf,"\" ",2); write(uf,p,pmax - p); while (1) { len = gzread(zf,buf,BUFLEN); if (len <= 0) break; write(uf,buf,len); } gzclose(zf); close(uf); g_free(buf); return res; /* caller frees the name and unlinks the file. */ }
int get_shsh_blobs(struct idevicerestore_client_t* client, uint64_t ecid, unsigned char* nonce, int nonce_size, plist_t build_identity, plist_t* tss) { plist_t request = NULL; plist_t response = NULL; *tss = NULL; if ((client->build_major <= 8) || (client->flags & FLAG_CUSTOM)) { error("checking for local shsh\n"); /* first check for local copy */ char zfn[1024]; if (client->version) { if (client->cache_dir) { sprintf(zfn, "%s/shsh/" FMT_qu "-%s-%s.shsh", client->cache_dir, (long long int)client->ecid, client->device->product_type, client->version); } else { sprintf(zfn, "shsh/" FMT_qu "-%s-%s.shsh", (long long int)client->ecid, client->device->product_type, client->version); } struct stat fst; if (stat(zfn, &fst) == 0) { gzFile zf = gzopen(zfn, "rb"); if (zf) { int blen = 0; int readsize = 16384; int bufsize = readsize; char* bin = (char*)malloc(bufsize); char* p = bin; do { int bytes_read = gzread(zf, p, readsize); if (bytes_read < 0) { fprintf(stderr, "Error reading gz compressed data\n"); exit(EXIT_FAILURE); } blen += bytes_read; if (bytes_read < readsize) { if (gzeof(zf)) { bufsize += bytes_read; break; } } bufsize += readsize; bin = realloc(bin, bufsize); p = bin + blen; } while (!gzeof(zf)); gzclose(zf); if (blen > 0) { if (memcmp(bin, "bplist00", 8) == 0) { plist_from_bin(bin, blen, tss); } else { plist_from_xml(bin, blen, tss); } } free(bin); } } else { error("no local file %s\n", zfn); } } else { error("No version found?!\n"); } } if (*tss) { info("Using cached SHSH\n"); return 0; } else { info("Trying to fetch new SHSH blob\n"); } request = tss_create_request(build_identity, ecid, nonce, nonce_size); if (request == NULL) { error("ERROR: Unable to create TSS request\n"); return -1; } response = tss_send_request(request, client->tss_url); if (response == NULL) { info("ERROR: Unable to send TSS request\n"); plist_free(request); return -1; } info("Received SHSH blobs\n"); plist_free(request); *tss = response; return 0; }
int try_load_map(char *filename) { gzFile gzfile = gzopen(filename, "rb"); if(!gzfile) return -1; // do something uint16_t fork_id, format_id; if(gzread(gzfile, &fork_id, 2) != 2) goto error; if(gzread(gzfile, &format_id, 2) != 2) goto error; if(fork_id != EMERGENCE_FORKID || format_id != EMERGENCE_FORMATID) goto error; // do something else clear_map(); string_clear(map_filename); string_cat_text(map_filename, filename); set_map_path(); if(!gzread_nodes(gzfile)) { printf("a\n"); goto error; } if(!gzread_conns(gzfile)) { printf("b\n"); goto error; } if(!gzread_curves(gzfile)) { printf("ca\n"); goto error; } if(!gzread_points(gzfile)) { printf("d\n"); goto error; } if(!gzread_fills(gzfile)) { printf("e\n"); goto error; } if(!gzread_lines(gzfile)) { printf("f\n"); goto error; } if(!gzread_objects(gzfile)) { printf("g\n"); goto error; } gzclose(gzfile); invalidate_bsp_tree(); map_active = 1; return 0; error: gzclose(gzfile); clear_map(); map_active = 0; return -2; }
/* read .bobj.gz file into a fluidsimDerivedMesh struct */ static DerivedMesh *fluidsim_read_obj(const char *filename) { int wri = 0,i; int gotBytes; gzFile gzf; int numverts = 0, numfaces = 0; DerivedMesh *dm = NULL; MFace *mf; MVert *mv; short *normals, *no_s; float no[3]; // ------------------------------------------------ // get numverts + numfaces first // ------------------------------------------------ gzf = gzopen(filename, "rb"); if (!gzf) { return NULL; } // read numverts gotBytes = gzread(gzf, &wri, sizeof(wri)); numverts = wri; // skip verts gotBytes = gzseek(gzf, numverts * 3 * sizeof(float), SEEK_CUR) != -1; // read number of normals if(gotBytes) gotBytes = gzread(gzf, &wri, sizeof(wri)); // skip normals gotBytes = gzseek(gzf, numverts * 3 * sizeof(float), SEEK_CUR) != -1; /* get no. of triangles */ if(gotBytes) gotBytes = gzread(gzf, &wri, sizeof(wri)); numfaces = wri; gzclose( gzf ); // ------------------------------------------------ if(!numfaces || !numverts || !gotBytes) return NULL; gzf = gzopen(filename, "rb"); if (!gzf) { return NULL; } dm = CDDM_new(numverts, 0, numfaces); if(!dm) { gzclose( gzf ); return NULL; } // read numverts gotBytes = gzread(gzf, &wri, sizeof(wri)); // read vertex position from file mv = CDDM_get_verts(dm); for(i=0; i<numverts; i++, mv++) gotBytes = gzread(gzf, mv->co, sizeof(float) * 3); // should be the same as numverts gotBytes = gzread(gzf, &wri, sizeof(wri)); if(wri != numverts) { if(dm) dm->release(dm); gzclose( gzf ); return NULL; } normals = MEM_callocN(sizeof(short) * numverts * 3, "fluid_tmp_normals" ); if(!normals) { if(dm) dm->release(dm); gzclose( gzf ); return NULL; } // read normals from file (but don't save them yet) for(i=numverts, no_s= normals; i>0; i--, no_s += 3) { gotBytes = gzread(gzf, no, sizeof(float) * 3); normal_float_to_short_v3(no_s, no); } /* read no. of triangles */ gotBytes = gzread(gzf, &wri, sizeof(wri)); if(wri!=numfaces) { printf("Fluidsim: error in reading data from file.\n"); if(dm) dm->release(dm); gzclose( gzf ); MEM_freeN(normals); return NULL; } // read triangles from file mf = CDDM_get_faces(dm); for(i=numfaces; i>0; i--, mf++) { int face[3]; gotBytes = gzread(gzf, face, sizeof(int) * 3); // check if 3rd vertex has index 0 (not allowed in blender) if(face[2]) { mf->v1 = face[0]; mf->v2 = face[1]; mf->v3 = face[2]; } else { mf->v1 = face[1]; mf->v2 = face[2]; mf->v3 = face[0]; } mf->v4 = 0; test_index_face(mf, NULL, 0, 3); } gzclose( gzf ); CDDM_calc_edges(dm); CDDM_apply_vert_normals(dm, (short (*)[3])normals); MEM_freeN(normals); // CDDM_calc_normals(result); return dm; }
/* retrieve a list from fd. add to blacklist bl */ struct bl * add_blacklist(struct bl *bl, size_t *blc, size_t *bls, gzFile gzf, int white) { int i, n, start, bu = 0, bs = 0, serrno = 0; char *buf = NULL, *tmp; struct bl *blt; for (;;) { /* read in gzf, then parse */ if (bu == bs) { tmp = realloc(buf, bs + (1024 * 1024) + 1); if (tmp == NULL) { free(buf); buf = NULL; bs = 0; serrno = errno; goto bldone; } bs += 1024 * 1024; buf = tmp; } n = gzread(gzf, buf + bu, bs - bu); if (n == 0) goto parse; else if (n == -1) { serrno = errno; goto bldone; } else bu += n; } parse: start = 0; for (i = 0; i <= bu; i++) { if (*blc == *bls) { *bls += 1024; blt = realloc(bl, *bls * sizeof(struct bl)); if (blt == NULL) { *bls -= 1024; serrno = errno; goto bldone; } bl = blt; } if (i == bu || buf[i] == '\n') { buf[i] = '\0'; if (parse_netblock(buf + start, bl + *blc, bl + *blc + 1, white)) *blc += 2; start = i + 1; } } if (bu == 0) errno = EIO; bldone: if (buf) free(buf); if (serrno) errno = serrno; return (bl); }
void maq2tam_core(gzFile fp, const char *rg) { maqmap_t *mm; maqmap1_t mm1, *m1; int ret; m1 = &mm1; mm = maqmap_read_header(fp); while ((ret = gzread(fp, m1, sizeof(maqmap1_t))) == sizeof(maqmap1_t)) { int j, flag = 0, se_mapq = m1->seq[MAX_READLEN-1]; if (m1->flag) flag |= 1; if ((m1->flag&PAIRFLAG_PAIRED) || ((m1->flag&PAIRFLAG_SW) && m1->flag != 192)) flag |= 2; if (m1->flag == 192) flag |= 4; if (m1->flag == 64) flag |= 8; if (m1->pos&1) flag |= 0x10; if ((flag&1) && m1->dist != 0) { int c; if (m1->dist > 0) { if (m1->flag&(PAIRFLAG_FF|PAIRFLAG_RF)) c = 0; else if (m1->flag&(PAIRFLAG_FR|PAIRFLAG_RR)) c = 1; else c = m1->pos&1; } else { if (m1->flag&(PAIRFLAG_FF|PAIRFLAG_FR)) c = 0; else if (m1->flag&(PAIRFLAG_RF|PAIRFLAG_RR)) c = 1; else c = m1->pos&1; } if (c) flag |= 0x20; } if (m1->flag) { int l = strlen(m1->name); if (m1->name[l-2] == '/') { flag |= (m1->name[l-1] == '1')? 0x40 : 0x80; m1->name[l-2] = '\0'; } } printf("%s\t%d\t", m1->name, flag); printf("%s\t%d\t", mm->ref_name[m1->seqid], (m1->pos>>1)+1); if (m1->flag == 130) { int c = (int8_t)m1->seq[MAX_READLEN-1]; printf("%d\t", m1->alt_qual); if (c == 0) printf("%dM\t", m1->size); else { if (c > 0) printf("%dM%dI%dM\t", m1->map_qual, c, m1->size - m1->map_qual - c); else printf("%dM%dD%dM\t", m1->map_qual, -c, m1->size - m1->map_qual); } se_mapq = 0; // zero SE mapQ for reads aligned by SW } else { if (flag&4) printf("0\t*\t"); else printf("%d\t%dM\t", m1->map_qual, m1->size); } printf("*\t0\t%d\t", m1->dist); for (j = 0; j != m1->size; ++j) { if (m1->seq[j] == 0) putchar('N'); else putchar("ACGT"[m1->seq[j]>>6&3]); } putchar('\t'); for (j = 0; j != m1->size; ++j) putchar((m1->seq[j]&0x3f) + 33); putchar('\t'); if (rg) printf("RG:Z:%s\t", rg); if (flag&4) { // unmapped printf("MF:i:%d\n", m1->flag); } else { printf("MF:i:%d\t", m1->flag); if (m1->flag) printf("AM:i:%d\tSM:i:%d\t", m1->alt_qual, se_mapq); printf("NM:i:%d\tUQ:i:%d\tH0:i:%d\tH1:i:%d\n", m1->info1&0xf, m1->info2, m1->c[0], m1->c[1]); } } if (ret > 0) fprintf(stderr, "Truncated! Continue anyway.\n"); maq_delete_maqmap(mm); }
GF_EXPORT char *gf_xml_sax_peek_node(GF_SAXParser *parser, char *att_name, char *att_value, char *substitute, char *get_attr, char *end_pattern, Bool *is_substitute) { u32 state, att_len, alloc_size; #ifdef NO_GZIP u64 pos; #else z_off_t pos; #endif Bool from_buffer; Bool dobreak=0; char szLine1[XML_INPUT_SIZE+2], szLine2[XML_INPUT_SIZE+2], *szLine, *cur_line, *sep, *start, first_c, *result; #define CPYCAT_ALLOC(__str, __is_copy) if ( strlen(__str) + (__is_copy ? 0 : strlen(szLine))>=alloc_size) {\ alloc_size = 1 + (u32) strlen(__str); \ if (!__is_copy) alloc_size += (u32) strlen(szLine); \ szLine = gf_realloc(szLine, alloc_size); \ }\ if (__is_copy) strcpy(szLine, __str); \ else strcat(szLine, __str); \ from_buffer=0; #ifdef NO_GZIP if (!parser->f_in) from_buffer=1; #else if (!parser->gz_in) from_buffer=1; #endif result = NULL; szLine1[0] = szLine2[0] = 0; pos=0; if (!from_buffer) { #ifdef NO_GZIP pos = gf_f64_tell(parser->f_in); #else pos = gztell(parser->gz_in); #endif } att_len = (u32) strlen(parser->buffer + parser->att_name_start); if (att_len<2*XML_INPUT_SIZE) att_len = 2*XML_INPUT_SIZE; alloc_size = att_len; szLine = (char *) gf_malloc(sizeof(char)*alloc_size); strcpy(szLine, parser->buffer + parser->att_name_start); cur_line = szLine; att_len = (u32) strlen(att_value); state = 0; goto retry; while (1) { u32 read; u8 sep_char; if (!from_buffer) { #ifdef NO_GZIP if (!feof(parser->f_in)) break; #else if (!gzeof(parser->gz_in)) break; #endif } if (dobreak) break; if (cur_line == szLine2) { cur_line = szLine1; } else { cur_line = szLine2; } if (from_buffer) { dobreak=1; } else { #ifdef NO_GZIP read = fread(cur_line, 1, XML_INPUT_SIZE, parser->f_in); #else read = gzread(parser->gz_in, cur_line, XML_INPUT_SIZE); #endif cur_line[read] = cur_line[read+1] = 0; CPYCAT_ALLOC(cur_line, 0); } if (end_pattern) { start = strstr(szLine, end_pattern); if (start) { start[0] = 0; dobreak = 1; } } retry: if (state == 2) goto fetch_attr; sep = strstr(szLine, att_name); if (!sep && !state) { state = 0; start = strrchr(szLine, '<'); if (start) { CPYCAT_ALLOC(start, 1); } else { CPYCAT_ALLOC(cur_line, 1); } continue; } if (!state) { state = 1; /*load next line*/ first_c = sep[0]; sep[0] = 0; start = strrchr(szLine, '<'); if (!start) goto exit; sep[0] = first_c; CPYCAT_ALLOC(start, 1); sep = strstr(szLine, att_name); } sep = strchr(sep, '='); if (!sep) { state = 0; CPYCAT_ALLOC(cur_line, 1); continue; } while (sep[0] && (sep[0] != '\"') && (sep[0] != '\'') ) sep++; if (!sep[0]) continue; sep_char = sep[0]; sep++; while (sep[0] && strchr(" \n\r\t", sep[0]) ) sep++; if (!sep[0]) continue; if (!strchr(sep, sep_char)) continue; /*found*/ if (!strncmp(sep, att_value, att_len)) { u32 pos; sep = szLine + 1; while (strchr(" \t\r\n", sep[0])) sep++; pos = 0; while (!strchr(" \t\r\n", sep[pos])) pos++; first_c = sep[pos]; sep[pos] = 0; state = 2; if (!substitute || !get_attr || strcmp(sep, substitute) ) { if (is_substitute) *is_substitute = 0; result = gf_strdup(sep); goto exit; } sep[pos] = first_c; fetch_attr: sep = strstr(szLine + 1, get_attr); if (!sep) { CPYCAT_ALLOC(cur_line, 1); continue; } sep += strlen(get_attr); while (strchr("= \t\r\n", sep[0])) sep++; sep++; pos = 0; while (!strchr(" \t\r\n/>", sep[pos])) pos++; sep[pos-1] = 0; result = gf_strdup(sep); if (is_substitute) *is_substitute = 1; goto exit; } state = 0; CPYCAT_ALLOC(sep, 1); goto retry; } exit: gf_free(szLine); if (!from_buffer) { #ifdef NO_GZIP gf_f64_seek(parser->f_in, pos, SEEK_SET); #else gzrewind(parser->gz_in); gzseek(parser->gz_in, pos, SEEK_SET); #endif } return result; }
static GF_Err xml_sax_read_file(GF_SAXParser *parser) { GF_Err e = GF_EOS; unsigned char szLine[XML_INPUT_SIZE+2]; #ifdef NO_GZIP if (!parser->f_in) return GF_BAD_PARAM; #else if (!parser->gz_in) return GF_BAD_PARAM; #endif while (!parser->suspended) { #ifdef NO_GZIP s32 read = fread(szLine, 1, XML_INPUT_SIZE, parser->f_in); #else s32 read = gzread(parser->gz_in, szLine, XML_INPUT_SIZE); #endif if ((read<=0) /*&& !parser->node_depth*/) break; szLine[read] = 0; szLine[read+1] = 0; e = gf_xml_sax_parse(parser, szLine); if (e) break; if (parser->file_pos > parser->file_size) parser->file_size = parser->file_pos + 1; if (parser->on_progress) parser->on_progress(parser->sax_cbck, parser->file_pos, parser->file_size); } #ifdef NO_GZIP if (feof(parser->f_in)) { #else if (gzeof(parser->gz_in)) { #endif if (!e) e = GF_EOS; if (parser->on_progress) parser->on_progress(parser->sax_cbck, parser->file_size, parser->file_size); #ifdef NO_GZIP fclose(parser->f_in); parser->f_in = NULL; #else gzclose(parser->gz_in); parser->gz_in = 0; #endif parser->elt_start_pos = parser->elt_end_pos = 0; parser->elt_name_start = parser->elt_name_end = 0; parser->att_name_start = 0; parser->current_pos = 0; parser->line_size = 0; parser->att_sep = 0; parser->file_pos = 0; parser->file_size = 0; parser->line_size = 0; } return e; } GF_EXPORT GF_Err gf_xml_sax_parse_file(GF_SAXParser *parser, const char *fileName, gf_xml_sax_progress OnProgress) { FILE *test; GF_Err e; #ifndef NO_GZIP gzFile gzInput; #endif unsigned char szLine[6]; parser->on_progress = OnProgress; if (!strncmp(fileName, "gmem://", 7)) { u32 size; u8 *xml_mem_address; if (sscanf(fileName, "gmem://%d@%p", &size, &xml_mem_address) != 2) { return GF_URL_ERROR; } parser->file_size = size; memcpy(szLine, xml_mem_address, 3); szLine[4] = szLine[5] = 0; e = gf_xml_sax_init(parser, szLine); if (e) return e; parser->file_pos = 4; parser->elt_start_pos = 0; parser->current_pos = 0; e = gf_xml_sax_parse(parser, xml_mem_address+3); if (parser->on_progress) parser->on_progress(parser->sax_cbck, parser->file_pos, parser->file_size); parser->elt_start_pos = parser->elt_end_pos = 0; parser->elt_name_start = parser->elt_name_end = 0; parser->att_name_start = 0; parser->current_pos = 0; parser->line_size = 0; parser->att_sep = 0; parser->file_pos = 0; parser->file_size = 0; parser->line_size = 0; return e; } /*check file exists and gets its size (zlib doesn't support SEEK_END)*/ test = gf_f64_open(fileName, "rb"); if (!test) return GF_URL_ERROR; gf_f64_seek(test, 0, SEEK_END); assert(gf_f64_tell(test) < 1<<31); parser->file_size = (u32) gf_f64_tell(test); fclose(test); #ifdef NO_GZIP parser->f_in = gf_f64_open(fileName, "rt"); fread(szLine, 1, 4, parser->f_in); #else gzInput = gzopen(fileName, "rb"); if (!gzInput) return GF_IO_ERR; parser->gz_in = gzInput; /*init SAX parser (unicode setup)*/ gzread(gzInput, szLine, 4); #endif szLine[4] = szLine[5] = 0; e = gf_xml_sax_init(parser, szLine); if (e) return e; parser->file_pos = 4; /* souchay : not sure for next 2 lines, but it works better it seems */ parser->elt_start_pos = 0; parser->current_pos = 0; return xml_sax_read_file(parser); }
/* convert arguments */ static int xgzread(void *cookie, char *data, int size) { return gzread(cookie, data, size); }
int main(int argc, char *argv[]) { int ch, idx, plen, nready, interactive = 0, listonly = 0; const char *id, *user = NULL, *pattern = NULL, *tty = NULL, *decimal = "."; char path[PATH_MAX], buf[LINE_MAX], *cp, *ep; double seconds, to_wait, speed = 1.0, max_wait = 0; FILE *lfile; fd_set *fdsw; sigaction_t sa; size_t len, nbytes, nread, off; ssize_t nwritten; #if !defined(HAVE_GETPROGNAME) && !defined(HAVE___PROGNAME) setprogname(argc > 0 ? argv[0] : "sudoreplay"); #endif #ifdef HAVE_SETLOCALE setlocale(LC_ALL, ""); decimal = localeconv()->decimal_point; #endif while ((ch = getopt(argc, argv, "d:f:hlm:s:V")) != -1) { switch(ch) { case 'd': session_dir = optarg; break; case 'f': /* Set the replay filter. */ replay_filter = 0; for (cp = strtok(optarg, ","); cp; cp = strtok(NULL, ",")) { if (strcmp(cp, "stdout") == 0) SET(replay_filter, 1 << IOFD_STDOUT); else if (strcmp(cp, "stderr") == 0) SET(replay_filter, 1 << IOFD_STDERR); else if (strcmp(cp, "ttyout") == 0) SET(replay_filter, 1 << IOFD_TTYOUT); else errorx(1, "invalid filter option: %s", optarg); } break; case 'h': help(); /* NOTREACHED */ case 'l': listonly = 1; break; case 'm': errno = 0; max_wait = strtod(optarg, &ep); if (*ep != '\0' || errno != 0) errorx(1, "invalid max wait: %s", optarg); break; case 's': errno = 0; speed = strtod(optarg, &ep); if (*ep != '\0' || errno != 0) errorx(1, "invalid speed factor: %s", optarg); break; case 'V': (void) printf("%s version %s\n", getprogname(), PACKAGE_VERSION); exit(0); default: usage(1); /* NOTREACHED */ } } argc -= optind; argv += optind; if (listonly) exit(list_sessions(argc, argv, pattern, user, tty)); if (argc != 1) usage(1); /* 6 digit ID in base 36, e.g. 01G712AB */ id = argv[0]; if (!VALID_ID(id)) errorx(1, "invalid ID %s", id); plen = snprintf(path, sizeof(path), "%s/%.2s/%.2s/%.2s/timing", session_dir, id, &id[2], &id[4]); if (plen <= 0 || plen >= sizeof(path)) errorx(1, "%s/%.2s/%.2s/%.2s/%.2s/timing: %s", session_dir, id, &id[2], &id[4], strerror(ENAMETOOLONG)); plen -= 7; /* Open files for replay, applying replay filter for the -f flag. */ for (idx = 0; idx < IOFD_MAX; idx++) { if (ISSET(replay_filter, 1 << idx) || idx == IOFD_TIMING) { io_fds[idx].v = open_io_fd(path, plen, io_fnames[idx]); if (io_fds[idx].v == NULL) error(1, "unable to open %s", path); } } /* Read log file. */ path[plen] = '\0'; strlcat(path, "/log", sizeof(path)); lfile = fopen(path, "r"); if (lfile == NULL) error(1, "unable to open %s", path); cp = NULL; len = 0; /* Pull out command (third line). */ if (getline(&cp, &len, lfile) == -1 || getline(&cp, &len, lfile) == -1 || getline(&cp, &len, lfile) == -1) { errorx(1, "invalid log file %s", path); } printf("Replaying sudo session: %s", cp); free(cp); fclose(lfile); fflush(stdout); zero_bytes(&sa, sizeof(sa)); sigemptyset(&sa.sa_mask); sa.sa_flags = SA_RESETHAND; sa.sa_handler = cleanup; (void) sigaction(SIGINT, &sa, NULL); (void) sigaction(SIGKILL, &sa, NULL); (void) sigaction(SIGTERM, &sa, NULL); (void) sigaction(SIGHUP, &sa, NULL); sa.sa_flags = SA_RESTART; sa.sa_handler = SIG_IGN; (void) sigaction(SIGTSTP, &sa, NULL); (void) sigaction(SIGQUIT, &sa, NULL); /* XXX - read user input from /dev/tty and set STDOUT to raw if not a pipe */ /* Set stdin to raw mode if it is a tty */ interactive = isatty(STDIN_FILENO); if (interactive) { ch = fcntl(STDIN_FILENO, F_GETFL, 0); if (ch != -1) (void) fcntl(STDIN_FILENO, F_SETFL, ch | O_NONBLOCK); if (!term_raw(STDIN_FILENO, 1)) error(1, "cannot set tty to raw mode"); } fdsw = (fd_set *)emalloc2(howmany(STDOUT_FILENO + 1, NFDBITS), sizeof(fd_mask)); /* * Timing file consists of line of the format: "%f %d\n" */ #ifdef HAVE_ZLIB_H while (gzgets(io_fds[IOFD_TIMING].g, buf, sizeof(buf)) != NULL) { #else while (fgets(buf, sizeof(buf), io_fds[IOFD_TIMING].f) != NULL) { #endif if (!parse_timing(buf, decimal, &idx, &seconds, &nbytes)) errorx(1, "invalid timing file line: %s", buf); if (interactive) check_input(STDIN_FILENO, &speed); /* Adjust delay using speed factor and clamp to max_wait */ to_wait = seconds / speed; if (max_wait && to_wait > max_wait) to_wait = max_wait; delay(to_wait); /* Even if we are not relaying, we still have to delay. */ if (io_fds[idx].v == NULL) continue; /* All output is sent to stdout. */ while (nbytes != 0) { if (nbytes > sizeof(buf)) len = sizeof(buf); else len = nbytes; #ifdef HAVE_ZLIB_H nread = gzread(io_fds[idx].g, buf, len); #else nread = fread(buf, 1, len, io_fds[idx].f); #endif nbytes -= nread; off = 0; do { /* no stdio, must be unbuffered */ nwritten = write(STDOUT_FILENO, buf + off, nread - off); if (nwritten == -1) { if (errno == EINTR) continue; if (errno == EAGAIN) { FD_SET(STDOUT_FILENO, fdsw); do { nready = select(STDOUT_FILENO + 1, NULL, fdsw, NULL, NULL); } while (nready == -1 && errno == EINTR); if (nready == 1) continue; } error(1, "writing to standard output"); } off += nwritten; } while (nread > off); } } term_restore(STDIN_FILENO, 1); exit(0); } static void delay(double secs) { struct timespec ts, rts; int rval; /* * Typical max resolution is 1/HZ but we can't portably check that. * If the interval is small enough, just ignore it. */ if (secs < 0.0001) return; rts.tv_sec = secs; rts.tv_nsec = (secs - (double) rts.tv_sec) * 1000000000.0; do { memcpy(&ts, &rts, sizeof(ts)); rval = nanosleep(&ts, &rts); } while (rval == -1 && errno == EINTR); if (rval == -1) error(1, "nanosleep: tv_sec %ld, tv_nsec %ld", ts.tv_sec, ts.tv_nsec); } static void * open_io_fd(char *path, int len, const char *suffix) { path[len] = '\0'; strlcat(path, suffix, PATH_MAX); #ifdef HAVE_ZLIB_H return gzopen(path, "r"); #else return fopen(path, "r"); #endif }
int GZIPROMReaderRead(void * file, void * buffer, u32 size) { return gzread(file, buffer, size); }
short int GeoIP_update_database (char * license_key, int verbose, void (*f)( char * )) { struct hostent *hostlist; int sock; char * buf; struct sockaddr_in sa; int offset = 0, err; char * request_uri; char * compr; unsigned long comprLen; FILE *comp_fh, *cur_db_fh, *gi_fh; gzFile gz_fh; char * file_path_gz, * file_path_test; MD5_CONTEXT context; unsigned char buffer[1024], digest[16]; char hex_digest[33] = "00000000000000000000000000000000\0"; unsigned int i; GeoIP * gi; char * db_info; char block[BLOCK_SIZE]; int block_size = BLOCK_SIZE; size_t len; size_t written; _GeoIP_setup_dbfilename(); /* get MD5 of current GeoIP database file */ if ((cur_db_fh = fopen (GeoIPDBFileName[GEOIP_COUNTRY_EDITION], "rb")) == NULL) { GeoIP_printf(f,"%s%s", NoCurrentDB, GeoIPDBFileName[GEOIP_COUNTRY_EDITION]); } else { md5_init(&context); while ((len = fread (buffer, 1, 1024, cur_db_fh)) > 0) md5_write (&context, buffer, len); md5_final (&context); memcpy(digest,context.buf,16); fclose (cur_db_fh); for (i = 0; i < 16; i++) { // "%02x" will write 3 chars snprintf (&hex_digest[2*i], 3, "%02x", digest[i]); } GeoIP_printf(f, MD5Info, hex_digest); } hostlist = GeoIP_get_host_or_proxy(); if (hostlist == NULL) return GEOIP_DNS_ERR; if (hostlist->h_addrtype != AF_INET) return GEOIP_NON_IPV4_ERR; if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { return GEOIP_SOCKET_OPEN_ERR; } memset(&sa, 0, sizeof(struct sockaddr_in)); sa.sin_port = htons(GeoIPHTTPPort); memcpy(&sa.sin_addr, hostlist->h_addr_list[0], hostlist->h_length); sa.sin_family = AF_INET; if (verbose == 1){ GeoIP_printf(f,"Connecting to MaxMind GeoIP Update server\n"); GeoIP_printf(f, "via Host or Proxy Server: %s:%d\n", hostlist->h_name, GeoIPHTTPPort); } /* Download gzip file */ if (connect(sock, (struct sockaddr *)&sa, sizeof(struct sockaddr))< 0) return GEOIP_CONNECTION_ERR; request_uri = malloc(sizeof(char) * (strlen(license_key) + strlen(GeoIPHTTPRequest) + strlen(GeoIPProxyHTTP) + strlen(GeoIPProxiedHost) + 36 + 1)); if (request_uri == NULL) return GEOIP_OUT_OF_MEMORY_ERR; sprintf(request_uri,GeoIPHTTPRequest,GeoIPProxyHTTP,GeoIPProxiedHost,license_key, hex_digest); send(sock, request_uri, strlen(request_uri),0); free(request_uri); buf = malloc(sizeof(char) * block_size + 1); if (buf == NULL) return GEOIP_OUT_OF_MEMORY_ERR; if (verbose == 1) GeoIP_printf(f,"Downloading gzipped GeoIP Database...\n"); for (;;) { int amt; amt = recv(sock, &buf[offset], block_size,0); if (amt == 0) { break; } else if (amt == -1) { free(buf); return GEOIP_SOCKET_READ_ERR; } offset += amt; buf = realloc(buf, offset+block_size + 1); if (buf == NULL) return GEOIP_OUT_OF_MEMORY_ERR; } buf[offset]=0; compr = strstr(buf, "\r\n\r\n"); if ( compr == NULL ) { free(buf); return GEOIP_INVALID_SERVER_RESPONSE; } /* skip searchstr "\r\n\r\n" */ compr += 4; comprLen = offset + buf - compr; if (strstr(compr, "License Key Invalid") != NULL) { if (verbose == 1) GeoIP_printf(f,"Failed\n"); free(buf); return GEOIP_LICENSE_KEY_INVALID_ERR; } else if (strstr(compr, "Invalid product ID or subscription expired") != NULL){ free(buf); return GEOIP_PRODUCT_ID_INVALID_ERR; } else if (strstr(compr, "No new updates available") != NULL) { free(buf); return GEOIP_NO_NEW_UPDATES; } if (verbose == 1) GeoIP_printf(f,"Done\n"); /* save gzip file */ file_path_gz = malloc(sizeof(char) * (strlen(GeoIPDBFileName[GEOIP_COUNTRY_EDITION]) + 4)); if (file_path_gz == NULL) return GEOIP_OUT_OF_MEMORY_ERR; strcpy(file_path_gz,GeoIPDBFileName[GEOIP_COUNTRY_EDITION]); strcat(file_path_gz,".gz"); if (verbose == 1) { GeoIP_printf(f, SavingGzip, file_path_gz); } comp_fh = fopen(file_path_gz, "wb"); if(comp_fh == NULL) { free(file_path_gz); free(buf); return GEOIP_GZIP_IO_ERR; } written = fwrite(compr, 1, comprLen, comp_fh); fclose(comp_fh); free(buf); if ( written != comprLen ) return GEOIP_GZIP_IO_ERR; if (verbose == 1) GeoIP_printf(f,"Done\n"); if (verbose == 1) GeoIP_printf(f,"Uncompressing gzip file ... "); /* uncompress gzip file */ gz_fh = gzopen(file_path_gz, "rb"); file_path_test = malloc(sizeof(char) * (strlen(GeoIPDBFileName[GEOIP_COUNTRY_EDITION]) + 6)); if (file_path_test == NULL) return GEOIP_OUT_OF_MEMORY_ERR; strcpy(file_path_test,GeoIPDBFileName[GEOIP_COUNTRY_EDITION]); strcat(file_path_test,".test"); gi_fh = fopen(file_path_test, "wb"); if(gi_fh == NULL) { free(file_path_test); return GEOIP_TEST_IO_ERR; } for (;;) { int amt; amt = gzread(gz_fh, block, block_size); if (amt == -1) { free(file_path_test); fclose(gi_fh); gzclose(gz_fh); return GEOIP_GZIP_READ_ERR; } if (amt == 0) { break; } if ( fwrite(block,1,amt,gi_fh) != amt ){ free(file_path_test); fclose(gi_fh); gzclose(gz_fh); return GEOIP_GZIP_READ_ERR; } } gzclose(gz_fh); unlink(file_path_gz); free(file_path_gz); fclose(gi_fh); if (verbose == 1) GeoIP_printf(f,"Done\n"); if (verbose == 1) { GeoIP_printf(f, WritingFile, GeoIPDBFileName[GEOIP_COUNTRY_EDITION]); } /* sanity check */ gi = GeoIP_open(file_path_test, GEOIP_STANDARD); if (verbose == 1) GeoIP_printf(f,"Performing santity checks ... "); if (gi == NULL) { GeoIP_printf(f,"Error opening sanity check database\n"); return GEOIP_SANITY_OPEN_ERR; } /* this checks to make sure the files is complete, since info is at the end */ /* dependent on future databases having MaxMind in info */ if (verbose == 1) GeoIP_printf(f,"database_info "); db_info = GeoIP_database_info(gi); if (db_info == NULL) { GeoIP_delete(gi); if (verbose == 1) GeoIP_printf(f,"FAIL\n"); return GEOIP_SANITY_INFO_FAIL; } if (strstr(db_info, "MaxMind") == NULL) { free(db_info); GeoIP_delete(gi); if (verbose == 1) GeoIP_printf(f,"FAIL\n"); return GEOIP_SANITY_INFO_FAIL; } free(db_info); if (verbose == 1) GeoIP_printf(f,"PASS "); /* this performs an IP lookup test of a US IP address */ if (verbose == 1) GeoIP_printf(f,"lookup "); if (strcmp(GeoIP_country_code_by_addr(gi,"24.24.24.24"), "US") != 0) { GeoIP_delete(gi); if (verbose == 1) GeoIP_printf(f,"FAIL\n"); return GEOIP_SANITY_LOOKUP_FAIL; } GeoIP_delete(gi); if (verbose == 1) GeoIP_printf(f,"PASS\n"); /* install GeoIP.dat.test -> GeoIP.dat */ err = rename(file_path_test, GeoIPDBFileName[GEOIP_COUNTRY_EDITION]); if (err != 0) { GeoIP_printf(f,"GeoIP Install error while renaming file\n"); return GEOIP_RENAME_ERR; } if (verbose == 1) GeoIP_printf(f,"Done\n"); return 0; }
/* =========================================================================== * Test read/write of .gz files */ void test_gzio( const char *fname, Byte *uncompr, uLong uncomprLen) { #ifdef NO_GZCOMPRESS fprintf(stderr, "NO_GZCOMPRESS -- gz* functions cannot compress\n"); #else int err; int len = (int)strlen(hello)+1; gzFile file; z_off_t pos; file = gzopen(fname, "wb"); if (file == NULL) { fprintf(stderr, "gzopen error\n"); exit(1); } gzputc(file, 'h'); if (gzputs(file, "ello") != 4) { fprintf(stderr, "gzputs err: %s\n", gzerror(file, &err)); exit(1); } if (gzprintf(file, ", %s!", "hello") != 8) { fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err)); exit(1); } gzseek(file, 1L, SEEK_CUR); /* add one zero byte */ gzclose(file); file = gzopen(fname, "rb"); if (file == NULL) { fprintf(stderr, "gzopen error\n"); exit(1); } strcpy((char*)uncompr, "garbage"); if (gzread(file, uncompr, (unsigned)uncomprLen) != len) { fprintf(stderr, "gzread err: %s\n", gzerror(file, &err)); exit(1); } if (strcmp((char*)uncompr, hello)) { fprintf(stderr, "bad gzread: %s\n", (char*)uncompr); exit(1); } else { printf("gzread(): %s\n", (char*)uncompr); } pos = gzseek(file, -8L, SEEK_CUR); if (pos != 6 || gztell(file) != pos) { fprintf(stderr, "gzseek error, pos=%ld, gztell=%ld\n", (long)pos, (long)gztell(file)); exit(1); } if (gzgetc(file) != ' ') { fprintf(stderr, "gzgetc error\n"); exit(1); } if (gzungetc(' ', file) != ' ') { fprintf(stderr, "gzungetc error\n"); exit(1); } gzgets(file, (char*)uncompr, (int)uncomprLen); if (strlen((char*)uncompr) != 7) { /* " hello!" */ fprintf(stderr, "gzgets err after gzseek: %s\n", gzerror(file, &err)); exit(1); } if (strcmp((char*)uncompr, hello + 6)) { fprintf(stderr, "bad gzgets after gzseek\n"); exit(1); } else { printf("gzgets() after gzseek: %s\n", (char*)uncompr); } gzclose(file); #endif }
short int GeoIP_update_database_general (char * user_id,char * license_key,char *data_base_type, int verbose,char ** client_ipaddr, void (*f)( char *)) { struct hostent *hostlist; int sock; char * buf; struct sockaddr_in sa; int offset = 0, err; char * request_uri; char * compr; unsigned long comprLen; FILE *comp_fh, *cur_db_fh, *gi_fh; gzFile gz_fh; char * file_path_gz, * file_path_test; MD5_CONTEXT context; MD5_CONTEXT context2; unsigned char buffer[1024], digest[16] ,digest2[16]; char hex_digest[33] = "0000000000000000000000000000000\0"; char hex_digest2[33] = "0000000000000000000000000000000\0"; unsigned int i; char *f_str; GeoIP * gi; char * db_info; char *ipaddress; char *geoipfilename; char *tmpstr; int dbtype; int lookupresult = 1; char block[BLOCK_SIZE]; int block_size = BLOCK_SIZE; size_t len; size_t request_uri_len; size_t size; hostlist = GeoIP_get_host_or_proxy(); if (hostlist == NULL) return GEOIP_DNS_ERR; if (hostlist->h_addrtype != AF_INET) return GEOIP_NON_IPV4_ERR; if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { return GEOIP_SOCKET_OPEN_ERR; } memset(&sa, 0, sizeof(struct sockaddr_in)); sa.sin_port = htons(GeoIPHTTPPort); memcpy(&sa.sin_addr, hostlist->h_addr_list[0], hostlist->h_length); sa.sin_family = AF_INET; if (verbose == 1) { GeoIP_printf(f,"Connecting to MaxMind GeoIP server\n"); GeoIP_printf(f, "via Host or Proxy Server: %s:%d\n", hostlist->h_name, GeoIPHTTPPort); } if (connect(sock, (struct sockaddr *)&sa, sizeof(struct sockaddr))< 0) return GEOIP_CONNECTION_ERR; request_uri = malloc(sizeof(char) * (strlen(GeoIPHTTPRequestFilename) + strlen(GeoIPProxyHTTP) + strlen(GeoIPProxiedHost) + strlen(data_base_type) + strlen(GeoIPUpdateHost) + 1)); if (request_uri == NULL) return GEOIP_OUT_OF_MEMORY_ERR; /* get the file name from a web page using the product id */ sprintf(request_uri,GeoIPHTTPRequestFilename,GeoIPProxyHTTP,GeoIPProxiedHost,data_base_type,GeoIPUpdateHost); if (verbose == 1) { GeoIP_printf(f, "sending request %s \n",request_uri); } send(sock, request_uri, strlen(request_uri),0); /* send the request */ free(request_uri); buf = malloc(sizeof(char) * (block_size+4)); if (buf == NULL) return GEOIP_OUT_OF_MEMORY_ERR; offset = 0; for (;;){ int amt; amt = recv(sock, &buf[offset], block_size,0); if (amt == 0){ break; } else if (amt == -1) { free(buf); return GEOIP_SOCKET_READ_ERR; } offset += amt; buf = realloc(buf, offset + block_size + 4); } buf[offset] = 0; offset = 0; tmpstr = strstr(buf, "\r\n\r\n"); if ( tmpstr == NULL ) { free(buf); return GEOIP_INVALID_SERVER_RESPONSE; } /* skip searchstr "\r\n\r\n" */ tmpstr += 4; if (tmpstr[0] == '.' || strchr(tmpstr, '/') != NULL || strchr(tmpstr, '\\') != NULL) { free(buf); return GEOIP_INVALID_SERVER_RESPONSE; } geoipfilename = _GeoIP_full_path_to(tmpstr); free(buf); /* print the database product id and the database filename */ if (verbose == 1){ GeoIP_printf(f, "database product id %s database file name %s \n",data_base_type,geoipfilename); } _GeoIP_setup_dbfilename(); /* get MD5 of current GeoIP database file */ if ((cur_db_fh = fopen (geoipfilename, "rb")) == NULL) { GeoIP_printf(f, NoCurrentDB, geoipfilename); } else { md5_init(&context); while ((len = fread (buffer, 1, 1024, cur_db_fh)) > 0) md5_write (&context, buffer, len); md5_final (&context); memcpy(digest,context.buf,16); fclose (cur_db_fh); for (i = 0; i < 16; i++) sprintf (&hex_digest[2*i], "%02x", digest[i]); GeoIP_printf(f, MD5Info, hex_digest ); } if (verbose == 1) { GeoIP_printf(f,"MD5 sum of database %s is %s \n",geoipfilename,hex_digest); } if (client_ipaddr[0] == NULL) { /* We haven't gotten our IP address yet, so let's request it */ if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { free(geoipfilename); return GEOIP_SOCKET_OPEN_ERR; } memset(&sa, 0, sizeof(struct sockaddr_in)); sa.sin_port = htons(GeoIPHTTPPort); memcpy(&sa.sin_addr, hostlist->h_addr_list[0], hostlist->h_length); sa.sin_family = AF_INET; if (verbose == 1) GeoIP_printf(f,"Connecting to MaxMind GeoIP Update server\n"); /* Download gzip file */ if (connect(sock, (struct sockaddr *)&sa, sizeof(struct sockaddr))< 0) { free(geoipfilename); return GEOIP_CONNECTION_ERR; } request_uri = malloc(sizeof(char) * (strlen(GeoIPHTTPRequestClientIP) + strlen(GeoIPProxyHTTP) + strlen(GeoIPProxiedHost) + strlen(GeoIPUpdateHost) + 1 )); if (request_uri == NULL) { free(geoipfilename); return GEOIP_OUT_OF_MEMORY_ERR; } /* get client ip address from MaxMind web page */ sprintf(request_uri,GeoIPHTTPRequestClientIP,GeoIPProxyHTTP,GeoIPProxiedHost,GeoIPUpdateHost); send(sock, request_uri, strlen(request_uri),0); /* send the request */ if (verbose == 1) { GeoIP_printf(f, "sending request %s", request_uri); } free(request_uri); buf = malloc(sizeof(char) * (block_size+1)); if (buf == NULL) { free(geoipfilename); return GEOIP_OUT_OF_MEMORY_ERR; } offset = 0; for (;;){ int amt; amt = recv(sock, &buf[offset], block_size,0); if (amt == 0) { break; } else if (amt == -1) { free(buf); return GEOIP_SOCKET_READ_ERR; } offset += amt; buf = realloc(buf, offset+block_size+1); } buf[offset] = 0; offset = 0; ipaddress = strstr(buf, "\r\n\r\n") + 4; /* get the ip address */ ipaddress = malloc(strlen(strstr(buf, "\r\n\r\n") + 4)+5); strcpy(ipaddress,strstr(buf, "\r\n\r\n") + 4); client_ipaddr[0] = ipaddress; if (verbose == 1) { GeoIP_printf(f, "client ip address: %s\n",ipaddress); } free(buf); close(sock); } ipaddress = client_ipaddr[0]; /* make a md5 sum of ip address and license_key and store it in hex_digest2 */ md5_init(&context2); md5_write (&context2, (byte *)license_key, 12);//add license key to the md5 sum md5_write (&context2, (byte *)ipaddress, strlen(ipaddress));//add ip address to the md5 sum md5_final (&context2); memcpy(digest2,context2.buf,16); for (i = 0; i < 16; i++) snprintf (&hex_digest2[2*i], 3, "%02x", digest2[i]);// change the digest to a hex digest if (verbose == 1) { GeoIP_printf(f, "md5sum of ip address and license key is %s \n",hex_digest2); } /* send the request using the user id,product id, * md5 sum of the prev database and * the md5 sum of the license_key and ip address */ if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { return GEOIP_SOCKET_OPEN_ERR; } memset(&sa, 0, sizeof(struct sockaddr_in)); sa.sin_port = htons(GeoIPHTTPPort); memcpy(&sa.sin_addr, hostlist->h_addr_list[0], hostlist->h_length); sa.sin_family = AF_INET; if (connect(sock, (struct sockaddr *)&sa, sizeof(struct sockaddr))< 0) return GEOIP_CONNECTION_ERR; request_uri_len = sizeof(char) * 2036; request_uri = malloc(request_uri_len); if (request_uri == NULL) return GEOIP_OUT_OF_MEMORY_ERR; snprintf(request_uri, request_uri_len, GeoIPHTTPRequestMD5,GeoIPProxyHTTP,GeoIPProxiedHost,hex_digest,hex_digest2,user_id,data_base_type); send(sock, request_uri, strlen(request_uri),0); if (verbose == 1) { GeoIP_printf(f, "sending request %s\n",request_uri); } free(request_uri); offset = 0; buf = malloc(sizeof(char) * block_size); if (buf == NULL) return GEOIP_OUT_OF_MEMORY_ERR; if (verbose == 1) GeoIP_printf(f,"Downloading gzipped GeoIP Database...\n"); for (;;) { int amt; amt = recv(sock, &buf[offset], block_size,0); if (amt == 0) { break; } else if (amt == -1) { free(buf); return GEOIP_SOCKET_READ_ERR; } offset += amt; buf = realloc(buf, offset+block_size); if (buf == NULL) return GEOIP_OUT_OF_MEMORY_ERR; } compr = strstr(buf, "\r\n\r\n") + 4; comprLen = offset + buf - compr; if (strstr(compr, "License Key Invalid") != NULL) { if (verbose == 1) GeoIP_printf(f,"Failed\n"); free(buf); return GEOIP_LICENSE_KEY_INVALID_ERR; } else if (strstr(compr, "No new updates available") != NULL) { free(buf); GeoIP_printf(f, "%s is up to date, no updates required\n", geoipfilename); return GEOIP_NO_NEW_UPDATES; } else if (strstr(compr, "Invalid UserId") != NULL){ free(buf); return GEOIP_USER_ID_INVALID_ERR; } else if (strstr(compr, "Invalid product ID or subscription expired") != NULL){ free(buf); return GEOIP_PRODUCT_ID_INVALID_ERR; } if (verbose == 1) GeoIP_printf(f,"Done\n"); GeoIP_printf(f, "Updating %s\n", geoipfilename); /* save gzip file */ file_path_gz = malloc(sizeof(char) * (strlen(geoipfilename) + 4)); if (file_path_gz == NULL) return GEOIP_OUT_OF_MEMORY_ERR; strcpy(file_path_gz,geoipfilename); strcat(file_path_gz,".gz"); if (verbose == 1) { GeoIP_printf(f, SavingGzip, file_path_gz ); } comp_fh = fopen(file_path_gz, "wb"); if(comp_fh == NULL) { free(file_path_gz); free(buf); return GEOIP_GZIP_IO_ERR; } size = fwrite(compr, 1, comprLen, comp_fh); fclose(comp_fh); free(buf); if ( size != comprLen ) { return GEOIP_GZIP_IO_ERR; } if (verbose == 1) { GeoIP_printf(f, "download data to a gz file named %s \n",file_path_gz); GeoIP_printf(f,"Done\n"); GeoIP_printf(f,"Uncompressing gzip file ... "); } file_path_test = malloc(sizeof(char) * (strlen(GeoIPDBFileName[GEOIP_COUNTRY_EDITION]) + 6)); if (file_path_test == NULL) { free(file_path_gz); return GEOIP_OUT_OF_MEMORY_ERR; } strcpy(file_path_test,GeoIPDBFileName[GEOIP_COUNTRY_EDITION]); strcat(file_path_test,".test"); gi_fh = fopen(file_path_test, "wb"); if(gi_fh == NULL) { free(file_path_test); free(file_path_gz); return GEOIP_TEST_IO_ERR; } /* uncompress gzip file */ offset = 0; gz_fh = gzopen(file_path_gz, "rb"); for (;;) { int amt; amt = gzread(gz_fh, block, block_size); if (amt == -1) { free(file_path_gz); free(file_path_test); gzclose(gz_fh); fclose(gi_fh); return GEOIP_GZIP_READ_ERR; } if (amt == 0) { break; } if ( amt != fwrite(block,1,amt,gi_fh) ){ return GEOIP_GZIP_IO_ERR; } } gzclose(gz_fh); unlink(file_path_gz); free(file_path_gz); fclose(gi_fh); if (verbose == 1) GeoIP_printf(f,"Done\n"); if (verbose == 1) { len = strlen(WritingFile) + strlen(geoipfilename) - 1; f_str = malloc(len); snprintf(f_str,len,WritingFile,geoipfilename); free(f_str); } /* sanity check */ gi = GeoIP_open(file_path_test, GEOIP_STANDARD); if (verbose == 1) GeoIP_printf(f,"Performing santity checks ... "); if (gi == NULL) { GeoIP_printf(f,"Error opening sanity check database\n"); return GEOIP_SANITY_OPEN_ERR; } /* get the database type */ dbtype = GeoIP_database_edition(gi); if (verbose == 1) { GeoIP_printf(f, "Database type is %d\n",dbtype); } /* this checks to make sure the files is complete, since info is at the end dependent on future databases having MaxMind in info (ISP and Organization databases currently don't have info string */ if ((dbtype != GEOIP_ISP_EDITION)&& (dbtype != GEOIP_ORG_EDITION)) { if (verbose == 1) GeoIP_printf(f,"database_info "); db_info = GeoIP_database_info(gi); if (db_info == NULL) { GeoIP_delete(gi); if (verbose == 1) GeoIP_printf(f,"FAIL null\n"); return GEOIP_SANITY_INFO_FAIL; } if (strstr(db_info, "MaxMind") == NULL) { free(db_info); GeoIP_delete(gi); if (verbose == 1) GeoIP_printf(f,"FAIL maxmind\n"); return GEOIP_SANITY_INFO_FAIL; } free(db_info); if (verbose == 1) GeoIP_printf(f,"PASS "); } /* this performs an IP lookup test of a US IP address */ if (verbose == 1) GeoIP_printf(f,"lookup "); if (dbtype == GEOIP_NETSPEED_EDITION) { int netspeed = GeoIP_id_by_name(gi,"24.24.24.24"); lookupresult = 0; if (netspeed == GEOIP_CABLEDSL_SPEED){ lookupresult = 1; } } if (dbtype == GEOIP_COUNTRY_EDITION) { /* if data base type is country then call the function * named GeoIP_country_code_by_addr */ lookupresult = 1; if (strcmp(GeoIP_country_code_by_addr(gi,"24.24.24.24"), "US") != 0) { lookupresult = 0; } if (verbose == 1) { GeoIP_printf(f,"testing GEOIP_COUNTRY_EDITION\n"); } } if (dbtype == GEOIP_REGION_EDITION_REV1) { /* if data base type is region then call the function * named GeoIP_region_by_addr */ GeoIPRegion *r = GeoIP_region_by_addr(gi,"24.24.24.24"); lookupresult = 0; if (r != NULL) { lookupresult = 1; free(r); } if (verbose == 1) { GeoIP_printf(f,"testing GEOIP_REGION_EDITION\n"); } } if (dbtype == GEOIP_CITY_EDITION_REV1) { /* if data base type is city then call the function * named GeoIP_record_by_addr */ GeoIPRecord *r = GeoIP_record_by_addr(gi,"24.24.24.24"); lookupresult = 0; if (r != NULL) { lookupresult = 1; free(r); } if (verbose == 1) { GeoIP_printf(f,"testing GEOIP_CITY_EDITION\n"); } } if ((dbtype == GEOIP_ISP_EDITION)|| (dbtype == GEOIP_ORG_EDITION)) { /* if data base type is isp or org then call the function * named GeoIP_org_by_addr */ GeoIPRecord *r = (GeoIPRecord*)GeoIP_org_by_addr(gi,"24.24.24.24"); lookupresult = 0; if (r != NULL) { lookupresult = 1; free(r); } if (verbose == 1) { if (dbtype == GEOIP_ISP_EDITION) { GeoIP_printf(f,"testing GEOIP_ISP_EDITION\n"); } if (dbtype == GEOIP_ORG_EDITION) { GeoIP_printf(f,"testing GEOIP_ORG_EDITION\n"); } } } if (lookupresult == 0) { GeoIP_delete(gi); if (verbose == 1) GeoIP_printf(f,"FAIL\n"); return GEOIP_SANITY_LOOKUP_FAIL; } GeoIP_delete(gi); if (verbose == 1) GeoIP_printf(f,"PASS\n"); /* install GeoIP.dat.test -> GeoIP.dat */ err = rename(file_path_test, geoipfilename); if (err != 0) { GeoIP_printf(f,"GeoIP Install error while renaming file\n"); return GEOIP_RENAME_ERR; } if (verbose == 1) GeoIP_printf(f,"Done\n"); free(geoipfilename); return 0; }
char* decompress(const char *zfin) { FILE *fin, *fout; unsigned char * buf; unsigned long size, nReadBytes; unsigned char signature[4]; static unsigned char szCurrentFile[MAX_PATH]; assert(zfin != NULL); /* open and detect file type */ fin = fopen(zfin, "rb"); if(!fin) { printf("Unzip: file [%s] open error\n", zfin); return NULL; } fread(signature, 1, 4, fin); if( signature[3] == 0x04 && signature[2] == 0x03 && signature[1] == 0x4b && signature[0] == 0x50 ) { int ret; unzFile unZipDir; unz_file_info unZipFileInfo; printf("Unzip: [%s] is a zip file\n", zfin); unZipDir = unzOpen(zfin); if(unZipDir == NULL) { printf("Unzip: can't uncompress %s\n", zfin); return NULL; } ret = unzGoToFirstFile(unZipDir); while(ret == UNZ_OK) { ret = unzGetCurrentFileInfo(unZipDir, &unZipFileInfo, szCurrentFile, MAX_PATH, NULL, 0, NULL, 0); if(ret) { printf("Unzip: get file info error\n"); return NULL; } else if(szCurrentFile[strlen(szCurrentFile)-1] == '/') { printf("[./%s]\n", szCurrentFile); mkdir(szCurrentFile); } else { size = unZipFileInfo.uncompressed_size; printf(" ./%s -> %d\n", szCurrentFile, size); buf = malloc(size); assert(buf != NULL); if (UNZ_OK == unzOpenCurrentFile(unZipDir)) { nReadBytes = unzReadCurrentFile(unZipDir, buf, size); unzCloseCurrentFile(unZipDir); fout = fopen(szCurrentFile, "wb"); assert(fout != NULL); fwrite(buf, size, 1, fout); fclose(fout); } free(buf); } ret = unzGoToNextFile(unZipDir); } unzClose(unZipDir); } else if(signature[1] == 0x8b && signature[0] == 0x1f) { int len; char * ptr; gzFile gzfile; printf("Unzip: [%s] is a gzip file\n", zfin); gzfile = gzopen(zfin, "rb"); if(gzfile == NULL) { printf("Unzip: can't uncompress %s\n", zfin); return NULL; } buf = malloc(MAX_BUFFER); assert(buf != NULL); size = gzread(gzfile, buf, MAX_BUFFER); len = strlen(zfin); ptr = strrchr(zfin, '.'); if(ptr) len = ptr - zfin; else len -= 1; strncpy(szCurrentFile, zfin, len); printf("%s -> %d\n", szCurrentFile, size); fout = fopen(szCurrentFile, "wb"); assert(fout != NULL); fwrite(buf, 1, size, fout); fclose(fout); gzclose(gzfile); free(buf); } else { printf("Unzip: file [%s] is not a zip or gzip file\n", zfin); } return szCurrentFile; }
使用范例(安装之后):zlib.c #include <stdio.h> #include <zlib.h> #include <string.h> /** * compile - gcc zlib.c -L /usr/local/lib -static -lz * 测试: * .gz文件 * 说明: * 运用过程中出现了诸多问题: * 问题1.写入字符串的时候用sizeof求的大小写入,文件出错! * 问题2.写入完成,未关闭,文件出错! * 注意: * 原来一直以为一个压缩文件可以存在多个文件,但是发现库里面并没由涉及到相关读取多个文件的函数!于是疑惑了很久? * 最后选择了几个文件,用鼠标右键->compress发现多个文件的情况下,没有.gz选项!唉,原来这种压缩文件只能放一个文件啊 啊 啊 啊! * * 后来发现可以压缩为.tar.gz格式的(hl.tar.gz),也是可以的!我发现自己又想错了,唉,看来只有实践才能证明一切!而且也能打开,打开的时候才发现秘密:每一个文件内容开始都是文件名+权限+其他信息+0ustar+文件拥有者+所在组 + 内容 下一个文件同样是这样的格式;因此我们可以根据这种格式去解析出我们所有的文件,这就是解压缩的原理! * 如果是文件夹的话,读取文件的内容就是文件夹/文件名+基本信息+0ustar最后是文件夹的信息 * 文件夹/信息 + 5ustar + 文件拥有者+所在组+目录名称,这样也可以解析了? * 但是写入呢?待续.... */ int main(int argc, const char *argv[]) { /// < 字符串压缩 #if 0 char src[] = "kittttytt aaaaaaa bbbbbbb cccccccc"; uLongf srcLen = sizeof(src); char dest[1024] = {0}; uLongf destLen = sizeof(dest); printf("src len is %ld %s\n", srcLen, src); //35 compress(dest, &destLen, src, srcLen); printf("compress len is %ld, %s\n", destLen, dest); //27 memset(src, 0, srcLen); uncompress(src, &srcLen, dest, destLen); printf("uncomparesss len is %ld, %s\n", srcLen, src); //35 #endif /// < 写入压缩文件 #if 0 /// < 写入文件 gzFile gzf = gzopen("Makefile.gz", "w9"); char buf[] = "----888888-----"; gzwrite(gzf, buf, strlen(buf)); /// < 没反映,咋回事?操,原来我以读的方式。去? 这里多一个字符都会造成文件损坏!!! gzclose(gzf); /// < 记得关闭,如果不关闭,鼠标打开文件的时候是损坏的文件!我也是折腾了很久. return 0; #endif /// < 做什么的? //printf("%d\n", gzdirect(gzf)); /// < 读取压缩文件 #if 0 /// < 压缩文件读 gzFile gzf = gzopen("Makefile.gz", "rb"); /// < 读取.gz文件内容中的1024字节 char buffer[1024]; memset(buffer, 0, 1024); gzread(gzf, buffer, 1024); printf("%s\n", buffer); /// < 定位到开始 gzseek(gzf, 0, SEEK_SET); gzclose(gzf); #endif int c; gzFile gzf = gzopen("ss.tar.gz", "rb"); while ( (c = gzgetc(gzf)) != EOF) putchar(c); gzclose(gzf); /**结果: * hl/find0000664000175000017500000000006512025551320010112 0ustar * hlhlfind /usr/include -name "*.h"|xargs grep sockaddr_in * hl/Makefile0000664000175000017500000000005012031175655010713 0ustar * hlhl/usr/share/qt4/mkspecs/common/g++.conf: * hl/0000775000175000017500000000000012031177505007254 5ustar hlhlhl */ return 0; }
xmlChar * swish_io_slurp_gzfile_len( xmlChar *filename, off_t *flen, boolean binmode ) { off_t bytes_read, buffer_len; int ret; gzFile fh; xmlChar *buffer; unsigned int buf_size; int compression_rate = 3; /* seems about right */ buf_size = sizeof(xmlChar)*(*flen)*compression_rate; buffer = swish_xmalloc(buf_size); buffer_len = 0; fh = gzopen((char*)filename, "r"); if (fh == NULL) { SWISH_CROAK("Failed to open file '%s' for read: %s", filename, strerror(errno)); } while ((bytes_read = gzread(fh, buffer, buf_size)) != 0) { if (bytes_read == -1) { SWISH_CROAK("Error reading gzipped file '%s': %s", filename, strerror(errno)); } if (SWISH_DEBUG & SWISH_DEBUG_IO) { SWISH_DEBUG_MSG("Read %d bytes from %s", bytes_read, filename); } if (bytes_read < buf_size) { if (SWISH_DEBUG & SWISH_DEBUG_IO) { SWISH_DEBUG_MSG("Read to end of file"); } buffer_len = bytes_read; break; } buf_size *= compression_rate; buffer = swish_xrealloc(buffer, buf_size); if (SWISH_DEBUG & SWISH_DEBUG_IO) { SWISH_DEBUG_MSG("grew buffer to %d", buf_size); } buffer_len = bytes_read; ret = gzrewind(fh); if (SWISH_DEBUG & SWISH_DEBUG_IO) { SWISH_DEBUG_MSG("gzrewind ret = %d", ret); } } ret = gzclose(fh); // TODO check for err? buffer[buffer_len] = '\0'; if (!binmode) { no_nulls(filename, buffer, (long)buffer_len); } if (SWISH_DEBUG & SWISH_DEBUG_IO) { SWISH_DEBUG_MSG("slurped gzipped file '%s' buffer_len=%d buf_size=%d orig flen=%d", filename, buffer_len, buf_size, *flen); } /* set the flen pointer to the actual length */ *flen = buffer_len; return buffer; }
// Donuts are a tasty treat and delicious with powdered sugar. void MDFNMOV_AddJoy(void *donutdata, uint32 donutlen) { gzFile fp; if(!current) return; /* Not playback nor recording. */ if(current < 0) /* Playback */ { int t; fp = slots[-1 - current]; while((t = gzgetc(fp)) >= 0 && t) { if(t == MDFNNPCMD_LOADSTATE) { uint32 len; StateMem sm; len = gzgetc(fp); len |= gzgetc(fp) << 8; len |= gzgetc(fp) << 16; len |= gzgetc(fp) << 24; if(len >= 5 * 1024 * 1024) // A sanity limit of 5MiB { StopPlayback(); return; } memset(&sm, 0, sizeof(StateMem)); sm.len = len; sm.data = (uint8 *)malloc(len); if(gzread(fp, sm.data, len) != len) { StopPlayback(); return; } if(!MDFNSS_LoadSM(&sm, 0, 0)) { StopPlayback(); return; } } else MDFN_DoSimpleCommand(t); } if(t < 0) { StopPlayback(); return; } if(gzread(fp, donutdata, donutlen) != donutlen) { StopPlayback(); return; } } else /* Recording */ { if(MDFN_StateEvilIsRunning()) { smem_putc(&RewindBuffer, 0); smem_write(&RewindBuffer, donutdata, donutlen); } else { fp = slots[current - 1]; gzputc(fp, 0); gzwrite(fp, donutdata, donutlen); } } }
int tar_extract_all( TAR *t, char *prefix ) { union tar_buffer buffer; int len, err, getheader = 1, remaining = 0; FILE *outfile = NULL; char fname[BLOCKSIZE + PATH_MAX]; while( 1 ) { len = gzread( *t, &buffer, BLOCKSIZE ); if( len < 0 ) { fprintf( stderr, "%s\n", gzerror(*t, &err) ); } /* * Always expect complete blocks to process * the tar information. */ if( len != 0 && len != BLOCKSIZE ) { fprintf( stderr, "gzread: incomplete block read\n" ); return -1; } /* * If we have to get a tar header */ if( getheader == 1 ) { /* * If we met the end of the tar * or the end-of-tar block, we are done */ if( (len == 0) || (buffer.header.name[0] == 0) ) { break; } sprintf( fname, "%s/%s", prefix, buffer.header.name ); /* Check magic value in header */ if( strncmp( buffer.header.magic, "GNUtar", 6 ) && strncmp( buffer.header.magic, "ustar", 5 ) ) { //fprintf(stderr, "not a tar file\n"); return -1; } switch( buffer.header.typeflag ) { case DIRTYPE: makedir( fname ); break; case REGTYPE: case AREGTYPE: remaining = getoct( buffer.header.size, 12 ); if( remaining ) { outfile = fopen( fname, "wb" ); if( outfile == NULL ) { /* try creating directory */ char *p = strrchr( fname, '/' ); if( p != NULL ) { *p = '\0'; makedir( fname ); *p = '/'; outfile = fopen( fname, "wb" ); if( !outfile ) { fprintf( stderr, "tar couldn't create %s\n", fname ); } } } } else outfile = NULL; /* * could have no contents */ getheader = (remaining) ? 0 : 1; break; default: break; } } else { unsigned int bytes = (remaining > BLOCKSIZE)?BLOCKSIZE:remaining; if( outfile != NULL ) { if( fwrite( &buffer, sizeof(char), bytes, outfile ) != bytes ) { fprintf( stderr, "error writing %s skipping...\n", fname ); fclose( outfile ); outfile = NULL; unlink( fname ); } } remaining -= bytes; if( remaining == 0 ) { getheader = 1; if( outfile != NULL ) { fclose(outfile); outfile = NULL; } } } } return 0; }
/** * Read file from disk into allocated buffer and return the buffer * or NULL for error. If pFileSize is non-NULL, read file size * is set to that. */ Uint8 *File_Read(const char *pszFileName, long *pFileSize, const char * const ppszExts[]) { char *filepath = NULL; Uint8 *pFile = NULL; long FileSize = 0; /* Does the file exist? If not, see if can scan for other extensions and try these */ if (!File_Exists(pszFileName) && ppszExts) { /* Try other extensions, if succeeds, returns correct one */ filepath = File_FindPossibleExtFileName(pszFileName, ppszExts); } if (!filepath) filepath = strdup(pszFileName); /* Is it a gzipped file? */ if (File_DoesFileExtensionMatch(filepath, ".gz")) { gzFile hGzFile; /* Open and read gzipped file */ hGzFile = gzopen(filepath, "rb"); if (hGzFile != NULL) { /* Find size of file: */ do { /* Seek through the file until we hit the end... */ gzseek(hGzFile, 1024, SEEK_CUR); } while (!gzeof(hGzFile)); FileSize = gztell(hGzFile); gzrewind(hGzFile); /* Read in... */ pFile = malloc(FileSize); if (pFile) FileSize = gzread(hGzFile, pFile, FileSize); gzclose(hGzFile); } } else if (File_DoesFileExtensionMatch(filepath, ".zip")) { /* It is a .ZIP file! -> Try to load the first file in the archive */ pFile = ZIP_ReadFirstFile(filepath, &FileSize, ppszExts); } else /* It is a normal file */ { FILE *hDiskFile; /* Open and read normal file */ hDiskFile = fopen(filepath, "rb"); if (hDiskFile != NULL) { /* Find size of file: */ fseek(hDiskFile, 0, SEEK_END); FileSize = ftell(hDiskFile); fseek(hDiskFile, 0, SEEK_SET); /* Read in... */ pFile = malloc(FileSize); if (pFile) FileSize = fread(pFile, 1, FileSize, hDiskFile); fclose(hDiskFile); } } free(filepath); /* Store size of file we read in (or 0 if failed) */ if (pFileSize) *pFileSize = FileSize; return pFile; /* Return to where read in/allocated */ }
TInt CTestlibz::libzgzio() { __UHEAP_MARK; #ifdef NO_GZCOMPRESS INFO_PRINT1(_L("NO_GZCOMPRESS -- gz* functions cannot compress")); return KErrNone; #else const char Buffer[] = "Symbian Libz!"; Byte uncompr[15]; char fname[] = "C:\\Libz\\Test1\\bye.gz"; int err = 0; int len; gzFile file; z_off_t pos; err = iRfile.Create(iRfs, KGZFILE, EFileShareAny); if( err != KErrNone && err != KErrAlreadyExists ) { INFO_PRINTF1(_L("File create successfully\n")); return KErrGeneral; } len = (int)strlen(Buffer)+1; //-------------gzopen()------------------ INFO_PRINTF1(_L("gzopen()\n")); file = gzopen(fname, "wb"); if (file == NULL) { INFO_PRINTF1(_L("gzopen error")); return KErrGeneral; } //-------------gzputc() ------------ INFO_PRINTF1(_L("gputc()\n")); gzputc(file, 'S'); if (gzputs(file, "ymbian") != 6) { INFO_PRINTF2(_L("gzputs err: %s\n"), gzerror(file, &err)); return KErrGeneral; } //-------------gzprintf() ------------ INFO_PRINTF1(_L("gzprintf()\n")); if (gzprintf(file, " %s!", "Libz") != 6) { INFO_PRINTF2(_L("gzprintf err: %s\n"), gzerror(file, &err)); return KErrGeneral; } //-------------gzseek() ------------ INFO_PRINTF1(_L("gzseek()\n")); if(gzseek(file, 1L, SEEK_CUR) != 14){ /* add one zero byte */ INFO_PRINTF2(_L("gzseek err: %s\n"), gzerror(file, &err)); return KErrGeneral; } //-------------gzclose() ------------ INFO_PRINTF1(_L("gzclose()\n")); if(gzclose(file) == Z_ERRNO){ INFO_PRINTF2(_L("gzclose err: %s\n"), gzerror(file, &err)); return KErrGeneral; } //-------------gzopen()------------------ INFO_PRINTF1(_L("gzopen()\n")); file = gzopen(fname, "rb"); if (file == NULL) { INFO_PRINTF1(_L("gzopen error\n")); return KErrGeneral; } strcpy((char*)uncompr, "garbage"); //-------------gzread()------------------ INFO_PRINTF1(_L("gzread()\n")); if (gzread(file, uncompr, sizeof(uncompr)) != len) { INFO_PRINTF2(_L("gzread err: %s\n"), gzerror(file, &err)); return KErrGeneral; } if (strcmp((char*)uncompr, Buffer)) { INFO_PRINTF2(_L("bad gzread: %s\n"), (char*)uncompr); return KErrGeneral; } //-------------gzseek() & gztell()----------------- INFO_PRINTF1(_L("gzseek & gztell()\n")); pos = gzseek(file, -7L, SEEK_CUR); if (gztell(file) != pos || pos != 7) { INFO_PRINTF3(_L("gzseek error, pos=%ld, gztell=%ld\n"), (long)pos, (long)gztell(file)); return KErrGeneral; } //-------------gzgetc()------------------ INFO_PRINTF1(_L("gzgetc()\n")); if (gzgetc(file) != ' ') { INFO_PRINTF1(_L("gzgetc error")); return KErrGeneral; } //-------------gzungetc()------------------ INFO_PRINTF1(_L("gzungetc\n")); if (gzungetc(' ', file) != ' ') { INFO_PRINTF1(_L("gzungetc error\n")); return KErrGeneral; } //-------------gzgets()------------------ INFO_PRINTF1(_L("gzgets()\n")); gzgets(file, (char*)uncompr, sizeof(uncompr)); if (strlen((char*)uncompr) != 6) { /* " Libz!" */ INFO_PRINTF2(_L("gzgets err after gzseek: %s\n"), gzerror(file, &err)); return KErrGeneral; } if (strcmp((char*)uncompr, Buffer + 7)) { INFO_PRINTF1(_L("bad gzgets after gzseek\n")); return KErrGeneral; } //-------------gzclose() ------------ if(gzclose(file) == Z_ERRNO) { INFO_PRINTF2(_L("gzclose err: %s\n"), gzerror(file, &err)); return KErrGeneral; } #endif __UHEAP_MARKEND; return KErrNone; }
// read full kernel into obuf[], gzip-decompress into ibuf[], // return decompressed size int PackVmlinuzI386::decompressKernel() { // read whole kernel image obuf.alloc(file_size); fi->seek(0, SEEK_SET); fi->readx(obuf, file_size); { const upx_byte *base = NULL; unsigned relocated = 0; // See startup_32: in linux/arch/i386/boot/compressed/head.S const upx_byte *p = &obuf[setup_size]; unsigned cpa_0 = 0; unsigned cpa_1 = 0; int j; if (0x205<=h.version) { cpa_0 = h.kernel_alignment; cpa_1 = 0u - cpa_0; } else for ((p = &obuf[setup_size]), (j= 0); j < 0x200; ++j, ++p) { if (0==memcmp("\x89\xeb\x81\xc3", p, 4) && 0==memcmp("\x81\xe3", 8+ p, 2)) { // movl %ebp,%ebx // addl $imm.w,%ebx // andl $imm.w,%ebx cpa_0 = 1+ get_te32( 4+ p); cpa_1 = get_te32(10+ p); break; } } for ((p = &obuf[setup_size]), (j= 0); j < 0x200; ++j, ++p) { if (0==memcmp("\x8d\x83", p, 2) // leal d32(%ebx),%eax && 0==memcmp("\xff\xe0", 6+ p, 2) // jmp *%eax ) { relocated = get_te32(2+ p); } if (0==memcmp("\xE8\x00\x00\x00\x00\x5D", p, 6)) { // "call 1f; 1f: pop %ebp" determines actual execution address. // linux-2.6.21 (spring 2007) and later; upx stub needs work // unless LOAD_PHYSICAL_ADDR is known. // Allowed code is: linux-2.6.23/arch/x86/head_32.S 2008-01-01 // call 1f // 1: popl %ebp // subl $1b, %ebp # 32-bit immediate // movl $LOAD_PHYSICAL_ADDR, %ebx // if (0==memcmp("\x81\xed", 6+ p, 2) // subl $imm.w,%ebp && 0==memcmp("\xbb", 12+ p, 1) ) { // movl $imm.w,%ebx physical_start = get_te32(13+ p); } else if (0==memcmp("\x81\xed", 6+ p, 2) // subl $imm.w,%ebp && is_pow2(cpa_0) && (0u-cpa_0)==cpa_1) { base = (5+ p) - get_te32(8+ p); config_physical_align = cpa_0; } else { throwCantPack("Unrecognized relocatable kernel"); } } // Find "ljmp $__BOOT_CS,$__PHYSICAL_START" if any. if (0==memcmp("\xEA\x00\x00", p, 3) && 0==(0xf & p[3]) && 0==p[4]) { /* whole megabyte < 16 MiB */ physical_start = get_te32(1+ p); break; } } if (base && relocated) { p = base + relocated; for (j = 0; j < 0x200; ++j, ++p) { if (0==memcmp("\x01\x9c\x0b", p, 3) // addl %ebx,d32(%ebx,%ecx) ) { page_offset = 0u - get_te32(3+ p); } if (0==memcmp("\x89\xeb", p, 2) // movl %ebp,%ebx && 0==memcmp("\x81\xeb", 2+ p, 2) // subl $imm32,%ebx ) { physical_start = get_te32(4+ p); } } } } checkAlreadyPacked(obuf + setup_size, UPX_MIN(file_size - setup_size, (off_t)1024)); int gzoff = setup_size; if (0x208<=h.version) { gzoff += h.payload_offset; } for (; gzoff < file_size; gzoff++) { // find gzip header (2 bytes magic + 1 byte method "deflated") int off = find(obuf + gzoff, file_size - gzoff, "\x1F\x8B\x08", 3); if (off < 0) break; gzoff += off; const int gzlen = (h.version < 0x208) ? (file_size - gzoff) : h.payload_length; if (gzlen < 256) break; // check gzip flag byte unsigned char flags = obuf[gzoff + 3]; if ((flags & 0xe0) != 0) // reserved bits set continue; //printf("found gzip header at offset %d\n", gzoff); // try to decompress int klen; int fd; off_t fd_pos; for (;;) { klen = -1; fd = -1; fd_pos = -1; // open fi->seek(gzoff, SEEK_SET); fd = dup(fi->getFd()); if (fd < 0) break; gzFile zf = gzdopen(fd, "rb"); if (zf == NULL) break; // estimate gzip-decompressed kernel size & alloc buffer if (ibuf.getSize() == 0) ibuf.alloc(gzlen * 3); // decompress klen = gzread(zf, ibuf, ibuf.getSize()); fd_pos = lseek(fd, 0, SEEK_CUR); gzclose(zf); fd = -1; if (klen != (int)ibuf.getSize()) break; // realloc and try again unsigned s = ibuf.getSize(); ibuf.dealloc(); ibuf.alloc(3 * s / 2); } if (fd >= 0) (void) close(fd); if (klen <= 0) continue; if (klen <= gzlen) continue; if (0x208<=h.version && 0==memcmp("\177ELF", ibuf, 4)) { // Full ELF in theory; for now, try to handle as .bin at physical_start. // Check for PT_LOAD.p_paddr being ascending and adjacent. Elf_LE32_Ehdr const *const ehdr = (Elf_LE32_Ehdr const *)(void const *)ibuf; Elf_LE32_Phdr const *phdr = (Elf_LE32_Phdr const *)(ehdr->e_phoff + (char const *)ehdr); Elf_LE32_Shdr const *shdr = (Elf_LE32_Shdr const *)(ehdr->e_shoff + (char const *)ehdr); unsigned hi_paddr = 0, lo_paddr = 0; unsigned delta_off = 0; for (unsigned j=0; j < ehdr->e_phnum; ++j, ++phdr) { if (phdr->PT_LOAD==phdr->p_type) { unsigned step = (hi_paddr + phdr->p_align - 1) & ~(phdr->p_align - 1); if (0==hi_paddr) { // first PT_LOAD if (physical_start!=phdr->p_paddr) { return 0; } delta_off = phdr->p_paddr - phdr->p_offset; lo_paddr = phdr->p_paddr; hi_paddr = phdr->p_filesz + phdr->p_paddr; } else if (step==phdr->p_paddr && delta_off==(phdr->p_paddr - phdr->p_offset)) { hi_paddr = phdr->p_filesz + phdr->p_paddr; } else { return 0; // Not equivalent to a .bin. Too complex for now. } } } // FIXME: ascending order is only a convention; might need sorting. for (unsigned j=1; j < ehdr->e_shnum; ++j) { if (shdr->SHT_PROGBITS==shdr->sh_type) { // SHT_REL might be intermixed if (shdr->SHF_EXECINSTR & shdr[j].sh_flags) { filter_len += shdr[j].sh_size; // FIXME: include sh_addralign } else { break; } } } memmove(ibuf, (lo_paddr - delta_off) + ibuf, hi_paddr - lo_paddr); // FIXME: set_size // FIXME: .bss ? Apparently handled by head.S } if (opt->force > 0) return klen; // some checks if (fd_pos != file_size) { //printf("fd_pos: %ld, file_size: %ld\n", (long)fd_pos, (long)file_size); // linux-2.6.21.5/arch/i386/boot/compressed/vmlinux.lds // puts .data.compressed ahead of .text, .rodata, etc; // so piggy.o need not be last in bzImage. Alas. //throwCantPack("trailing bytes after kernel image; use option '-f' to force packing"); } // see /usr/src/linux/arch/i386/kernel/head.S // 2.4.x: [cli;] cld; mov $...,%eax if (memcmp(ibuf, "\xFC\xB8", 2) == 0) goto head_ok; if (memcmp(ibuf, "\xFA\xFC\xB8", 3) == 0) goto head_ok; // 2.6.21.5 CONFIG_PARAVIRT mov %cs,%eax; test $3,%eax; jne ...; if (memcmp(ibuf, "\x8c\xc8\xa9\x03\x00\x00\x00\x0f\x85", 9) == 0) goto head_ok; if (memcmp(ibuf, "\x8c\xc8\xa8\x03\x0f\x85", 6) == 0) goto head_ok; // 2.6.x: [cli;] cld; lgdt ... if (memcmp(ibuf, "\xFC\x0F\x01", 3) == 0) goto head_ok; if (memcmp(ibuf, "\xFA\xFC\x0F\x01", 4) == 0) goto head_ok; // 2.6.x+grsecurity+strongswan+openwall+trustix: ljmp $0x10,... if (ibuf[0] == 0xEA && memcmp(ibuf+5, "\x10\x00", 2) == 0) goto head_ok; // x86_64 2.6.x if (0xB8==ibuf[0] // mov $...,%eax && 0x8E==ibuf[5] && 0xD8==ibuf[6] // mov %eax,%ds && 0x0F==ibuf[7] && 0x01==ibuf[8] && 020==(070 & ibuf[9]) // lgdtl && 0xB8==ibuf[14] // mov $...,%eax && 0x0F==ibuf[19] && 0xA2==ibuf[20] // cpuid ) goto head_ok; // cmpw $0x207,0x206(%esi) Debian vmlinuz-2.6.24-12-generic if (0==memcmp("\x66\x81\xbe\x06\x02\x00\x00\x07\x02", ibuf, 9)) goto head_ok; // testb $0x40,0x211(%esi) Fedora vmlinuz-2.6.25-0.218.rc8.git7.fc9.i686 if (0==memcmp("\xf6\x86\x11\x02\x00\x00\x40", ibuf, 7)) goto head_ok; // rex.W prefix for x86_64 if (0x48==ibuf[0]) throwCantPack("x86_64 bzImage is not yet supported"); throwCantPack("unrecognized kernel architecture; use option '-f' to force packing"); head_ok: // FIXME: more checks for special magic bytes in ibuf ??? // FIXME: more checks for kernel architecture ??? return klen; } return 0; }
static int cli_untgz(int fd, const char *destdir) { char *path, osize[13], name[101], type; char block[TAR_BLOCKSIZE]; int nbytes, nread, nwritten, in_block = 0, fdd = -1; unsigned int size, pathlen = strlen(destdir) + 100 + 5; FILE *outfile = NULL; STATBUF foo; gzFile infile = NULL; cli_dbgmsg("in cli_untgz()\n"); if((fdd = dup(fd)) == -1) { cli_errmsg("cli_untgz: Can't duplicate descriptor %d\n", fd); return -1; } if((infile = gzdopen(fdd, "rb")) == NULL) { cli_errmsg("cli_untgz: Can't gzdopen() descriptor %d, errno = %d\n", fdd, errno); if(FSTAT(fdd, &foo) == 0) close(fdd); return -1; } path = (char *) cli_calloc(sizeof(char), pathlen); if(!path) { cli_errmsg("cli_untgz: Can't allocate memory for path\n"); cli_untgz_cleanup(NULL, infile, NULL, fdd); return -1; } while(1) { nread = gzread(infile, block, TAR_BLOCKSIZE); if(!in_block && !nread) break; if(nread != TAR_BLOCKSIZE) { cli_errmsg("cli_untgz: Incomplete block read\n"); cli_untgz_cleanup(path, infile, outfile, fdd); return -1; } if(!in_block) { if (block[0] == '\0') /* We're done */ break; strncpy(name, block, 100); name[100] = '\0'; if(strchr(name, '/')) { cli_errmsg("cli_untgz: Slash separators are not allowed in CVD\n"); cli_untgz_cleanup(path, infile, outfile, fdd); return -1; } snprintf(path, pathlen, "%s"PATHSEP"%s", destdir, name); cli_dbgmsg("cli_untgz: Unpacking %s\n", path); type = block[156]; switch(type) { case '0': case '\0': break; case '5': cli_errmsg("cli_untgz: Directories are not supported in CVD\n"); cli_untgz_cleanup(path, infile, outfile, fdd); return -1; default: cli_errmsg("cli_untgz: Unknown type flag '%c'\n", type); cli_untgz_cleanup(path, infile, outfile, fdd); return -1; } in_block = 1; if(outfile) { if(fclose(outfile)) { cli_errmsg("cli_untgz: Cannot close file %s\n", path); outfile = NULL; cli_untgz_cleanup(path, infile, outfile, fdd); return -1; } outfile = NULL; } if(!(outfile = fopen(path, "wb"))) { cli_errmsg("cli_untgz: Cannot create file %s\n", path); cli_untgz_cleanup(path, infile, outfile, fdd); return -1; } strncpy(osize, block + 124, 12); osize[12] = '\0'; if((sscanf(osize, "%o", &size)) == 0) { cli_errmsg("cli_untgz: Invalid size in header\n"); cli_untgz_cleanup(path, infile, outfile, fdd); return -1; } } else { /* write or continue writing file contents */ nbytes = size > TAR_BLOCKSIZE ? TAR_BLOCKSIZE : size; nwritten = fwrite(block, 1, nbytes, outfile); if(nwritten != nbytes) { cli_errmsg("cli_untgz: Wrote %d instead of %d (%s)\n", nwritten, nbytes, path); cli_untgz_cleanup(path, infile, outfile, fdd); return -1; } size -= nbytes; if(size == 0) in_block = 0; } } cli_untgz_cleanup(path, infile, outfile, fdd); return 0; }
bool Data::load(const char * file, char * prefix, bool * compressed) { char fullPath[1024]; strcpy(fullPath, file); //scan for actual filename, rather than full path char * filename = fullPath; while (true) { char * x = strstr(filename, DIR_SYMBOL); if (x == '\0') break; else { filename = x; filename++; } } char * dimP, * typeP, *gzP; dimP = strstr(filename, "."); *dimP = '\0'; dimP++; typeP = strstr(dimP, "."); *typeP = '\0'; typeP++; gzP = strstr(typeP, "."); if (gzP) { *gzP = '\0'; gzP++; *compressed = true; } char *xSize, *ySize, *zSize; xSize = dimP; ySize = strstr(dimP, "x"); *ySize = '\0'; ySize++; zSize = strstr(ySize, "x"); *zSize = '\0'; zSize++; size[0] = atoi(xSize); size[1] = atoi(ySize); size[2] = atoi(zSize); std::clog << "Loading " << filename << " with dimensions " << size[0] << " x " << size[1] << " x " << size[2] << ", of type " << typeP << std::endl; strcpy(prefix, fullPath); totalSize = size[0] * size[1] * size[2]; std::ifstream infile; #if USE_ZLIB gzFile zinfile; zinfile = gzopen(file, "rb"); if (!zinfile) { std::cerr << "could not open" << filename << std::endl; return false; } #else infile.open(file, std::ios::in); if (!infile) { std::clog << "could not open " << filename << std::endl; return false; } #endif data = new DataType[totalSize]; if (strcmp(typeP, "uint8") == 0) { unsigned char * charData = new unsigned char [totalSize]; #if USE_ZLIB gzread(zinfile, reinterpret_cast<char*> (charData), totalSize); #else infile.read(reinterpret_cast<char*> (charData), totalSize); #endif for (uint i = 0; i < totalSize; i++) { data[i] = static_cast<DataType> (charData[i]); } delete[] charData; } else if (strcmp(typeP, "uint16") == 0) { unsigned short int * intData = new unsigned short int [totalSize]; #if USE_ZLIB gzread(zinfile, reinterpret_cast<char*> (intData), totalSize * 2); #else infile.read(reinterpret_cast<char*> (intData), totalSize * 2); #endif for (uint i = 0; i < totalSize; i++) { data[i] = static_cast<DataType> (intData[i]); } delete[] intData; } else if (strcmp(typeP, "float") == 0) { float * floatData = new float [totalSize]; #if USE_ZLIB gzread(zinfile, reinterpret_cast<char*> (floatData), totalSize * 4); #else infile.read(reinterpret_cast<char*> (floatData), totalSize * 4); #endif for (uint i = 0; i < totalSize; i++) { data[i] = static_cast<DataType> (floatData[i]); } delete[] floatData; } else if (strcmp(typeP, "double") == 0) { double * doubleData = new double [totalSize]; #if USE_ZLIB gzread(zinfile, reinterpret_cast<char*> (doubleData), totalSize * 8); #else infile.read(reinterpret_cast<char*> (doubleData), totalSize * 8); #endif for (uint i = 0; i < totalSize; i++) { data[i] = static_cast<DataType> (doubleData[i]); } delete[] doubleData; } else { std::cerr << "unrecognized type : " << typeP << std::endl; return false; } maxValue = minValue = data[0]; for (uint i = 0; i < totalSize; i++) { if (data[i] > maxValue) maxValue = data[i]; if (data[i] < minValue) minValue = data[i]; } std::clog << "max value was " << (int) maxValue << " and min value was " << (int) minValue << std::endl; #if USE_ZLIB gzclose(zinfile); #else infile.close(); #endif return true; }