Ejemplo n.º 1
0
static PyObject *
struct_calcsize(PyObject *self, PyObject *args)
{
	char *fmt;
	const formatdef *f;
	int size;

	if (!PyArg_ParseTuple(args, "s:calcsize", &fmt))
		return NULL;
	f = whichtable(&fmt);
	size = calcsize(fmt, f);
	if (size < 0)
		return NULL;
	return PyInt_FromLong((long)size);
}
Ejemplo n.º 2
0
static PyObject *
patches(PyObject *self, PyObject *args)
{
	PyObject *text, *bins, *result;
	struct flist *patch;
	const char *in;
	char *out;
	Py_ssize_t len, outlen, inlen;

	if (!PyArg_ParseTuple(args, "OO:mpatch", &text, &bins))
		return NULL;

	len = PyList_Size(bins);
	if (!len) {
		/* nothing to do */
		Py_INCREF(text);
		return text;
	}

	if (PyObject_AsCharBuffer(text, &in, &inlen))
		return NULL;

	patch = fold(bins, 0, len);
	if (!patch)
		return NULL;

	outlen = calcsize(inlen, patch);
	if (outlen < 0) {
		result = NULL;
		goto cleanup;
	}
	result = PyBytes_FromStringAndSize(NULL, outlen);
	if (!result) {
		result = NULL;
		goto cleanup;
	}
	out = PyBytes_AsString(result);
	if (!apply(out, in, inlen, patch)) {
		Py_DECREF(result);
		result = NULL;
	}
cleanup:
	lfree(patch);
	return result;
}
Ejemplo n.º 3
0
static PyObject *
struct_unpack(PyObject *self, PyObject *args)
{
	const formatdef *f, *e;
	char *str, *start, *fmt, *s;
	char c;
	int len, size, num;
	PyObject *res, *v;

	if (!PyArg_ParseTuple(args, "ss#:unpack", &fmt, &start, &len))
		return NULL;
	f = whichtable(&fmt);
	size = calcsize(fmt, f);
	if (size < 0)
		return NULL;
	if (size != len) {
		PyErr_SetString(StructError,
				"unpack str size does not match format");
		return NULL;
	}
	res = PyList_New(0);
	if (res == NULL)
		return NULL;
	str = start;
	s = fmt;
	while ((c = *s++) != '\0') {
		if (isspace(Py_CHARMASK(c)))
			continue;
		if ('0' <= c && c <= '9') {
			num = c - '0';
			while ('0' <= (c = *s++) && c <= '9')
			       num = num*10 + (c - '0');
			if (c == '\0')
				break;
		}
		else
			num = 1;

		e = getentry(c, f);
		if (e == NULL)
			goto fail;
		str = start + align((int)(str-start), c, e);
		if (num == 0 && c != 's')
			continue;

		do {
			if (c == 'x') {
				str += num;
				break;
			}
			if (c == 's') {
				/* num is string size, not repeat count */
				v = PyString_FromStringAndSize(str, num);
				if (v == NULL)
					goto fail;
				str += num;
				num = 0;
			}
			else if (c == 'p') {
				/* num is string buffer size,
				   not repeat count */
				int n = *(unsigned char*)str;
				/* first byte (unsigned) is string size */
				if (n >= num)
					n = num-1;
				v = PyString_FromStringAndSize(str+1, n);
				if (v == NULL)
					goto fail;
				str += num;
				num = 0;
			}
			else {
				v = e->unpack(str, e);
				if (v == NULL)
					goto fail;
				str += e->size;
			}
			if (v == NULL || PyList_Append(res, v) < 0)
				goto fail;
			Py_DECREF(v);
		} while (--num > 0);
	}

	v = PyList_AsTuple(res);
	Py_DECREF(res);
	return v;

 fail:
	Py_DECREF(res);
	return NULL;
}
Ejemplo n.º 4
0
static PyObject *
struct_pack(PyObject *self, PyObject *args)
{
	const formatdef *f, *e;
	PyObject *format, *result, *v;
	char *fmt;
	int size, num;
	int i, n;
	char *s, *res, *restart, *nres;
	char c;

	if (args == NULL || !PyTuple_Check(args) ||
	    (n = PyTuple_Size(args)) < 1)
	{
		PyErr_SetString(PyExc_TypeError,
			"struct.pack requires at least one argument");
		return NULL;
	}
	format = PyTuple_GetItem(args, 0);
	fmt = PyString_AsString(format);
	if (!fmt)
		return NULL;
	f = whichtable(&fmt);
	size = calcsize(fmt, f);
	if (size < 0)
		return NULL;
	result = PyString_FromStringAndSize((char *)NULL, size);
	if (result == NULL)
		return NULL;

	s = fmt;
	i = 1;
	res = restart = PyString_AsString(result);

	while ((c = *s++) != '\0') {
		if (isspace(Py_CHARMASK(c)))
			continue;
		if ('0' <= c && c <= '9') {
			num = c - '0';
			while ('0' <= (c = *s++) && c <= '9')
			       num = num*10 + (c - '0');
			if (c == '\0')
				break;
		}
		else
			num = 1;

		e = getentry(c, f);
		if (e == NULL)
			goto fail;
		nres = restart + align((int)(res-restart), c, e);
		/* Fill padd bytes with zeros */
		while (res < nres)
			*res++ = '\0';
		if (num == 0 && c != 's')
			continue;
		do {
			if (c == 'x') {
				/* doesn't consume arguments */
				memset(res, '\0', num);
				res += num;
				break;
			}
			if (i >= n) {
				PyErr_SetString(StructError,
					"insufficient arguments to pack");
				goto fail;
				}
			v = PyTuple_GetItem(args, i++);
			if (v == NULL)
				goto fail;
			if (c == 's') {
				/* num is string size, not repeat count */
				int n;
				if (!PyString_Check(v)) {
					PyErr_SetString(StructError,
					  "argument for 's' must be a string");
					goto fail;
				}
				n = PyString_Size(v);
				if (n > num)
					n = num;
				if (n > 0)
					memcpy(res, PyString_AsString(v), n);
				if (n < num)
					memset(res+n, '\0', num-n);
				res += num;
				break;
			}
			else if (c == 'p') {
				/* num is string size + 1,
				   to fit in the count byte */
				int n;
				num--; /* now num is max string size */
				if (!PyString_Check(v)) {
					PyErr_SetString(StructError,
					  "argument for 'p' must be a string");
					goto fail;
				}
				n = PyString_Size(v);
				if (n > num)
					n = num;
				if (n > 0)
					memcpy(res+1, PyString_AsString(v), n);
				if (n < num)
					/* no real need, just to be nice */
					memset(res+1+n, '\0', num-n);
				if (n > 255)
					n = 255;
				*res++ = n; /* store the length byte */
				res += num;
				break;
			}
			else {
				if (e->pack(res, v, e) < 0)
					goto fail;
				res += e->size;
			}
		} while (--num > 0);
	}

	if (i < n) {
		PyErr_SetString(StructError,
				"too many arguments for pack format");
		goto fail;
	}

	return result;

 fail:
	Py_DECREF(result);
	return NULL;
}
Ejemplo n.º 5
0
				"too many arguments for pack format");
		goto fail;
	}

	return result;

 fail:
	Py_DECREF(result);
	return NULL;
}


PyDoc_STRVAR(unpack__doc__,
"unpack(fmt, string) -> (v1, v2, ...)\n\
Unpack the string, containing packed C structure data, according\n\
to fmt.  Requires len(string)==calcsize(fmt).\n\
See struct.__doc__ for more on format strings.");

static PyObject *
struct_unpack(PyObject *self, PyObject *args)
{
	const formatdef *f, *e;
	char *str, *start, *fmt, *s;
	char c;
	int len, size, num;
	PyObject *res, *v;

	if (!PyArg_ParseTuple(args, "ss#:unpack", &fmt, &start, &len))
		return NULL;
	f = whichtable(&fmt);
	size = calcsize(fmt, f);
	if (size < 0)
Ejemplo n.º 6
0
static void sortmsgs()
{
	size_t i, n;
	struct msglist *m;
	struct msglist **prev_list;
	int savesizes=0;

	unsigned long nextuid;

	if (msglist_cnt == 0)	return;

	if ((msglist_a=(struct msglist **)malloc(
			msglist_cnt*sizeof(struct msglist *))) == 0)
	{
		perror("malloc");
		msglist_cnt=0;
		return;
	}

	for (i=0, m=msglist_l; m; m=m->next, i++)
	{
		m->isnew=0;
		msglist_a[i]=m;
	}
	qsort(msglist_a, msglist_cnt, sizeof(*msglist_a), cmpmsgs);

	nextuid=1;

	prev_list=readpop3dlist(&nextuid);

	n=0;

	for (i=0; i<msglist_cnt; i++)
	{
		while (prev_list[n] &&
		       cmpmsgs(&prev_list[n], &msglist_a[i]) < 0)
		{
			++n;
			savesizes=1;
		}

		if (prev_list[n] &&
		    cmpmsgs(&prev_list[n], &msglist_a[i]) == 0)
		{
			msglist_a[i]->size=prev_list[n]->size;
			msglist_a[i]->uid=prev_list[n]->uid;
			n++;
		}
		else
		{
			msglist_a[i]->uid.n=nextuid++;
			msglist_a[i]->uid.uidv=uidv;
			msglist_a[i]->isnew=1;
			if (convert_v0)
				msglist_a[i]->uid.n=0;

			calcsize(msglist_a[i]);
			savesizes=1;
		}      
	}

	if (prev_list[n])
		savesizes=1;

	for (i=0; prev_list[i]; i++)
	{
		free(prev_list[i]->filename);
		free(prev_list[i]);
	}

	free(prev_list);

	if (savesizes && savepop3dlist(msglist_a, msglist_cnt, nextuid) < 0)
	{
		fprintf(stderr, "ERR: Error while saving courierpop3dsizelist"
			", user=%s\n", authaddr);

		for (i=n=0; i<msglist_cnt; i++)
		{
			if (msglist_a[i]->isnew)
				continue;

			msglist_a[n]=msglist_a[i];
			++n;
		}

		if (n == 0 && n < msglist_cnt &&
		    stat(msglist_a[0]->filename, &enomem_stat) == 0)
		{
			enomem_1msg=1;
			++n;
		}
		msglist_cnt=n;

	}
}
Ejemplo n.º 7
0
		int ii=atoi(buf);
		if(ii<-1000)ii=-1000;
		if(ii>1000)ii=1000;
		
		int res = ((ii+1000) * ww) / 2000;

		if(res<0)res=0;
		if(res>ww)res=ww;
		return res;

	}
	

	liqcell *bx = liqcell_child_lookup( self,"bx");
	liqcell_setcaption(bx, msgx);	
	liqcell_setsize(bx, calcsize(msgx,600) , bx->h);

	liqcell *by = liqcell_child_lookup( self,"by");
	liqcell_setcaption(by, msgy);
	liqcell_setsize(by, calcsize(msgy,600) , by->h);

	liqcell *bz = liqcell_child_lookup( self,"bz");
	liqcell_setcaption(bz, msgz);
	liqcell_setsize(bz, calcsize(msgz,600) , bz->h);
	
	liqcell_setdirty(  self, 1);

}


int CComando::serialize(std::vector<dataUnion> outData, std::string& serializedData)
{
    if (outDataLen_!=-1 && (int)outData.size()!=outDataLen_)
        return -1;
    serializedData.clear();
    serializedData.push_back(number_);
    if(outDataLen_==0)
        serializedData.push_back(0x00);
    else
    {
        int outDataLen=outDataLen_;
        if(outDataLen_==-1)
            outDataLen=(int)outData.size();
        std::string outType;
        if(outType_.size()==1)
        {
            for(int i=0;i<outDataLen;i++)
                outType+=outType_;
        }
        else
        {
            if(outType_[0]!='x' && (int)outType_.size()!=outDataLen)
                return -1;
            std::string type;
            if(outType_[0]!='x')
            {
                type=outType_;
            }
            else
            {
                std::string xType(outType_);
                xType.erase(0,1);
                for(int i=0;i<(outDataLen/calcsize(xType));i++)
                    type+=xType;
            }
            outType=type;
        }
        if(outDataLen!=outData.size()) return -1;
        std::string sd;
        uint8_t *data_p;
        for (int i=0;i<outDataLen;i++)
        {
            switch(outType[i])
            {
                case 'b':
                    sd.push_back(outData[i].b);
                    break;
                case 'h':
                    data_p=(uint8_t *)(&outData[i].h);
                    sd.push_back(data_p[0]);
                    sd.push_back(data_p[1]);
                    break;
                case 'f':
                    data_p=(uint8_t *)(&outData[i].f);
                    sd.push_back(data_p[0]);
                    sd.push_back(data_p[1]);
                    sd.push_back(data_p[2]);
                    sd.push_back(data_p[3]);
                    break;
                case 'l':
                    data_p=(uint8_t *)(&outData[i].l);
                    sd.push_back(data_p[0]);
                    sd.push_back(data_p[1]);
                    sd.push_back(data_p[2]);
                    sd.push_back(data_p[3]);
                    break;
                case 's':
                    sd+=outData[i].s;
                    break;
            }
        }
        serializedData.push_back(sd.size());
        serializedData+=sd;
    }
    return 1;
}
//TODO: Revisar éste método que a veces da error
int CComando::deserialize(std::string inData, std::vector<dataUnion>& recivedData)
{
    recivedData.clear();
    uint8_t cn=static_cast<uint8_t>(inData[0]);
    uint8_t idl=static_cast<uint8_t>(inData[1]);
    
    int inDataLen=inDataLen_;
    if(inDataLen==-1)
        inDataLen=idl/calcsize(inType_);
    
    std::string inType;
    if(inType_.size()==1)
    {
        for(int i=0;i<inDataLen;i++)
            inType+=inType_;
    }
    else
    {
        if(inType_[0]!='x' && (int)inType_.size()!=inDataLen)
            return -1;
        std::string type;
        if(inType_[0]!='x')
        {
            type=inType_;
        }
        else
        {
            std::string xType(inType_);
            xType.erase(0,1);
            for(int i=0;i<inDataLen;i++)
                type+=xType;
            inDataLen=type.size();
        }
        inType=type;
    }
    
    if(cn!=number_ || idl!=calcsize(inType))
        return -1;
    if (inDataLen==0)
        return 1;
    dataUnion data;
    uint8_t readed=2;
    uint8_t* data_p=(uint8_t *)(inData.c_str());
    for (int i=0;i<inDataLen;i++)
    {
        switch(inType[i])
        {
            case 'b':
                data.b=static_cast<uint8_t>(inData[readed]);
                readed+=sizeof(uint8_t);
                break;
            case 'h':
                data.h=*((short *)(data_p+readed));
                readed+=sizeof(short);
                break;
            case 'f':
                data.f=*((float *)(data_p+readed));
                readed+=sizeof(float);
                break;
            case 'l':
                data.l=*((long *)(data_p+readed));
                readed+=sizeof(long);
                break;
            case 's':
                data.s=std::string((char *)(data_p+readed));
                readed+=data.s.size();
                break;
        }
        recivedData.push_back(data);
    }
    return 1;
}