static SCM gunzip(SCM smob) { DEFLATE_BLOB *blob; char *buf; int ret; SCM out; z_stream strm; blob = (DEFLATE_BLOB *)SCM_SMOB_DATA(smob); strm.zalloc = Z_NULL; strm.zfree = Z_NULL; strm.opaque = Z_NULL; strm.avail_in = 0; strm.next_in = Z_NULL; ret = inflateInit(&strm); if (ret != Z_OK) { log_msg("zlib: inflate init failed\n"); return SCM_BOOL_F; } buf = (char *)malloc(blob->orig_len); strm.total_in = strm.avail_in = blob->zip_len; strm.total_out = strm.avail_out = blob->orig_len; strm.next_in = (unsigned char *)blob->payload; strm.next_out = (unsigned char *)buf; inflate(&strm, Z_FINISH); inflateEnd(&strm); out = scm_from_utf8_stringn(buf, blob->orig_len); free(buf); return out; scm_remember_upto_here_1(out); }
/* A tricky optimization, but probably worth it. */ unsigned long scm_i_utf8_string_hash (const char *str, size_t len) { const scm_t_uint8 *end, *ustr = (const scm_t_uint8 *) str; unsigned long ret; /* The length of the string in characters. This name corresponds to Jenkins' original name. */ size_t length; scm_t_uint32 a, b, c, u32; if (len == (size_t) -1) len = strlen (str); end = ustr + len; if (u8_check (ustr, len) != NULL) /* Invalid UTF-8; punt. */ return scm_i_string_hash (scm_from_utf8_stringn (str, len)); length = u8_strnlen (ustr, len); /* Set up the internal state. */ a = b = c = 0xdeadbeef + ((scm_t_uint32)(length<<2)) + 47; /* Handle most of the key. */ while (length > 3) { ustr += u8_mbtouc_unsafe (&u32, ustr, end - ustr); a += u32; ustr += u8_mbtouc_unsafe (&u32, ustr, end - ustr); b += u32; ustr += u8_mbtouc_unsafe (&u32, ustr, end - ustr); c += u32; mix (a, b, c); length -= 3; } /* Handle the last 3 elements's. */ ustr += u8_mbtouc_unsafe (&u32, ustr, end - ustr); a += u32; if (--length) { ustr += u8_mbtouc_unsafe (&u32, ustr, end - ustr); b += u32; if (--length) { ustr += u8_mbtouc_unsafe (&u32, ustr, end - ustr); c += u32; } } final (a, b, c);
static PyObject *eval_string_wrapper(PyObject *string_arg) { const char *s = PyString_AsString(string_arg); SCM string = scm_from_utf8_stringn(s, PyString_Size(string_arg)); return scm2py(scm_eval_string(string)); }
SCM py2scm(PyObject *value) { if (value == Py_None) { return SCM_UNSPECIFIED; } if (PyBool_Check(value)) { int v = PyObject_IsTrue(value); if (v == -1) return NULL; return scm_from_bool(v); } if (PyInt_Check(value)) { long v = PyInt_AsLong(value); if (PyErr_Occurred()) return NULL; return scm_from_long(v); } if (PyFloat_Check(value)) { double v = PyFloat_AsDouble(value); if (PyErr_Occurred()) return NULL; return scm_from_double(v); } if (PyString_Check(value)) { const char *s = PyString_AsString(value); if (s == NULL) return NULL; return scm_from_utf8_stringn(s, PyString_Size(value)); } if (PyUnicode_Check(value)) { scm_dynwind_begin(0); PyObject *utf8_str = PyUnicode_AsUTF8String(value); if (utf8_str == NULL) { scm_dynwind_end(); return NULL; } scm_dynwind_py_decref(utf8_str); const char *s = PyString_AsString(utf8_str); if (s == NULL) { scm_dynwind_end(); return NULL; } SCM result = scm_from_utf8_stringn(s, PyString_Size(utf8_str)); scm_dynwind_end(); return result; } if (PySequence_Check(value)) { unsigned int i = PySequence_Size(value); SCM r = SCM_EOL; while (i-- > 0) { PyObject *item = PySequence_GetItem(value, i); r = scm_cons(py2scm(item), r); } return r; } if (PyObject_TypeCheck(value, &ProcedureType)) return ((Procedure *)value)->proc; if (PyCallable_Check(value)) { SCM gsubr = scm_c_make_gsubr( "<Python function>", 0, 0, 1, &call_callable); Py_INCREF(value); SCM ptr = scm_from_pointer(value, (void (*)(void *))Py_DecRef); gsubr_alist = scm_acons(gsubr, ptr, gsubr_alist); return gsubr; } char buf[BUFSIZ]; snprintf(buf, BUFSIZ, "Python type \"%.50s\" doesn't have a " "corresponding Guile type", value->ob_type->tp_name); scm_error(scm_from_utf8_symbol("misc-error"), NULL, buf, SCM_EOL, SCM_EOL); /* does not return */ fprintf(stderr, "*** scm_error shouldn't have returned ***\n"); return SCM_UNSPECIFIED; }