// Needed for quicksort; partitions the part of the array between the first two params // by moving everything less than the third param before the pivot, and those greater // after it, and returns the final position of the pivot element long partition(long left, long right, long pivotIndex) { // Store rank value (what we're sorting by) at the pivot point long pivRank = ranks[pivotIndex]; // Move pivots to the end of their array swapLong(ranks, pivotIndex, right); swapChar(sentences, pivotIndex, right); // Move things around based on how they compare to the pivot point long storeIndex = left; long i; for (i = left; i < right ;i++) { if (ranks[i] > pivRank) { swapLong(ranks, i, storeIndex); swapChar(sentences, i, storeIndex); storeIndex++; } } // Move pivots to their final places swapLong(ranks, storeIndex, right); swapChar(sentences, storeIndex, right); return storeIndex; }
/* * adfReadGenBlock * */ RETCODE adfReadGenBlock(struct Volume *vol, SECTNUM nSect, struct GenBlock *block) { unsigned char buf[LOGICAL_BLOCK_SIZE]; int len; char name[MAXNAMELEN+1]; if (adfReadBlock(vol, nSect, buf)!=RC_OK) return RC_ERROR; block->type =(int) swapLong(buf); block->secType =(int) swapLong(buf+vol->blockSize-4); block->sect = nSect; block->name = NULL; if (block->type==T_HEADER) { switch(block->secType) { case ST_FILE: case ST_DIR: case ST_LFILE: case ST_LDIR: len = min(MAXNAMELEN, buf[vol->blockSize-80]); strncpy(name, buf+vol->blockSize-79, len); name[len] = '\0'; block->name = strdup(name); block->parent = swapLong(buf+vol->blockSize-12); break; case ST_ROOT: break; default: ; } } return RC_OK; }
/* * adfMountHdFile * */ RETCODE adfMountHdFile(struct Device *dev) { struct Volume* vol; uint8_t buf[512]; int32_t size; BOOL found; dev->devType = DEVTYPE_HARDFILE; dev->nVol = 0; dev->volList = (struct Volume**)malloc(sizeof(struct Volume*)); if (!dev->volList) { (*adfEnv.eFct)("adfMountHdFile : malloc"); return RC_ERROR; } vol=(struct Volume*)malloc(sizeof(struct Volume)); if (!vol) { (*adfEnv.eFct)("adfMountHdFile : malloc"); return RC_ERROR; } dev->volList[0] = vol; dev->nVol++; /* fixed by Dan, ... and by Gary */ vol->volName=NULL; dev->cylinders = dev->size/512; dev->heads = 1; dev->sectors = 1; vol->firstBlock = 0; size = dev->size + 512-(dev->size%512); /*printf("size=%ld\n",size);*/ vol->rootBlock = (size/512)/2; /*printf("root=%ld\n",vol->rootBlock);*/ do { adfReadDumpSector(dev, vol->rootBlock, 512, buf); found = swapLong(buf)==T_HEADER && swapLong(buf+508)==ST_ROOT; if (!found) (vol->rootBlock)--; }while (vol->rootBlock>1 && !found); if (vol->rootBlock==1) { (*adfEnv.eFct)("adfMountHdFile : rootblock not found"); return RC_ERROR; } vol->lastBlock = vol->rootBlock*2 - 1 ; return RC_OK; }
/* * adfGetCacheEntry * * Returns a cache entry, starting from the offset p (the index into records[]) * This offset is updated to the end of the returned entry. */ void adfGetCacheEntry(struct bDirCacheBlock *dirc, int *p, struct CacheEntry *cEntry) { int ptr; ptr = *p; /*printf("p=%d\n",ptr);*/ #ifdef LITT_ENDIAN cEntry->header = swapLong(dirc->records+ptr); cEntry->size = swapLong(dirc->records+ptr+4); cEntry->protect = swapLong(dirc->records+ptr+8); cEntry->days = swapShort(dirc->records+ptr+16); cEntry->mins = swapShort(dirc->records+ptr+18); cEntry->ticks = swapShort(dirc->records+ptr+20); #else cEntry->header = Long(dirc->records+ptr); cEntry->size = Long(dirc->records+ptr+4); cEntry->protect = Long(dirc->records+ptr+8); cEntry->days = Short(dirc->records+ptr+16); cEntry->mins = Short(dirc->records+ptr+18); cEntry->ticks = Short(dirc->records+ptr+20); #endif cEntry->type =(signed char) dirc->records[ptr+22]; cEntry->nLen = dirc->records[ptr+23]; /* cEntry->name = (char*)calloc(1,sizeof(char)*(cEntry->nLen+1)); if (!cEntry->name) return; */ memcpy(cEntry->name, dirc->records+ptr+24, cEntry->nLen); cEntry->name[(int)(cEntry->nLen)]='\0'; cEntry->cLen = dirc->records[ptr+24+cEntry->nLen]; if (cEntry->cLen>0) { /* cEntry->comm =(char*)calloc(1,sizeof(char)*(cEntry->cLen+1)); if (!cEntry->comm) { free( cEntry->name ); cEntry->name=NULL; return; } */ memcpy(cEntry->comm,dirc->records+ptr+24+cEntry->nLen+1,cEntry->cLen); } cEntry->comm[(int)(cEntry->cLen)]='\0'; /*printf("cEntry->nLen %d cEntry->cLen %d %s\n",cEntry->nLen,cEntry->cLen,cEntry->name);*/ *p = ptr+24+cEntry->nLen+1+cEntry->cLen; /* the starting offset of each record must be even (68000 constraint) */ if ((*p%2)!=0) *p=(*p)+1; }
Boolean CWad::init() { int i; SInt32 length; string package, file, extension; string entryName; _fileStream->OpenDataFork(fsRdPerm); _fileStream->SetMarker(0, streamFrom_Start); length = sizeof(wadinfo_t); _fileStream->GetBytes(&_wadheader, length); _diroffset = swapLong(_wadheader.infotableofs); _fileStream->SetMarker(_diroffset, streamFrom_Start); _numlumps = swapLong(_wadheader.numlumps); _dirsize = _numlumps * sizeof(filelump_t); _wad_entries = (filelump_t *)CMemoryTracker::safeAlloc(_numlumps, sizeof(filelump_t), "_wad_entries"); if (!_wad_entries) goto fail; _fileStream->GetBytes(_wad_entries, _dirsize); for (i=0;i<_numlumps;i++) { // extract name char s[9]; int j; for(j = 0; j < 7; ++j) { s[j] = _wad_entries[i].name[j]; if (!s[j]) break; } s[++j] = 0; entryName = (const char*)s; this->addFileWithName(i, entryName.c_str()); } return true; fail: dprintf("could not load wad entries\n"); return false; }
// called only on the root pak CPakStream *CWad::LoadItemAtIndex(CFileArchive *inParent, string itemName, UInt32 inIndex) { // fixme assert this is the root package long offset = swapLong(_wad_entries[inIndex].filepos); long pakSize = swapLong(_wad_entries[inIndex].size); dprintf("itemName %s wad item %d offset %d size %d\n", itemName.c_str(), inIndex, offset, pakSize); _fileStream->SetMarker(offset, streamFrom_Start); Handle pakData = ::NewHandle(pakSize); ::HLock(pakData); _fileStream->GetBytes(*pakData, pakSize); ::HUnlock(pakData); return new CPakStream(inParent, itemName, inIndex, pakData); }
void SarReader::readArchive( ArchiveInfo *ai, int archive_type, unsigned int offset ) { unsigned int i; /* Read header */ for (i=0; i<offset; i++) readChar( ai->file_handle ); // for commands "ns2" and "ns3" if ( archive_type == ARCHIVE_TYPE_NS2 ) { // new archive type since NScr2.91 // - header starts with base_offset (byte-swapped), followed by // filename data - doesn't tell how many files! // - filenames are surrounded by ""s // - new NS2 filename def: "filename", length (4bytes, swapped) // - no compression type? really, no compression. // - not sure if NS2 uses key_table or not, using default funcs for now ai->base_offset = swapLong( readLong( ai->file_handle ) ); ai->base_offset += offset; // need to parse the whole header to see how many files there are ai->num_of_files = 0; long unsigned int cur_offset = offset + 4; // there's an extra byte at the end of the header, not sure what for while(1){ unsigned char ch = key_table[fgetc( ai->file_handle )]; if (ch != '"') break; cur_offset++; do cur_offset++; while( (ch = key_table[fgetc( ai->file_handle )] ) != '"' ); cur_offset += 4; readLong( ai->file_handle ); ai->num_of_files++; } ai->fi_list = new FileInfo[ ai->num_of_files ]; // now go back to the beginning and read the file info cur_offset = ai->base_offset; fseek( ai->file_handle, 4 + offset, SEEK_SET ); for ( i=0 ; i<ai->num_of_files ; i++ ){ unsigned int count = 0; //skip the beginning double-quote unsigned char ch = key_table[fgetc( ai->file_handle )]; while( (ch = key_table[fgetc( ai->file_handle )] ) != '"' ){ if ( 'a' <= ch && ch <= 'z' ) ch += 'A' - 'a'; ai->fi_list[i].name[count++] = ch; } ai->fi_list[i].name[count] = '\0'; ai->fi_list[i].compression_type = getRegisteredCompressionType( ai->fi_list[i].name ); ai->fi_list[i].offset = cur_offset; ai->fi_list[i].length = swapLong( readLong( ai->file_handle ) ); ai->fi_list[i].original_length = ai->fi_list[i].length; cur_offset += ai->fi_list[i].length; } } else { // old NSA filename def: filename, ending '\0' byte , compr-type byte, // start (4byte), length (4byte)) ai->num_of_files = readShort( ai->file_handle ); ai->fi_list = new FileInfo[ ai->num_of_files ]; ai->base_offset = readLong( ai->file_handle ); ai->base_offset += offset; for ( i=0 ; i<ai->num_of_files ; i++ ){ unsigned char ch; int count = 0; while( (ch = key_table[fgetc( ai->file_handle )] ) ){ if ( 'a' <= ch && ch <= 'z' ) ch += 'A' - 'a'; ai->fi_list[i].name[count++] = ch; } ai->fi_list[i].name[count] = ch; if ( archive_type == ARCHIVE_TYPE_NSA ) ai->fi_list[i].compression_type = readChar( ai->file_handle ); else ai->fi_list[i].compression_type = NO_COMPRESSION; ai->fi_list[i].offset = readLong( ai->file_handle ) + ai->base_offset; ai->fi_list[i].length = readLong( ai->file_handle ); if ( archive_type == ARCHIVE_TYPE_NSA ){ ai->fi_list[i].original_length = readLong( ai->file_handle ); } else{ ai->fi_list[i].original_length = ai->fi_list[i].length; } /* Registered Plugin check */ if ( ai->fi_list[i].compression_type == NO_COMPRESSION ) ai->fi_list[i].compression_type = getRegisteredCompressionType( ai->fi_list[i].name ); if ( ai->fi_list[i].compression_type == NBZ_COMPRESSION || ai->fi_list[i].compression_type == SPB_COMPRESSION ){ // Delaying checking decompressed file length to prevent // massive random access in the archives at the start-up. ai->fi_list[i].original_length = 0; } } } }
// called only on the root pak long CWad::SizeOfItemAtIndex(string itemName, UInt32 inIndex) { #pragma unused (itemName) // fixme assert this is the root package return swapLong(_wad_entries[inIndex].size); }
int LPAFreadImageAnswer(LPAF *lpaf, int current) { char *fullname, fname[100] ; FILE *fp ; IMAGE Iheader ; int i, ecode, frame, current_frame ; LP_BOX *lpb ; struct extpar *xp ; #ifdef _MSDOS long *parms ; #else int *parms ; #endif fullname = lpaf->filelist[current] ; ImageUnpackFileName(fullname, ¤t_frame, &i, fname) ; fp = fopen(fname, "rb") ; if (!fp) ErrorReturn(-1,(ERROR_NO_FILE,"LPAFreadImageAnswer(%d): could not open %s", current, fname)) ; ecode = fread_header(fp, &Iheader, fname) ; fclose(fp) ; if (ecode) ErrorReturn(-2, (ERROR_BADFILE, "LPAFreadImageAnswer(%s): could not read header",fname)); if (Iheader.numparam < Iheader.num_frame) return(0) ; /* read answer from header */ #if 0 fprintf(stderr, "reading lp values from %dth entry in image file\n", current_frame); #endif lpb = &lpaf->coords[current] ; for (frame = 0, xp = Iheader.params ; xp ; xp = xp->nextp) if (frame++ == current_frame) break ; /* if hips file created on Sun, then the parameters are actually longs. */ #ifndef _MSDOS parms = xp->val.v_pi ; #else parms = (long *)xp->val.v_pi ; #endif #ifndef _MSDOS if (parms[0] < 0 || parms[0] >= Iheader.cols) { parms[0] = swapInt(parms[0]) ; parms[1] = swapInt(parms[1]) ; for (i = 0 ; i < NPOINTS ; i++) { parms[2+2*i] = swapInt(parms[2*i]) ; parms[2+2*i+1] = swapInt(parms[2*i+1]) ; } } #else if (parms[0] < 0 || parms[0] >= (long)Iheader.cols) { parms[0] = swapLong(parms[0]) ; parms[1] = swapLong(parms[1]) ; for (i = 0 ; i < NPOINTS ; i++) { parms[2+2*i] = swapLong(parms[2*i]) ; parms[2+2*i+1] = swapLong(parms[2*i+1]) ; } } #endif if ((int)parms[0] == INIT_VAL) /* not yet written with real value */ return(0) ; lpb->xc = (int)parms[0] ; lpb->yc = (int)parms[1] ; for (i = 0 ; i < NPOINTS ; i++) { lpb->xp[i] = (int)parms[2+2*i] ; lpb->yp[i] = (int)parms[2+2*i+1] ; } if (lpb->xc < 0 || lpb->xc >= Iheader.cols || lpb->yc < 0 || lpb->xc >= Iheader.rows ) return(0) ; return(1) ; }