/* * rasqal_expression_evaluate_str: * @e: The expression to evaluate. * @eval_context: Evaluation context * * INTERNAL - Evaluate RASQAL_EXPR_STR (literal expr) expression. * * Return value: A #rasqal_literal value or NULL on failure. */ static rasqal_literal* rasqal_expression_evaluate_str(rasqal_expression *e, rasqal_evaluation_context *eval_context, int *error_p) { rasqal_world* world = eval_context->world; rasqal_literal* l1 = NULL; rasqal_literal* result = NULL; const unsigned char *s; size_t len; unsigned char *new_s; l1 = rasqal_expression_evaluate2(e->arg1, eval_context, error_p); if(*error_p || !l1) goto failed; /* Note: flags removes RASQAL_COMPARE_XQUERY as this is the * explicit stringify operation and we want URIs as strings. */ s = rasqal_literal_as_counted_string(l1, &len, (eval_context->flags & ~RASQAL_COMPARE_XQUERY), error_p); if(!s || *error_p) goto failed; new_s = (unsigned char *)RASQAL_MALLOC(cstring, len + 1); if(!new_s) goto failed; memcpy((char*)new_s, (const char*)s, len + 1); /* after this new_s is owned by result */ result = rasqal_new_string_literal(world, new_s, NULL, NULL, NULL); if(l1) rasqal_free_literal(l1); return result; failed: if(error_p) *error_p = 1; if(l1) rasqal_free_literal(l1); return NULL; }
/* * rasqal_expression_evaluate_bnode_constructor: * @e: The expression to evaluate. * @eval_context: Evaluation context * * INTERNAL - Evaluate RASQAL_EXPR_BNODE (string) expression. * * Return value: A #rasqal_literal blank node value or NULL on failure. */ static rasqal_literal* rasqal_expression_evaluate_bnode_constructor(rasqal_expression *e, rasqal_evaluation_context *eval_context, int *error_p) { rasqal_world* world = eval_context->world; rasqal_literal *l1 = NULL; unsigned char *new_s = NULL; if(e->arg1) { const unsigned char *s; size_t len; l1 = rasqal_expression_evaluate2(e->arg1, eval_context, error_p); if(*error_p || !l1) goto failed; s = rasqal_literal_as_counted_string(l1, &len, eval_context->flags, error_p); if(*error_p) goto failed; new_s = (unsigned char*)RASQAL_MALLOC(cstring, len + 1); if(!new_s) goto failed; memcpy((char*)new_s, s, len + 1); rasqal_free_literal(l1); } else { new_s = rasqal_world_generate_bnodeid(world, NULL); if(!new_s) goto failed; } /* after this new_s is owned by the result */ return rasqal_new_simple_literal(world, RASQAL_LITERAL_BLANK, new_s); failed: if(error_p) *error_p = 1; if(l1) rasqal_free_literal(l1); return NULL; }
/* * rasqal_expression_evaluate_strdt: * @e: The expression to evaluate. * @eval_context: Evaluation context * * INTERNAL - Evaluate RASQAL_EXPR_STRDT(expr) expression. * * Return value: A #rasqal_literal string value or NULL on failure. */ static rasqal_literal* rasqal_expression_evaluate_strdt(rasqal_expression *e, rasqal_evaluation_context *eval_context, int *error_p) { rasqal_world* world = eval_context->world; rasqal_literal *l1 = NULL; rasqal_literal *l2 = NULL; const unsigned char* s = NULL; unsigned char* new_s = NULL; size_t len; raptor_uri* dt_uri = NULL; l1 = rasqal_expression_evaluate2(e->arg1, eval_context, error_p); if(*error_p || !l1) goto failed; if(l1->language || l1->datatype) { /* not a simple literal so return NULL success */ rasqal_free_literal(l1); return NULL; } s = rasqal_literal_as_counted_string(l1, &len, eval_context->flags, error_p); if(*error_p) goto failed; l2 = rasqal_expression_evaluate2(e->arg2, eval_context, error_p); if(*error_p || !l2) goto failed; dt_uri = rasqal_literal_as_uri(l2); if(dt_uri) { dt_uri = raptor_uri_copy(dt_uri); } else { const unsigned char *uri_string; uri_string = rasqal_literal_as_string_flags(l2, eval_context->flags, error_p); if(*error_p) goto failed; dt_uri = raptor_new_uri(world->raptor_world_ptr, uri_string); if(!dt_uri) goto failed; } new_s =(unsigned char*)RASQAL_MALLOC(cstring, len + 1); if(!new_s) goto failed; memcpy(new_s, s, len + 1); rasqal_free_literal(l1); rasqal_free_literal(l2); /* after this new_s and dt_uri become owned by result */ return rasqal_new_string_literal(world, new_s, /* language */ NULL, dt_uri, /* qname */ NULL); failed: if(error_p) *error_p = 1; if(new_s) RASQAL_FREE(cstring, new_s); if(l1) rasqal_free_literal(l1); if(l2) rasqal_free_literal(l2); return NULL; }
/* * rasqal_expression_evaluate_strlang: * @e: The expression to evaluate. * @eval_context: Evaluation context * * INTERNAL - Evaluate RASQAL_EXPR_STRLANG(expr) expression. * * Return value: A #rasqal_literal string value or NULL on failure. */ static rasqal_literal* rasqal_expression_evaluate_strlang(rasqal_expression *e, rasqal_evaluation_context *eval_context, int *error_p) { rasqal_world* world = eval_context->world; rasqal_literal *l1 = NULL; rasqal_literal *l2 = NULL; const unsigned char* s = NULL; const unsigned char* lang = NULL; unsigned char* new_s = NULL; unsigned char* new_lang = NULL; size_t len; size_t lang_len; l1 = rasqal_expression_evaluate2(e->arg1, eval_context, error_p); if(*error_p || !l1) goto failed; if(l1->language || l1->datatype) { /* not a simple literal so return NULL success */ rasqal_free_literal(l1); return NULL; } s = rasqal_literal_as_counted_string(l1, &len, eval_context->flags, error_p); if(*error_p) goto failed; l2 = rasqal_expression_evaluate2(e->arg2, eval_context, error_p); if(*error_p || !l2) goto failed; lang = rasqal_literal_as_counted_string(l2, &lang_len, eval_context->flags, error_p); if(*error_p) goto failed; new_s = (unsigned char*)RASQAL_MALLOC(cstring, len + 1); if(!new_s) goto failed; memcpy(new_s, s, len + 1); new_lang = (unsigned char*)RASQAL_MALLOC(cstring, lang_len + 1); if(!new_lang) goto failed; memcpy(new_lang, lang, lang_len + 1); rasqal_free_literal(l1); rasqal_free_literal(l2); /* after this new_s and new_lang become owned by result */ return rasqal_new_string_literal(world, new_s, (const char*)new_lang, /*datatype */ NULL, /* qname */ NULL); failed: if(error_p) *error_p = 1; if(new_s) RASQAL_FREE(cstring, new_s); if(l1) rasqal_free_literal(l1); if(l2) rasqal_free_literal(l2); return NULL; }
/* * rasqal_expression_evaluate_digest: * @e: The expression to evaluate. * @eval_context: Evaluation context * * INTERNAL - Evaluate SPARQL 1.1 RASQAL_EXPR_MD5, RASQAL_EXPR_SHA1, * RASQAL_EXPR_SHA224, RASQAL_EXPR_SHA256, RASQAL_EXPR_SHA384, * RASQAL_EXPR_SHA512 (string) expression. * * Return value: A #rasqal_literal xsd:string value or NULL on failure. */ rasqal_literal* rasqal_expression_evaluate_digest(rasqal_expression *e, rasqal_evaluation_context *eval_context, int *error_p) { rasqal_world* world = eval_context->world; rasqal_digest_type md_type = RASQAL_DIGEST_NONE; rasqal_literal* l1; const unsigned char *s; unsigned char *new_s; size_t len; int output_len; unsigned char *output = NULL; unsigned int i; unsigned char* p; /* Turn EXPR enum into DIGEST enum - we know they are ordered the same */ if(e->op >= RASQAL_EXPR_MD5 && e->op <= RASQAL_EXPR_SHA512) md_type = (rasqal_digest_type)(e->op - RASQAL_EXPR_MD5 + RASQAL_DIGEST_MD5); else return NULL; l1 = rasqal_expression_evaluate2(e->arg1, eval_context, error_p); if(*error_p || !l1) goto failed; s = rasqal_literal_as_counted_string(l1, &len, eval_context->flags, error_p); if(*error_p) goto failed; output_len = rasqal_digest_buffer(md_type, NULL, NULL, 0); if(output_len < 0) return NULL; output = RASQAL_MALLOC(unsigned char*, output_len); if(!output) return NULL; output_len = rasqal_digest_buffer(md_type, output, s, len); if(output_len < 0) goto failed; new_s = RASQAL_MALLOC(unsigned char*, (output_len * 2) + 1); if(!new_s) goto failed; p = new_s; for(i = 0; i < RASQAL_GOOD_CAST(unsigned int, output_len); i++) { unsigned short hex; char c = output[i]; hex = (c & 0xf0) >> 4; *p++ = (hex < 10) ? ('0' + hex) : ('a' + hex - 10); hex = (c & 0x0f); *p++ = (hex < 10) ? ('0' + hex) : ('a' + hex - 10); } *p = '\0'; RASQAL_FREE(char, output); rasqal_free_literal(l1); /* after this new_s becomes owned by result */ return rasqal_new_string_literal(world, new_s, NULL, NULL, NULL); failed: if(output) RASQAL_FREE(char, output); if(l1) rasqal_free_literal(l1); return NULL; }