示例#1
0
void iter()
{
    int i;
    int j;
    unsigned long k;

    mem = (unsigned long **) pmalloc(BUF_SIZE * sizeof(unsigned long *));
    for (i=0; i < BUF_SIZE; ++i) {
        mem[i] = (unsigned long *) pmalloc(BUF_SIZE * sizeof(unsigned long));
        for (j=0; j < BUF_SIZE; ++j) {
            mem[i][j] = i * j;
        }
    }

    k = 0;
    while(1) {
        for (i=0; i < BUF_SIZE; ++i) {
            __asm__ __volatile__("");
            for (j=0; j < BUF_SIZE; ++j) {
                k += mem[j][i] + i*j;
                mem[j][i] = k;
            }
        }
//		usleep(1000);
    }

    for (i=0; i < BUF_SIZE; ++i) {
        pfree(mem[i], BUF_SIZE * sizeof(unsigned long));
    }
    pfree(mem, BUF_SIZE * sizeof(unsigned long *));
}
示例#2
0
int readconfig()
{
    FILE *handle;
    struct stringarray *wconf;
    char inistring[400];
    char *pt;
    pcontext;
    wconf=conf;
    if(conf!=NULL) resetconfig();
    handle=fopen(configfile,"r");
    if(handle==NULL) return -1;
    while(fgets(inistring,sizeof(inistring),handle))
    {
	pt=strchr(inistring,'\n');
	if(pt!=NULL) *pt=0;
	if(wconf==NULL)
	{
	    wconf=(struct stringarray *)pmalloc(sizeof(struct stringarray));
	    conf=wconf;
	} else {
	    wconf->next=(struct stringarray *)pmalloc(sizeof(struct stringarray));
	    wconf=wconf->next;
	}
	wconf->entry=(char *)pmalloc(strlen(inistring)+1);
	strcpy(wconf->entry,inistring);
    }
    fclose(handle);
    return 0x0;
}
示例#3
0
文件: jid.c 项目: named-data/muc
/**
 * nameprep the domain identifier in a JID and check if it is valid
 *
 * @param jid data structure holding the JID
 * @return 0 if JID is valid, non zero otherwise
 */
static int _jid_safe_domain(jid id) {
    int result=0;

    /* there must be a domain identifier */
    if (id->server == NULL || *id->server == '\0')
	return 1;

    /* nameprep the domain identifier */
    result = _jid_cached_stringprep(id->server, strlen(id->server)+1, _jid_prep_cache_domain);
    if (result == STRINGPREP_TOO_SMALL_BUFFER) {
	/* nameprep wants to expand the string, e.g. conversion from &szlig; to ss */
	size_t biggerbuffersize = 1024;
	char *biggerbuffer = pmalloc(id->p, biggerbuffersize);
	if (biggerbuffer == NULL)
	    return 1;
	strcpy(biggerbuffer, id->server);
	result = _jid_cached_stringprep(biggerbuffer, biggerbuffersize, _jid_prep_cache_domain);
	id->server = biggerbuffer;
    }
    if (result != STRINGPREP_OK)
	return 1;

    /* the namepreped domain must not be longer than 1023 bytes */
    if (j_strlen(id->server) > 1023)
	return 1;

    /* if nothing failed, the domain is valid */
    return 0;
}
示例#4
0
文件: conference_room.c 项目: bcy/muc
/* call strescape on a string and xhtmlize the string (\n-><br/>) */
char * _con_room_xhtml_strescape(pool p, char * buf) {
  char *result,*temp;
  int i,j;
  int oldlen, newlen;
  result = strescape(p, buf);
  oldlen = newlen=strlen(result);
  for (i=0; i<oldlen;i++) {
    if (result[i]=='\n') {
      newlen += 6;
    }
  }

  if(oldlen == newlen) return result;

  temp = pmalloc(p,newlen+1);
  if (temp == NULL) return NULL;

  for (i=j=0;i<oldlen;i++) {
    if (result[i]== '\n') {
      memcpy(&temp[j], "<br />", 6);
      j+=6;
    }
    else temp[j++] = result[i];
  }
  temp[j]='\0';
  return temp;
}
示例#5
0
char* strpcpy(char **dest,char *src)
{
	pmalloc(strlen(src)+1,(void**)dest);
	memcpy(*dest,src,strlen(src)+1);
	dest[strlen(src)] = '\0';
	return *dest;
}
示例#6
0
文件: xmlnode.c 项目: bcy/muc
static void _xmlnode_merge(xmlnode data)
{
    xmlnode cur;
    char *merge, *scur;
    int imerge;

    /* get total size of all merged cdata */
    imerge = 0;
    for(cur = data; cur != NULL && cur->type == NTYPE_CDATA; cur = cur->next)
        imerge += cur->data_sz;

    /* copy in current data and then spin through all of them and merge */
    scur = merge = pmalloc(data->p,imerge + 1);
    for(cur = data; cur != NULL && cur->type == NTYPE_CDATA; cur = cur->next)
    {
        memcpy(scur,cur->data,cur->data_sz);
        scur += cur->data_sz;
    }
    *scur = '\0';

    /* this effectively hides all of the merged-in chunks */
    data->next = cur;
    if(cur == NULL)
        data->parent->lastchild = data;
    else
        cur->prev = data;

    /* reset data */
    data->data = merge;
    data->data_sz = imerge;
    
}
示例#7
0
//  TODO make it work for undo log restore on failure too.
//  Note, a log can grow beyond this size with a pointer to a continuation log, but this is TODO.
//  Initialize a log with an existing memory location.  Note this location is not freed on exit.
//  If ptr is null, then the log is created and freed on exit.
WrapLogger::WrapLogger(int size, char *ptr) : _log(ptr)
{
	m_dofree = false;
	if (_log == NULL)
	{
		_log = (char *)pmalloc(size);
		m_dofree = true;
	}
	assert(_log != NULL);
	//printf("malloc log %p of size %d.\n", _log, size);
	_currentLocation = _log;

	//  Initialize the log in SCM.
	//  Size.
	_sizeOfLog = size;
	ntstore(_currentLocation, &_sizeOfLog, 4);
	_currentLocation += 4;

	//  Continuation pointer.
	_continuationLog = NULL;
	ntstore(_currentLocation, &_continuationLog, 8);
	_currentLocation += 8;

	_wrapId = 0;
	ntstore(_currentLocation, &_wrapId, 4);
	_currentLocation += 4;

	_flagEntry = 0;
	ntstore(_currentLocation, &_flagEntry, 4);
	_currentLocation += 4;

	//  TODO make sure not necessary!!!
	//p_msync();
}
示例#8
0
文件: png.c 项目: xach/wfcomp
data_source *make_png (char *filename,
                       unsigned long target_start,
                       unsigned long start,
                       unsigned long end)
{
    data_source *source;
    png_source_data *data;

    source = make_data_source();
    data = (png_source_data *)pmalloc(sizeof(png_source_data));

    data->filename = filename;

    source->data = data;
    source->target_start = target_start;
    source->start = start;
    source->end = end;

    source->initialize_fun = png_initialize;
    source->draw_fun = png_draw;

    /* FIXME: Really deinitialize eventually */
    source->deinitialize_fun = no_op;

    return source;
}
示例#9
0
文件: jid.c 项目: named-data/muc
/**
 * resourceprep the resource identifier in a JID and check if it is valid
 *
 * @param jid data structure holding the JID
 * @return 0 if JID is valid, non zero otherwise
 */
static int _jid_safe_resource(jid id) {
    int result=0;

    /* it is valid to have no resource identifier in the JID */
    if (id->resource == NULL)
	return 0;

    /* resource prep the resource identifier */
    result = _jid_cached_stringprep(id->resource, strlen(id->resource)+1, _jid_prep_cache_resource);
    if (result == STRINGPREP_TOO_SMALL_BUFFER) {
	/* resourceprep wants to expand the string, e.g. conversion from &szlig; to ss */
	size_t biggerbuffersize = 1024;
	char *biggerbuffer = pmalloc(id->p, biggerbuffersize);
	if (biggerbuffer == NULL)
	    return 1;
	strcpy(biggerbuffer, id->resource);
	result = _jid_cached_stringprep(id->resource, strlen(id->resource)+1, _jid_prep_cache_resource);
	id->resource = biggerbuffer;
    }
    if (result != STRINGPREP_OK)
	return 1;

    /* the resourcepreped resource must not be longer than 1023 bytes */
    if (j_strlen(id->resource) > 1023)
	return 1;

    /* check if resource was zeroed by stringprep */
    if (*id->resource == '\0')
	id->resource = NULL;

    /* if nothing failed, the resource is valid */
    return 0;

}
示例#10
0
文件: pool.c 项目: bcy/muc
char *pstrdup(pool p, const char *src) {
  char *dest;

  if (src == NULL)
     return NULL;

  dest = pmalloc(p, (strlen(src) + 1));
  memcpy(dest, src, strlen(src));
  return dest;
}
示例#11
0
static void
test_malloc_free_loop(size_t size)
{
	int err;
	for (int i = 0; i < MAX_MALLOC_FREE_LOOP; ++i) {
		err = pmalloc(mock_pop, &addr->ptr, size);
		UT_ASSERTeq(err, 0);
		pfree(mock_pop, &addr->ptr);
	}
}
示例#12
0
ReAst *ast_new(int type, int c, ReAst *lhs, ReAst *rhs)
{
    ReAst *ast = pmalloc(sizeof(ReAst));
    ast->type = type;
    ast->c = c;
    ast->nongreedy = 0;
    ast->lhs = lhs;
    ast->rhs = rhs;
    return ast;
}
示例#13
0
文件: str.c 项目: named-data/muc
spool spool_new(pool p)
{
    spool s;

    s = pmalloc(p, sizeof(struct spool_struct));
    s->p = p;
    s->len = 0;
    s->last = NULL;
    s->first = NULL;
    return s;
}
示例#14
0
static char* memdup(struct pool* p,const char* x,const char* end) {
  char* y=0;
  if (x<end) {
    y=pmalloc(p,end-x+1);
    if (y) {
      memcpy(y,x,end-x);
      y[end-x]=0;
    }
  }
  return y;
}
示例#15
0
文件: mem_pool.c 项目: skoo87/Xpipe
void *pcalloc(mem_pool_t *pool, size_t size)
{
    void *p;

    p = pmalloc(pool, size);
    if (p) {
        memset(p, 0, size);
    }

    return p;
}
示例#16
0
InPng *open_png (char *filename)
{
    InPng *in_png;
    FILE *fh;

    in_png = pmalloc(sizeof(InPng));

    if (!(fh = fopen(filename, "rb"))) {
	fail("could not open png input file");
    }

    in_png->png = png_create_read_struct(PNG_LIBPNG_VER_STRING,
					 NULL, NULL, NULL);
    
    if (!in_png->png) {
	fail("png_create_read_struct failure");
    }

    in_png->info = png_create_info_struct(in_png->png);

    if (!in_png->info) {
	fail("png_create_info_struct failed");
    }

    png_init_io(in_png->png, fh);

    png_read_info(in_png->png, in_png->info);

    if (in_png->info->color_type != PNG_COLOR_TYPE_RGB_ALPHA) {
	fail("only RGBA pngs are supported");
    }

    if (in_png->info->bit_depth != 8) {
	fail("only 8-bit pngs are supported");
    }

    in_png->rowstride = in_png->info->width * 4;
    in_png->row = (png_bytep )pmalloc(in_png->rowstride * sizeof(png_byte));

    return in_png;
}
示例#17
0
void blockCow(int *data, int nwraps, int wrapsize, int arraySize)
{
	int i, j, index;
	//int delay = getDelay();
	arraySize = 20000;
	printf("IntsPerLine= %d  \tIntsPerBlock= %d  \tarraySize= %d\n", IntsPerLine, IntsPerBlock, arraySize);

	shadowPaging = (int *)pmalloc(arraySize * sizeof(int));
	long ns = getNsTime();
	for (i=0; i < nwraps; i+=1)
	{
		//printf("+");

		/*
		//  Copy old values.
		//  64 bytes per cache line * 16 cache lines
		//  16 ints per cache line (64 bytes) * 16 cache lines = IntsPerBlock ints (BlockSize bytes)
		for (k = 0; k < 16; k++)
		{
			ntstore(blockCowLog + (i*IntsPerBlock) + (k*16), data + (i*IntsPerBlock) + (k*16), 64);
			//if (k != 15)
				//doDelay(delay);
		}
		p_msync();

		for (j = 0; j < wrapsize; j++)
		{
			index = (i * IntsPerBlock) + ((IntsPerBlock * j) / wrapsize);
			ntstore(data + index, &j, sizeof(int));
		}
		*/

		for (j = 0; j < IntsPerBlock; j++)
		{
			index = (i * IntsPerBlock) + j;
			index %= arraySize;
			if ((j+1) % (IntsPerBlock / wrapsize) == 0)
			{
				//printf("j=%d\n", j);
				//  assign new data.
				data[index] = 124;
			}
			ntstore(shadowPaging + index, data+index, sizeof(int));
		}
		p_msync();
		// sim pointer flip.
		ntstore(shadowPaging, data, sizeof(int));
		p_msync();
	}
	printf("\n");
	ns = getNsTime() - ns;
	printf("BlockCopyTime= %ld \tAverageBlockCopy= %ld\n", ns, ns/nwraps);
}
示例#18
0
文件: jpacket.c 项目: bcy/muc
jpacket jpacket_new(xmlnode x)
{
    jpacket p;

    if(x == NULL)
        return NULL;

    p = pmalloc(xmlnode_pool(x),sizeof(_jpacket));
    p->x = x;

    return jpacket_reset(p);
}
示例#19
0
文件: p_memory.c 项目: gandaro/piebnc
struct newpeert *newpeer(int usern)
{
    static struct peernodes *th,*thold;
    if(_lastpeer==usern && P_CREATE!=1) return _npeer;
    _lastpeer=usern;
    if (dummypeer==NULL) {
        dummypeer=(struct newpeert *)pmalloc(sizeof(struct newpeert));
    }
    th=peernode;
    while(th!=NULL)
    {
        if (th->uid==usern)
        {
            if (th->peer==NULL) {
                th->peer=(struct newpeert *) pmalloc(sizeof(struct newpeert));
            }
            thispeer=th;
            _npeer=th->peer;
            return th->peer;
        }
        thold=th;
        th=th->next;
    }
    if (P_CREATE==1)
    {
        P_CREATE=0; /* resetting this */
        thold->next=(struct peernodes *) pmalloc(sizeof(struct peernodes));
        th=thold->next;
        th->peer=(struct newpeert *) pmalloc(sizeof(struct newpeert));
        th->uid=usern;
        th->next=NULL;
        thispeer=th;
        _npeer=th->peer;
        return th->peer;
    } else {
        thispeer=NULL;
        _npeer=dummypeer;
        return dummypeer; /* yes, sure. Get it */
    }
}
示例#20
0
文件: p_memory.c 项目: gandaro/piebnc
struct datalinkt *datalink(int usern)
{
    static struct linknodes *th,*thold;
    if(_lastlink==usern && D_CREATE!=1) return _nlink;
    _lastlink=usern;
    if (dummylink==NULL) {
        dummylink=(struct datalinkt *) pmalloc(sizeof(struct datalinkt));
        memset(dummylink,0x0,sizeof(struct datalinkt));
    }
    th=linknode;
    while(th!=NULL)
    {
        if (th->uid==usern)
        {
            if (th->link==NULL)
                th->link=(struct datalinkt *) pmalloc(sizeof(struct datalinkt));
            thislink=th;
            _nlink=th->link;
            return th->link;
        }
        thold=th;
        th=th->next;
    }
    if (D_CREATE==1)
    {
        D_CREATE=0; /* resetting this */
        thold->next=(struct linknodes *) pmalloc(sizeof(struct linknodes));
        th=thold->next;
        th->link=(struct datalinkt *) pmalloc(sizeof(struct datalinkt));
        th->uid=usern;
        th->next=NULL;
        thislink=th;
        _nlink=th->link;
        return th->link;
    } else {
        thislink=NULL;
        _nlink=dummylink;
        return dummylink; /* yes, sure. Get it */
    }
}
示例#21
0
int addtopology(char *from, char *to)
{
    struct topologyt *top,*a,*b;
    int i;
    int litype;
    if(topology==NULL)
    {
        topology=(struct topologyt *)pmalloc(sizeof(struct topologyt));
        strmncpy(topology->server,me,sizeof(topology->server));
        topology->linktype=TP_ROOT;
    }
    a=gettopology(from);
    b=gettopology(to);
    if(a!=NULL && b!=NULL) return 0x0;
    if(a==NULL && b==NULL) return 0x0;
    if(a==NULL) 
    {
        top=b;
        litype=TP_LFROM;
    } else {
        top=a;
        litype=TP_LTO;
    }
    for(i=0;i<100;i++)
    {
        if(top->linked[i]==NULL)
        {
            top->linked[i]=(struct topologyt *)pmalloc(sizeof(struct topologyt));
            top=top->linked[i];
            top->linktype=litype;
            if(litype==TP_LFROM)
            strmncpy(top->server,from,sizeof(top->server));
            else
            strmncpy(top->server,to,sizeof(top->server));
            return 0x0;
        }
    }
    return 0x0;
}
示例#22
0
文件: pool.c 项目: aguivip/flaswf
char *pstrndup(pool_t p, const char *src, int len)
{
	char *dst;

	if (src == NULL) 
		return NULL;

	dst = (char *) pmalloc(p, len + 1);
	memcpy(dst, src, len);
	dst[len] = '\0';

	return dst;
}
示例#23
0
文件: pool.c 项目: aguivip/flaswf
char *pstrdup(pool_t p, const char *src)
{
	char *dst;
	int len;

	if (src == NULL) 
		return NULL;

	len = strlen(src) + 1;
	dst = (char *) pmalloc(p, len);
	memcpy(dst, src, len);
	return dst;
}
示例#24
0
static void
test_realloc(size_t org, size_t dest)
{
	int err;
	struct palloc_heap *heap = &mock_pop->heap;
	err = pmalloc(mock_pop, &addr->ptr, org);
	UT_ASSERTeq(err, 0);
	UT_ASSERT(palloc_usable_size(heap, addr->ptr) >= org);
	err = prealloc(mock_pop, &addr->ptr, dest);
	UT_ASSERTeq(err, 0);
	UT_ASSERT(palloc_usable_size(heap, addr->ptr) >= dest);
	pfree(mock_pop, &addr->ptr);
}
示例#25
0
void *test(void *nn)
{
	int n = (uint64_t)nn;
	int i,j,index;
	int reads;
	int *data = (int *)pmalloc(arraySize * sizeof(int));
	printf("Array size=%d integers with start address: %p\n", arraySize, data);


	for (i=0; i < numWraps; i+=1)
	{
		attachThreadToCore(6 - (n % 6));

		WRAPTOKEN w = wrapOpen();

//		int wrapsize = (random() % maxWrapSize) + 1;
//				maxWrapSize / 2;
		for (j = 0; j < wrapSize; j++)
		{
			index = random() % threadMemSize;
			index += n*threadMemSize;

			//int *p = &(data[index]);
			//wrapWrite(data+index, &i, sizeof(int), w);
			//memcpy(data+index, &i, sizeof(int));
			//printf("data[%d](%p)=%d ", index, p, *p);
			//record(p, STORE | WRAPPED | PERSISTENT);
			//count ++;

			wrapStore32(data+index, i, w);

			for (reads = 0; reads < rwRatio; reads++)
			{
				//wrapRead(data+(random()%arraySize), sizeof(int), w);
				wrapLoad32(data+(random()%arraySize), w);
			}

			if (delay > 0)
			{
				struct timeval start;
				gettimeofday(&start, NULL);
				while (elapsed(start) < delay);
			}
		}
		wrapClose(w);
	}
	//printf("Done %d\n", n);
	pfree(data);

	return NULL;
}
示例#26
0
int addtranslate(int usern, char *totranslate, char *from, char *dest, int direction, char *lang, char *command)
{
    struct translatet *lkm;
    int lastuid;
    int cnt=0;
    char buf[200];
    pcontext;
    if(translate==NULL)
    {
	translate=(struct translatet *)pmalloc(sizeof(struct translatet));
	lkm=translate;
	lastuid=0;
    } else {
	lkm=translate;
	lastuid=translate->uid;
	while(lkm->next!=NULL)
	{
	    lastuid=lkm->next->uid;
	    lkm=lkm->next;
	    cnt++;
	}
	lkm->next=(struct translatet *)pmalloc(sizeof(struct translatet));
	lkm=lkm->next;
    }
    if(cnt>MAXSYNTRANS) return -1;
    lastuid++;
    lkm->uid=lastuid;
    lkm->delayed=30; /* before it times out, doubled, altavista got slow */
    lkm->translatetext=(char *)pmalloc(strlen(totranslate)+1);
    strcpy(lkm->translatetext,totranslate);
    if(direction==TR_TO)
    {
	ap_snprintf(buf,sizeof(buf),lngtxt(861),command,dest);
    } else {
	if(strchr("&!#+",*dest)!=NULL)
	    ap_snprintf(buf,sizeof(buf),lngtxt(862),from,command,dest);
	else	
	    ap_snprintf(buf,sizeof(buf),lngtxt(863),from,command,user(usern)->nick);
    }
    lkm->translatedtext=(char *)pmalloc(strlen(buf)+1);
    strcpy(lkm->translatedtext,buf);
    lkm->dest=(char *)pmalloc(strlen(dest)+1);
    strcpy(lkm->dest,dest);
    lkm->source=(char *)pmalloc(strlen(from)+1);
    strcpy(lkm->source,from);
    lkm->lang=(char *)pmalloc(strlen(lang)+1);
    strcpy(lkm->lang,lang);
    lkm->direction=direction;
    lkm->usern=usern;
    lkm->sock=createsocket(0,ST_CONNECT,lastuid,SGR_NONE,NULL,translateconnected,translatederror,translatedpart1,translatedone,NULL,AF_INET,SSL_OFF);
    lkm->sock=connectto(lkm->sock,lngtxt(864),80,NULL);
    return 0x0;    
}
示例#27
0
void jqueue_push(jqueue_t q, void *data, int priority) {
	_jqueue_node_t qn, scan;
	
	assert((int)(q != NULL));
	q->size++;

	/* node from the cache, or make a new one */
	qn = q->cache;
	if (qn != NULL)
		q->cache = qn->next;
	else
		qn = (_jqueue_node_t)pmalloc(q->p, sizeof(struct _jqueue_node_st));

	qn->data = data;
	qn->priority = priority;

	qn->next = NULL;
	qn->prev = NULL;

	/* first one */
	if (q->back == NULL && q->front == NULL) {
		q->back = qn;
		q->front = qn;

		return;
	}

	/* find the first node with priority <= to us */
	for (scan = q->back; scan != NULL && scan->priority > priority; scan = scan->next);

	/* didn't find one, so we have top priority - push us on the front */
	if (scan == NULL) {
		qn->prev = q->front;
		qn->prev->next = qn;
		q->front = qn;

		return;
	}

	/* push us in front of scan */
	qn->next = scan;
	qn->prev = scan->prev;

	if (scan->prev != NULL)
		scan->prev->next = qn;
	else
		q->back = qn;

	scan->prev = qn;
}
示例#28
0
文件: xtree.c 项目: dalinhuang/mcgame
/* public functions */
xtree_t xtree_new(int base, int prime)
{
	xtree_t xnew;
	pool_t p;

	p = pool_new();
	xnew = pmalloc(p, sizeof(xtree_st));
	xnew->p = p;
	xnew->base = (base ? base : 0xf422f);
	xnew->prime = (prime ? prime :  31);
	xnew->count = 0;
	xnew->trees = (node_t *) pmalloc_z(p, sizeof(node_t) * xnew->prime);
	return xnew;
}
示例#29
0
文件: charset.c 项目: Doap/transports
/*  Converts a string encoded in the eight-bit character set 
	to the UTF-8 encoding of UCS-4.  Characters that are
	"undefined" in Windows-1252 are replaced with question-mark character. */
char *it_convert_windows2utf8(pool p, const char *windows_str)
{
    int n, i = 0;
    char *result = NULL;
    int q;
    size_t numconv;
    size_t inbytesleft, outbytesleft;
    char *inbuf, *outbuf;

    log_debug( ZONE, "it_convert_windows2utf8");
    if(windows_str==NULL) return NULL;

    /* for now, allocate more than enough space... */
    result = (char*)pmalloc(p,strlen(windows_str)*4+1);

    inbuf = (char *)windows_str;
    outbuf = result;
    inbytesleft = strlen(windows_str);
    outbytesleft = strlen(windows_str)*4;

    q = 1;
    while(q)
    {
        numconv = iconv(toutf8, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
        if(numconv == (size_t)(-1))
        {
            switch(errno)
            {
                case EILSEQ:
                case EINVAL:
                    inbytesleft--;
                    outbytesleft--;
                    inbuf++;
                    *outbuf++ = UTF8SUB;
                    break;
                case E2BIG:
                default:
                    q = 0;
                    break;
            }
        }
        else
        {
            q = 0;
        }
    }
    *outbuf = NULCHR;
    return result;

}
示例#30
0
文件: charset.c 项目: Doap/transports
/*  Converts a string encoded in the UTF-8 encoding of UCS-4 to the eight-bit
	encoding.  Characters that cannot be
	converted to this encoding are replaced with the question-mark
	character. */
char *it_convert_utf82windows(pool p, const char *utf8_str)
{
    int q = 1;
    size_t numconv;
    size_t inbytesleft, outbytesleft;
    unsigned char *result = NULL;
    char *inbuf, *outbuf;

    log_debug( ZONE, "it_convert_utf82windows");
    if(utf8_str==NULL) return NULL;

    result=(unsigned char*)pmalloc(p,strlen(utf8_str)+1);
    inbuf = (char *)utf8_str;
    outbuf = result;
    inbytesleft = outbytesleft = strlen(utf8_str);

    while(q)
    {
        numconv = iconv(fromutf8, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
        if(numconv == (size_t)(-1))
        {
            switch(errno)
            {
                case EILSEQ:
                case EINVAL:
                    inbytesleft--;
                    outbytesleft--;
                    inbuf++;
                    *outbuf++ = WINSUB;
                    while((*inbuf & BYTETEST_PF) == BYTEU8_PFIX)
                    {
                        inbytesleft--;
                        inbuf++;
                    }
                    break;
                case E2BIG:
                default:
                    q = 0;
                    break;
            }
        }
        else
        {
            q = 0;
        }
    }
    *outbuf = NULCHR;
    return result;

}