// title {{{ static PyObject * icu_title(PyObject *self, PyObject *args) { char *input, *ans, *buf3 = NULL; const char *loc; int32_t sz; UChar *buf, *buf2; PyObject *ret; UErrorCode status = U_ZERO_ERROR; if (!PyArg_ParseTuple(args, "ses", &loc, "UTF-8", &input)) return NULL; sz = (int32_t)strlen(input); buf = (UChar*)calloc(sz*4 + 1, sizeof(UChar)); buf2 = (UChar*)calloc(sz*8 + 1, sizeof(UChar)); if (buf == NULL || buf2 == NULL) return PyErr_NoMemory(); u_strFromUTF8(buf, sz*4, NULL, input, sz, &status); u_strToTitle(buf2, sz*8, buf, -1, NULL, loc, &status); ans = input; sz = u_strlen(buf2); free(buf); if (U_SUCCESS(status) && sz > 0) { buf3 = (char*)calloc(sz*5+1, sizeof(char)); if (buf3 == NULL) return PyErr_NoMemory(); u_strToUTF8(buf3, sz*5, NULL, buf2, -1, &status); if (U_SUCCESS(status)) ans = buf3; } ret = PyUnicode_DecodeUTF8(ans, strlen(ans), "replace"); if (ret == NULL) return PyErr_NoMemory(); free(buf2); if (buf3 != NULL) free(buf3); PyMem_Free(input); return ret; } // }}}
// chr {{{ static PyObject * icu_chr(PyObject *self, PyObject *args) { UErrorCode status = U_ZERO_ERROR; UChar32 code = 0; UChar buf[5] = {0}; int32_t sz = 0; char utf8[21]; PyObject *result = NULL; if (!PyArg_ParseTuple(args, "I", &code)) return NULL; u_strFromUTF32(buf, 4, &sz, &code, 1, &status); if (U_FAILURE(status)) { PyErr_SetString(PyExc_ValueError, "arg not in range(0x110000)"); goto end; } u_strToUTF8(utf8, 20, &sz, buf, sz, &status); if (U_FAILURE(status)) { PyErr_SetString(PyExc_ValueError, "arg not in range(0x110000)"); goto end; } result = PyUnicode_DecodeUTF8(utf8, sz, "strict"); end: return result; } // }}}
static PyObject * winutil_folder_path(PyObject *self, PyObject *args) { int res; DWORD dwFlags; PyObject *ans = NULL; TCHAR wbuf[MAX_PATH]; CHAR buf[4*MAX_PATH]; memset(wbuf, 0, sizeof(TCHAR)*MAX_PATH); memset(buf, 0, sizeof(CHAR)*MAX_PATH); if (!PyArg_ParseTuple(args, "l", &dwFlags)) return NULL; res = SHGetFolderPath(NULL, dwFlags, NULL, 0, wbuf); if (res != S_OK) { if (res == E_FAIL) PyErr_SetString(PyExc_ValueError, "Folder does not exist."); PyErr_SetString(PyExc_ValueError, "Folder not valid"); return NULL; } res = WideCharToMultiByte(CP_UTF8, 0, wbuf, -1, buf, 4*MAX_PATH, NULL, NULL); ans = PyUnicode_DecodeUTF8(buf, res-1, "strict"); return ans; }
static PyObject* usbobserver_get_bsd_path(io_object_t dev) { char cpath[ MAXPATHLEN ]; CFTypeRef PropRef; size_t dev_path_length; cpath[0] = '\0'; PropRef = IORegistryEntryCreateCFProperty(dev, CFSTR(kIOBSDNameKey), kCFAllocatorDefault, 0); if (!PropRef) return NULL; strcpy(cpath, _PATH_DEV); dev_path_length = strlen(cpath); if (!CFStringGetCString(PropRef, cpath + dev_path_length, MAXPATHLEN - dev_path_length, kCFStringEncodingUTF8)) return NULL; return PyUnicode_DecodeUTF8(cpath, strlen(cpath), "replace"); }
static bson_bool_t cbson_loads_visit_utf8 (const bson_iter_t *iter, const char *key, size_t v_utf8_len, const char *v_utf8, void *data) { PyObject **ret = data; PyObject *value; if (!(value = PyUnicode_DecodeUTF8(v_utf8, v_utf8_len, "strict"))) { Py_DECREF(*ret); *ret = NULL; return TRUE; } cbson_loads_set_item(*ret, key, value); Py_DECREF(value); return FALSE; }
static PyObject * pyspeak_api_text_to_phonemes(PyObject *self, PyObject *args, PyObject *kwargs) { PyObject *ret; const char *text; int opts; char *phoneme_text; static char *kwlist[] = {"text", "opts", NULL}; opts = 0; // opts = 0x01 | 0x10 | 0x20 | 0x40; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|i", kwlist, &text, &opts)) return NULL; phoneme_text = espeak_TextToPhonemes(&text, espeakCHARS_AUTO, opts); phoneme_text = strdup(phoneme_text); ret = PyUnicode_DecodeUTF8(phoneme_text, strlen(phoneme_text), NULL); free(phoneme_text); return ret; }
// character_name_from_code {{{ static PyObject * icu_character_name_from_code(PyObject *self, PyObject *args) { char name[512] = {0}; int32_t sz, alias = 0; UErrorCode status = U_ZERO_ERROR; PyObject *palias = NULL, *result = NULL; UChar32 code = 0; if (!PyArg_ParseTuple(args, "I|O", &code, &palias)) return NULL; if (palias != NULL && PyObject_IsTrue(palias)) alias = 1; if (alias) { sz = u_charName(code, U_CHAR_NAME_ALIAS, name, 511, &status); } else { sz = u_charName(code, U_UNICODE_CHAR_NAME, name, 511, &status); } if (U_FAILURE(status)) { PyErr_SetString(PyExc_ValueError, "Failed to get name for code"); goto end; } result = PyUnicode_DecodeUTF8(name, sz, "strict"); end: return result; } // }}}
static void decode_decrypt_result(PyGpgmeContext *self) { PyObject *err_type, *err_value, *err_traceback; PyObject *value; gpgme_decrypt_result_t res; PyErr_Fetch(&err_type, &err_value, &err_traceback); PyErr_NormalizeException(&err_type, &err_value, &err_traceback); if (!PyErr_GivenExceptionMatches(err_type, pygpgme_error)) goto end; res = gpgme_op_decrypt_result(self->ctx); if (res == NULL) goto end; if (res->unsupported_algorithm) { value = PyUnicode_DecodeUTF8(res->unsupported_algorithm, strlen(res->unsupported_algorithm), "replace"); } else { Py_INCREF(Py_None); value = Py_None; } if (value) { PyObject_SetAttrString(err_value, "unsupported_algorithm", value); Py_DECREF(value); } value = PyBool_FromLong(res->wrong_key_usage); if (value) { PyObject_SetAttrString(err_value, "wrong_key_usage", value); Py_DECREF(value); } end: PyErr_Restore(err_type, err_value, err_traceback); }
static BSON_INLINE void cbson_loads_set_item (PyObject *obj, const char *key, PyObject *value) { PyObject *keyobj; if (PyDict_Check(obj)) { if (*key == '_' && !strcmp(key, "_id")) { keyobj = gStr_id; Py_INCREF(keyobj); } else if (!(keyobj = PyUnicode_DecodeUTF8(key, strlen(key), "strict"))) { keyobj = PyString_FromString(key); } if (keyobj) { PyDict_SetItem(obj, keyobj, value); Py_DECREF(keyobj); } } else { PyList_Append(obj, value); } }
static PyObject* usbobserver_find_prop(io_registry_entry_t e, CFStringRef key, int is_string ) { char buf[500]; long val = 0; PyObject *ans; IOOptionBits bits = kIORegistryIterateRecursively | kIORegistryIterateParents; CFTypeRef PropRef = IORegistryEntrySearchCFProperty( e, kIOServicePlane, key, NULL, bits ); if (!PropRef) return NULL; buf[0] = '\0'; if(is_string) { CFStringGetCString(PropRef, buf, 500, kCFStringEncodingUTF8); ans = PyUnicode_DecodeUTF8(buf, strlen(buf), "replace"); } else { CFNumberGetValue((CFNumberRef)PropRef, kCFNumberLongType, &val); ans = PyLong_FromLong(val); } CFRelease(PropRef); return ans; }
PyObject* PyCOMPS_toxml_str(PyObject *self, PyObject *args, PyObject *kwds) { PyObject *ret; const char *errors = NULL; COMPS_XMLOptions *xml_options = NULL; COMPS_DefaultsOptions *def_options = NULL; char* keywords[] = {"xml_options", "def_options",NULL}; if (PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&", keywords, __pycomps_dict_to_xml_opts, &xml_options, __pycomps_dict_to_def_opts, &def_options)) { } else { return NULL; } char *s = comps2xml_str(((PyCOMPS*)self)->comps_doc, xml_options, def_options); if (xml_options) free(xml_options); if (def_options) free(def_options); ret = PyUnicode_DecodeUTF8(s, strlen(s), errors); free(s); return ret; }
/* Verify that the identifier follows PEP 3131. All identifier strings are guaranteed to be "ready" unicode objects. */ static int verify_identifier(struct tok_state *tok) { PyObject *s; int result; if (tok->decoding_erred) return 0; s = PyUnicode_DecodeUTF8(tok->start, tok->cur - tok->start, NULL); if (s == NULL || PyUnicode_READY(s) == -1) { if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) { PyErr_Clear(); tok->done = E_IDENTIFIER; } else { tok->done = E_ERROR; } return 0; } result = PyUnicode_IsIdentifier(s); Py_DECREF(s); if (result == 0) tok->done = E_IDENTIFIER; return result; }
//------------------------------------------------------------------------------------- void PythonApp::onExecScriptCommand(Network::Channel* pChannel, KBEngine::MemoryStream& s) { if(pChannel->isExternal()) return; std::string cmd; s.readBlob(cmd); PyObject* pycmd = PyUnicode_DecodeUTF8(cmd.data(), cmd.size(), NULL); if(pycmd == NULL) { SCRIPT_ERROR_CHECK(); return; } DEBUG_MSG(fmt::format("PythonApp::onExecScriptCommand: size({}), command={}.\n", cmd.size(), cmd)); std::string retbuf = ""; PyObject* pycmd1 = PyUnicode_AsEncodedString(pycmd, "utf-8", NULL); script_.run_simpleString(PyBytes_AsString(pycmd1), &retbuf); if(retbuf.size() == 0) { retbuf = "\r\n"; } // 将结果返回给客户端 Network::Bundle* pBundle = Network::Bundle::createPoolObject(); ConsoleInterface::ConsoleExecCommandCBMessageHandler msgHandler; (*pBundle).newMessage(msgHandler); ConsoleInterface::ConsoleExecCommandCBMessageHandlerArgs1::staticAddToBundle((*pBundle), retbuf); pChannel->send(pBundle); Py_DECREF(pycmd); Py_DECREF(pycmd1); }
static PyObject * marshal_Load_internal(PyObject *py_stream, PyObject *py_callback, int skipcrc) { // Return value: New Reference char *stream; Py_ssize_t size; char *s; char *end; int type = -1; // current object type int shared = -1; // indicates whether current object is shared int i; char *error = "NO ERROR SPECIFIED"; char errortext[256]; Py_ssize_t length = 0; // generic length value. int shared_mapsize; int shared_count; // shared object index counter int *shared_map; // points to shared object mapping at end of stream PyObject **shared_obj = NULL; // holds the shared objects PyObject *obj = NULL; // currently decoded object PyObject *result = NULL; // final result int ct_ix = 0; struct Container ct_stack[MAX_DEPTH]; struct Container *container = &ct_stack[0]; if(PyString_AsStringAndSize(py_stream, &stream, &size) == -1) return NULL; s = stream; container->obj = NULL; container->type = 0; container->free = -1; container->index = 0; if(size < 6 || *s++ != PROTOCOL_ID) { int offset = 0; result = unpickle(py_stream, &offset); if(!result) goto cleanup; return result; } // how many shared objects in this stream? shared_mapsize = *(int32_t *)s; s += 4; // Security Check: assert there is enough data for that many items. if((5 + shared_mapsize*4) > size) { PyErr_Format(UnmarshalError, "Not enough room in stream for map. Wanted %d, but have only %d bytes remaining...", (shared_mapsize*4), ((int)size-5)); goto cleanup; } // ok, we got the map data right here... shared_map = (int32_t *)&stream[size - shared_mapsize * 4]; // Security Check #2: assert all map entries are between 1 and shared_mapsize for(i=0; i<shared_mapsize; i++) { if( (shared_map[i] > shared_mapsize) || (shared_map[i] < 1) ) { PyErr_SetString(UnmarshalError, "Bogus map data in marshal stream"); goto cleanup; } } // the start of which is incidentally also the end of the object data. end = (char *)shared_map; // create object table shared_obj = PyMem_MALLOC(shared_mapsize * sizeof(PyObject *)); if(!shared_obj) goto cleanup; // zero out object table for(shared_count = 0; shared_count < shared_mapsize; shared_count++) shared_obj[shared_count] = NULL; shared_count = 0; // start decoding. while(s < end) { // This outer loop is responsible for reading and decoding the next // object from the stream. The object is then handed to the inner loop, // which adds it to the current container, or returns it to the caller. // get type of next object to decode and check shared flag type = *s++; shared = type & SHARED_FLAG; type &= ~SHARED_FLAG; // if token uses a normal length value, read it now. if(needlength[type]) { READ_LENGTH; } else length = 0; #if MARSHAL_DEBUG // if(shared) { char text[220]; DEBUG_INDENT; sprintf(text, "pos:%4d type:%s(0x%02x) shared:%d len:%4d map:[", s-stream, tokenname[type], type, shared?1:0, length); printf(text); for(i=0; i<shared_mapsize; i++) printf("%d(%d),", shared_obj[i], shared_obj[i] ? ((PyObject *)(shared_obj[i]))->ob_refcnt : 0); printf("]\r\n"); } #endif // MARSHAL_DEBUG switch(type) { // // break statement: // attempts to add the newly decoded object (in the obj variable) to // the currently building container object. // // continue statement: // indicates the decoded object or type marker was handled/consumed // by the case and should _not_ be added to the currently building // container object or used in any other way; immediately decode a // new object // //--------------------------------------------------------------------- // SCALAR TYPES //--------------------------------------------------------------------- case TYPE_INT8: CHECK_SIZE(1); obj = PyInt_FromLong(*(int8_t *)s); s++; break; case TYPE_INT16: CHECK_SIZE(2); obj = PyInt_FromLong(*(int16_t *)s); s += 2; break; case TYPE_INT32: CHECK_SIZE(4); obj = PyInt_FromLong(*(int32_t *)s); s += 4; break; case TYPE_INT64: CHECK_SIZE(8); obj = PyLong_FromLongLong(*(int64_t *)s); s += 8; break; case TYPE_LONG: CHECK_SIZE(length); if(!length) obj = PyLong_FromLong(0); else { obj = _PyLong_FromByteArray((unsigned char *)s, length, 1, 1); Py_INCREF(obj); } CHECK_SHARED(obj); s += length; break; case TYPE_FLOAT: CHECK_SIZE(8); obj = PyFloat_FromDouble(*(double *)s); s += 8; break; case TYPE_CHECKSUM: CHECK_SIZE(4); if(!skipcrc && (*(uint32_t *)s != (uint32_t)adler32(1, s, (unsigned long)(end-s)))) { error = "checksum error"; goto fail; } s += 4; // because this type does not yield an object, go grab another // object right away! continue; //--------------------------------------------------------------------- // STRING TYPES //--------------------------------------------------------------------- case TYPE_STRINGR: if (length < 1 || length >= PyList_GET_SIZE(string_table)) { if(PyList_GET_SIZE(string_table)) PyErr_Format(UnmarshalError, "Invalid string table index %d", (int)length); else PyErr_SetString(PyExc_RuntimeError, "_stringtable not initialized"); goto cleanup; } obj = PyList_GET_ITEM(string_table, length); Py_INCREF(obj); break; case TYPE_STRING: // appears to be deprecated since machoVersion 213 CHECK_SIZE(1); length = *(unsigned char *)s++; CHECK_SIZE(length); obj = PyString_FromStringAndSize(s, length); s += length; break; case TYPE_STRING1: CHECK_SIZE(1); obj = PyString_FromStringAndSize(s, 1); s++; break; case TYPE_STREAM: // fallthrough, can be treated as string. case TYPE_STRINGL: // fallthrough, deprecated since machoVersion 213 case TYPE_BUFFER: // Type identifier re-used by CCP. treat as string. CHECK_SIZE(length); obj = PyString_FromStringAndSize(s, length); s += length; CHECK_SHARED(obj); break; case TYPE_UNICODE1: CHECK_SIZE(2); #ifdef Py_UNICODE_WIDE obj = _PyUnicodeUCS4_FromUCS2((void *)s, 1); #else obj = PyUnicode_FromWideChar((wchar_t *)s, 1); #endif s += 2; break; case TYPE_UNICODE: CHECK_SIZE(length*2); #ifdef Py_UNICODE_WIDE obj = _PyUnicodeUCS4_FromUCS2((void *)s, (int)length); #else obj = PyUnicode_FromWideChar((wchar_t *)s, length); #endif s += length*2; break; case TYPE_UTF8: CHECK_SIZE(length); obj = PyUnicode_DecodeUTF8(s, length, NULL); s += length; break; //--------------------------------------------------------------------- // SEQUENCE/MAPPING TYPES //--------------------------------------------------------------------- case TYPE_TUPLE1: NEW_SEQUENCE(TYPE_TUPLE, 1); continue; case TYPE_TUPLE2: NEW_SEQUENCE(TYPE_TUPLE, 2); continue; case TYPE_TUPLE: NEW_SEQUENCE(TYPE_TUPLE, (int)length); continue; case TYPE_LIST0: obj = PyList_New(0); CHECK_SHARED(obj); break; case TYPE_LIST1: NEW_SEQUENCE(TYPE_LIST, 1); continue; case TYPE_LIST: NEW_SEQUENCE(TYPE_LIST, (int)length); continue; case TYPE_DICT: if(length) { CHECK_SIZE(length*2); PUSH_CONTAINER(TYPE_DICT, (int)length*2); container->obj = PyDict_New(); container->obj2 = NULL; container->index = 0; CHECK_SHARED(container->obj); continue; } else { obj = PyDict_New(); CHECK_SHARED(obj); break; } //--------------------------------------------------------------------- // OBJECT TYPES //--------------------------------------------------------------------- case TYPE_REF: // length value is index in sharedobj array! if((length < 1 || length > shared_mapsize)) { error = "Shared reference index out of range"; goto fail; } if(!(obj = shared_obj[length-1])) { error = "Shared reference points to invalid object"; goto fail; } Py_INCREF(obj); //printf("Getting object %d from %d (refs:%d)\r\n", (int)obj, length-1, obj->ob_refcnt); break; case TYPE_GLOBAL: { PyObject *name; CHECK_SIZE(length); name = PyString_FromStringAndSize(s, length); if(!name) goto cleanup; s += length; if(!(obj = find_global(name))) { // exception should be set by find_global goto cleanup; } Py_DECREF(name); CHECK_SHARED(obj); break; } case TYPE_DBROW: case TYPE_INSTANCE: case TYPE_NEWOBJ: case TYPE_REDUCE: PUSH_CONTAINER(type, -1); container->obj = NULL; RESERVE_SLOT(container->index); continue; case TYPE_MARK: // this is a marker, not a real object. list/dict iterators check // for this type, but it can't be instantiated. break; default: if((obj = constants[type])) { Py_INCREF(obj); } else { error = "Unsupported type"; goto fail; } } // object decoding and construction done! if(!obj && type != TYPE_MARK) { // if obj is somehow NULL, whatever caused it is expected to have // set an exception at this point. goto cleanup; } #if MARSHAL_DEBUG /* if(obj && obj->ob_refcnt < 0) { char b[200]; sprintf(b, "type: %d, refcount: %d", type, obj->ob_refcnt); DEBUG(b); } */ if(obj) { DEBUG_INDENT; printf("`-- "); PyObject_Print(obj, stdout, 0); printf("\r\n"); fflush(stdout); } else { DEBUG_INDENT; printf("*** MARK\r\n"); } #endif // MARSHAL_DEBUG while(1) { // This inner loop does one of two things: // // - return the finished object to the caller if we're at the root // container. // // - add the object to the current container in a container- // specific manner. note that ownership of the reference is to be // given to the container object. #if MARSHAL_DEBUG { //char text[220]; DEBUG_INDENT; printf("container ix:%d (%08lx) type:%s[0x%02x] free:%d index:%d\r\n", ct_ix, container->obj, tokenname[container->type], container->type, container->free, container->index); } #endif // MARSHAL_DEBUG /* if(!container->obj) { error = "Root container popped off stack"; goto fail; } */ switch(container->type) { case TYPE_TUPLE: // tuples steal references. PyTuple_SET_ITEM(container->obj, container->index++, obj); break; case TYPE_LIST: // lists steal references. PyList_SET_ITEM(container->obj, container->index++, obj); break; case TYPE_DBROW: if(container->obj) { // we have an initialized DBRow. current object is a // non-scalar object for the row. append it. if(!dbrow_append_internal((PyDBRowObject *)container->obj, obj)) { // append call will have set an exception here. goto cleanup; } } else { // we now have a DBRowDescriptor, and the header data // should follow. Pull it and create the DBRow. READ_LENGTH; CHECK_SIZE(length); container->obj = PyDBRow_New((PyDBRowDescriptorObject *)obj, s, (int)length); container->free = 1+((PyDBRowDescriptorObject *)obj)->rd_num_objects; if(!container->obj) goto cleanup; Py_DECREF(obj); s += length; // add as shared object, if neccessary... UPDATE_SLOT(container->index, container->obj); } break; case TYPE_INSTANCE: { PyObject *cls; if(container->free == -1) { // create class instance if(!(cls = find_global(obj))) goto cleanup; container->obj = PyInstance_NewRaw(cls, 0); Py_DECREF(cls); if(!container->obj) goto cleanup; UPDATE_SLOT(container->index, container->obj); Py_DECREF(obj); break; } if(container->free == -2) { container->free = 1; // set state. if(!set_state(container->obj, obj)) goto cleanup; Py_DECREF(obj); break; } error = "invalid container state"; goto fail; } case TYPE_NEWOBJ: { PyObject *cls, *args, *__new__, *state; // instantiate the object... if(!(args = PyTuple_GetItem(obj, 0))) goto cleanup; if(!(cls = PyTuple_GetItem(args, 0))) goto cleanup; __new__ = PyObject_GetAttr(cls, py__new__); if(!__new__) goto cleanup; container->obj = PyObject_CallObject(__new__, args); Py_DECREF(__new__); if(!container->obj) goto cleanup; // add as shared object, if neccessary... UPDATE_SLOT(container->index, container->obj); // is there state data? if(PyTuple_GET_SIZE(obj) > 1) { state = PyTuple_GET_ITEM(obj, 1); if(!set_state(container->obj, state)) goto cleanup; } Py_DECREF(obj); // switch to list iterator LIST_ITERATOR; break; } case TYPE_REDUCE: { PyObject *callable, *args, *state; if(!(args = PyTuple_GetItem(obj, 1))) goto cleanup; if(!(callable = PyTuple_GET_ITEM(obj, 0))) goto cleanup; if(!(container->obj = PyObject_CallObject(callable, args))) goto cleanup; UPDATE_SLOT(container->index, container->obj); if(PyTuple_GET_SIZE(obj) > 2) { state = PyTuple_GET_ITEM(obj, 2); if(!set_state(container->obj, state)) goto cleanup; } Py_DECREF(obj); // switch to list iterator LIST_ITERATOR; break; } case TYPE_LIST_ITERATOR: if(type == TYPE_MARK) { // clear mark so nested iterator containers do not get terminated prematurely. type = -1; // decref the append method Py_XDECREF(container->obj2); container->obj2 = NULL; container->type = TYPE_DICT_ITERATOR; break; } if(!container->obj2) { // grab the append method from the container and keep // it around for speed. if(!(container->obj2 = PyObject_GetAttr(container->obj, pyappend))) goto cleanup; } if(!PyObject_CallFunctionObjArgs(container->obj2, obj, NULL)) goto cleanup; #if MARSHAL_DEBUG DEBUG_INDENT; printf("Appended %08lx to %08lx\r\n", obj, container->obj); #endif // MARSHAL_DEBUG Py_DECREF(obj); break; case TYPE_DICT_ITERATOR: if(type == TYPE_MARK) { // clear mark so nested iterator containers do not get terminated prematurely. type = -1; // we're done with dict iter. container is finished. container->free = 1; break; } POPULATE_DICT(container->obj2, obj); break; case TYPE_DICT: POPULATE_DICT(obj, container->obj2); break; case 0: // we're at the root. return the object to caller. result = obj; // avoid decreffing this object. obj = NULL; goto cleanup; } container->free--; if(container->free) // still room in this container. // break out of container handling to get next object for it. break; // this container is done, it is the next object to put into the // container under it! obj = container->obj; // switch context to said older container POP_CONTAINER; } // we've processed the object. clear it for next one. obj = NULL; } // if we get here, we're out of data, but it's a "clean" eof; we ran out // of data while expecting a new object... error = "Not enough objects in stream"; fail: PyErr_Format(UnmarshalError, "%s - type:0x%02x ctype:0x%02x len:%d share:%d pos:%d size:%d", error, type, container->type, (int)length, shared, (int)(s-stream), (int)(size)); cleanup: // on any error the current object we were working on will be unassociated // with anything, decref it. if decode was succesful or an object failed to // be created, it will be NULL anyway. Py_XDECREF(obj); // same story for containers... while(container->type) { Py_XDECREF(container->obj); // possibly unassociated object for dict entry? if(container->type == TYPE_DICT || container->type == TYPE_DICT_ITERATOR) { Py_XDECREF(container->obj2); } POP_CONTAINER; } if(shared_obj) { /* shared object list held a safety ref to all objects, decref em */ int i; for(i=0; i<shared_mapsize; i++) Py_XDECREF(shared_obj[i]); /* and free the list */ PyMem_FREE(shared_obj); } return result; }
static PyObject *AE_AddressDescToPath(PyObject *_self, PyObject *_args) { AEDesc desc; ProcessSerialNumber psn; pid_t pid; OSType creatorType = kLSUnknownCreator; Size cSize; char *cStr; CFStringRef bundleID = NULL; FSRef fsref; UInt8 path[PATH_MAX]; PyObject* pathObj; OSStatus err; if (!PyArg_ParseTuple(_args, "O&", AE_AEDesc_Convert, &desc)) return NULL; switch (desc.descriptorType) { case typeKernelProcessID: err = AEGetDescData(&desc, &pid, sizeof(pid)); if (err) return AE_MacOSError(err); err = GetProcessForPID(pid, &psn); if (err) return AE_MacOSError(err); err = GetProcessBundleLocation(&psn, &fsref); if (err) return AE_MacOSError(err); break; case typeProcessSerialNumber: err = AEGetDescData(&desc, &psn, sizeof(psn)); if (err) return AE_MacOSError(err); err = GetProcessBundleLocation(&psn, &fsref); if (err) return AE_MacOSError(err); break; case typeApplSignature: err = AEGetDescData(&desc, &creatorType, sizeof(creatorType)); if (err) return AE_MacOSError(err); err = LSFindApplicationForInfo(creatorType, bundleID, NULL, &fsref, NULL); if (err) return AE_MacOSError(err); break; case typeApplicationBundleID: cSize = AEGetDescDataSize(&desc); cStr = malloc((size_t)cSize); if (!cStr) return AE_MacOSError(errAECoercionFail); err = AEGetDescData(&desc, cStr, cSize); if (err) return AE_MacOSError(err); bundleID = CFStringCreateWithBytes(NULL, (UInt8 *)cStr, (CFIndex)cSize, kCFStringEncodingUTF8, 0); free(cStr); if (!bundleID) return AE_MacOSError(errAECoercionFail); err = LSFindApplicationForInfo(creatorType, bundleID, NULL, &fsref, NULL); if (err) return AE_MacOSError(err); break; case typeMachPort: // unsupported case typeApplicationURL: // unsupported (remote applications) default: return AE_MacOSError(errAECoercionFail); } err = FSRefMakePath(&fsref, path, sizeof(path)); if (err) return AE_MacOSError(err); pathObj = PyUnicode_DecodeUTF8((char *)path, strlen((char *)path), NULL); return Py_BuildValue("O", pathObj); }
// Returns a new reference. static PyObject* decode_val(DecodeBuffer* input, TType type, PyObject* typeargs, long string_limit, long container_limit) { switch (type) { case T_BOOL: { int8_t v = readByte(input); if (INT_CONV_ERROR_OCCURRED(v)) { return NULL; } switch (v) { case 0: Py_RETURN_FALSE; case 1: Py_RETURN_TRUE; // Don't laugh. This is a potentially serious issue. default: PyErr_SetString(PyExc_TypeError, "boolean out of range"); return NULL; } break; } case T_I08: { int8_t v = readByte(input); if (INT_CONV_ERROR_OCCURRED(v)) { return NULL; } return PyInt_FromLong(v); } case T_I16: { int16_t v = readI16(input); if (INT_CONV_ERROR_OCCURRED(v)) { return NULL; } return PyInt_FromLong(v); } case T_I32: { int32_t v = readI32(input); if (INT_CONV_ERROR_OCCURRED(v)) { return NULL; } return PyInt_FromLong(v); } case T_I64: { int64_t v = readI64(input); if (INT_CONV_ERROR_OCCURRED(v)) { return NULL; } // TODO(dreiss): Find out if we can take this fastpath always when // sizeof(long) == sizeof(long long). if (CHECK_RANGE(v, LONG_MIN, LONG_MAX)) { return PyInt_FromLong((long) v); } return PyLong_FromLongLong(v); } case T_DOUBLE: { double v = readDouble(input); if (v == -1.0 && PyErr_Occurred()) { return false; } return PyFloat_FromDouble(v); } case T_STRING: { Py_ssize_t len = readI32(input); char* buf; if (!readBytes(input, &buf, len)) { return NULL; } if (!check_length_limit(len, string_limit)) { return NULL; } if (is_utf8(typeargs)) return PyUnicode_DecodeUTF8(buf, len, 0); else return PyString_FromStringAndSize(buf, len); } case T_LIST: case T_SET: { SetListTypeArgs parsedargs; int32_t len; PyObject* ret = NULL; int i; bool use_tuple = false; if (!parse_set_list_args(&parsedargs, typeargs)) { return NULL; } if (!checkTypeByte(input, parsedargs.element_type)) { return NULL; } len = readI32(input); if (!check_length_limit(len, container_limit)) { return NULL; } use_tuple = type == T_LIST && parsedargs.immutable; ret = use_tuple ? PyTuple_New(len) : PyList_New(len); if (!ret) { return NULL; } for (i = 0; i < len; i++) { PyObject* item = decode_val(input, parsedargs.element_type, parsedargs.typeargs, string_limit, container_limit); if (!item) { Py_DECREF(ret); return NULL; } if (use_tuple) { PyTuple_SET_ITEM(ret, i, item); } else { PyList_SET_ITEM(ret, i, item); } } // TODO(dreiss): Consider biting the bullet and making two separate cases // for list and set, avoiding this post facto conversion. if (type == T_SET) { PyObject* setret; setret = parsedargs.immutable ? PyFrozenSet_New(ret) : PySet_New(ret); Py_DECREF(ret); return setret; } return ret; } case T_MAP: { int32_t len; int i; MapTypeArgs parsedargs; PyObject* ret = NULL; if (!parse_map_args(&parsedargs, typeargs)) { return NULL; } if (!checkTypeByte(input, parsedargs.ktag)) { return NULL; } if (!checkTypeByte(input, parsedargs.vtag)) { return NULL; } len = readI32(input); if (!check_length_limit(len, container_limit)) { return NULL; } ret = PyDict_New(); if (!ret) { goto error; } for (i = 0; i < len; i++) { PyObject* k = NULL; PyObject* v = NULL; k = decode_val(input, parsedargs.ktag, parsedargs.ktypeargs, string_limit, container_limit); if (k == NULL) { goto loop_error; } v = decode_val(input, parsedargs.vtag, parsedargs.vtypeargs, string_limit, container_limit); if (v == NULL) { goto loop_error; } if (PyDict_SetItem(ret, k, v) == -1) { goto loop_error; } Py_DECREF(k); Py_DECREF(v); continue; // Yuck! Destructors, anyone? loop_error: Py_XDECREF(k); Py_XDECREF(v); goto error; } if (parsedargs.immutable) { PyObject* thrift = PyImport_ImportModule("thrift.Thrift"); PyObject* cls = NULL; PyObject* arg = NULL; if (!thrift) { goto error; } cls = PyObject_GetAttrString(thrift, "TFrozenDict"); if (!cls) { goto error; } arg = PyTuple_New(1); PyTuple_SET_ITEM(arg, 0, ret); return PyObject_CallObject(cls, arg); } return ret; error: Py_XDECREF(ret); return NULL; } case T_STRUCT: { StructTypeArgs parsedargs; if (!parse_struct_args(&parsedargs, typeargs)) { return NULL; } return decode_struct(input, Py_None, parsedargs.klass, parsedargs.spec, string_limit, container_limit); } case T_STOP: case T_VOID: case T_UTF16: case T_UTF8: case T_U64: default: PyErr_SetString(PyExc_TypeError, "Unexpected TType"); return NULL; } }
static PyObject *py_normalize_token(PyObject *self, PyObject *args) { PyObject *s; uint32_t offset; uint32_t len; uint16_t type; uint64_t options; if (!PyArg_ParseTuple(args, "O(IIH)K:normalize", &s, &offset, &len, &type, &options)) { PyErr_SetString(PyExc_TypeError, "Error parsing arguments"); return 0; } token_t token = (token_t){(size_t)offset, (size_t)len, type}; PyObject *unistr = PyUnicode_FromObject(s); if (unistr == NULL) { PyErr_SetString(PyExc_TypeError, "Parameter could not be converted to unicode in scanner"); return 0; } #ifdef IS_PY3K // Python 3 encoding, supported by Python 3.3+ char *input = PyUnicode_AsUTF8(unistr); #else // Python 2 encoding PyObject *str = PyUnicode_AsEncodedString(unistr, "utf-8", "strict"); if (str == NULL) { PyErr_SetString(PyExc_ValueError, "Parameter could not be utf-8 encoded"); goto exit_decref_unistr; } char *input = PyBytes_AsString(str); #endif if (input == NULL) { goto exit_decref_str; } char_array *token_buffer = char_array_new_size(token.len); add_normalized_token(token_buffer, input, token, options); char *token_str = char_array_get_string(token_buffer); PyObject *result = PyUnicode_DecodeUTF8((const char *)token_str, token_buffer->n - 1, "strict"); if (result == NULL) { PyErr_SetString(PyExc_ValueError, "Error decoding token"); char_array_destroy(token_buffer); goto exit_decref_str; } char_array_destroy(token_buffer); #ifndef IS_PY3K Py_XDECREF(str); #endif Py_XDECREF(unistr); return result; exit_decref_str: #ifndef IS_PY3K Py_XDECREF(str); #endif exit_decref_unistr: Py_XDECREF(unistr); return 0; }
static PyObject *py_normalize_string_latin(PyObject *self, PyObject *args) { PyObject *arg1; uint64_t options; if (!PyArg_ParseTuple(args, "OK:normalize", &arg1, &options)) { return 0; } PyObject *unistr = PyUnicode_FromObject(arg1); if (unistr == NULL) { PyErr_SetString(PyExc_TypeError, "Parameter could not be converted to unicode in scanner"); return 0; } #ifdef IS_PY3K // Python 3 encoding, supported by Python 3.3+ char *input = PyUnicode_AsUTF8(unistr); #else // Python 2 encoding PyObject *str = PyUnicode_AsEncodedString(unistr, "utf-8", "strict"); if (str == NULL) { PyErr_SetString(PyExc_TypeError, "Parameter could not be utf-8 encoded"); goto exit_decref_unistr; } char *input = PyBytes_AsString(str); #endif if (input == NULL) { goto exit_decref_str; } char *normalized = normalize_string_latin(input, strlen(input), options); PyObject *result = PyUnicode_DecodeUTF8((const char *)normalized, strlen(normalized), "strict"); free(normalized); if (result == NULL) { PyErr_SetString(PyExc_ValueError, "Result could not be utf-8 decoded"); goto exit_decref_str; } #ifndef IS_PY3K Py_XDECREF(str); #endif Py_XDECREF(unistr); return result; exit_decref_str: #ifndef IS_PY3K Py_XDECREF(str); #endif exit_decref_unistr: Py_XDECREF(unistr); return 0; }
/* Globs filenames according to the EWF segment file naming schema * Returns a Python object if successful or NULL on error */ PyObject *pyewf_glob( PyObject *self, PyObject *arguments, PyObject *keywords ) { char error_string[ PYEWF_ERROR_STRING_SIZE ]; char **filenames = NULL; liberror_error_t *error = NULL; PyObject *list_object = NULL; PyObject *string_object = NULL; static char *function = "pyewf_glob"; static char *keyword_list[] = { "filename", NULL }; const char *errors = NULL; const char *filename = NULL; size_t filename_length = 0; int filename_index = 0; int number_of_filenames = 0; if( PyArg_ParseTupleAndKeywords( arguments, keywords, "|s", keyword_list, &filename ) == 0 ) { return( NULL ); } filename_length = libcstring_narrow_string_length( filename ); if( libewf_glob( filename, filename_length, LIBEWF_FORMAT_UNKNOWN, &filenames, &number_of_filenames, &error ) != 1 ) { if( liberror_error_backtrace_sprint( error, error_string, PYEWF_ERROR_STRING_SIZE ) == -1 ) { PyErr_Format( PyExc_IOError, "%s: unable to glob filenames.", function ); } else { PyErr_Format( PyExc_IOError, "%s: unable to glob filenames.\n%s", function, error_string ); } liberror_error_free( &error ); return( NULL ); } list_object = PyList_New( (Py_ssize_t) number_of_filenames ); for( filename_index = 0; filename_index < number_of_filenames; filename_index++ ) { filename_length = libcstring_narrow_string_length( filenames[ filename_index ] ); string_object = PyUnicode_DecodeUTF8( filenames[ filename_index ], filename_length, errors ); if( string_object == NULL ) { PyErr_Format( PyExc_IOError, "%s: unable to convert UTF-8 filename: %d into Unicode.", function, filename_index ); libewf_glob_free( filenames, number_of_filenames, NULL ); Py_DecRef( list_object ); return( NULL ); } if( PyList_SetItem( list_object, (Py_ssize_t) filename_index, string_object ) != 0 ) { PyErr_Format( PyExc_MemoryError, "%s: unable to set filename: %d in list.", function, filename_index ); libewf_glob_free( filenames, number_of_filenames, NULL ); Py_DecRef( string_object ); Py_DecRef( list_object ); return( NULL ); } } if( libewf_glob_free( filenames, number_of_filenames, &error ) != 1 ) { if( liberror_error_backtrace_sprint( error, error_string, PYEWF_ERROR_STRING_SIZE ) == -1 ) { PyErr_Format( PyExc_MemoryError, "%s: unable to free globbed filenames.", function ); } else { PyErr_Format( PyExc_MemoryError, "%s: unable to free globbed filenames.\n%s", function, error_string ); } liberror_error_free( &error ); Py_DecRef( list_object ); return( NULL ); } return( list_object ); }
PyObject* PyWeave_LogIter_tp_iternext(PyWeave_LogIterObject* self) { FILE* logfile = PyFile_AsFile(self->log->logfile); if(logfile && !fseek(logfile, sizeof(Weave::Log::Header) + self->offset, SEEK_SET)) { uint8_t packed_typecode[2]; if(fread(packed_typecode, sizeof(packed_typecode), 1, logfile) != 1) { self->offset = 0; return NULL; } Weave::Log::TypeCode typecode = (Weave::Log::TypeCode)(packed_typecode[0] | (packed_typecode[1] << 8)); uint8_t packed_size[4]; if(fread(packed_size, sizeof(packed_size), 1, logfile) != 1) { self->offset = 0; return NULL; } size_t size = packed_size[0] | (packed_size[1] << 8) | (packed_size[2] << 16) | (packed_size[3] << 24); self->offset += size + 6; if(typecode == Weave::Log::TC_CLIENT_MESSAGE || typecode == Weave::Log::TC_SERVER_MESSAGE) { PyWeave_MessageObject* msg = NULL; if(typecode == Weave::Log::TC_CLIENT_MESSAGE) msg = PyObject_New(PyWeave_MessageObject, &PyWeave_ClientMessageType); else if(typecode == Weave::Log::TC_SERVER_MESSAGE) msg = PyObject_New(PyWeave_MessageObject, &PyWeave_ServerMessageType); if(!msg) return NULL; Weave::Log::Message msg_header; if(fread(&msg_header, sizeof(msg_header), 1, logfile) != 1) { self->offset = 0; Py_DECREF(msg); return NULL; } msg->opcode = msg_header.opcode[0] | (msg_header.opcode[1] << 8) | (msg_header.opcode[2] << 16) | (msg_header.opcode[3] << 24); msg->data_length = size - sizeof(msg_header); if(msg->data_length) { msg->data = (char*)PyMem_Malloc(msg->data_length); fread(msg->data, msg->data_length, 1, logfile); } else { msg->data = NULL; } return (PyObject*)msg; } else if(typecode == Weave::Log::TC_SESSION_INFO) { PyWeave_SessionInfoObject* info_obj = PyObject_New(PyWeave_SessionInfoObject, &PyWeave_SessionInfoType); if(!info_obj) return NULL; if(fread(&(info_obj->info), sizeof(info_obj->info), 1, logfile) != 1) { self->offset = 0; Py_DECREF(info_obj); return NULL; } size_t account_length = size - sizeof(info_obj->info); if(account_length) { char* buffer = new char[account_length]; if(fread(buffer, account_length, 1, logfile) != 1) { self->offset = 0; Py_DECREF(info_obj); delete buffer; return NULL; } info_obj->account = PyUnicode_DecodeUTF8(buffer, account_length - 1, "strict"); if(!info_obj->account) { self->offset = 0; Py_DECREF(info_obj); delete buffer; return NULL; } delete buffer; } else { info_obj->account = NULL; } return (PyObject*)info_obj; } Py_RETURN_NONE; } else { self->offset = 0; } return NULL; }
/* Retrieves the MD5 hash * Returns a Python object holding the offset if successful or NULL on error */ PyObject *pyewf_file_entry_get_hash_value_md5( pyewf_file_entry_t *pyewf_file_entry ) { char error_string[ PYEWF_ERROR_STRING_SIZE ]; libcerror_error_t *error = NULL; PyObject *string_object = NULL; const char *errors = NULL; uint8_t *hash_value = NULL; static char *function = "pyewf_file_entry_get_hash_value_md5"; size_t hash_value_size = 33; int result = 0; if( pyewf_file_entry == NULL ) { PyErr_Format( PyExc_TypeError, "%s: invalid file entry.", function ); return( NULL ); } hash_value = (uint8_t *) PyMem_Malloc( sizeof( uint8_t ) * hash_value_size ); if( hash_value == NULL ) { PyErr_Format( PyExc_IOError, "%s: unable to create hash value.", function ); goto on_error; } Py_BEGIN_ALLOW_THREADS result = libewf_file_entry_get_utf8_hash_value_md5( pyewf_file_entry->file_entry, hash_value, hash_value_size, &error ); Py_END_ALLOW_THREADS if( result != 1 ) { if( libcerror_error_backtrace_sprint( error, error_string, PYEWF_ERROR_STRING_SIZE ) == -1 ) { PyErr_Format( PyExc_IOError, "%s: unable to retrieve hash value MD5.", function ); } else { PyErr_Format( PyExc_IOError, "%s: unable to retrieve hash value MD5.\n%s", function, error_string ); } libcerror_error_free( &error ); goto on_error; } /* Pass the string length to PyUnicode_DecodeUTF8 * otherwise it makes the end of string character is part * of the string */ string_object = PyUnicode_DecodeUTF8( (char *) hash_value, (Py_ssize_t) hash_value_size - 1, errors ); PyMem_Free( hash_value ); return( string_object ); on_error: if( hash_value != NULL ) { PyMem_Free( hash_value ); } return( NULL ); }
static PyObject *DecodeString (UMTypeInfo *ti, char *value, size_t cbValue) { //FIXME: This code must be endiness aware of system isn't little endian switch (ti->charset) { case MCS_big5_chinese_ci://1, case MCS_big5_bin://84, case MCS_dec8_swedish_ci://3, case MCS_dec8_bin://69, case MCS_cp850_general_ci://4, case MCS_cp850_bin://80, case MCS_hp8_english_ci://6, case MCS_hp8_bin://72, case MCS_koi8r_general_ci://7, case MCS_koi8r_bin://74, break; case MCS_latin1_german1_ci://5, case MCS_latin1_swedish_ci://8, case MCS_latin1_danish_ci://15, case MCS_latin1_german2_ci://31, case MCS_latin1_bin://47, case MCS_latin1_general_ci://48, case MCS_latin1_general_cs://49, case MCS_latin1_spanish_ci://94, return PyUnicode_DecodeLatin1 (value, cbValue, NULL); case MCS_latin2_czech_cs:// 2, case MCS_latin2_general_ci://9, case MCS_latin2_hungarian_ci://21, case MCS_latin2_croatian_ci://27, case MCS_latin2_bin://77, case MCS_swe7_swedish_ci://10, case MCS_swe7_bin://82, break; case MCS_ascii_general_ci://11, case MCS_ascii_bin://65, return PyUnicode_DecodeASCII(value, cbValue, NULL); case MCS_ujis_japanese_ci://12, case MCS_ujis_bin://91, case MCS_sjis_japanese_ci://13, case MCS_sjis_bin://88, case MCS_hebrew_general_ci://16, case MCS_hebrew_bin://71, case MCS_tis620_thai_ci://18, case MCS_tis620_bin://89, case MCS_euckr_korean_ci://19, case MCS_euckr_bin://85, case MCS_koi8u_general_ci://22, case MCS_koi8u_bin://75, case MCS_gb2312_chinese_ci://24, case MCS_gb2312_bin://86, case MCS_greek_general_ci://25, case MCS_greek_bin://70, break; case MCS_cp1250_general_ci://26, case MCS_cp1250_czech_cs://34, case MCS_cp1250_croatian_ci://44, case MCS_cp1250_bin://66, case MCS_cp1250_polish_ci://99, return PyUnicode_Decode(value, cbValue, "cp1250", NULL); case MCS_gbk_chinese_ci://28, case MCS_gbk_bin://87, case MCS_latin5_turkish_ci://30, case MCS_latin5_bin://78, case MCS_armscii8_general_ci://32, case MCS_armscii8_bin://64, break; case MCS_utf8_general_ci://33, case MCS_utf8_bin://83, case MCS_utf8_unicode_ci://192, case MCS_utf8_icelandic_ci://193, case MCS_utf8_latvian_ci://194, case MCS_utf8_romanian_ci://195, case MCS_utf8_slovenian_ci://196, case MCS_utf8_polish_ci://197, case MCS_utf8_estonian_ci://198, case MCS_utf8_spanish_ci://199, case MCS_utf8_swedish_ci://200, case MCS_utf8_turkish_ci://201, case MCS_utf8_czech_ci://202, case MCS_utf8_danish_ci://203, case MCS_utf8_lithuanian_ci://204, case MCS_utf8_slovak_ci://205, case MCS_utf8_spanish2_ci://206, case MCS_utf8_roman_ci://207, case MCS_utf8_persian_ci://208, case MCS_utf8_esperanto_ci://209, case MCS_utf8_hungarian_ci://210, case MCS_utf8_sinhala_ci://211, return PyUnicode_DecodeUTF8 (value, cbValue, NULL); case MCS_ucs2_general_ci://35, case MCS_ucs2_bin://90, case MCS_ucs2_unicode_ci://128, case MCS_ucs2_icelandic_ci://129, case MCS_ucs2_latvian_ci://130, case MCS_ucs2_romanian_ci://131, case MCS_ucs2_slovenian_ci://132, case MCS_ucs2_polish_ci://133, case MCS_ucs2_estonian_ci://134, case MCS_ucs2_spanish_ci://135, case MCS_ucs2_swedish_ci://136, case MCS_ucs2_turkish_ci://137, case MCS_ucs2_czech_ci://138, case MCS_ucs2_danish_ci://139, case MCS_ucs2_lithuanian_ci://140, case MCS_ucs2_slovak_ci://141, case MCS_ucs2_spanish2_ci://142, case MCS_ucs2_roman_ci://143, case MCS_ucs2_persian_ci://144, case MCS_ucs2_esperanto_ci://145, case MCS_ucs2_hungarian_ci://146, case MCS_ucs2_sinhala_ci://147, break; case MCS_cp866_general_ci://36, case MCS_cp866_bin://68, case MCS_keybcs2_general_ci://37, case MCS_keybcs2_bin://73, case MCS_macce_general_ci://38, case MCS_macce_bin://43, case MCS_macroman_general_ci://39, case MCS_macroman_bin://53, case MCS_cp852_general_ci://40, case MCS_cp852_bin://81, case MCS_latin7_estonian_cs://20, case MCS_latin7_general_ci://41, case MCS_latin7_general_cs://42, case MCS_latin7_bin://79, case MCS_utf8mb4_general_ci://45, case MCS_utf8mb4_bin://46, case MCS_utf8mb4_unicode_ci://224, case MCS_utf8mb4_icelandic_ci://225, case MCS_utf8mb4_latvian_ci://226, case MCS_utf8mb4_romanian_ci://227, case MCS_utf8mb4_slovenian_ci://228, case MCS_utf8mb4_polish_ci://229, case MCS_utf8mb4_estonian_ci://230, case MCS_utf8mb4_spanish_ci://231, case MCS_utf8mb4_swedish_ci://232, case MCS_utf8mb4_turkish_ci://233, case MCS_utf8mb4_czech_ci://234, case MCS_utf8mb4_danish_ci://235, case MCS_utf8mb4_lithuanian_ci://236, case MCS_utf8mb4_slovak_ci://237, case MCS_utf8mb4_spanish2_ci://238, case MCS_utf8mb4_roman_ci://239, case MCS_utf8mb4_persian_ci://240, case MCS_utf8mb4_esperanto_ci://241, case MCS_utf8mb4_hungarian_ci://242, case MCS_utf8mb4_sinhala_ci://243, case MCS_cp1251_bulgarian_ci://14, case MCS_cp1251_ukrainian_ci://23, case MCS_cp1251_bin://50, case MCS_cp1251_general_ci://51, case MCS_cp1251_general_cs://52, break; case MCS_utf16_general_ci://54, case MCS_utf16_bin://55, case MCS_utf16_unicode_ci://101, case MCS_utf16_icelandic_ci://102, case MCS_utf16_latvian_ci://103, case MCS_utf16_romanian_ci://104, case MCS_utf16_slovenian_ci://105, case MCS_utf16_polish_ci://106, case MCS_utf16_estonian_ci://107, case MCS_utf16_spanish_ci://108, case MCS_utf16_swedish_ci://109, case MCS_utf16_turkish_ci://110, case MCS_utf16_czech_ci://111, case MCS_utf16_danish_ci://112, case MCS_utf16_lithuanian_ci://113, case MCS_utf16_slovak_ci://114, case MCS_utf16_spanish2_ci://115, case MCS_utf16_roman_ci://116, case MCS_utf16_persian_ci://117, case MCS_utf16_esperanto_ci://118, case MCS_utf16_hungarian_ci://119, case MCS_utf16_sinhala_ci://120, //return PyUnicode_DecodeUTF16(value, cbValue / 2, NULL, NULL); break; case MCS_cp1256_general_ci://57, case MCS_cp1256_bin://67, break; case MCS_cp1257_lithuanian_ci://29, case MCS_cp1257_bin://58, case MCS_cp1257_general_ci://59, break; case MCS_utf32_general_ci://60, case MCS_utf32_bin://61, case MCS_utf32_unicode_ci://160, case MCS_utf32_icelandic_ci://161, case MCS_utf32_latvian_ci://162, case MCS_utf32_romanian_ci://163, case MCS_utf32_slovenian_ci://164, case MCS_utf32_polish_ci://165, case MCS_utf32_estonian_ci://166, case MCS_utf32_spanish_ci://167, case MCS_utf32_swedish_ci://168, case MCS_utf32_turkish_ci://169, case MCS_utf32_czech_ci://170, case MCS_utf32_danish_ci://171, case MCS_utf32_lithuanian_ci://172, case MCS_utf32_slovak_ci://173, case MCS_utf32_spanish2_ci://174, case MCS_utf32_roman_ci://175, case MCS_utf32_persian_ci://176, case MCS_utf32_esperanto_ci://177, case MCS_utf32_hungarian_ci://178, case MCS_utf32_sinhala_ci://179, //return PyUnicode_DecodeUTF32 (value, cbValue / 4, NULL, NULL); break; case MCS_geostd8_general_ci://92, case MCS_geostd8_bin://93, case MCS_cp932_japanese_ci://95, case MCS_cp932_bin://96, case MCS_eucjpms_japanese_ci://97, case MCS_eucjpms_bin://98, break; case MCS_binary: return PyString_FromStringAndSize(value, cbValue); default: break; } return PyErr_Format (PyExc_ValueError, "Unsupported character set %d when decoding string", (int) ti->charset); }
static void err_input(perrdetail *err) { PyObject *v, *w, *errtype, *errtext; PyObject *msg_obj = NULL; const char *msg = NULL; int offset = err->offset; errtype = PyExc_SyntaxError; switch (err->error) { case E_ERROR: goto cleanup; case E_SYNTAX: errtype = PyExc_IndentationError; if (err->expected == INDENT) msg = "expected an indented block"; else if (err->token == INDENT) msg = "unexpected indent"; else if (err->token == DEDENT) msg = "unexpected unindent"; else if (err->expected == NOTEQUAL) { errtype = PyExc_SyntaxError; msg = "with Barry as BDFL, use '<>' instead of '!='"; } else { errtype = PyExc_SyntaxError; msg = "invalid syntax"; } break; case E_TOKEN: msg = "invalid token"; break; case E_EOFS: msg = "EOF while scanning triple-quoted string literal"; break; case E_EOLS: msg = "EOL while scanning string literal"; break; case E_INTR: if (!PyErr_Occurred()) PyErr_SetNone(PyExc_KeyboardInterrupt); goto cleanup; case E_NOMEM: PyErr_NoMemory(); goto cleanup; case E_EOF: msg = "unexpected EOF while parsing"; break; case E_TABSPACE: errtype = PyExc_TabError; msg = "inconsistent use of tabs and spaces in indentation"; break; case E_OVERFLOW: msg = "expression too long"; break; case E_DEDENT: errtype = PyExc_IndentationError; msg = "unindent does not match any outer indentation level"; break; case E_TOODEEP: errtype = PyExc_IndentationError; msg = "too many levels of indentation"; break; case E_DECODE: { PyObject *type, *value, *tb; PyErr_Fetch(&type, &value, &tb); msg = "unknown decode error"; if (value != NULL) msg_obj = PyObject_Str(value); Py_XDECREF(type); Py_XDECREF(value); Py_XDECREF(tb); break; } case E_LINECONT: msg = "unexpected character after line continuation character"; break; case E_IDENTIFIER: msg = "invalid character in identifier"; break; case E_BADSINGLE: msg = "multiple statements found while compiling a single statement"; break; default: fprintf(stderr, "error=%d\n", err->error); msg = "unknown parsing error"; break; } /* err->text may not be UTF-8 in case of decoding errors. Explicitly convert to an object. */ if (!err->text) { errtext = Py_None; Py_INCREF(Py_None); } else { errtext = PyUnicode_DecodeUTF8(err->text, err->offset, "replace"); if (errtext != NULL) { Py_ssize_t len = strlen(err->text); offset = (int)PyUnicode_GET_LENGTH(errtext); if (len != err->offset) { Py_DECREF(errtext); errtext = PyUnicode_DecodeUTF8(err->text, len, "replace"); } } } v = Py_BuildValue("(OiiN)", err->filename, err->lineno, offset, errtext); if (v != NULL) { if (msg_obj) w = Py_BuildValue("(OO)", msg_obj, v); else w = Py_BuildValue("(sO)", msg, v); } else w = NULL; Py_XDECREF(v); PyErr_SetObject(errtype, w); Py_XDECREF(w); cleanup: Py_XDECREF(msg_obj); if (err->text != NULL) { PyObject_FREE(err->text); err->text = NULL; } }
static PyObject * Tkapp_Call(PyObject *self, PyObject *args) { Tcl_Obj *objStore[ARGSZ]; Tcl_Obj **objv = NULL; int objc = 0, i; PyObject *res = NULL; Tcl_Interp *interp = Tkapp_Interp(self); /* Could add TCL_EVAL_GLOBAL if wrapped by GlobalCall... */ int flags = TCL_EVAL_DIRECT; objv = objStore; if (args == NULL) objc = 0; else if (!PyTuple_Check(args)) { objc = 1; objv[0] = AsObj(args); if (objv[0] == 0) goto finally; Tcl_IncrRefCount(objv[0]); } else { objc = PyTuple_Size(args); if (objc > ARGSZ) { objv = (Tcl_Obj **)ckalloc(objc * sizeof(char *)); if (objv == NULL) { PyErr_NoMemory(); goto finally; } } for (i = 0; i < objc; i++) { PyObject *v = PyTuple_GetItem(args, i); if (v == Py_None) { objc = i; break; } objv[i] = AsObj(v); if (!objv[i]) goto finally; Tcl_IncrRefCount(objv[i]); } } ENTER_TCL i = Tcl_EvalObjv(interp, objc, objv, flags); ENTER_OVERLAP if (i == TCL_ERROR) Tkinter_Error(self); else { /* We could request the object result here, but doing so would confuse applications that expect a string. */ char *s = Tcl_GetStringResult(interp); char *p = s; /* If the result contains any bytes with the top bit set, it's UTF-8 and we should decode it to Unicode */ while (*p != '\0') { if (*p & 0x80) break; p++; } if (*p == '\0') res = PyString_FromStringAndSize(s, (int)(p-s)); else { /* Convert UTF-8 to Unicode string */ p = strchr(p, '\0'); res = PyUnicode_DecodeUTF8(s, (int)(p-s), "strict"); if (res == NULL) { PyErr_Clear(); res = PyString_FromStringAndSize(s, (int)(p-s)); } } } LEAVE_OVERLAP_TCL finally: for (i = 0; i < objc; i++) Tcl_DecrRefCount(objv[i]); if (objv != objStore) ckfree(FREECAST objv); return res; }
/* * HTML attribute encoder */ PyObject * tdi_soup_encode_attribute(PyObject *value, PyObject *encoding) { PyObject *tmp; Py_ssize_t size, length; char *cvalue, *ctmp; int is_unicode = (PyUnicode_CheckExact(value) || PyUnicode_Check(value)); char c; if (is_unicode) { if (!(value = PyUnicode_AsUTF8String(value))) return NULL; } else { if (!PyString_CheckExact(value) && !PyString_Check(value)) { PyErr_SetString(TDI_E_TemplateEncodingError, "Attribute encoder takes string or unicode"); return NULL; } Py_INCREF(value); } length = size = PyString_GET_SIZE(value); cvalue = PyString_AS_STRING(value); while (length--) { switch (*cvalue++) { case '&': size += 4; /* amp; */ break; case '<': case '>': size += 3; /* lt; or gt; */ break; case '"': size += 5; /* quot; */ break; } } /* +2 = surrounding quotes */ if (!(tmp = PyString_FromStringAndSize(NULL, size + 2))) { Py_DECREF(value); return NULL; } ctmp = PyString_AS_STRING(tmp); cvalue = PyString_AS_STRING(value); length = PyString_GET_SIZE(value); *ctmp++ = '"'; while (length--) { switch (c = *cvalue++) { case '&': (void)memcpy(ctmp, "&", 5); ctmp += 5; break; case '<': (void)memcpy(ctmp, "<", 4); ctmp += 4; break; case '>': (void)memcpy(ctmp, ">", 4); ctmp += 4; break; case '"': (void)memcpy(ctmp, """, 6); ctmp += 6; break; default: *ctmp++ = c; break; } } *ctmp = '"'; Py_DECREF(value); if (is_unicode) { const char *cencoding = PyString_AsString(encoding); if (!cencoding) { Py_DECREF(tmp); return NULL; } value = PyUnicode_DecodeUTF8(PyString_AS_STRING(tmp), PyString_GET_SIZE(tmp), "strict"); Py_DECREF(tmp); if (!value) return NULL; tmp = PyUnicode_AsEncodedString(value, cencoding, "xmlcharrefreplace"); Py_DECREF(value); if (!tmp) return NULL; } return tmp; }
PyObject* PYSTRING8(const char* s) { return PyUnicode_DecodeUTF8((const char*)s, (int)strlen(s), NULL); }
/* Creates a new string object from a GUID * Returns a Python object if successful or NULL on error */ PyObject *pylnk_string_new_from_guid( const uint8_t *guid_buffer, size_t guid_buffer_size ) { char guid_string[ 48 ]; libcerror_error_t *error = NULL; libfguid_identifier_t *guid = NULL; PyObject *string_object = NULL; const char *errors = NULL; static char *function = "pylnk_string_new_from_guid"; if( libfguid_identifier_initialize( &guid, &error ) != 1 ) { pylnk_error_raise( error, PyExc_IOError, "%s: unable to create GUID.", function ); libcerror_error_free( &error ); goto on_error; } if( libfguid_identifier_copy_from_byte_stream( guid, guid_buffer, guid_buffer_size, LIBFGUID_ENDIAN_LITTLE, &error ) != 1 ) { pylnk_error_raise( error, PyExc_IOError, "%s: unable to copy byte stream to GUID.", function ); libcerror_error_free( &error ); goto on_error; } if( libfguid_identifier_copy_to_utf8_string( guid, (uint8_t *) guid_string, 48, LIBFGUID_STRING_FORMAT_FLAG_USE_LOWER_CASE, &error ) != 1 ) { pylnk_error_raise( error, PyExc_IOError, "%s: unable to copy GUID to string.", function ); libcerror_error_free( &error ); goto on_error; } if( libfguid_identifier_free( &guid, &error ) != 1 ) { pylnk_error_raise( error, PyExc_IOError, "%s: unable to free GUID.", function ); libcerror_error_free( &error ); goto on_error; } /* Pass the string length to PyUnicode_DecodeUTF8 * otherwise it makes the end of string character is part * of the string */ string_object = PyUnicode_DecodeUTF8( guid_string, (Py_ssize_t) 36, errors ); return( string_object ); on_error: if( guid != NULL ) { libfguid_identifier_free( &guid, NULL ); } return( NULL ); }
/* Hall of shame, wasted time debugging the code below * 20min - Johan 2009-02-19 */ static PyObject * pygi_collect_attributes (PyObject *self, PyObject *args) { char *tag_name; PyObject *attributes; int indent, indent_len, i, j, self_indent; char *indent_char; gboolean first; GString *attr_value = NULL; int len; PyObject *result = NULL; if (!PyArg_ParseTuple(args, "sO!isi", &tag_name, &PyList_Type, &attributes, &self_indent, &indent_char, &indent)) return NULL; if (attributes == Py_None || !PyList_Size(attributes)) return PyUnicode_DecodeUTF8("", 0, "strict"); len = calc_attrs_length(attributes, indent, self_indent); if (len < 0) return NULL; if (len > 79) indent_len = self_indent + strlen(tag_name) + 1; else indent_len = 0; first = TRUE; attr_value = g_string_new (""); for (i = 0; i < PyList_Size (attributes); ++i) { PyObject *tuple, *pyvalue; PyObject *s = NULL; char *attr, *value, *escaped; tuple = PyList_GetItem (attributes, i); if (!PyTuple_Check (tuple)) { PyErr_SetString(PyExc_TypeError, "attribute item must be a tuple"); goto out; } if (!PyTuple_Size (tuple) == 2) { PyErr_SetString(PyExc_IndexError, "attribute item must be a tuple of length 2"); goto out; } if (PyTuple_GetItem(tuple, 1) == Py_None) continue; /* this leaks, but we exit after, so */ if (!PyArg_ParseTuple(tuple, "sO", &attr, &pyvalue)) goto out; if (PyUnicode_Check(pyvalue)) { s = PyUnicode_AsUTF8String(pyvalue); if (!s) goto out; value = PyString_AsString(s); } else if (PyString_Check(pyvalue)) { value = PyString_AsString(pyvalue); } else { PyErr_SetString(PyExc_TypeError, "value must be string or unicode"); goto out; } if (indent_len && !first) { g_string_append_c (attr_value, '\n'); for (j = 0; j < indent_len; j++) g_string_append_c (attr_value, ' '); } g_string_append_c (attr_value, ' '); g_string_append (attr_value, attr); g_string_append_c (attr_value, '='); g_string_append_c (attr_value, '\"'); escaped = g_markup_escape_text (value, -1); g_string_append (attr_value, escaped); g_string_append_c (attr_value, '\"'); if (first) first = FALSE; Py_XDECREF(s); } result = PyUnicode_DecodeUTF8 (attr_value->str, attr_value->len, "strict"); out: if (attr_value != NULL) g_string_free (attr_value, TRUE); return result; }
/* search_for_exec_prefix requires that argv0_path be no more than MAXPATHLEN bytes long. */ static int search_for_exec_prefix(wchar_t *argv0_path, wchar_t *home, wchar_t *_exec_prefix) { size_t n; /* If PYTHONHOME is set, we believe it unconditionally */ if (home) { wchar_t *delim; delim = wcschr(home, DELIM); if (delim) wcsncpy(exec_prefix, delim+1, MAXPATHLEN); else wcsncpy(exec_prefix, home, MAXPATHLEN); joinpath(exec_prefix, lib_python); joinpath(exec_prefix, L"lib-dynload"); return 1; } /* Check to see if argv[0] is in the build directory. "pybuilddir.txt" is written by setup.py and contains the relative path to the location of shared library modules. */ wcscpy(exec_prefix, argv0_path); joinpath(exec_prefix, L"pybuilddir.txt"); if (isfile(exec_prefix)) { FILE *f = _Py_wfopen(exec_prefix, L"rb"); if (f == NULL) errno = 0; else { char buf[MAXPATHLEN+1]; PyObject *decoded; wchar_t rel_builddir_path[MAXPATHLEN+1]; n = fread(buf, 1, MAXPATHLEN, f); buf[n] = '\0'; fclose(f); decoded = PyUnicode_DecodeUTF8(buf, n, "surrogateescape"); if (decoded != NULL) { Py_ssize_t k; k = PyUnicode_AsWideChar(decoded, rel_builddir_path, MAXPATHLEN); Py_DECREF(decoded); if (k >= 0) { rel_builddir_path[k] = L'\0'; wcscpy(exec_prefix, argv0_path); joinpath(exec_prefix, rel_builddir_path); return -1; } } } } /* Search from argv0_path, until root is found */ copy_absolute(exec_prefix, argv0_path, MAXPATHLEN+1); do { n = wcslen(exec_prefix); joinpath(exec_prefix, lib_python); joinpath(exec_prefix, L"lib-dynload"); if (isdir(exec_prefix)) return 1; exec_prefix[n] = L'\0'; reduce(exec_prefix); } while (exec_prefix[0]); /* Look at configure's EXEC_PREFIX */ wcsncpy(exec_prefix, _exec_prefix, MAXPATHLEN); joinpath(exec_prefix, lib_python); joinpath(exec_prefix, L"lib-dynload"); if (isdir(exec_prefix)) return 1; /* Fail */ return 0; }
/* Retrieves the header values * Returns a Python object holding the offset if successful or NULL on error */ PyObject *pyewf_handle_get_header_values( pyewf_handle_t *pyewf_handle ) { char error_string[ PYEWF_ERROR_STRING_SIZE ]; libcerror_error_t *error = NULL; PyObject *dictionary_object = NULL; PyObject *string_object = NULL; static char *function = "pyewf_handle_get_header_values"; const char *errors = NULL; char *header_value = NULL; char *header_value_identifier = NULL; size_t header_value_identifier_length = 0; size_t header_value_identifier_size = 0; size_t header_value_size = 0; uint32_t number_of_header_values = 0; uint32_t header_value_index = 0; int result = 0; if( pyewf_handle == NULL ) { PyErr_Format( PyExc_ValueError, "%s: invalid handle.", function ); return( NULL ); } Py_BEGIN_ALLOW_THREADS result = libewf_handle_get_number_of_header_values( pyewf_handle->handle, &number_of_header_values, &error ); Py_END_ALLOW_THREADS if( result == -1 ) { if( libcerror_error_backtrace_sprint( error, error_string, PYEWF_ERROR_STRING_SIZE ) == -1 ) { PyErr_Format( PyExc_IOError, "%s: failed to retrieve number of header values.", function ); } else { PyErr_Format( PyExc_IOError, "%s: failed to retrieve number of header values.\n%s", function, error_string ); } libcerror_error_free( &error ); goto on_error; } dictionary_object = PyDict_New(); for( header_value_index = 0; header_value_index < number_of_header_values; header_value_index++ ) { Py_BEGIN_ALLOW_THREADS result = libewf_handle_get_header_value_identifier_size( pyewf_handle->handle, header_value_index, &header_value_identifier_size, &error ); Py_END_ALLOW_THREADS if( result != 1 ) { if( libcerror_error_backtrace_sprint( error, error_string, PYEWF_ERROR_STRING_SIZE ) == -1 ) { PyErr_Format( PyExc_IOError, "%s: unable to retrieve header value: %d identifier size.", function, header_value_index ); } else { PyErr_Format( PyExc_IOError, "%s: unable to retrieve header value: %d identifier size.\n%s", function, header_value_index, error_string ); } libcerror_error_free( &error ); goto on_error; } header_value_identifier = (char *) PyMem_Malloc( sizeof( char ) * header_value_identifier_size ); if( header_value_identifier == NULL ) { PyErr_Format( PyExc_MemoryError, "%s: unable to create header value identifier.", function ); goto on_error; } Py_BEGIN_ALLOW_THREADS result = libewf_handle_get_header_value_identifier( pyewf_handle->handle, header_value_index, (uint8_t *) header_value_identifier, header_value_identifier_size, &error ); Py_END_ALLOW_THREADS if( result != 1 ) { if( libcerror_error_backtrace_sprint( error, error_string, PYEWF_ERROR_STRING_SIZE ) == -1 ) { PyErr_Format( PyExc_IOError, "%s: unable to retrieve header value: %d identifier.", function, header_value_index ); } else { PyErr_Format( PyExc_IOError, "%s: unable to retrieve header value: %d identifier.\n%s", function, header_value_index, error_string ); } libcerror_error_free( &error ); goto on_error; } header_value_identifier_length = libcstring_narrow_string_length( header_value_identifier ); Py_BEGIN_ALLOW_THREADS result = libewf_handle_get_utf8_header_value_size( pyewf_handle->handle, (uint8_t *) header_value_identifier, header_value_identifier_length, &header_value_size, &error ); Py_END_ALLOW_THREADS if( result == -1 ) { if( libcerror_error_backtrace_sprint( error, error_string, PYEWF_ERROR_STRING_SIZE ) == -1 ) { PyErr_Format( PyExc_IOError, "%s: unable to retrieve UTF-8 header value: %s size.", function, header_value_identifier ); } else { PyErr_Format( PyExc_IOError, "%s: unable to retrieve UTF-8 header value: %s size.\n%s", function, header_value_identifier, error_string ); } libcerror_error_free( &error ); goto on_error; } /* Ignore emtpy header values */ if( ( result != 0 ) && ( header_value_size > 0 ) ) { header_value = (char *) PyMem_Malloc( sizeof( char ) * header_value_size ); if( header_value == NULL ) { PyErr_Format( PyExc_MemoryError, "%s: unable to create header value.", function ); goto on_error; } Py_BEGIN_ALLOW_THREADS result = libewf_handle_get_utf8_header_value( pyewf_handle->handle, (uint8_t *) header_value_identifier, header_value_identifier_length, (uint8_t *) header_value, header_value_size, &error ); Py_END_ALLOW_THREADS if( result != 1 ) { if( libcerror_error_backtrace_sprint( error, error_string, PYEWF_ERROR_STRING_SIZE ) == -1 ) { PyErr_Format( PyExc_IOError, "%s: unable to retrieve UTF-8 header value: %s.", function, header_value_identifier ); } else { PyErr_Format( PyExc_IOError, "%s: unable to retrieve UTF-8 header value: %s.\n%s", function, header_value_identifier, error_string ); } libcerror_error_free( &error ); goto on_error; } /* Pass the string length to PyUnicode_DecodeUTF8 * otherwise it makes the end of string character is part * of the string */ string_object = PyUnicode_DecodeUTF8( header_value, header_value_size - 1, errors ); if( string_object == NULL ) { PyErr_Format( PyExc_IOError, "%s: unable to convert UTF-8 header value: %s into Unicode.", function, header_value_identifier ); goto on_error; } if( PyDict_SetItemString( dictionary_object, header_value_identifier, string_object ) != 0 ) { PyErr_Format( PyExc_MemoryError, "%s: unable to set header value: %s in dictionary.", function, header_value_identifier ); goto on_error; } string_object = NULL; PyMem_Free( header_value ); header_value = NULL; } PyMem_Free( header_value_identifier ); header_value_identifier = NULL; }