1
0
mirror of https://github.com/samba-team/samba.git synced 2025-01-08 21:18:16 +03:00

s4:librpc: Add functions converting between bytes and UTF‐16 strings

Signed-off-by: Joseph Sutton <josephsutton@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>

Autobuild-User(master): Andrew Bartlett <abartlet@samba.org>
Autobuild-Date(master): Thu Nov 16 06:23:35 UTC 2023 on atb-devel-224
This commit is contained in:
Joseph Sutton 2023-11-16 11:20:22 +13:00 committed by Andrew Bartlett
parent 5f3f3c0cc3
commit fe1d45cf1e
2 changed files with 49 additions and 0 deletions

View File

@ -419,6 +419,51 @@ PyObject *PyString_FromStringOrNULL(const char *str)
return PyUnicode_FromString(str);
}
PyObject *PyBytes_FromUtf16StringOrNULL(const uint16_t *str)
{
size_t len;
if (str == NULL) {
Py_RETURN_NONE;
}
len = utf16_len(str);
return PyBytes_FromStringAndSize((const char *)str, len);
}
uint16_t *PyUtf16String_FromBytes(TALLOC_CTX *mem_ctx, PyObject *value)
{
char *bytes = NULL;
Py_ssize_t len = 0;
uint16_t *utf16_string = NULL;
int ret;
ret = PyBytes_AsStringAndSize(value, &bytes, &len);
if (ret) {
return NULL;
}
if (len < 0) {
PyErr_SetString(PyExc_ValueError, "bytes length is negative");
return NULL;
}
/* Ensure that the bytes object contains no embedded null terminator. */
if ((size_t)len != utf16_len_n(bytes, len)) {
PyErr_SetString(PyExc_ValueError,
"value contains an embedded null terminator");
return NULL;
}
utf16_string = talloc_utf16_strlendup(mem_ctx, bytes, len);
if (utf16_string == NULL) {
PyErr_NoMemory();
return NULL;
}
return utf16_string;
}
PyObject *pyrpc_import_union(PyTypeObject *type, TALLOC_CTX *mem_ctx, int level,
const void *in, const char *typename)
{

View File

@ -59,6 +59,10 @@ PyObject *py_return_ndr_struct(const char *module_name, const char *type_name,
PyObject *PyString_FromStringOrNULL(const char *str);
PyObject *PyBytes_FromUtf16StringOrNULL(const uint16_t *str);
uint16_t *PyUtf16String_FromBytes(TALLOC_CTX *mem_ctx, PyObject *value);
PyObject *pyrpc_import_union(PyTypeObject *type, TALLOC_CTX *mem_ctx, int level,
const void *in, const char *typename);
void *pyrpc_export_union(PyTypeObject *type, TALLOC_CTX *mem_ctx, int level,