#include "py_conv.h"
static void fstr_pull(fstring str, UNISTR *uni)
{
rpcstr_pull(str, uni->buffer, sizeof(fstring), -1, STR_TERMINATE);
}
static void fstr_pull2(fstring str, UNISTR2 *uni)
{
rpcstr_pull(str, uni->buffer, sizeof(fstring), -1, STR_TERMINATE);
}
PyObject *from_struct(void *s, struct pyconv *conv)
{
PyObject *obj, *item;
int i;
obj = PyDict_New();
for (i = 0; conv[i].name; i++) {
switch (conv[i].type) {
case PY_UNISTR: {
UNISTR *u = (UNISTR *)((char *)s + conv[i].offset);
fstring str = "";
if (u->buffer)
fstr_pull(str, u);
item = PyString_FromString(str);
PyDict_SetItemString(obj, conv[i].name, item);
break;
}
case PY_UNISTR2: {
UNISTR2 *u = (UNISTR2 *)((char *)s + conv[i].offset);
fstring str = "";
if (u->buffer)
fstr_pull2(str, u);
item = PyString_FromString(str);
PyDict_SetItemString(obj, conv[i].name, item);
break;
}
case PY_UINT32: {
uint32 *u = (uint32 *)((char *)s + conv[i].offset);
item = PyInt_FromLong(*u);
PyDict_SetItemString(obj, conv[i].name, item);
break;
}
case PY_UINT16: {
uint16 *u = (uint16 *)((char *)s + conv[i].offset);
item = PyInt_FromLong(*u);
PyDict_SetItemString(obj, conv[i].name, item);
break;
}
case PY_STRING: {
char *str = (char *)s + conv[i].offset;
item = PyString_FromString(str);
PyDict_SetItemString(obj, conv[i].name, item);
break;
}
case PY_UID: {
uid_t *uid = (uid_t *)((char *)s + conv[i].offset);
item = PyInt_FromLong(*uid);
PyDict_SetItemString(obj, conv[i].name, item);
break;
}
case PY_GID: {
gid_t *gid = (gid_t *)((char *)s + conv[i].offset);
item = PyInt_FromLong(*gid);
PyDict_SetItemString(obj, conv[i].name, item);
break;
}
default:
break;
}
}
return obj;
}
BOOL to_struct(void *s, PyObject *dict, struct pyconv *conv)
{
PyObject *visited, *key, *value;
BOOL result = False;
int i;
visited = PyDict_New();
for (i = 0; conv[i].name; i++) {
PyObject *obj;
obj = PyDict_GetItemString(dict, conv[i].name);
if (!obj)
goto done;
switch (conv[i].type) {
case PY_UNISTR: {
UNISTR *u = (UNISTR *)((char *)s + conv[i].offset);
char *str = "";
if (!PyString_Check(obj))
goto done;
str = PyString_AsString(obj);
init_unistr(u, str);
break;
}
case PY_UINT32: {
uint32 *u = (uint32 *)((char *)s + conv[i].offset);
if (!PyInt_Check(obj))
goto done;
*u = PyInt_AsLong(obj);
break;
}
case PY_UINT16: {
uint16 *u = (uint16 *)((char *)s + conv[i].offset);
if (!PyInt_Check(obj))
goto done;
*u = PyInt_AsLong(obj);
break;
}
default:
break;
}
PyDict_SetItemString(visited, conv[i].name,
PyInt_FromLong(1));
}
i = 0;
while (PyDict_Next(dict, &i, &key, &value)) {
if (!PyDict_GetItem(visited, key))
goto done;
}
result = True;
done:
Py_DECREF(visited);
return result;
}
PyObject *from_unistr_list(uint16 *dependentfiles)
{
PyObject *list;
int offset = 0;
list = PyList_New(0);
while (*(dependentfiles + offset) != 0) {
fstring name;
int len;
len = rpcstr_pull(name, dependentfiles + offset,
sizeof(fstring), -1, STR_TERMINATE);
offset += len / 2;
PyList_Append(list, PyString_FromString(name));
}
return list;
}