Exemplo n.º 1
0
PyObject* _PYSTRING(const Char* s, int utf8)
{
	int	lens = (int)Strlen(s);
#if defined(Py_UNICODE_WIDE)
	PyObject *u, *x;
	u = PyUnicode_DecodeUTF16((char*)s,2*lens,NULL,NULL);
	if(!utf8 || !u) return u;
	x = PyUnicode_AsUTF8String(u);
	Py_DECREF(u);
	return x;
#else
	return utf8 ? PyUnicode_EncodeUTF8((Py_UNICODE*)s, lens, NULL)
				: PyUnicode_FromUnicode((Py_UNICODE*)s, lens);
#endif
}
Exemplo n.º 2
0
/*
 * This function returns a NEW reference, i.e. caller must decref it in the end.
 */
PyObject* JySync_Init_PyUnicode_From_JyUnicode(jobject src, PyTypeObject* nonNativeSubtype)
{
	env(NULL);
	jstring jstr = (*env)->CallObjectMethod(env, src, pyObject_asString);
	jstring charsetName = (*env)->NewStringUTF(env, "UTF-16BE");
	jobject stringByteArray = (*env)->CallObjectMethod(env, jstr,
		string_getBytes, charsetName);
	jbyte* stringBytes = (*env)->GetByteArrayElements(env, stringByteArray, NULL);
	jsize len = (*env)->GetArrayLength(env, stringByteArray);
	int byteOrder = 1; //indicates BE
	PyObject* unicode = PyUnicode_DecodeUTF16(
			(char*) stringBytes,  /* UTF-16 encoded string */
			len,                  /* size of string */
			NULL,                 /* error handling */
			&byteOrder            /* pointer to byteorder to use
			                         0=native;-1=LE,1=BE; */
			);
	(*env)->ReleaseByteArrayElements(env, stringByteArray, stringBytes, JNI_ABORT);
	return unicode;
}
Exemplo n.º 3
0
PyObject *lightblueobex_readheaders(obex_t *obex, obex_object_t *obj)
{
    PyObject *headers;
    uint8_t hi;
    obex_headerdata_t hv;
    uint32_t hv_size;
    int r;
    PyObject *value = NULL;

    DEBUG("%s()\n", __func__);

    headers = PyDict_New();
    if (headers == NULL)
        return NULL;

    if (obex == NULL || obj == NULL || headers == NULL) {
        DEBUG("\treadheaders() got null argument\n");
        return NULL;
    }

    if (!PyDict_Check(headers)) {
        DEBUG("\treadheaders() arg must be dict\n");
        return NULL;
    }

    while (OBEX_ObjectGetNextHeader(obex, obj, &hi, &hv, &hv_size)) {
        DEBUG("\tread header: 0x%02x\n", hi);
        switch (hi & OBEX_HI_MASK) {
        case OBEX_UNICODE:
        {
            if (hv_size < 2) {
                value = PyUnicode_FromUnicode(NULL, 0);
            } else {
                /* hv_size-2 for 2-byte null terminator */
                int byteorder = OBEX_BIG_ENDIAN;
                value = PyUnicode_DecodeUTF16((const char*)hv.bs, hv_size-2,
                        NULL, &byteorder);
                if (value == NULL) {
                    DEBUG("\terror reading unicode header 0x%02x\n", hi);
                    if (PyErr_Occurred()) {
                        PyErr_Print();
                        PyErr_Clear();  /* let caller set exception */
                    }
                    return NULL;
                }
            }
            break;
        }
        case OBEX_BYTE_STREAM:
        {
            value = PyBuffer_New(hv_size);
            if (value != NULL) {
                void *buf;
                Py_ssize_t buflen;
                if (PyObject_AsWriteBuffer(value, &buf, &buflen) < 0) {
                    Py_DECREF(value);
                    DEBUG("\terror writing to buffer for header 0x%02x\n", hi);
                    return NULL;
                }
                memcpy(buf, hv.bs, buflen);
            }
            break;
        }
        case OBEX_BYTE:
        {
            value = PyInt_FromLong(hv.bq1);
            break;
        }
        case OBEX_INT:
        {
            value = PyLong_FromUnsignedLong(hv.bq4);
            break;
        }
        default:
            DEBUG("\tunknown header id encoding %d\n", (hi & OBEX_HI_MASK));
            return NULL;
        }

        if (value == NULL) {
            if (PyErr_Occurred() == NULL)
                DEBUG("\terror reading headers\n");
            return NULL;
        }
        r = PyDict_SetItem(headers, PyInt_FromLong((long)hi), value);
        Py_DECREF(value);
        if (r < 0) {
            DEBUG("\tPyDict_SetItem() error\n");
            if (PyErr_Occurred()) {
                PyErr_Print();
                PyErr_Clear();  /* let caller set exception */
            }
            return NULL;
        }
    }
    return headers;
}