Compiles
[iotcloud.git] / version2 / src / C / Slot.cc
index 982b87b485ebf0c40517c0f6274ff85441440e86..10c92ded4e5a592518ab3e7dba9f3c89c945690c 100644 (file)
@@ -1,24 +1,49 @@
 #include "Slot.h"
-
-Slot::Slot(Table *_table, int64_t _seqnum, int64_t _machineid, char *_prevhmac, char *_hmac, int64_t _localSequenceNumber) {
-       seqnum = _seqnum;
-       machineid = _machineid;
-       prevhmac = _prevhmac;
-       hmac = _hmac;
-       entries = new Vector<Entry *>();
-       livecount = 1;
-       seqnumlive = true;
-       freespace = SLOT_SIZE - getBaseSize();
-       table = _table;
-       localSequenceNumber = _localSequenceNumber;
+#include "ByteBuffer.h"
+#include "Entry.h"
+#include "Error.h"
+#include "CloudComm.h"
+#include "Table.h"
+#include "LastMessage.h"
+#include "Mac.h"
+
+Slot::Slot(Table *_table, int64_t _seqnum, int64_t _machineid, Array<char> *_prevhmac, Array<char> *_hmac, int64_t _localSequenceNumber) :
+       seqnum(_seqnum),
+       prevhmac(_prevhmac),
+       hmac(_hmac),
+       machineid(_machineid),
+       entries(new Vector<Entry *>()),
+       livecount(1),
+       seqnumlive(true),
+       freespace(SLOT_SIZE - getBaseSize()),
+       table(_table),
+       localSequenceNumber(_localSequenceNumber) {
 }
 
-Slot::Slot(Table *_table, int64_t _seqnum, int64_t _machineid, char *_prevhmac, int64_t _localSequenceNumber) {
-       this(_table, _seqnum, _machineid, _prevhmac, NULL, _localSequenceNumber);
+Slot::Slot(Table *_table, int64_t _seqnum, int64_t _machineid, Array<char> *_prevhmac, int64_t _localSequenceNumber) :
+       seqnum(_seqnum),
+       prevhmac(_prevhmac),
+       hmac(NULL),
+       machineid(_machineid),
+       entries(new Vector<Entry *>()),
+       livecount(1),
+       seqnumlive(true),
+       freespace(SLOT_SIZE - getBaseSize()),
+       table(_table),
+       localSequenceNumber(_localSequenceNumber) {
 }
 
-Slot::Slot(Table *_table, int64_t _seqnum, int64_t _machineid, int64_t _localSequenceNumber) {
-       this(_table, _seqnum, _machineid, new char[HMAC_SIZE], NULL, _localSequenceNumber);
+Slot::Slot(Table *_table, int64_t _seqnum, int64_t _machineid, int64_t _localSequenceNumber) :
+       seqnum(_seqnum),
+       prevhmac(new Array<char>(HMAC_SIZE)),
+       hmac(NULL),
+       machineid(_machineid),
+       entries(new Vector<Entry *>()),
+       livecount(1),
+       seqnumlive(true),
+       freespace(SLOT_SIZE - getBaseSize()),
+       table(_table),
+       localSequenceNumber(_localSequenceNumber) {
 }
 
 Entry *Slot::addEntry(Entry *e) {
@@ -29,12 +54,6 @@ Entry *Slot::addEntry(Entry *e) {
        return e;
 }
 
-void Slot::removeEntry(Entry *e) {
-       entries->remove(e);
-       livecount--;
-       freespace += e->getSize();
-}
-
 void Slot::addShallowEntry(Entry *e) {
        entries->add(e);
        livecount++;
@@ -54,32 +73,36 @@ Vector<Entry *> *Slot::getEntries() {
        return entries;
 }
 
-Slot *Slotdecode(Table *table, char *array, Mac *mac) {
-       mac->update(array, HMAC_SIZE, array.length - HMAC_SIZE);
-       char *realmac = mac->doFinal();
+Slot *Slot_decode(Table *table, Array<char> *array, Mac *mac) {
+       mac->update(array, HMAC_SIZE, array->length() - HMAC_SIZE);
+       Array<char> *realmac = mac->doFinal();
 
        ByteBuffer *bb = ByteBuffer_wrap(array);
-       char *hmac = new char[HMAC_SIZE];
-       char *prevhmac = new char[HMAC_SIZE];
+       Array<char> *hmac = new Array<char>(HMAC_SIZE);
+       Array<char> *prevhmac = new Array<char>(HMAC_SIZE);
        bb->get(hmac);
        bb->get(prevhmac);
-       if (!Arrays.equals(realmac, hmac))
+       if (!realmac->equals(hmac))
                throw new Error("Server Error: Invalid HMAC!  Potential Attack!");
 
        int64_t seqnum = bb->getLong();
        int64_t machineid = bb->getLong();
        int numentries = bb->getInt();
-       Slot slot = new Slot(table, seqnum, machineid, prevhmac, hmac, -1);
+       Slot *slot = new Slot(table, seqnum, machineid, prevhmac, hmac, -1);
 
        for (int i = 0; i < numentries; i++) {
-               slot->addShallowEntry(Entry->decode(slot, bb));
+               slot->addShallowEntry(Entry_decode(slot, bb));
        }
 
        return slot;
 }
 
-char *Slot::encode(Mac mac) {
-       char *array = new char[SLOT_SIZE];
+char Slot::getType() {
+       return TypeSlot;
+}
+
+Array<char> *Slot::encode(Mac *mac) {
+       Array<char> *array = new Array<char>(SLOT_SIZE);
        ByteBuffer *bb = ByteBuffer_wrap(array);
        /* Leave space for the slot HMAC.  */
        bb->position(HMAC_SIZE);
@@ -87,12 +110,13 @@ char *Slot::encode(Mac mac) {
        bb->putLong(seqnum);
        bb->putLong(machineid);
        bb->putInt(entries->size());
-       for (Entry entry : entries) {
+       for (uint ei = 0; ei < entries->size(); ei++) {
+               Entry *entry = entries->get(ei);
                entry->encode(bb);
        }
        /* Compute our HMAC */
-       mac->update(array, HMAC_SIZE, array.length - HMAC_SIZE);
-       char *realmac = mac->doFinal();
+       mac->update(array, HMAC_SIZE, array->length() - HMAC_SIZE);
+       Array<char> *realmac = mac->doFinal();
        hmac = realmac;
        bb->position(0);
        bb->put(realmac);
@@ -108,9 +132,10 @@ char *Slot::encode(Mac mac) {
 
 Vector<Entry *> *Slot::getLiveEntries(bool resize) {
        Vector<Entry *> *liveEntries = new Vector<Entry *>();
-       for (Entry *entry : entries) {
+       for (uint ei = 0; ei < entries->size(); ei++) {
+               Entry *entry = entries->get(ei);
                if (entry->isLive()) {
-                       if (!resize || entry->getType() != Entry->TypeTableStatus)
+                       if (!resize || entry->getType() != TypeTableStatus)
                                liveEntries->add(entry);
                }
        }
@@ -143,8 +168,8 @@ void Slot::decrementLiveCount() {
        }
 }
 
-char *Slot::getSlotCryptIV() {
-       ByteBuffer *buffer = ByteBuffer_allocate(CloudComm.IV_SIZE);
+Array<char> *Slot::getSlotCryptIV() {
+       ByteBuffer *buffer = ByteBuffer_allocate(CloudComm_IV_SIZE);
        buffer->putLong(machineid);
        int64_t localSequenceNumberShift = localSequenceNumber << 16;
        buffer->putLong(localSequenceNumberShift);