bool ZCharacterItem::Save(ZFile *file) { size_t n; for(int i=0;i<MMCIP_END;i++) { ZItem *pItem=GetItem(MMatchCharItemParts(i)); int nBullet=pItem->GetBulletSpare(); n=zfwrite(&nBullet,sizeof(nBullet),1,file); if(n!=1) return false; int nBulletCurrMagazine=pItem->GetBulletCurrMagazine(); n=zfwrite(&nBulletCurrMagazine,sizeof(nBulletCurrMagazine),1,file); if(n!=1) return false; } return true; }
/** A unified header writing routine for .bin and .fits files. It write the array information and string header if any. */ void write_header(const header_t *header, file_t *fp) { if(fp->isfits) { if(header->magic!=MCC_ANY) { write_fits_header(fp, header->str, header->magic, 2, &header->nx, &header->ny); } } else { if(header->str) { write_bin_headerstr(header->str, fp); } //2015/01/27: We converted dummy header to a subtype. uint32_t magic2=M_SKIP; zfwrite(&magic2, sizeof(uint32_t), 1, fp); zfwrite(&header->magic, sizeof(uint32_t), 1, fp); zfwrite(&header->nx, sizeof(uint64_t), 1, fp); zfwrite(&header->ny, sizeof(uint64_t), 1, fp); } }
/* Append the header to current position in the file. First write magic number, then the length of the header, then the header, then the length of the header again, then the magic number again. The two set of strlen and header are used to identify the header from the end of the file and also to verify that what we are reading are indeed header. The header may be written multiple times. They will be concatenated when read. The header should contain key=value entries just like the configuration files. The entries should be separated by new line charactor. Don't need to write M_SKIP here because we have a pair of magic. The length of header is rounded to multiple of 8 bytes. */ static void write_bin_headerstr(const char *str, file_t *fp) { if(!str) return; if(fp->isfits) error("fits file is not supported\n"); uint32_t magic=M_HEADER; uint64_t nlen=strlen(str)+1; uint64_t nlen2=(nlen%8)?((nlen/8+1)*8):(nlen); char zero[8]= {0}; zfwrite(&magic, sizeof(uint32_t), 1, fp); zfwrite(&nlen2, sizeof(uint64_t), 1, fp); /*make str 8 byte alignment. */ zfwrite(str, 1, nlen, fp); if(nlen2>nlen) zfwrite(zero, 1, nlen2-nlen, fp); zfwrite(&nlen2, sizeof(uint64_t), 1, fp); zfwrite(&magic, sizeof(uint32_t), 1, fp); }
/** Write an 1-d or 2-d array into the file. First write a magic number that represents the data type. Then write two numbers representing the dimension. Finally write the data itself. */ void writearr(const void *fpn, /**<[in] The file pointer*/ const int isfn, /**<[in] Is this a filename or already opened file*/ const size_t size, /**<[in] Size of each element*/ const uint32_t magic,/**<[in] The magic number. see bin.h*/ const char *str, /**<[in] The header as string*/ const void *p, /**<[in] The data of the array*/ const uint64_t nx, /**<[in] Number of rows. this index changes fastest*/ const uint64_t ny /**<[in] Number of columns. 1 for vector*/ ) { file_t *fp; header_t header= {magic, nx, ny, (char*)str}; if(isfn) { fp=zfopen((char*)fpn, "wb"); } else { fp=(file_t*) fpn; } if(!fp) { warning("fp is empty\n"); return; } write_header(&header, fp); if(nx*ny>0) zfwrite(p, size, nx*ny, fp); if(isfn) zfclose(fp); }
int ZipOp::ZipStreamStream(void) { fwindow_size = 0L; if (fGEncrypt) { fkey = fGPassword; if (!fkey || !*(fkey)) { // use global if (GetUserPW() != DZ_ERR_GOOD) return -1; // error fkey = fuser_key; } } fZipInfile = new ZStream(this, _T("0:<INSTREAM>"), fSS->fSSInput); AutoStream inz(&fZipInfile); fimax = fSS->Size; fcrc = crc32(0L, NULL, 0); fisize = 0; CB->SetArg1(1); CB->UserCB(zacCount); // Pass total filesize. CB->SetFileSize(fimax); CB->UserCB(zacSize); ulg f_crc = 0; __int64 fsz = 0; bool haveCRC = false; if (fkey) { if (!fNoPrecalc) { if (Verbose < 0) Notify(ITRACE, _T("about to call Precalculate CRC")); // +++++ get CRC before we start CB->UserXItem(fimax, 13, _T("*PreCalculate")); __int64 pos1 = 0; if (!fZipInfile->IsFile) pos1 = fZipInfile->SetPosition(0, FILE_CURRENT); // get start posn f_crc = crc32(0L, NULL, 0); unsigned long byts; while (true) { unsigned ToRead = sizeof(fwindow); if (fimax > 0 && (fsz + ToRead) > fimax) { ToRead = (unsigned)(fimax - fsz); if (!ToRead) break; } if (!fZipInfile->Read(fwindow, ToRead, &byts) || !byts) break; fsz += byts; f_crc = crc32(f_crc, (const uch*)fwindow, (int)byts); CB->UserXProgress(byts, 13); if (Abort_Flag) Fatal(DZ_ERM_ABORT, 0); } fSS->CRC = f_crc; haveCRC = true; // reposition if (fZipInfile->SetPosition(pos1, FILE_BEGIN) != pos1) { if (Verbose) Notify(IVERBOSE, _T("Could not reposition %s [%s]"), fZipInfile->fname.c_str(), SysMsg().c_str()); return DZError(DZ_ERM_ERROR_SEEK); } if (fimax > fsz) fimax = fsz; } // ----- get CRC before we start // Since we do not yet know the crc here, we pretend that the crc is the // modification time: // if (!haveCRC) // fSS->CRC = z->tim << 16; if (Verbose < 0) Notify(ITRACE, _T("using supplied CRC %lu"), fSS->CRC); } // connect to output fZipOutfile = new ZStream(this, _T("0:<OUTSTREAM>"), fSS->fSSOutput); AutoStream outz(&fZipOutfile); CB->UserItem(fimax, _T("<INSTREAM>")); if (fkey) crypthead(fkey, fSS->CRC); // write // Write stored or deflated file to zip file fSS->Method &= 0xFF; if (fSS->Method != DEFLATE) fSS->Method = 0; if (flevel < 1) fSS->Method = 0; int mthd = (int)fSS->Method; if (mthd == DEFLATE) { if (Verbose < 0) Notify(ITRACE, _T("about to call Deflate")); bi_init(); ush att = BINARY; ush flg = FLAG_ENCRYPT_BIT; // will be changed in deflate() ct_init(&att, &mthd); lm_init(flevel, &flg); // PERFORM THE DEFLATE fSS->Size = deflate(); if (Abort_Flag) Fatal(DZ_ERM_ABORT, 0); } else { int k; if (Verbose) Notify(IVERBOSE, _T("Storing %s "), fZipInfile->fname.c_str()); while ((k = read_buf(fwindow, sizeof(fwindow))) > 0 && k != EOF) { if (Abort_Flag) Fatal(DZ_ERM_ABORT, 0); if (!zfwrite(fwindow, (extent)k)) return DZ_ERM_TEMP_FAILED; } } /* Finished Item */ CB->UserItem(-1, _T("<INSTREAM>")); // mark end of item CB->UserCB(zacEndOfBatch); // done with stream compression if (haveCRC) { if (f_crc != fcrc) Notify(DZ_ERR_ERROR_READ | IWARNING, _T(" File CRC changed while zipping: %s"), fZipInfile->fname.c_str()); if (fisize != fsz) Notify(DZ_ERR_ERROR_READ | IWARNING, _T(" File size changed while zipping: %s"), fZipInfile->fname.c_str()); } fSS->Size = fisize; fSS->CRC = fcrc; fSS->Method = (DWORD)mthd | (fkey ? 0xff00 : 0); return DZ_ERR_GOOD; }
/* Write fits header. str is extra header that will be put in fits comment */ static void write_fits_header(file_t *fp, const char *str, uint32_t magic, int count, ...) { uint64_t naxis[count]; va_list ap; va_start (ap, count); /*Initialize the argument list. */ int empty=0; for (int i = 0; i < count; i++) { uint64_t *addr=va_arg (ap, uint64_t*); /*Get the next argument value. */ if((*addr)==0) empty=1; naxis[i]=*addr; } va_end(ap); if(empty) count=0; int bitpix=0; switch(magic & 0xFFFF) { case M_FLT: bitpix=-32; break; case M_DBL: bitpix=-64; break; case M_INT32: bitpix=32; break; case M_INT16: bitpix=16; break; case M_INT8: bitpix=8; break; default: error("Data type is not yet supported. magic=%x\n", magic); } const int nh=36; char header[nh][80]; memset(header, ' ', sizeof(char)*36*80); int hc=0; if(fp->isfits==1) { snprintf(header[hc], 80, "%-8s= %20s", "SIMPLE", "T"); header[hc][30]=' '; hc++; fp->isfits++; } else { snprintf(header[hc], 80, "%-8s= %s", "XTENSION", "'IMAGE '"); header[hc][20]=' '; hc++; } snprintf(header[hc], 80, "%-8s= %20d", "BITPIX", bitpix); header[hc][30]=' '; hc++; snprintf(header[hc], 80, "%-8s= %20d", "NAXIS", count); header[hc][30]=' '; hc++; #define FLUSH_OUT /*write the block and reset */ \ if(hc==nh){ \ zfwrite(header, sizeof(char), 36*80, fp); \ memset(header, ' ', sizeof(char)*36*80); \ hc=0; \ } for (int i = 0; i < count; i++) { FLUSH_OUT; snprintf(header[hc], 80, "%-5s%-3d= %20lu", "NAXIS", i+1, (unsigned long)(naxis[i])); header[hc][30]=' '; hc++; } if(str) { const char *str2=str+strlen(str); while(str<str2) { char *nl=strchr(str, '\n'); int length; if(nl) { length=nl-str+1; if(length<=70) nl[0]=';'; } else { length=strlen(str); } if(length>70) length=70; FLUSH_OUT; strncpy(header[hc], "COMMENT ", 10); strncpy(header[hc]+10, str, length); hc++; str+=length; } } FLUSH_OUT; snprintf(header[hc], 80, "%-8s", "END"); header[hc][8]=' '; hc++; zfwrite(header, sizeof(char), 36*80, fp); #undef FLUSH_OUT }