SCM yacl_scm_b64url_decode (SCM scmb64) { if (!scm_is_string (scmb64)) scm_throw (scm_from_locale_symbol ("BADSTR"), SCM_BOOL_T); size_t scmb64len, outlen; char * b64url = scm_to_utf8_stringn (scmb64, &scmb64len); if (NULL == b64url) scm_throw (scm_from_locale_symbol ("BADDECODE"), SCM_BOOL_T); uint8_t *decode = yacl_b64url_decode (b64url, &outlen); free (b64url); if (NULL == decode) scm_throw (scm_from_locale_symbol ("BADDECODED"), SCM_BOOL_T); SCM b64 = scm_c_make_bytevector (outlen); memcpy (SCM_BYTEVECTOR_CONTENTS (b64), decode, outlen); free (decode); return b64; }
static SCM api_rwrite(SCM s_, SCM buffer_) { servlet *s = scm_to_pointer(s_); size_t length; char *str = scm_to_utf8_stringn(buffer_, &length); size_t ret = rwrite(s, str, length); free(str); return scm_from_size_t(ret); }
static inline SCM scm_rexp_general_compile (rexp_t (*compile) (const uint8_t *pattern), SCM pattern) { uint8_t *_pattern = (uint8_t *) scm_to_utf8_stringn (pattern, NULL); rexp_t _re = compile (_pattern); free (_pattern); return (_re == NULL) ? SCM_BOOL_F : scm_from_rexp_t (_re); }
VISIBLE SCM scm_rexp_substring (SCM match, SCM string, SCM subexpression) { uint8_t *_string = (uint8_t *) scm_to_utf8_stringn (string, NULL); uint8_t *_substring = u8_rexp_substr (scm_to_rexp_match_t (match), _string, scm_to_size_t (subexpression)); free (_string); return (_substring == NULL) ? SCM_BOOL_F : scm_from_utf8_string ((const char *) _substring); }
static SCM scm_rexp_general_compile_once (rexp_t (*compile) (const uint8_t *pattern), SCM pattern) { SCM buffers = scm_fluid_ref (_rexp_buffers ()); SCM my_rexp = scm_hash_ref (buffers, pattern, SCM_BOOL_F); if (scm_is_false (my_rexp)) { uint8_t *_pattern = (uint8_t *) scm_to_utf8_stringn (pattern, NULL); rexp_t _re = compile (_pattern); free (_pattern); my_rexp = (_re == NULL) ? SCM_BOOL_F : scm_from_rexp_t (_re); scm_hash_set_x (buffers, pattern, my_rexp); } return my_rexp; }
static SCM squeeze(SCM source, int method) { z_stream strm; DEFLATE_BLOB *blob; int ret; size_t inlen, outlen; char *inbuf, *outbuf; strm.zalloc = Z_NULL; strm.zfree = Z_NULL; strm.opaque = Z_NULL; if (method == SQUEEZE_GZIP) { ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 31, 8, Z_DEFAULT_STRATEGY); } else { ret = deflateInit(&strm, Z_DEFAULT_COMPRESSION); } if (ret != Z_OK) { log_msg("zlib: deflate init failed\n"); return SCM_BOOL_F; } inbuf = scm_to_utf8_stringn(source, &inlen); strm.total_in = strm.avail_in = inlen; strm.next_in = (unsigned char*)inbuf; outlen = deflateBound(&strm, inlen); outbuf = (char *)malloc(outlen); strm.total_out = 0; strm.avail_out = outlen; strm.next_out = (unsigned char *)outbuf; ret = deflate(&strm, Z_FINISH); free(inbuf); blob = (DEFLATE_BLOB *)scm_gc_malloc(sizeof(DEFLATE_BLOB) + strm.total_out, "gzip-blob"); blob->zip_len = strm.total_out; blob->orig_len = inlen; log_msg("compress %s %d -> %d\n", (method == SQUEEZE_GZIP ? "gzip" : "deflate"), blob->orig_len, blob->zip_len); blob->method = method; ret = deflateEnd(&strm); if (ret != Z_OK) printf("deflateEnd: %d\n", ret); memcpy(blob->payload, outbuf, blob->zip_len); free(outbuf); SCM_RETURN_NEWSMOB(deflate_tag, blob); }
static SCM scm_rexp_match_or_search (rexp_match_t match_or_search (rexp_t re, const uint8_t *s), SCM re, SCM string, SCM start) { const int _start = (SCM_UNBNDP (start)) ? 0 : scm_to_int (start); uint8_t *_string = (uint8_t *) scm_to_utf8_stringn (string, NULL); rexp_t _re = scm_to_rexp_t (re); rexp_match_t _match = match_or_search (_re, &_string[_start]); free (_string); SCM result = SCM_BOOL_F; if (_match != NULL) { if (_start != 0) for (size_t k = 0; k <= _match->capture_count; k++) { // FIXME: Catch overflow. _match->ovector[2 * k] += _start; _match->ovector[2 * k + 1] += _start; } result = scm_from_rexp_match_t (_match); } return result; }
PyObject *scm2py(SCM value) { if (value == NULL) return NULL; if (value == SCM_UNSPECIFIED) { Py_INCREF(Py_None); return Py_None; } if (scm_is_exact_integer(value)) return PyInt_FromLong(scm_to_long(value)); if (scm_is_real(value)) return PyFloat_FromDouble(scm_to_double(value)); if (scm_is_bool(value)) { PyObject *result = scm_to_bool(value) ? Py_True : Py_False; Py_INCREF(result); return result; } if (value == SCM_EOL) return PyTuple_New(0); if (scm_is_string(value)) { size_t len = 0; char *s = scm_to_utf8_stringn(value, &len); PyObject *result = PyUnicode_FromStringAndSize(s, len); free(s); return result; } if (scm_is_pair(value)) { unsigned int len = scm_to_uint(scm_length(value)); PyObject *result = PyTuple_New(len); scm_dynwind_begin(0); scm_dynwind_unwind_handler( (void (*)(void *))Py_DecRef, result, 0); unsigned int i; for (i = 0; i < len; i++) { PyObject *item = scm2py(scm_car(value)); if (item == NULL) { scm_dynwind_end(); Py_DECREF(result); return NULL; } PyTuple_SET_ITEM(result, i, item); value = scm_cdr(value); } scm_dynwind_end(); return result; } if (scm_to_bool(scm_procedure_p(value))) { SCM ptr = scm_assq_ref(gsubr_alist, value); if (!scm_is_false(ptr)) { PyObject *result = scm_to_pointer(ptr); Py_INCREF(result); return result; } Procedure *result = (Procedure *)ProcedureType.tp_alloc(&ProcedureType, 0); if (result == NULL) return NULL; result->proc = value; return (PyObject *)result; } char *msg = scm_to_utf8_stringn( scm_simple_format( SCM_BOOL_F, scm_from_utf8_string( "Guile expression ~S doesn't have a " "corresponding Python value"), scm_list_1(value)), NULL); PyErr_SetString(PyExc_TypeError, msg); free(msg); return NULL; }