pychm-0.8.6/0000755000076500000240000000000013574533170013750 5ustar dottedmagstaff00000000000000pychm-0.8.6/PKG-INFO0000644000076500000240000000174613574533170015055 0ustar dottedmagstaff00000000000000Metadata-Version: 1.0 Name: pychm Version: 0.8.6 Summary: Python package to handle CHM files Home-page: https://github.com/dottedmag/pychm Author: Mikhail Gusarov Author-email: dottedmag@dottedmag.net License: GPL Description: PyCHM ===== PyCHM is a Python library to manipulate CHM files (Microsoft HTML Help). This library supports Python 2.7 and Python 3.5+. It is in a maintenance mode and accepts only security and bug fixes. API --- The chm package contains four modules: * chm.chm: High-level support for CHM archives. * chm.extra: Extra utility functions - full-text search support, encoding detection. * chm.chmlib: Low level wrappers around the chmlib API (Python part). * chm._chmlib: Low level wrappers around the chmlib API (C part). This module is unstable and subject to change without notice. Platform: UNKNOWN pychm-0.8.6/LICENSE0000644000076500000240000000034613557050554014761 0ustar dottedmagstaff00000000000000Copyright (C) 2003-2006 Rubens Ramos Copyright (C) 2014,2019 Mikhail Gusarov This library is licensed under the GPLv2+. Please refer to the COPYING file for more details. pychm-0.8.6/MANIFEST.in0000644000076500000240000000010713557071331015501 0ustar dottedmagstaff00000000000000include COPYING include NEWS include LICENSE graft chm/chmlib_search.h pychm-0.8.6/README0000644000076500000240000000112013574533162014623 0ustar dottedmagstaff00000000000000PyCHM ===== PyCHM is a Python library to manipulate CHM files (Microsoft HTML Help). This library supports Python 2.7 and Python 3.5+. It is in a maintenance mode and accepts only security and bug fixes. API --- The chm package contains four modules: * chm.chm: High-level support for CHM archives. * chm.extra: Extra utility functions - full-text search support, encoding detection. * chm.chmlib: Low level wrappers around the chmlib API (Python part). * chm._chmlib: Low level wrappers around the chmlib API (C part). This module is unstable and subject to change without notice. pychm-0.8.6/chm/0000755000076500000240000000000013574533170014517 5ustar dottedmagstaff00000000000000pychm-0.8.6/chm/extra.py0000644000076500000240000000343713557047250016223 0ustar dottedmagstaff00000000000000# Copyright (C) 2019 Mikhail Gusarov # # pychm is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as # published by the Free Software Foundation; either version 2 of the # License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public # License along with this program; see the file COPYING. If not, # write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, # Boston, MA 02111-1307, USA from . import chmlib, _chmlib _lang_objects = [ (b'/$FIftiMain', 0x7E), (b'$WWKeywordLinks/BTree', 0x34), (b'$WWAssociativeLinks/BTree', 0x34), ] def get_lcid(f): for (obj, offset) in _lang_objects: (res, ui) = chmlib.chm_resolve_object(f, obj) if res == chmlib.CHM_RESOLVE_SUCCESS: (size, content) = chmlib.chm_retrieve_object(f, ui, offset, 4) if size != 0: return struct.unpack(' #include "chmlib_search.h" #include #define CHMFILE_CAPSULE_NAME "C.chmFile" #define CHMFILE_CLOSED ((void *)0x1) #if PY_MAJOR_VERSION < 3 #define YF "s" #else #define YF "y" #endif static struct chmFile *chmlib_get_chmfile(PyObject *chmfile_capsule) { if (!PyCapsule_IsValid(chmfile_capsule, CHMFILE_CAPSULE_NAME)) { PyErr_SetString(PyExc_ValueError, "Expected valid chmlib object"); return NULL; } struct chmFile *chmfile = (struct chmFile *)PyCapsule_GetPointer( chmfile_capsule, CHMFILE_CAPSULE_NAME); if (chmfile == CHMFILE_CLOSED) { PyErr_SetString(PyExc_RuntimeError, "chmlib object is closed"); return NULL; } return chmfile; } static void chmlib_chmfile_capsule_destructor(PyObject *chmfile_capsule) { struct chmFile *chmfile = chmlib_get_chmfile(chmfile_capsule); if (!chmfile) { PyErr_Clear(); return; } chm_close(chmfile); PyCapsule_SetPointer(chmfile_capsule, CHMFILE_CLOSED); } static PyObject *chmlib_chm_open(PyObject *self, PyObject *args) { const char *filename; if (!PyArg_ParseTuple(args, YF ":chmlib_chm_open", &filename)) return NULL; struct chmFile *chmfile = chm_open(filename); if (chmfile == NULL) Py_RETURN_NONE; return PyCapsule_New(chmfile, CHMFILE_CAPSULE_NAME, chmlib_chmfile_capsule_destructor); } static PyObject *chmlib_chm_close(PyObject *self, PyObject *args) { PyObject *chmfile_capsule; if (!PyArg_ParseTuple(args, "O:chmlib_chm_close", &chmfile_capsule)) return NULL; chmlib_chmfile_capsule_destructor(chmfile_capsule); Py_RETURN_NONE; } static PyObject *chmlib_chm_set_param(PyObject *self, PyObject *args) { PyObject *chmfile_capsule; long param_type, param_val; if (!PyArg_ParseTuple(args, "Oii:chmlib_chm_set_param", &chmfile_capsule, ¶m_type, ¶m_val)) return NULL; struct chmFile *chmfile = chmlib_get_chmfile(chmfile_capsule); if (!chmfile) return NULL; // It's the only available parameter, and it's not going to change any time // soon. if (param_type != 0) { PyErr_Format(PyExc_ValueError, "Expected CHM_PARAM_MAX_BLOCKS_CACHED (0), got %ld", param_type); return NULL; } // The value for CHM_PARAM_MAX_BLOCKS_CACHED parameter ultimately gets // assigned to Int32 cache_num_blocks if (param_val < 0 || param_val > 0x7fffffff) { PyErr_Format(PyExc_ValueError, "Expected value 0..2147483647, got %ld", param_val); return NULL; } chm_set_param(chmfile, (int)param_type, (int)param_val); Py_RETURN_NONE; } struct chmlib_enumerator_context { PyObject *chmfile_capsule; PyObject *py_enumerator; PyObject *py_context; int has_error; }; static PyObject *chmUnitInfoTuple(struct chmUnitInfo *ui) { return Py_BuildValue("(KKii" YF ")", ui->start, ui->length, ui->space, ui->flags, ui->path); } static void report_error(PyObject *type, const char *msg, PyObject *obj) { #if PY_MAJOR_VERSION < 3 PyObject *repr = PyObject_Repr(obj); if (!repr) PyErr_Format(type, "%s ", msg); else { PyErr_Format(type, "%s %s", msg, PyString_AsString(repr)); Py_DECREF(repr); } #else PyErr_Format(type, "%s %R", msg, obj); #endif } static int chmlib_chm_enumerator(struct chmFile *h, struct chmUnitInfo *ui, void *context) { struct chmlib_enumerator_context *ctx = context; long ret; PyObject *arglist = Py_BuildValue("(OOO)", ctx->chmfile_capsule, chmUnitInfoTuple(ui), ctx->py_context); PyObject *result = PyObject_CallObject(ctx->py_enumerator, arglist); Py_DECREF(arglist); if (result == NULL) goto fail; if (result == Py_None) { Py_DECREF(result); return CHM_ENUMERATOR_CONTINUE; } if ( #if PY_MAJOR_VERSION < 3 !PyInt_Check(result) && #endif !PyLong_Check(result)) { report_error(PyExc_RuntimeError, "chm_enumerate callback is expected to return " "integer or None, returned", result); goto fail2; } ret = PyLong_AsLong(result); if (ret == -1 && PyErr_Occurred() != NULL) goto fail2; return (int)ret; fail2: Py_DECREF(result); fail: ctx->has_error = 1; return CHM_ENUMERATOR_FAILURE; } static PyObject *chmlib_chm_enumerate_dir(PyObject *self, PyObject *args) { PyObject *chmfile_capsule; const char *prefix; int what; PyObject *enumerator; PyObject *context; int res; if (!PyArg_ParseTuple(args, "O" YF "iOO:chmlib_chm_enumerate", &chmfile_capsule, &prefix, &what, &enumerator, &context)) return NULL; struct chmFile *chmfile = chmlib_get_chmfile(chmfile_capsule); if (!chmfile) return NULL; if (!PyCallable_Check(enumerator)) { report_error(PyExc_TypeError, "A callable is expected for callback, got", enumerator); return NULL; } struct chmlib_enumerator_context ctx = { .chmfile_capsule = chmfile_capsule, .py_enumerator = enumerator, .py_context = context, }; res = chm_enumerate_dir(chmfile, prefix, what, chmlib_chm_enumerator, &ctx); if (ctx.has_error) return NULL; return PyLong_FromLong(res); } static PyObject *chmlib_chm_enumerate(PyObject *self, PyObject *args) { PyObject *chmfile_capsule; int what; PyObject *enumerator; PyObject *context; int res; if (!PyArg_ParseTuple(args, "OiOO:chmlib_chm_enumerate", &chmfile_capsule, &what, &enumerator, &context)) return NULL; struct chmFile *chmfile = chmlib_get_chmfile(chmfile_capsule); if (!chmfile) return NULL; if (!PyCallable_Check(enumerator)) { report_error(PyExc_TypeError, "A callable is expected for callback, got", enumerator); return NULL; } struct chmlib_enumerator_context ctx = { .chmfile_capsule = chmfile_capsule, .py_enumerator = enumerator, .py_context = context, }; res = chm_enumerate(chmfile, what, chmlib_chm_enumerator, &ctx); if (ctx.has_error) return NULL; return PyLong_FromLong(res); } static PyObject *chmlib_chm_resolve_object(PyObject *self, PyObject *args) { PyObject *chmfile_capsule; const char *path; struct chmUnitInfo ui; if (!PyArg_ParseTuple(args, "O" YF ":chmlib_chm_resolve_object", &chmfile_capsule, &path)) return NULL; struct chmFile *chmfile = chmlib_get_chmfile(chmfile_capsule); if (!chmfile) return NULL; if (chm_resolve_object(chmfile, path, &ui) == CHM_RESOLVE_FAILURE) { Py_RETURN_NONE; } return chmUnitInfoTuple(&ui); } static PyObject *chmlib_chm_retrieve_object(PyObject *self, PyObject *args) { PyObject *chmfile_capsule; unsigned long long uistart; unsigned long long uilength; int uispace; unsigned long long offset; long long length; long long res; char *buf; PyObject *pybuf; if (!PyArg_ParseTuple(args, "OKKiKL:chmlib_chm_retrieve_object", &chmfile_capsule, &uistart, &uilength, &uispace, &offset, &length)) return NULL; struct chmFile *chmfile = chmlib_get_chmfile(chmfile_capsule); if (!chmfile) return NULL; if (length < 0) { PyErr_Format(PyExc_ValueError, "Expected non-negative object length, got %lld", length); return NULL; } pybuf = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)length); if (!pybuf) return NULL; buf = PyBytes_AS_STRING(pybuf); struct chmUnitInfo ui = { .start = uistart, .length = uilength, .space = uispace, }; res = chm_retrieve_object(chmfile, &ui, (unsigned char *)buf, offset, length); if (res == 0) { Py_DECREF(pybuf); Py_RETURN_NONE; } if (res != length) { // error checking is not needed: pybuf is set to NULL in case of error _PyBytes_Resize(&pybuf, (Py_ssize_t)res); } return pybuf; } typedef struct { PyObject *cb; int has_error; } search_ctx; static int _search_cb(const char *topic, const char *url, void *context) { search_ctx *ctx = context; PyObject *arglist = Py_BuildValue("(" YF YF ")", topic, url); PyObject *result = PyObject_CallObject(ctx->cb, arglist); Py_DECREF(arglist); if (result == NULL) { ctx->has_error = 1; return -1; } Py_DECREF(result); return 0; } static PyObject *chmlib_search(PyObject *self, PyObject *args) { PyObject *chmfile_capsule; const char *text; int whole_words; int titles_only; PyObject *pycb; int ret; if (!PyArg_ParseTuple(args, "O" YF "iiO:chmlib_search", &chmfile_capsule, &text, &whole_words, &titles_only, &pycb)) return NULL; struct chmFile *chmfile = chmlib_get_chmfile(chmfile_capsule); if (!chmfile) return NULL; if (!PyCallable_Check(pycb)) { report_error(PyExc_TypeError, "A callable is expected for callback, got", pycb); return NULL; } search_ctx ctx = { .cb = pycb, }; ret = search(chmfile, text, whole_words, titles_only, _search_cb, &ctx); if (ctx.has_error) return NULL; return Py_BuildValue("i", ret); } static PyMethodDef chmlib_methods[] = { {"chm_open", chmlib_chm_open, METH_VARARGS, "Open a CHM file"}, {"chm_close", chmlib_chm_close, METH_VARARGS, "Open the CHM file"}, {"chm_enumerate", chmlib_chm_enumerate, METH_VARARGS, "Enumerate objects in CHM file"}, {"chm_set_param", chmlib_chm_set_param, METH_VARARGS, "Set parameters of CHM object"}, {"chm_enumerate_dir", chmlib_chm_enumerate_dir, METH_VARARGS, "Enumerate objects in CHM file"}, {"chm_resolve_object", chmlib_chm_resolve_object, METH_VARARGS, "Find the object by path in CHM file"}, {"chm_retrieve_object", chmlib_chm_retrieve_object, METH_VARARGS, "Get the object's content"}, {"search", chmlib_search, METH_VARARGS, "Search the CHM"}, {NULL}, }; #if PY_MAJOR_VERSION < 3 void init_chmlib(void) { Py_InitModule("_chmlib", chmlib_methods); } #else static struct PyModuleDef chmlib_module = { PyModuleDef_HEAD_INIT, "_chmlib", NULL, -1, chmlib_methods, }; PyMODINIT_FUNC PyInit__chmlib(void) { return PyModule_Create(&chmlib_module); } #endif pychm-0.8.6/chm/chmlib_search.h0000644000076500000240000000043013557041443017446 0ustar dottedmagstaff00000000000000#ifndef CHM_SEARCH_H #define CHM_SEARCH_H #include typedef int (*search_cb)(const char *topic, const char *url, void *context); int search(struct chmFile *chmfile, const char *text, int whole_words, int titles_only, search_cb cb, void *context); #endif pychm-0.8.6/chm/__init__.py0000644000076500000240000000246713574533162016642 0ustar dottedmagstaff00000000000000# Copyright (C) 2003-2006 Rubens Ramos # # pychm is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as # published by the Free Software Foundation; either version 2 of the # License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public # License along with this program; see the file COPYING. If not, # write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, # Boston, MA 02111-1307, USA # ''' chm - A package to manipulate CHM files The chm package provides four modules: chm, chmlib, extra and _chmlib. _chmlib and chmlib are very low level libraries generated from SWIG interface files, and are simple wrappers around the API defined by the C library chmlib. The extra module adds full-text search support. the chm module provides some higher level classes to simplify access to the CHM files information. ''' __all__ = ["chm", "chmlib", "_chmlib", "extra"] __version__ = "0.8.6" __revision__ = "$Id$" pychm-0.8.6/chm/search.c0000644000076500000240000002515613557072201016133 0ustar dottedmagstaff00000000000000#define PY_SSIZE_T_CLEAN #include #include "chmlib_search.h" #include #define false 0 #define true 1 #define FTS_HEADER_LEN 0x32 #define TOPICS_ENTRY_LEN 16 #define COMMON_BUF_LEN 1025 #define FREE(x) \ free(x); \ x = NULL static uint16_t get_uint16(uint8_t *b) { return b[0] | b[1] << 8; } static uint32_t get_uint32(uint8_t *b) { return b[0] | b[1] << 8 | b[2] << 16 | b[3] << 24; } static uint64_t be_encint(unsigned char *buffer, size_t *length) { uint64_t result = 0; int shift = 0; *length = 0; do { result |= ((*buffer) & 0x7f) << shift; shift += 7; *length = *length + 1; } while (*(buffer++) & 0x80); return result; } /* Finds the first unset bit in memory. Returns the number of set bits found. Returns -1 if the buffer runs out before we find an unset bit. */ static int ffus(unsigned char *byte, int *bit, size_t *length) { int bits = 0; *length = 0; while (*byte & (1 << *bit)) { if (*bit) --(*bit); else { ++byte; ++(*length); *bit = 7; } ++bits; } if (*bit) --(*bit); else { ++(*length); *bit = 7; } return bits; } static uint64_t sr_int(unsigned char *byte, int *bit, unsigned char s, unsigned char r, size_t *length) { uint64_t ret; unsigned char mask; int n, n_bits, num_bits, base, count; size_t fflen; *length = 0; if (!bit || *bit > 7 || s != 2) return ~(uint64_t)0; ret = 0; count = ffus(byte, bit, &fflen); *length += fflen; byte += *length; n_bits = n = r + (count ? count - 1 : 0); while (n > 0) { num_bits = n > *bit ? *bit : n - 1; base = n > *bit ? 0 : *bit - (n - 1); switch (num_bits) { case 0: mask = 1; break; case 1: mask = 3; break; case 2: mask = 7; break; case 3: mask = 0xf; break; case 4: mask = 0x1f; break; case 5: mask = 0x3f; break; case 6: mask = 0x7f; break; case 7: mask = 0xff; break; default: mask = 0xff; break; } mask <<= base; ret = (ret << (num_bits + 1)) | (uint64_t)((*byte & mask) >> base); if (n > *bit) { ++byte; ++(*length); n -= *bit + 1; *bit = 7; } else { *bit -= n; n = 0; } } if (count) ret |= (uint64_t)1 << n_bits; return ret; } static uint32_t get_leaf_node_offset(struct chmFile *chmfile, const char *text, uint32_t initial_offset, uint32_t buff_size, uint16_t tree_depth, struct chmUnitInfo *ui) { unsigned char word_len; unsigned char pos; uint16_t free_space; char *wrd_buf; char *word = NULL; uint32_t test_offset = 0; uint32_t i = sizeof(uint16_t); unsigned char *buffer = malloc(buff_size); if (NULL == buffer) return 0; while (--tree_depth) { if (initial_offset == test_offset) { FREE(buffer); return 0; } test_offset = initial_offset; if (chm_retrieve_object(chmfile, ui, buffer, initial_offset, buff_size) == 0) { FREE(buffer); return 0; } free_space = get_uint16(buffer); while (i < buff_size - free_space) { word_len = *(buffer + i); pos = *(buffer + i + 1); wrd_buf = malloc(word_len); memcpy(wrd_buf, buffer + i + 2, word_len - 1); wrd_buf[word_len - 1] = 0; if (pos == 0) { FREE(word); word = (char *)strdup(wrd_buf); } else { word = realloc(word, word_len + pos + 1); strcpy(word + pos, wrd_buf); } FREE(wrd_buf); if (strcasecmp(text, word) <= 0) { initial_offset = get_uint32(buffer + i + word_len + 1); break; } i += word_len + sizeof(unsigned char) + sizeof(uint32_t) + sizeof(uint16_t); } } if (initial_offset == test_offset) initial_offset = 0; FREE(word); FREE(buffer); return initial_offset; } static int pychm_process_wlc(struct chmFile *chmfile, uint64_t wlc_count, uint64_t wlc_size, uint32_t wlc_offset, unsigned char ds, unsigned char dr, unsigned char cs, unsigned char cr, unsigned char ls, unsigned char lr, struct chmUnitInfo *uimain, struct chmUnitInfo *uitbl, struct chmUnitInfo *uistrings, struct chmUnitInfo *topics, struct chmUnitInfo *urlstr, search_cb cb, void *context) { uint32_t stroff, urloff; uint64_t i, j, count; size_t length; int wlc_bit = 7; size_t off = 0; uint64_t index = 0; unsigned char entry[TOPICS_ENTRY_LEN]; unsigned char combuf[COMMON_BUF_LEN]; unsigned char *buffer = malloc((size_t)wlc_size); char *url = NULL; char *topic = NULL; if (chm_retrieve_object(chmfile, uimain, buffer, wlc_offset, wlc_size) == 0) { FREE(buffer); return false; } for (i = 0; i < wlc_count; ++i) { if (wlc_bit != 7) { ++off; wlc_bit = 7; } index += sr_int(buffer + off, &wlc_bit, ds, dr, &length); off += length; if (chm_retrieve_object(chmfile, topics, entry, index * 16, TOPICS_ENTRY_LEN) == 0) { FREE(topic); FREE(url); FREE(buffer); return false; } combuf[COMMON_BUF_LEN - 1] = 0; stroff = get_uint32(entry + 4); FREE(topic); if (chm_retrieve_object(chmfile, uistrings, combuf, stroff, COMMON_BUF_LEN - 1) == 0) { topic = strdup("Untitled in index"); } else { combuf[COMMON_BUF_LEN - 1] = 0; topic = strdup((char *)combuf); } urloff = get_uint32(entry + 8); if (chm_retrieve_object(chmfile, uitbl, combuf, urloff, 12) == 0) { FREE(buffer); return false; } urloff = get_uint32(combuf + 8); if (chm_retrieve_object(chmfile, urlstr, combuf, urloff + 8, COMMON_BUF_LEN - 1) == 0) { FREE(topic); FREE(url); FREE(buffer); return false; } combuf[COMMON_BUF_LEN - 1] = 0; FREE(url); url = strdup((char *)combuf); if (topic && url) { if (cb(topic, url, context) == -1) return -1; } count = sr_int(buffer + off, &wlc_bit, cs, cr, &length); off += length; for (j = 0; j < count; ++j) { sr_int(buffer + off, &wlc_bit, ls, lr, &length); off += length; } } FREE(topic); FREE(url); FREE(buffer); return true; } int search(struct chmFile *chmfile, const char *text, int whole_words, int titles_only, search_cb cb, void *context) { unsigned char header[FTS_HEADER_LEN]; unsigned char doc_index_s; unsigned char doc_index_r; unsigned char code_count_s; unsigned char code_count_r; unsigned char loc_codes_s; unsigned char loc_codes_r; unsigned char word_len, pos; unsigned char *buffer; char *word = NULL; uint32_t node_offset; uint32_t node_len; uint16_t tree_depth; uint32_t i; uint16_t free_space; uint64_t wlc_count, wlc_size; uint32_t wlc_offset; char *wrd_buf; unsigned char title; size_t encsz; struct chmUnitInfo ui, uitopics, uiurltbl, uistrings, uiurlstr; int partial = false; if (NULL == text) return -1; if (chm_resolve_object(chmfile, "/$FIftiMain", &ui) != CHM_RESOLVE_SUCCESS || chm_resolve_object(chmfile, "/#TOPICS", &uitopics) != CHM_RESOLVE_SUCCESS || chm_resolve_object(chmfile, "/#STRINGS", &uistrings) != CHM_RESOLVE_SUCCESS || chm_resolve_object(chmfile, "/#URLTBL", &uiurltbl) != CHM_RESOLVE_SUCCESS || chm_resolve_object(chmfile, "/#URLSTR", &uiurlstr) != CHM_RESOLVE_SUCCESS) return false; if (chm_retrieve_object(chmfile, &ui, header, 0, FTS_HEADER_LEN) == 0) return false; doc_index_s = header[0x1E]; doc_index_r = header[0x1F]; code_count_s = header[0x20]; code_count_r = header[0x21]; loc_codes_s = header[0x22]; loc_codes_r = header[0x23]; if (doc_index_s != 2 || code_count_s != 2 || loc_codes_s != 2) { return false; } node_offset = get_uint32(header + 0x14); node_len = get_uint32(header + 0x2e); tree_depth = get_uint16(header + 0x18); i = sizeof(uint16_t); buffer = malloc(node_len); node_offset = get_leaf_node_offset(chmfile, text, node_offset, node_len, tree_depth, &ui); if (!node_offset) { FREE(buffer); return false; } do { if (chm_retrieve_object(chmfile, &ui, buffer, node_offset, node_len) == 0) { FREE(word); FREE(buffer); return false; } free_space = get_uint16(buffer + 6); i = sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint16_t); encsz = 0; while (i < node_len - free_space) { word_len = *(buffer + i); pos = *(buffer + i + 1); wrd_buf = malloc(word_len); memcpy(wrd_buf, buffer + i + 2, word_len - 1); wrd_buf[word_len - 1] = 0; if (pos == 0) { FREE(word); word = (char *)strdup(wrd_buf); } else { word = realloc(word, word_len + pos + 1); strcpy(word + pos, wrd_buf); } FREE(wrd_buf); i += 2 + word_len; title = *(buffer + i - 1); wlc_count = be_encint(buffer + i, &encsz); i += encsz; wlc_offset = get_uint32(buffer + i); i += sizeof(uint32_t) + sizeof(uint16_t); wlc_size = be_encint(buffer + i, &encsz); i += encsz; node_offset = get_uint32(buffer); if (!title && titles_only) continue; if (whole_words && !strcasecmp(text, word)) { partial = pychm_process_wlc( chmfile, wlc_count, wlc_size, wlc_offset, doc_index_s, doc_index_r, code_count_s, code_count_r, loc_codes_s, loc_codes_r, &ui, &uiurltbl, &uistrings, &uitopics, &uiurlstr, cb, context); FREE(word); FREE(buffer); return partial; } if (!whole_words) { if (!strncasecmp(word, text, strlen(text))) { partial = true; int ret = pychm_process_wlc( chmfile, wlc_count, wlc_size, wlc_offset, doc_index_s, doc_index_r, code_count_s, code_count_r, loc_codes_s, loc_codes_r, &ui, &uiurltbl, &uistrings, &uitopics, &uiurlstr, cb, context); if (ret == -1) { FREE(word); FREE(buffer); return -1; } } else if (strncasecmp(text, word, strlen(text)) < -1) break; } } } while (!whole_words && !strncmp(word, text, strlen(text)) && node_offset); FREE(word); FREE(buffer); return partial; } pychm-0.8.6/chm/chm.py0000644000076500000240000005111113557067425015645 0ustar dottedmagstaff00000000000000# Copyright (C) 2003-2006 Rubens Ramos # # Based on code by: # Copyright (C) 2003 Razvan Cojocaru # # pychm is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as # published by the Free Software Foundation; either version 2 of the # License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public # License along with this program; see the file COPYING. If not, # write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, # Boston, MA 02111-1307, USA ''' chm - A high-level front end for the chmlib python module. The chm module provides high level access to the functionality included in chmlib. It encapsulates functions in the CHMFile class, and provides some additional features, such as the ability to obtain the contents tree of a CHM archive. ''' from . import chmlib from . import extra import array import posixpath import sys charset_table = { 0: b'iso8859_1', # ANSI_CHARSET 238: b'iso8859_2', # EASTEUROPE_CHARSET 178: b'iso8859_6', # ARABIC_CHARSET 161: b'iso8859_7', # GREEK_CHARSET 177: b'iso8859_8', # HEBREW_CHARSET 162: b'iso8859_9', # TURKISH_CHARSET 222: b'iso8859_11', # THAI_CHARSET 186: b'iso8859_13', # BALTIC_CHARSET 204: b'cp1251', # RUSSIAN_CHARSET 255: b'cp437', # OEM_CHARSET 128: b'cp932', # SHIFTJIS_CHARSET 134: b'cp936', # GB2312_CHARSET 129: b'cp949', # HANGUL_CHARSET 136: b'cp950', # CHINESEBIG5_CHARSET 1: None, # DEFAULT_CHARSET 2: None, # SYMBOL_CHARSET 130: None, # JOHAB_CHARSET 163: None, # VIETNAMESE_CHARSET 77: None, # MAC_CHARSET } locale_table = { 0x0436: (b'iso8859_1', b'Afrikaans', b'Western Europe & US'), 0x041c: (b'iso8859_2', b'Albanian', b'Central Europe'), 0x0401: (b'iso8859_6', b'Arabic_Saudi_Arabia', b'Arabic'), 0x0801: (b'iso8859_6', b'Arabic_Iraq', b'Arabic'), 0x0c01: (b'iso8859_6', b'Arabic_Egypt', b'Arabic'), 0x1001: (b'iso8859_6', b'Arabic_Libya', b'Arabic'), 0x1401: (b'iso8859_6', b'Arabic_Algeria', b'Arabic'), 0x1801: (b'iso8859_6', b'Arabic_Morocco', b'Arabic'), 0x1c01: (b'iso8859_6', b'Arabic_Tunisia', b'Arabic'), 0x2001: (b'iso8859_6', b'Arabic_Oman', b'Arabic'), 0x2401: (b'iso8859_6', b'Arabic_Yemen', b'Arabic'), 0x2801: (b'iso8859_6', b'Arabic_Syria', b'Arabic'), 0x2c01: (b'iso8859_6', b'Arabic_Jordan', b'Arabic'), 0x3001: (b'iso8859_6', b'Arabic_Lebanon', b'Arabic'), 0x3401: (b'iso8859_6', b'Arabic_Kuwait', b'Arabic'), 0x3801: (b'iso8859_6', b'Arabic_UAE', b'Arabic'), 0x3c01: (b'iso8859_6', b'Arabic_Bahrain', b'Arabic'), 0x4001: (b'iso8859_6', b'Arabic_Qatar', b'Arabic'), 0x042b: (None, b'Armenian', b'Armenian'), 0x042c: (b'iso8859_9', b'Azeri_Latin', b'Turkish'), 0x082c: (b'cp1251', b'Azeri_Cyrillic', b'Cyrillic'), 0x042d: (b'iso8859_1', b'Basque', b'Western Europe & US'), 0x0423: (b'cp1251', b'Belarusian', b'Cyrillic'), 0x0402: (b'cp1251', b'Bulgarian', b'Cyrillic'), 0x0403: (b'iso8859_1', b'Catalan', b'Western Europe & US'), 0x0404: (b'cp950', b'Chinese_Taiwan', b'Traditional Chinese'), 0x0804: (b'cp936', b'Chinese_PRC', b'Simplified Chinese'), 0x0c04: (b'cp950', b'Chinese_Hong_Kong', b'Traditional Chinese'), 0x1004: (b'cp936', b'Chinese_Singapore', b'Simplified Chinese'), 0x1404: (b'cp950', b'Chinese_Macau', b'Traditional Chinese'), 0x041a: (b'iso8859_2', b'Croatian', b'Central Europe'), 0x0405: (b'iso8859_2', b'Czech', b'Central Europe'), 0x0406: (b'iso8859_1', b'Danish', b'Western Europe & US'), 0x0413: (b'iso8859_1', b'Dutch_Standard', b'Western Europe & US'), 0x0813: (b'iso8859_1', b'Dutch_Belgian', b'Western Europe & US'), 0x0409: (b'iso8859_1', b'English_United_States', b'Western Europe & US'), 0x0809: (b'iso8859_1', b'English_United_Kingdom', b'Western Europe & US'), 0x0c09: (b'iso8859_1', b'English_Australian', b'Western Europe & US'), 0x1009: (b'iso8859_1', b'English_Canadian', b'Western Europe & US'), 0x1409: (b'iso8859_1', b'English_New_Zealand', b'Western Europe & US'), 0x1809: (b'iso8859_1', b'English_Irish', b'Western Europe & US'), 0x1c09: (b'iso8859_1', b'English_South_Africa', b'Western Europe & US'), 0x2009: (b'iso8859_1', b'English_Jamaica', b'Western Europe & US'), 0x2409: (b'iso8859_1', b'English_Caribbean', b'Western Europe & US'), 0x2809: (b'iso8859_1', b'English_Belize', b'Western Europe & US'), 0x2c09: (b'iso8859_1', b'English_Trinidad', b'Western Europe & US'), 0x3009: (b'iso8859_1', b'English_Zimbabwe', b'Western Europe & US'), 0x3409: (b'iso8859_1', b'English_Philippines', b'Western Europe & US'), 0x0425: (b'iso8859_13', b'Estonian', b'Baltic'), 0x0438: (b'iso8859_1', b'Faeroese', b'Western Europe & US'), 0x0429: (b'iso8859_6', b'Farsi', b'Arabic'), 0x040b: (b'iso8859_1', b'Finnish', b'Western Europe & US'), 0x040c: (b'iso8859_1', b'French_Standard', b'Western Europe & US'), 0x080c: (b'iso8859_1', b'French_Belgian', b'Western Europe & US'), 0x0c0c: (b'iso8859_1', b'French_Canadian', b'Western Europe & US'), 0x100c: (b'iso8859_1', b'French_Swiss', b'Western Europe & US'), 0x140c: (b'iso8859_1', b'French_Luxembourg', b'Western Europe & US'), 0x180c: (b'iso8859_1', b'French_Monaco', b'Western Europe & US'), 0x0437: (None, b'Georgian', b'Georgian'), 0x0407: (b'iso8859_1', b'German_Standard', b'Western Europe & US'), 0x0807: (b'iso8859_1', b'German_Swiss', b'Western Europe & US'), 0x0c07: (b'iso8859_1', b'German_Austrian', b'Western Europe & US'), 0x1007: (b'iso8859_1', b'German_Luxembourg', b'Western Europe & US'), 0x1407: (b'iso8859_1', b'German_Liechtenstein', b'Western Europe & US'), 0x0408: (b'iso8859_7', b'Greek', b'Greek'), 0x040d: (b'iso8859_8', b'Hebrew', b'Hebrew'), 0x0439: (None, b'Hindi', b'Indic'), 0x040e: (b'iso8859_2', b'Hungarian', b'Central Europe'), 0x040f: (b'iso8859_1', b'Icelandic', b'Western Europe & US'), 0x0421: (b'iso8859_1', b'Indonesian', b'Western Europe & US'), 0x0410: (b'iso8859_1', b'Italian_Standard', b'Western Europe & US'), 0x0810: (b'iso8859_1', b'Italian_Swiss', b'Western Europe & US'), 0x0411: (b'cp932', b'Japanese', b'Japanese'), 0x043f: (b'cp1251', b'Kazakh', b'Cyrillic'), 0x0457: (None, b'Konkani', b'Indic'), 0x0412: (b'cp949', b'Korean', b'Korean'), 0x0426: (b'iso8859_13', b'Latvian', b'Baltic'), 0x0427: (b'iso8859_13', b'Lithuanian', b'Baltic'), 0x042f: (b'cp1251', b'Macedonian', b'Cyrillic'), 0x043e: (b'iso8859_1', b'Malay_Malaysia', b'Western Europe & US'), 0x083e: (b'iso8859_1', b'Malay_Brunei_Darussalam', b'Western Europe & US'), 0x044e: (None, b'Marathi', b'Indic'), 0x0414: (b'iso8859_1', b'Norwegian_Bokmal', b'Western Europe & US'), 0x0814: (b'iso8859_1', b'Norwegian_Nynorsk', b'Western Europe & US'), 0x0415: (b'iso8859_2', b'Polish', b'Central Europe'), 0x0416: (b'iso8859_1', b'Portuguese_Brazilian', b'Western Europe & US'), 0x0816: (b'iso8859_1', b'Portuguese_Standard', b'Western Europe & US'), 0x0418: (b'iso8859_2', b'Romanian', b'Central Europe'), 0x0419: (b'cp1251', b'Russian', b'Cyrillic'), 0x044f: (None, b'Sanskrit', b'Indic'), 0x081a: (b'iso8859_2', b'Serbian_Latin', b'Central Europe'), 0x0c1a: (b'cp1251', b'Serbian_Cyrillic', b'Cyrillic'), 0x041b: (b'iso8859_2', b'Slovak', b'Central Europe'), 0x0424: (b'iso8859_2', b'Slovenian', b'Central Europe'), 0x040a: (b'iso8859_1', b'Spanish_Trad_Sort', b'Western Europe & US'), 0x080a: (b'iso8859_1', b'Spanish_Mexican', b'Western Europe & US'), 0x0c0a: (b'iso8859_1', b'Spanish_Modern_Sort', b'Western Europe & US'), 0x100a: (b'iso8859_1', b'Spanish_Guatemala', b'Western Europe & US'), 0x140a: (b'iso8859_1', b'Spanish_Costa_Rica', b'Western Europe & US'), 0x180a: (b'iso8859_1', b'Spanish_Panama', b'Western Europe & US'), 0x1c0a: (b'iso8859_1', b'Spanish_Dominican_Repub', b'Western Europe & US'), 0x200a: (b'iso8859_1', b'Spanish_Venezuela', b'Western Europe & US'), 0x240a: (b'iso8859_1', b'Spanish_Colombia', b'Western Europe & US'), 0x280a: (b'iso8859_1', b'Spanish_Peru', b'Western Europe & US'), 0x2c0a: (b'iso8859_1', b'Spanish_Argentina', b'Western Europe & US'), 0x300a: (b'iso8859_1', b'Spanish_Ecuador', b'Western Europe & US'), 0x340a: (b'iso8859_1', b'Spanish_Chile', b'Western Europe & US'), 0x380a: (b'iso8859_1', b'Spanish_Uruguay', b'Western Europe & US'), 0x3c0a: (b'iso8859_1', b'Spanish_Paraguay', b'Western Europe & US'), 0x400a: (b'iso8859_1', b'Spanish_Bolivia', b'Western Europe & US'), 0x440a: (b'iso8859_1', b'Spanish_El_Salvador', b'Western Europe & US'), 0x480a: (b'iso8859_1', b'Spanish_Honduras', b'Western Europe & US'), 0x4c0a: (b'iso8859_1', b'Spanish_Nicaragua', b'Western Europe & US'), 0x500a: (b'iso8859_1', b'Spanish_Puerto_Rico', b'Western Europe & US'), 0x0441: (b'iso8859_1', b'Swahili', b'Western Europe & US'), 0x041d: (b'iso8859_1', b'Swedish', b'Western Europe & US'), 0x081d: (b'iso8859_1', b'Swedish_Finland', b'Western Europe & US'), 0x0449: (None, b'Tamil', b'Indic'), 0x0444: (b'cp1251', b'Tatar', b'Cyrillic'), 0x041e: (b'iso8859_11', b'Thai', b'Thai'), 0x041f: (b'iso8859_9', b'Turkish', b'Turkish'), 0x0422: (b'cp1251', b'Ukrainian', b'Cyrillic'), 0x0420: (b'iso8859_6', b'Urdu', b'Arabic'), 0x0443: (b'iso8859_9', b'Uzbek_Latin', b'Turkish'), 0x0843: (b'cp1251', b'Uzbek_Cyrillic', b'Cyrillic'), 0x042a: (None, b'Vietnamese', b'Vietnamese') } class CHMFile: "A class to manage access to CHM files." filename = b'' file = None title = b'' home = b'/' index = None topics = None encoding = None lcid = None binaryindex = None def __init__(self): self.searchable = 0 def LoadCHM(self, archiveName): '''Loads a CHM archive. This function will also call GetArchiveInfo to obtain information such as the index file name and the topics file. It returns 1 on success, and 0 if it fails. ''' if self.filename != b'': self.CloseCHM() if isinstance(archiveName, str): archiveName = archiveName.encode(sys.getfilesystemencoding()) self.file = chmlib.chm_open(archiveName) if self.file is None: return 0 self.filename = archiveName self.GetArchiveInfo() return 1 def CloseCHM(self): '''Closes the CHM archive. This function will close the CHM file, if it is open. All variables are also reset. ''' if self.filename != b'': chmlib.chm_close(self.file) self.file = None self.filename = b'' self.title = b'' self.home = b'/' self.index = None self.topics = None self.encoding = None def GetArchiveInfo(self): '''Obtains information on CHM archive. This function checks the /#SYSTEM file inside the CHM archive to obtain the index, home page, topics, encoding and title. It is called from LoadCHM. ''' self.searchable = extra.is_searchable(self.file) self.lcid = None result, ui = chmlib.chm_resolve_object(self.file, b'/#SYSTEM') if (result != chmlib.CHM_RESOLVE_SUCCESS): return 0 size, text = chmlib.chm_retrieve_object(self.file, ui, 4, ui.length) if (size == 0): return 0 buff = array.array('B', text) index = 0 while (index < size): cursor = buff[index] + (buff[index+1] * 256) if (cursor == 0): index += 2 cursor = buff[index] + (buff[index+1] * 256) index += 2 self.topics = b'/' + text[index:index+cursor-1] elif (cursor == 1): index += 2 cursor = buff[index] + (buff[index+1] * 256) index += 2 self.index = b'/' + text[index:index+cursor-1] elif (cursor == 2): index += 2 cursor = buff[index] + (buff[index+1] * 256) index += 2 self.home = b'/' + text[index:index+cursor-1] elif (cursor == 3): index += 2 cursor = buff[index] + (buff[index+1] * 256) index += 2 self.title = text[index:index+cursor-1] elif (cursor == 4): index += 2 cursor = buff[index] + (buff[index+1] * 256) index += 2 self.lcid = buff[index] + (buff[index+1] * 256) elif (cursor == 6): index += 2 cursor = buff[index] + (buff[index+1] * 256) index += 2 tmp = text[index:index+cursor-1] if not self.topics: tmp1 = b'/' + tmp + b'.hhc' tmp2 = b'/' + tmp + b'.hhk' res1, ui1 = chmlib.chm_resolve_object(self.file, tmp1) res2, ui2 = chmlib.chm_resolve_object(self.file, tmp2) if not self.topics and res1 == chmlib.CHM_RESOLVE_SUCCESS: self.topics = b'/' + tmp + b'.hhc' if not self.index and res2 == chmlib.CHM_RESOLVE_SUCCESS: self.index = b'/' + tmp + b'.hhk' elif (cursor == 16): index += 2 cursor = buff[index] + (buff[index+1] * 256) index += 2 self.encoding = text[index:index+cursor-1] else: index += 2 cursor = buff[index] + (buff[index+1] * 256) index += 2 index += cursor self.GetWindowsInfo() if not self.lcid: self.lcid = extra.get_lcid(self.file) return 1 def GetTopicsTree(self): '''Reads and returns the topics tree. This auxiliary function reads and returns the topics tree file contents for the CHM archive. ''' if self.topics is None: return None if self.topics: res, ui = chmlib.chm_resolve_object(self.file, self.topics) if (res != chmlib.CHM_RESOLVE_SUCCESS): return None size, text = chmlib.chm_retrieve_object(self.file, ui, 0, ui.length) if (size == 0): return None return text def GetIndex(self): '''Reads and returns the index tree. This auxiliary function reads and returns the index tree file contents for the CHM archive. ''' if self.index is None: return None if self.index: res, ui = chmlib.chm_resolve_object(self.file, self.index) if (res != chmlib.CHM_RESOLVE_SUCCESS): return None size, text = chmlib.chm_retrieve_object(self.file, ui, 0, ui.length) if (size == 0): return None return text def ResolveObject(self, document): '''Tries to locate a document in the archive. This function tries to locate the document inside the archive. It returns a tuple where the first element is zero if the function was successful, and the second is the UnitInfo for that document. The UnitInfo is used to retrieve the document contents ''' if self.file: path = posixpath.normpath(document) return chmlib.chm_resolve_object(self.file, path) else: return (1, None) def RetrieveObject(self, ui, start=-1, length=-1): '''Retrieves the contents of a document. This function takes a UnitInfo and two optional arguments, the first being the start address and the second is the length. These define the amount of data to be read from the archive. ''' if self.file and ui: if start == -1: offset = 0 else: offset = start if length == -1: size = ui.length - offset else: size = length content = b'' while size > 0: (sz, chunk) = chmlib.chm_retrieve_object(self.file, ui, offset, size) if sz == 0: return (0, b'') content += chunk[:sz] size -= sz offset += sz return (len(content), content) else: return (0, b'') def Search(self, text, wholewords=0, titleonly=0): '''Performs full-text search on the archive. The first parameter is the word to look for, the second indicates if the search should be for whole words only, and the third parameter indicates if the search should be restricted to page titles. This method will return a tuple, the first item indicating if the search results were partial, and the second item being a dictionary containing the results.''' if text and text != b'' and self.file: return extra.search(self.file, text, wholewords, titleonly) else: return None def IsSearchable(self): '''Indicates if the full-text search is available for this archive - this flag is updated when GetArchiveInfo is called''' return self.searchable def GetEncoding(self): '''Returns a string that can be used with the codecs python package to encode or decode the files in the chm archive. If an error is found, or if it is not possible to find the encoding, None is returned.''' if self.encoding: vals = self.encoding.split(b',') if len(vals) > 2: try: return charset_table[int(vals[2])] except KeyError: pass return None def GetLCID(self): '''Returns the archive Locale ID''' if self.lcid in locale_table: return locale_table[self.lcid] else: return None def GetDWORD(self, buff, idx=0): '''Internal method. Reads a double word (4 bytes) from a buffer. ''' result = buff[idx] + (buff[idx+1] << 8) + (buff[idx+2] << 16) + \ (buff[idx+3] << 24) if result == 0xFFFFFFFF: result = 0 return result def GetString(self, text, idx): '''Internal method. Retrieves a string from the #STRINGS buffer. ''' next = text.find(b'\x00', idx) chunk = text[idx:next] return chunk def GetWindowsInfo(self): '''Gets information from the #WINDOWS file. Checks the #WINDOWS file to see if it has any info that was not found in #SYSTEM (topics, index or default page. ''' result, ui = chmlib.chm_resolve_object(self.file, b'/#WINDOWS') if (result != chmlib.CHM_RESOLVE_SUCCESS): return -1 size, text = chmlib.chm_retrieve_object(self.file, ui, 0, 8) if (size < 8): return -2 buff = array.array('B', text) num_entries = self.GetDWORD(buff, 0) entry_size = self.GetDWORD(buff, 4) if num_entries < 1: return -3 size, text = chmlib.chm_retrieve_object(self.file, ui, 8, entry_size) if (size < entry_size): return -4 buff = array.array('B', text) toc_index = self.GetDWORD(buff, 0x60) idx_index = self.GetDWORD(buff, 0x64) dft_index = self.GetDWORD(buff, 0x68) result, ui = chmlib.chm_resolve_object(self.file, b'/#STRINGS') if (result != chmlib.CHM_RESOLVE_SUCCESS): return -5 size, text = chmlib.chm_retrieve_object(self.file, ui, 0, ui.length) if (size == 0): return -6 if (not self.topics): self.topics = self.GetString(text, toc_index) if not self.topics.startswith(b'/'): self.topics = b'/' + self.topics if (not self.index): self.index = self.GetString(text, idx_index) if not self.index.startswith(b'/'): self.index = b'/' + self.index if (dft_index != 0): self.home = self.GetString(text, dft_index) if not self.home.startswith(b'/'): self.home = b'/' + self.home pychm-0.8.6/chm/chmlib.py0000644000076500000240000000333513557073173016336 0ustar dottedmagstaff00000000000000from collections import namedtuple from . import _chmlib import sys # Python 2 compatibility try: unicode except: unicode = str CHM_UNCOMPRESSED = 0 CHM_COMPRESSED = 1 chmUnitInfo = namedtuple('chmUnitInfo', ['start', 'length', 'space', 'flags', 'path']) def chm_open(filename): if isinstance(filename, unicode): filename = filename.encode(sys.getfilesystemencoding()) return _chmlib.chm_open(filename) def chm_close(h): _chmlib.chm_close(h) CHM_PARAM_MAX_BLOCKS_CACHED = 0 def chm_set_param(h, paramType, paramVal): _chmlib.chm_set_param(paramType, paramVal) CHM_ENUMERATE_NORMAL = 1 CHM_ENUMERATE_META = 2 CHM_ENUMERATE_SPECIAL = 4 CHM_ENUMERATE_FILES = 8 CHM_ENUMERATE_DIRS = 16 CHM_ENUMERATE_ALL = 31 CHM_ENUMERATOR_FAILURE = 0 CHM_ENUMERATOR_CONTINUE = 1 CHM_ENUMERATOR_SUCCESS = 2 def chm_enumerate(h, what, e, context): def enumerator(ctx, ui, context): return e(ctx, chmUnitInfo._make(ui), context) return _chmlib.chm_enumerate(h, what, enumerator, context) def chm_enumerate_dir(h, prefix, what, e, context): def enumerator(ctx, ui, context): return e(ctx, chmUnitInfo._make(ui), context) return _chmlib.chm_enumerate_dir(h, prefix, what, enumerator, context) CHM_RESOLVE_SUCCESS = 0 CHM_RESOLVE_FAILURE = 1 def chm_resolve_object(h, path): out = _chmlib.chm_resolve_object(h, path) if out is not None: return CHM_RESOLVE_SUCCESS, chmUnitInfo._make(out) return CHM_RESOLVE_FAILURE, None def chm_retrieve_object(h, ui, addr, length): buf = _chmlib.chm_retrieve_object(h, ui.start, ui.length, ui.space, addr, length) if buf is None: return 0, None return len(buf), buf pychm-0.8.6/COPYING0000644000076500000240000003542413526272004015004 0ustar dottedmagstaff00000000000000 GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Library General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS pychm-0.8.6/setup.py0000644000076500000240000000123513574533162015464 0ustar dottedmagstaff00000000000000from setuptools import setup, Extension with open("README", "r") as fh: long_description = fh.read() setup(name="pychm", version="0.8.6", description="Python package to handle CHM files", author="Rubens Ramos", author_email="rubensr@users.sourceforge.net", maintainer="Mikhail Gusarov", maintainer_email="dottedmag@dottedmag.net", url="https://github.com/dottedmag/pychm", license="GPL", long_description=long_description, py_modules=["chm.chm", "chm.chmlib", "chm.extra"], ext_modules=[Extension("chm._chmlib", ["chm/_chmlib.c", "chm/search.c"], libraries=["chm"])]) pychm-0.8.6/NEWS0000644000076500000240000000474013574533162014455 0ustar dottedmagstaff000000000000000.8.6 * Fix crash on Windows, Linux introduced in 0.8.5 (#7) * Restore compatibility with Python 2.7 (malformed runtime error messages, compilation warnings) (#6) --------------------------------------------------------------------------- 0.8.5 * Get rid of SWIG * Python 3 support --------------------------------------------------------------------------- 0.8.4.1 * New maintainer (Mikhail Gusarov) * Fix compilation under OS X and Windows. --------------------------------------------------------------------------- 0.8.4 * Word search (does not apply to whole words) is not case sensitive anymore (Glenn Washburn) * Fixed cases where contents tab was not being displayed (#1563936). --------------------------------------------------------------------------- 0.8.3 * Fixing identification of Cyrillic language (cp1251). * Bug #1464957: Using #WINDOWS and #STRINGS to obtain default page. (this caused gnochm to crash when opening certain files, or to simply show nothing as the startup page) --------------------------------------------------------------------------- 0.8.2 * Identification of index file was slightly improved. --------------------------------------------------------------------------- 0.8.1 * Bug #1039336: Chinese characters can't be displayed correctly Improved detection of character set encoding --------------------------------------------------------------------------- 0.8.0 * Added support to access the LCID from a CHM archive - this functionality is now required by gnochm 0.8.0 --------------------------------------------------------------------------- 0.7.0 * Added support to search for words inside CHM archives. This was implemented in a separate module (chm.extra) and is used in chm.chm. * Added method to CHMFile to read the archive encoding, so that python codecs can be used to encode/decode contents of html files. --------------------------------------------------------------------------- 0.6.0 * Removed IndexParser and other tree-related classes and functions from chm.py (chm.chm), because the tree structure was causing memory leaks in gnochm. GetIndex and GetTopics now return raw text. HTML parsing is not done inside the chm package anymore. * Documentation added to the Python wrappers in chm.py --------------------------------------------------------------------------- 0.5.0 * First public release --------------------------------------------------------------------------- pychm-0.8.6/pychm.egg-info/0000755000076500000240000000000013574533170016562 5ustar dottedmagstaff00000000000000pychm-0.8.6/pychm.egg-info/PKG-INFO0000644000076500000240000000174613574533167017675 0ustar dottedmagstaff00000000000000Metadata-Version: 1.0 Name: pychm Version: 0.8.6 Summary: Python package to handle CHM files Home-page: https://github.com/dottedmag/pychm Author: Mikhail Gusarov Author-email: dottedmag@dottedmag.net License: GPL Description: PyCHM ===== PyCHM is a Python library to manipulate CHM files (Microsoft HTML Help). This library supports Python 2.7 and Python 3.5+. It is in a maintenance mode and accepts only security and bug fixes. API --- The chm package contains four modules: * chm.chm: High-level support for CHM archives. * chm.extra: Extra utility functions - full-text search support, encoding detection. * chm.chmlib: Low level wrappers around the chmlib API (Python part). * chm._chmlib: Low level wrappers around the chmlib API (C part). This module is unstable and subject to change without notice. Platform: UNKNOWN pychm-0.8.6/pychm.egg-info/SOURCES.txt0000644000076500000240000000041113574533170020442 0ustar dottedmagstaff00000000000000COPYING LICENSE MANIFEST.in NEWS README setup.py chm/__init__.py chm/_chmlib.c chm/chm.py chm/chmlib.py chm/chmlib_search.h chm/extra.py chm/search.c pychm.egg-info/PKG-INFO pychm.egg-info/SOURCES.txt pychm.egg-info/dependency_links.txt pychm.egg-info/top_level.txtpychm-0.8.6/pychm.egg-info/top_level.txt0000644000076500000240000000000413574533167021314 0ustar dottedmagstaff00000000000000chm pychm-0.8.6/pychm.egg-info/dependency_links.txt0000644000076500000240000000000113574533167022636 0ustar dottedmagstaff00000000000000 pychm-0.8.6/setup.cfg0000644000076500000240000000007313574533170015571 0ustar dottedmagstaff00000000000000[egg_info] tag_build = tag_date = 0 tag_svn_revision = 0