Beispiel #1
0
char gen_bit(int index) {

    char* R = new char[BLOCK_SIZE];
    copy_bytes(I, R); // R = I
    xor_bytes(S, R); // R = I xor S
    //cout << "R = I xor S = "; print_hex(R, BLOCK_SIZE); cout << endl;

    char* X = f(R); // X = f(R, k)
    //cout << "X = f(R, K) = "; print_hex(X, BLOCK_SIZE); cout << endl;
    
    char* T = new char[BLOCK_SIZE];
    copy_bytes(X, T);
    xor_bytes(I, T); // T = X xor I
    //cout << "T = X xor I = "; print_hex(T, BLOCK_SIZE); cout << endl;
    
    S = f(T); // S = f(T, K)
    //cout << "S = f(T, K) = "; print_hex(S, BLOCK_SIZE); cout << endl;
    
    int i = index / 8;
    index %= 8;
   
    char mask = 1;
    mask <<= index;

    char bit = X[i] & mask;
    if (index == 7) {
        bit >>= 1;
        bit &= 0x7f;
        bit >>= index - 1;
    } else {
Beispiel #2
0
bool CDetour::CreateDetour()
{
	if (!gameconf->GetMemSig(signame, &detour_address))
	{
		g_pSM->LogError(myself, "Could not locate %s - Disabling detour", signame);
		return false;
	}

	if (!detour_address)
	{
		g_pSM->LogError(myself, "Sigscan for %s failed - Disabling detour to prevent crashes", signame);
		return false;
	}

	detour_restore.bytes = copy_bytes((unsigned char *)detour_address, NULL, OP_JMP_SIZE+1);

	/* First, save restore bits */
	for (size_t i=0; i<detour_restore.bytes; i++)
	{
		detour_restore.patch[i] = ((unsigned char *)detour_address)[i];
	}

	JitWriter wr;
	JitWriter *jit = &wr;
	jit_uint32_t CodeSize = 0;
	
	wr.outbase = NULL;
	wr.outptr = NULL;

jit_rewind:

	/* Patch old bytes in */
	if (wr.outbase != NULL)
	{
		copy_bytes((unsigned char *)detour_address, (unsigned char*)wr.outptr, detour_restore.bytes);
	}
	wr.outptr += detour_restore.bytes;

	/* Return to the original function */
	jitoffs_t call = IA32_Jump_Imm32(jit, 0);
	IA32_Write_Jump32_Abs(jit, call, (unsigned char *)detour_address + detour_restore.bytes);

	if (wr.outbase == NULL)
	{
		CodeSize = wr.get_outputpos();
		wr.outbase = (jitcode_t)spengine->AllocatePageMemory(CodeSize);
		spengine->SetReadWrite(wr.outbase);
		wr.outptr = wr.outbase;
		detour_trampoline = wr.outbase;
		goto jit_rewind;
	}

	spengine->SetReadExecute(wr.outbase);

	*trampoline = detour_trampoline;

	return true;
}
int
copy_ROBIN_SETCLIENTID(const ROBIN_SETCLIENTID *from, ROBIN_SETCLIENTID *to)
{
if (copy_RobinHeader(&(from)->hdr, &(to)->hdr)) return ENOMEM;
if (copy_bytes(&(from)->clientid, &(to)->clientid)) return ENOMEM;
if (copy_bytes(&(from)->identity, &(to)->identity)) return ENOMEM;
if (copy_RobinEncryptionKey(&(from)->verifier, &(to)->verifier)) return ENOMEM;
return 0;
}
Beispiel #4
0
template <class LbrStruct> static
void fillNfa(NFA *nfa, lbr_common *c, ReportID report, const depth &repeatMin,
             const depth &repeatMax, u32 minPeriod, enum RepeatType rtype) {
    assert(nfa);

    RepeatStateInfo rsi(rtype, repeatMin, repeatMax, minPeriod);

    DEBUG_PRINTF("selected %s model for {%s,%s} repeat\n",
                 repeatTypeName(rtype), repeatMin.str().c_str(),
                 repeatMax.str().c_str());

    // Fill the lbr_common structure first. Note that the RepeatInfo structure
    // directly follows the LbrStruct.
    const u32 info_offset = sizeof(LbrStruct);
    c->repeatInfoOffset = info_offset;
    c->report = report;

    RepeatInfo *info = (RepeatInfo *)((char *)c + info_offset);
    info->type = verify_u8(rtype);
    info->repeatMin = depth_to_u32(repeatMin);
    info->repeatMax = depth_to_u32(repeatMax);
    info->stateSize = rsi.stateSize;
    info->packedCtrlSize = rsi.packedCtrlSize;
    info->horizon = rsi.horizon;
    info->minPeriod = minPeriod;
    copy_bytes(&info->packedFieldSizes, rsi.packedFieldSizes);
    info->patchCount = rsi.patchCount;
    info->patchSize = rsi.patchSize;
    info->encodingSize = rsi.encodingSize;
    info->patchesOffset = rsi.patchesOffset;

    // Fill the NFA structure.
    nfa->nPositions = repeatMin;
    nfa->streamStateSize = verify_u32(rsi.packedCtrlSize + rsi.stateSize);
    nfa->scratchStateSize = (u32)sizeof(lbr_state);
    nfa->minWidth = verify_u32(repeatMin);
    nfa->maxWidth = repeatMax.is_finite() ? verify_u32(repeatMax) : 0;

    // Fill the lbr table for sparse lbr model.
    if (rtype == REPEAT_SPARSE_OPTIMAL_P) {
        u64a *table = getTable<LbrStruct>(nfa);
        // Adjust table length according to the optimal patch length.
        size_t len = nfa->length;
        assert((u32)repeatMax >= rsi.patchSize);
        len -= sizeof(u64a) * ((u32)repeatMax - rsi.patchSize);
        nfa->length = verify_u32(len);
        info->length = verify_u32(sizeof(RepeatInfo)
                                  + sizeof(u64a) * (rsi.patchSize + 1));
        copy_bytes(table, rsi.table);
    }
}
Beispiel #5
0
make_render_cashes()
{

got_hash = 1;
switch (vs.draw_mode)
	{
	case 4:	/* transparent */
	case 6:
		copy_bytes(vs.ccolor*3+render_form->cmap, tcolor, 3);
		return(make_thash());
	case 5:	/* defocus */
	case 15: /* antialias */
	case 17: /* brighten */
	case 18: /* desaturate */
	case 22: /* emboss */
		return(make_bhash());
	case 7:	/* alt screen source */
	case 12: /* cel pattern */
		return(make_thash());
	case 21: /* darken */
		tcolor[0] = tcolor[1] = tcolor[2] = 0;
		return(make_thash());
	case 1:	/* vspread */
	case 2:	/* h spread */
	case 3:	/* line spread */
	case 25:	/* rad spread */
		vs.randseed = 0;
	case 11: /* glow */
		return(make_glow());
	}
return(1);
}
Beispiel #6
0
u8 *jit_end_loop(u8 *buffer, branch *b) {
    buffer = copy_bytes(buffer, jne_zero);
    b->patch_loc = buffer;
    buffer += 4;
    b->target_loc = buffer;
    return buffer;
}
Beispiel #7
0
/* Are colors distinct enough from each other? */
visible_cmap()
{
int i, j, t;
UBYTE *a, *b;
UBYTE mycolors[6][3];

for (i=0; i<5; i++)
	{
	copy_bytes(render_form->cmap + 3*mc_colors[i], mycolors[i], 3);
	}
for (i=0; i<4; i++)	/* let menu red be invisible if it wants... */
	{
	a = mycolors[i];
	for (j=0; j<4; j++)
		{
		if (i != j)
			{
			b = mycolors[j];
			if (color_dif(a,b) < SEE_T)
				{
				return(0);
				}
			}
		}
	}
return(1);
}
Beispiel #8
0
check_cmap()
{
int i, j, t;
UBYTE *a, *b;
UBYTE mycolors[6][3];

for (i=0; i<4; i++)
	{
	copy_bytes(vf.cmap + 3*mc_colors[i], mycolors[i], 3);
	}
sys5 = 0;
for (i=0; i<4; i++)
	{
	a = mycolors[i];
	for (j=0; j<4; j++)
		{
		if (i != j)
			{
			b = mycolors[j];
			if (color_dif(a,b) < SEE_T)
				{
				sys5 = 1;
				goto BREAKOUT;
				}
			}
		}
	}
BREAKOUT:
if (sys5)
	{
	jset_colors(SYS_C, 4, vs.mcideals);
	for (i=0; i<4; i++)
		mc_colors[i] = i + SYS_C;
	}
}
Beispiel #9
0
  void CountCodePattern::initComparing() {
    // general count stub;
    //  0: lea   count_addr, %eax
    //  6: movl  (%eax), %edx
    //  8: leal  1(%edx), %edx
    // 11: movl  %edx, (%eax)
    // 13: cmpl  %edx, limit
    // ??: jne jump_addr
    //     call recompile_addr
 
    
    Assembler* oldAssembler = theAssembler;
    Assembler* a = theAssembler = new Assembler(100, 100, false, true);

    a->leal(no_reg, initial_count_addr, VMAddressOperand, edx);
    countAddr_offset = a->offset() - sizeof(int32);
    a->movl(edx, 0, NumberOperand,  eax);
    a->incl(eax);
    a->movl(eax,  edx, 0, NumberOperand);
    a->cmpl(0xabcdef01, NumberOperand,   eax); 
    limit_offset = a->offset() - sizeof(int32);
    a->jne(0x87654321, CodeAddressOperand);
    nmAddr_offset = a->offset() - sizeof(int32);
    a->call((int32)Recompile_stub, VMAddressOperand);
    recompileStub_offset = a->offset() - sizeof(int32);

    instsSize = a->offset();
    pattern = (pc_t)AllocateHeap(instsSize, "countStub pattern");
    copy_bytes(a->instsStart, pattern, instsSize);
    *(int32*)&pattern[recompileStub_offset] -= pattern - a->instsStart; // fixup the call
    a->finalize();
    theAssembler = oldAssembler;    
  }
Beispiel #10
0
int gbIso9660 :: extract_recursive(const char * extract_path, vector<tree_entry> * te)
{
	int ret;

	for(unsigned i=0; i< te->size(); i++)
	{
		//Build the complete path
		char * create = new char[strlen(extract_path) + strlen((const char *)(*te)[i].name) + 2];
		strcpy(create, extract_path);
		strcat(create, "/");
		strcat(create, (const char *)(*te)[i].name);

#ifdef DEBUG
		printf("Extracting %s..\n", create);
#endif

		if((*te)[i].childs)
		{
			//Its a folder
			mkdir(create, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
			ret = extract_recursive(create, (*te)[i].childs);
		}
		else //Its a file
			ret = copy_bytes((*te)[i].location * SECTOR_SIZE, (*te)[i].size, create);

		delete [] create;

		if(ret != OK)
			return ERROR_CANT_CREATE;
	};

	return OK;
};
Beispiel #11
0
/* Send a file to out. Used to transmit the preludes.
 */
static int
transmit_file( char *mode, char *name, FILE *out )
{
	const char *prefix;
	char buf[PATH_MAX];
	FILE *in;

	if( !(prefix = im_guess_prefix( argv0, "VIPSHOME" )) )
		return( -1 );

	/* Send it!
	 */
	im_snprintf( buf, PATH_MAX, "%s/share/vips/vips2dj/%s/%s", 
		prefix, mode, name );
	if( !(in = fopen( buf, "r" )) ) {
		im_errormsg( "vips2dj: can't find \"%s\"", name );
		return( -1 );
	}
	if( copy_bytes( in, out ) ) {
		fclose( in );
		return( -1 );
	}
	fclose( in );

	return( 0 );
}
int
copy_ROBIN_STATUSDATA(const ROBIN_STATUSDATA *from, ROBIN_STATUSDATA *to)
{
if (copy_RobinStatus(&(from)->rep, &(to)->rep)) return ENOMEM;
if (copy_bytes(&(from)->data, &(to)->data)) return ENOMEM;
return 0;
}
Beispiel #13
0
char* f(char* m) {
    int n = 2;
    char* message = new char[BLOCK_SIZE * n];
    for (int i = 0; i < n; i++) {
        copy_bytes(m, message + BLOCK_SIZE * i);
    }
    char* e = cipher(message, BLOCK_SIZE * n, K);

    char* res = new char[BLOCK_SIZE];
    copy_bytes(e + BLOCK_SIZE * (n - 1), res);
    
    delete e;
    delete message;

    return res;
}
Beispiel #14
0
u8 *jit_dec_ptr(u8 *buffer, u32 amount) {
    buffer = copy_bytes(buffer, dec_ptr);
    *buffer++ = amount & 0xff;
    *buffer++ = (amount >> 8) & 0xff;
    *buffer++ = (amount >>16) & 0xff;
    *buffer++ = (amount >>24) & 0xff;
    return buffer;
}
Beispiel #15
0
void put_timestamp(char* x) {
    time_t t = time(NULL);
    copy_bytes((char*)&t, x);
    int r = rand();
    xor_bytes((char*)&r, x);
    xor_bytes((char*)&r, x + 4);

}
int
copy_ROBIN_TRANS_APPEND(const ROBIN_TRANS_APPEND *from, ROBIN_TRANS_APPEND *to)
{
if (copy_RobinHeader(&(from)->hdr, &(to)->hdr)) return ENOMEM;
if (copy_RobinToken(&(from)->token, &(to)->token)) return ENOMEM;
if (copy_bytes(&(from)->data, &(to)->data)) return ENOMEM;
return 0;
}
Beispiel #17
0
static int def_realloc(void **ptr, unsigned long size_old, unsigned long size_new)
{
  char *ptr_new = def_alloc(size_new);
  if (!ptr_new) return 0;
  copy_bytes(*ptr, ptr_new, size_old);
  def_dealloc(*ptr);
  *ptr = ptr_new;
  return 1;
}
Beispiel #18
0
mlx_extract()
{
	char filename[13],pathname[80];
	char *ext;
	int nblocks,block;
	register int i;
	long blockptr[MAXNBLOCKS+1];
	long addr[32],leng[32];	/* oversized */
	long end_record= idEND;
	GLOBRECORD *ptr;

	/* load multi-block file containing block to be extracted */
	Dsetdrv(mlt_drive); dsetpath(mlt_path);
	if (!readdatafile(MLXEXTTITL,mlxfile,mlxpath,MLX_EXT,0)) return;
	mlt_drive= Dgetdrv(); Dgetpath(mlt_path,mlt_drive+1);
	mlx_mem= tempmem;

	/* choose block from it */
	nblocks= scan_blocks(heap[mlx_mem].start,blockptr);
	if (!nblocks) return;	/* can't happen? */
	block= select_block(BLOCK2EXT,nblocks,blockptr);
	if ( (block<0) || (block>=nblocks) ) return;

	/* name of data file defaults to block comment */
	Dsetdrv(data_drive); dsetpath(data_path);
	ptr= (GLOBRECORD*)(blockptr[block]);
	copy_bytes(ptr->comment,filename,12L);
	filename[12]=0;
	ext=filename;
	for (i=0; i<9; i++) if (ext[i]=='.') break;
	if (i<9)
		ext+=(i+1);
	else
		ext= ALL_EXT;
	if (getfile(pathname,filename,ext,MLXXDATTITL)<=0) return;
	data_drive= Dgetdrv(); Dgetpath(data_path,data_drive+1);

	/* build file -- */
	i=0;

	/* -- header */
	addr[i]= (long)(&dataheader);
	leng[i++]= sizeof(dataheader);

	/* -- the selected block */
	addr[i]= blockptr[block];
	leng[i++]= blockptr[block+1] - blockptr[block] ;

	/* -- end record */
	addr[i]= (long)(&end_record);
	leng[i++]= 4;

	save_file(filename,pathname,i,addr,leng);

}	/* end mlx_extract() */
Beispiel #19
0
noreturn static void pager_fallback(void) {
        int r;

        r = copy_bytes(STDIN_FILENO, STDOUT_FILENO, (uint64_t) -1, false);
        if (r < 0) {
                log_error_errno(r, "Internal pager failed: %m");
                _exit(EXIT_FAILURE);
        }

        _exit(EXIT_SUCCESS);
}
Beispiel #20
0
int gbIso9660 :: extract_file(const char * file_name, const char * extract_path)
{
	tree_entry * te = get_file_info(file_name);

	//Check for the file data on the tree
	if(!te)
		return ERROR_NOT_FOUND;

	//Create the output file
	return copy_bytes(te->location * SECTOR_SIZE, te->size, extract_path);
};
Beispiel #21
0
int copy_file_fd(const char *from, int fdt, bool try_reflink) {
        _cleanup_close_ int fdf = -1;

        assert(from);
        assert(fdt >= 0);

        fdf = open(from, O_RDONLY|O_CLOEXEC|O_NOCTTY);
        if (fdf < 0)
                return -errno;

        return copy_bytes(fdf, fdt, (off_t) -1, try_reflink);
}
Beispiel #22
0
void *func_detour(void *pfunc, void *detour)
{
	func_t *func = func_register(pfunc);
	
	pr_debug("func_detour %08x %s\n", (uintptr_t)pfunc, func->name_demangled);
	
	assert(!func->has_detour);
	
	/* nop out the entire trampoline page first */
	memset((void *)func->trampoline_addr, 0x90, func->trampoline_size);
	
	/* copy the first 6+ bytes of the function over to the trampoline */
	size_t len = copy_bytes((uint8_t *)func->func_addr,
		(uint8_t *)func->trampoline_addr, 6);
	assert(len >= 6);
	assert(len <= 20);
	assert(len <= func->func_size);
	
	/* save the original bytes */
	func->restore_len = len;
	memcpy(func->restore_buf, (void *)func->func_addr, len);
	
	/* make the original function jump to the detour function */
	func_unprotect(func);
	*((uint8_t  *)(func->func_addr + 0x00)) = 0xff; // jmp far
	*((uint8_t  *)(func->func_addr + 0x01)) = 0x25; // m32
	*((uint32_t *)(func->func_addr + 0x02)) = (uint32_t)&func->detour_addr;
	memset((void *)func->func_addr + 6, 0x90, len - 6); // nop
	func_protect(func);
	
	func->trampoline_dest = func->func_addr + len;
	
	/* make the trampoline jump to the original function */
	*((uint8_t  *)(func->trampoline_addr + len + 0x00)) = 0xff; // jmp far
	*((uint8_t  *)(func->trampoline_addr + len + 0x01)) = 0x25; // m32
	*((uint32_t *)(func->trampoline_addr + len + 0x02)) =
		(uint32_t)&func->trampoline_dest;
	
	func->detour_addr = (uintptr_t)detour;
	func->has_detour  = true;
	
	/*pr_debug("%s\nstruct func @ %p\n"
		"func_addr       = 0x%08x\n"
		"detour_addr     = 0x%08x\n"
		"trampoline_addr = 0x%08x\n",
		__func__, func,
		func->func_addr,
		func->detour_addr,
		func->trampoline_addr);*/
	
	return (void *)func->trampoline_addr;
}
Beispiel #23
0
extern "C" uint16_t att_read_callback(uint16_t handle, uint16_t attribute_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
	//printf("gatt client test, att_read_callback_t handle 0x%04x, offset %u, buffer %p, buffer_size %u\n", handle, offset, buffer, buffer_size);
	switch(test){
		case READ_CHARACTERISTIC_DESCRIPTOR:
		case READ_CHARACTERISTIC_VALUE:
			result_counter++;
			if (buffer){
				return copy_bytes((uint8_t *)short_value, short_value_length, offset, buffer, buffer_size);
			}
			return short_value_length;
		case READ_LONG_CHARACTERISTIC_DESCRIPTOR:
		case READ_LONG_CHARACTERISTIC_VALUE:
			result_counter++;
			if (buffer) {
				return copy_bytes((uint8_t *)long_value, long_value_length, offset, buffer, buffer_size);
			}
			return long_value_length;
		default:
			break;
	}
	return 0;
}
int
copy_ROBIN_TRANS_ABORT(const ROBIN_TRANS_ABORT *from, ROBIN_TRANS_ABORT *to)
{
if (copy_RobinHeader(&(from)->hdr, &(to)->hdr)) return ENOMEM;
if (copy_RobinTransFlags(&(from)->flags, &(to)->flags)) return ENOMEM;
if (copy_RobinToken(&(from)->token, &(to)->token)) return ENOMEM;
if ((from)->txid) {
(to)->txid = malloc(sizeof(*(to)->txid));
if ((to)->txid == NULL) return ENOMEM;
if (copy_bytes((from)->txid, (to)->txid)) return ENOMEM;
} else
(to)->txid = NULL;
return 0;
}
//==================================================================
// Copies bytes from src to this array.
//==================================================================
void CByteArray::CopyBytes(unsigned char *pSource, int i)
{
	if( !pSource )
	{
		printf("Invalid source passed in.\n");
	}

	// Copy the bytes
	copy_bytes(pSource, m_codeBytes, i);

	// Increment index by i.
	m_curIndex += i;

}
Beispiel #26
0
/**
 * Take in a collection of exclusive sub engines and produces a tamarama, also
 * returns via out_top_remap, a mapping indicating how tops in the subengines in
 * relate to the tamarama's tops.
 */
aligned_unique_ptr<NFA> buildTamarama(const TamaInfo &tamaInfo, const u32 queue,
                        map<pair<const NFA *, u32>, u32> &out_top_remap) {
    vector<u32> top_base;
    remapTops(tamaInfo, top_base, out_top_remap);

    size_t subSize = tamaInfo.subengines.size();
    DEBUG_PRINTF("subSize:%lu\n", subSize);
    size_t total_size =
        sizeof(NFA) +               // initial NFA structure
        sizeof(Tamarama) +          // Tamarama structure
        sizeof(u32) * subSize +     // base top event value for subengines,
                                    // used for top remapping at runtime
        sizeof(u32) * subSize + 64; // offsets to subengines in bytecode and
                                    // padding for subengines

    for (const auto &sub : tamaInfo.subengines) {
        total_size += ROUNDUP_CL(sub->length);
    }

    // use subSize as a sentinel value for no active subengines,
    // so add one to subSize here
    u32 activeIdxSize = calcPackedBytes(subSize + 1);
    aligned_unique_ptr<NFA> nfa = aligned_zmalloc_unique<NFA>(total_size);
    nfa->type = verify_u8(TAMARAMA_NFA_0);
    nfa->length = verify_u32(total_size);
    nfa->queueIndex = queue;

    char *ptr = (char *)nfa.get() + sizeof(NFA);
    char *base_offset = ptr;
    Tamarama *t = (Tamarama *)ptr;
    t->numSubEngines = verify_u32(subSize);
    t->activeIdxSize = verify_u8(activeIdxSize);

    ptr += sizeof(Tamarama);
    copy_bytes(ptr, top_base);
    ptr += byte_length(top_base);

    u32 *offsets = (u32*)ptr;
    char *sub_nfa_offset = ptr + sizeof(u32) * subSize;
    copyInSubnfas(base_offset, *nfa, tamaInfo, offsets, sub_nfa_offset,
                  activeIdxSize);
    assert((size_t)(sub_nfa_offset - (char *)nfa.get()) <= total_size);
    return nfa;
}
Beispiel #27
0
int main( int argc, char *argv[] )
{
    Huint   *src;
    Huint   *dst;

    src = (Huint*)malloc( SIZE );
    dst = (Huint*)malloc( SIZE );

    copy_bytes( (Hubyte*)dst, (Hubyte*)src, SIZE );
    copy_short( (Hushort*)dst, (Hushort*)src, SIZE/2 );
    copy_int( dst, src, SIZE/4 );
    copy_mem( dst, src, SIZE );
    copy_smart( dst, src, SIZE );

    free( src );
    free( dst );

    return 0;
}
Beispiel #28
0
  void CountCodePattern::initCounting() {
    // general count stub; 
    //  0: lea   count_addr, %eax
    //  6: movl  (%eax), %edx
    //  8: leal  1(%edx), %edx
    // 11: movl  %edx, (%eax)
    // 13: jmp    jump_addr
    // 18: <end>
    
    Assembler* oldAssembler = theAssembler;
    Assembler* a = theAssembler = new Assembler(100, 100, false, true);
    a->incl( no_reg, initial_count_addr, VMAddressOperand);
    countAddr_offset = a->offset() - sizeof(int);
    a->jmp(initial_nmAddr, CodeAddressOperand);
    nmAddr_offset = a->offset() - sizeof(int);
    instsSize = a->offset();

    pattern = (pc_t)AllocateHeap(instsSize, "countStub pattern");
    copy_bytes(a->instsStart, pattern, instsSize);
    a->finalize();
    theAssembler = oldAssembler;
  }
Beispiel #29
0
/* Fill the buffer with available data, skipping anything before start_off.  
 * Call repeatedly until curr_off is past the end of the structure being 
 * filled, or a non-zero value is returned, indicating an error */
int fill(server *srv, chunkqueue *cq, void *buf, size_t size,
		off_t start_off, off_t *curr_off)
{
	size_t bytes, buf_off;

	if (*curr_off < start_off) {
		bytes = discard_bytes(srv, cq, start_off - *curr_off);
		DEBUGLOG("sd", "Discarding bytes:", bytes);
		*curr_off += bytes;
		return 0;
	}

	buf_off = *curr_off - start_off;
	bytes = copy_bytes(srv, ((char *)buf) + buf_off, cq, size - buf_off);
	*curr_off += bytes;

	if (*curr_off < start_off + size) {
		DEBUGLOG("s", "Filling incomplete");
	}

	return 0;
}
Beispiel #30
0
static int fd_copy_regular(int df, const char *from, const struct stat *st, int dt, const char *to) {
        _cleanup_close_ int fdf = -1, fdt = -1;
        int r, q;

        assert(from);
        assert(st);
        assert(to);

        fdf = openat(df, from, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
        if (fdf < 0)
                return -errno;

        fdt = openat(dt, to, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, st->st_mode & 07777);
        if (fdt < 0)
                return -errno;

        r = copy_bytes(fdf, fdt, (off_t) -1, true);
        if (r < 0) {
                unlinkat(dt, to, 0);
                return r;
        }

        if (fchown(fdt, st->st_uid, st->st_gid) < 0)
                r = -errno;

        if (fchmod(fdt, st->st_mode & 07777) < 0)
                r = -errno;

        q = close(fdt);
        fdt = -1;

        if (q < 0) {
                r = -errno;
                unlinkat(dt, to, 0);
        }

        return r;
}