jobject JySync_Init_JyUnicode_From_PyUnicode(PyObject* src, jclass subtype) { PyObject* utf16 = PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(src), PyUnicode_GET_SIZE(src), NULL, 1); //BE Py_ssize_t len = PyString_GET_SIZE(utf16); //Py_ssize_t len = PyUnicode_GET_DATA_SIZE(utf16); // jputsLong(len); env(NULL); //jstring jstr = (*env)->NewStringUTF(env, PyString_AS_STRING(src)); jstring charsetName = (*env)->NewStringUTF(env, "UTF-16BE"); jobject strByteArray = (*env)->NewByteArray(env, len); jbyte* strBytes = (*env)->GetByteArrayElements(env, strByteArray, NULL); memcpy(strBytes, PyString_AS_STRING(utf16), len); //memcpy(strBytes, PyUnicode_AS_DATA(utf16), len); (*env)->ReleaseByteArrayElements(env, strByteArray, strBytes, 0); //copy back and free buffer jobject jstr = (*env)->NewObject(env, stringClass, string_fromBytesAndCharsetNameConstructor, strByteArray, charsetName); // if (JyNI_HasJyAttribute(AS_JY_NO_GC(src), JyAttributeStringInterned)) // jstr = (*env)->CallObjectMethod(env, jstr, stringIntern); return (*env)->CallStaticObjectMethod(env, pyPyClass, pyPy_newUnicode, jstr); }
int lightblueobex_addheaders(obex_t *obex, PyObject *headers, obex_object_t *obj) { uint8_t hi; obex_headerdata_t hv; PyObject *key, *value; Py_ssize_t pos = 0; int r = -1; DEBUG("%s()\n", __func__); if (headers == NULL || !PyDict_Check(headers)) { DEBUG("\taddheaders() arg must be dict\n"); return -1; } /* add connection-id first */ key = PyInt_FromLong(OBEX_HDR_CONNECTION); if (key != NULL) { value = PyDict_GetItem(headers, key); /* don't decref! */ Py_DECREF(key); key = NULL; if (value != NULL) { DEBUG("\tadding connection-id\n"); r = lightblueobex_add4byteheader(obex, obj, OBEX_HDR_CONNECTION, value); if (r < 0) { DEBUG("\terror adding connection-id header\n"); return -1; } } } /* add target header first (shouldn't have both conn-id and target) */ key = PyInt_FromLong(OBEX_HDR_TARGET); if (key != NULL) { value = PyDict_GetItem(headers, key); /* don't decref! */ Py_DECREF(key); key = NULL; if (value != NULL) { DEBUG("\tadding target\n"); r = lightblueobex_addbytestreamheader(obex, obj, OBEX_HDR_TARGET, value); if (r < 0) { DEBUG("\terror adding target header\n"); return -1; } } } while (PyDict_Next(headers, &pos, &key, &value)) { if (key == NULL || value == NULL) { DEBUG("\terror reading headers dict\n"); return -1; } if (!PyInt_Check(key)) { DEBUG("\theader id must be int, was %s\n", key->ob_type->tp_name); return -1; } hi = (uint8_t)PyInt_AsUnsignedLongMask(key); if (hi == OBEX_HDR_CONNECTION || hi == OBEX_HDR_TARGET) { /* these are already added */ continue; } DEBUG("\tadding header: 0x%02x\n", hi); switch (hi & OBEX_HI_MASK) { case OBEX_UNICODE: { PyObject *encoded = NULL; if (PyUnicode_Check(value)) { encoded = PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(value), PyUnicode_GET_SIZE(value), NULL, OBEX_BIG_ENDIAN); } else { /* try converting to unicode */ PyObject *tmp = NULL; tmp = PyUnicode_FromObject(value); if (tmp == NULL) { if (PyErr_Occurred()) { PyErr_Print(); PyErr_Clear(); /* let caller set exception */ } DEBUG("\tfailed to convert header value for id 0x%02x to unicode\n", hi); return -1; } encoded = PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(tmp), PyUnicode_GET_SIZE(tmp), NULL, OBEX_BIG_ENDIAN); Py_DECREF(tmp); } if (encoded == NULL) { if (PyErr_Occurred()) { PyErr_Print(); PyErr_Clear(); /* let caller set exception */ } DEBUG("\tfailed to encode value for header 0x%02x to UTF-16 big endian\n", hi); return -1; } r = lightblueobex_addunicodeheader(obex, obj, hi, encoded); Py_DECREF(encoded); break; } case OBEX_BYTE_STREAM: { r = lightblueobex_addbytestreamheader(obex, obj, hi, value); break; } case OBEX_BYTE: { long intvalue; if (!PyInt_Check(value)) { DEBUG("\theader value for id 0x%02x must be int, was %s\n", hi, value->ob_type->tp_name); return -1; } intvalue = PyInt_AsLong(value); if (PyErr_Occurred()) { DEBUG("\terror reading int value for 0x%02x\n", hi); PyErr_Clear(); return -1; } hv.bq1 = (uint8_t)intvalue; r = OBEX_ObjectAddHeader(obex, obj, hi, hv, 1, OBEX_FL_FIT_ONE_PACKET); break; } case OBEX_INT: { r = lightblueobex_add4byteheader(obex, obj, hi, value); break; } default: DEBUG("\tunknown header id encoding %d\n", (hi & OBEX_HI_MASK)); return -1; } if (r < 0) { DEBUG("\terror adding header 0x%02x\n", hi); return -1; } } return 1; }