--- /dev/null
+
+/* structure in Deocder
+ */
+
+public class Decoded {
+ int flowId;
+ String data;
+}
+
--- /dev/null
+/* =============================================================================
+ *
+ * decoder.java
+ *
+ * =============================================================================
+ *
+ * Copyright (C) Stanford University, 2006. All Rights Reserved.
+ * Author: Chi Cao Minh
+ *
+ * =============================================================================
+ *
+ * For the license of bayes/sort.h and bayes/sort.c, please see the header
+ * of the files.
+ *
+ * ------------------------------------------------------------------------
+ *
+ * For the license of kmeans, please see kmeans/LICENSE.kmeans
+ *
+ * ------------------------------------------------------------------------
+ *
+ * For the license of ssca2, please see ssca2/COPYRIGHT
+ *
+ * ------------------------------------------------------------------------
+ *
+ * For the license of lib/mt19937ar.c and lib/mt19937ar.h, please see the
+ * header of the files.
+ *
+ * ------------------------------------------------------------------------
+ *
+ * For the license of lib/rbtree.h and lib/rbtree.c, please see
+ * lib/LEGALNOTICE.rbtree and lib/LICENSE.rbtree
+ *
+ * ------------------------------------------------------------------------
+ *
+ * Unless otherwise noted, the following license applies to STAMP files:
+ *
+ * Copyright (c) 2007, Stanford University
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * 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.
+ *
+ * * Neither the name of Stanford University nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY STANFORD UNIVERSITY ``AS IS'' AND ANY
+ * EXPRESS 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 STANFORD UNIVERSITY 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.
+ *
+ * =============================================================================
+ */
+
+
+#define MAP_T RBTree
+#define MAP_ALLOC(hash, cmp) RBTree.alloc(cmp)
+#define MAP_INSERT(map, key, data) map.insert(key, data)
+#define MAP_CONTAINS(map, key) map.contains(key);
+#define MAP_FIND(map,key) map.get(key);
+#define MAP_INSERT(map,key,data) map.insert(key,data);
+#define MAP_REMOVE(map,key) map.deleteNode(key);
+
+public class Decoder {
+
+ MAP_T fragmentedMapPtr; /* contains list of packet_t* */
+ Queue_t decodedQueuePtr; /* contains decoded_t* */
+
+ public Decoder() {}
+
+/* =============================================================================
+ * decoder_alloc
+ * =============================================================================
+ decoder_t* decoder_alloc ();
+ */
+ public static Decoder alloc() {
+ Decoder decoderPtr;
+
+ decoderPtr = new Decoder();
+ if(decoderPtr) {
+ decoderPtr.fragementedMapPtr = MAP_ALLOC(0,0);
+
+ if(decoderPtr.fragementedMapPtr == null)
+ {
+ System.out.println("Assert in Decoder.alloc");
+ System.exit(1);
+ }
+
+ decoderPtr.decodedQueuePtr = Queue_t.alloc(1024);
+ if(decoderPtr.decodedQueuePtr == null)
+ {
+ System.out.println("Assert in Decoder.alloc");
+ System.exit(1);
+ }
+
+ return decoderPtr;
+ }
+
+
+/* =============================================================================
+ * decoder_free
+ * =============================================================================
+ void decoder_free (decoder_t* decoderPtr);
+ */
+ public free(Decoder decoderPtr) {
+ fragmentedMapPtr = null;
+ decodedQueuePtr = null;
+ decoderPtr = null;
+ }
+
+
+/* =============================================================================
+ * decoder_process
+ * =============================================================================
+ error_t decoder_process (decoder_t* decoderPtr, char* bytes, long numByte);
+ */
+ public int process(Packet bytes,int numByte)
+ {
+ boolean status;
+
+ /*
+ * Basic error checing
+ */
+
+ if (numByte < PACKET_HEADER_LENGTH) {
+ return ERROR.SHORT;
+ }
+
+ /* need to look into it later */
+ Packet packetPtr = (Packet)bytes;
+ int flowId = packetPtr.flowId;
+ int fragmentId = packetPtr.fragmentId;
+ int numFragment = packetPtr.numFragment;
+ int length = packetPtr.length;
+
+ if (flowId < 0) {
+ return ERROR.FLOWID;
+ }
+
+ if ((fragmentedId < 0) || (fragmentId >= numFragment)) {
+ return ERROR.FRAGMENTID;
+ }
+
+ if (length < 0) {
+ return ERROR.LENGTH;
+ }
+
+ /*
+ * Add to fragmented map for reassembling
+ */
+
+ if (numFragment > 1) {
+
+ List_t fragmentListPtr = (List_t)MAP_FIND(fragmentedMapPtr,flowId);
+
+ if (fragmentListPtr == null) {
+
+ fragmentListPtr = List_t.alloc(1); // packet_compareFragmentId
+ if(fragmentListPtr == null) {
+ System.out.println("Assertion in process");
+ System.exit(1);
+ }
+
+ status = MAP_INSERT(fragmentedMapPtr,flowId,fragmentListPtr);
+ if(status == null) {
+ System.out.println("Assertion in process");
+ System.exit(1);
+ }
+ } else {
+
+ List_Iter it;
+ it.reset(fragmentListPtr);
+
+ if(it.hasNext(fragmentListPtr) == null) {
+ System.out.println("Assertion in process");
+ System.exit(1);
+ }
+
+ Packet firstFragmentPtr = (Packet)it.next(fragmentListPtr);
+ int expectedNumFragment = firstFragmentPtr.numFragment;
+
+ if (numFragment != expectedNumFragment) {
+ status = MAP_REMOVE(fragmentedMapPtr,flowId);
+ if(status == null) {
+ System.out.println("Assertion in process");
+ System.exit(1);
+ }
+
+ status = fragmentListPtr.insert(packetPtr);
+ if(status == null) {
+ System.out.println("Assertion in process");
+ System.exit(1);
+ }
+
+ /*
+ * If we have all thefragments we can reassemble them
+ */
+
+ if(fragmentListPtr.getSize() == numFragment) {
+
+ int numByte = 0;
+ int i = 0;
+ it.reset(fragmentListPtr);
+
+ while (it.hasNext(fragmentListPtr)) {
+ Packet fragmentPtr = (Packet)it.next(fragmentListPtr);
+
+ if(fragmentPtr.fragmentId != i) {
+ status = MAP_REMOVE(fragmentedMapPtr,(flowId);
+
+ if(status == null) {
+ System.out.println("Assertion in process");
+ System.exit(1);
+ }
+ return ERROR.INCOMPLETE; /* should be sequential */
+ }
+ numByte += fragmentPtr.length;
+ i++;
+ }
+
+ char[] data = new char[numByte+1];
+ if(data == null) {
+ System.out.println("Assertion in process");
+ System.exit(1);
+ }
+
+ data[numByte] = '\0';
+ int dst = 0; // index of data
+ int i;
+ it.reset(fragmentListPtr);
+ while(it.hasNext(fragmentLIstPtr)) {
+ Packet fragmentPtr = (Packet)it.next(fragmentListPtr);
+
+ for(i=0;i<fragmentPtr.length;i++)
+ data[dst++] = fragmentPtr.data[i];
+ }
+
+ if(dst != data + numByte) {
+ System.out.println("Assertion in process");
+ System.exit(1);
+ }
+
+ Decoded decodedPtr = new Decoded();
+ if(decodedPtr == null) {
+ System.out.println("Assertion in process");
+ System.exit(1);
+ }
+
+ decodedPtr.flowId = flowId;
+ decodedPtr.data = String.copyValueOf(data);
+
+ status = decodededQueuePtr.queue_push(decodedPtr);
+
+ if(status == null) {
+ System.out.println("Assertion in process");
+ System.exit(1);
+ }
+
+ status = MAP_REMOVE(fragmentedMapPtr,flowId);
+
+ if(status == null) {
+ System.out.println("Assertion in process");
+ System.exit(1);
+ }
+ }
+ }
+ } else {
+
+ /*
+ * This is the only fragment, so it is ready
+ */
+
+ if (fragmentId != 0) {
+ return ERROR.FRAGMENTID;
+ }
+
+ String data = new String();
+ if(data == null) {
+ System.out.println("Assertion in process");
+ System.exit(1);
+ }
+
+ data.concat(packetPtr.data);
+
+ Decoded decodedPtr = new Decoded();
+ if(decodedPtr == null) {
+ System.out.println("Assertion in process");
+ System.exit(1);
+ }
+
+ decodedPtr.flowId = flowId;
+ decodedPtr.data = data;
+
+ status = decodedQueuePtr.queue_push(decodedPtr);
+
+ if(status == null) {
+ System.out.println("Assertion in process");
+ System.exit(1);
+ }
+
+ return ERROR.NONE;
+
+ }
+
+
+
+
+/* =============================================================================
+ * TMdecoder_process
+ * =============================================================================
+ error_t TMdecoder_process (TM_ARGDECL decoder_t* decoderPtr, char* bytes, long numByte);
+ */
+
+
+/* =============================================================================
+ * decoder_getComplete
+ * -- If none, returns NULL
+ * =============================================================================
+ char* decoder_getComplete (decoder_t* decoderPtr, long* decodedFlowIdPtr); */
+ public String getComplete(int[] decodedFlowId) {
+ String data;
+ Decoded decodedPtr = (Decoded)decodedQueuePtr.queue_pop();
+
+ if(decodedPtr) {
+ decodedFlowId[0] = decodedPtr.flowId;
+ data = decodedPtr.data;
+ decodedPtr = null;
+ } else {
+ decodedFlowId = -1;
+ data= null;
+ }
+
+ return data;
+ }
+
+
+
+/* =============================================================================
+ * TMdecoder_getComplete
+ * -- If none, returns NULL
+ * =============================================================================
+ char* TMdecoder_getComplete (TM_ARGDECL decoder_t* decoderPtr, long* decodedFlowIdPtr);
+ */
+}
+
+/* =============================================================================
+ *
+ * End of decoder.java
+ *
+ * =============================================================================
+ */
--- /dev/null
+/* =============================================================================
+ *
+ * error.java
+ *
+ * =============================================================================
+ *
+ * Copyright (C) Stanford University, 2006. All Rights Reserved.
+ * Author: Chi Cao Minh
+ *
+ * =============================================================================
+ *
+ * For the license of bayes/sort.h and bayes/sort.c, please see the header
+ * of the files.
+ *
+ * ------------------------------------------------------------------------
+ *
+ * For the license of kmeans, please see kmeans/LICENSE.kmeans
+ *
+ * ------------------------------------------------------------------------
+ *
+ * For the license of ssca2, please see ssca2/COPYRIGHT
+ *
+ * ------------------------------------------------------------------------
+ *
+ * For the license of lib/mt19937ar.c and lib/mt19937ar.h, please see the
+ * header of the files.
+ *
+ * ------------------------------------------------------------------------
+ *
+ * For the license of lib/rbtree.h and lib/rbtree.c, please see
+ * lib/LEGALNOTICE.rbtree and lib/LICENSE.rbtree
+ *
+ * ------------------------------------------------------------------------
+ *
+ * Unless otherwise noted, the following license applies to STAMP files:
+ *
+ * Copyright (c) 2007, Stanford University
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * 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.
+ *
+ * * Neither the name of Stanford University nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY STANFORD UNIVERSITY ``AS IS'' AND ANY
+ * EXPRESS 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 STANFORD UNIVERSITY 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.
+ *
+ * =============================================================================
+ */
+
+public class ERROR {
+ int NONE = 0;
+ int SHORT = 1;
+ int FLOWID = 2;
+ int FRAGMENTID = 3;
+ int LENGTH = 4;
+ int NUMFRAGMENT = 5;
+ int INCOMPLETE = 6;
+ int SIGNATURE = 7;
+}
+
+
+/* =============================================================================
+ *
+ * End of error.java
+ *
+ * =============================================================================
+ */
--- /dev/null
+public class List_Iter {
+ List_Node itPtr;
+
+ /* =============================================================================
+ * list_iter_reset
+ * =============================================================================
+ void list_iter_reset (list_iter_t* itPtr, list_t* listPtr);
+ */
+ public List_Iter() {
+ itPtr = null;
+ }
+
+ public void reset(List_t listPtr)
+ {
+ itPtr = listPtr.head;
+ }
+
+ /* =============================================================================
+ * list_iter_hasNext
+ * =============================================================================
+ * bool_t list_iter_hasNext (list_iter_t* itPtr, list_t* listPtr);
+ */
+ public boolean hasNext(List_t listPtr) {
+ return (itPtr.nextPtr != null)? true : false;
+ }
+
+ /* =============================================================================
+ * list_iter_next
+ * =============================================================================
+ * void* list_iter_next (list_iter_t* itPtr, list_t* listPtr);
+ */
+ public Object next(List_t listPtr)
+ {
+ itPtr = itPtr.nextPtr;
+ return itPtr.dataPtr;
+ }
+}
+
--- /dev/null
+
+public class List_Node {
+ Object dataPtr;
+ List_Node nextPtr;
+
+ public List_Node() {
+ dataPtr = null;
+ nextPtr = null;
+ }
+}
--- /dev/null
+/* =============================================================================
+ *
+ * List_t.java
+ * -- Sorted singly linked list
+ * -- Options: duplicate allowed
+ * (DLIST_NO_DUPLICATES) is no implemented yet (default: allow duplicates)
+ *
+ * =============================================================================
+ *
+ * Copyright (C) Stanford University, 2006. All Rights Reserved.
+ * Author: Chi Cao Minh
+ *
+ * =============================================================================
+ *
+ * For the license of bayes/sort.h and bayes/sort.c, please see the header
+ * of the files.
+ *
+ * ------------------------------------------------------------------------
+ *
+ * For the license of kmeans, please see kmeans/LICENSE.kmeans
+ *
+ * ------------------------------------------------------------------------
+ *
+ * For the license of ssca2, please see ssca2/COPYRIGHT
+ *
+ * ------------------------------------------------------------------------
+ *
+ * For the license of lib/mt19937ar.c and lib/mt19937ar.h, please see the
+ * header of the files.
+ *
+ * ------------------------------------------------------------------------
+ *
+ * For the license of lib/rbtree.h and lib/rbtree.c, please see
+ * lib/LEGALNOTICE.rbtree and lib/LICENSE.rbtree
+ *
+ * ------------------------------------------------------------------------
+ *
+ * Unless otherwise noted, the following license applies to STAMP files:
+ *
+ * Copyright (c) 2007, Stanford University
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * 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.
+ *
+ * * Neither the name of Stanford University nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY STANFORD UNIVERSITY ``AS IS'' AND ANY
+ * EXPRESS 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 STANFORD UNIVERSITY 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.
+ *
+ * =============================================================================
+ */
+
+
+public class List_t {
+
+ public List_Node head;
+ int flag;
+ int size;
+
+ public List_t() {
+ head = new List_Node();
+ }
+
+
+ /* =======================================================================
+ * allocNode
+ * -- Returns null on failure
+ * =======================================================================
+ */
+ private List_Node allocNode(Object dataPtr)
+ {
+ List_Node nodePtr = new List_Node();
+
+ if(nodePtr == null) {
+ return null;
+ }
+
+ nodePtr.dataPtr = dataPtr;
+ nodePtr.nextPtr = null;
+
+ return nodePtr;
+ }
+
+
+/* =============================================================================
+ * list_alloc
+ * -- If NULL passed for 'compare' function, will compare data pointer addresses
+ * -- Returns NULL on failure
+ * =============================================================================
+ * list_t* list_alloc (long (*compare)(const void*, const void*));
+ *
+ *
+ */
+
+ public static List_t alloc(int flag)
+ {
+ List_t listPtr = new List_t();
+
+ if(listPtr == null) {
+ return null;
+ }
+
+ listPtr.head.dataPtr = null;
+ listPtr.head.nextPtr = null;
+ listPtr.size = 0;
+
+ listPtr.flag = flag;
+
+ return listPtr;
+ }
+
+/* =============================================================================
+ * list_free
+ * -- If NULL passed for 'compare' function, will compare data pointer addresses
+ * -- Returns NULL on failure
+ * =============================================================================
+ * void list_free (list_t* listPtr);
+ */
+ public static void free(List_t listPtr)
+ {
+ listPtr = null;
+ }
+
+// privae freeList
+
+/* =============================================================================
+ * list_isEmpty
+ * -- Return TRUE if list is empty, else FALSE
+ * =============================================================================
+ * bool_t list_isEmpty (list_t* listPtr);
+ */
+ public boolean isEmpty()
+ {
+ return (head.nextPtr == null);
+ }
+
+/* =============================================================================
+ * list_getSize
+ * -- Returns size of list
+ * =============================================================================
+ * long list_getSize (list_t* listPtr);
+ */
+ public int getSize() {
+ return size;
+ }
+
+/* =============================================================================
+ * findPrevious
+ * =============================================================================
+ * void* list_find (list_t* listPtr, void* dataPtr);
+ */
+ private List_Node findPrevious(Object dataPtr)
+ {
+ List_Node prevPtr = head;
+ List_Node nodePtr = prevPtr.nextPtr;
+
+ for(; nodePtr != null; nodePtr = nodePtr.nextPtr) {
+ if (compare(nodePtr.dataPtr,dataPtr) >= 0) {
+ return prevPtr;
+ }
+ prevPtr = nodePtr;
+ }
+
+ return prevPtr;
+ }
+
+ /* =============================================================================
+ * list_find
+ * -- Returns NULL if not found, else returns pointer to data
+ * =============================================================================
+ * void* list_find (list_t* listPtr, void* dataPtr);
+ */
+ public Object find(Object dataPtr) {
+ List_Node nodePtr;
+ List_Node prevPtr = findPrevious(dataPtr);
+
+ nodePtr = prevPtr.nextPtr;
+
+ if((nodePtr == null) ||
+ (compare(nodePtr.dataPtr,dataPtr) != 0)) {
+ return null;
+ }
+
+ return (nodePtr.dataPtr);
+ }
+
+ public int compare(Object obj1,Object obj2)
+ {
+ if(flag == 1)
+ {
+ return Packet.compareFragmentID(obj1,obj2);
+ }
+ else
+ return compareObject(obj1,obj2);
+ }
+
+/* =============================================================================
+ * list_insert
+ * -- Return TRUE on success, else FALSE
+ * =============================================================================
+ * bool_t list_insert (list_t* listPtr, void* dataPtr);
+ */
+ public boolean insert(Object dataPtr) {
+ List_Node prevPtr;
+ List_Node nodePtr;
+ List_Node currPtr;
+
+ prevPtr = findPrevious(dataPtr);
+ currPtr = prevPtr.nextPtr;
+
+ nodePtr = allocNode(dataPtr);
+ if (nodePtr == null) {
+ return false;
+ }
+
+ nodePtr.nextPtr = currPtr;
+ prevPtr.nextPtr = nodePtr;
+ size++;
+
+ return true;
+ }
+
+
+/* =============================================================================
+ * list_remove
+ * -- Returns TRUE if successful, else FALSE
+ * =============================================================================
+ * bool_t list_remove (list_t* listPtr, void* dataPtr);
+ */
+ public boolean remove(Object dataPtr)
+ {
+ List_Node prevPtr;
+ List_Node nodePtr;
+
+ prevPtr = findPrevious(dataPtr);
+
+ nodePtr = prevPtr.nextPtr;
+
+ if((nodePtr != null) &&
+ (compare(nodePtr.dataPtr,dataPtr) == 0))
+ {
+ prevPtr.nextPtr = nodePtr.nextPtr;
+ nodePtr.nextPtr = null;
+ nodePtr = null;
+ size--;
+
+ return true;
+ }
+
+ return false;
+ }
+
+ int compareObject(Object obj1,Object obj2) {
+ return 1;
+ }
+
+
+/* =============================================================================
+ * list_clear
+ * -- Removes all elements
+ * =============================================================================
+ * void list_clear (list_t* listPtr);
+ */
+ public void clear() {
+ head = new List_Node();
+ size = 0;
+ }
+
+/* =============================================================================
+ *
+ * End of list.java
+ *
+ * =============================================================================
+ */
+
+ /* Test list */
+
+ public static void main(String[] argv) {
+ List_t listPtr;
+ int[] data1 = new int[5];
+ int[] data2 = new int[6];
+
+ int i;
+
+ System.out.println("Starting...");
+ }
+
+}
+
+
+
--- /dev/null
+/* =============================================================================
+ *
+ * queue.java
+ *
+ * =============================================================================
+ *
+ * Copyright (C) Stanford University, 2006. All Rights Reserved.
+ * Author: Chi Cao Minh
+ *
+ * Ported to Java
+ * Author:Alokika Dash
+ * University of California, Irvine
+ *
+ * =============================================================================
+ *
+ * Unless otherwise noted, the following license applies to STAMP files:
+ *
+ * Copyright (c) 2007, Stanford University
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * 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.
+ *
+ * * Neither the name of Stanford University nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY STANFORD UNIVERSITY ``AS IS'' AND ANY
+ * EXPRESS 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 STANFORD UNIVERSITY 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.
+ *
+ * =============================================================================
+ */
+
+#define QUEUE_GROWTH_FACTOR 2
+
+public class Queue_t {
+ int pop; /* points before element to pop */
+ int push;
+ int capacity;
+ Object[] elements;
+
+ public Queue_t() {
+ }
+
+ /* =============================================================================
+ * queue_alloc
+ * =============================================================================
+ */
+ public static Queue_t queue_alloc (int initCapacity)
+ {
+ Queue_t queuePtr = new Queue_t();
+
+ int capacity = ((initCapacity < 2) ? 2 : initCapacity);
+ queuePtr.elements = new Object[capacity];
+ if (queuePtr.elements == null) {
+ queuePtr = null;
+ return null;
+ }
+ queuePtr.pop = capacity - 1;
+ queuePtr.push = 0;
+ queuePtr.capacity = capacity;
+
+ return queuePtr;
+ }
+
+
+ /* =============================================================================
+ * Pqueue_alloc
+ * =============================================================================
+ */
+ public Queue_t
+ Pqueue_alloc (int initCapacity)
+ {
+ Queue_t queuePtr = new Queue_t();
+
+ int capacity = ((initCapacity < 2) ? 2 : initCapacity);
+ queuePtr.elements = new Object[capacity];
+ if (queuePtr.elements == null) {
+ queuePtr = null;
+ return null;
+ }
+ queuePtr.pop = capacity - 1;
+ queuePtr.push = 0;
+ queuePtr.capacity = capacity;
+
+ return queuePtr;
+ }
+
+ /* =============================================================================
+ * queue_free
+ * =============================================================================
+ */
+ public void
+ queue_free (Queue_t queuePtr)
+ {
+ queuePtr.elements = null;
+ queuePtr = null;
+ }
+
+
+ /* =============================================================================
+ * Pqueue_free
+ * =============================================================================
+ */
+ public void
+ Pqueue_free (Queue_t queuePtr)
+ {
+ queuePtr.elements = null;
+ queuePtr = null;
+ }
+
+
+ /* =============================================================================
+ * TMqueue_free
+ * =============================================================================
+ *
+ public void
+ TMqueue_free (TM_ARGDECL Queue* queuePtr)
+ {
+ queuePtr.elements = null;
+ queuePtr = null;
+ }
+
+*/
+ /* =============================================================================
+ * queue_isEmpty
+ * =============================================================================
+ */
+ public boolean
+ queue_isEmpty ()
+ {
+ //int pop = queuePtr.pop;
+ //int push = queuePtr.push;
+ //int capacity = queuePtr.capacity;
+
+ return (((pop + 1) % capacity == push) ? true : false);
+ }
+
+
+ /* =============================================================================
+ * queue_clear
+ * =============================================================================
+ */
+ public void
+ queue_clear ()
+ {
+ pop = capacity - 1;
+ push = 0;
+ }
+
+
+ /* =============================================================================
+ * TMqueue_isEmpty
+ * =============================================================================
+ */
+
+
+
+ /* =============================================================================
+ * queue_push
+ * =============================================================================
+ */
+ public boolean
+ queue_push (Object dataPtr)
+ {
+
+ if(pop == push) {
+
+ System.out.println("push == pop in Queue.java");
+ return false;
+ }
+
+
+ /* Need to resize */
+ int newPush = (push + 1) % capacity;
+ if (newPush == pop) {
+
+ int newCapacity = capacity * QUEUE_GROWTH_FACTOR;
+ Object[] newElements = new Object[newCapacity];
+ if (newElements == null) {
+ return false;
+ }
+
+ int dst = 0;
+ Object[] tmpelements = elements;
+ if (pop < push) {
+ int src;
+ for (src = (pop + 1); src < push; src++, dst++) {
+ newElements[dst] = elements[src];
+ }
+ } else {
+ int src;
+ for (src = (pop + 1); src < capacity; src++, dst++) {
+ newElements[dst] = elements[src];
+ }
+ for (src = 0; src < push; src++, dst++) {
+ newElements[dst] = elements[src];
+ }
+ }
+
+ //elements = null;
+ elements = newElements;
+ pop = newCapacity - 1;
+ capacity = newCapacity;
+ push = dst;
+ newPush = push + 1; /* no need modulo */
+ }
+
+ elements[push] = dataPtr;
+ push = newPush;
+
+ return true;
+ }
+
+
+ /* =============================================================================
+ * Pqueue_push
+ * =============================================================================
+ */
+ public boolean
+ Pqueue_push (Queue_t queuePtr, Object dataPtr)
+ {
+ int pop = queuePtr.pop;
+ int push = queuePtr.push;
+ int capacity = queuePtr.capacity;
+
+ if(pop == push) {
+ System.out.println("push == pop in Queue.java");
+ return false;
+ }
+
+ /* Need to resize */
+ int newPush = (push + 1) % capacity;
+ if (newPush == pop) {
+
+ int newCapacity = capacity * QUEUE_GROWTH_FACTOR;
+ Object[] newElements = new Object[newCapacity];
+ if (newElements == null) {
+ return false;
+ }
+
+ int dst = 0;
+ Object[] elements = queuePtr.elements;
+ if (pop < push) {
+ int src;
+ for (src = (pop + 1); src < push; src++, dst++) {
+ newElements[dst] = elements[src];
+ }
+ } else {
+ int src;
+ for (src = (pop + 1); src < capacity; src++, dst++) {
+ newElements[dst] = elements[src];
+ }
+ for (src = 0; src < push; src++, dst++) {
+ newElements[dst] = elements[src];
+ }
+ }
+
+ elements = null;
+ queuePtr.elements = newElements;
+ queuePtr.pop = newCapacity - 1;
+ queuePtr.capacity = newCapacity;
+ push = dst;
+ newPush = push + 1; /* no need modulo */
+
+ }
+
+ queuePtr.elements[push] = dataPtr;
+ queuePtr.push = newPush;
+
+ return true;
+ }
+
+ /* =============================================================================
+ * queue_pop
+ * =============================================================================
+ */
+ public Object
+ //queue_pop (Queue queuePtr)
+ queue_pop ()
+ {
+ //int pop = queuePtr.pop;
+ //int push = queuePtr.push;
+ //int capacity = queuePtr.capacity;
+
+ int newPop = (pop + 1) % capacity;
+ if (newPop == push) {
+ return null;
+ }
+
+ //Object dataPtr = queuePtr.elements[newPop];
+ //queuePtr.pop = newPop;
+ Object dataPtr = elements[newPop];
+ pop = newPop;
+
+ return dataPtr;
+ }
+
+
+}
+/* =============================================================================
+ *
+ * End of queue.java
+ *
+ * =============================================================================
+ */
--- /dev/null
+public class Random {
+ long[] mt;
+ int mti;
+ long RANDOM_DEFAULT_SEED;
+ /* period parameter */
+ int N;
+ int M;
+ long MATRIX_A;
+ long UPPER_MASK;
+ long LOWER_MASK;
+
+ public Random() {
+ RANDOM_DEFAULT_SEED = 0L;
+ N = 624;
+ M = 397;
+ mt = new long[N];
+ mti = N;
+ MATRIX_A = 0x9908b0dfL; /* constant vector a */
+ UPPER_MASK = 0x80000000L; /* most significant w-r bits */
+ LOWER_MASK = 0x7fffffffL; /* least significant r bits */
+ }
+
+ public void random_alloc() {
+ init_genrand(this.RANDOM_DEFAULT_SEED);
+ }
+
+ /* initializes mt[N] with a seed */
+ public void init_genrand(long s) {
+ int mti;
+ mt[0]= s & 0xFFFFFFFFL;
+ for (mti=1; mti<N; mti++) {
+ mt[mti] = (1812433253L * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
+ /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
+ /* In the previous versions, MSBs of the seed affect */
+ /* only MSBs of the array mt[]. */
+ /* 2002/01/09 modified by Makoto Matsumoto */
+ mt[mti] &= 0xFFFFFFFFL;
+ /* for >32 bit machines */
+ }
+ this.mti=mti;
+ }
+
+ public void random_seed(long seed) {
+ init_genrand(seed);
+ }
+
+ public long random_generate() {
+ return genrand_int32();
+ }
+
+ //public static long genrand_int32(long[] mt, long mtiPtr) {
+ public long genrand_int32() {
+ long y;
+ long[] mag01= new long[2];
+ mag01[0] = 0x0L;
+ mag01[1] = MATRIX_A;
+ int mti = this.mti;
+
+ /* mag01[x] = x * MATRIX_A for x=0,1 */
+
+ if (mti >= N) { /* generate N words at one time */
+ int kk;
+
+ if (mti == N+1) /* if init_genrand() has not been called, */
+ init_genrand(5489L); /* a default initial seed is used */
+
+ for (kk=0;kk<N-M;kk++) {
+ y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
+ mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[(int)(y & 0x1L)];
+ }
+ for (;kk<N-1;kk++) {
+ y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
+ mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[(int)(y & 0x1L)];
+ }
+ y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
+ mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[(int)(y & 0x1L)];
+
+ mti = 0;
+ }
+
+ y = mt[mti++];
+
+ /* Tempering */
+ y ^= (y >> 11);
+ y ^= (y << 7) & 0x9d2c5680L;
+ y ^= (y << 15) & 0xefc60000L;
+ y ^= (y >> 18);
+
+ this.mti = mti;
+
+ return y;
+ }
+}
--- /dev/null
+public class Vector_t {
+ int size;
+ int capacity;
+ Object[] elements;
+// QuickSort qsort;
+
+ public Vector_t() {
+// qsort = new QuickSort();
+ }
+
+ /* =============================================================================
+ * Vector_alloc
+ * -- Returns null if failed
+ * =============================================================================
+ */
+ public static Vector_t vector_alloc (int initCapacity) {
+ int capacity = Math.imax(initCapacity, 1);
+ Vector_t vectorPtr = new Vector_t();
+ if(vectorPtr != null) {
+ vectorPtr.size = 0;
+ vectorPtr.capacity = capacity;
+ vectorPtr.elements = new Object[capacity];
+ if(vectorPtr.elements == null)
+ return null;
+ }
+ return vectorPtr;
+ }
+
+ /* =============================================================================
+ * Vector_free
+ * =============================================================================
+ */
+ public void
+ vector_free ()
+ {
+ elements = null;
+ }
+
+ /* =============================================================================
+ * Vector_at
+ * -- Returns null if failed
+ * =============================================================================
+ */
+ public Object vector_at (int i) {
+ if ((i < 0) || (i >= size)) {
+ System.out.println("Illegal Vector.element\n");
+ return null;
+ }
+ return (elements[i]);
+ }
+
+
+ /* =============================================================================
+ * Vector_pushBack
+ * -- Returns false if fail, else true
+ * =============================================================================
+ */
+ public boolean vector_pushBack (Object dataPtr) {
+ if (size == capacity) {
+ int newCapacity = capacity * 2;
+ Object[] newElements = new Object[newCapacity];
+
+ //void** newElements = (void**)malloc(newCapacity * sizeof(void*));
+ if (newElements == null) {
+ return false;
+ }
+ capacity = newCapacity;
+ for (int i = 0; i < size; i++) {
+ newElements[i] = elements[i];
+ }
+ elements = null;
+ elements = newElements;
+ }
+
+ elements[size++] = dataPtr;
+
+ return true;
+ }
+
+ /* =============================================================================
+ * Vector_popBack
+ * -- Returns null if fail, else returns last element
+ * =============================================================================
+ */
+ public Object
+ vector_popBack ()
+ {
+ if (size < 1) {
+ return null;
+ }
+
+ return (elements[--(size)]);
+ }
+
+ /* =============================================================================
+ * Vector_getSize
+ * =============================================================================
+ */
+ public int
+ vector_getSize ()
+ {
+ return (size);
+ }
+
+ /* =============================================================================
+ * Vector_clear
+ * =============================================================================
+ */
+ public void
+ vector_clear ()
+ {
+ size = 0;
+ }
+
+ /* =============================================================================
+ * Vector_sort
+ * =============================================================================
+ *
+ public void
+ vector_sort ()
+ {
+ //qsort.sort(elements, 0, (elements.length - 1));
+ qsort.sort(elements);
+ //qsort(elements, size, 4, compare);
+ }
+
+ * =============================================================================
+ * Vector_copy
+ * =============================================================================
+ */
+ public static boolean
+ vector_copy (Vector_t dstVectorPtr, Vector_t srcVectorPtr)
+ {
+ int dstCapacity = dstVectorPtr.capacity;
+ int srcSize = srcVectorPtr.size;
+ if (dstCapacity < srcSize) {
+ int srcCapacity = srcVectorPtr.capacity;
+ Object[] elements = new Object[srcCapacity];
+
+ if (elements == null) {
+ return false;
+ }
+ dstVectorPtr.elements = null;
+ dstVectorPtr.elements = elements;
+ dstVectorPtr.capacity = srcCapacity;
+ }
+
+ for(int i = 0; i< srcSize; i++) {
+ dstVectorPtr.elements[i] = srcVectorPtr.elements[i];
+ }
+
+ dstVectorPtr.size = srcSize;
+
+ return true;
+ }
+}
--- /dev/null
+
+
+public class convertURNHex extends Preprocessor
+{
+ public convertURNHex() {
+ super();
+ }
+ public void process(String str)
+ {
+ char[] str_char = str.toCharArray();
+ int src = 0;
+ int dst = 0;
+ char c;
+
+ while((c = str_char[src]) != '\0') {
+ if (c == '%') {
+ char[] hex = new char[3];
+ hex[0] = Character.toLowerCase((str_char[src+1]));
+ hex[1] = Character.toLowerCase((str_char[src+2]));
+ hex[2] = '\0';
+
+ String a = String.valueOf(hex);
+ int i = Integer.parseInt(a,16);
+ src+=2;
+ str_char[src] = (char)i;
+ }
+ str_char[dst] = str_char[src];
+ dst++;
+ src++;
+ }
+ str_char[dst] = '\0';
+
+ str = String.valueOf(str_char);
+ }
+}
--- /dev/null
+
+public class toLower extends Preprocessor
+{
+ public toLower() {
+ super();
+ }
+ public void process(String str)
+ {
+ return str.toLowerCase();
+ }
+
+}