LCOV - code coverage report
Current view: top level - source4/librpc/ndr - py_misc.c (source / functions) Hit Total Coverage
Test: coverage report for master 2b515b7d Lines: 77 88 87.5 %
Date: 2024-02-28 12:06:22 Functions: 9 9 100.0 %

          Line data    Source code
       1             : /* 
       2             :    Unix SMB/CIFS implementation.
       3             :    Samba utility functions
       4             : 
       5             :    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
       6             :    
       7             :    This program is free software; you can redistribute it and/or modify
       8             :    it under the terms of the GNU General Public License as published by
       9             :    the Free Software Foundation; either version 3 of the License, or
      10             :    (at your option) any later version.
      11             :    
      12             :    This program is distributed in the hope that it will be useful,
      13             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15             :    GNU General Public License for more details.
      16             :    
      17             :    You should have received a copy of the GNU General Public License
      18             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      19             : */
      20             : #include "lib/replace/system/python.h"
      21             : #include "python/py3compat.h"
      22             : #include "librpc/gen_ndr/misc.h"
      23             : 
      24    13917148 : static PyObject *py_GUID_richcmp(PyObject *py_self, PyObject *py_other, int op)
      25             : {
      26     1197312 :         int ret;
      27    13917148 :         struct GUID *self = pytalloc_get_ptr(py_self), *other;
      28    13917148 :         other = pytalloc_get_ptr(py_other);
      29    13917148 :         if (other == NULL) {
      30           0 :                 Py_INCREF(Py_NotImplemented);
      31           0 :                 return Py_NotImplemented;
      32             :         }
      33             : 
      34    13917148 :         ret = GUID_compare(self, other);
      35             : 
      36    13917148 :         switch (op) {
      37        5307 :                 case Py_EQ: if (ret == 0) Py_RETURN_TRUE; else Py_RETURN_FALSE;
      38    13911836 :                 case Py_NE: if (ret != 0) Py_RETURN_TRUE; else Py_RETURN_FALSE;
      39           2 :                 case Py_LT: if (ret <  0) Py_RETURN_TRUE; else Py_RETURN_FALSE;
      40           3 :                 case Py_GT: if (ret >  0) Py_RETURN_TRUE; else Py_RETURN_FALSE;
      41           0 :                 case Py_LE: if (ret <= 0) Py_RETURN_TRUE; else Py_RETURN_FALSE;
      42           0 :                 case Py_GE: if (ret >= 0) Py_RETURN_TRUE; else Py_RETURN_FALSE;
      43             :         }
      44           0 :         Py_INCREF(Py_NotImplemented);
      45           0 :         return Py_NotImplemented;
      46             : }
      47             : 
      48     1047110 : static PyObject *py_GUID_str(PyObject *py_self)
      49             : {
      50     1047110 :         struct GUID *self = pytalloc_get_ptr(py_self);
      51       85210 :         struct GUID_txt_buf buf;
      52     1047110 :         PyObject *ret = PyUnicode_FromString(GUID_buf_string(self, &buf));
      53     1047110 :         return ret;
      54             : }
      55             : 
      56        3389 : static PyObject *py_GUID_repr(PyObject *py_self)
      57             : {
      58        3389 :         struct GUID *self = pytalloc_get_ptr(py_self);
      59         145 :         struct GUID_txt_buf buf;
      60        3389 :         PyObject *ret = PyUnicode_FromFormat(
      61             :                 "GUID('%s')", GUID_buf_string(self, &buf));
      62        3389 :         return ret;
      63             : }
      64             : 
      65    14584448 : static int py_GUID_init(PyObject *self, PyObject *args, PyObject *kwargs)
      66             : {
      67    14584448 :         PyObject *str = NULL;
      68     1277989 :         NTSTATUS status;
      69    14584448 :         struct GUID *guid = pytalloc_get_ptr(self);
      70    14584448 :         const char *kwnames[] = { "str", NULL };
      71             : 
      72    14584448 :         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", discard_const_p(char *, kwnames), &str))
      73           0 :                 return -1;
      74             : 
      75    14584448 :         if (str != NULL) {
      76     1277701 :                 DATA_BLOB guid_val;
      77     1277701 :                 Py_ssize_t _size;
      78             : 
      79    14572332 :                 if (PyUnicode_Check(str)) {
      80    13923627 :                         guid_val.data =
      81    13923627 :                                 discard_const_p(uint8_t,
      82             :                                                 PyUnicode_AsUTF8AndSize(str, &_size));
      83      648705 :                 } else if (PyBytes_Check(str)) {
      84      729178 :                         guid_val.data =
      85      648705 :                                 discard_const_p(uint8_t,
      86             :                                                 PyBytes_AsString(str));
      87      648705 :                         _size = PyBytes_Size(str);
      88             :                 } else {
      89           0 :                         PyErr_SetString(PyExc_TypeError,
      90             :                                         "Expected a string or bytes argument to GUID()");
      91           5 :                         return -1;
      92             :                 }
      93    14572332 :                 guid_val.length = _size;
      94    14572332 :                 status = GUID_from_data_blob(&guid_val, guid);
      95    14572332 :                 if (!NT_STATUS_IS_OK(status)) {
      96           5 :                         PyErr_SetNTSTATUS(status);
      97           5 :                         return -1;
      98             :                 }
      99             :         }
     100             : 
     101    13306459 :         return 0;
     102             : }
     103             : 
     104        7926 : static void py_GUID_patch(PyTypeObject *type)
     105             : {
     106        7926 :         type->tp_init = py_GUID_init;
     107        7926 :         type->tp_str = py_GUID_str;
     108        7926 :         type->tp_repr = py_GUID_repr;
     109        7926 :         type->tp_richcompare = py_GUID_richcmp;
     110        7732 : }
     111             : 
     112             : #define PY_GUID_PATCH py_GUID_patch
     113             : 
     114          53 : static int py_policy_handle_init(PyObject *self, PyObject *args, PyObject *kwargs)
     115             : {
     116          53 :         char *str = NULL;
     117           3 :         NTSTATUS status;
     118          53 :         struct policy_handle *handle = pytalloc_get_ptr(self);
     119          53 :         const char *kwnames[] = { "uuid", "type", NULL };
     120             : 
     121          53 :         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|sI", discard_const_p(char *, kwnames), &str, &handle->handle_type))
     122           0 :                 return -1;
     123             : 
     124          53 :         if (str != NULL) {
     125           3 :                 status = GUID_from_string(str, &handle->uuid);
     126           3 :                 if (!NT_STATUS_IS_OK(status)) {
     127           0 :                         PyErr_SetNTSTATUS(status);
     128           0 :                         return -1;
     129             :                 }
     130             :         }
     131             : 
     132          50 :         return 0;
     133             : }
     134             : 
     135           1 : static PyObject *py_policy_handle_repr(PyObject *py_self)
     136             : {
     137           1 :         struct policy_handle *self = pytalloc_get_ptr(py_self);
     138           1 :         struct GUID_txt_buf buf;
     139           1 :         PyObject *ret = PyUnicode_FromFormat(
     140             :                 "policy_handle(%d, '%s')",
     141             :                 self->handle_type,
     142           1 :                 GUID_buf_string(&self->uuid, &buf));
     143           1 :         return ret;
     144             : }
     145             : 
     146           1 : static PyObject *py_policy_handle_str(PyObject *py_self)
     147             : {
     148           1 :         struct policy_handle *self = pytalloc_get_ptr(py_self);
     149           1 :         struct GUID_txt_buf buf;
     150           1 :         PyObject *ret = PyUnicode_FromFormat(
     151             :                 "%d, %s",
     152             :                 self->handle_type,
     153           1 :                 GUID_buf_string(&self->uuid, &buf));
     154           1 :         return ret;
     155             : }
     156             : 
     157        7926 : static void py_policy_handle_patch(PyTypeObject *type)
     158             : {
     159        7926 :         type->tp_init = py_policy_handle_init;
     160        7926 :         type->tp_repr = py_policy_handle_repr;
     161        7926 :         type->tp_str = py_policy_handle_str;
     162        7732 : }
     163             : 
     164             : #define PY_POLICY_HANDLE_PATCH py_policy_handle_patch
     165             : 

Generated by: LCOV version 1.14