示例#1
0
文件: ini.c 项目: macosunity/rengine
/*
 *	Recursively adds sections to the tree of sections
 */
static void insert_section(ini_section *r, ini_section *n) {	
	assert(r);
	assert(n);
	
	if(my_stricmp(r->name, n->name) < 0) {
		if(!r->left) 
			r->left = n;
		else
			insert_section(r->left, n);
	} else {
		if(!r->right) 
			r->right = n;
		else
			insert_section(r->right, n);
	}		
}
示例#2
0
文件: ini.c 项目: macosunity/rengine
/*
 *	Creates a new section, and adds it to a tree of sections
 */
static ini_section *add_section(ini_section **root, char *name) {
	ini_section *n;
	
	assert(root);
	assert(name);
	
	n = find_section(*root, name);
	if(n) {
		free(name);
		return n;
	}
	
	n = malloc(sizeof *n);
	if(!n) return NULL;
	
	n->name = name;
	
	n->fields = NULL;
	n->left = n->right = NULL;
	
	if(*root)
		insert_section(*root, n);
	else
		*root = n;
	
	return n;
}
示例#3
0
文件: main.cpp 项目: akaridawn/lang3
int insert_plot(const char *input_filename, const char *out_filename)
{
	uint32_t offsets[2];
	int ret;
	unsigned char *text_buffer;
	FILE *fp;
	uint32_t section_offset=0, t1t2size=0;

	if ((text_buffer = (unsigned char *)calloc(0x800000, sizeof(unsigned char))) == NULL)
	{
		printf("Error allocating memory\n");
		return -2;
	}

	ret=insert_section(input_filename, text_buffer, &section_offset, t1t2size, offsets);
	
	if ((fp = fopen(out_filename, "wb")) == NULL)
	{
		printf("Error opening %s for writing\n", out_filename);
		free(text_buffer);
		return -3;
	}

	fwrite((void *)text_buffer, 1, DoubleWordSwap(offsets[1]), fp);
	fclose(fp);
	return 0;
}
示例#4
0
文件: ini.c 项目: macosunity/rengine
/*
 *	Sets a parameter 'par' in section 'sec's value to 'val', replacing the 
 *	current value if it already exists, or creates the section if it does not 
 *	exist
 */
int ini_put(struct ini_file *ini, const char *sec, const char *par, const char *val) {
	ini_section *s;
	ini_pair *p, **pp;
	
	if(!ini || !val) return 0;
	
	p = find_param(ini, sec, par);
	if(p) {
		/* Replace the existing value */
		char *t = p->value;
		if(!(p->value = my_strdup(val))) {
			p->value = t;
			return 0;
		}
				
		free(t);
		return 1;
	}
	
	if(sec) {
		s = find_section(ini->sections, sec);
		if(!s) {
			/* Create a new section */			
			if(!(s = malloc(sizeof *s))) return 0;			
			if(!(s->name = my_strdup(sec))) {
				free(s);
				return 0;
			}
			
			s->fields = NULL;
			s->left = s->right = NULL;
			
			if(ini->sections)
				insert_section(ini->sections, s);
			else
				ini->sections = s;
		}
		
		pp = &s->fields;
	} else
		pp = &ini->globals;
	
	if(!(p = malloc(sizeof *p)))
		return 0;
	
	if(!(p->param = my_strdup(par)) || !(p->value = my_strdup(val))) {
		free(p);
		return 0;
	}
	
	p->left = p->right = NULL;
	
	if(!*pp)
		*pp = p;
	else
		insert_pair(*pp, p);
		
	return 1;	
}
示例#5
0
int 
reply_info_encode(struct query_info* qinfo, struct reply_info* rep, 
	uint16_t id, uint16_t flags, sldns_buffer* buffer, time_t timenow, 
	struct regional* region, uint16_t udpsize, int dnssec)
{
	uint16_t ancount=0, nscount=0, arcount=0;
	struct compress_tree_node* tree = 0;
	int r;
	size_t rr_offset; 

	sldns_buffer_clear(buffer);
	if(udpsize < sldns_buffer_limit(buffer))
		sldns_buffer_set_limit(buffer, udpsize);
	if(sldns_buffer_remaining(buffer) < LDNS_HEADER_SIZE)
		return 0;

	sldns_buffer_write(buffer, &id, sizeof(uint16_t));
	sldns_buffer_write_u16(buffer, flags);
	sldns_buffer_write_u16(buffer, rep->qdcount);
	/* set an, ns, ar counts to zero in case of small packets */
	sldns_buffer_write(buffer, "\000\000\000\000\000\000", 6);

	/* insert query section */
	if(rep->qdcount) {
		if((r=insert_query(qinfo, &tree, buffer, region)) != 
			RETVAL_OK) {
			if(r == RETVAL_TRUNC) {
				/* create truncated message */
				sldns_buffer_write_u16_at(buffer, 4, 0);
				LDNS_TC_SET(sldns_buffer_begin(buffer));
				sldns_buffer_flip(buffer);
				return 1;
			}
			return 0;
		}
	}
	/* roundrobin offset. using query id for random number.  With ntohs
	 * for different roundrobins for sequential id client senders. */
	rr_offset = RRSET_ROUNDROBIN?ntohs(id):0;

	/* "prepend" any local alias records in the answer section if this
	 * response is supposed to be authoritative.  Currently it should
	 * be a single CNAME record (sanity-checked in worker_handle_request())
	 * but it can be extended if and when we support more variations of
	 * aliases. */
	if(qinfo->local_alias && (flags & BIT_AA)) {
		struct reply_info arep;
		time_t timezero = 0; /* to use the 'authoritative' TTL */
		memset(&arep, 0, sizeof(arep));
		arep.flags = rep->flags;
		arep.an_numrrsets = 1;
		arep.rrset_count = 1;
		arep.rrsets = &qinfo->local_alias->rrset;
		if((r=insert_section(&arep, 1, &ancount, buffer, 0,
			timezero, region, &tree, LDNS_SECTION_ANSWER,
			qinfo->qtype, dnssec, rr_offset)) != RETVAL_OK) {
			if(r == RETVAL_TRUNC) {
				/* create truncated message */
				sldns_buffer_write_u16_at(buffer, 6, ancount);
				LDNS_TC_SET(sldns_buffer_begin(buffer));
				sldns_buffer_flip(buffer);
				return 1;
			}
			return 0;
		}
	}

	/* insert answer section */
	if((r=insert_section(rep, rep->an_numrrsets, &ancount, buffer, 
		0, timenow, region, &tree, LDNS_SECTION_ANSWER, qinfo->qtype, 
		dnssec, rr_offset)) != RETVAL_OK) {
		if(r == RETVAL_TRUNC) {
			/* create truncated message */
			sldns_buffer_write_u16_at(buffer, 6, ancount);
			LDNS_TC_SET(sldns_buffer_begin(buffer));
			sldns_buffer_flip(buffer);
			return 1;
		}
		return 0;
	}
	sldns_buffer_write_u16_at(buffer, 6, ancount);

	/* if response is positive answer, auth/add sections are not required */
	if( ! (MINIMAL_RESPONSES && positive_answer(rep, qinfo->qtype)) ) {
		/* insert auth section */
		if((r=insert_section(rep, rep->ns_numrrsets, &nscount, buffer, 
			rep->an_numrrsets, timenow, region, &tree,
			LDNS_SECTION_AUTHORITY, qinfo->qtype,
			dnssec, rr_offset)) != RETVAL_OK) {
			if(r == RETVAL_TRUNC) {
				/* create truncated message */
				sldns_buffer_write_u16_at(buffer, 8, nscount);
				LDNS_TC_SET(sldns_buffer_begin(buffer));
				sldns_buffer_flip(buffer);
				return 1;
			}
			return 0;
		}
		sldns_buffer_write_u16_at(buffer, 8, nscount);

		/* insert add section */
		if((r=insert_section(rep, rep->ar_numrrsets, &arcount, buffer, 
			rep->an_numrrsets + rep->ns_numrrsets, timenow, region, 
			&tree, LDNS_SECTION_ADDITIONAL, qinfo->qtype, 
			dnssec, rr_offset)) != RETVAL_OK) {
			if(r == RETVAL_TRUNC) {
				/* no need to set TC bit, this is the additional */
				sldns_buffer_write_u16_at(buffer, 10, arcount);
				sldns_buffer_flip(buffer);
				return 1;
			}
			return 0;
		}
		sldns_buffer_write_u16_at(buffer, 10, arcount);
	}
	sldns_buffer_flip(buffer);
	return 1;
}
示例#6
0
struct ini_doc *create_ini_doc(char * file_name) {
	char buffer[256];

	FILE * file = fopen(file_name, "r");

	if (file == NULL)
		return 0;

	struct ini_doc* pIniDoc = malloc(sizeof(struct ini_doc));
	if (pIniDoc == NULL)
		return NULL;

	pIniDoc->sectionNum = 0;
	pIniDoc->sections = NULL;

	struct ini_section * pSection = NULL;

	while (1) {
		char* line = fgets(buffer, sizeof(buffer), file);
		int length = 0;
		int index = -1;
		char * name = NULL;
		char *value = NULL;
		if (line == NULL)
			break;

		line = trim_string(line, &length);
		if (line == NULL)
			continue;
		if (line[0] == ';')
			continue;
		if (line[0] == '[') {
			index = find_index(line, 1, ']');
			if (index > 2) //find a new section
					{
				line[index] = '\0';
				name = (char*) malloc(index);
				strcpy(name, line + 1);
				name[index - 1] = '\0';
				if (pSection != NULL) {
					insert_section(pIniDoc, pSection);
				}
				pSection = (struct ini_section*) malloc(
						sizeof(struct ini_section));
				pSection->next = NULL;
				pSection->name = name;

			}
		} else if (pSection != NULL) {
			index = find_index(line, 0,'=');
			if (index >= 1 && index < length - 1) //find a new key-value
					{
				line[index] = '\0';
				struct ini_key *pKey = (struct ini_key *) malloc(
						sizeof(struct ini_key));
				pKey->next = NULL;
				name = (char*) malloc(index + 1);
				strcpy(name, line);
				name[index] = '\0';
				pKey->key = name;

				value = (char*) malloc(length - index);
				strcpy(value,line+index+1);
				value[length-index-1]='\0';
				pKey->value = value;

				insert_key(pSection,pKey);

			}

		}
	}

	if (pSection != NULL) {
		insert_section(pIniDoc, pSection);
	}
	fclose(file);

	return pIniDoc;
}
示例#7
0
int 
reply_info_encode(struct query_info* qinfo, struct reply_info* rep, 
	uint16_t id, uint16_t flags, ldns_buffer* buffer, uint32_t timenow, 
	struct regional* region, uint16_t udpsize, int dnssec)
{
	uint16_t ancount=0, nscount=0, arcount=0;
	struct compress_tree_node* tree = 0;
	int r;
	size_t rr_offset; 

	ldns_buffer_clear(buffer);
	if(udpsize < ldns_buffer_limit(buffer))
		ldns_buffer_set_limit(buffer, udpsize);
	if(ldns_buffer_remaining(buffer) < LDNS_HEADER_SIZE)
		return 0;

	ldns_buffer_write(buffer, &id, sizeof(uint16_t));
	ldns_buffer_write_u16(buffer, flags);
	ldns_buffer_write_u16(buffer, rep->qdcount);
	/* set an, ns, ar counts to zero in case of small packets */
	ldns_buffer_write(buffer, "\000\000\000\000\000\000", 6);

	/* insert query section */
	if(rep->qdcount) {
		if((r=insert_query(qinfo, &tree, buffer, region)) != 
			RETVAL_OK) {
			if(r == RETVAL_TRUNC) {
				/* create truncated message */
				ldns_buffer_write_u16_at(buffer, 4, 0);
				LDNS_TC_SET(ldns_buffer_begin(buffer));
				ldns_buffer_flip(buffer);
				return 1;
			}
			return 0;
		}
	}
	/* roundrobin offset. using query id for random number */
	rr_offset = RRSET_ROUNDROBIN?id:0;

	/* insert answer section */
	if((r=insert_section(rep, rep->an_numrrsets, &ancount, buffer, 
		0, timenow, region, &tree, LDNS_SECTION_ANSWER, qinfo->qtype, 
		dnssec, rr_offset)) != RETVAL_OK) {
		if(r == RETVAL_TRUNC) {
			/* create truncated message */
			ldns_buffer_write_u16_at(buffer, 6, ancount);
			LDNS_TC_SET(ldns_buffer_begin(buffer));
			ldns_buffer_flip(buffer);
			return 1;
		}
		return 0;
	}
	ldns_buffer_write_u16_at(buffer, 6, ancount);

	/* if response is positive answer, auth/add sections are not required */
	if( ! (MINIMAL_RESPONSES && positive_answer(rep, qinfo->qtype)) ) {
		/* insert auth section */
		if((r=insert_section(rep, rep->ns_numrrsets, &nscount, buffer, 
			rep->an_numrrsets, timenow, region, &tree,
			LDNS_SECTION_AUTHORITY, qinfo->qtype,
			dnssec, rr_offset)) != RETVAL_OK) {
			if(r == RETVAL_TRUNC) {
				/* create truncated message */
				ldns_buffer_write_u16_at(buffer, 8, nscount);
				LDNS_TC_SET(ldns_buffer_begin(buffer));
				ldns_buffer_flip(buffer);
				return 1;
			}
			return 0;
		}
		ldns_buffer_write_u16_at(buffer, 8, nscount);

		/* insert add section */
		if((r=insert_section(rep, rep->ar_numrrsets, &arcount, buffer, 
			rep->an_numrrsets + rep->ns_numrrsets, timenow, region, 
			&tree, LDNS_SECTION_ADDITIONAL, qinfo->qtype, 
			dnssec, rr_offset)) != RETVAL_OK) {
			if(r == RETVAL_TRUNC) {
				/* no need to set TC bit, this is the additional */
				ldns_buffer_write_u16_at(buffer, 10, arcount);
				ldns_buffer_flip(buffer);
				return 1;
			}
			return 0;
		}
		ldns_buffer_write_u16_at(buffer, 10, arcount);
	} else {
		ldns_buffer_write_u16_at(buffer, 8, nscount);
		ldns_buffer_write_u16_at(buffer, 10, arcount);
	}
	ldns_buffer_flip(buffer);
	return 1;
}
示例#8
0
文件: main.cpp 项目: akaridawn/lang3
int insert_d00(const char *orig_filename, const char *input_files, const char *out_filename)
{
	char filename[_MAX_PATH];
	int i;
	FILE *origfp, *outfp;
	uint32_t section_offset=0x800;
	unsigned char *text_buffer;
	//uint16_t *tbl;
	uint32_t offset, size;
	uint32_t *offsets;
	//int text_counter;
	int num_sections;
	int ret;

	origfp = fopen(orig_filename, "rb");

	if (origfp == NULL)
	{
		printf("Error opening %s for reading\n", out_filename);
		return -1;
	}

	if ((text_buffer = (unsigned char *)calloc(0x800000, sizeof(unsigned char))) == NULL)
	{
		printf("Error allocating memory\n");
		fclose(origfp);
		return -2;
	}

	offsets = (uint32_t *)(text_buffer+4);

	fread((void *)&num_sections, 4, 1, origfp);
	*((uint32_t *)text_buffer) = num_sections;
	num_sections = DoubleWordSwap(num_sections);

	for (i = 0; i < num_sections; i++)
	{
		// Section:
		// 0x00000000: table1(each entry is 4 bytes), first entry is offset to second table 
		// 0x????????: data for table1 here
		// 0x????????: table2(each entry is 4 bytes). Last entry in table is offset to table 3
		// 0x????????: data for table 2
		// 0x????????: table 3. First entry is size of table 3 + table 3 data(text strings)(4 bytes), after that it's offsets to strings(2 bytes per entry), after that strings.

		uint32_t tbl1offset, tbl2offset, t1t2size;

		fseek(origfp, 4+(i*8), SEEK_SET);
		fread((void *)&offset, 4, 1, origfp);
		offset = DoubleWordSwap(offset);
		fread((void *)&size, 4, 1, origfp);
		size = DoubleWordSwap(size);

		fseek(origfp, offset*2048, SEEK_SET);
		// Table 1
		fread((void *)&tbl1offset, 4, 1, origfp);
		tbl1offset = DoubleWordSwap(tbl1offset);

		fseek(origfp, (offset*2048)+tbl1offset+(16*4), SEEK_SET);
		// Table 2
		fread((void *)&tbl2offset, 4, 1, origfp);
		tbl2offset = DoubleWordSwap(tbl2offset);

		t1t2size = tbl1offset+tbl2offset;

		// Backup table 1 and table 1 data, table 2 and table 2 data
		fseek(origfp, offset*2048, SEEK_SET);
		fread(text_buffer+section_offset, 1, t1t2size, origfp);

		sprintf(filename, input_files, i+1);
		ret = insert_section(filename, text_buffer, &section_offset, t1t2size, offsets+(i*2));

		if (ret == -1)
		{
			fseek(origfp, offset*2048, SEEK_SET);
			fread(text_buffer+section_offset, 1, size, origfp);

			offsets[(i*2)] = DoubleWordSwap(section_offset/2048);
			offsets[(i*2)+1] = DoubleWordSwap(size);
			section_offset += size;

			if (section_offset & 0x7FF)
			{
				section_offset += 0x800;
				section_offset &= ~0x7FF;
			}

			ret = 0;
		}
		if (ret != 0)
		{
			fclose(origfp);
			free(text_buffer);
			return ret;
		}
	}

	if ((outfp = fopen(out_filename, "wb")) == NULL)
	{
		printf("Error opening %s for writing\n", out_filename);
		ret = -1;
	}
	else
	{
		fwrite((void *)text_buffer, 1, section_offset, outfp);
		fflush(outfp);
		ret = 0;
	}

	if (origfp)
		fclose(origfp);
	if (outfp)
		fclose(outfp);
	if (text_buffer)
		free(text_buffer);
	return ret;
}