bool TimeWidget::setTime(long time) { this->_time = time; if(time != -1) { bool negative = false; if (time < 0) { negative = true; time *= -1; } int minutes, seconds, milliseconds; seconds = ((time / 1000) % 60) ; minutes = ((time / (1000*60)) % 60); milliseconds = time - seconds*1000 - minutes*60*1000; Wt::WString out = Wt::WString("{1}:{2}:{3}").arg(zero_pad(minutes,2)).arg(zero_pad(seconds,2)).arg(zero_pad(milliseconds,3)); if (negative) { this->setText(Wt::WString("- {1}").arg(out)); } else { this->setText(out); } } return true; }
zstring TimeZone::toString() const { zstring result; if ( *this ) { if ( !gmtoff_ ) result = 'Z'; else { result += *this < 0 ? '-' : '+'; result += zero_pad( std::abs( getHours() ), 2 ); result += ':'; result += zero_pad( std::abs( getMinutes() ), 2 ); } } return result; }
vec spectrum(const vec &v, const vec &w, int noverlap) { int nfft = w.size(); it_assert_debug(pow2i(levels2bits(nfft)) == nfft, "The window size must be a power of two in spectrum()!"); vec P(nfft / 2 + 1), wd(nfft); P = 0.0; double w_energy = energy(w); if (nfft > v.size()) { P = sqr(abs(fft(to_cvec(elem_mult(zero_pad(v, nfft), w)))(0, nfft / 2))); P /= w_energy; } else { int k = (v.size() - noverlap) / (nfft - noverlap), idx = 0; for (int i = 0; i < k; i++) { wd = elem_mult(v(idx, idx + nfft - 1), w); P += sqr(abs(fft(to_cvec(wd))(0, nfft / 2))); idx += nfft - noverlap; } P /= k * w_energy; } P.set_size(nfft / 2 + 1, true); return P; }
int cf_ccm_decrypt(const cf_prp *prp, void *prpctx, const uint8_t *cipher, size_t ncipher, size_t L, const uint8_t *header, size_t nheader, const uint8_t *nonce, size_t nnonce, const uint8_t *tag, size_t ntag, uint8_t *plain) { uint8_t block[CF_MAXBLOCK]; assert(ntag >= 4 && ntag <= 16 && ntag % 2 == 0); assert(L >= 2 && L <= 8); assert(nnonce == prp->blocksz - L - 1); uint8_t ctr_nonce[CF_MAXBLOCK]; build_ctr_nonce(ctr_nonce, L, nonce, nnonce); cf_ctr ctr; cf_ctr_init(&ctr, prp, prpctx, ctr_nonce); cf_ctr_custom_counter(&ctr, prp->blocksz - L, L); /* Decrypt tag. */ uint8_t plain_tag[CF_MAXBLOCK]; cf_ctr_cipher(&ctr, tag, plain_tag, ntag); cf_ctr_discard_block(&ctr); /* Decrypt message. */ cf_ctr_cipher(&ctr, cipher, plain, ncipher); cf_cbcmac_stream cm; cf_cbcmac_stream_init(&cm, prp, prpctx); /* Add first block. */ add_block0(&cm, block, prp->blocksz, nonce, nnonce, L, ncipher, nheader, ntag); if (nheader) add_aad(&cm, block, header, nheader); cf_cbcmac_stream_update(&cm, plain, ncipher); zero_pad(&cm); /* Finish tag. */ cf_cbcmac_stream_nopad_final(&cm, block); int err = 0; if (!mem_eq(block, plain_tag, ntag)) { err = 1; mem_clean(plain, ncipher); } mem_clean(block, sizeof block); mem_clean(plain_tag, sizeof plain_tag); return err; }
static void nh_final(nh_ctx *hc, UINT8 *result) /* After passing some number of data buffers to nh_update() for integration * into an NH context, nh_final is called to produce a hash result. If any * bytes are in the buffer hc->data, incorporate them into the * NH context. Finally, add into the NH accumulation "state" the total number * of bits hashed. The resulting numbers are written to the buffer "result". * If nh_update was never called, L1_PAD_BOUNDARY zeroes are incorporated. */ { int nh_len, nbits; if (hc->next_data_empty != 0) { nh_len = ((hc->next_data_empty + (L1_PAD_BOUNDARY - 1)) & ~(L1_PAD_BOUNDARY - 1)); zero_pad(hc->data + hc->next_data_empty, nh_len - hc->next_data_empty); nh_transform(hc, hc->data, nh_len); hc->bytes_hashed += hc->next_data_empty; } else if (hc->bytes_hashed == 0) { nh_len = L1_PAD_BOUNDARY; zero_pad(hc->data, L1_PAD_BOUNDARY); nh_transform(hc, hc->data, nh_len); } nbits = (hc->bytes_hashed << 3); ((UINT64 *)result)[0] = ((UINT64 *)hc->state)[0] + nbits; #if (UMAC_OUTPUT_LEN >= 8) ((UINT64 *)result)[1] = ((UINT64 *)hc->state)[1] + nbits; #endif #if (UMAC_OUTPUT_LEN >= 12) ((UINT64 *)result)[2] = ((UINT64 *)hc->state)[2] + nbits; #endif #if (UMAC_OUTPUT_LEN == 16) ((UINT64 *)result)[3] = ((UINT64 *)hc->state)[3] + nbits; #endif nh_reset(hc); }
void cf_ccm_encrypt(const cf_prp *prp, void *prpctx, const uint8_t *plain, size_t nplain, size_t L, const uint8_t *header, size_t nheader, const uint8_t *nonce, size_t nnonce, uint8_t *cipher, uint8_t *tag, size_t ntag) { uint8_t block[CF_MAXBLOCK]; assert(ntag >= 4 && ntag <= 16 && ntag % 2 == 0); assert(L >= 2 && L <= 8); assert(nnonce == prp->blocksz - L - 1); cf_cbcmac_stream cm; cf_cbcmac_stream_init(&cm, prp, prpctx); /* Add first block. */ add_block0(&cm, block, prp->blocksz, nonce, nnonce, L, nplain, nheader, ntag); /* Add AAD with length prefix, if present. */ if (nheader) add_aad(&cm, block, header, nheader); /* Add message. */ cf_cbcmac_stream_update(&cm, plain, nplain); zero_pad(&cm); /* Finish tag. */ cf_cbcmac_stream_nopad_final(&cm, block); /* Start encryption. */ /* Construct A_0 */ uint8_t ctr_nonce[CF_MAXBLOCK]; build_ctr_nonce(ctr_nonce, L, nonce, nnonce); cf_ctr ctr; cf_ctr_init(&ctr, prp, prpctx, ctr_nonce); cf_ctr_custom_counter(&ctr, prp->blocksz - L, L); /* Encrypt tag first. */ cf_ctr_cipher(&ctr, block, block, prp->blocksz); memcpy(tag, block, ntag); /* Then encrypt message. */ cf_ctr_cipher(&ctr, plain, cipher, nplain); }
/* nb. block is general workspace. */ static void add_aad(cf_cbcmac_stream *cm, uint8_t block[CF_MAXBLOCK], const uint8_t *header, size_t nheader) { assert(nheader <= 0xffffffff); /* we don't support 64 bit lengths. */ /* Add length using stupidly complicated rules. */ if (nheader < 0xff00) { write_be(block, nheader, 2); cf_cbcmac_stream_update(cm, block, 2); } else { write_be(block, 0xfffe, 2); write_be(block + 2, nheader, 4); cf_cbcmac_stream_update(cm, block, 6); } cf_cbcmac_stream_update(cm, header, nheader); zero_pad(cm); }
_WCRTLINK int write( int handle, const void *buffer, unsigned len ) #endif /**********************************************************************/ { unsigned iomode_flags; char *buf; unsigned buf_size; unsigned len_written, i, j; #if defined(__NT__) HANDLE h; LONG cur_ptr_low; LONG cur_ptr_high; DWORD rc1; #else tiny_ret_t rc1; #endif int rc2; __handle_check( handle, -1 ); iomode_flags = __GetIOMode( handle ); if( iomode_flags == 0 ) { #if defined(__WINDOWS__) || defined(__WINDOWS_386__) // How can we write to the handle if we never opened it? JBS return( _lwrite( handle, buffer, len ) ); #else __set_errno( EBADF ); return( -1 ); #endif } if( !(iomode_flags & _WRITE) ) { __set_errno( EACCES ); /* changed from EBADF to EACCES 23-feb-89 */ return( -1 ); } #if defined(__NT__) h = __getOSHandle( handle ); #endif // put a semaphore around our writes _AccessFileH( handle ); if( (iomode_flags & _APPEND) && !(iomode_flags & _ISTTY) ) { #if defined(__NT__) if( GetFileType( h ) == FILE_TYPE_DISK ) { cur_ptr_low = 0; cur_ptr_high = 0; rc1 = SetFilePointer( h, cur_ptr_low, &cur_ptr_high, FILE_END ); if( rc1 == INVALID_SET_FILE_POINTER ) { // this might be OK so if( GetLastError() != NO_ERROR ) { _ReleaseFileH( handle ); return( __set_errno_nt() ); } } } #elif defined(__OS2__) { unsigned long dummy; rc1 = DosChgFilePtr( handle, 0L, SEEK_END, &dummy ); // should we explicitly ignore ERROR_SEEK_ON_DEVICE here? } #else rc1 = TinySeek( handle, 0L, SEEK_END ); #endif #if !defined(__NT__) if( TINY_ERROR( rc1 ) ) { _ReleaseFileH( handle ); return( __set_errno_dos( TINY_INFO( rc1 ) ) ); } #endif } len_written = 0; rc2 = 0; // Pad the file with zeros if necessary if( iomode_flags & _FILEEXT ) { // turn off file extended flag __SetIOMode_nogrow( handle, iomode_flags&(~_FILEEXT) ); // It is not required to pad a file with zeroes on an NTFS file system; // unfortunately it is required on FAT (and probably FAT32). (JBS) rc2 = zero_pad( handle ); } if( rc2 == 0 ) { if( iomode_flags & _BINARY ) { /* if binary mode */ rc2 = os_write( handle, buffer, len, &len_written ); /* end of binary mode part */ } else { /* text mode */ i = stackavail(); if( i < 0x00b0 ) { __STKOVERFLOW(); /* not enough stack space */ } buf_size = 512; if( i < (512 + 48) ) { buf_size = 128; } #if defined(__AXP__) || defined(__PPC__) buf = alloca( buf_size ); #else buf = __alloca( buf_size ); #endif j = 0; for( i = 0; i < len; ) { if( ((const char*)buffer)[i] == '\n' ) { buf[j] = '\r'; ++j; if( j == buf_size ) { rc2 = os_write( handle, buf, buf_size, &j ); if( rc2 == -1 ) break; len_written += j; if( rc2 == ENOSPC ) break; len_written = i; j = 0; } } buf[j] = ((const char*)buffer)[i]; ++i; ++j; if( j == buf_size ) { rc2 = os_write( handle, buf, buf_size, &j ); if( rc2 == -1 ) break; len_written += j; if( rc2 == ENOSPC ) break; len_written = i; j = 0; } } if( j ) { rc2 = os_write( handle, buf, j, &i ); if( rc2 == ENOSPC ) { len_written += i; } else { len_written = len; } } /* end of text mode part */ } } _ReleaseFileH( handle ); if( rc2 == -1 ) { return( rc2 ); } else { return( len_written ); } }
Fluxrec *do_corr(Fluxrec *flux1, Fluxrec *flux2, int size, int *corsize, int nbad) { int no_error=1; /* Flag set to 0 on error */ int nx; /* Size of gridded arrays */ float xmin,xmax; /* Min and max day numbers */ float dx; /* Grid step-size between days */ float test; /* Tests size to see if it's a power of 2 */ float *zpad1=NULL; /* Zero padded light curve */ float *zpad2=NULL; /* Zero padded light curve */ Fluxrec *grflux1=NULL; /* Gridded version of flux1 */ Fluxrec *grflux2=NULL; /* Gridded version of flux2 */ Fluxrec *correl=NULL; /* Cross-correlation of flux1 and flux2 */ /* * Note that the day part of the flux array will be sorted by * construction, so the min and max values will be easy to find. */ xmin = flux1->day; xmax = (flux1+size-1)->day; /* * Check if size is a power of 2 because the number of points in the * curve MUST be a power of two if the cross-correlation function * is cross_corr_fft or cross_corr_nr. */ test = log((float)size)/log(2.0); /* * If it is or if CORRFUNC == 3 , then set the values for nx and dx, * as simple functions of size, xmin, and xmax, and then zero * pad the input data */ if(test-(int)test == 0) { nx = size; dx = (xmax-xmin)/(nx-1); if(!(zpad1 = zero_pad(flux1,nx))) no_error = 0; if(!(zpad2 = zero_pad(flux2,nx))) no_error = 0; } /* * If size is not a power of 2 and CORRFUNC != 3, interpolate the data onto * an appropriately spaced grid with a size that is a power of 2. */ else { /* * Compute new values for nx and dx */ nx = pow(2.0f,floor((log((double)size)/log(2.0)+0.5))); dx = (xmax-xmin)/(nx-1); printf("do_corr: Using grid of %d points for cross-correlation.\n",nx); if(!(grflux1 = lin_interp(flux1,size,nx,dx,nbad))) no_error = 0; if(no_error) if(!(grflux2 = lin_interp(flux2,size,nx,dx,nbad))) no_error = 0; /* * Zero-pad the gridded data */ if(no_error) if(!(zpad1 = zero_pad(grflux1,nx))) no_error = 0; if(no_error) if(!(zpad2 = zero_pad(grflux2,nx))) no_error = 0; } /* * Do the cross-correlation, via one of three styles: * 1. FFT method (Numerical Recipes FFTs, then mulitply and transform back * 2. Numerical Recipes correl function * 3. Time domain method with no FFTs */ if(no_error) { nx *= ZEROFAC; switch(CORRFUNC) { case 1: if(!(correl = cross_corr_fft(zpad1,zpad2,nx,dx))) no_error = 0; break; case 2: if(!(correl = cross_corr_nr(zpad1,zpad2,nx,dx))) no_error = 0; break; default: fprintf(stderr,"ERROR: do_corr. Bad choice of correlation function\n"); no_error = 0; } } /* * Clean up */ zpad1 = del_array(zpad1); zpad2 = del_array(zpad2); grflux1 = del_fluxrec(grflux1); grflux2 = del_fluxrec(grflux2); if(no_error) { *corsize = nx; return correl; } else { *corsize = 0; return NULL; fprintf(stderr,"ERROR: do_corr\n"); } }
//Correlation void xcorr(const cvec &x, const cvec &y, cvec &out, const int max_lag, const std::string scaleopt, bool autoflag) { int N = std::max(x.length(), y.length()); //Compute the FFT size as the "next power of 2" of the input vector's length (max) int b = ceil_i(::log2(2.0 * N - 1)); int fftsize = pow2i(b); int end = fftsize - 1; cvec temp2; if (autoflag == true) { //Take FFT of input vector cvec X = fft(zero_pad(x, fftsize)); //Compute the abs(X).^2 and take the inverse FFT. temp2 = ifft(elem_mult(X, conj(X))); } else { //Take FFT of input vectors cvec X = fft(zero_pad(x, fftsize)); cvec Y = fft(zero_pad(y, fftsize)); //Compute the crosscorrelation temp2 = ifft(elem_mult(X, conj(Y))); } // Compute the total number of lags to keep. We truncate the maximum number of lags to N-1. int maxlag; if ((max_lag == -1) || (max_lag >= N)) maxlag = N - 1; else maxlag = max_lag; //Move negative lags to the beginning of the vector. Drop extra values from the FFT/IFFt if (maxlag == 0) { out.set_size(1, false); out = temp2(0); } else out = concat(temp2(end - maxlag + 1, end), temp2(0, maxlag)); //Scale data if (scaleopt == "biased") //out = out / static_cast<double_complex>(N); out = out / static_cast<std::complex<double> >(N); else if (scaleopt == "unbiased") { //Total lag vector vec lags = linspace(-maxlag, maxlag, 2 * maxlag + 1); cvec scale = to_cvec(static_cast<double>(N) - abs(lags)); out /= scale; } else if (scaleopt == "coeff") { if (autoflag == true) // Normalize by Rxx(0) out /= out(maxlag); else { //Normalize by sqrt(Rxx(0)*Ryy(0)) double rxx0 = sum(abs(elem_mult(x, x))); double ryy0 = sum(abs(elem_mult(y, y))); out /= std::sqrt(rxx0 * ryy0); } } else if (scaleopt == "none") {} else it_warning("Unknow scaling option in XCORR, defaulting to <none> "); }
Array1D<T> zero_pad(const Array1D<T>& v) { int n = pow2(needed_bits(v.size())); return n==v.size() ? v : zero_pad(v, n); }