NS_PREFIX INT NS_DIM_PREFIX SM_Decompose_LR_pivot (const SPARSE_MATRIX *sm, DOUBLE *values, DOUBLE *LR, int *pivot) { register int i,j,k,n; register DOUBLE *Row_Ptr; n = sm->nrows; if (n!=sm->ncols) ERR_RETURN(-1); /* Error: sparse matrix not symmetric */ /* clear LR */ for (i=0; i<n*n; i++) LR[i] = 0.0; /* then copy sm into LR */ for (i=0; i<n; i++) { Row_Ptr = LR+i*n; for (j=sm->row_start[i]; j<sm->row_start[i+1]; j++) { k = sm->col_ind[j]; if (k>=n) ERR_RETURN(-1); /* Error: column index too large */ Row_Ptr[k] = values[sm->offset[j]]; } } /* then call the block decomposition routine */ return (Decompose_LR_pivot (n, LR, pivot)); }
NS_PREFIX INT NS_DIM_PREFIX SM2Array (const SPARSE_MATRIX *sm, SHORT *comps) { int i,j,nr,nc,off,posc; nr = sm->nrows; nc = sm->ncols; if (nr*nc>MAX_MAT_COMP) ERR_RETURN (-1); posc = sm->row_start[0]; for (i=0; i<nr; i++) { for (j=0; j<nc; j++) { if (posc>=sm->row_start[i+1]) off = -1; else { if (sm->col_ind[posc]==j) { off = sm->offset[posc]; posc++; } else off = -1; } *comps++ = off; } if (posc!=sm->row_start[i+1]) ERR_RETURN (-2); } return (0); }
int require_partition_offset_validate(struct fun_context *fctx) { if (fctx->argc != 3) ERR_RETURN("require-partition-offset requires a partition number and a block offset"); int partition = strtol(fctx->argv[1], NULL, 0); if (partition < 0 || partition > 3) ERR_RETURN("require-partition-offset requires the partition number to be between 0, 1, 2, or 3"); CHECK_ARG_UINT64(fctx->argv[2], "require-partition-offset requires a non-negative integer block offset"); return 0; }
int require_uboot_variable_validate(struct fun_context *fctx) { if (fctx->argc != 4) ERR_RETURN("require-uboot-variable requires a uboot-environment reference, variable name, and value"); const char *uboot_env_name = fctx->argv[1]; cfg_t *ubootsec = cfg_gettsec(fctx->cfg, "uboot-environment", uboot_env_name); if (!ubootsec) ERR_RETURN("require-uboot-variable can't find uboot-environment reference"); return 0; }
int fatfs_pwrite(struct block_cache *output, off_t block_offset, const char *filename, int offset, const char *buffer, off_t size) { // Check if this is the same file as a previous pwrite call if (current_file_ && strcmp(current_file_, filename) != 0) close_open_files(); MAYBE_MOUNT(output, block_offset); if (!current_file_) { CHECK("fat_write can't open file", filename, f_open(&fil_, filename, FA_OPEN_ALWAYS | FA_WRITE)); // Assuming it opens ok, cache the filename for future writes. current_file_ = strdup(filename); } // Check if this pwrite requires a seek. DWORD desired_offset = offset; if (desired_offset != f_tell(&fil_)) { // Need to seek, but if we're seeking past the end, be sure to fill in with zeros. if (desired_offset > f_size(&fil_)) { // Seek to the end CHECK("fat_write can't seek to end of file", filename, f_lseek(&fil_, f_size(&fil_))); // Write zeros. DWORD zero_count = desired_offset - f_tell(&fil_); char zero_buffer[FWUP_BLOCK_SIZE]; memset(zero_buffer, 0, sizeof(zero_buffer)); while (zero_count) { DWORD btw = (zero_count < sizeof(zero_buffer) ? zero_count : sizeof(zero_buffer)); UINT bw; CHECK("fat_write can't write", filename, f_write(&fil_, zero_buffer, btw, &bw)); if (btw != bw) ERR_RETURN("Error writing file to FAT: %s, expected %ld bytes written, got %d (maybe the disk is full?)", filename, size, bw); zero_count -= bw; } } else { CHECK("fat_write can't seek in file", filename, f_lseek(&fil_, desired_offset)); } } UINT bw; CHECK("fat_write can't write", filename, f_write(&fil_, buffer, size, &bw)); if (size != bw) ERR_RETURN("Error writing file to FAT: %s, expected %ld bytes written, got %d (maybe the disk is full?)", filename, size, bw); return 0; }
/** * @brief fatfs_cp copy a file * @param fc the current FAT session * @param from_name original filename * @param to_name the name of the copy filename * @return 0 on success */ int fatfs_cp(struct block_cache *output, off_t block_offset, const char *from_name, const char *to_name) { close_open_files(); MAYBE_MOUNT(output, block_offset); FIL fromfil; FIL tofil; CHECK("fatfs_cp can't open file", from_name, f_open(&fromfil, from_name, FA_READ)); CHECK("fatfs_cp can't open file", to_name, f_open(&tofil, to_name, FA_CREATE_ALWAYS | FA_WRITE)); for (;;) { char buffer[4096]; UINT bw, br; CHECK("fatfs_cp can't read", from_name, f_read(&fromfil, buffer, sizeof(buffer), &br)); if (br == 0) break; CHECK("fatfs_cp can't write", to_name, f_write(&tofil, buffer, br, &bw)); if (br != bw) ERR_RETURN("Error copying file to FAT"); } f_close(&fromfil); f_close(&tofil); return 0; }
int require_path_on_device_validate(struct fun_context *fctx) { if (fctx->argc != 3) ERR_RETURN("require-path-on-device requires a path and a device"); return 0; }
NS_PREFIX INT NS_DIM_PREFIX SM_Compute_Reduced_Offsets (SPARSE_MATRIX *sm, SHORT *reduced_offsets) { register INT i, j, k, off; if (sm->N < 0) ERR_RETURN (-1); /* Error: sparse matrix has negative size */ k = 0; for (i=0; i<sm->N; i++) { off = sm->offset[i]; /* did it already occur? */ for (j=0; j<i; j++) if (sm->offset[j] == off) break; if (j<i) break; /* yes: no need to take it again */ /* no: take it into the reduced_offsets */ reduced_offsets[k++] = off; } return (k); }
NS_PREFIX INT NS_DIM_PREFIX SM_Compute_Reduced_Size (SPARSE_MATRIX *sm) { register INT i, j, off; register INT ident_count; if (sm->N<0) ERR_RETURN (-1); /* Error: sparse matrix has negative size */ ident_count = 0; for (i=0; i<sm->N; i++) { off = sm->offset[i]; for (j=i+1; j<sm->N; j++) { if (sm->offset[j] == off) { /* identification: increase the counter and break the loop. */ ident_count++; break; } } } return(sm->N-ident_count); }
int require_fat_file_exists_validate(struct fun_context *fctx) { if (fctx->argc != 3) ERR_RETURN("require-fat-file-exists requires a FAT FS block offset and a filename"); CHECK_ARG_UINT64(fctx->argv[1], "require-fat-file-exists requires a non-negative integer block offset"); return 0; }
int require_path_at_offset_validate(struct fun_context *fctx) { if (fctx->argc != 3) ERR_RETURN("require-path-at-offset requires a path and a block offset"); CHECK_ARG_UINT64(fctx->argv[2], "require-path-at-offset requires a non-negative integer block offset"); return 0; }
int require_fat_file_match_validate(struct fun_context *fctx) { if (fctx->argc != 4) ERR_RETURN("require-fat-file-match requires a FAT FS block offset, a filename, and a pattern"); CHECK_ARG_UINT64(fctx->argv[1], "require-fat-file-match requires a non-negative integer block offset"); return 0; }
int fwfile_add_meta_conf(cfg_t *cfg, struct archive *a, const unsigned char *signing_key) { char *configtxt; size_t configtxt_len; configtxt_len = fwup_cfg_to_string(cfg, &configtxt); if (configtxt_len == 0) ERR_RETURN("Could not create meta.conf contents"); int rc = fwfile_add_meta_conf_str(configtxt, configtxt_len, a, signing_key); free(configtxt); return rc; }
static int check_resource(struct resource_list *list, const char *file_resource_name, struct archive *a, struct archive_entry *ae) { struct resource_list *item = rlist_find_by_name(list, file_resource_name); if (!item) ERR_RETURN("Can't find file-resource for %s", file_resource_name); if (item->processed) ERR_RETURN("Processing %s twice. Archive is corrupt.", file_resource_name); item->processed = true; struct sparse_file_map sfm; sparse_file_init(&sfm); OK_OR_RETURN(sparse_file_get_map_from_resource(item->resource, &sfm)); size_t expected_length = sparse_file_data_size(&sfm); ssize_t archive_length = archive_entry_size(ae); if (archive_length < 0) ERR_RETURN("Missing file length in archive for %s", file_resource_name); if ((size_t) archive_length != expected_length) ERR_RETURN("Length mismatch for %s", file_resource_name); char *expected_hash = cfg_getstr(item->resource, "blake2b-256"); if (!expected_hash || strlen(expected_hash) != crypto_generichash_BYTES * 2) ERR_RETURN("invalid blake2b-256 hash for '%s'", file_resource_name); crypto_generichash_state hash_state; crypto_generichash_init(&hash_state, NULL, 0, crypto_generichash_BYTES); size_t length_left = expected_length; while (length_left != 0) { char buffer[4096]; size_t to_read = sizeof(buffer); if (to_read > length_left) to_read = length_left; ssize_t len = archive_read_data(a, buffer, to_read); if (len <= 0) ERR_RETURN("Error reading '%s' in archive", archive_entry_pathname(ae)); crypto_generichash_update(&hash_state, (const unsigned char*) buffer, len); length_left -= len; } unsigned char hash[crypto_generichash_BYTES]; crypto_generichash_final(&hash_state, hash, sizeof(hash)); char hash_str[sizeof(hash) * 2 + 1]; bytes_to_hex(hash, hash_str, sizeof(hash)); if (memcmp(hash_str, expected_hash, sizeof(hash_str)) != 0) ERR_RETURN("Detected blake2b digest mismatch for %s", file_resource_name); return 0; }
NS_PREFIX INT NS_DIM_PREFIX SM_Compute_Diff_From_Offset (INT N, SHORT *offset, ptrdiff_t *Diff) { register int i; if (N<0) ERR_RETURN (-1); /* Error: sparse matrix has negative size */ if (N==0) return (0); /* might be an error, but we prefer to do nothing */ for (i=0; i<N; i++) *Diff++ = (ptrdiff_t) ((offset[(i+1)%N]-offset[i])*sizeof(DOUBLE)); return(0); }
NS_PREFIX INT NS_DIM_PREFIX SM_Compute_yDiff_From_Offset (INT N, SHORT *col_ind, SHORT *cmp_off, ptrdiff_t *Diff) { register int i; if (N<0) ERR_RETURN (-1); /* Error: sparse matrix has negative size */ if (N==0) return (0); /* probably an error, but we prefer to do nothing */ for (i=0; i<N; i++) *Diff++ = (ptrdiff_t) ((cmp_off[col_ind[(i+1)%N]]-cmp_off[col_ind[i]]) *sizeof(DOUBLE)); return(0); }
int btContent::InitialFromMI(const char *metainfo_fname,const char *saveas) { #define ERR_RETURN() {if(b) delete []b; return -1;} unsigned char *ptr = m_shake_buffer; char *b; const char *s; size_t flen, q, r; b = _file2mem(metainfo_fname,&flen); if ( !b ) return -1; // announce if( !meta_str("announce",&s,&r) ) ERR_RETURN(); if( r > MAXPATHLEN ) ERR_RETURN(); m_announce = new char [r + 1]; memcpy(m_announce, s, r); m_announce[r] = '\0'; // infohash if( !(r = meta_pos("info")) ) ERR_RETURN(); if( !(q = decode_dict(b + r, flen - r, (char *) 0)) ) ERR_RETURN(); Sha1(b + r, q, m_shake_buffer + 28); if( meta_int("creation date",&r)) m_create_date = (time_t) r; // hash table if( !meta_str("info|pieces",&s,&m_hashtable_length) || m_hashtable_length % 20 != 0) ERR_RETURN(); m_hash_table = new unsigned char[m_hashtable_length]; #ifndef WINDOWS if( !m_hash_table ) ERR_RETURN(); #endif memcpy(m_hash_table, s, m_hashtable_length); if(!meta_int("info|piece length",&m_piece_length)) ERR_RETURN(); m_npieces = m_hashtable_length / 20; if( m_piece_length > cfg_max_slice_size * cfg_req_queue_length ){ fprintf(stderr,"error, piece length too long[%u]. please recomplie CTorrent with a larger cfg_max_slice_size in <btconfig.h>.\n", m_piece_length); ERR_RETURN(); } if( m_piece_length < cfg_req_slice_size ) cfg_req_slice_size = m_piece_length; else{ for( ;(m_piece_length / cfg_req_slice_size) >= cfg_req_queue_length; ){ cfg_req_slice_size *= 2; if( cfg_req_slice_size > cfg_max_slice_size ) ERR_RETURN(); } } if( m_btfiles.BuildFromMI(b, flen, saveas) < 0) ERR_RETURN(); delete []b; PrintOut(); if( arg_flg_exam_only ) return 0; if( ( r = m_btfiles.CreateFiles() ) < 0) ERR_RETURN(); global_piece_buffer = new char[m_piece_length]; #ifndef WINDOWS if( !global_piece_buffer ) ERR_RETURN(); #endif pBF = new BitField(m_npieces); #ifndef WINDOWS if( !pBF ) ERR_RETURN(); #endif m_left_bytes = m_btfiles.GetTotalLength() / m_piece_length; if( m_btfiles.GetTotalLength() % m_piece_length ) m_left_bytes++; if( m_left_bytes != m_npieces ) ERR_RETURN(); m_left_bytes = m_btfiles.GetTotalLength(); if( arg_bitfield_file ){ if( !arg_flg_check_only ){ if( pBF->SetReferFile(arg_bitfield_file) >= 0){ size_t idx; r = 0; for( idx = 0; idx < m_npieces; idx++ ) if( pBF->IsSet(idx) ) m_left_bytes -= GetPieceLength(idx); } else{ fprintf(stderr,"warn, couldn't set bit field refer file %s.\n",arg_bitfield_file); } } if( r ) CheckExist(); }else if( arg_flg_force_seed_mode ){ pBF->SetAll(); m_left_bytes = 0; }else if( r ){ CheckExist(); } printf("Already/Total: %u/%u\n",pBF->Count(),m_npieces); if( arg_flg_check_only ){ if( arg_bitfield_file ) pBF->WriteToFile(arg_bitfield_file); exit(1); } CacheConfigure(); *ptr = (unsigned char) 19; ptr++; // protocol string length memcpy(ptr,"BitTorrent protocol",19); ptr += 19; // protocol string memset(ptr,0,8); // reserved set zero. { // peer id int i; ptr = m_shake_buffer + 48; for( i = 0; i < 20; i++,ptr++) *ptr = (unsigned char)(rand() & 0xFF); } return 0; }
int _intercept_open(const char *pathname) { int fd; CURLcode ret; intercept_t *obj; long status; RESOLVE(open64); RESOLVE(close); #define ERR_RETURN(err) do { if (fd >= 0) o_close(fd); errno = err; return -1; } while (0); #define CURL_SETOPT(option, value) if ((ret = curl_easy_setopt(obj->curl, option, value)) != CURLE_OK) ERR_RETURN(EACCES); if ((fd = o_open64("/dev/null", O_RDONLY, 0644)) < 0) ERR_RETURN(errno); DEBUGF("new fd=%d\n", fd); if (fd > HIGHEST_FD) ERR_RETURN(EMFILE); if (!(obj = calloc(1, sizeof(intercept_t)))) ERR_RETURN(ENOMEM); obj->refcount = 1; obj->url = strdup(pathname); if (!(obj->curl = curl_easy_init())) ERR_RETURN(ENOMEM); if (getenv("DEBUG")) CURL_SETOPT(CURLOPT_VERBOSE, 0x1); CURL_SETOPT(CURLOPT_URL, obj->url); CURL_SETOPT(CURLOPT_NOBODY, 0x1); CURL_SETOPT(CURLOPT_WRITEHEADER, obj); CURL_SETOPT(CURLOPT_HEADERFUNCTION, _curl_parse_header); CURL_SETOPT(CURLOPT_WRITEFUNCTION, _curl_output_null); if ((ret = curl_easy_perform(obj->curl)) != CURLE_OK) ERR_RETURN(EACCES); if ((ret = curl_easy_getinfo(obj->curl, CURLINFO_RESPONSE_CODE, &status)) != CURLE_OK || status != 200) ERR_RETURN(ENOENT); DEBUGF("curl_easy_perform succeeded\n"); if (!obj->size || !(obj->flags & FEAT_RANGE_SUPPORT)) { DEBUGF("unacceptable resource: size=%zu, flags=%d\n", obj->size, obj->flags); ERR_RETURN(ENXIO); } CURL_SETOPT(CURLOPT_WRITEHEADER, NULL); CURL_SETOPT(CURLOPT_HEADERFUNCTION, NULL); #undef CURL_SETOPT intercept[fd] = obj; errno = 0; return fd; }