Beispiel #1
0
//нужно учитывать выравнивание
UINT8_T sector_Create(UINT8_T count,UINT8_T aligment)
{
	sl=(SectorList*)local_malloc(sizeof(SectorList)); //размер структуры с описанием секторов
	if(sl==NULL)
		return ERR_LOCAL_MALLOC;

	memset((void*)sl,0x00,sizeof(SectorList));

	//с учетом выравнивания памяти сектора
	sl->sector=(SectorInfo*)local_malloc((sizeof(SectorInfo)*count+(aligment-1)) &  ~(aligment-1));
	if(sl->sector==NULL)
	{
		local_free((void*)sl);
		sl=NULL;
		return ERR_LOCAL_MALLOC;
	}

	memset((void*)sl->sector,0x00,sizeof(SectorInfo)*count);

	xStart=(BlockLink*)local_malloc(sizeof(BlockLink));
	if( (xStart==NULL) )
	{
		local_free((void*)sl->sector);
		local_free((void*)sl);
		sl=NULL;
		return ERR_LOCAL_MALLOC;
	}

	sl->sector_counter=count;

	ApplicationSectorPrepareHook();
	
	return ERR_OK;
}
Beispiel #2
0
/*-----------------------------------------------------------*/
void sector_ResourceFree()
{
	local_free((void*)sl->sector);
	local_free((void*)sl);
	local_free((void*)xStart);

	sl=NULL;
	xStart=NULL;
}
Beispiel #3
0
static void cleanupImp(LADSPA_Handle instance) {
	Imp *plugin_data = (Imp *)instance;
	local_free(plugin_data->block_time);
	local_free(plugin_data->block_freq);
	local_free(plugin_data->op);
	local_free(plugin_data->overlap);
	local_free(plugin_data->opc);
	free(instance);
}
Beispiel #4
0
/* frees completely a struct rdata list */
void free_rdata_list(struct rdata* head)
{
	struct rdata* l;
	struct rdata* next_l;
	l=head;
	while (l != 0) {
		next_l = l->next;
		/* free the parsed rdata*/
		if (l->rdata) local_free(l->rdata);
		local_free(l);
		l = next_l;
	}
}
Beispiel #5
0
/*!
 * \brief destroys everything init_io_wait allocated
 * \param h IO handle
 */
void destroy_io_wait(io_wait_h* h)
{
	switch(h->poll_method){
#ifdef HAVE_EPOLL
		case POLL_EPOLL_LT:
		case POLL_EPOLL_ET:
			destroy_epoll(h);
			if (h->ep_array){
				local_free(h->ep_array);
				h->ep_array=0;
			}
		break;
#endif
#ifdef HAVE_KQUEUE
		case POLL_KQUEUE:
			destroy_kqueue(h);
			if (h->kq_array){
				local_free(h->kq_array);
				h->kq_array=0;
			}
			if (h->kq_changes){
				local_free(h->kq_changes);
				h->kq_changes=0;
			}
			break;
#endif
#ifdef HAVE_SIGIO_RT
		case POLL_SIGIO_RT:
			destroy_sigio(h);
			break;
#endif
#ifdef HAVE_DEVPOLL
		case POLL_DEVPOLL:
			destroy_devpoll(h);
			break;
#endif
		default: /*do  nothing*/
			;
	}
		if (h->fd_array){
			local_free(h->fd_array);
			h->fd_array=0;
		}
		if (h->fd_hash){
			local_free(h->fd_hash);
			h->fd_hash=0;
		}
}
Beispiel #6
0
/* parses a PTR record into a ptr_rdata structure */
struct ptr_rdata* dns_ptr_parser( unsigned char* msg, unsigned char* end,
									  unsigned char* rdata)
{
	struct ptr_rdata* pname;
	int len;
	char name[MAX_DNS_NAME];
	
	pname=0;
	if (dn_expand(msg, end, rdata, name, MAX_DNS_NAME-1)==-1)
		goto error;
	len=strlen(name);
	if (len>255)
		goto error;
	/* alloc sizeof struct + space for the null terminated name */
	pname=local_malloc(sizeof(struct ptr_rdata)-1+len+1);
	if(pname==0){
		LOG(L_ERR, "ERROR: dns_ptr_parser: out of memory\n");
		goto error;
	}
	pname->ptrdname_len=len;
	memcpy(pname->ptrdname, name, pname->ptrdname_len);
	pname->ptrdname[pname->ptrdname_len]=0;
	return pname;
error:
	if (pname) local_free(pname);
	return 0;
}
Beispiel #7
0
/** parses a CNAME record into a cname_rdata structure */
struct cname_rdata* dns_cname_parser( unsigned char* msg, unsigned char* end,
									  unsigned char* rdata)
{
	struct cname_rdata* cname;
	int len;
	char name[MAX_DNS_NAME];
	
	cname=0;
	if (dn_expand(msg, end, rdata, name, MAX_DNS_NAME-1)==-1)
		goto error;
	len=strlen(name);
	if (len>255)
		goto error;
	/* alloc sizeof struct + space for the null terminated name */
	cname=local_malloc(sizeof(struct cname_rdata)-1+len+1);
	if(cname==0){
		LM_ERR("out of memory\n");
		goto error;
	}
	cname->name_len=len;
	memcpy(cname->name, name, cname->name_len);
	cname->name[cname->name_len]=0;
	return cname;
error:
	if (cname) local_free(cname);
	return 0;
}
Beispiel #8
0
/* Free memory used by owner-drawn strings.  */
static void
w32_free_submenu_strings (HMENU menu)
{
  int i, num = GetMenuItemCount (menu);
  for (i = 0; i < num; i++)
    {
      MENUITEMINFO info;
      memset (&info, 0, sizeof (info));
      info.cbSize = sizeof (info);
      info.fMask = MIIM_DATA | MIIM_TYPE | MIIM_SUBMENU;

      get_menu_item_info (menu, i, TRUE, &info);

      /* Owner-drawn names are held in dwItemData.  */
      if ((info.fType & MF_OWNERDRAW) && info.dwItemData)
	{
#ifdef MENU_DEBUG
	  DebPrint ("Menu: freeing %ld for owner-draw", info.dwItemData);
#endif
	  local_free (info.dwItemData);
	}

      /* Recurse down submenus.  */
      if (info.hSubMenu)
	w32_free_submenu_strings (info.hSubMenu);
    }
}
Beispiel #9
0
/*-----------------------------------------------------------*/
UINT8_T WriteBlockLink(UINT8_T index, BlockLink *bl)
{
	UINT16_T crc;
	UINT8_T	 *buf;
	UINT8_T	 i;

	buf=(UINT8_T*)local_malloc(sl->sector[index].bl_size);

	if(buf==NULL)
		return ERR_LOCAL_MALLOC;

	memset((void*)buf,0x00,sl->sector[index].bl_size);

	//сформировать пакет для записи и сгенерить crc16 если требуется
	//копируем заголовок сегмента в массив для записи
	i=0;
	memcpy((void*)buf,(void*)&bl->body.pxNextFreeBlock,sl->sector[index].StartAddrLen);
	i=sl->sector[index].StartAddrLen;
	memcpy((void*)(buf+i),(void*)&bl->body.xBlockSize,sl->sector[index].SectorSizeLen);
	i+=sl->sector[index].SectorSizeLen;

	if((sl->sector[index].Type & SECTOR_CRC)>0)
	{
		Crc16_Clear();
		crc=Crc16(buf,i);
		memcpy((void*)(buf+i),(void*)&crc,sizeof(UINT16_T));
	}

	i=ERR_OK;

	i=sector_write(index, bl->pxCurrentAddr, (void*)buf, sl->sector[index].bl_size);

	local_free(buf);
	return i;
}
Beispiel #10
0
/*-----------------------------------------------------------*/
UINT8_T	sector_Free(UINT8_T index, UINT32_T pv )
{
	UINT32_T puc=pv;
	BlockLink *pxLink=NULL;
	UINT8_T err=ERR_OK;

	if(sl==NULL)
	{
		err=ERR_SL_NULL;
		goto exit;
	}

	pxLink=(BlockLink*)local_malloc(sizeof(BlockLink));

	if(pxLink==NULL)
	{
		err=ERR_LOCAL_MALLOC;
		goto exit;
	}

	memset((void*)pxLink,0x00,sizeof(BlockLink));

	if( pv != 0 )
	{
		/* The memory being freed will have an BlockLink_t structure immediately
		before it. */
		puc -= sl->sector[index].bl_size;

		/* This casting is to keep the compiler from issuing warnings. */
		pxLink->pxCurrentAddr=puc;
		err=ReadBlockLink(index,pxLink);
		if(err!=ERR_OK)
			goto exit;

		if( ( pxLink->body.xBlockSize & (UINT32_T)(0x01 << ((8*sl->sector[index].SectorSizeLen)-1)) ) != 0 )								//!!!!!!!!!!!!!!
		{
			if( pxLink->body.pxNextFreeBlock == 0 )
			{
				/* The block is being returned to the heap - it is no longer
				allocated. */
				pxLink->body.xBlockSize&=~(UINT32_T)(0x01 << ((8*sl->sector[index].SectorSizeLen)-1));

				/* Add this block to the list of free blocks. */
				sl->sector[index].FreeBytesRemaining+=pxLink->body.xBlockSize;
				prvInsertBlockIntoFreeList(index,pxLink);
			}
		}
	}

exit:
	local_free((void*)pxLink);

	return err;
}
Beispiel #11
0
/*  TXT rdata format:
 *
 * one or several character strings:
 *  01234567
 * +--------------------+
 * | len    | string   / ...
 * |------------------+
 */
static struct txt_rdata* dns_txt_parser(unsigned char* msg, unsigned char* end,
										unsigned char* rdata)
{
	struct txt_rdata* txt;
	int len, n, i;
	int str_size;
	unsigned char* p;
	unsigned char* st;
	
	txt=0;
	if (unlikely((rdata+1)>end)) goto error;
	n=0;
	str_size=0;
	/* count the number of strings */
	p=rdata;
	do{
		len=*p;
		p+=len+1;
		str_size+=len+1; /* 1 for the term. 0 */
		if (unlikely(p>end)) goto error;
		n++;
	}while(p<end);
	/* alloc sizeof struct + space for the dns_cstr array + space for
	   the strings */
	txt=local_malloc(sizeof(struct txt_rdata) +(n-1)*sizeof(struct dns_cstr)+
						str_size);
	if(unlikely(txt==0)){
		LOG(L_ERR, "ERROR: dns_txt_parser: out of memory\n");
		goto error;
	}
	/* string table */
	st=(unsigned char*)txt+sizeof(struct txt_rdata) +
		(n-1)*sizeof(struct dns_cstr);
	txt->cstr_no=n;
	txt->tslen=str_size;
	/* fill the structure */
	p=rdata;
	for (i=0; i<n; i++){
		len=*p;
		memcpy(st, p+1, len);
		st[len]=0;
		txt->txt[i].cstr_len=len;
		txt->txt[i].cstr=(char*)st;
		st+=len+1;
		p+=len+1;
	}
	return txt;
error:
	if (txt) local_free(txt);
	return 0;
}
Beispiel #12
0
/*-----------------------------------------------------------*/
UINT8_T ReadBlockLink(UINT8_T index , BlockLink *bl)
{
	UINT16_T crc;
	UINT8_T	 *buf;
	UINT8_T	 i;
	UINT8_T	 err=ERR_OK;

	buf=(UINT8_T*)local_malloc(sl->sector[index].bl_size);

	if(buf==NULL)
		return ERR_LOCAL_MALLOC;

	err=sector_read(index, bl->pxCurrentAddr, (void*)buf, sl->sector[index].bl_size);

	if(err!=ERR_OK)
		goto exit;

	//проверка CRC если требуется
	if((sl->sector[index].Type & SECTOR_CRC)>0)
	{
		Crc16_Clear();
		i=sl->sector[index].StartAddrLen + sl->sector[index].SectorSizeLen; //размер структуры BlockLink
		crc=Crc16(buf,i);
		
		err=memcmp((void*)(buf+i),(void*)&crc,sizeof(UINT16_T));
		if(err!=0x00)
		{
			err=ERR_CRC;
			goto exit;
		}
	}

	//загружаем в структуру
	i=sl->sector[index].StartAddrLen;
	bl->body.pxNextFreeBlock=0;
	memcpy((void*)&bl->body.pxNextFreeBlock,(void*)buf,i);
	bl->body.xBlockSize=0;
	memcpy((void*)&bl->body.xBlockSize,(void*)(buf+i),sl->sector[index].SectorSizeLen);

exit:
	local_free(buf);

	return err;
}
Beispiel #13
0
/* SRV rdata format:
 *            111111
 *  0123456789012345
 * +----------------+
 * |     priority   |
 * |----------------|
 * |     weight     |
 * |----------------|
 * |   port number  |
 * |----------------|
 * |                |
 * ~      name      ~
 * |                |
 * +----------------+
 */
struct srv_rdata* dns_srv_parser( unsigned char* msg, unsigned char* end,
								  unsigned char* eor,
								  unsigned char* rdata)
{
	struct srv_rdata* srv;
	unsigned short priority;
	unsigned short weight;
	unsigned short port;
	int len;
	char name[MAX_DNS_NAME];
	
	srv=0;
	if ((rdata+6+1)>eor) goto error;
	
	memcpy((void*)&priority, rdata, 2);
	memcpy((void*)&weight,   rdata+2, 2);
	memcpy((void*)&port,     rdata+4, 2);
	rdata+=6;
	if (dn_expand(msg, end, rdata, name, MAX_DNS_NAME-1)<0)
		goto error;
	len=strlen(name);
	if (len>255)
		goto error;
	/* alloc enought space for the struct + null terminated name */
	srv=local_malloc(sizeof(struct srv_rdata)-1+len+1);
	if (srv==0){
		LOG(L_ERR, "ERROR: dns_srv_parser: out of memory\n");
		goto error;
	}
	srv->priority=ntohs(priority);
	srv->weight=ntohs(weight);
	srv->port=ntohs(port);
	srv->name_len=len;
	memcpy(srv->name, name, srv->name_len);
	srv->name[srv->name_len]=0;
	
	return srv;
error:
	if (srv) local_free(srv);
	return 0;
}
Beispiel #14
0
/*  EBL rdata format:
 *  (see http://tools.ietf.org/html/draft-ietf-enum-branch-location-record-03)
 * one or several character strings:
 *  01234567
 * +--------+
 * | postion|
 * +-----------+
 * / separator /
 * +-----------+
 * /   apex    /
 * +----------+
 *
 * where separator is a character string ( 8 bit len, followed by len chars)
 * and apex is a domain-name.
 */
static struct ebl_rdata* dns_ebl_parser(unsigned char* msg, unsigned char* end,
										unsigned char* eor,
										unsigned char* rdata)
{
	struct ebl_rdata* ebl;
	int sep_len;
	int apex_len;
	char apex[MAX_DNS_NAME];
	
	ebl=0;
	/* check if len is at least 4 chars (minimum possible):
	     pos (1 byte) +  sep. (min 1 byte) + apex (min. 2 bytes) 
	   and also check if rdata+1 (pos) + 1 (sep. len) + sep_len + 1 is ok*/
	if (unlikely(((rdata+4)>eor)||((rdata+1+1+rdata[1]+2)>eor))) goto error;
	sep_len=rdata[1];
	if (unlikely(dn_expand(msg, end, rdata+1+1+sep_len,
							apex, MAX_DNS_NAME-1)==-1))
		goto error;
	apex_len=strlen(apex);
	/* alloc sizeof struct + space for the 2 null-terminated strings */
	ebl=local_malloc(sizeof(struct ebl_rdata)-1+sep_len+1+apex_len+1);
	if (ebl==0){
		LOG(L_ERR, "ERROR: dns_ebl_parser: out of memory\n");
		goto error;
	}
	ebl->position=rdata[0];
	ebl->separator=&ebl->str_table[0];
	ebl->apex=ebl->separator+sep_len+1;
	ebl->separator_len=sep_len;
	ebl->apex_len=apex_len;
	memcpy(ebl->separator, rdata+2, sep_len);
	ebl->separator[sep_len]=0;
	memcpy(ebl->apex, apex, apex_len);
	ebl->apex[apex_len]=0;
	
	return ebl;
error:
	if (ebl) local_free(ebl);
	return 0;
}
Beispiel #15
0
/*-----------------------------------------------------------*/
UINT8_T sector_Init(UINT8_T index)
{
	BlockLink		*pxFirstFreeBlock=NULL;
	UINT32_T		pucAlignedHeap;
	UINT32_T		ulAddress;
	UINT32_T		xTotalHeapSize;
	UINT8_T			err;

	pxFirstFreeBlock=(BlockLink*)local_malloc(sizeof(BlockLink));

	if(pxFirstFreeBlock==NULL)
		return ERR_LOCAL_MALLOC;

	/* Ensure the heap starts on a correctly aligned boundary. */
	ulAddress =	sl->sector[index].xStart_Addr + sl->sector[index].bl_size;
	xTotalHeapSize = sl->sector[index].FreeBytesRemaining - sl->sector[index].bl_size;

	if( ( ulAddress & (sl->sector[index].ByteAligment-1) ) != 0 )
	{
		ulAddress += sl->sector[index].ByteAligment-1 ;
		ulAddress &=  ~( ( UINT32_T ) (sl->sector[index].ByteAligment-1) );
		xTotalHeapSize -= ulAddress - sl->sector[index].xStart_Addr;
	}

	pucAlignedHeap = ulAddress;

	/* xStart is used to hold a pointer to the first item in the list of free
	blocks.  The void cast is used to prevent compiler warnings. */
	sl->sector[index].xStart_Addr= ulAddress - sl->sector[index].bl_size;
	xStart->pxCurrentAddr=sl->sector[index].xStart_Addr;
	xStart->body.pxNextFreeBlock=pucAlignedHeap;
	xStart->body.xBlockSize=0;

	err=WriteBlockLink(index,xStart);

	if(err==ERR_OK)
	{
		/* pxEnd is used to mark the end of the list of free blocks and is inserted
		at the end of the heap space. */
		ulAddress = ( ( UINT32_T ) pucAlignedHeap ) + xTotalHeapSize;
		ulAddress -= sl->sector[index].bl_size;
		ulAddress &= ~( (UINT32_T)(sl->sector[index].ByteAligment-1));

		//записать pxEnd используя буффер pxFirstFreeBlock
		pxFirstFreeBlock->body.pxNextFreeBlock=0;
		pxFirstFreeBlock->body.xBlockSize=0;
		sl->sector[index].pxEnd_Addr=ulAddress;
		pxFirstFreeBlock->pxCurrentAddr=sl->sector[index].pxEnd_Addr;
		err=WriteBlockLink(index,pxFirstFreeBlock);

		if(err==ERR_OK)
		{
			/* To start with there is a single free block that is sized to take up the
			entire heap space, minus the space taken by pxEnd. */
			pxFirstFreeBlock->pxCurrentAddr=pucAlignedHeap;
			pxFirstFreeBlock->body.xBlockSize= ulAddress - pucAlignedHeap;
			pxFirstFreeBlock->body.pxNextFreeBlock=sl->sector[index].pxEnd_Addr;

			err=WriteBlockLink(index,pxFirstFreeBlock);

			if(err==ERR_OK)
			{
				/* Only one block exists - and it covers the entire usable heap space. */
				sl->sector[index].FreeBytesRemaining = pxFirstFreeBlock->body.xBlockSize;

				#if (configUSE_SegmentCounter==TRUE)
				sl->sector[index].xSegmentCounter=3;
				#endif
			}
		}
	}

	local_free((void*)pxFirstFreeBlock);

	return err;
}
Beispiel #16
0
/* NAPTR rdata format:
 *            111111
 *  0123456789012345
 * +----------------+
 * |      order     |
 * |----------------|
 * |   preference   |
 * |----------------|
 * ~     flags      ~
 * |   (string)     |
 * |----------------|
 * ~    services    ~
 * |   (string)     |
 * |----------------|
 * ~    regexp      ~
 * |   (string)     |
 * |----------------|
 * ~  replacement   ~
   |    (name)      |
 * +----------------+
 */
struct naptr_rdata* dns_naptr_parser( unsigned char* msg, unsigned char* end,
										unsigned char* eor,
										unsigned char* rdata)
{
	struct naptr_rdata* naptr;
	unsigned char* flags;
	unsigned char* services;
	unsigned char* regexp;
	unsigned short order;
	unsigned short pref;
	unsigned char flags_len;
	unsigned char services_len;
	unsigned char regexp_len;
	int len;
	char repl[MAX_DNS_NAME];
	
	naptr = 0;
	if ((rdata + 7 + 1)>eor) goto error;
	
	memcpy((void*)&order, rdata, 2);
	memcpy((void*)&pref, rdata + 2, 2);
	flags_len = rdata[4];
	if ((rdata + 7 + 1 +  flags_len) > eor)
		goto error;
	flags=rdata+5;
	services_len = rdata[5 + flags_len];
	if ((rdata + 7 + 1 + flags_len + services_len) > eor)
		goto error;
	services=rdata + 6 + flags_len;
	regexp_len = rdata[6 + flags_len + services_len];
	if ((rdata + 7 +1 + flags_len + services_len + regexp_len) > eor)
		goto error;
	regexp=rdata + 7 + flags_len + services_len;
	rdata = rdata + 7 + flags_len + services_len + regexp_len;
	if (dn_expand(msg, end, rdata, repl, MAX_DNS_NAME-1) == -1)
		goto error;
	len=strlen(repl);
	if (len>255)
		goto error;
	naptr=local_malloc(sizeof(struct naptr_rdata)+flags_len+services_len+
						regexp_len+len+1-1);
	if (naptr == 0){
		LOG(L_ERR, "ERROR: dns_naptr_parser: out of memory\n");
		goto error;
	}
	naptr->order=ntohs(order);
	naptr->pref=ntohs(pref);
	
	naptr->flags=&naptr->str_table[0];
	naptr->flags_len=flags_len;
	memcpy(naptr->flags, flags, naptr->flags_len);
	naptr->services=&naptr->str_table[flags_len];
	naptr->services_len=services_len;
	memcpy(naptr->services, services, naptr->services_len);
	naptr->regexp=&naptr->str_table[flags_len+services_len];
	naptr->regexp_len=regexp_len;
	memcpy(naptr->regexp, regexp, naptr->regexp_len);
	naptr->repl=&naptr->str_table[flags_len+services_len+regexp_len];
	naptr->repl_len=len;
	memcpy(naptr->repl, repl, len);
	naptr->repl[len]=0; /* null term. */
	
	return naptr;
error:
	if (naptr) local_free(naptr);
	return 0;
}
Beispiel #17
0
/*-----------------------------------------------------------*/
UINT8_T sector_Open(UINT8_T index, UINT8_T aligment ,UINT32_T addr)
{
	UINT16_T size;
	UINT8_T	err=ERR_OK;
	SectorList *header=NULL;
	UINT8_T i;

	//подготовить
	if(aligment==0)
		return ERR_WRONG_ALIGMENT;

	size=(sizeof(SectorList)+(aligment-1)) & ~(aligment-1);
	header=(SectorList*)local_malloc( size );

	if(header==NULL)
		return ERR_LOCAL_MALLOC;

	//чтение заголовка. Учитываем выравнивание
	size=(sizeof(SectorList)-sizeof(SectorInfo*)+(aligment-1)) & ~(aligment-1);
	err=sector_read(index, addr, (void*)header, size);

	if(err==ERR_OK)
	{
		i=~header->sector_counter;
		if(i == header->crc)
		{
			//создание структур
			err=sector_Create(header->sector_counter,aligment);

			if(err==ERR_OK)
			{
				err=sector_read(index, (addr+size), (void*)sl->sector, (sizeof(SectorInfo)*sl->sector_counter + (aligment-1)) & ~(aligment-1) );

				if(err!=ERR_OK)
				{
					sector_ResourceFree();
				}
				else
				{
					sl->crc=header->crc;
					//проверка CRC16
					Crc16_Clear();
					Crc16((UINT8_T*)&sl->sector_counter,sizeof(UINT8_T));
					Crc16((UINT8_T*)&sl->crc,sizeof(UINT8_T));
					sl->crc16=Crc16((UINT8_T*)sl->sector,sizeof(SectorInfo)*sl->sector_counter);

					if(header->crc16!=sl->crc16)
					{
						sector_ResourceFree();
						err=ERR_CRC;
					}
					else
					{
						for(i=0;i<sl->sector_counter;i++)
						{
							if(sl->sector[i].Type!=SECTOR_FREE)
							{
								size=(sl->sector[index].pxEnd_Addr+sl->sector[index].bl_size)-sl->sector[index].StartAddr;
								ApplicationSectorOpenHook(i, sl->sector[i].StartAddr,size);
							}
						}
					}
				}
			}
		}
		else
		{
			err=ERR_CRC;
		}
	}

	local_free(header);
	return err;
}
Beispiel #18
0
/*-----------------------------------------------------------*/
UINT8_T sector_AddNewSector(SectorConfig* config)
{
	UINT8_T i;
	SectorInfo *si=NULL;
	UINT16_T size,total_size,prev_total_size;

	if(sl==NULL)
		return ERR_SL_NULL;

	//если есть свободное место
	for(i=0; i<(sl->sector_counter) ;i++)
	{
		if(sl->sector[i].Type==SECTOR_FREE)
		{
			config->index=i;
			return sector_Insert(config);
		}
	}

	//если нет, то remalloc
	for(i=0;i<(sl->sector_counter);i++)
	{
		//если только main
		if((sl->sector[i].Type & SECTOR_MAIN)>0)
		{
			if((sl->sector[i].Type & SECTOR_START) == 0)
			{
				//старый размер списка
				size=(sizeof(SectorInfo)*(sl->sector_counter) + (sl->sector[i].ByteAligment-1)) & ~(sl->sector[i].ByteAligment-1);
				prev_total_size= size + ((sizeof(SectorList)-sizeof(SectorInfo*))+( sl->sector[i].ByteAligment - 1)) & ~(sl->sector[i].ByteAligment-1);

				//новый размер списка с описанием секторов
				size=(sizeof(SectorInfo)*(sl->sector_counter+1) + (sl->sector[i].ByteAligment-1)) & ~(sl->sector[i].ByteAligment-1);
				total_size= size + ((sizeof(SectorList)-sizeof(SectorInfo*))+( sl->sector[i].ByteAligment - 1)) & ~(sl->sector[i].ByteAligment-1);

				if(sl->sector[i].FreeBytesRemaining < (total_size-prev_total_size))
					return ERR_NO_FREE_SPACE;
				
				si=(SectorInfo*)local_malloc( size );

				if(si==NULL)
					return ERR_LOCAL_MALLOC;

				memset((void*)si,0x00,size);
				memcpy((void*)si, (void*)sl->sector, (sizeof(SectorInfo) * sl->sector_counter));

				config->index=sl->sector_counter;

				sl->sector_counter++;
				local_free((void*)sl->sector);
				sl->sector=si;

				//скорректировать размер main сектора
				sl->sector[i].FreeBytesRemaining-=(total_size-prev_total_size);

				return sector_Insert(config);
			}
			else
			{
				return ERR_NO_FREE_SPACE;
			}
		}
	}

	return ERR_NO_MAIN;
}
Beispiel #19
0
/*-----------------------------------------------------------*/
UINT8_T prvInsertBlockIntoFreeList(UINT8_T index, BlockLink *pxBlockToInsert )
{
	BlockLink *pxIterator,*pxBlockToInsertBuf=NULL;
	UINT8_T err=ERR_OK;

	pxBlockToInsertBuf=(BlockLink*)local_malloc(sizeof(BlockLink));

	if(pxBlockToInsertBuf==NULL)
	{
		err=ERR_LOCAL_MALLOC;
		goto exit;
	}

	memset((void*)pxBlockToInsertBuf,0x00,sizeof(BlockLink));

	//чтение сектора xStart
	xStart->pxCurrentAddr=sl->sector[index].xStart_Addr;
	err=ReadBlockLink(index, xStart);
	if(err!=ERR_OK)
		goto exit;

	/* Iterate through the list until a block is found that has a higher address
	than the block being inserted. */
	pxIterator = xStart;

	while(pxIterator->body.pxNextFreeBlock < pxBlockToInsert->pxCurrentAddr)
	{
		pxIterator->pxCurrentAddr=pxIterator->body.pxNextFreeBlock;
		err=ReadBlockLink(index,pxIterator);
		if(err!=ERR_OK)
			goto exit;
	}

	/* Do the block being inserted, and the block it is being inserted after
	make a contiguous block of memory? */
	if( (pxIterator->pxCurrentAddr + pxIterator->body.xBlockSize ) == pxBlockToInsert->pxCurrentAddr )
	{
		pxIterator->body.xBlockSize+=pxBlockToInsert->body.xBlockSize;
		pxBlockToInsert = pxIterator;

		#if (configUSE_SegmentCounter==TRUE)
		sl->sector[index].xSegmentCounter--;
		#endif
	}

	/* Do the block being inserted, and the block it is being inserted before
	make a contiguous block of memory? */
	if((pxBlockToInsert->pxCurrentAddr+pxBlockToInsert->body.xBlockSize)==pxIterator->body.pxNextFreeBlock)
	{
		//if(pxIterator->body.pxNextFreeBlock!=pxEnd->pxCurrentAddr) //ошибка!
		if(pxIterator->body.pxNextFreeBlock!=sl->sector[index].pxEnd_Addr)
		{
			/* Form one big block from the two blocks. */
			pxBlockToInsertBuf->pxCurrentAddr=pxIterator->body.pxNextFreeBlock;
			err=ReadBlockLink(index,pxBlockToInsertBuf);
			if(err!=ERR_OK)
				goto exit;

			pxBlockToInsert->body.xBlockSize=pxBlockToInsertBuf->body.xBlockSize;
			pxBlockToInsert->body.pxNextFreeBlock=pxBlockToInsertBuf->body.pxNextFreeBlock;

			#if (configUSE_SegmentCounter==TRUE)
			sl->sector[index].xSegmentCounter--;
			#endif
		}
		else
		{
			pxBlockToInsert->body.pxNextFreeBlock=sl->sector[index].pxEnd_Addr;//pxEnd->pxCurrentAddr;
		}
	}
	else
	{
		pxBlockToInsert->body.pxNextFreeBlock=pxIterator->body.pxNextFreeBlock;
	}

	/* If the block being inserted plugged a gab, so was merged with the block
	before and the block after, then it's pxNextFreeBlock pointer will have
	already been set, and should not be set here as that would make it point
	to itself. */
	if( pxIterator->pxCurrentAddr != pxBlockToInsert->pxCurrentAddr )
	{
		pxIterator->body.pxNextFreeBlock = pxBlockToInsert->pxCurrentAddr;
		err=WriteBlockLink(index,pxIterator);
		if(err!=ERR_OK)
			goto exit;
	}

	err=WriteBlockLink(index,pxBlockToInsert);
	//if(err!=ERR_OK)
	//	goto exit;
exit:
	local_free((void*)pxBlockToInsertBuf);

	return ERR_OK;
}
Beispiel #20
0
/* gets the DNS records for name:type
 * returns a dyn. alloc'ed struct rdata linked list with the parsed responses
 * or 0 on error
 * see rfc1035 for the query/response format */
struct rdata* get_record(char* name, int type, int flags)
{
	int size;
	int skip;
	int qno, answers_no;
	int i, r;
	static union dns_query buff;
	unsigned char* p;
	unsigned char* end;
	unsigned char* rd_end;
	static char rec_name[MAX_DNS_NAME]; /* placeholder for the record name */
	int rec_name_len;
	unsigned short rtype, class, rdlength;
	unsigned int ttl;
	struct rdata* head;
	struct rdata** crt;
	struct rdata** last;
	struct rdata* rd;
	struct srv_rdata* srv_rd;
	struct srv_rdata* crt_srv;
	int search_list_used;
	int name_len;
	struct rdata* fullname_rd;
	char c;
	
#ifdef USE_DNSSEC
	val_status_t val_status;
#endif

	name_len=strlen(name);

	for (i = 0; i < name_len; i++) {
	    c = name[i];
	    if (((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')) ||
		((c >= '0') && (c <= '9')) || (name[i] == '.') ||
		(name[i] == '-') || (name[i] == '_'))
			continue;
	    LM_DBG("'%s' is not domain name\n", name);
	    return 0;
	}

	if (cfg_get(core, core_cfg, dns_search_list)==0) {
		search_list_used=0;
		name_len=0;
	} else {
		search_list_used=1;
	}
	fullname_rd=0;

#ifndef USE_DNSSEC
	size=res_search(name, C_IN, type, buff.buff, sizeof(buff));
#else
	size=val_res_query((val_context_t *) NULL,
                      (char *) name, 
                      (int) C_IN,
		      (int) type, 
                      (unsigned char *) buff.buff, 
		      (int) sizeof(buff),
                      &val_status);	
	if(!val_istrusted(val_status)){
		LOG(L_INFO, "INFO: got not trusted record when resolving %s\n",name);
	}
#endif

	if (unlikely(size<0)) {
		DBG("get_record: lookup(%s, %d) failed\n", name, type);
		goto not_found;
	}
	else if (unlikely(size > sizeof(buff))) size=sizeof(buff);
	head=rd=0;
	last=crt=&head;
	
	p=buff.buff+DNS_HDR_SIZE;
	end=buff.buff+size;
	if (unlikely(p>=end)) goto error_boundary;
	qno=ntohs((unsigned short)buff.hdr.qdcount);

	for (r=0; r<qno; r++){
		/* skip the name of the question */
		if (unlikely((p=dns_skipname(p, end))==0)) {
			LOG(L_ERR, "ERROR: get_record: skipname==0\n");
			goto error;
		}
		p+=2+2; /* skip QCODE & QCLASS */
	#if 0
		for (;(p<end && (*p)); p++);
		p+=1+2+2; /* skip the ending  '\0, QCODE and QCLASS */
	#endif
		if (unlikely(p>end)) {
			LOG(L_ERR, "ERROR: get_record: p>=end\n");
			goto error;
		}
	};
	answers_no=ntohs((unsigned short)buff.hdr.ancount);
again:
	for (r=0; (r<answers_no) && (p<end); r++){
#if 0
		/*  ignore it the default domain name */
		if ((p=dns_skipname(p, end))==0) {
			LOG(L_ERR, "ERROR: get_record: skip_name=0 (#2)\n");
			goto error;
		}
#else
		if (unlikely((skip=dn_expand(buff.buff, end, p, rec_name,
							MAX_DNS_NAME-1))==-1)){
			LOG(L_ERR, "ERROR: get_record: dn_expand(rec_name) failed\n");
			goto error;
		}
#endif
		p+=skip;
		rec_name_len=strlen(rec_name);
		if (unlikely(rec_name_len>255)){
			LOG(L_ERR, "ERROR: get_record: dn_expand(rec_name): name too"
					" long  (%d)\n", rec_name_len);
			goto error;
		}
		/* check if enough space is left for type, class, ttl & size */
		if (unlikely((p+2+2+4+2)>end)) goto error_boundary;
		/* get type */
		memcpy((void*) &rtype, (void*)p, 2);
		rtype=ntohs(rtype);
		p+=2;
		/* get  class */
		memcpy((void*) &class, (void*)p, 2);
		class=ntohs(class);
		p+=2;
		/* get ttl*/
		memcpy((void*) &ttl, (void*)p, 4);
		ttl=ntohl(ttl);
		p+=4;
		/* get size */
		memcpy((void*)&rdlength, (void*)p, 2);
		rdlength=ntohs(rdlength);
		p+=2;
		rd_end=p+rdlength;
		if (unlikely((rd_end)>end)) goto error_boundary;
		if ((flags & RES_ONLY_TYPE) && (rtype!=type)){
			/* skip */
			p=rd_end;
			continue;
		}
		/* expand the "type" record  (rdata)*/
		
		rd=(struct rdata*) local_malloc(sizeof(struct rdata)+rec_name_len+
										1-1);
		if (rd==0){
			LOG(L_ERR, "ERROR: get_record: out of memory\n");
			goto error;
		}
		rd->type=rtype;
		rd->pclass=class;
		rd->ttl=ttl;
		rd->next=0;
		memcpy(rd->name, rec_name, rec_name_len);
		rd->name[rec_name_len]=0;
		rd->name_len=rec_name_len;
		/* check if full name matches */
		if ((search_list_used==1)&&(fullname_rd==0)&&
				(rec_name_len>=name_len)&&
				(strncasecmp(rec_name, name, name_len)==0)) {
			/* now we have record whose name is the same (up-to the
			 * name_len with the searched one):
			 * if the length is the same - we found full match, no fake
			 *  cname needed, just clear the flag
			 * if the length of the name differs - it has matched using
			 *  search list remember the rd, so we can create fake CNAME
			 *  record when all answers are used and no better match found
			 */
			if (rec_name_len==name_len)
				search_list_used=0;
			/* this is safe.... here was rec_name_len > name_len */
			else if (rec_name[name_len]=='.') {
#ifdef HAVE_RESOLV_RES
				if ((cfg_get(core, core_cfg, dns_search_fmatch)==0) ||
						(match_search_list(&_res, rec_name+name_len+1)!=0))
#endif
					fullname_rd=rd;
			}
		}
		switch(rtype){
			case T_SRV:
				srv_rd= dns_srv_parser(buff.buff, end, rd_end, p);
				rd->rdata=(void*)srv_rd;
				if (unlikely(srv_rd==0)) goto error_parse;
				
				/* insert sorted into the list */
				for (crt=&head; *crt; crt= &((*crt)->next)){
					if ((*crt)->type!=T_SRV)
						continue;
					crt_srv=(struct srv_rdata*)(*crt)->rdata;
					if ((srv_rd->priority <  crt_srv->priority) ||
					   ( (srv_rd->priority == crt_srv->priority) && 
							 (srv_rd->weight > crt_srv->weight) ) ){
						/* insert here */
						goto skip;
					}
				}
				last=&(rd->next); /*end of for => this will be the last
									element*/
			skip:
				/* insert here */
				rd->next=*crt;
				*crt=rd;
				break;
			case T_A:
				rd->rdata=(void*) dns_a_parser(p, rd_end);
				if (unlikely(rd->rdata==0)) goto error_parse;
				*last=rd; /* last points to the last "next" or the list
							 	head*/
				last=&(rd->next);
				break;
			case T_AAAA:
				rd->rdata=(void*) dns_aaaa_parser(p, rd_end);
				if (unlikely(rd->rdata==0)) goto error_parse;
				*last=rd;
				last=&(rd->next);
				break;
			case T_CNAME:
				rd->rdata=(void*) dns_cname_parser(buff.buff, end, p);
				if(unlikely(rd->rdata==0)) goto error_parse;
				*last=rd;
				last=&(rd->next);
				break;
			case T_NAPTR:
				rd->rdata=(void*)dns_naptr_parser(buff.buff, end, rd_end, p);
				if(unlikely(rd->rdata==0)) goto error_parse;
				*last=rd;
				last=&(rd->next);
				break;
			case T_TXT:
				rd->rdata= dns_txt_parser(buff.buff, rd_end, p);
				if (rd->rdata==0) goto error_parse;
				*last=rd;
				last=&(rd->next);
				break;
			case T_EBL:
				rd->rdata= dns_ebl_parser(buff.buff, end, rd_end, p);
				if (rd->rdata==0) goto error_parse;
				*last=rd;
				last=&(rd->next);
				break;
			case T_PTR:
				rd->rdata=(void*) dns_ptr_parser(buff.buff, end, p);
				if(unlikely(rd->rdata==0)) goto error_parse;
				*last=rd;
				last=&(rd->next);
				break;
			default:
				LOG(L_ERR, "WARNING: get_record: unknown type %d\n", rtype);
				rd->rdata=0;
				*last=rd;
				last=&(rd->next);
		}
		
		p+=rdlength;
		
	}
	if (flags & RES_AR){
		flags&=~RES_AR;
		answers_no=ntohs((unsigned short)buff.hdr.nscount);
#ifdef RESOLVE_DBG
		DBG("get_record: skipping %d NS (p=%p, end=%p)\n", answers_no, p,
				end);
#endif
		for (r=0; (r<answers_no) && (p<end); r++){
			/* skip over the ns records */
			if ((p=dns_skipname(p, end))==0) {
				LOG(L_ERR, "ERROR: get_record: skip_name=0 (#3)\n");
				goto error;
			}
			/* check if enough space is left for type, class, ttl & size */
			if (unlikely((p+2+2+4+2)>end)) goto error_boundary;
			memcpy((void*)&rdlength, (void*)p+2+2+4, 2);
			p+=2+2+4+2+ntohs(rdlength);
		}
		answers_no=ntohs((unsigned short)buff.hdr.arcount);
#ifdef RESOLVE_DBG
		DBG("get_record: parsing %d ARs (p=%p, end=%p)\n", answers_no, p,
				end);
#endif
		goto again; /* add also the additional records */
	}

	/* if the name was expanded using DNS search list
	 * create fake CNAME record to convert the short name
	 * (queried) to long name (answered)
	 */
	if ((search_list_used==1)&&(fullname_rd!=0)) {
		rd=(struct rdata*) local_malloc(sizeof(struct rdata)+name_len+1-1);
		if (unlikely(rd==0)){
			LOG(L_ERR, "ERROR: get_record: out of memory\n");
			goto error;
		}
		rd->type=T_CNAME;
		rd->pclass=fullname_rd->pclass;
		rd->ttl=fullname_rd->ttl;
		rd->next=head;
		memcpy(rd->name, name, name_len);
		rd->name[name_len]=0;
		rd->name_len=name_len;
		/* alloc sizeof struct + space for the null terminated name */
		rd->rdata=(void*)local_malloc(sizeof(struct cname_rdata)-1+
										head->name_len+1);
		if(unlikely(rd->rdata==0)){
			LOG(L_ERR, "ERROR: get_record: out of memory\n");
			goto error_rd;
		}
		((struct cname_rdata*)(rd->rdata))->name_len=fullname_rd->name_len;
		memcpy(((struct cname_rdata*)(rd->rdata))->name, fullname_rd->name,
				fullname_rd->name_len);
		((struct cname_rdata*)(rd->rdata))->name[head->name_len]=0;
		head=rd;
	}

	return head;
error_boundary:
		LOG(L_ERR, "ERROR: get_record: end of query buff reached\n");
		if (head) free_rdata_list(head);
		return 0;
error_parse:
		LOG(L_ERR, "ERROR: get_record: rdata parse error (%s, %d), %p-%p"
						" rtype=%d, class=%d, ttl=%d, rdlength=%d \n",
				name, type,
				p, end, rtype, class, ttl, rdlength);
error_rd:
		if (rd) local_free(rd); /* rd->rdata=0 & rd is not linked yet into
								   the list */
error:
		LOG(L_ERR, "ERROR: get_record \n");
		if (head) free_rdata_list(head);
not_found:
	/* increment error counter */
	counter_inc(dns_cnts_h.failed_dns_req);
	return 0;
}
Beispiel #21
0
static int
add_menu_item (HMENU menu, widget_value *wv, HMENU item)
{
  UINT fuFlags;
  char *out_string, *p, *q;
  int return_value;
  size_t nlen, orig_len;
  USE_SAFE_ALLOCA;

  if (menu_separator_name_p (wv->name))
    {
      fuFlags = MF_SEPARATOR;
      out_string = NULL;
    }
  else
    {
      if (wv->enabled)
	fuFlags = MF_STRING;
      else
	fuFlags = MF_STRING | MF_GRAYED;

      if (wv->key != NULL)
	{
	  out_string = SAFE_ALLOCA (strlen (wv->name) + strlen (wv->key) + 2);
	  p = stpcpy (out_string, wv->name);
	  p = stpcpy (p, "\t");
	  strcpy (p, wv->key);
	}
      else
	out_string = (char *)wv->name;

      /* Quote any special characters within the menu item's text and
	 key binding.  */
      nlen = orig_len = strlen (out_string);
      if (unicode_append_menu)
        {
          /* With UTF-8, & cannot be part of a multibyte character.  */
          for (p = out_string; *p; p++)
            {
              if (*p == '&')
                nlen++;
            }
        }
#ifndef NTGUI_UNICODE
      else
        {
          /* If encoded with the system codepage, use multibyte string
             functions in case of multibyte characters that contain '&'.  */
          for (p = out_string; *p; p = _mbsinc (p))
            {
              if (_mbsnextc (p) == '&')
                nlen++;
            }
        }
#endif /* !NTGUI_UNICODE */

      if (nlen > orig_len)
        {
          p = out_string;
          out_string = SAFE_ALLOCA (nlen + 1);
          q = out_string;
          while (*p)
            {
              if (unicode_append_menu)
                {
                  if (*p == '&')
                    *q++ = *p;
                  *q++ = *p++;
                }
#ifndef NTGUI_UNICODE
              else
                {
                  if (_mbsnextc (p) == '&')
                    {
                      _mbsncpy (q, p, 1);
                      q = _mbsinc (q);
                    }
                  _mbsncpy (q, p, 1);
                  p = _mbsinc (p);
                  q = _mbsinc (q);
                }
#endif /* !NTGUI_UNICODE */
            }
          *q = '\0';
        }

      if (item != NULL)
	fuFlags = MF_POPUP;
      else if (wv->title || wv->call_data == 0)
	{
	  /* Only use MF_OWNERDRAW if GetMenuItemInfo is usable, since
	     we can't deallocate the memory otherwise.  */
	  if (get_menu_item_info)
	    {
              out_string = (char *) local_alloc (strlen (wv->name) + 1);
              strcpy (out_string, wv->name);
#ifdef MENU_DEBUG
	      DebPrint ("Menu: allocating %ld for owner-draw", out_string);
#endif
	      fuFlags = MF_OWNERDRAW | MF_DISABLED;
	    }
	  else
	    fuFlags = MF_DISABLED;
	}

      /* Draw radio buttons and tickboxes. */
      else if (wv->selected && (wv->button_type == BUTTON_TYPE_TOGGLE ||
				wv->button_type == BUTTON_TYPE_RADIO))
	fuFlags |= MF_CHECKED;
      else
	fuFlags |= MF_UNCHECKED;
    }

  if (unicode_append_menu && out_string)
    {
      /* Convert out_string from UTF-8 to UTF-16-LE.  */
      int utf8_len = strlen (out_string);
      WCHAR * utf16_string;
      if (fuFlags & MF_OWNERDRAW)
	utf16_string = local_alloc ((utf8_len + 1) * sizeof (WCHAR));
      else
	utf16_string = SAFE_ALLOCA ((utf8_len + 1) * sizeof (WCHAR));

      utf8to16 ((unsigned char *)out_string, utf8_len, utf16_string);
      return_value = unicode_append_menu (menu, fuFlags,
					  item != NULL ? (UINT_PTR) item
					    : (UINT_PTR) wv->call_data,
					  utf16_string);

#ifndef NTGUI_UNICODE /* Fallback does not apply when always UNICODE */
      if (!return_value)
	{
	  /* On W9x/ME, Unicode menus are not supported, though AppendMenuW
	     apparently does exist at least in some cases and appears to be
	     stubbed out to do nothing.  out_string is UTF-8, but since
	     our standard menus are in English and this is only going to
	     happen the first time a menu is used, the encoding is
	     of minor importance compared with menus not working at all.  */
	  return_value =
	    AppendMenu (menu, fuFlags,
			item != NULL ? (UINT_PTR) item: (UINT_PTR) wv->call_data,
			out_string);
	  /* Don't use Unicode menus in future, unless this is Windows
	     NT or later, where a failure of AppendMenuW does NOT mean
	     Unicode menus are unsupported.  */
	  if (osinfo_cache.dwPlatformId != VER_PLATFORM_WIN32_NT)
	    unicode_append_menu = NULL;
	}
#endif /* NTGUI_UNICODE */

      if (unicode_append_menu && (fuFlags & MF_OWNERDRAW))
	local_free (out_string);
    }
  else
    {
      return_value =
	AppendMenu (menu,
		    fuFlags,
		    item != NULL ? (UINT_PTR) item : (UINT_PTR) wv->call_data,
		    out_string );
    }

  /* This must be done after the menu item is created.  */
  if (!wv->title && wv->call_data != 0)
    {
      if (set_menu_item_info)
	{
	  MENUITEMINFO info;
	  memset (&info, 0, sizeof (info));
	  info.cbSize = sizeof (info);
	  info.fMask = MIIM_DATA;

	  /* Set help string for menu item.  Leave it as a pointer to
	     a Lisp_String until it is ready to be displayed, since GC
	     can happen while menus are active.  */
	  if (!NILP (wv->help))
	    {
	      /* We use XUNTAG below because in a 32-bit build
		 --with-wide-int we cannot pass a Lisp_Object
		 via a DWORD member of MENUITEMINFO.  */
	      /* As of Jul-2012, w32api headers say that dwItemData
		 has DWORD type, but that's a bug: it should actually
		 be ULONG_PTR, which is correct for 32-bit and 64-bit
		 Windows alike.  MSVC headers get it right; hopefully,
		 MinGW headers will, too.  */
	      eassert (STRINGP (wv->help));
	      info.dwItemData = (ULONG_PTR) XUNTAG (wv->help, Lisp_String);
	    }
	  if (wv->button_type == BUTTON_TYPE_RADIO)
	    {
	      /* CheckMenuRadioItem allows us to differentiate TOGGLE and
		 RADIO items, but is not available on NT 3.51 and earlier.  */
	      info.fMask |= MIIM_TYPE | MIIM_STATE;
	      info.fType = MFT_RADIOCHECK | MFT_STRING;
	      info.dwTypeData = out_string;
	      info.fState = wv->selected ? MFS_CHECKED : MFS_UNCHECKED;
	    }

	  set_menu_item_info (menu,
			      item != NULL ? (UINT_PTR) item : (UINT_PTR) wv->call_data,
			      FALSE, &info);
	}
    }
  SAFE_FREE ();
  return return_value;
}
Beispiel #22
0
void OSC_Base::freeData(osc_message* message) {
  if (message->data) local_free(message->data);
}
Beispiel #23
0
size_t OSC_Base::_Send(const char *path, const char *typestring, va_list ap) {
  const char *typestring_beginning = typestring;
  char *buffer; 
  data_holder_type data_holder;
  uint32_t i;
  int position = 0;
  uint8_t *src, *dst;
  uint32_t total_length;
  va_list ap_size;

  for (i = 0; i < MAX_PATH_LENGTH && path[i]; i++);
  total_length = (i+4)&(~3);
  for (i = 0; i < MAX_TYPESTRING_LENGTH && typestring[i]; i++);
  total_length += (i+4)&(~3);

  // find the size of all the data members (and count the length of the typestring)
  va_copy(ap_size, ap); // we need to use va_args twice
  for (i = 0; i < MAX_TYPESTRING_LENGTH && typestring[i]; i++) {
    // get the first character out of the way
    switch (typestring[i]) {
    case 'c': // TYPE_CHARACTER; 
    case 'i': // TYPE_INTEGER;
    case 'm': // TYPE_MIDI;
    case 'r': // TYPE_RGB;
      total_length+=4; data_holder.value_uint32 = va_arg(ap_size, uint32_t); break;
    case 'f': // TYPE_FLOAT;
      total_length+=4; data_holder.value_float  = (float)va_arg(ap_size, double); break; 

    case 'd': // TYPE_DOUBLE;
    case 'h': // TYPE_LONG;
    case 't': // TYPE_TIMETAG;
      total_length+=8; data_holder.value_sint64 = va_arg(ap_size, sint64_t); break;

    case 's':
      data_holder.value_string = va_arg(ap_size, char*);
      data_holder.length = strlen(data_holder.value_string);
      total_length += (data_holder.length+3)&(~3);
      break; // TYPE_STRING;
      
    case 'b': // TYPE_BLOB
    case 'B':
      data_holder.length       = va_arg(ap_size, uint32_t);
      data_holder.value_blob   = va_arg(ap_size, uint8_t*);
      total_length += 4 + (data_holder.length+3)&(~3);
      break; // TYPE_BLOB with the advanced length field

    }
  }
  va_end(ap_size);
  //total_length += (i+3)&(~3);

  buffer = (char*)local_tx_malloc(total_length);
  if (!buffer) return 0;
  memset(buffer, 0x00, total_length);
  
  memset(&data_holder, 0x00, sizeof(data_holder_type));

  // name
  for (; position < total_length &&       *path != 0x00; position++) buffer[position] = *path++;
  buffer[position++] = 0x00;
  for (; (position & 0x03) != 0x00; position++) buffer[position] = 0x00;

  // typestring
  for (; position < total_length && *typestring != 0x00; position++) buffer[position] = *typestring++;
  buffer[position++] = 0x00;
  for (; (position & 0x03) != 0x00; position++) buffer[position] = 0x00;

  // now onto the data
  typestring = typestring_beginning;

  
  for (;position < total_length && *typestring != 0x00; typestring++) {
    // get the first character out of the way
    switch (*typestring) {
    case 'c': data_holder.value_uint32 = va_arg(ap, uint32_t);      break; // TYPE_CHARACTER; 
    case 'i': data_holder.value_sint32 = va_arg(ap, int);           break; // TYPE_INTEGER;
    case 'd': data_holder.value_double = va_arg(ap, double);        break; // TYPE_DOUBLE;
    case 'f': data_holder.value_float  = (float)va_arg(ap, double); break; // TYPE_FLOAT;
    case 'm': data_holder.value_uint32 = va_arg(ap, uint32_t);      break; // TYPE_MIDI;
    case 'r': data_holder.value_uint32 = va_arg(ap, uint32_t);      break; // TYPE_COLOR;

      // please note, these are work-arounds to an issue where va_arg requires 64-bit variables
      // to be located on 8-byte boundaries while the stack pushes 64bit variables without this requirement.
    case 'h': data_holder.value_sint64 = va_arg(ap, sint64_t); break;
    case 't': data_holder.value_sint64 = va_arg(ap, sint64_t); break;

    case 's': data_holder.value_string = va_arg(ap, char*);    break; // TYPE_STRING;
    case 'b': data_holder.length       = va_arg(ap, uint32_t);
              data_holder.value_blob   = va_arg(ap, uint8_t*); break; // TYPE_BLOB
    case 'B': data_holder.length       = va_arg(ap, uint32_t);
              data_holder.value_blob   = va_arg(ap, uint8_t*);
              data_holder.crc16        = 0;// TODO: crc16(0, data_holder.value_blob, (data_holder.shortlength << 2)); <<<<============================================== THIS NEEDS FIXING
              data_holder.marker       = 1; break; // TYPE_BLOB with the advanced length field
    case 'F': break; // TYPE_FALSE;
    case 'I': break; // TYPE_IMPULSE;
    case 'N': break; // TYPE_NONE;
    case 'T': break; // TYPE_TRUE;
    case ',': break;
    }


    // now copy the data into place
    switch (*typestring) {
    case 'c': // TYPE_CHARACTER;
    case 'f': // TYPE_FLOAT;
    case 'i': // TYPE_INTEGER;
    case 'm': // TYPE_MIDI;
    case 'r': // TYPE_RGB;
      //*(uint32_t*)(&buffer[position]) = data_holder.value_uint32;
      *(uint8_t*)(&buffer[position++]) = data_holder.value_bytes[3];
      *(uint8_t*)(&buffer[position++]) = data_holder.value_bytes[2];
      *(uint8_t*)(&buffer[position++]) = data_holder.value_bytes[1];
      *(uint8_t*)(&buffer[position++]) = data_holder.value_bytes[0];
      //position += 4;
      break;

    case 'd': // TYPE_DOUBLE;
    case 'h': // TYPE_LONG;
    case 't': // TYPE_TIMETAG;
      *(uint8_t*)(&buffer[position++]) = data_holder.value_bytes[7];
      *(uint8_t*)(&buffer[position++]) = data_holder.value_bytes[6];
      *(uint8_t*)(&buffer[position++]) = data_holder.value_bytes[5];
      *(uint8_t*)(&buffer[position++]) = data_holder.value_bytes[4];
      *(uint8_t*)(&buffer[position++]) = data_holder.value_bytes[3];
      *(uint8_t*)(&buffer[position++]) = data_holder.value_bytes[2];
      *(uint8_t*)(&buffer[position++]) = data_holder.value_bytes[1];
      *(uint8_t*)(&buffer[position++]) = data_holder.value_bytes[0];
      //*(uint64_t*)(&buffer[position]) = data_holder.value_uint64;
      //position += 8;
      break;

    case 'b': // TYPE_BLOB;
      position += 4;
      src = (uint8_t*)data_holder.value_blob;
      dst = (uint8_t*)&buffer[position];
      for (i = 0; i < (data_holder.length+3)>>2; i++) {
        dst[0] = src[3];
        dst[1] = src[2];
        dst[2] = src[1];
        dst[3] = src[0];
        dst+=4; src+=4;
      }
      position += (data_holder.length+3) & (~3);
      break;

    case 'B': // TYPE_BLOB;
      position += 4;
      src = (uint8_t*)data_holder.value_blob;
      dst = (uint8_t*)&buffer[position];
      for (i = 0; i < data_holder.shortlength; i++) {
        dst[0] = src[3];
        dst[1] = src[2];
        dst[2] = src[1];
        dst[3] = src[0];
        dst+=4; src+=4;
      }
      position += (data_holder.length+3) & (~3);
      break;

    case 's': // TYPE_STRING;    
      data_holder.length = 0;
      for (;position < total_length && *data_holder.value_blob;) {
        buffer[position++] = *data_holder.value_blob++;
        data_holder.length++;
      }
      buffer[position++] = 0x00;
      for (; position < total_length && (position & 0x03) != 0x00; position++) buffer[position] = 0x00;
      break;

    default:
      break;
    }
  }

  _tx(buffer, total_length);
  local_free(buffer);
  return total_length;
}
Beispiel #24
0
/*-----------------------------------------------------------*/
UINT8_T	sector_Malloc(UINT8_T index,UINT32_T *addr, UINT32_T xWantedSize )
{
	BlockLink *pxBlock=NULL, *pxNewBlockLink=NULL, *pxPreviousBlock=NULL;
	UINT8_T err=ERR_OK;

	if(sl==NULL)
	{
		err=ERR_SL_NULL;
		goto exit;
	}

	pxBlock=(BlockLink*)local_malloc(sizeof(BlockLink));
	pxNewBlockLink=(BlockLink*)local_malloc(sizeof(BlockLink));

	if( (pxBlock==NULL) || (pxNewBlockLink==NULL))
	{
		err=ERR_LOCAL_MALLOC;
		goto exit;
	}

	memset((void*)pxBlock,0x00,sizeof(BlockLink));
	memset((void*)pxNewBlockLink,0x00,sizeof(BlockLink));

	/* The wanted size is increased so it can contain a BlockLink_t
	structure in addition to the requested amount of bytes. */
	if(xWantedSize==0)
	{
		err=ERR_WRONG_SIZE;
		goto exit;
	}

	xWantedSize += sl->sector[index].bl_size;

	/* Ensure that blocks are always aligned to the required number
	of bytes. */
	if((xWantedSize & (sl->sector[index].ByteAligment-1)) !=0x00)
	{
		/* Byte alignment required. */
		xWantedSize += (sl->sector[index].ByteAligment - (xWantedSize & (sl->sector[index].ByteAligment-1)));
	}

	if(xWantedSize > sl->sector[index].FreeBytesRemaining)
	{
		err=ERR_NO_FREE_SPACE;
		goto exit;
	}

	/* Traverse the list from the start	(lowest address) block until
	one	of adequate size is found. */
	//чтение сектора xStart
	xStart->pxCurrentAddr=sl->sector[index].xStart_Addr;
	err=ReadBlockLink(index, xStart);

	if(err!=ERR_OK)
		goto exit;

	pxPreviousBlock = xStart;

	pxBlock->pxCurrentAddr=xStart->body.pxNextFreeBlock;
	err=ReadBlockLink(index,pxBlock);

	if(err!=ERR_OK)
		goto exit;

	while( (pxBlock->body.xBlockSize < xWantedSize) && (pxBlock->body.pxNextFreeBlock !=0) )
	{
		pxPreviousBlock=pxBlock; //скопировать а не ссылка!

		pxBlock->pxCurrentAddr=pxBlock->body.pxNextFreeBlock;
		err=ReadBlockLink(index,pxBlock);

		if(err!=ERR_OK)
			goto exit;
	}

	/* If the end marker was reached then a block of adequate size
	was	not found. */
	if(pxBlock->pxCurrentAddr != sl->sector[index].pxEnd_Addr)
	{
		/* Return the memory space pointed to - jumping over the
		BlockLink_t structure at its start. */
		*addr=pxPreviousBlock->body.pxNextFreeBlock + sl->sector[index].bl_size;

		/* This block is being returned for use so must be taken out
		of the list of free blocks. */
		if(pxPreviousBlock->pxCurrentAddr != pxBlock->pxCurrentAddr)
		{
			pxPreviousBlock->body.pxNextFreeBlock=pxBlock->body.pxNextFreeBlock;

			err=WriteBlockLink(index,pxPreviousBlock);
			if(err!=ERR_OK)
				goto exit;
		}

		/* If the block is larger than required it can be split into
		two. */
		if( (pxBlock->body.xBlockSize - xWantedSize) > (UINT32_T)(sl->sector[index].bl_size<<1) )
		{
			/* This block is to be split into two.  Create a new
			block following the number of bytes requested. The void
			cast is used to prevent byte alignment warnings from the
			compiler. */
			pxNewBlockLink->pxCurrentAddr = pxBlock->pxCurrentAddr + xWantedSize;

			/* Calculate the sizes of two blocks split from the
			single block. */
			pxNewBlockLink->body.xBlockSize=pxBlock->body.xBlockSize - xWantedSize;
			pxNewBlockLink->body.pxNextFreeBlock=0;

			pxBlock->body.xBlockSize=xWantedSize;

			/* Insert the new block into the list of free blocks. */
			err=prvInsertBlockIntoFreeList(index , pxNewBlockLink);

			if(err!=ERR_OK)
				goto exit;

			#if (configUSE_SegmentCounter==TRUE)
			sl->sector[index].xSegmentCounter++;
			#endif
		}

		sl->sector[index].FreeBytesRemaining -= pxBlock->body.xBlockSize;

		/* The block is being returned - it is allocated and owned
		by the application and has no "next" block. */
		pxBlock->body.xBlockSize |= (UINT32_T)(0x01 << ((8*sl->sector[index].SectorSizeLen)-1));
		pxBlock->body.pxNextFreeBlock = 0;
		err=WriteBlockLink(index,pxBlock);
		//if(err!=ERR_OK)
		//	goto exit;
	}

exit:

	local_free((void*)pxBlock);
	local_free((void*)pxNewBlockLink);

	return err;
}