Exemplo n.º 1
0
static Elf_Bhdr *add_boot_note(Elf_Bhdr *bhdr, const char *name,
                               unsigned type, const char *desc, unsigned descsz)
{
    Elf_Nhdr nhdr;
    unsigned ent_size, new_size, pad;
    char *addr;

    if (!bhdr)
        return NULL;

    nhdr.n_namesz = name? strlen(name)+1 : 0;
    nhdr.n_descsz = descsz;
    nhdr.n_type = type;
    ent_size = sizeof(nhdr) + padded(nhdr.n_namesz) + padded(nhdr.n_descsz);
    if (bhdr->b_size + ent_size > 0xffff) {
        printf("Boot notes too big\n");
        free(bhdr);
        return NULL;
    }
    if (bhdr->b_size + ent_size > bhdr->b_checksum) {
        do {
            new_size = bhdr->b_checksum * 2;
        } while (new_size < bhdr->b_size + ent_size);
        if (new_size > 0xffff)
            new_size = 0xffff;
        debug("expanding boot note size to %u\n", new_size);
#ifdef HAVE_REALLOC
        bhdr = realloc(bhdr, new_size);
        bhdr->b_checksum = new_size;
#else
        printf("Boot notes too big\n");
        free(bhdr);
        return NULL;
#endif
    }

    addr = (char *) bhdr;
    addr += bhdr->b_size;
    memcpy(addr, &nhdr, sizeof(nhdr));
    addr += sizeof(nhdr);

    if (name && nhdr.n_namesz) {
        memcpy(addr, name, nhdr.n_namesz);
        addr += nhdr.n_namesz;
        pad = padded(nhdr.n_namesz) - nhdr.n_namesz;
        memset(addr, 0, pad);
        addr += pad;
    }

    memcpy(addr, desc, nhdr.n_descsz);
    addr += nhdr.n_descsz;
    pad = padded(nhdr.n_descsz) - nhdr.n_descsz;
    memset(addr, 0, pad);

    bhdr->b_size += ent_size;
    bhdr->b_records++;
    return bhdr;
}
Exemplo n.º 2
0
    size_t process(univector<T, Tag>& output, univector_ref<const T> input)
    {
        const itype required_input_size = input_size_for_output(output.size());

        const itype input_size = input.size();
        for (size_t i = 0; i < output.size(); i++)
        {
            const itype intermediate_index =
                output_position_to_intermediate(static_cast<itype>(i) + output_position);
            const itype intermediate_start = intermediate_index - taps + 1;
            const std::lldiv_t input_pos =
                floor_div(intermediate_start + interpolation_factor - 1, interpolation_factor);
            const itype input_start        = input_pos.quot; // first input sample
            const itype tap_start          = interpolation_factor - 1 - input_pos.rem;
            const univector_ref<T> tap_ptr = filter.slice(static_cast<size_t>(tap_start * depth));

            if (input_start >= input_position + input_size)
            {
                output[i] = T(0);
            }
            else if (input_start >= input_position)
            {
                output[i] = dotproduct(input.slice(input_start - input_position, depth), tap_ptr);
            }
            else
            {
                const itype prev_count = input_position - input_start;
                output[i]              = dotproduct(delay.slice(size_t(depth - prev_count)), tap_ptr) +
                            dotproduct(input.slice(0, size_t(depth - prev_count)),
                                       tap_ptr.slice(size_t(prev_count), size_t(depth - prev_count)));
            }
        }

        if (required_input_size >= depth)
        {
            delay.slice(0, delay.size()) = padded(input.slice(size_t(required_input_size - depth)));
        }
        else
        {
            delay.truncate(size_t(depth - required_input_size)) = delay.slice(size_t(required_input_size));
            delay.slice(size_t(depth - required_input_size))    = padded(input);
        }

        input_position += required_input_size;
        output_position += output.size();

        return required_input_size;
    }
Exemplo n.º 3
0
int keystore_rsa_priv_enc(int flen, const unsigned char* from, unsigned char* to, RSA* rsa,
        int padding) {
    ALOGV("keystore_rsa_sign(%d, %p, %p, %p, %d)", flen, from, to, rsa, padding);

    int num = RSA_size(rsa);
    UniquePtr<uint8_t> padded(new uint8_t[num]);
    if (padded.get() == NULL) {
        ALOGE("could not allocate padded signature");
        return 0;
    }

    switch (padding) {
    case RSA_PKCS1_PADDING:
        if (!RSA_padding_add_PKCS1_type_1(padded.get(), num, from, flen)) {
            return 0;
        }
        break;
    case RSA_X931_PADDING:
        if (!RSA_padding_add_X931(padded.get(), num, from, flen)) {
            return 0;
        }
        break;
    case RSA_NO_PADDING:
        if (!RSA_padding_add_none(padded.get(), num, from, flen)) {
            return 0;
        }
        break;
    default:
        ALOGE("Unknown padding type: %d", padding);
        return 0;
    }

    uint8_t* key_id = reinterpret_cast<uint8_t*>(RSA_get_ex_data(rsa, rsa_key_handle));
    if (key_id == NULL) {
        ALOGE("key had no key_id!");
        return 0;
    }

    Keystore_Reply reply;
    if (keystore_cmd(CommandCodes[SIGN], &reply, 2, strlen(reinterpret_cast<const char*>(key_id)),
            key_id, static_cast<size_t>(num), reinterpret_cast<const uint8_t*>(padded.get()))
            != NO_ERROR) {
        ALOGE("There was an error during rsa_mod_exp");
        return 0;
    }

    const size_t replyLen = reply.length();
    if (replyLen <= 0) {
        ALOGW("No valid signature returned");
        return 0;
    }

    memcpy(to, reply.get(), replyLen);

    ALOGV("rsa=%p keystore_rsa_sign => returning %p len %llu", rsa, to,
            (unsigned long long) replyLen);
    return static_cast<int>(replyLen);
}
Exemplo n.º 4
0
    size_t skip(size_t output_size, univector_ref<const T> input)
    {
        const itype required_input_size = input_size_for_output(output_size);

        if (required_input_size >= depth)
        {
            delay.slice(0, delay.size()) = padded(input.slice(size_t(required_input_size - depth)));
        }
        else
        {
            delay.truncate(size_t(depth - required_input_size)) = delay.slice(size_t(required_input_size));
            delay.slice(size_t(depth - required_input_size))    = padded(input);
        }

        input_position += required_input_size;
        output_position += output_size;

        return required_input_size;
    }
Exemplo n.º 5
0
void convolve_filter<T>::set_data(const univector<T>& data)
{
    univector<T> input(fft.size);
    const T ifftsize = reciprocal(T(fft.size));
    for (size_t i = 0; i < ir_segments.size(); i++)
    {
        segments[i].resize(block_size);
        ir_segments[i].resize(block_size, 0);
        input = padded(data.slice(i * block_size, block_size));

        fft.execute(ir_segments[i], input, temp, dft_pack_format::Perm);
        process(ir_segments[i], ir_segments[i] * ifftsize);
    }
    saved_input.resize(block_size, 0);
    scratch.resize(block_size * 2);
    premul.resize(block_size, 0);
    cscratch.resize(block_size);
    overlap.resize(block_size, 0);
}
Exemplo n.º 6
0
void convolve_filter<T>::process_buffer(T* output, const T* input, size_t size)
{
    size_t processed = 0;
    while (processed < size)
    {
        const size_t processing = std::min(size - processed, block_size - input_position);
        internal::builtin_memcpy(saved_input.data() + input_position, input + processed,
                                 processing * sizeof(T));

        process(scratch, padded(saved_input));
        fft.execute(segments[position], scratch, temp, dft_pack_format::Perm);

        if (input_position == 0)
        {
            process(premul, zeros());
            for (size_t i = 1; i < segments.size(); i++)
            {
                const size_t n = (position + i) % segments.size();
                fft_multiply_accumulate(premul, ir_segments[i], segments[n], dft_pack_format::Perm);
            }
        }
        fft_multiply_accumulate(cscratch, premul, ir_segments[0], segments[position], dft_pack_format::Perm);

        fft.execute(scratch, cscratch, temp, dft_pack_format::Perm);

        process(make_univector(output + processed, processing),
                scratch.slice(input_position) + overlap.slice(input_position));

        input_position += processing;
        if (input_position == block_size)
        {
            input_position = 0;
            process(saved_input, zeros());

            internal::builtin_memcpy(overlap.data(), scratch.data() + block_size, block_size * sizeof(T));

            position = position > 0 ? position - 1 : segments.size() - 1;
        }

        processed += processing;
    }
}
Exemplo n.º 7
0
static int lreplace( gdcm::Tag const &t, const char *value, gdcm::VL const & vl, gdcm::DataSet &ds ) {
    if ( t.GetGroup() < 0x0008 ) return 0;

    if ( t.IsPrivate() && vl == 0 && ds.FindDataElement( t ) ) {
	gdcm::DataElement de ( ds.GetDataElement( t ) );
	if ( de.GetVR() != gdcm::VR::INVALID ) {
	    if ( de.GetVR() == gdcm::VR::SQ && *value == 0 )
		{ return emptyData( t, ds ); } // Only allowed operation for a Private Tag
	    else { return 0; } //Trying to replace value of a Private Tag
	}
	de.SetByteValue( "", 0 );
	ds.Insert( de );
	return 1;
    } else {
	const gdcm::DictEntry &entry = dicts.GetDictEntry( t );

	if ( entry.GetVR() == gdcm::VR::INVALID || entry.GetVR() == gdcm::VR::UN )
	    { return 0; }

	if ( entry.GetVR() == gdcm::VR::SQ && vl == 0 && value && *value == 0 )
	    { return emptyData( t, ds ); }

	else if ( entry.GetVR() && gdcm::VR::VRBINARY && vl == 0 )
		{ return replaceData( t, ds, entry, "", 0 ); }

	else { // ASCII
	    if ( value ) {
		std::string padded( value, vl);
		if ( vl.IsOdd() && (entry.GetVR() != gdcm::VR::UI) ) { padded += " "; }
		return replaceData( t, ds, entry, padded.c_str(), (gdcm::VL::Type)padded.size() );
	    }
	}
    }

    return 0;
}
Exemplo n.º 8
0
    /**
     * Evaluate the score of the given configurations
     */
    vector<double> FAsTMatch::evaluateConfigs( Mat& image, Mat& templ, vector<Mat>& affine_matrices,
                                               Mat& xs, Mat& ys, bool photometric_invariance ) {
        
        int r1x = 0.5 * (templ.cols - 1),
            r1y = 0.5 * (templ.rows - 1),
            r2x = 0.5 * (image.cols - 1),
            r2y = 0.5 * (image.rows - 1);
        
        int no_of_configs = static_cast<int>(affine_matrices.size());
        int no_of_points  = xs.cols;
        
        /* Use a padded image, to avoid boundary checking */
        Mat padded( image.rows * 3, image.cols, image.type(), Scalar(0.0) );
        image.copyTo( Mat(padded, Rect(0, image.rows, image.cols, image.rows)) );
        
        /* Create a lookup array for our template values based on the given random x and y points */
        int * xs_ptr = xs.ptr<int>(0),
            * ys_ptr = ys.ptr<int>(0);
        
        vector<float> vals_i1( no_of_points );
        for( int i = 0; i < no_of_points; i++ )
            vals_i1[i] = templ.at<float>(ys_ptr[i] - 1, xs_ptr[i] - 1);
        
        
        /* Recenter our indices */
        Mat xs_centered = xs.clone() - (r1x + 1),
            ys_centered = ys.clone() - (r1y + 1);
        
        int * xs_ptr_cent = xs_centered.ptr<int>(0),
            * ys_ptr_cent = ys_centered.ptr<int>(0);
        
        vector<double> distances(no_of_configs, 0.0 );
        
        /* Calculate the score for each configurations on each of our randomly sampled points */
        tbb::parallel_for( 0, no_of_configs, 1, [&](int i) {

            float a11 = affine_matrices[i].at<float>(0, 0),
                  a12 = affine_matrices[i].at<float>(0, 1),
                  a13 = affine_matrices[i].at<float>(0, 2),
                  a21 = affine_matrices[i].at<float>(1, 0),
                  a22 = affine_matrices[i].at<float>(1, 1),
                  a23 = affine_matrices[i].at<float>(1, 2);
            
            double tmp_1 = (r2x + 1) + a13 + 0.5;
            double tmp_2 = (r2y + 1) + a23 + 0.5 + 1 * image.rows;
            double score = 0.0;
            
            if(!photometric_invariance) {
                for( int j = 0; j < no_of_points; j++ ) {
                    int target_x = int( a11 * xs_ptr_cent[j] + a12 * ys_ptr_cent[j] + tmp_1 ),
                        target_y = int( a21 * xs_ptr_cent[j] + a22 * ys_ptr_cent[j] + tmp_2 );
                    
                    score += abs(vals_i1[j] - padded.at<float>(target_y - 1, target_x - 1) );
                }
            }
            else {
                vector<double> xs_target(no_of_points),
                               ys_target(no_of_points);
                
                double  sum_x         = 0.0,
                        sum_y         = 0.0,
                        sum_x_squared = 0.0,
                        sum_y_squared = 0.0;
                
                for( int j = 0; j < no_of_points; j++ ) {
                    int target_x = int( a11 * xs_ptr_cent[j] + a12 * ys_ptr_cent[j] + tmp_1 ),
                        target_y = int( a21 * xs_ptr_cent[j] + a22 * ys_ptr_cent[j] + tmp_2 );
                    
                    float xi = vals_i1[j],
                          yi = padded.at<float>(target_y - 1, target_x - 1);
                    
                    xs_target[j] = xi;
                    ys_target[j] = yi;
                    
                    sum_x += xi;
                    sum_y += yi;
                    
                    sum_x_squared += (xi * xi);
                    sum_y_squared += (yi * yi);
                }
                
                double  epsilon = 1e-7;
                double  mean_x = sum_x / no_of_points,
                        mean_y = sum_y / no_of_points,
                        sigma_x = sqrt((sum_x_squared - ( sum_x * sum_x ) / no_of_points ) / no_of_points) + epsilon,
                        sigma_y = sqrt((sum_y_squared - ( sum_y * sum_y ) / no_of_points ) / no_of_points) + epsilon;

                double sigma_div = sigma_x / sigma_y;
                double temp = -mean_x + sigma_div * mean_y;
                
                
                for( int j = 0; j < no_of_points; j++ )
                    score += fabs( xs_target[j] - sigma_div * ys_target[j] + temp );
            }
            
            distances[i] = score / no_of_points;
        });
        
        return distances;
    }
Exemplo n.º 9
0
//
// Loads the raw reflection data directly from a *.d3dbsp file and stores it in the "dest" buffer
// Returns TRUE if successful
// Will fail if destSize does not match the length of the reflection data in the file
//
BOOL LoadBSPReflectionData(const char* bspPath, BYTE* dest, size_t destSize)
{
	FILE* h;
	if (fopen_s(&h, bspPath, "rb") != 0)
	{
		printf("ERROR: Could not open file %s\n", bspPath);
		return FALSE;
	}

	DWORD magic = 0;
	fread(&magic, 4, 1, h);
	if (magic != 'PSBI')
	{
		printf("ERROR: File %s appears to be corrupt\n", bspPath);
		fclose(h);
		return FALSE;
	}

	DWORD version = 0;
	fread(&version, 4, 1, h);
	if (version != 45)
	{
		printf("ERROR: D3DBSP %s is version %d when it should be version %d\n", bspPath, version, 45);
		fclose(h);
		return FALSE;
	}

	DWORD lumpCount = 0;
	fread(&lumpCount, 4, 1, h);
	DWORD* index = new DWORD[lumpCount * 2];
	fread(index, 8, lumpCount, h);

	DWORD offset = ftell(h);
	DWORD size = 0;

	for (DWORD i = 0; i < lumpCount*2; i+=2)
	{
		if (index[i] == 0x29)
		{
			size = index[i + 1];
			break;
		}
		else
			offset += padded(index[i + 1]);
	}

	//
	// If the size of the reflection data in the D3DBSP file does not match the size
	// of the reflection data in the fastfile, the user likely added or removed one
	// or more reflection probes from the map and then rebuilt the BSP
	//
	if (padded(size) != padded(destSize))
	{
		printf("ERROR: Fastfile probe count does not match the BSP probe count - please rebuild fastfile\n");
		fclose(h);
		return FALSE;
	}

	fseek(h, offset, SEEK_SET);
	fread(dest, 1, size, h);
	fclose(h);
	
	return TRUE;
}