|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <Python.h> |
|
#include "generic.h" |
|
#include "apt_pkgmodule.h" |
|
#include <apt-pkg/hashes.h> |
|
|
|
static PyObject *hashstring_new(PyTypeObject *type,PyObject *Args, |
|
PyObject *kwds) |
|
{ |
|
char *Type = NULL; |
|
char *Hash = NULL; |
|
char *kwlist[] = {"type", "hash", NULL}; |
|
if (PyArg_ParseTupleAndKeywords(Args, kwds, "s|s:__new__", kwlist, &Type, |
|
&Hash) == 0) |
|
return 0; |
|
CppPyObject<HashString*> *PyObj = CppPyObject_NEW<HashString*>(NULL, type); |
|
if (Hash) |
|
PyObj->Object = new HashString(Type,Hash); |
|
else |
|
PyObj->Object = new HashString(Type); |
|
return PyObj; |
|
} |
|
|
|
static PyObject *hashstring_repr(PyObject *self) |
|
{ |
|
HashString *hash = GetCpp<HashString*>(self); |
|
return PyString_FromFormat("<%s object: \"%s\">", self->ob_type->tp_name, |
|
hash->toStr().c_str()); |
|
} |
|
|
|
static PyObject *hashstring_str(PyObject *self) |
|
{ |
|
const HashString *hash = GetCpp<HashString*>(self); |
|
return CppPyString(hash->toStr()); |
|
} |
|
|
|
static PyObject *hashstring_get_hashtype(PyObject *self) |
|
{ |
|
const HashString *hash = GetCpp<HashString*>(self); |
|
return CppPyString(hash->HashType()); |
|
} |
|
|
|
static PyObject *hashstring_get_usable(PyObject *self) |
|
{ |
|
const HashString *hash = GetCpp<HashString*>(self); |
|
return PyBool_FromLong(hash->usable()); |
|
} |
|
|
|
static PyObject *hashstring_get_hashvalue(PyObject *self) |
|
{ |
|
const HashString *hash = GetCpp<HashString*>(self); |
|
return CppPyString(hash->HashValue()); |
|
} |
|
|
|
static char *hashstring_verify_file_doc = |
|
"verify_file(filename: str) -> bool\n\n" |
|
"Verify that the file indicated by filename matches the hash."; |
|
|
|
static PyObject *hashstring_verify_file(PyObject *self,PyObject *args) |
|
{ |
|
const HashString *hash = GetCpp<HashString*>(self); |
|
char *filename; |
|
if (PyArg_ParseTuple(args, "s:verify_file", &filename) == 0) |
|
return 0; |
|
return PyBool_FromLong(hash->VerifyFile(filename)); |
|
} |
|
|
|
static PyMethodDef hashstring_methods[] = { |
|
{"verify_file",hashstring_verify_file,METH_VARARGS, |
|
hashstring_verify_file_doc}, |
|
{NULL} |
|
}; |
|
|
|
static PyGetSetDef hashstring_getset[] = { |
|
{"hashtype",(getter)hashstring_get_hashtype,0, |
|
"The type of the hash, as a string (possible: MD5Sum,SHA1,SHA256)."}, |
|
{"hashvalue",(getter)hashstring_get_hashvalue,0, |
|
"The value of the hash, as a hexadecimal string\n" |
|
"\n" |
|
".. versionadded:: 1.9.0"}, |
|
{"usable",(getter)hashstring_get_usable,0, |
|
"True if the hashstring is a trusted hash type."}, |
|
{NULL} |
|
}; |
|
|
|
static PyObject *hashstring_richcompare(PyObject *obj1, PyObject *obj2, int op) |
|
{ |
|
if (!PyObject_TypeCheck(obj1, &PyHashString_Type)) |
|
return PyErr_SetString(PyExc_TypeError, "Expected HashString"), nullptr; |
|
if (!PyObject_TypeCheck(obj2, &PyHashString_Type)) |
|
return PyErr_SetString(PyExc_TypeError, "Expected HashString"), nullptr; |
|
|
|
const HashString *a = GetCpp<HashString*>(obj1); |
|
const HashString *b = GetCpp<HashString*>(obj2); |
|
PyObject *result = Py_False; |
|
|
|
switch (op) { |
|
case Py_LT: |
|
case Py_GT: |
|
result = Py_False; |
|
break; |
|
case Py_LE: |
|
case Py_GE: |
|
case Py_EQ: |
|
result = *a == *b ? Py_True : Py_False; |
|
break; |
|
case Py_NE: |
|
result = *a != *b ? Py_True : Py_False; |
|
break; |
|
} |
|
|
|
Py_INCREF(result); |
|
return result; |
|
} |
|
|
|
static char *hashstring_doc = |
|
"HashString(type, hash) OR HashString('type:hash')\n\n" |
|
"Create a new HashString object. The first form allows you to specify\n" |
|
"a type and a hash, and the second form a single string where type and\n" |
|
"hash are separated by a colon, e.g.::\n\n" |
|
" HashString('MD5Sum', '6cc1b6e6655e3555ac47e5b5fe26d04e')\n\n" |
|
"Valid options for 'type' are: MD5Sum, SHA1, SHA256."; |
|
PyTypeObject PyHashString_Type = { |
|
PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|
"apt_pkg.HashString", |
|
sizeof(CppPyObject<HashString*>), |
|
0, |
|
|
|
CppDeallocPtr<HashString*>, |
|
0, |
|
0, |
|
0, |
|
0, |
|
hashstring_repr, |
|
0, |
|
0, |
|
0, |
|
0, |
|
0, |
|
hashstring_str, |
|
0, |
|
0, |
|
0, |
|
Py_TPFLAGS_DEFAULT | |
|
Py_TPFLAGS_BASETYPE, |
|
hashstring_doc, |
|
0, |
|
0, |
|
hashstring_richcompare, |
|
0, |
|
0, |
|
0, |
|
hashstring_methods, |
|
0, |
|
hashstring_getset, |
|
0, |
|
0, |
|
0, |
|
0, |
|
0, |
|
0, |
|
0, |
|
hashstring_new, |
|
}; |
|
|