NS_PRIVATE NsError _ns_nzm_attach_branch_to_parent( NsModel *model, NzmBranch *child, NzmBranch *parent ) { nslistiter first; nslistiter last; nslistiter closest; nslistiter root; ns_assert( 0 < child->parent_cyl ); if( parent->num_cyls < child->parent_cyl ) return ns_error_inval( NS_ERROR_LEVEL_RECOVERABLE, NS_MODULE ); /* Find the start and end vertex of the parent cylinder that child attaches to. */ first = _ns_nzm_find_first_cylinder_vertex( parent, child->parent_cyl ); ns_verify( NULL != first ); last = _ns_nzm_find_last_cylinder_vertex( parent, child->parent_cyl ); ns_verify( NULL != last ); ns_assert( ns_list_iter_not_equal( first, last ) ); ns_assert( _ns_nzm_validate_cylinder( first, last, child->parent_cyl ) ); /* Find the closest vertex in the cylinder to the first vertex of the child branch. */ root = ns_list_begin( &child->vertices ); closest = _ns_nzm_find_closest_cylinder_vertex( first, last, root ); /* If the closest vertex is "close enough" then merge the first vertex of the child to it. Otherwise insert a new vertex into the cylinder and connect the first vertex of the child. */ return ns_model_merge_vertices( model, _nzm_branch_model_vertex( closest ), _nzm_branch_model_vertex( root ) ); }
NS_PRIVATE NsValueType* _ns_value_type( const nschar *name ) { nspointer type = NULL; ns_verify( ns_hash_table_lookup( ns_value_db(), ( nschar* )name, &type ) ); ns_assert_with_details( NULL != type, name ); return type; }
NsError _ns_value_db_init( void ) { NsError error; ns_verify( ! _ns_value_db_did_init ); if( NS_SUCCESS( ns_hash_table_construct( ns_value_db(), ns_ascii_hash, ns_ascii_streq, NULL, ns_value_type_delete ), error ) ) _ns_value_db_did_init = NS_TRUE; return error; }
NsError ns_value_register ( const nschar *name, nssize size, void ( *reset )( NsValue* ), void ( *unset )( NsValue* ), void ( *arg )( NsValue*, ns_va_list* ) ) { NsValueType *type; NsError error; ns_assert( NULL != name ); error = ns_no_error(); ns_mutex_lock( ns_value_static_mutex() ); if( ! ns_hash_table_exists( ns_value_db(), ( nschar* )name ) ) { ns_verify( size <= NS_VALUE_MAX_SIZE ); if( NS_SUCCESS( ns_value_type_new( &type, name, size, reset, unset, arg ), error ) ) if( NS_FAILURE( ns_hash_table_add( ns_value_db(), ( nschar* )type->name, type ), error ) ) ns_value_type_delete( type ); } ns_mutex_unlock( ns_value_static_mutex() ); return error; }
/* res_nsendsigned */ int res_nsendsigned(res_state statp, const u_char *msg, int msglen, ns_tsig_key *key, u_char *answer, int anslen) { res_state nstatp; DST_KEY *dstkey; int usingTCP = 0; u_char *newmsg; int newmsglen, bufsize, siglen; u_char sig[64]; HEADER *hp; time_t tsig_time; int ret; dst_init(); nstatp = (res_state) malloc(sizeof(*statp)); if (nstatp == NULL) { errno = ENOMEM; return (-1); } memcpy(nstatp, statp, sizeof(*statp)); bufsize = msglen + 1024; newmsg = (u_char *) malloc(bufsize); if (newmsg == NULL) { errno = ENOMEM; return (-1); } memcpy(newmsg, msg, msglen); newmsglen = msglen; if (ns_samename(key->alg, NS_TSIG_ALG_HMAC_MD5) != 1) dstkey = NULL; else dstkey = dst_buffer_to_key(key->name, KEY_HMAC_MD5, NS_KEY_TYPE_AUTH_ONLY, NS_KEY_PROT_ANY, key->data, key->len); if (dstkey == NULL) { errno = EINVAL; free(nstatp); free(newmsg); return (-1); } nstatp->nscount = 1; siglen = sizeof(sig); ret = ns_sign(newmsg, &newmsglen, bufsize, ns_r_noerror, dstkey, NULL, 0, sig, &siglen, 0); if (ret < 0) { free (nstatp); free (newmsg); dst_free_key(dstkey); if (ret == NS_TSIG_ERROR_NO_SPACE) errno = EMSGSIZE; else if (ret == -1) errno = EINVAL; return (ret); } if (newmsglen > NS_PACKETSZ || (nstatp->options & RES_IGNTC)) usingTCP = 1; if (usingTCP == 0) nstatp->options |= RES_IGNTC; else nstatp->options |= RES_USEVC; retry: ret = res_nsend(nstatp, newmsg, newmsglen, answer, anslen); if (ret < 0) { free (nstatp); free (newmsg); dst_free_key(dstkey); return (ret); } anslen = ret; ret = ns_verify(answer, &anslen, dstkey, sig, siglen, NULL, NULL, &tsig_time, nstatp->options & RES_KEEPTSIG); if (ret != 0) { Dprint(nstatp->pfcode & RES_PRF_REPLY, (stdout, ";; TSIG invalid (%s)\n", p_rcode(ret))); free (nstatp); free (newmsg); dst_free_key(dstkey); if (ret == -1) errno = EINVAL; else errno = ENOTTY; return (-1); } Dprint(nstatp->pfcode & RES_PRF_REPLY, (stdout, ";; TSIG ok\n")); hp = (HEADER *) answer; if (hp->tc && usingTCP == 0) { nstatp->options &= ~RES_IGNTC; usingTCP = 1; goto retry; } free (nstatp); free (newmsg); dst_free_key(dstkey); return (anslen); }
/*% res_nsendsigned */ int res_nsendsigned(res_state statp, const u_char *msg, int msglen, ns_tsig_key *key, u_char *answer, int anslen) { res_state nstatp; DST_KEY *dstkey; int usingTCP = 0; u_char *newmsg; int newmsglen, bufsize, siglen; u_char sig[64]; HEADER *hp; time_t tsig_time; int ret; int len; dst_init(); nstatp = (res_state) malloc(sizeof(*statp)); if (nstatp == NULL) { errno = ENOMEM; return (-1); } memcpy(nstatp, statp, sizeof(*statp)); bufsize = msglen + 1024; newmsg = (u_char *) malloc(bufsize); if (newmsg == NULL) { free(nstatp); errno = ENOMEM; return (-1); } memcpy(newmsg, msg, msglen); newmsglen = msglen; if (ns_samename(key->alg, NS_TSIG_ALG_HMAC_MD5) != 1) dstkey = NULL; else dstkey = dst_buffer_to_key(key->name, KEY_HMAC_MD5, NS_KEY_TYPE_AUTH_ONLY, NS_KEY_PROT_ANY, key->data, key->len); if (dstkey == NULL) { errno = EINVAL; free(nstatp); free(newmsg); return (-1); } nstatp->nscount = 1; siglen = sizeof(sig); ret = ns_sign(newmsg, &newmsglen, bufsize, NOERROR, dstkey, NULL, 0, sig, &siglen, 0); if (ret < 0) { free (nstatp); free (newmsg); dst_free_key(dstkey); if (ret == NS_TSIG_ERROR_NO_SPACE) errno = EMSGSIZE; else if (ret == -1) errno = EINVAL; return (ret); } if (newmsglen > PACKETSZ || nstatp->options & RES_USEVC) usingTCP = 1; if (usingTCP == 0) nstatp->options |= RES_IGNTC; else nstatp->options |= RES_USEVC; /* * Stop res_send printing the answer. */ nstatp->options &= ~RES_DEBUG; nstatp->pfcode &= ~RES_PRF_REPLY; retry: len = res_nsend(nstatp, newmsg, newmsglen, answer, anslen); if (len < 0) { free (nstatp); free (newmsg); dst_free_key(dstkey); return (len); } ret = ns_verify(answer, &len, dstkey, sig, siglen, NULL, NULL, &tsig_time, nstatp->options & RES_KEEPTSIG); if (ret != 0) { Dprint((statp->options & RES_DEBUG) || ((statp->pfcode & RES_PRF_REPLY) && (statp->pfcode & RES_PRF_HEAD1)), (stdout, ";; got answer:\n")); DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_REPLY), (stdout, "%s", ""), answer, (anslen > len) ? len : anslen); if (ret > 0) { Dprint(statp->pfcode & RES_PRF_REPLY, (stdout, ";; server rejected TSIG (%s)\n", p_rcode(ret))); } else { Dprint(statp->pfcode & RES_PRF_REPLY, (stdout, ";; TSIG invalid (%s)\n", p_rcode(-ret))); } free (nstatp); free (newmsg); dst_free_key(dstkey); if (ret == -1) errno = EINVAL; else errno = ENOTTY; return (-1); } hp = (HEADER *) answer; if (hp->tc && !usingTCP && (statp->options & RES_IGNTC) == 0U) { nstatp->options &= ~RES_IGNTC; usingTCP = 1; goto retry; } Dprint((statp->options & RES_DEBUG) || ((statp->pfcode & RES_PRF_REPLY) && (statp->pfcode & RES_PRF_HEAD1)), (stdout, ";; got answer:\n")); DprintQ((statp->options & RES_DEBUG) || (statp->pfcode & RES_PRF_REPLY), (stdout, "%s", ""), answer, (anslen > len) ? len : anslen); Dprint(statp->pfcode & RES_PRF_REPLY, (stdout, ";; TSIG ok\n")); free (nstatp); free (newmsg); dst_free_key(dstkey); return (len); }
isc_result_t ns_verify_tcp(u_char *msg, unsigned *msglen, ns_tcp_tsig_state *state, int required) { HEADER *hp = (HEADER *)msg; u_char *recstart, *rdatastart, *sigstart; unsigned sigfieldlen, otherfieldlen; u_char *cp, *eom = msg + *msglen, *cp2; char name[MAXDNAME], alg[MAXDNAME]; u_char buf[MAXDNAME]; int n, type, length, fudge, id, error; time_t timesigned; if (msg == NULL || msglen == NULL || state == NULL) return ISC_R_INVALIDARG; state->counter++; if (state->counter == 0) return (ns_verify(msg, msglen, state->key, state->sig, state->siglen, state->sig, &state->siglen, ×igned, 0)); if (state->siglen > 0) { u_int16_t siglen_n = htons(state->siglen); dst_verify_data(SIG_MODE_INIT, state->key, &state->ctx, NULL, 0, NULL, 0); dst_verify_data(SIG_MODE_UPDATE, state->key, &state->ctx, (u_char *)&siglen_n, INT16SZ, NULL, 0); dst_verify_data(SIG_MODE_UPDATE, state->key, &state->ctx, state->sig, state->siglen, NULL, 0); state->siglen = 0; } cp = recstart = ns_find_tsig(msg, eom); if (recstart == NULL) { if (required) return ISC_R_NO_TSIG; dst_verify_data(SIG_MODE_UPDATE, state->key, &state->ctx, msg, *msglen, NULL, 0); return ISC_R_SUCCESS; } hp->arcount = htons(ntohs(hp->arcount) - 1); dst_verify_data(SIG_MODE_UPDATE, state->key, &state->ctx, msg, (unsigned)(recstart - msg), NULL, 0); /* Read the key name. */ n = dn_expand(msg, eom, cp, name, MAXDNAME); if (n < 0) return ISC_R_FORMERR; cp += n; /* Read the type. */ BOUNDS_CHECK(cp, 2*INT16SZ + INT32SZ + INT16SZ); GETSHORT(type, cp); if (type != ns_t_tsig) return ISC_R_NO_TSIG; /* Skip the class and TTL, save the length. */ cp += INT16SZ + INT32SZ; GETSHORT(length, cp); if (eom - cp != length) return ISC_R_FORMERR; /* Read the algorithm name. */ rdatastart = cp; n = dn_expand(msg, eom, cp, alg, MAXDNAME); if (n < 0) return ISC_R_FORMERR; if (ns_samename(alg, NS_TSIG_ALG_HMAC_MD5) != 1) return ISC_R_BADKEY; cp += n; /* Verify that the key used is OK. */ if ((ns_samename(state->key->dk_key_name, name) != 1 || state->key->dk_alg != KEY_HMAC_MD5)) return ISC_R_BADKEY; /* Read the time signed and fudge. */ BOUNDS_CHECK(cp, INT16SZ + INT32SZ + INT16SZ); cp += INT16SZ; GETLONG(timesigned, cp); GETSHORT(fudge, cp); /* Read the signature. */ BOUNDS_CHECK(cp, INT16SZ); GETSHORT(sigfieldlen, cp); BOUNDS_CHECK(cp, sigfieldlen); sigstart = cp; cp += sigfieldlen; /* Read the original id and error. */ BOUNDS_CHECK(cp, 2*INT16SZ); GETSHORT(id, cp); GETSHORT(error, cp); /* Parse the other data. */ BOUNDS_CHECK(cp, INT16SZ); GETSHORT(otherfieldlen, cp); BOUNDS_CHECK(cp, otherfieldlen); cp += otherfieldlen; if (cp != eom) return ISC_R_FORMERR; /* * Do the verification. */ /* Digest the time signed and fudge. */ cp2 = buf; PUTSHORT(0, cp2); /* Top 16 bits of time. */ PUTLONG(timesigned, cp2); PUTSHORT(NS_TSIG_FUDGE, cp2); dst_verify_data(SIG_MODE_UPDATE, state->key, &state->ctx, buf, (unsigned)(cp2 - buf), NULL, 0); n = dst_verify_data(SIG_MODE_FINAL, state->key, &state->ctx, NULL, 0, sigstart, sigfieldlen); if (n < 0) return ISC_R_BADSIG; if (sigfieldlen > sizeof(state->sig)) return ISC_R_BADSIG; if (sigfieldlen > sizeof(state->sig)) return ISC_R_NOSPACE; memcpy(state->sig, sigstart, sigfieldlen); state->siglen = sigfieldlen; /* Verify the time. */ if (abs(timesigned - time(NULL)) > fudge) return ISC_R_BADTIME; *msglen = recstart - msg; if (error != NOERROR) return ns_rcode_to_isc (error); return ISC_R_SUCCESS; }
NS_PRIVATE NsError _ns_pixel_proc_subsample_lum ( NsPixelType pixel_type, nsconstpointer src_pixels, nssize src_width, nssize src_height, nssize src_length, nssize src_row_align, nspointer dest_pixels, nssize dest_width, nssize dest_height, nssize dest_length, nssize dest_row_align, nsdouble scale_x, nsdouble scale_y, nsdouble scale_z, NsProgress *progress ) { SUBSAMPLER ss; int ( *func )( SUBSAMPLER* ); nsconstpointer src_slice; nssize src_bytes_per_slice; nspointer dest_slice; nspointer dest_end; nssize dest_bytes_per_slice; nsint index; nssize z; /* This implementation cannot handle aligned scan lines. */ ns_verify( 1 == src_row_align ); ns_verify( 1 == dest_row_align ); InitSubsampler( &ss, ( nsint )src_width, ( nsint )src_height, ( nsint )src_length, scale_x / 100.0, scale_y / 100.0, scale_z / 100.0 ); ns_verify( ( nsint )dest_width == ss.newxdim ); ns_verify( ( nsint )dest_height == ss.newydim ); ns_verify( ( nsint )dest_length == ss.newzdim ); func = NULL; switch( pixel_type ) { case NS_PIXEL_LUM_U8: func = SubsampleLum1ub; break; case NS_PIXEL_LUM_U12: func = SubsampleLum1us12; break; case NS_PIXEL_LUM_U16: func = SubsampleLum1us16; break; default: ns_assert_not_reached(); } ns_assert( NULL != func ); dest_slice = dest_pixels; src_bytes_per_slice = ns_pixel_buffer_slice_size( pixel_type, src_width, src_height, src_row_align ); dest_bytes_per_slice = ns_pixel_buffer_slice_size( pixel_type, dest_width, dest_height, dest_row_align ); dest_end = NS_OFFSET_POINTER( void, dest_pixels, dest_length * dest_bytes_per_slice ); ns_progress_set_title( progress, _ns_pixel_proc_subsample.title ); ns_progress_update( progress, NS_PROGRESS_BEGIN ); z = 0; /* TODO: The subsampler library has been altered to handle a volume that is completely in memory. The original code was designed to be fed a series of images from disk and to save a new subsampled volume (as slices) to disk. The library could be re-written to better handle this and could also be multi-threaded, but this was not done to save time. */ while( ! SubsamplerIsComplete( &ss ) ) { if( ns_progress_cancelled( progress ) ) break; ns_progress_update( progress, 100.0f * ( ( nsfloat )z / ( nsfloat )dest_length ) ); ++z; while( ( index = SubsamplerNextIndex( &ss ) ) != -1 ) { /*TEMP!!!!!!!!!!!*///ns_print( "%d ", index ); ns_assert( ( nssize )index < src_length ); src_slice = NS_OFFSET_POINTER( const void, src_pixels, ( nssize )index * src_bytes_per_slice ); if( SubsamplerAddSrcImage( &ss, src_slice ) ) { CleanSubsampler( &ss ); return ns_error_nomem( NS_ERROR_LEVEL_CRITICAL, NS_MODULE ); } } ns_assert( NS_OFFSET_POINTER( void, dest_slice, dest_bytes_per_slice ) <= dest_end ); SubsamplerAddDestImage( &ss, dest_slice ); if( ( func )( &ss ) ) { CleanSubsampler( &ss ); return ns_error_nomem( NS_ERROR_LEVEL_CRITICAL, NS_MODULE ); } /*TEMP!!!!!!!!!!!*///ns_print( "SUB%d ", ss.lastout ); dest_slice = NS_OFFSET_POINTER( void, dest_slice, dest_bytes_per_slice ); } CleanSubsampler( &ss ); ns_progress_update( progress, NS_PROGRESS_END ); return ns_no_error(); }