Exemple #1
0
int
send_request(Request req)
{
	if (! req->socket ) {
		req->socket = connect_socket(req->host,req->port,0);
		if (! req->socket) {
			error("Failed to connect to %s:%i\n",req->host,req->port);
			return 0;
		}
		add_req_socket(req->socket->fd);
		return 0;
	}
	if (req->length < 0) {
		str cmd = _("%s %s HTTP/1.1\r\n",req->method,req->path);
		write_socket(req->socket,cmd);
		request_headers(req,_("Host"),req->host);
		send_headers(req->socket,req->headers);
		req->length = outbound_content_length(req->contents,req->raw_contents);	
		return req->contents != NULL || req->raw_contents != NULL ;
	}
	req->written += req->contents ?
			send_contents(req->socket,req->contents,is_chunked(req->headers)) :
		req->raw_contents ?
			send_raw_contents(req->socket,req->raw_contents,req->written,0):
			0;
	if (is_chunked(req->headers) && req->written >= req->length)
		write_chunk(req->socket,NULL,0);
	return req->written < req->length;
}
Exemple #2
0
/**
 * Encodes the strings and writes the encoded data to file.
 * @param out File to write to
 * @param head Head of list of strings to encode & write
 * @param label_prefix
 */
static void write_huffman_strings(FILE *out, const string_list_t *head,
                                  const char *label_prefix)
{
    const string_list_t *string;
    int string_id = 0;
    for (string = head; string != NULL; string = string->next) {
        char strlabel[256];
        char strcomment[80];

        sprintf(strlabel, "%sString%d", label_prefix, string_id++);

        strcpy(strcomment, "\"");
        if (strlen((char *)string->text) < 40) {
            strcat(strcomment, (char *)string->text);
        } else {
            strncat(strcomment, (char *)string->text, 37);
            strcat(strcomment, "...");
        }
        strcat(strcomment, "\"");

        /* Write encoded data */
        write_chunk(out, strlabel, strcomment,
                    string->huff_data, string->huff_size, 16);
    }
}
Exemple #3
0
void create_mgif_delta()
  {
  void *d;
  long siz;

  //conv_2_rgb();
  init_palmap();
  if (BZTRATA) create_color_state();
  set_max_color_change(BZTRATA);
  create_low_quality();
  if (delta_data_size+frame_delta_size>FRAME_LEN)
     {
     create_mgif_lzw();
     return;
     }
  if (!frame_delta_size) return;
  reduce_palette();
  filter_colors(frame_delta,frame_delta_size,color_map);
  d=join_two_blocks(delta_data,frame_delta,delta_data_size,frame_delta_size,&siz);
  init_lzw_compressor(8);
  memset(lzw_buffer,0,sizeof(lzw_buffer));
  siz=lzw_encode(d,lzw_buffer,siz);
  done_lzw_compressor();
  free(d);
  if (siz>FRAME_LEN)
     {
     create_mgif_lzw();
     return;
     }
  write_chunk(MGIF_DELTA,siz,lzw_buffer);
  create_mgif_pal();
  }
VOID png_write(struct RecorderData *data, APTR fh, APTR argb, ULONG modulo, ULONG width, ULONG height, ULONG dupcount)
{
	struct png_header ihdr;
	ULONG length, length2;
	UBYTE *buffer;

	buffer = data->writebuffer;

	ihdr.width = width;
	ihdr.height = height;
	ihdr.depth = 8;
	ihdr.colortype = 2; /* RGB */
	ihdr.compression = 0;
	ihdr.filter = 0;
	ihdr.interlaced = 0;

	length = write_chunk(buffer, sizeof(ihdr), LE_SWAPLONG_C(MAKE_ID('I','H','D','R')), &ihdr);
	length2 = png_encode(&buffer[length], data->writebuffersize - length - 8, argb, modulo, width, height);

	if (length2)
	{
		static const struct png_iend iend = { { 0, LE_SWAPLONG_C(MAKE_ID('I','E','N','D')) }, 0xae426082 };
		ULONG i;

		length += length2;

		qcopy((APTR)&iend, &buffer[length], sizeof(iend));

		for (i = 0; i < dupcount; i++)
		{
			dowrite(fh, buffer, length);
		}
	}
}
Exemple #5
0
void create_mgif_lzw()
  {
  int siz;

  //conv_2_rgb();
  init_palmap();
  create_mgif_pal();
  filter_colors(frame_buffer,FRAME_LEN,color_map);
  create_last_frame(frame_buffer,last_frame,hipal,FRAME_LEN);
  init_lzw_compressor(8);
  memset(lzw_buffer,0,sizeof(lzw_buffer));
  siz=lzw_encode(frame_buffer,lzw_buffer,FRAME_LEN);
  if (siz>FRAME_LEN) write_chunk(MGIF_COPY,FRAME_LEN,frame_buffer);
  else write_chunk(MGIF_LZW,siz,lzw_buffer);
  done_lzw_compressor(8);
  }
Exemple #6
0
/* Shuffle large input stream by splitting into chunks */
int shuffle_by_chunks() {
    long i = 0, l = 0;
    int fidcounter = 0;
    char filename[MAX_STRING_LENGTH];
    CREC *array;
    FILE *fin = stdin, *fid;
    array = malloc(sizeof(CREC) * array_size);
    
    fprintf(stderr,"SHUFFLING COOCCURRENCES\n");
    if(verbose > 0) fprintf(stderr,"array size: %lld\n", array_size);
    sprintf(filename,"%s_%04d.bin",file_head, fidcounter);
    fid = fopen(filename,"w");
    if(fid == NULL) {
        fprintf(stderr, "Unable to open file %s.\n",filename);
        return 1;
    }
    if(verbose > 1) fprintf(stderr, "Shuffling by chunks: processed 0 lines.");
    
    while(1) { //Continue until EOF
        if(i >= array_size) {// If array is full, shuffle it and save to temporary file
            shuffle(array, i-2);
            l += i;
            if(verbose > 1) fprintf(stderr, "\033[22Gprocessed %ld lines.", l);
            write_chunk(array,i,fid);
            fclose(fid);
            fidcounter++;
            sprintf(filename,"%s_%04d.bin",file_head, fidcounter);
            fid = fopen(filename,"w");
            if(fid == NULL) {
                fprintf(stderr, "Unable to open file %s.\n",filename);
                return 1;
            }
            i = 0;
        }
        fread(&array[i], sizeof(CREC), 1, fin);
        if(feof(fin)) break;
        i++;
    }
    shuffle(array, i-1); //Last chunk may be smaller than array_size
    write_chunk(array,i,fid);
    l += i;
    if(verbose > 1) fprintf(stderr, "\033[22Gprocessed %ld lines.\n", l);
    if(verbose > 1) fprintf(stderr, "Wrote %d temporary file(s).\n", fidcounter + 1);
    fclose(fid);
    free(array);
    return shuffle_merge(fidcounter + 1); // Merge and shuffle together temporary files
}
Exemple #7
0
int main (int argc, char const *argv[])
{	
	UNUSED_ARG(argc);
	UNUSED_ARG(argv);

	unsigned i;
	
	memory_pool * mp = mempool_new(POOL_ENTRIES, POOL_ENTRY_SIZE);
	
	void * chunks[POOL_ENTRIES];

// Allocate all then free all
	for(i = 0; i < POOL_ENTRIES; ++i) {
		//Allocate chunk
		chunks[i] = mempool_alloc(mp);
		//Write into it
		write_chunk(chunks[i], (char)(i % 255));
	}
	
	for(i = 0; i < POOL_ENTRIES; ++i) {
		//Verify contents
		verify_chunk(chunks[i], (char)(i % 255));
		//Release
		mempool_free(mp, chunks[i]);
		chunks[i] = NULL;
	}

// Allocate all then free by using is_mempool_exhaused
	for(i = 0; is_mempool_exhaused(mp) == false; ++i) {
		//Allocate chunk
		chunks[i] = mempool_alloc(mp);
		//Write into it
		write_chunk(chunks[i], (char)(i % 255));
	}
	assert(i == POOL_ENTRIES);
	
	for(i = 0; i < POOL_ENTRIES; ++i) {
		//Verify contents
		verify_chunk(chunks[i], (char)(i % 255));
		//Release
		mempool_free(mp, chunks[i]);
		chunks[i] = NULL;
	}
	
	printf("TEST SUCCESSFUL!\n");
	return 0;
}
Exemple #8
0
void create_sound_track(int size)
  {
  void *p;

  p=getmem(size);
  memset(p,0,size);
  write_chunk(MGIF_SOUND,size,p);
  free(p);
  }
 void RtmpMessageContext::to_chunk(
     RtmpMessageHeader const & msg, 
     RtmpChunkHeader & chunk)
 {
     RtmpChunkHeader & wchunk(write_chunk(msg.chunk));
     chunk.calc_timestamp = msg.timestamp;
     chunk.message_length = msg.length;
     chunk.message_type_id = msg.type;
     chunk.message_stream_id = msg.stream;
     chunk = wchunk.dec(chunk);
 }
Exemple #10
0
static int write_object_header(int objId, yaffs_ObjectType t, struct stat *s, int parent, const char *name, int equivalentObj, const char * alias)
{
	__u8 bytes[chunkSize];
	
	
	yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)bytes;
	
	memset(bytes,0xff,sizeof(bytes));
	
	oh->type = t;

	oh->parentObjectId = parent;
	
	strncpy(oh->name,name,YAFFS_MAX_NAME_LENGTH);
	
	
	if(t != YAFFS_OBJECT_TYPE_HARDLINK)
	{
		oh->yst_mode = s->st_mode;
		oh->yst_uid = s->st_uid;
// NCB 12/9/02		oh->yst_gid = s->yst_uid;
		oh->yst_gid = s->st_gid;
		oh->yst_atime = s->st_atime;
		oh->yst_mtime = s->st_mtime;
		oh->yst_ctime = s->st_ctime;
		oh->yst_rdev  = s->st_rdev;
	}
	
	if(t == YAFFS_OBJECT_TYPE_FILE)
	{
		oh->fileSize = s->st_size;
	}
	
	if(t == YAFFS_OBJECT_TYPE_HARDLINK)
	{
		oh->equivalentObjectId = equivalentObj;
	}
	
	if(t == YAFFS_OBJECT_TYPE_SYMLINK)
	{
		strncpy(oh->alias,alias,YAFFS_MAX_ALIAS_LENGTH);
	}

	if (convert_endian)
	{
    		object_header_little_to_big_endian(oh);
	}
	
	return write_chunk(bytes,objId,0,0xffff);
	
}
/*
 * Write a big chunk with 'progress' indicator '.' for every MiB
 */
static int write_big_chunk(int fd, void *addr, size_t count)
{
	unsigned char *a = addr;
	size_t total = 0;

	if (count < 0x100000)
		return write_chunk(fd, addr, count);
	/* if large chunk then print dot to show progress */
	while (total < count) {
		size_t bytes = count - total;

		kick_mpcore_wdt();

		if (bytes > 0x100000)
			bytes = 0x100000;
		if (write_chunk(fd, a, bytes))
			return -1;
		putc('.');
		total += bytes;
		a += bytes;
	}
	putc('\n');
	return 0;
}
Exemple #12
0
/* Merge shuffled temporary files; doesn't necessarily produce a perfect shuffle, but good enough */
int shuffle_merge(int num) {
    long i, j, k, l = 0;
    int fidcounter = 0;
    CREC *array;
    char filename[MAX_STRING_LENGTH];
    FILE **fid, *fout = stdout;
    
    array = malloc(sizeof(CREC) * array_size);
    fid = malloc(sizeof(FILE) * num);
    for(fidcounter = 0; fidcounter < num; fidcounter++) { //num = number of temporary files to merge
        sprintf(filename,"%s_%04d.bin",file_head, fidcounter);
        fid[fidcounter] = fopen(filename, "rb");
        if(fid[fidcounter] == NULL) {
            fprintf(stderr, "Unable to open file %s.\n",filename);
            return 1;
        }
    }
    if(verbose > 0) fprintf(stderr, "Merging temp files: processed %ld lines.", l);
    
    while(1) { //Loop until EOF in all files
        i = 0;
        //Read at most array_size values into array, roughly array_size/num from each temp file
        for(j = 0; j < num; j++) {
            if(feof(fid[j])) continue;
            for(k = 0; k < array_size / num; k++){
                fread(&array[i], sizeof(CREC), 1, fid[j]);
                if(feof(fid[j])) break;
                i++;
            }
        }
        if(i == 0) break;
        l += i;
        shuffle(array, i-1); // Shuffles lines between temp files
        write_chunk(array,i,fout);
        if(verbose > 0) fprintf(stderr, "\033[31G%ld lines.", l);
    }
    fprintf(stderr, "\033[0GMerging temp files: processed %ld lines.", l);
    for(fidcounter = 0; fidcounter < num; fidcounter++) {
        fclose(fid[fidcounter]);
        sprintf(filename,"%s_%04d.bin",file_head, fidcounter);
        remove(filename);
    }
    fprintf(stderr, "\n\n");
    free(array);
    return 0;
}
Exemple #13
0
static void write_share_chunk(struct buffer *buffer, struct share *share)
{
	struct buffer sharebuf = { .bytes = share->chunk, .len = share->chunk_len, .max = share->chunk_len };
	write_chunk(buffer, &sharebuf, "SHAR");
}

size_t blob_write(const struct blob *blob, const unsigned char key[KDF_HASH_LEN], char **out)
{
	struct buffer buffer;
	struct share *last_share = NULL;
	_cleanup_free_ char *version;

	memset(&buffer, 0, sizeof(buffer));

	version = xultostr(blob->version);
	buffer_append(&buffer, "LPAV", 4);
	write_plain_string(&buffer, version);
	buffer_append(&buffer, "LOCL", 4);
	write_plain_string(&buffer, LASTPASS_CLI_VERSION);

	for (struct account *account = blob->account_head; account; account = account->next) {
		if (!account->share)
			write_account_chunk(&buffer, account, key);
	}
	for (struct account *account = blob->account_head; account; account = account->next) {
		if (!account->share)
			continue;
		if (last_share != account->share) {
			write_share_chunk(&buffer, account->share);
			last_share = account->share;
		}
		write_account_chunk(&buffer, account, account->share->key);
	}

	*out = buffer.bytes;
	return buffer.len;
}

static struct blob *local_blob(const unsigned char key[KDF_HASH_LEN], const struct private_key *private_key)
{
	_cleanup_free_ char *blob = NULL;
	size_t len = config_read_encrypted_buffer("blob", &blob, key);
	if (!blob)
		return NULL;
	return blob_parse(blob, len, key, private_key);
}
Exemple #14
0
}static int
inflate_into_chunk(struct mem_chunk m)
{unsigned cont_size,decompressed;const char*cont;z_stream str;int err;
  unsigned orig_size;MemSet(&str,sizeof str,0);decompressed=0;
  orig_size=*((const unsigned*)*indices);cont=*indices+sizeof(orig_size);
  cont_size=MemPtrSize(*indices)-2;decompressed=0;
  str.next_in=(char*)cont;str.avail_in=cont_size;
  err=inflateInit(&str);
  if(err){zlib_error_alert(err,"inflateInit");return err;}
  while(decompressed<orig_size)
  {unsigned n;str.next_out=zlib_buf;str.avail_out=zlib_buf_size;
   err=inflate(&str,0);
   if(err&&err!=Z_STREAM_END)
   {zlib_error_alert(err,"inflate");inflateEnd(&str);return err;}
   n=zlib_buf_size-str.avail_out;
   write_chunk(m,decompressed,zlib_buf,n);decompressed+=n;
  }while(err!=Z_STREAM_END);
  err=inflateEnd(&str);if(err)zlib_error_alert(err,"inflateEnd");return err;
}static const char*saved_uncompressed;
Exemple #15
0
void gen_handler(struct evhttp_request *req, void *arg)
{
    DBG();
    /*struct evbuffer *evb = evbuffer_new(); */

    const char *uri = evhttp_request_get_uri(req);
    struct evhttp_uri *decoded_uri = NULL;
    const char *path;
    char *decoded_path;

    /* Decode the URI */
    decoded_uri = evhttp_uri_parse(uri);
    if (!decoded_uri) {
        reply_error(req, HTTP_BADREQUEST, "Bad URI: %s", uri);
        return;
    }

    path = evhttp_uri_get_path(decoded_uri);
    if (!path) {
        logging(LOG_INFO, "request path is nil, replace it as /");
        path = "/";
    }

    decoded_path = evhttp_uridecode(path, 0, NULL);

    uint64_t chunkid;
    char *slash = strchr(path + 1, '/');
    *slash = '\0';
    const char *op = path + 1;
    sscanf(slash + 1, "%" SCNu64, &chunkid);

    logging(LOG_INFO, "%s, %" PRIu64, op, chunkid);

    if (strcmp(op, "put") == 0) {
        write_chunk(chunkid, req);
    } else if (strcmp(op, "get") == 0) {
        read_chunk(chunkid, req);
    } else {
        reply_error(req, HTTP_NOTFOUND, "not found: %s", uri);
        return;
    }
}
Exemple #16
0
int
write (int fd, char *buf, int len)
{
  int rv = 0;
  int c;
#if 0
  if (fd == 2)
    fprintf (stderr, "%.*s", buf, len);
  else if (fd == 1)
    printf ("%.*s", buf, len);
#endif
  while (len > 0)
    {
      int l = (len > CIO_BUF_SIZE) ? CIO_BUF_SIZE : len;
      c = write_chunk (fd, buf, l);
      if (c < 0)
	return c;
      rv += l;
      len -= l;
      buf += l;
    }
  return rv;
}
static int write_object_header(int objId, yaffs_ObjectType t, struct stat *s, int parent, const char *name, int equivalentObj, const char * alias, const char *secontext)
{
	__u8 bytes[chunkSize];
	
	
	yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)bytes;
	char *xb = (char *)bytes + sizeof(*oh);
	int xnamelen = strlen(XATTR_NAME_SELINUX) + 1;
	int xvalsize = 0;
	int xreclen = 0;

	if (secontext) {
		xvalsize = strlen(secontext) + 1;
		xreclen = sizeof(int) + xnamelen + xvalsize;
	}
	
	memset(bytes,0xff,sizeof(bytes));
	
	oh->type = t;

	oh->parentObjectId = parent;
	
	strncpy(oh->name,name,YAFFS_MAX_NAME_LENGTH);

	if (xreclen) {
		memcpy(xb, &xreclen, sizeof(int));
		xb += sizeof(int);
		strcpy(xb, XATTR_NAME_SELINUX);
		xb += xnamelen;
		memcpy(xb, secontext, xvalsize);
	}
	
	if(t != YAFFS_OBJECT_TYPE_HARDLINK)
	{
		oh->yst_mode = s->st_mode;
		oh->yst_uid = s->st_uid;
// NCB 12/9/02		oh->yst_gid = s->yst_uid;
		oh->yst_gid = s->st_gid;
		oh->yst_atime = s->st_atime;
		oh->yst_mtime = s->st_mtime;
		oh->yst_ctime = s->st_ctime;
		oh->yst_rdev  = s->st_rdev;
	}
	
	if(t == YAFFS_OBJECT_TYPE_FILE)
	{
		oh->fileSize = s->st_size;
	}
	
	if(t == YAFFS_OBJECT_TYPE_HARDLINK)
	{
		oh->equivalentObjectId = equivalentObj;
	}
	
	if(t == YAFFS_OBJECT_TYPE_SYMLINK)
	{
		strncpy(oh->alias,alias,YAFFS_MAX_ALIAS_LENGTH);
	}

	if (convert_endian)
	{
    		object_header_little_to_big_endian(oh);
	}
	
	return write_chunk(bytes,objId,0,0xffff);
	
}
Exemple #18
0
int
end_response()
{
	write_chunk(client.response->socket,NULL,0);
	return 0;
}
/*
 * Dump crash to file
 */
static int write_elf(Elf32_Ehdr *elfhdr_addr, int fd)
{
	Elf32_Ehdr *oldhdr = elfhdr_addr;
	Elf32_Ehdr *ehdr;
	Elf32_Phdr *phdr;
	u32 i;
	u32 offset;
	u32 tot;
	u32 phdr_cnt;
	u32 notes_cnt = 0;
	u32 save;
	u32 len;

	offset = oldhdr->e_ehsize + oldhdr->e_phentsize * oldhdr->e_phnum;
	ehdr = (Elf32_Ehdr *) malloc(offset);
	if (ehdr == NULL) {
		debug("elf header alloc error\n");
		return -1;
	}
	memcpy(ehdr, oldhdr, offset);

	/*
	 * check program header entries and update length
	 * for merged PT_NOTE segments
	 */
	tot = 0;
	phdr_cnt = ehdr->e_phnum;
	debug("phdr_cnt=%d\n", phdr_cnt);
	for (i = 0; i < phdr_cnt; i++) {
		phdr = (Elf32_Phdr *) ((char *) ehdr + ehdr->e_ehsize +
				       i * ehdr->e_phentsize);
		len = check_phdr(phdr);
		debug("prog hdr %d: %x ad %x len %x adjusted to %x\n",
		      i, (u32) phdr, phdr->p_paddr, phdr->p_filesz, len);
		phdr->p_filesz = len;
		phdr->p_memsz = len;
		if (phdr->p_type == PT_NOTE) {	/* note segment */
			tot += len;
			notes_cnt++;
		}
	}
	debug("Length of %d note segments: %x\n", notes_cnt, tot);

	/*
	 * all PT_NOTE segments have been merged into one.
	 * Update ELF Header accordingly
	 */
	ehdr->e_phnum = phdr_cnt - notes_cnt + 1;

	/* write elf header into file on sdcard */
	if (write_chunk(fd, ehdr, (size_t) ehdr->e_ehsize)) {
		free(ehdr);
		return -1;
	}

	/* write program headers into file on sdcard */
	offset = ehdr->e_ehsize + ehdr->e_phentsize * ehdr->e_phnum;
	debug("Write Phdr: proghdr_cnt=%d\n", phdr_cnt);
	for (i = 0; i < phdr_cnt; i++) {
		phdr = (Elf32_Phdr *) ((char *)ehdr + ehdr->e_ehsize +
				       i * ehdr->e_phentsize);
		save = phdr->p_filesz;
		if (i == 0) {
			phdr->p_filesz = tot;
			phdr->p_memsz = tot;
		} else if (phdr->p_type == PT_NOTE) /* note segment */
			continue;
		phdr->p_offset = offset;
		debug("prog hdr %d: %x ad %x len %x off %x\n",
		       i, (u32) phdr, phdr->p_paddr, phdr->p_filesz,
		       phdr->p_offset);
		offset += phdr->p_filesz;
		if (write_chunk(fd, (void *) phdr, (size_t)
				ehdr->e_phentsize)) {
			free(ehdr);
			return -1;
		}
		phdr->p_filesz = save;
		phdr->p_memsz = save;
	}

	/* write segments into file on sdcard */
	debug("write segments...\n");
	for (i = 0; i < phdr_cnt; i++) {
		phdr = (Elf32_Phdr *) ((char *) ehdr + ehdr->e_ehsize +
				       i * ehdr->e_phentsize);
		if (phdr->p_type > PT_NULL) {
			if (write_big_chunk(fd, (void *) phdr->p_paddr,
							 phdr->p_filesz)) {
				free(ehdr);
				return -1;
			}
		}
	}

	free(ehdr);
	return 0;
}
Exemple #20
0
int ratt_table_push(ratt_table_t *table, void const *chunk)
{
	RATTLOG_TRACE();
	return write_chunk(table, chunk, ratt_table_get_tail_next);
}
Exemple #21
0
int ratt_table_insert(ratt_table_t *table, void const *chunk)
{
	RATTLOG_TRACE();
	return write_chunk(table, chunk, ratt_table_get_frag_first);
}
Exemple #22
0
static int winhttp_stream_write_chunked(
	git_smart_subtransport_stream *stream,
	const char *buffer,
	size_t len)
{
	winhttp_stream *s = (winhttp_stream *)stream;
	int error;

	if (!s->request && winhttp_stream_connect(s) < 0)
		return -1;

	if (!s->sent_request) {
		/* Send Transfer-Encoding: chunked header */
		if (!WinHttpAddRequestHeaders(s->request,
			transfer_encoding, (ULONG) -1L,
			WINHTTP_ADDREQ_FLAG_ADD)) {
			giterr_set(GITERR_OS, "Failed to add a header to the request");
			return -1;
		}

		if ((error = send_request(s, 0, 1)) < 0)
			return error;

		s->sent_request = 1;
	}

	if (len > CACHED_POST_BODY_BUF_SIZE) {
		/* Flush, if necessary */
		if (s->chunk_buffer_len > 0) {
			if (write_chunk(s->request, s->chunk_buffer, s->chunk_buffer_len) < 0)
				return -1;

			s->chunk_buffer_len = 0;
		}

		/* Write chunk directly */
		if (write_chunk(s->request, buffer, len) < 0)
			return -1;
	}
	else {
		/* Append as much to the buffer as we can */
		int count = (int)min(CACHED_POST_BODY_BUF_SIZE - s->chunk_buffer_len, len);

		if (!s->chunk_buffer)
			s->chunk_buffer = git__malloc(CACHED_POST_BODY_BUF_SIZE);

		memcpy(s->chunk_buffer + s->chunk_buffer_len, buffer, count);
		s->chunk_buffer_len += count;
		buffer += count;
		len -= count;

		/* Is the buffer full? If so, then flush */
		if (CACHED_POST_BODY_BUF_SIZE == s->chunk_buffer_len) {
			if (write_chunk(s->request, s->chunk_buffer, s->chunk_buffer_len) < 0)
				return -1;

			s->chunk_buffer_len = 0;

			/* Is there any remaining data from the source? */
			if (len > 0) {
				memcpy(s->chunk_buffer, buffer, len);
				s->chunk_buffer_len = (unsigned int)len;
			}
		}
	}

	return 0;
}
Exemple #23
0
static int winhttp_stream_read(
	git_smart_subtransport_stream *stream,
	char *buffer,
	size_t buf_size,
	size_t *bytes_read)
{
	winhttp_stream *s = (winhttp_stream *)stream;
	winhttp_subtransport *t = OWNING_SUBTRANSPORT(s);
	DWORD dw_bytes_read;
	char replay_count = 0;
	int error;

replay:
	/* Enforce a reasonable cap on the number of replays */
	if (++replay_count >= 7) {
		giterr_set(GITERR_NET, "Too many redirects or authentication replays");
		return -1;
	}

	/* Connect if necessary */
	if (!s->request && winhttp_stream_connect(s) < 0)
		return -1;

	if (!s->received_response) {
		DWORD status_code, status_code_length, content_type_length, bytes_written;
		char expected_content_type_8[MAX_CONTENT_TYPE_LEN];
		wchar_t expected_content_type[MAX_CONTENT_TYPE_LEN], content_type[MAX_CONTENT_TYPE_LEN];

		if (!s->sent_request) {

			if ((error = send_request(s, s->post_body_len, 0)) < 0)
				return error;

			s->sent_request = 1;
		}

		if (s->chunked) {
			assert(s->verb == post_verb);

			/* Flush, if necessary */
			if (s->chunk_buffer_len > 0 &&
				write_chunk(s->request, s->chunk_buffer, s->chunk_buffer_len) < 0)
				return -1;

			s->chunk_buffer_len = 0;

			/* Write the final chunk. */
			if (!WinHttpWriteData(s->request,
				"0\r\n\r\n", 5,
				&bytes_written)) {
				giterr_set(GITERR_OS, "Failed to write final chunk");
				return -1;
			}
		}
		else if (s->post_body) {
			char *buffer;
			DWORD len = s->post_body_len, bytes_read;

			if (INVALID_SET_FILE_POINTER == SetFilePointer(s->post_body,
					0, 0, FILE_BEGIN) &&
				NO_ERROR != GetLastError()) {
				giterr_set(GITERR_OS, "Failed to reset file pointer");
				return -1;
			}

			buffer = git__malloc(CACHED_POST_BODY_BUF_SIZE);

			while (len > 0) {
				DWORD bytes_written;

				if (!ReadFile(s->post_body, buffer,
					min(CACHED_POST_BODY_BUF_SIZE, len),
					&bytes_read, NULL) ||
					!bytes_read) {
					git__free(buffer);
					giterr_set(GITERR_OS, "Failed to read from temp file");
					return -1;
				}

				if (!WinHttpWriteData(s->request, buffer,
					bytes_read, &bytes_written)) {
					git__free(buffer);
					giterr_set(GITERR_OS, "Failed to write data");
					return -1;
				}

				len -= bytes_read;
				assert(bytes_read == bytes_written);
			}

			git__free(buffer);

			/* Eagerly close the temp file */
			CloseHandle(s->post_body);
			s->post_body = NULL;
		}

		if (!WinHttpReceiveResponse(s->request, 0)) {
			giterr_set(GITERR_OS, "Failed to receive response");
			return -1;
		}

		/* Verify that we got a 200 back */
		status_code_length = sizeof(status_code);

		if (!WinHttpQueryHeaders(s->request,
			WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER,
			WINHTTP_HEADER_NAME_BY_INDEX,
			&status_code, &status_code_length,
			WINHTTP_NO_HEADER_INDEX)) {
				giterr_set(GITERR_OS, "Failed to retrieve status code");
				return -1;
		}

		/* The implementation of WinHTTP prior to Windows 7 will not
		 * redirect to an identical URI. Some Git hosters use self-redirects
		 * as part of their DoS mitigation strategy. Check first to see if we
		 * have a redirect status code, and that we haven't already streamed
		 * a post body. (We can't replay a streamed POST.) */
		if (!s->chunked &&
			(HTTP_STATUS_MOVED == status_code ||
			 HTTP_STATUS_REDIRECT == status_code ||
			 (HTTP_STATUS_REDIRECT_METHOD == status_code &&
			  get_verb == s->verb) ||
			 HTTP_STATUS_REDIRECT_KEEP_VERB == status_code)) {

			/* Check for Windows 7. This workaround is only necessary on
			 * Windows Vista and earlier. Windows 7 is version 6.1. */
			wchar_t *location;
			DWORD location_length;
			char *location8;

			/* OK, fetch the Location header from the redirect. */
			if (WinHttpQueryHeaders(s->request,
				WINHTTP_QUERY_LOCATION,
				WINHTTP_HEADER_NAME_BY_INDEX,
				WINHTTP_NO_OUTPUT_BUFFER,
				&location_length,
				WINHTTP_NO_HEADER_INDEX) ||
				GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
				giterr_set(GITERR_OS, "Failed to read Location header");
				return -1;
			}

			location = git__malloc(location_length);
			GITERR_CHECK_ALLOC(location);

			if (!WinHttpQueryHeaders(s->request,
				WINHTTP_QUERY_LOCATION,
				WINHTTP_HEADER_NAME_BY_INDEX,
				location,
				&location_length,
				WINHTTP_NO_HEADER_INDEX)) {
				giterr_set(GITERR_OS, "Failed to read Location header");
				git__free(location);
				return -1;
			}

			/* Convert the Location header to UTF-8 */
			if (git__utf16_to_8_alloc(&location8, location) < 0) {
				giterr_set(GITERR_OS, "Failed to convert Location header to UTF-8");
				git__free(location);
				return -1;
			}

			git__free(location);

			/* Replay the request */
			winhttp_stream_close(s);

			if (!git__prefixcmp_icase(location8, prefix_https)) {
				/* Upgrade to secure connection; disconnect and start over */
				if (gitno_connection_data_from_url(&t->connection_data, location8, s->service_url) < 0) {
					git__free(location8);
					return -1;
				}

				winhttp_close_connection(t);

				if (winhttp_connect(t) < 0)
					return -1;
			}

			git__free(location8);
			goto replay;
		}

		/* Handle authentication failures */
		if (HTTP_STATUS_DENIED == status_code && get_verb == s->verb) {
			int allowed_types;

			if (parse_unauthorized_response(s->request, &allowed_types, &t->auth_mechanism) < 0)
				return -1;

			if (allowed_types &&
				(!t->cred || 0 == (t->cred->credtype & allowed_types))) {
				int cred_error = 1;

				/* Start with the user-supplied credential callback, if present */
				if (t->owner->cred_acquire_cb) {
					cred_error = t->owner->cred_acquire_cb(&t->cred, t->owner->url,
						t->connection_data.user, allowed_types,	t->owner->cred_acquire_payload);

					if (cred_error < 0)
						return cred_error;
				}

				/* Invoke the fallback credentials acquisition callback if necessary */
				if (cred_error > 0) {
					cred_error = fallback_cred_acquire_cb(&t->cred, t->owner->url,
						t->connection_data.user, allowed_types, NULL);

					if (cred_error < 0)
						return cred_error;
				}

				if (!cred_error) {
					assert(t->cred);

					winhttp_stream_close(s);

					/* Successfully acquired a credential */
					goto replay;
				}
			}
		}

		if (HTTP_STATUS_OK != status_code) {
			giterr_set(GITERR_NET, "Request failed with status code: %d", status_code);
			return -1;
		}

		/* Verify that we got the correct content-type back */
		if (post_verb == s->verb)
			p_snprintf(expected_content_type_8, MAX_CONTENT_TYPE_LEN, "application/x-git-%s-result", s->service);
		else
			p_snprintf(expected_content_type_8, MAX_CONTENT_TYPE_LEN, "application/x-git-%s-advertisement", s->service);

		if (git__utf8_to_16(expected_content_type, MAX_CONTENT_TYPE_LEN, expected_content_type_8) < 0) {
			giterr_set(GITERR_OS, "Failed to convert expected content-type to wide characters");
			return -1;
		}

		content_type_length = sizeof(content_type);

		if (!WinHttpQueryHeaders(s->request,
			WINHTTP_QUERY_CONTENT_TYPE,
			WINHTTP_HEADER_NAME_BY_INDEX,
			&content_type, &content_type_length,
			WINHTTP_NO_HEADER_INDEX)) {
				giterr_set(GITERR_OS, "Failed to retrieve response content-type");
				return -1;
		}

		if (wcscmp(expected_content_type, content_type)) {
			giterr_set(GITERR_NET, "Received unexpected content-type");
			return -1;
		}

		s->received_response = 1;
	}

	if (!WinHttpReadData(s->request,
		(LPVOID)buffer,
		(DWORD)buf_size,
		&dw_bytes_read))
	{
		giterr_set(GITERR_OS, "Failed to read data");
		return -1;
	}

	*bytes_read = dw_bytes_read;

	return 0;
}
Exemple #24
0
static int process_directory(int parent, const char *path, int fixstats, mkyaffs2image_callback callback)
{

	DIR *dir;
	struct dirent *entry;

	//nDirectories++;
	
	dir = opendir(path);
	
	if(dir)
	{
		while((entry = readdir(dir)) != NULL)
		{
		
			/* Ignore . and .. */
			if(strcmp(entry->d_name,".") &&
			   strcmp(entry->d_name,".."))
 			{
 				char full_name[PATH_MAX];
				struct stat stats;
				int equivalentObj;
				int newObj;
				
				sprintf(full_name,"%s/%s",path,entry->d_name);
				
				lstat(full_name,&stats);
				
				if(S_ISLNK(stats.st_mode) ||
				    S_ISREG(stats.st_mode) ||
				    S_ISDIR(stats.st_mode) ||
				    S_ISFIFO(stats.st_mode) ||
				    S_ISBLK(stats.st_mode) ||
				    S_ISCHR(stats.st_mode) ||
				    S_ISSOCK(stats.st_mode))
				{
				
					newObj = obj_id++;
					if (callback != NULL)
                        callback(full_name);
					//nObjects++;

                    if (fixstats) {
                        fix_stat(full_name, &stats);
                    }

					//printf("Object %d, %s is a ",newObj,full_name);
					
					/* We're going to create an object for it */
					if((equivalentObj = find_obj_in_list(stats.st_dev, stats.st_ino)) > 0)
					{
					 	/* we need to make a hard link */
					 	//printf("hard link to object %d\n",equivalentObj);
						error =  write_object_header(newObj, YAFFS_OBJECT_TYPE_HARDLINK, &stats, parent, entry->d_name, equivalentObj, NULL);
					}
					else 
					{
						
						add_obj_to_list(stats.st_dev,stats.st_ino,newObj);
						
						if(S_ISLNK(stats.st_mode))
						{
					
							char symname[PATH_MAX];
						
							memset(symname,0, sizeof(symname));
					
							readlink(full_name,symname,sizeof(symname) -1);
						
							//printf("symlink to \"%s\"\n",symname);
							error =  write_object_header(newObj, YAFFS_OBJECT_TYPE_SYMLINK, &stats, parent, entry->d_name, -1, symname);

						}
						else if(S_ISREG(stats.st_mode))
						{
							//printf("file, ");
							error =  write_object_header(newObj, YAFFS_OBJECT_TYPE_FILE, &stats, parent, entry->d_name, -1, NULL);

							if(error >= 0)
							{
								int h;
								__u8 bytes[chunkSize];
								int nBytes;
								int chunk = 0;
								
								h = open(full_name,O_RDONLY);
								if(h >= 0)
								{
									memset(bytes,0xff,sizeof(bytes));
									while((nBytes = read(h,bytes,sizeof(bytes))) > 0)
									{
										chunk++;
										write_chunk(bytes,newObj,chunk,nBytes);
										memset(bytes,0xff,sizeof(bytes));
									}
									if(nBytes < 0) 
									   error = nBytes;
									   
									//printf("%d data chunks written\n",chunk);
								}
								else
								{
									perror("Error opening file");
								}
								close(h);
								
							}							
														
						}
						else if(S_ISSOCK(stats.st_mode))
						{
							//printf("socket\n");
							error =  write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
						}
						else if(S_ISFIFO(stats.st_mode))
						{
							//printf("fifo\n");
							error =  write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
						}
						else if(S_ISCHR(stats.st_mode))
						{
							//printf("character device\n");
							error =  write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
						}
						else if(S_ISBLK(stats.st_mode))
						{
							//printf("block device\n");
							error =  write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL);
						}
						else if(S_ISDIR(stats.st_mode))
						{
							//printf("directory\n");
							error =  write_object_header(newObj, YAFFS_OBJECT_TYPE_DIRECTORY, &stats, parent, entry->d_name, -1, NULL);
// NCB modified 10/9/2001				process_directory(1,full_name);
							process_directory(newObj,full_name,fixstats,callback);
						}
					}
				}
				else
				{
					//printf(" we don't handle this type\n");
				}
			}
		}

		closedir(dir);
	}
	
	return 0;

}
Exemple #25
0
int main(int argc, char **argv)
{
  boot_img_hdr hdr;

  int bootimg_fd;

  void *boot_data = 0;
  unsigned boot_size = 0;
  char boot_magic[BOOT_MAGIC_SIZE+1];
  char kernel_fn[MAXNAMELEN];
  void *kernel_data = 0;
  char ramdisk_fn[MAXNAMELEN];
  void *ramdisk_data = 0;
  char second_fn[MAXNAMELEN];
  void *second_data = 0;
  char header_fn[MAXNAMELEN];
  char *cmdline = "";
  char *bootimg_fn = 0;
  char *board = "";
  unsigned pagesize = 2048;
  int fd;
  /*SHA_CTX ctx;*/
  /*uint8_t* sha;*/
  unsigned long i;
  unsigned count;
  char *msg1 = "";
  char *msg2 = "";
  int offset;
  FILE *fp;
  unsigned kernel_offset =  0x00008000;
  unsigned ramdisk_offset = 0x01000000;
  unsigned second_offset =  0x00F00000;
  unsigned tags_offset =    0x00000100;
  unsigned kernel_base = 0;
  unsigned ramdisk_base = 0;
  unsigned second_base = 0;
  unsigned tags_base = 0;

  argc--;
  argv++;

  while(argc > 0){
    char *arg = argv[0];
    char *val = argv[1];
    if(argc < 2) {
      return usage();
    }
    argc -= 2;
    argv += 2;
    if(!strcmp(arg, "--input") || !strcmp(arg, "-i")) {
      bootimg_fn = val;
    } else {
      return usage();
    }
  }

  if(bootimg_fn == 0) {
    fprintf(stderr,"error: no input filename specified\n");
    return usage();
  }

  sprintf(kernel_fn, "%s-kernel", bootimg_fn);
  bootimg_fd = open(bootimg_fn, O_RDONLY);
  if (bootimg_fd < 0) {
    fprintf(stderr,"error: could not open %s\n", bootimg_fn);
    return 1;
  }

  count = read(bootimg_fd, (void *)&hdr, sizeof(hdr));
  if (count != sizeof(hdr)) {
    msg1 = "could not read hdr from input";
    goto fail;
  }
  for (i = 0; i < BOOT_MAGIC_SIZE; i++) {
    boot_magic[i] = hdr.magic[i];
  }
  boot_magic[BOOT_MAGIC_SIZE] = '\0';
  if (strcmp(BOOT_MAGIC, boot_magic)) {
    fprintf(stderr, "error: %s boot image file's magic value %s does not match %s\n",
            bootimg_fn, boot_magic, BOOT_MAGIC);
    goto fail;
  }
  if (hdr.page_size != 2048 && hdr.page_size != 4096 &&
      hdr.page_size != 8192 && hdr.page_size != 16384) {
    fprintf(stderr, "warning: hdr.page_size %d must be 2048, 4096, 8192 or 16384\n",
            hdr.page_size);
    goto fail;
  }
  count = lseek(bootimg_fd, hdr.page_size, SEEK_SET);
  if (count != hdr.page_size) {
    msg1 = "boot header is not a page";
    goto fail;
  }

  sprintf(header_fn, "%s-header", bootimg_fn);
  fp = fopen(header_fn, "w+");
  if(!fp) {
    msg1 = "could not create";
    msg2 = header_fn;
    goto fail;
  }
  kernel_base  = hdr.kernel_addr - kernel_offset;
  ramdisk_base = hdr.ramdisk_addr - ramdisk_offset;
  second_base  = hdr.second_addr - second_offset;
  tags_base    = hdr.tags_addr - tags_offset;

  fprintf(fp, "Assumptions\n");
  fprintf(fp, "kernel_offset = 0x%X\n", kernel_offset);
  fprintf(fp, "ramdisk_offset = 0x%X\n", ramdisk_offset);
  fprintf(fp, "second_offset = 0x%X\n", second_offset);
  fprintf(fp, "tags_offset = 0x%X\n", tags_offset);
  fprintf(fp, "Detected values\n");
  fprintf(fp, "kernel_size = %d\n", hdr.kernel_size);
  fprintf(fp, "kernel_addr = 0x%X\n", hdr.kernel_addr);
  fprintf(fp, "ramdisk_size = %d\n", hdr.ramdisk_size);
  fprintf(fp, "ramdisk_addr = 0x%X\n", hdr.ramdisk_addr);
  fprintf(fp, "second_size = %d\n", hdr.second_size);
  fprintf(fp, "second_addr = 0x%X\n", hdr.second_addr);
  fprintf(fp, "tags_addr = 0x%X\n", hdr.tags_addr);
  fprintf(fp, "page_size = %d\n", hdr.page_size);
  /*fprintf(fp, "unused = %d%d\n", hdr.unused[0], hdr.unused[1]);*/
  fprintf(fp, "name/board = %s\n", hdr.name);
  fprintf(fp, "cmdline = %s\n", hdr.cmdline);
  /*fprintf(fp, "id = %s\n", hdr.id);*/
  fprintf(fp, "Computed values\n");
  fprintf(fp, "kernel_base = 0x%X\n", kernel_base);
  fprintf(fp, "ramdisk_base = 0x%X, kernel_base - ramdisk_offset=0x%X\n", ramdisk_base,
          kernel_base - ramdisk_base);
  fprintf(fp, "second_base = 0x%X, kernel_base - second_offset=0x%X\n", second_base,
          kernel_base - second_base);
  fprintf(fp, "tags_base = 0x%X, kernel_base - tags_offset = 0x%X\n", tags_base,
          kernel_base - tags_base);
  if (kernel_base != ramdisk_base ||
      kernel_base != second_base ||
      kernel_base != tags_base) {
    fprintf(fp, "WARNING! base addresses do not match!\n");
    fprintf(stderr, "WARNING! base addresses do not match!\n");
  }

  fclose(fp);

  /* kernel */
  if(hdr.kernel_size == 0) {
    msg1 = "kernel size is zero";
    goto fail;
  }
  offset = hdr.page_size;
  write_chunk(bootimg_fd, bootimg_fn, hdr.kernel_size, "kernel", offset);

  /* ramdisk */
  if(hdr.ramdisk_size == 0) {
    msg1 = "ramdisk size is zero";
    goto fail;
  }
  offset += pages(hdr.page_size, hdr.kernel_size) * hdr.page_size;
  write_chunk(bootimg_fd, bootimg_fn, hdr.ramdisk_size, "first-ramdisk", offset);

  /* ramdisk 2 */
  if(hdr.second_size > 0) {
    offset += pages(hdr.page_size, hdr.ramdisk_size) * hdr.page_size;
    write_chunk(bootimg_fd, bootimg_fn, hdr.second_size, "second-ramdisk", offset);
  }

  return 0;

 fail:
  close(bootimg_fd);
  fprintf(stderr,"error: %s %s '%s': %s\n", msg1, msg2, bootimg_fn,
          strerror(errno));
  return 1;
}
Exemple #26
0
static int http_stream_write_chunked(
	git_smart_subtransport_stream *stream,
	const char *buffer,
	size_t len)
{
	http_stream *s = (http_stream *)stream;
	http_subtransport *t = OWNING_SUBTRANSPORT(s);

	assert(t->connected);

	/* Send the request, if necessary */
	if (!s->sent_request) {
		git_buf request = GIT_BUF_INIT;

		clear_parser_state(t);

		if (gen_request(&request, s, 0) < 0) {
			giterr_set(GITERR_NET, "Failed to generate request");
			return -1;
		}

		if (gitno_send(&t->socket, request.ptr, request.size, 0) < 0) {
			git_buf_free(&request);
			return -1;
		}

		git_buf_free(&request);

		s->sent_request = 1;
	}

	if (len > CHUNK_SIZE) {
		/* Flush, if necessary */
		if (s->chunk_buffer_len > 0) {
			if (write_chunk(&t->socket, s->chunk_buffer, s->chunk_buffer_len) < 0)
				return -1;

			s->chunk_buffer_len = 0;
		}

		/* Write chunk directly */
		if (write_chunk(&t->socket, buffer, len) < 0)
			return -1;
	}
	else {
		/* Append as much to the buffer as we can */
		int count = min(CHUNK_SIZE - s->chunk_buffer_len, len);

		if (!s->chunk_buffer)
			s->chunk_buffer = git__malloc(CHUNK_SIZE);

		memcpy(s->chunk_buffer + s->chunk_buffer_len, buffer, count);
		s->chunk_buffer_len += count;
		buffer += count;
		len -= count;

		/* Is the buffer full? If so, then flush */
		if (CHUNK_SIZE == s->chunk_buffer_len) {
			if (write_chunk(&t->socket, s->chunk_buffer, s->chunk_buffer_len) < 0)
				return -1;

			s->chunk_buffer_len = 0;

			if (len > 0) {
				memcpy(s->chunk_buffer, buffer, len);
				s->chunk_buffer_len = len;
			}
		}
	}

	return 0;
}
Exemple #27
0
static int winhttp_stream_write_chunked(
	git_smart_subtransport_stream *stream,
	const char *buffer,
	size_t len)
{
	winhttp_stream *s = (winhttp_stream *)stream;
	winhttp_subtransport *t = OWNING_SUBTRANSPORT(s);

	if (!s->request && winhttp_stream_connect(s) < 0)
		return -1;

	if (!s->sent_request) {
		/* Send Transfer-Encoding: chunked header */
		if (!WinHttpAddRequestHeaders(s->request,
			transfer_encoding, (ULONG) -1L,
			WINHTTP_ADDREQ_FLAG_ADD)) {
			giterr_set(GITERR_OS, QT_TRANSLATE_NOOP("libgit2", "Failed to add a header to the request"));
			return -1;
		}

		if (!WinHttpSendRequest(s->request,
			WINHTTP_NO_ADDITIONAL_HEADERS, 0,
			WINHTTP_NO_REQUEST_DATA, 0,
			WINHTTP_IGNORE_REQUEST_TOTAL_LENGTH, 0)) {
			giterr_set(GITERR_OS, QT_TRANSLATE_NOOP("libgit2", "Failed to send request"));
			return -1;
		}

		s->sent_request = 1;
	}

	if (len > CACHED_POST_BODY_BUF_SIZE) {
		/* Flush, if necessary */
		if (s->chunk_buffer_len > 0) {
			if (write_chunk(s->request, s->chunk_buffer, s->chunk_buffer_len) < 0)
				return -1;

			s->chunk_buffer_len = 0;
		}

		/* Write chunk directly */
		if (write_chunk(s->request, buffer, len) < 0)
			return -1;
	}
	else {
		/* Append as much to the buffer as we can */
		int count = min(CACHED_POST_BODY_BUF_SIZE - s->chunk_buffer_len, (int)len);

		if (!s->chunk_buffer)
			s->chunk_buffer = git__malloc(CACHED_POST_BODY_BUF_SIZE);

		memcpy(s->chunk_buffer + s->chunk_buffer_len, buffer, count);
		s->chunk_buffer_len += count;
		buffer += count;
		len -= count;

		/* Is the buffer full? If so, then flush */
		if (CACHED_POST_BODY_BUF_SIZE == s->chunk_buffer_len) {
			if (write_chunk(s->request, s->chunk_buffer, s->chunk_buffer_len) < 0)
				return -1;

			s->chunk_buffer_len = 0;

			/* Is there any remaining data from the source? */
			if (len > 0) {
				memcpy(s->chunk_buffer, buffer, len);
				s->chunk_buffer_len = (unsigned int)len;
			}
		}
	}

	return 0;
}
static int process_directory(int parent, const char *path, int fixstats)
{

	DIR *dir;
	struct dirent *entry;
	char *secontext = NULL;

	nDirectories++;
	
	dir = opendir(path);
	
	if(dir)
	{
		while((entry = readdir(dir)) != NULL)
		{
		
			/* Ignore . and .. */
			if(strcmp(entry->d_name,".") &&
			   strcmp(entry->d_name,".."))
 			{
 				char full_name[500];
				char *suffix, dest_name[500];
				int ret;
				struct stat stats;
				int equivalentObj;
				int newObj;
				
				sprintf(full_name,"%s/%s",path,entry->d_name);
				
				lstat(full_name,&stats);

				if (sehnd) {
					suffix = full_name + seprefixlen;
					ret = snprintf(dest_name,
						       sizeof dest_name,
						       "%s%s", mntpoint,
						       suffix);
					if (ret < 0 ||
					    (size_t) ret >= sizeof dest_name) {
						fprintf(stderr,
							"snprintf failed on %s%s\n",
							mntpoint, suffix);
						exit(1);
					}

					char *sepath = NULL;
					if (dest_name[0] == '/')
					        sepath = strdup(dest_name);
					else if (asprintf(&sepath, "/%s", dest_name) < 0)
                                                sepath = NULL;

					if (!sepath) {
					        perror("malloc");
					        exit(1);
					}

					if (selabel_lookup(sehnd, &secontext,
							   sepath,
							   stats.st_mode) < 0) {
					        perror("selabel_lookup");
					        free(sepath);
					        exit(1);
					}
					free(sepath);
				}

				if(S_ISLNK(stats.st_mode) ||
				    S_ISREG(stats.st_mode) ||
				    S_ISDIR(stats.st_mode) ||
				    S_ISFIFO(stats.st_mode) ||
				    S_ISBLK(stats.st_mode) ||
				    S_ISCHR(stats.st_mode) ||
				    S_ISSOCK(stats.st_mode))
				{
				
					newObj = obj_id++;
					nObjects++;

                    if (fixstats) {
                        fix_stat(full_name, &stats);
                    }

					//printf("Object %d, %s is a ",newObj,full_name);
					
					/* We're going to create an object for it */
					if((equivalentObj = find_obj_in_list(stats.st_dev, stats.st_ino)) > 0)
					{
					 	/* we need to make a hard link */
					 	//printf("hard link to object %d\n",equivalentObj);
						error =  write_object_header(newObj, YAFFS_OBJECT_TYPE_HARDLINK, &stats, parent, entry->d_name, equivalentObj, NULL, secontext);
					}
					else 
					{
						
						add_obj_to_list(stats.st_dev,stats.st_ino,newObj);
						
						if(S_ISLNK(stats.st_mode))
						{
					
							char symname[500];
						
							memset(symname,0, sizeof(symname));
					
							readlink(full_name,symname,sizeof(symname) -1);
						
							//printf("symlink to \"%s\"\n",symname);
							error =  write_object_header(newObj, YAFFS_OBJECT_TYPE_SYMLINK, &stats, parent, entry->d_name, -1, symname, secontext);

						}
						else if(S_ISREG(stats.st_mode))
						{
							//printf("file, ");
							error =  write_object_header(newObj, YAFFS_OBJECT_TYPE_FILE, &stats, parent, entry->d_name, -1, NULL, secontext);

							if(error >= 0)
							{
								int h;
								__u8 bytes[chunkSize];
								int nBytes;
								int chunk = 0;
								
								h = open(full_name,O_RDONLY);
								if(h >= 0)
								{
									memset(bytes,0xff,sizeof(bytes));
									while((nBytes = read(h,bytes,sizeof(bytes))) > 0)
									{
										chunk++;
										write_chunk(bytes,newObj,chunk,nBytes);
										memset(bytes,0xff,sizeof(bytes));
									}
									if(nBytes < 0) 
									   error = nBytes;
									   
									//printf("%d data chunks written\n",chunk);
								}
								else
								{
									perror("Error opening file");
								}
								close(h);
								
							}							
														
						}
						else if(S_ISSOCK(stats.st_mode))
						{
							//printf("socket\n");
							error =  write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL, secontext);
						}
						else if(S_ISFIFO(stats.st_mode))
						{
							//printf("fifo\n");
							error =  write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL, secontext);
						}
						else if(S_ISCHR(stats.st_mode))
						{
							//printf("character device\n");
							error =  write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL, secontext);
						}
						else if(S_ISBLK(stats.st_mode))
						{
							//printf("block device\n");
							error =  write_object_header(newObj, YAFFS_OBJECT_TYPE_SPECIAL, &stats, parent, entry->d_name, -1, NULL, secontext);
						}
						else if(S_ISDIR(stats.st_mode))
						{
							//printf("directory\n");
							error =  write_object_header(newObj, YAFFS_OBJECT_TYPE_DIRECTORY, &stats, parent, entry->d_name, -1, NULL, secontext);
// NCB modified 10/9/2001				process_directory(1,full_name);
							process_directory(newObj,full_name,fixstats);
						}
					}
				}
				else
				{
					//printf(" we don't handle this type\n");
				}
			}
		}
		closedir(dir);
	}
	
	return 0;

}
Exemple #29
0
static void lts_write_dir(archive_t archive,string_map_t map,lts_t lts,int segments){
    if (map) arch_set_write_policy(archive,map);
    dir_info_t info=DIRinfoCreate(segments);
    int i,j;
    uint32_t k;
    char filename[1024];
    stream_t output;
    stream_t *src_out;
    stream_t *lbl_out;
    stream_t *dst_out;

    if (lts->root_count !=1) Abort("LTS has %u initial states DIR requires 1",lts->root_count);
    lts_set_type(lts,LTS_BLOCK);
    info->label_tau=lts->tau;
    int type_no=lts_type_get_edge_label_typeno(lts->ltstype,0);
    switch(lts_type_get_format(lts->ltstype,type_no)){
        case LTStypeChunk:
        case LTStypeEnum:
            break;
        default:
            Abort("DIR is limited to Chunk/Enum edge labels.");
    }
    info->label_count=VTgetCount(lts->values[type_no]);
    info->initial_seg=lts->root_list[0]%segments;
    info->initial_ofs=lts->root_list[0]/segments;
    output=arch_write(archive,"TermDB");
    int last_idx = 0;
    table_iterator_t it = VTiterator (lts->values[type_no]);
    while (IThasNext(it)) {
        chunk label_c = ITnext (it);
        int idx = VTputChunk (lts->values[type_no], label_c);
        while (last_idx < idx) { // fill non-dense indices
            write_chunk (output, (chunk){0, ""});
            last_idx++;
        }
        write_chunk (output, label_c);
    }
    DSclose(&output);
    src_out=(stream_t*)RTmalloc(segments*sizeof(stream_t));
    lbl_out=(stream_t*)RTmalloc(segments*sizeof(stream_t));
    dst_out=(stream_t*)RTmalloc(segments*sizeof(stream_t));
    for(i=0;i<segments;i++) {
        for(j=0;j<segments;j++) {
            sprintf(filename,"src-%d-%d",i,j);
            src_out[j]=arch_write(archive,filename);
            sprintf(filename,"label-%d-%d",i,j);
            lbl_out[j]=arch_write(archive,filename);
            sprintf(filename,"dest-%d-%d",i,j);
            dst_out[j]=arch_write(archive,filename);
        }
        for(j=i;j<(int)lts->states;j+=segments){
            for(k=lts->begin[j];k<lts->begin[j+1];k++){
                int dseg=(lts->dest[k])%segments;
                info->transition_count[i][dseg]++;
                DSwriteU32(src_out[dseg],info->state_count[i]);
                DSwriteU32(lbl_out[dseg],lts->label[k]);
                DSwriteU32(dst_out[dseg],(lts->dest[k])/segments);
            }
            info->state_count[i]++;
        }
        for(j=0;j<segments;j++) {
            DSclose(&src_out[j]);
            DSclose(&lbl_out[j]);
            DSclose(&dst_out[j]);
        }
    }
    info->info="bsim2 output";
    output=arch_write(archive,"info");
    DIRinfoWrite(output,info);
    DSclose(&output);
    info->info=NULL;
    DIRinfoDestroy(info);
}
Exemple #30
0
static int http_stream_read(
	git_smart_subtransport_stream *stream,
	char *buffer,
	size_t buf_size,
	size_t *bytes_read)
{
	http_stream *s = (http_stream *)stream;
	http_subtransport *t = OWNING_SUBTRANSPORT(s);
	parser_context ctx;
	size_t bytes_parsed;

replay:
	*bytes_read = 0;

	assert(t->connected);

	if (!s->sent_request) {
		git_buf request = GIT_BUF_INIT;

		clear_parser_state(t);

		if (gen_request(&request, s, 0) < 0) {
			giterr_set(GITERR_NET, "Failed to generate request");
			return -1;
		}

		if (gitno_send(&t->socket, request.ptr, request.size, 0) < 0) {
			git_buf_free(&request);
			return -1;
		}

		git_buf_free(&request);

		s->sent_request = 1;
	}

	if (!s->received_response) {
		if (s->chunked) {
			assert(s->verb == post_verb);

			/* Flush, if necessary */
			if (s->chunk_buffer_len > 0 &&
				write_chunk(&t->socket, s->chunk_buffer, s->chunk_buffer_len) < 0)
				return -1;

			s->chunk_buffer_len = 0;

			/* Write the final chunk. */
			if (gitno_send(&t->socket, "0\r\n\r\n", 5, 0) < 0)
				return -1;
		}

		s->received_response = 1;
	}

	while (!*bytes_read && !t->parse_finished) {
		t->parse_buffer.offset = 0;

		if (gitno_recv(&t->parse_buffer) < 0)
			return -1;

		/* This call to http_parser_execute will result in invocations of the
		 * on_* family of callbacks. The most interesting of these is
		 * on_body_fill_buffer, which is called when data is ready to be copied
		 * into the target buffer. We need to marshal the buffer, buf_size, and
		 * bytes_read parameters to this callback. */
		ctx.t = t;
		ctx.s = s;
		ctx.buffer = buffer;
		ctx.buf_size = buf_size;
		ctx.bytes_read = bytes_read;

		/* Set the context, call the parser, then unset the context. */
		t->parser.data = &ctx;

		bytes_parsed = http_parser_execute(&t->parser,
			&t->settings,
			t->parse_buffer.data,
			t->parse_buffer.offset);

		t->parser.data = NULL;

		/* If there was a handled authentication failure, then parse_error
		 * will have signaled us that we should replay the request. */
		if (PARSE_ERROR_REPLAY == t->parse_error) {
			s->sent_request = 0;

			if (http_connect(t) < 0)
				return -1;

			goto replay;
		}

		if (t->parse_error < 0)
			return -1;

		if (bytes_parsed != t->parse_buffer.offset) {
			giterr_set(GITERR_NET,
				"HTTP parser error: %s",
				http_errno_description((enum http_errno)t->parser.http_errno));
			return -1;
		}
	}

	return 0;
}