You are viewing a plain text version of this content. The canonical link for it is here.
Posted to triplesoup-commits@incubator.apache.org by le...@apache.org on 2007/04/13 08:56:16 UTC
svn commit: r528394 [19/35] - in
/incubator/triplesoup/donations/TRIPLES-3-RDFStore: ./ dbms/ dbms/client/
dbms/client/t/ dbms/dbmsproxy/ dbms/deamon/ dbms/doc/ dbms/include/
dbms/libdbms/ dbms/utils/ doc/ include/ lib/ lib/DBD/ lib/RDFStore/
lib/RDFSt...
Added: incubator/triplesoup/donations/TRIPLES-3-RDFStore/rdfstore_ap_sha1.c
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-3-RDFStore/rdfstore_ap_sha1.c?view=auto&rev=528394
==============================================================================
--- incubator/triplesoup/donations/TRIPLES-3-RDFStore/rdfstore_ap_sha1.c (added)
+++ incubator/triplesoup/donations/TRIPLES-3-RDFStore/rdfstore_ap_sha1.c Fri Apr 13 01:56:01 2007
@@ -0,0 +1,311 @@
+/* ====================================================================
+ * The Apache Software License, Version 1.1
+ *
+ * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
+ * reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * 3. The end-user documentation included with the redistribution,
+ * if any, must include the following acknowledgment:
+ * "This product includes software developed by the
+ * Apache Software Foundation (http://www.apache.org/)."
+ * Alternately, this acknowledgment may appear in the software itself,
+ * if and wherever such third-party acknowledgments normally appear.
+ *
+ * 4. The names "Apache" and "Apache Software Foundation" must
+ * not be used to endorse or promote products derived from this
+ * software without prior written permission. For written
+ * permission, please contact apache@apache.org.
+ *
+ * 5. Products derived from this software may not be called "Apache",
+ * nor may "Apache" appear in their name, without prior written
+ * permission of the Apache Software Foundation.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ * Portions of this software are based upon public domain software
+ * originally written at the National Center for Supercomputing Applications,
+ * University of Illinois, Urbana-Champaign.
+ *
+ * $Id: rdfstore_ap_sha1.c,v 1.1 2003/05/05 17:57:39 areggiori Exp $
+ */
+
+/*
+ *
+ * This software also makes use of the following component:
+ *
+ * NIST Secure Hash Algorithm
+ * heavily modified by Uwe Hollerbach uh@alumni.caltech edu
+ * from Peter C. Gutmann's implementation as found in
+ * Applied Cryptography by Bruce Schneier
+ * This code is hereby placed in the public domain
+ */
+
+/*
+ *
+ * This file differes from the main Apache 1.3.x distribution
+ * because the EBCDIC support has been removed, the code has been
+ * made more stand alone (removed dependences with ap_config.h and ap.h)
+ * and ap_sha1_base64() has been taken off.
+ * In addition, all macros and subroutines have been local to rdfstore_ and RDFSTORE_
+ * namespace to avoid symbol conflicts with apache when the following code
+ * is run under apache Web server i.e. mod_perl or mod_php
+ *
+ */
+
+#include <string.h>
+
+#include "rdfstore_ap_sha1.h"
+
+/* a bit faster & bigger, if defined */
+#define UNROLL_LOOPS
+
+/* NIST's proposed modification to SHA, 7/11/94 */
+#define USE_MODIFIED_SHA
+
+/* SHA f()-functions */
+#define f1(x,y,z) ((x & y) | (~x & z))
+#define f2(x,y,z) (x ^ y ^ z)
+#define f3(x,y,z) ((x & y) | (x & z) | (y & z))
+#define f4(x,y,z) (x ^ y ^ z)
+
+/* SHA constants */
+#define CONST1 0x5a827999L
+#define CONST2 0x6ed9eba1L
+#define CONST3 0x8f1bbcdcL
+#define CONST4 0xca62c1d6L
+
+/* 32-bit rotate */
+
+#define ROT32(x,n) ((x << n) | (x >> (32 - n)))
+
+#define FUNC(n,i) \
+ temp = ROT32(A,5) + f##n(B,C,D) + E + W[i] + CONST##n; \
+ E = D; D = C; C = ROT32(B,30); B = A; A = temp
+
+#define SHA_BLOCKSIZE 64
+
+typedef unsigned char RDFSTORE_AP_BYTE;
+
+/* do SHA transformation */
+static void sha_transform(RDFSTORE_AP_SHA1_CTX *sha_info)
+{
+ int i;
+ RDFSTORE_AP_LONG temp, A, B, C, D, E, W[80];
+
+ for (i = 0; i < 16; ++i) {
+ W[i] = sha_info->data[i];
+ }
+ for (i = 16; i < 80; ++i) {
+ W[i] = W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16];
+#ifdef USE_MODIFIED_SHA
+ W[i] = ROT32(W[i], 1);
+#endif /* USE_MODIFIED_SHA */
+ }
+ A = sha_info->digest[0];
+ B = sha_info->digest[1];
+ C = sha_info->digest[2];
+ D = sha_info->digest[3];
+ E = sha_info->digest[4];
+#ifdef UNROLL_LOOPS
+ FUNC(1, 0); FUNC(1, 1); FUNC(1, 2); FUNC(1, 3); FUNC(1, 4);
+ FUNC(1, 5); FUNC(1, 6); FUNC(1, 7); FUNC(1, 8); FUNC(1, 9);
+ FUNC(1,10); FUNC(1,11); FUNC(1,12); FUNC(1,13); FUNC(1,14);
+ FUNC(1,15); FUNC(1,16); FUNC(1,17); FUNC(1,18); FUNC(1,19);
+
+ FUNC(2,20); FUNC(2,21); FUNC(2,22); FUNC(2,23); FUNC(2,24);
+ FUNC(2,25); FUNC(2,26); FUNC(2,27); FUNC(2,28); FUNC(2,29);
+ FUNC(2,30); FUNC(2,31); FUNC(2,32); FUNC(2,33); FUNC(2,34);
+ FUNC(2,35); FUNC(2,36); FUNC(2,37); FUNC(2,38); FUNC(2,39);
+
+ FUNC(3,40); FUNC(3,41); FUNC(3,42); FUNC(3,43); FUNC(3,44);
+ FUNC(3,45); FUNC(3,46); FUNC(3,47); FUNC(3,48); FUNC(3,49);
+ FUNC(3,50); FUNC(3,51); FUNC(3,52); FUNC(3,53); FUNC(3,54);
+ FUNC(3,55); FUNC(3,56); FUNC(3,57); FUNC(3,58); FUNC(3,59);
+
+ FUNC(4,60); FUNC(4,61); FUNC(4,62); FUNC(4,63); FUNC(4,64);
+ FUNC(4,65); FUNC(4,66); FUNC(4,67); FUNC(4,68); FUNC(4,69);
+ FUNC(4,70); FUNC(4,71); FUNC(4,72); FUNC(4,73); FUNC(4,74);
+ FUNC(4,75); FUNC(4,76); FUNC(4,77); FUNC(4,78); FUNC(4,79);
+#else /* !UNROLL_LOOPS */
+ for (i = 0; i < 20; ++i) {
+ FUNC(1,i);
+ }
+ for (i = 20; i < 40; ++i) {
+ FUNC(2,i);
+ }
+ for (i = 40; i < 60; ++i) {
+ FUNC(3,i);
+ }
+ for (i = 60; i < 80; ++i) {
+ FUNC(4,i);
+ }
+#endif /* !UNROLL_LOOPS */
+ sha_info->digest[0] += A;
+ sha_info->digest[1] += B;
+ sha_info->digest[2] += C;
+ sha_info->digest[3] += D;
+ sha_info->digest[4] += E;
+}
+
+union endianTest {
+ long Long;
+ char Char[sizeof(long)];
+};
+
+static char isLittleEndian(void)
+{
+ static union endianTest u;
+ u.Long = 1;
+ return (u.Char[0] == 1);
+}
+
+/* change endianness of data */
+
+/* count is the number of bytes to do an endian flip */
+static void maybe_byte_reverse(RDFSTORE_AP_LONG *buffer, int count)
+{
+ int i;
+ RDFSTORE_AP_BYTE ct[4], *cp;
+
+ if (isLittleEndian()) { /* do the swap only if it is little endian */
+ count /= sizeof(RDFSTORE_AP_LONG);
+ cp = (RDFSTORE_AP_BYTE *) buffer;
+ for (i = 0; i < count; ++i) {
+ ct[0] = cp[0];
+ ct[1] = cp[1];
+ ct[2] = cp[2];
+ ct[3] = cp[3];
+ cp[0] = ct[3];
+ cp[1] = ct[2];
+ cp[2] = ct[1];
+ cp[3] = ct[0];
+ cp += sizeof(RDFSTORE_AP_LONG);
+ }
+ }
+}
+
+/* initialize the SHA digest */
+
+void rdfstore_ap_SHA1Init(RDFSTORE_AP_SHA1_CTX *sha_info)
+{
+ sha_info->digest[0] = 0x67452301L;
+ sha_info->digest[1] = 0xefcdab89L;
+ sha_info->digest[2] = 0x98badcfeL;
+ sha_info->digest[3] = 0x10325476L;
+ sha_info->digest[4] = 0xc3d2e1f0L;
+ sha_info->count_lo = 0L;
+ sha_info->count_hi = 0L;
+ sha_info->local = 0;
+}
+
+/* update the SHA digest */
+
+void rdfstore_ap_SHA1Update_binary(RDFSTORE_AP_SHA1_CTX *sha_info,
+ const unsigned char *buffer,
+ unsigned int count)
+{
+ unsigned int i;
+
+ if ((sha_info->count_lo + ((RDFSTORE_AP_LONG) count << 3)) < sha_info->count_lo) {
+ ++sha_info->count_hi;
+ }
+ sha_info->count_lo += (RDFSTORE_AP_LONG) count << 3;
+ sha_info->count_hi += (RDFSTORE_AP_LONG) count >> 29;
+ if (sha_info->local) {
+ i = SHA_BLOCKSIZE - sha_info->local;
+ if (i > count) {
+ i = count;
+ }
+ memcpy(((RDFSTORE_AP_BYTE *) sha_info->data) + sha_info->local, buffer, i);
+ count -= i;
+ buffer += i;
+ sha_info->local += i;
+ if (sha_info->local == SHA_BLOCKSIZE) {
+ maybe_byte_reverse(sha_info->data, SHA_BLOCKSIZE);
+ sha_transform(sha_info);
+ }
+ else {
+ return;
+ }
+ }
+ while (count >= SHA_BLOCKSIZE) {
+ memcpy(sha_info->data, buffer, SHA_BLOCKSIZE);
+ buffer += SHA_BLOCKSIZE;
+ count -= SHA_BLOCKSIZE;
+ maybe_byte_reverse(sha_info->data, SHA_BLOCKSIZE);
+ sha_transform(sha_info);
+ }
+ memcpy(sha_info->data, buffer, count);
+ sha_info->local = count;
+}
+
+void rdfstore_ap_SHA1Update(RDFSTORE_AP_SHA1_CTX *sha_info, const char *buf,
+ unsigned int count)
+{
+ rdfstore_ap_SHA1Update_binary(sha_info, (const unsigned char *) buf, count);
+}
+
+/* finish computing the SHA digest */
+
+void rdfstore_ap_SHA1Final(unsigned char digest[RDFSTORE_SHA_DIGESTSIZE],
+ RDFSTORE_AP_SHA1_CTX *sha_info)
+{
+ int count, i, j;
+ RDFSTORE_AP_LONG lo_bit_count, hi_bit_count, k;
+
+ lo_bit_count = sha_info->count_lo;
+ hi_bit_count = sha_info->count_hi;
+ count = (int) ((lo_bit_count >> 3) & 0x3f);
+ ((RDFSTORE_AP_BYTE *) sha_info->data)[count++] = 0x80;
+ if (count > SHA_BLOCKSIZE - 8) {
+ memset(((RDFSTORE_AP_BYTE *) sha_info->data) + count, 0, SHA_BLOCKSIZE - count);
+ maybe_byte_reverse(sha_info->data, SHA_BLOCKSIZE);
+ sha_transform(sha_info);
+ memset((RDFSTORE_AP_BYTE *) sha_info->data, 0, SHA_BLOCKSIZE - 8);
+ }
+ else {
+ memset(((RDFSTORE_AP_BYTE *) sha_info->data) + count, 0,
+ SHA_BLOCKSIZE - 8 - count);
+ }
+ maybe_byte_reverse(sha_info->data, SHA_BLOCKSIZE);
+ sha_info->data[14] = hi_bit_count;
+ sha_info->data[15] = lo_bit_count;
+ sha_transform(sha_info);
+
+ for (i = 0, j = 0; j < RDFSTORE_SHA_DIGESTSIZE; i++) {
+ k = sha_info->digest[i];
+ digest[j++] = (unsigned char) ((k >> 24) & 0xff);
+ digest[j++] = (unsigned char) ((k >> 16) & 0xff);
+ digest[j++] = (unsigned char) ((k >> 8) & 0xff);
+ digest[j++] = (unsigned char) (k & 0xff);
+ }
+}
Added: incubator/triplesoup/donations/TRIPLES-3-RDFStore/rdfstore_bits.c
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-3-RDFStore/rdfstore_bits.c?view=auto&rev=528394
==============================================================================
--- incubator/triplesoup/donations/TRIPLES-3-RDFStore/rdfstore_bits.c (added)
+++ incubator/triplesoup/donations/TRIPLES-3-RDFStore/rdfstore_bits.c Fri Apr 13 01:56:01 2007
@@ -0,0 +1,580 @@
+/*
+##############################################################################
+# Copyright (c) 2000-2006 All rights reserved
+# Alberto Reggiori <ar...@webweaving.org>
+# Dirk-Willem van Gulik <di...@webweaving.org>
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+#
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in
+# the documentation and/or other materials provided with the
+# distribution.
+#
+# 3. The end-user documentation included with the redistribution,
+# if any, must include the following acknowledgment:
+# "This product includes software developed by
+# Alberto Reggiori <ar...@webweaving.org> and
+# Dirk-Willem van Gulik <di...@webweaving.org>."
+# Alternately, this acknowledgment may appear in the software itself,
+# if and wherever such third-party acknowledgments normally appear.
+#
+# 4. All advertising materials mentioning features or use of this software
+# must display the following acknowledgement:
+# This product includes software developed by the University of
+# California, Berkeley and its contributors.
+#
+# 5. Neither the name of the University nor the names of its contributors
+# may be used to endorse or promote products derived from this software
+# without specific prior written permission.
+#
+# 6. Products derived from this software may not be called "RDFStore"
+# nor may "RDFStore" appear in their names without prior written
+# permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+# ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+# OF THE POSSIBILITY OF SUCH DAMAGE.
+#
+# ====================================================================
+#
+# This software consists of work developed by Alberto Reggiori and
+# Dirk-Willem van Gulik. The RDF specific part is based based on public
+# domain software written at the Stanford University Database Group by
+# Sergey Melnik. For more information on the RDF API Draft work,
+# please see <http://www-db.stanford.edu/~melnik/rdf/api.html>
+# The DBMS TCP/IP server part is based on software originally written
+# by Dirk-Willem van Gulik for Web Weaving Internet Engineering m/v Enschede,
+# The Netherlands.
+#
+##############################################################################
+
+ $Id: rdfstore_bits.c,v 1.16 2006/06/19 10:10:21 areggiori Exp $
+*/
+
+#include <sys/types.h>
+#include <sys/time.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <string.h>
+#include <assert.h>
+
+#include "dbms.h"
+#include "dbms_compat.h"
+
+#include "rdfstore.h"
+#include "rdfstore_bits.h"
+#include "rdfstore_log.h"
+
+/* within p->size / p->bits
+ * set at -bit- position 'at'
+ * the masked bits to value
+ *
+ * return the modified bits
+ */
+
+/*
+ * #define RDFSTORE_DEBUG_BITS
+ */
+
+int rdfstore_bits_setmask(
+ unsigned int * size,
+ unsigned char * bits,
+ unsigned int at,
+ unsigned int mask,
+ unsigned int value,
+ unsigned int max
+) {
+ register int depth,change;
+
+ if (mask == 0) return(0);
+
+ /* auto extend if needed... */
+ if ( (at/8) >= *size) {
+ unsigned int n=*size;
+ unsigned int s= STEP * ( 1 + at/8/STEP );
+ if (s>max) {
+ fprintf(stderr, "Too many bit=%d byte=%d %d of %d\n",
+ at, at/8, s, max);
+ exit(1);
+ };
+ *size = s;
+ bzero(bits+n, s-n);
+ };
+
+ /* x x x x x x as stored
+ * 0 0 1 1 1 0 mask
+ * 0 0 0 1 1 0 value
+ */
+ mask <<= at % 8;
+ value <<= at % 8;
+ at /= 8;
+ change =0; depth = 0;
+ do {
+ register unsigned char d,c;
+ if (at>=max) {
+ fprintf(stderr,"Uncontrolled overflow %d of %d\n",
+ at, max);
+ exit(1);
+ };
+
+ c = bits[ at ];
+ d = ( c & (~ mask) ) | value;
+
+ if (d != c) {
+ bits[ at ] = d;
+ change |= (d ^ c) << depth;
+ };
+ at ++;
+
+ depth += 8;
+ mask >>= 8;
+ value >>= 8;
+
+ } while ((mask) && (at < *size ));
+
+ return (change);
+ };
+
+/* Return the record number (bit number / 4) of the
+ * first record from record 'at' onwards which has
+ * a bit set within the mask.
+ */
+unsigned int
+rdfstore_bits_getfirstrecord (
+ unsigned int size, /* in bytes */
+ unsigned char * bits, /* bit array */
+ unsigned int at, /* as record no (bits/4) */
+ unsigned char mask /* 0000 to 1111 */
+) {
+ unsigned mask2 = mask << 4;
+ unsigned i = at >> 1;
+ unsigned char c = bits[i];
+
+ assert(mask < 16);
+ assert(mask != 0);
+
+ if (at & 1)
+ c &= 0xF0;
+
+ do {
+ if ((c & 0x0f) & mask)
+ return 2*i+0;
+ if ((c & 0xf0) & mask2)
+ return 2*i+1;
+ c = bits[ ++i ];
+ } while (i < size);
+
+ return size*2;
+}
+
+/*
+ * rdfstore_bits_isanyset - returns != 0 if any bit in the bitmask is set
+ * in addition it returns the positions of the first bit set in at
+ *
+ */
+
+int rdfstore_bits_isanyset(
+ unsigned int * size,
+ unsigned char * bits,
+ unsigned int * at,
+ unsigned char mask
+) {
+ register unsigned rest=0;
+ rest = ( *at % 8 );
+ mask = mask << rest;
+ *at /= 8;
+
+ while ((mask) && (*at < *size)) {
+ register int c = bits[ *at ] & mask;
+ if (c) {
+ (*at) *=8;
+ (*at) += rest;
+ return c;
+ };
+ (*at)++;
+ };
+
+ return 0;
+ };
+
+/*
+ * returns the first bit set from (and including) at in the
+ * bit array of size bytes. If no bit set is found after
+ * the size-est byte of bits; size*8 is returned (i.e. the number
+ * of * last bit (not byte)+1; starting from zero.
+ *
+ * size in bytes
+ * bits unsigned array of bytes with 8 bits each
+ * at location in bits.
+ * return location in bits (or size*8).
+ *
+ */
+unsigned int rdfstore_bits_getfirstsetafter (
+ unsigned int size,
+ unsigned char * bits,
+ unsigned int at
+) {
+ register unsigned int i = at >> 3;
+ register unsigned char c = bits[ i ];
+
+ /* first byte is special; skip over the bits
+ * before 'at'.
+ */
+ c &= ( 0xFF << (at & 0x7 ));
+ do {
+ if (c) {
+ i <<= 3;
+#define _RX(x) if (c & (1<<x)) return (i+x);
+ _RX(0); _RX(1); _RX(2); _RX(3);
+ _RX(4); _RX(5); _RX(6);
+ return (i+7);
+#undef _RX
+ }
+ i++;
+ c = bits[i];
+ } while (i < size);
+
+ /* Fail; return bits+1. */
+ return size<<3;
+}
+
+/* slightly tricky bin-ops; in that it can cope with 'infinitive
+ * lenght type tricks.. Returns the len of the changed bitseq.
+ */
+
+/*
+ * exor - Exor's to bitvectors to each other, ba of length la and bb of len lb
+ *
+ * returns result in bc (should be preallocated) and length as function result
+ */
+
+unsigned int rdfstore_bits_exor (
+ unsigned int la, unsigned char * ba,
+ unsigned int lb, unsigned char * bb,
+ unsigned char * bc
+ )
+{
+ register unsigned int len,i;
+ /* set in a, but not set in b
+ * a b -> a|b ^ b
+ * 0 0 0 0
+ * 0 1 1 0
+ * 1 0 1 1
+ * 1 1 1 0
+ */
+#if 0
+ A real EXOR does
+ 00 -> 0
+ 10 -> 1
+ 01 -> 1
+ 11 -> 0
+#endif
+ for(len=0,i=0; (i<la) || (i<lb); i++) {
+ register unsigned char a = ( i>=la ) ? 0 : ba[i];
+ register unsigned char b = ( i>=lb ) ? 0 : bb[i];
+#if 0
+ /* real exor */
+ register unsigned char c = a ^ b;
+#endif
+ register unsigned char c = (a | b) ^ b;
+ if (c) len = i+1;
+ bc[i] = c;
+ };
+ return len;
+ };
+
+/*
+ * or - Or's to bitvectors to each other, ba of length la and bb of length lb
+ *
+ * returns result in bc (should be preallocated) and length as function result
+ */
+
+unsigned int rdfstore_bits_or (
+ unsigned int la, unsigned char * ba,
+ unsigned int lb, unsigned char * bb,
+ unsigned char * bc
+ )
+{
+ register unsigned int len,i;
+ for(len=0,i=0; (i<la) || (i<lb); i++) {
+ register unsigned char a = ( i>=la ) ? 0 : ba[i];
+ register unsigned char b = ( i>=lb ) ? 0 : bb[i];
+ register unsigned char c = (a | b);
+ if (c) len = i+1;
+ bc[i] = c;
+ };
+ return len;
+ };
+
+
+/*
+ * and - And's to bitvectors to each other, ba of length la and bb of length lb
+ *
+ * returns result in bc (should be preallocated) and length as function result
+ */
+
+unsigned int rdfstore_bits_and (
+ unsigned int la, unsigned char * ba,
+ unsigned int lb, unsigned char * bb,
+ unsigned char * bc
+ )
+{
+ register unsigned int len,i;
+ for(len=0,i=0; (i<la) && (i<lb); i++) {
+ register unsigned char a = ( i>=la ) ? 0 : ba[i];
+ register unsigned char b = ( i>=lb ) ? 0 : bb[i];
+ register unsigned char c = (a & b);
+ if (c) len = i+1;
+ bc[i] = c;
+ };
+
+ return len;
+ };
+
+/*
+ * not - Not's a bitvector ba of length la
+ *
+ * returns result in bb (should be preallocated) and length as function result
+ */
+
+unsigned int rdfstore_bits_not (
+ unsigned int la, unsigned char * ba,
+ unsigned char * bb
+ )
+{
+ register unsigned int len,i;
+ for(len=0,i=0; (i<la) ; i++) {
+ register unsigned char a = ( i>=la ) ? 0 : ba[i];
+ register unsigned char b = ~ a;
+ if (b) len = i+1;
+ bb[i] = b;
+ };
+
+ return len;
+ };
+
+
+/*
+ * shorten - removes the top zero bits of the bitvector
+ *
+ * returns length of bitvector (without trailing zeroes) as bytes as function
+ * result
+ */
+
+unsigned int rdfstore_bits_shorten(
+ unsigned int la, unsigned char * ba
+ )
+{
+ while( ( la >0 ) && (ba[la-1] == 0) ) la--;
+ return(la);
+ };
+
+/* n = 6 - size of a record.
+ * A = row of records; at 1 bit wide.
+ * lenght in bytes, not bits !
+ * B = row of records; each 6 bits wide.
+ * lenght in bytes, not bits !
+ * M = mask of 6 bits.
+ * no lenght
+ * OUT:
+ * bc filled
+ * returns number of bytes in use.
+ *
+ */
+unsigned int rdfstore_bits_and2(
+ int n,
+ unsigned int la, unsigned char * ba,
+ unsigned int lb, unsigned char * bb,
+ unsigned char mask,
+ unsigned char * bc
+ )
+{
+ unsigned int i = 0;
+ int endbit = la * 8;
+ assert(n <= 8); /* up to 8 bits - see q+1 below */
+ assert(mask < (1<<n)); /* Mask cannot be bigger than N bits */
+
+ bzero(bc,la); /* Out array of length A max */
+
+ /* If B has less records than A has bits; shorten A
+ */
+ if (lb * 8 / n < endbit)
+ endbit = (lb * 8 / n) * 8;
+
+#ifdef RDFSTORE_DEBUG_BITS
+{
+int i,j=0;
+printf("rdfstore_bits_and2 la=%d lb=%d endbit=%d endbyte=%d\n",(int)la,(int)lb,endbit,endbit/8);
+printf("rdfstore_bits_and2 ba -->'");
+for(i=0;i<8*la;i++) {
+ printf("Rec %d %c\n", i,(ba[i>>3] & (1<<(i&7))) ? '1':'0');
+ };
+printf("'\n");
+printf("rdfstore_bits_and2 bb -->'");
+for(i=0;i<8*lb;i++) {
+ if (i % n == 0) {
+ int a = 0;
+ if (j<8*la) a= ba[j>>3] & (1<<(j&7));
+ printf("Rec %d A=%d ",j,a ? '1':'0');
+ j++;
+ };
+ printf("%c", (bb[i>>3] & (1<<(i&7))) ? '1':'0');
+ if (i % n == n-1) printf("\n");
+ };
+printf("'\n");
+printf("rdfstore_bits_and2 mask -->'");
+for(i=0;i<8;i++) {
+ printf("%c", (mask & (1<<(i&7))) ? '1':'0');
+ };
+printf("'\n");
+}
+#endif
+
+ for(i=0; i < endbit ; i++) {
+ /* Check if bit 'i' is set or not
+ */
+ if (ba[ i>>3 ] & (1<<(i & 7))) {
+ unsigned int p = n * i; /* bit number where the record starts */
+ unsigned int q = p >> 3; /* byte number. */
+ unsigned int r = p & 7; /* bit offset in the byte */
+ unsigned int record;
+
+ /* fetch N bits from the B. Note 8 bits max now; if we have
+ * records of more than 8 bits; then add q + 2.
+ */
+ record = (((bb[ q + 1 ] << 8) + bb[ q ]) >> (r));
+
+ /* If there is one or more bits in the record set; within
+ * the mask; set a bit at recno in the output.
+ */
+
+ if (record & mask) /* and2 */
+ bc[ i >> 3 ] |= (1 << ( i & 7));
+ };
+ };
+
+#ifdef RDFSTORE_DEBUG_BITS
+{
+int j;
+printf("rdfstore_bits_or2 bc -->'");
+for(j=0;j<8*(i>>3);j++) {
+ printf("Rec %d %c\n", j,(bc[j>>3] & (1<<(j&7))) ? '1':'0');
+ };
+printf("'\n");
+};
+#endif
+
+ /* Return the lenght in bytes, not bits */
+ return i >> 3;
+ };
+
+/* n = 6 - size of a record.
+ * A = row of records; at 1 bit wide.
+ * lenght in bytes, not bits !
+ * B = row of records; each 6 bits wide.
+ * lenght in bytes, not bits !
+ * M = mask of 6 bits.
+ * no lenght
+ * OUT:
+ * bc filled
+ * returns number of bytes in use.
+ *
+ */
+unsigned int rdfstore_bits_or2(
+ int n,
+ unsigned int la, unsigned char * ba,
+ unsigned int lb, unsigned char * bb,
+ unsigned char mask,
+ unsigned char * bc
+ )
+{
+ unsigned int i = 0;
+ int endbit = la * 8;
+ assert(n <= 8); /* up to 8 bits - see q+1 below */
+ assert(mask < (1<<n)); /* Mask cannot be bigger than N bits */
+
+ bzero(bc,la); /* Out array of length A max */
+
+ /* If B has less records than A has bits; shorten A
+ */
+ if (lb * 8 / n < endbit)
+ endbit = (lb * 8 / n) * 8;
+
+#ifdef RDFSTORE_DEBUG_BITS
+{
+int i,j=0;
+printf("rdfstore_bits_or2 la=%d lb=%d endbit=%d endbyte=%d\n",(int)la,(int)lb,endbit,endbit/8);
+printf("rdfstore_bits_or2 ba -->'");
+for(i=0;i<8*la;i++) {
+ printf("Rec %d %c\n", i,(ba[i>>3] & (1<<(i&7))) ? '1':'0');
+ };
+printf("'\n");
+printf("rdfstore_bits_or2 bb -->'");
+for(i=0;i<8*lb;i++) {
+ if (i % n == 0) {
+ int a = 0;
+ if (j<8*la) a= ba[j>>3] & (1<<(j&7));
+ printf("Rec %d A=%d ",j,a ? '1':'0');
+ j++;
+ };
+ printf("%c", (bb[i>>3] & (1<<(i&7))) ? '1':'0');
+ if (i % n == n-1) printf("\n");
+ };
+printf("'\n");
+printf("rdfstore_bits_or2 mask -->'");
+for(i=0;i<8;i++) {
+ printf("%c", (mask & (1<<(i&7))) ? '1':'0');
+ };
+printf("'\n");
+}
+#endif
+
+ for(i=0; i < endbit ; i++) {
+ unsigned int p = n * i; /* bit number where the record starts */
+ unsigned int q = p >> 3; /* byte number. */
+ unsigned int r = p & 7; /* bit offset in the byte */
+ unsigned int record;
+
+ /* fetch N bits from the B. Note 8 bits max now; if we have
+ * records of more than 8 bits; then add q + 2.
+ */
+ record = (((bb[ q + 1 ] << 8) + bb[ q ]) >> (r));
+
+ /* If there is one or more bits in the record set; within
+ * the mask; set a bit at recno in the output.
+ */
+
+ if ( (ba[ i>>3 ] & (1<<(i & 7))) | (record & mask) ) /* or2 */
+ bc[ i >> 3 ] |= (1 << ( i & 7));
+ };
+
+#ifdef RDFSTORE_DEBUG_BITS
+{
+int j;
+printf("rdfstore_bits_or2 bc -->'");
+for(j=0;j<8*(i>>3);j++) {
+ printf("Rec %d %c\n", j,(bc[j>>3] & (1<<(j&7))) ? '1':'0');
+ };
+printf("'\n");
+};
+#endif
+
+ /* Return the lenght in bytes, not bits */
+ return i >> 3;
+ };
Added: incubator/triplesoup/donations/TRIPLES-3-RDFStore/rdfstore_compress.c
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-3-RDFStore/rdfstore_compress.c?view=auto&rev=528394
==============================================================================
--- incubator/triplesoup/donations/TRIPLES-3-RDFStore/rdfstore_compress.c (added)
+++ incubator/triplesoup/donations/TRIPLES-3-RDFStore/rdfstore_compress.c Fri Apr 13 01:56:01 2007
@@ -0,0 +1,131 @@
+/*
+ *
+ * Copyright (c) 2000-2006 Alberto Reggiori <ar...@webweaving.org>
+ * Dirk-Willem van Gulik <di...@webweaving.org>
+ *
+ * NOTICE
+ *
+ * This product is distributed under a BSD/ASF like license as described in the 'LICENSE'
+ * file you should have received together with this source code. If you did not get a
+ * a copy of such a license agreement you can pick up one at:
+ *
+ * http://rdfstore.sourceforge.net/LICENSE
+ *
+ * $Id: rdfstore_compress.c,v 1.12 2006/06/19 10:10:21 areggiori Exp $
+ */
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <strings.h>
+#include <string.h>
+
+#include "rdfstore_compress.h"
+#include "sflcomp.h"
+#include "my_compress.h"
+#include "fraenkel_compress.h"
+
+#define xross_map(name) \
+ static void _ ## name (unsigned int srclen,unsigned char* src, unsigned int * dstlen, unsigned char * dst) \
+ { \
+ * dstlen = name (src,dst,srclen); \
+ }
+
+#define xross_pair( name ) \
+ xross_map( compress_ ## name ); \
+ xross_map( expand_ ## name );
+
+/* Build 5x2 functions to de- and en-code using the right arguments.
+ * XXX todo - remove those functions and edit the rdfstore_kernel.c functions
+ * to accept 'bcopy' style arguments.
+ */
+xross_pair( nulls );
+xross_pair( bits );
+xross_pair( block );
+xross_pair( rle );
+xross_pair( mine );
+xross_pair( fraenkel );
+
+static void _bcopy(unsigned int srclen,unsigned char* src, unsigned int * dstlen, unsigned char * dst)
+{
+ bcopy(src,dst,srclen);
+ * dstlen = srclen;
+};
+
+#ifdef DEBUG_BDB_PAGESIZES
+/* Fake compression method which will pad each record up to a specific
+ * multiple of 2; such as 1024 - as to hide small size changes for
+ * the BDB above.
+ */
+#define LOGSIZE (10) /* 1024 */
+static void _enc(unsigned int srclen,unsigned char* src, unsigned int * dstlen, unsigned char * dst)
+{
+ *(int *)dst = srclen;
+ bcopy(src,dst+4,srclen);
+ srclen = ((srclen >> LOGSIZE) + 1) << LOGSIZE;
+ * dstlen = srclen;
+};
+
+static void _dec(unsigned int srclen,unsigned char* src, unsigned int * dstlen, unsigned char * dst)
+{
+ * dstlen = *(int *)src;
+ bcopy(src,dst+4,srclen);
+};
+#endif
+
+int rdfstore_compress_init(
+ rdfstore_compression_types type,
+ void(**func_decode)(unsigned int,unsigned char*, unsigned int *, unsigned char *),
+ void(**func_encode)(unsigned int,unsigned char*, unsigned int *, unsigned char *)
+)
+{
+ if ((type == RDFSTORE_COMPRESSION_TYPE_DEFAULT) &&
+ (getenv("RDFSTORE_COMPRESSION")) &&
+ (atoi(getenv("RDFSTORE_COMPRESSION")))
+ ) {
+ type = atoi(getenv("RDFSTORE_COMPRESSION"));
+ fprintf(stderr,"Override type %d\n",type);
+ }
+
+ switch(type) {
+#ifdef DEBUG_BDB_PAGESIZES
+ case 999:
+ *func_encode = &_enc;
+ *func_decode = &_dec;
+ break;
+#endif
+ case RDFSTORE_COMPRESSION_TYPE_NONE:
+ *func_encode = &_bcopy;
+ *func_decode = &_bcopy;
+ break;
+ case RDFSTORE_COMPRESSION_TYPE_BITS:
+ *func_encode = &_compress_bits;
+ *func_decode = &_expand_bits;
+ break;
+ case RDFSTORE_COMPRESSION_TYPE_BLOCK:
+ *func_encode = &_compress_block;
+ *func_decode = &_expand_block;
+ break;
+ case RDFSTORE_COMPRESSION_TYPE_RLE:
+ *func_encode = &_compress_rle;
+ *func_decode = &_expand_rle;
+ break;
+ case RDFSTORE_COMPRESSION_TYPE_FRAENKEL:
+ *func_encode = &_compress_fraenkel;
+ *func_decode = &_expand_fraenkel;
+ break;
+ case RDFSTORE_COMPRESSION_TYPE_ORIGINAL:
+ *func_encode = &_compress_mine;
+ *func_decode = &_expand_mine;
+ break;
+ case RDFSTORE_COMPRESSION_TYPE_DEFAULT: /* break intentionally missing */
+ case RDFSTORE_COMPRESSION_TYPE_NULLS:
+ *func_encode = &_compress_nulls;
+ *func_decode = &_expand_nulls;
+ break;
+ default:
+ fprintf(stderr,"No compression default specified\n");
+ exit(1);
+ }
+ return 0;
+}
Added: incubator/triplesoup/donations/TRIPLES-3-RDFStore/rdfstore_digest.c
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-3-RDFStore/rdfstore_digest.c?view=auto&rev=528394
==============================================================================
--- incubator/triplesoup/donations/TRIPLES-3-RDFStore/rdfstore_digest.c (added)
+++ incubator/triplesoup/donations/TRIPLES-3-RDFStore/rdfstore_digest.c Fri Apr 13 01:56:01 2007
@@ -0,0 +1,371 @@
+/*
+ * Copyright (c) 2000-2006 All rights reserved
+ * Alberto Reggiori <ar...@webweaving.org>
+ * Dirk-Willem van Gulik <di...@webweaving.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * 3. The end-user documentation included with the redistribution,
+ * if any, must include the following acknowledgment:
+ * "This product includes software developed by
+ * Alberto Reggiori <ar...@webweaving.org> and
+ * Dirk-Willem van Gulik <di...@webweaving.org>."
+ * Alternately, this acknowledgment may appear in the software itself,
+ * if and wherever such third-party acknowledgments normally appear.
+ *
+ * 4. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the University of
+ * California, Berkeley and its contributors.
+ *
+ * 5. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * 6. Products derived from this software may not be called "RDFStore"
+ * nor may "RDFStore" appear in their names without prior written
+ * permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * ====================================================================
+ *
+ * This software consists of work developed by Alberto Reggiori and
+ * Dirk-Willem van Gulik. The RDF specific part is based based on public
+ * domain software written at the Stanford University Database Group by
+ * Sergey Melnik. For more information on the RDF API Draft work,
+ * please see <http://www-db.stanford.edu/~melnik/rdf/api.html>
+ * The DBMS TCP/IP server part is based on software originally written
+ * by Dirk-Willem van Gulik for Web Weaving Internet Engineering m/v Enschede,
+ * The Netherlands.
+ *
+ * $Id: rdfstore_digest.c,v 1.12 2006/06/19 10:10:21 areggiori Exp $
+ *
+ */
+
+#include <stdio.h>
+
+#include "rdfstore_digest.h"
+#include "rdfstore_log.h"
+#include "rdfstore_serializer.h"
+
+/*
+#define RDFSTORE_DEBUG_DIGEST
+*/
+
+#ifdef RDFSTORE_DEBUG_DIGEST
+#define DIGEST_PRINT(dd) {\
+ int i = 0;\
+ printf("Statement digest at line %s:%d is '",__FILE__,__LINE__);\
+ for (i = 0; i < RDFSTORE_SHA_DIGESTSIZE; i++) {\
+ printf("%02X", dd[i]);\
+ };\
+ printf("'\n");\
+ }
+#else
+#define DIGEST_PRINT(dd) {}
+#endif
+
+
+int
+rdfstore_digest_digest(unsigned char *input, int len, unsigned char digest[RDFSTORE_SHA_DIGESTSIZE])
+{
+ RDFSTORE_AP_SHA1_CTX sha_info;
+
+ rdfstore_ap_SHA1Init(&sha_info);
+ rdfstore_ap_SHA1Update(&sha_info, input, len);
+ rdfstore_ap_SHA1Final(digest, &sha_info);
+
+ DIGEST_PRINT(digest);
+ return 0;
+};
+
+const char *
+rdfstore_digest_get_digest_algorithm()
+{
+ return "SHA-1";
+};
+
+/* As it stands - this is a 32 bit (partial) hash - we are not using the full
+ * 160 bots of a normal SHA1 operation
+ */
+static rdf_store_digest_t
+rdfstore_digest_crc64(unsigned char * dd)
+{
+ if (dd == NULL)
+ return 0;
+
+ return (rdf_store_digest_t) htonl(*(uint32_t *) dd);
+}
+
+int
+rdfstore_digest_get_node_digest(RDF_Node * node, unsigned char dd[RDFSTORE_SHA_DIGESTSIZE], int unique)
+{
+ unsigned char *input = NULL;
+ int status = 0;
+ int len = 0;
+
+ if (node == NULL)
+ return -1;
+
+ if (node->type != 1) {
+ if (node->value.resource.identifier == NULL)
+ return -1;
+
+#ifdef RDFSTORE_DEBUG_DIGEST
+ printf("get_node_digest( RESOURCE '%s')\n", node->value.resource.identifier);
+#endif
+
+ len = node->value.resource.identifier_len;
+
+ input = (unsigned char *) RDFSTORE_MALLOC(
+ sizeof(unsigned char) * (len + 1)); /* also bNode bit flag below 1/0 */
+
+ if (input == NULL)
+ return -1;
+
+ memcpy(input, node->value.resource.identifier,len);
+
+ if( node->type == 2 ) {
+ memcpy(input+len, "1", 1); /* is bNode */
+ } else {
+ memcpy(input+len, "0", 1);
+ };
+ len++;
+ } else if (node->type == 1) {
+ int len_lang,len_dt;
+ /* literals can be empty i.e. node->value.literal.string can be NULL */
+
+#ifdef RDFSTORE_DEBUG_DIGEST
+ printf("get_node_digest( LITERAL '%s')\n", node->value.literal.string);
+#endif
+
+ len = (node->value.literal.string != NULL) ? node->value.literal.string_len : 0;
+
+ len_lang=0;
+ len_dt=0;
+ if( unique ) {
+ if (node->value.literal.lang != NULL)
+ len_lang = strlen(node->value.literal.lang);
+
+ if (node->value.literal.parseType == 1)
+ len_dt = strlen(RDFSTORE_RDF_PARSETYPE_LITERAL);
+ else if (node->value.literal.dataType != NULL)
+ len_dt= strlen(node->value.literal.dataType);
+ };
+
+ input = (unsigned char *) RDFSTORE_MALLOC(
+ sizeof(unsigned char) * (len + len_lang + len_dt + 2)); /* the two double quotes signs to distinguish between resources and literals */
+
+ if (input == NULL)
+ return -1;
+
+ /*
+ * the following assures that different digests are generated
+ * for the same string for Literal and URI ref of a Resource
+ * e.g. "http://www.google.com" and <http://www.google.com>
+ * would result in different digests
+ */
+ memcpy(input, "\"", 1);
+ if (node->value.literal.string != NULL) {
+ memcpy(input+1, node->value.literal.string, len);
+ };
+ memcpy(input+1+len, "\"", 1);
+
+ /* keep the digest unique per xml:lang and rdf:datatype if requested */
+ if( unique ) {
+ if (node->value.literal.lang != NULL)
+ memcpy(input+1+len+1, node->value.literal.lang, len_lang);
+ if (node->value.literal.parseType == 1)
+ memcpy(input+1+len+1+len_lang, RDFSTORE_RDF_PARSETYPE_LITERAL, len_dt);
+ else if (node->value.literal.dataType != NULL)
+ memcpy(input+1+len+1+len_lang, node->value.literal.dataType, len_dt);
+ };
+ len += len_lang + len_dt + 2;
+ } else {
+ return -1;
+ };
+
+ status = rdfstore_digest_digest(input, len, dd);
+
+ RDFSTORE_FREE(input);
+
+ return status;
+};
+
+/*
+ * crc64 of an SHA-1 cryptographic hash - see Stanford API Draft and GUID
+ * stuff
+ */
+rdf_store_digest_t
+rdfstore_digest_get_node_hashCode( RDF_Node * node, int unique )
+{
+ unsigned char dd[RDFSTORE_SHA_DIGESTSIZE];
+ rdf_store_digest_t hc = 0;
+
+ if (node == NULL)
+ return 0;
+
+#ifdef RDFSTORE_DEBUG_DIGEST
+ if (node->hashcode)
+ printf("Node hashcode for '%s' already carried out '%d'\n", (node->type != 1) ? node->value.resource.identifier : node->value.literal.string, node->hashcode);
+#endif
+
+ if (node->hashcode)
+ return node->hashcode;
+
+ if ((rdfstore_digest_get_node_digest(node, dd, unique)) != 0) {
+ hc = 0;
+ } else {
+ hc = rdfstore_digest_crc64(dd);
+ };
+
+ return hc;
+};
+
+int
+rdfstore_digest_get_statement_digest(RDF_Statement * statement, RDF_Node * given_context, unsigned char dd[RDFSTORE_SHA_DIGESTSIZE])
+{
+ unsigned char dds[RDFSTORE_SHA_DIGESTSIZE];
+ unsigned char ddp[RDFSTORE_SHA_DIGESTSIZE];
+ unsigned char ddo[RDFSTORE_SHA_DIGESTSIZE];
+ unsigned char ddc[RDFSTORE_SHA_DIGESTSIZE];
+ /* unsigned char ddn[RDFSTORE_SHA_DIGESTSIZE]; */
+ unsigned char *input = NULL;
+ RDF_Node *context = NULL;
+ int status = 0;
+
+ if (statement == NULL)
+ return -1;
+
+ if (given_context == NULL) {
+ if (statement->context != NULL)
+ context = statement->context;
+ } else {
+ /* use given context instead */
+ context = given_context;
+ };
+
+ if ((rdfstore_digest_get_node_digest(statement->subject, dds, 1)) != 0)
+ return -1;
+
+ DIGEST_PRINT(dds);
+
+ if ((rdfstore_digest_get_node_digest(statement->predicate, ddp, 1)) != 0)
+ return -1;
+
+ DIGEST_PRINT(ddp);
+
+ if ((rdfstore_digest_get_node_digest(statement->object, ddo, 1)) != 0) /* distinguish RDF literal hashcode by xml:lang or rdf:datatype */
+ return -1;
+
+ DIGEST_PRINT(ddo);
+
+ if (context != NULL) {
+ if ((rdfstore_digest_get_node_digest(context, ddc, 1)) != 0)
+ return -1;
+
+ DIGEST_PRINT(ddc);
+
+ input = (unsigned char *) RDFSTORE_MALLOC(
+ sizeof(unsigned char) * (RDFSTORE_SHA_DIGESTSIZE * 4)); /* s,p,o,c */
+ } else {
+ input = (unsigned char *) RDFSTORE_MALLOC(
+ sizeof(unsigned char) * (RDFSTORE_SHA_DIGESTSIZE * 3)); /* s,p,o */
+ };
+
+ if (input == NULL)
+ return -1;
+
+ memcpy(input, dds, RDFSTORE_SHA_DIGESTSIZE);
+ memcpy(input + RDFSTORE_SHA_DIGESTSIZE, ddp, RDFSTORE_SHA_DIGESTSIZE);
+
+ if (statement->object->type == 1) {
+ register int i;
+ unsigned char c = ddo[0];
+ /*
+ * rotate one byte - see why at
+ * http://www-db.stanford.edu/~melnik/rdf/api.html#digest
+ * even if it says rotate to the left why is the right :)
+ */
+ for (i = 0; i < RDFSTORE_SHA_DIGESTSIZE - 1; i++)
+ ddo[i] = ddo[i + 1];
+ ddo[RDFSTORE_SHA_DIGESTSIZE - 1] = c;
+ };
+ memcpy(input + (2 * RDFSTORE_SHA_DIGESTSIZE), ddo, RDFSTORE_SHA_DIGESTSIZE);
+
+ if (context != NULL)
+ memcpy(input + (3 * RDFSTORE_SHA_DIGESTSIZE), ddc, RDFSTORE_SHA_DIGESTSIZE);
+
+ status = rdfstore_digest_digest(input,
+ (context != NULL) ? (RDFSTORE_SHA_DIGESTSIZE * 4) : (RDFSTORE_SHA_DIGESTSIZE * 3),
+ dd);
+
+ DIGEST_PRINT(dd);
+
+ RDFSTORE_FREE(input);
+
+ return status;
+}
+
+rdf_store_digest_t
+rdfstore_digest_get_statement_hashCode(RDF_Statement * statement, RDF_Node * given_context)
+{
+ unsigned char dd[RDFSTORE_SHA_DIGESTSIZE];
+ rdf_store_digest_t hc = 0;
+
+ if (statement == NULL)
+ return 0;
+
+#ifdef RDFSTORE_DEBUG_DIGEST
+ if (statement->hashcode) {
+ char *ntriples_rep = rdfstore_ntriples_statement(statement, NULL);
+ printf("Statement hashcode for '%s' already carried out '%d'\n", ntriples_rep, statement->hashcode);
+ RDFSTORE_FREE(ntriples_rep);
+ };
+#endif
+
+ if (statement->hashcode)
+ return statement->hashcode;
+
+ if ((rdfstore_digest_get_statement_digest(statement, given_context, dd)) != 0) {
+ hc = 0;
+ } else {
+ /*
+ * perhaps it is instead => s.hashCode() * 7) + p.hashCode()) *
+ * 7 + o.hashCode() + c.hashCode()
+ */
+ hc = rdfstore_digest_crc64(dd);
+ };
+
+#ifdef RDFSTORE_DEBUG_DIGEST
+ {
+ char *ntriples_rep = rdfstore_ntriples_statement(statement, NULL);
+ printf("Just computed statement hashcode for '%s' to '%d' %s\n", ntriples_rep, hc, (given_context != NULL) ? "(not to be cached)" : "");
+ RDFSTORE_FREE(ntriples_rep);
+ };
+#endif
+
+ return hc;
+}
Added: incubator/triplesoup/donations/TRIPLES-3-RDFStore/rdfstore_flat_store.c
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-3-RDFStore/rdfstore_flat_store.c?view=auto&rev=528394
==============================================================================
--- incubator/triplesoup/donations/TRIPLES-3-RDFStore/rdfstore_flat_store.c (added)
+++ incubator/triplesoup/donations/TRIPLES-3-RDFStore/rdfstore_flat_store.c Fri Apr 13 01:56:01 2007
@@ -0,0 +1,397 @@
+/*
+##############################################################################
+# Copyright (c) 2000-2006 All rights reserved
+# Alberto Reggiori <ar...@webweaving.org>
+# Dirk-Willem van Gulik <di...@webweaving.org>
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+#
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in
+# the documentation and/or other materials provided with the
+# distribution.
+#
+# 3. The end-user documentation included with the redistribution,
+# if any, must include the following acknowledgment:
+# "This product includes software developed by
+# Alberto Reggiori <ar...@webweaving.org> and
+# Dirk-Willem van Gulik <di...@webweaving.org>."
+# Alternately, this acknowledgment may appear in the software itself,
+# if and wherever such third-party acknowledgments normally appear.
+#
+# 4. All advertising materials mentioning features or use of this software
+# must display the following acknowledgement:
+# This product includes software developed by the University of
+# California, Berkeley and its contributors.
+#
+# 5. Neither the name of the University nor the names of its contributors
+# may be used to endorse or promote products derived from this software
+# without specific prior written permission.
+#
+# 6. Products derived from this software may not be called "RDFStore"
+# nor may "RDFStore" appear in their names without prior written
+# permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+# ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+# OF THE POSSIBILITY OF SUCH DAMAGE.
+#
+# ====================================================================
+#
+# This software consists of work developed by Alberto Reggiori and
+# Dirk-Willem van Gulik. The RDF specific part is based based on public
+# domain software written at the Stanford University Database Group by
+# Sergey Melnik. For more information on the RDF API Draft work,
+# please see <http://www-db.stanford.edu/~melnik/rdf/api.html>
+# The DBMS TCP/IP server part is based on software originally written
+# by Dirk-Willem van Gulik for Web Weaving Internet Engineering m/v Enschede,
+# The Netherlands.
+#
+##############################################################################
+#
+# $Id: rdfstore_flat_store.c,v 1.25 2006/06/19 10:10:21 areggiori Exp $
+*/
+#include "dbms.h"
+#include "dbms_compat.h"
+#include "dbms_comms.h"
+
+/* Public API of flat store */
+#include "rdfstore_flat_store.h"
+
+/* Generic backend API */
+#include "backend_store.h"
+
+#include "rdfstore_log.h"
+#include "rdfstore.h"
+
+#include "rdfstore_flat_store_private.h"
+
+#include "backend_bdb_store.h"
+#include "backend_dbms_store.h"
+#include "backend_caching_store.h"
+
+static void default_myfree(void *adr) {
+ RDFSTORE_FREE(adr);
+}
+static void *default_mymalloc(size_t x) {
+ return RDFSTORE_MALLOC(x);
+}
+static void default_myerror(char *err, int erx) {
+ fprintf(stderr, "rdfstore_flat_store Error[%d]: %s\n", erx, err);
+}
+
+void
+rdfstore_flat_store_set_error(FLATDB * me, char *msg, rdfstore_flat_store_error_t erx) {
+ if ( me == NULL )
+ return;
+
+ if (me && me->store)
+ (*(me->store->set_error)) (me->instance, msg, erx);
+ else
+ perror(msg);
+ };
+
+char *
+rdfstore_flat_store_get_error(FLATDB * me) {
+ if ( me == NULL )
+ return NULL;
+
+ return (*(me->store->get_error)) (me->instance);
+ };
+
+/* clone a key or value for older BDB */
+DBT
+rdfstore_flat_store_kvdup(FLATDB * me, DBT data) {
+ return (*(me->store->kvdup)) (me->instance, data);
+ };
+
+#ifdef RDFSTORE_FLAT_STORE_DEBUG
+void
+rdfstore_flat_store_reset_debuginfo(
+ FLATDB * me
+) {
+ if ( me == NULL )
+ return;
+
+ (*(me->store->reset_debuginfo)) (me->instance);
+}
+#endif
+
+/*
+ * NOTE: all the functions return 0 on success and non zero value if error
+ * (see above and include/rdfstore_flat_store.h for known error codes)
+ */
+rdfstore_flat_store_error_t
+rdfstore_flat_store_open(
+ int remote,
+ int ro,
+ FLATDB * *mme,
+ char *dir,
+ char *name,
+ unsigned int local_hash_flags,
+ char *host,
+ int port,
+ void *(*_my_malloc) (size_t size),
+ void (*_my_free) (void *),
+ void (*_my_report) (dbms_cause_t cause, int count),
+ void (*_my_error) (char *err, int erx),
+ int bt_compare_fcn_type ) {
+ FLATDB *me;
+ rdfstore_flat_store_error_t err;
+
+ if (getenv("RDFSTORE_CACHE"))
+ remote |= 0x10;
+
+ if (_my_error == NULL)
+ _my_error = default_myerror;
+
+ if (_my_malloc == NULL)
+ _my_malloc = default_mymalloc;
+
+ if (_my_free == NULL)
+ _my_free = default_myfree;
+
+ me = (FLATDB *) _my_malloc(sizeof(FLATDB));
+
+ if (me == NULL) {
+ perror("Out of memory during flat store backend creation.");
+ return FLAT_STORE_E_NOMEM;
+ };
+
+ switch (remote) {
+ case 0x0:
+ me->store = backend_bdb;
+ break;
+ case 0x1:
+ me->store = backend_dbms;
+ break;
+ case 0x10:
+ case 0x11:
+ me->store = backend_caching;
+ break;
+ default:
+ perror("Backend type is not available");
+ return FLAT_STORE_E_NOMEM;
+ break;
+ };
+
+
+ err = (*(me->store->open)) (
+ remote, ro, (void **) &(me->instance),
+ dir, name, local_hash_flags, host, port,
+ _my_malloc, _my_free, _my_report, _my_error,
+ bt_compare_fcn_type
+ );
+ if (err) {
+ (*_my_free) (me);
+ return err;
+ }
+ me->free = _my_free;
+
+#ifdef RDFSTORE_FLAT_STORE_DEBUG
+ rdfstore_flat_store_reset_debuginfo(me);
+#endif
+
+ *mme = me; /* XXX need to check with alberto what this
+ * is XXX */
+ return 0;
+ };
+
+rdfstore_flat_store_error_t
+rdfstore_flat_store_close(
+ FLATDB * me
+) {
+ void (*_my_free) (void *) = me->free;
+ int retval = 0;
+
+ if ( me == NULL )
+ return FLAT_STORE_E_UNDEF;
+
+ retval = (*(me->store->close)) (me->instance);
+ _my_free(me);
+
+#ifdef RDFSTORE_FLAT_STORE_DEBUG
+ fprintf(stderr, "rdfstore_flat_store_close '%s'\n", me->filename);
+#endif
+
+ return retval;
+ };
+
+rdfstore_flat_store_error_t
+rdfstore_flat_store_fetch(
+ FLATDB * me,
+ DBT key,
+ DBT * val
+) {
+ if ( me == NULL )
+ return FLAT_STORE_E_UNDEF;
+
+ return (*(me->store->fetch)) (me->instance, key, val);
+ };
+
+rdfstore_flat_store_error_t
+rdfstore_flat_store_fetch_compressed (
+ FLATDB * me,
+ void(*func_decode)(unsigned int,unsigned char*, unsigned int *, unsigned char *),
+ DBT key,
+ unsigned int * outsize, unsigned char * outchar ) {
+
+ if ( me == NULL )
+ return FLAT_STORE_E_UNDEF;
+
+ return (*(me->store->fetch_compressed))(me->instance,func_decode,key,outsize,outchar);
+ };
+
+
+rdfstore_flat_store_error_t
+rdfstore_flat_store_store(
+ FLATDB * me,
+ DBT key,
+ DBT val ) {
+
+ if ( me == NULL )
+ return FLAT_STORE_E_UNDEF;
+
+ return (*(me->store->store)) (me->instance, key, val);
+ };
+
+rdfstore_flat_store_error_t
+rdfstore_flat_store_store_compressed (
+ FLATDB * me,
+ void(*func_encode)(unsigned int,unsigned char*, unsigned int *, unsigned char *),
+ DBT key,
+ unsigned int insize, unsigned char * inchar,
+ unsigned char * buff ) {
+
+ if ( me == NULL )
+ return FLAT_STORE_E_UNDEF;
+
+ return (*(me->store->store_compressed))(me->instance,func_encode,key,insize,inchar,buff);
+ };
+
+rdfstore_flat_store_error_t
+rdfstore_flat_store_exists(
+ FLATDB * me,
+ DBT key ) {
+
+ if ( me == NULL )
+ return FLAT_STORE_E_UNDEF;
+
+ return (*(me->store->exists)) (me->instance, key);
+ };
+
+rdfstore_flat_store_error_t
+rdfstore_flat_store_delete(
+ FLATDB * me,
+ DBT key ) {
+
+ if ( me == NULL )
+ return FLAT_STORE_E_UNDEF;
+
+ return (*(me->store->delete)) (me->instance, key);
+ };
+
+rdfstore_flat_store_error_t
+rdfstore_flat_store_clear(
+ FLATDB * me ) {
+
+ if ( me == NULL )
+ return FLAT_STORE_E_UNDEF;
+
+ return (*(me->store->clear)) (me->instance);
+ };
+
+rdfstore_flat_store_error_t
+rdfstore_flat_store_from(
+ FLATDB * me,
+ DBT closest_key,
+ DBT * key ) {
+
+ if ( me == NULL )
+ return FLAT_STORE_E_UNDEF;
+
+ return (*(me->store->from)) (me->instance, closest_key, key);
+ };
+
+rdfstore_flat_store_error_t
+rdfstore_flat_store_first(
+ FLATDB * me,
+ DBT * key ) {
+
+ if ( me == NULL )
+ return FLAT_STORE_E_UNDEF;
+
+ return (*(me->store->first)) (me->instance, key);
+ };
+
+rdfstore_flat_store_error_t
+rdfstore_flat_store_next(
+ FLATDB * me,
+ DBT previous_key,
+ DBT * next_key ) {
+
+ if ( me == NULL )
+ return FLAT_STORE_E_UNDEF;
+
+ return (*(me->store->next)) (me->instance, previous_key, next_key);
+ };
+
+rdfstore_flat_store_error_t
+rdfstore_flat_store_inc(
+ FLATDB * me,
+ DBT key,
+ DBT * new_value ) {
+
+ if ( me == NULL )
+ return FLAT_STORE_E_UNDEF;
+
+ return (*(me->store->inc)) (me->instance, key, new_value);
+ };
+
+/* packed rdf_store_counter_t decrement */
+rdfstore_flat_store_error_t
+rdfstore_flat_store_dec(
+ FLATDB * me,
+ DBT key,
+ DBT * new_value ) {
+
+ if ( me == NULL )
+ return FLAT_STORE_E_UNDEF;
+
+ return (*(me->store->dec)) (me->instance, key, new_value);
+ };
+
+rdfstore_flat_store_error_t
+rdfstore_flat_store_sync(
+ FLATDB * me ) {
+
+ if ( me == NULL )
+ return FLAT_STORE_E_UNDEF;
+
+ return (*(me->store->sync)) (me->instance);
+ };
+
+int
+rdfstore_flat_store_isremote(
+ FLATDB * me ) {
+
+ if ( me == NULL )
+ return -1;
+
+ return (*(me->store->isremote)) (me->instance);
+ };
+