int CBInventoryDeserialise(CBInventory * self) { CBByteArray * bytes = CBGetMessage(self)->bytes; if (! bytes) { CBLogError("Attempting to deserialise a CBInventory with no bytes."); return CB_DESERIALISE_ERROR; } if (bytes->length < 37) { CBLogError("Attempting to deserialise a CBInventory with less bytes than required for one item."); return CB_DESERIALISE_ERROR; } CBVarInt itemNum = CBByteArrayReadVarInt(bytes, 0); if (itemNum.val > 50000) { CBLogError("Attempting to deserialise a CBInventory with a var int over 50000."); return CB_DESERIALISE_ERROR; } self->itemNum = 0; self->itemFront = NULL; // Run through the items and deserialise each one. int cursor = itemNum.size; for (int x = 0; x < itemNum.val; x++) { // Make new CBInventoryItem from the rest of the data. CBByteArray * data = CBByteArraySubReference(bytes, cursor, bytes->length-cursor); CBInventoryItem * item = CBNewInventoryItemFromData(data); // Deserialise int len = CBInventoryItemDeserialise(item); if (len == CB_DESERIALISE_ERROR){ CBLogError("CBInventory cannot be deserialised because of an error with the CBInventoryItem number %u.", x); CBReleaseObject(data); return CB_DESERIALISE_ERROR; } // Take item CBInventoryTakeInventoryItem(self, item); // Adjust length data->length = len; CBReleaseObject(data); cursor += len; } return cursor; }
uint32_t CBInventoryBroadcastDeserialise(CBInventoryBroadcast * self){ CBByteArray * bytes = CBGetMessage(self)->bytes; if (NOT bytes) { CBGetMessage(self)->onErrorReceived(CB_ERROR_MESSAGE_DESERIALISATION_NULL_BYTES,"Attempting to deserialise a CBInventoryBroadcast with no bytes."); return 0; } if (bytes->length < 37) { CBGetMessage(self)->onErrorReceived(CB_ERROR_MESSAGE_DESERIALISATION_BAD_BYTES,"Attempting to deserialise a CBInventoryBroadcast with less bytes than required for one item."); return 0; } CBVarInt itemNum = CBVarIntDecode(bytes, 0); if (itemNum.val > 1388) { CBGetMessage(self)->onErrorReceived(CB_ERROR_MESSAGE_DESERIALISATION_BAD_BYTES,"Attempting to deserialise a CBInventoryBroadcast with a var int over 1388."); return 0; } // Run through the items and deserialise each one. self->items = malloc(sizeof(*self->items) * (size_t)itemNum.val); if (NOT self->items) { CBGetMessage(self)->onErrorReceived(CB_ERROR_OUT_OF_MEMORY,"Cannot allocate %i bytes of memory in CBInventoryBroadcastDeserialise",sizeof(*self->items) * (size_t)itemNum.val); return 0; } self->itemNum = itemNum.val; uint16_t cursor = itemNum.size; for (uint16_t x = 0; x < itemNum.val; x++) { // Make new CBInventoryItem from the rest of the data. CBByteArray * data = CBByteArraySubReference(bytes, cursor, bytes->length-cursor); if (NOT data) { CBGetMessage(self)->onErrorReceived(CB_ERROR_INIT_FAIL,"Could not create a new CBByteArray in CBInventoryBroadcastDeserialise for inventory broadcast number %u.",x); return 0; } self->items[x] = CBNewInventoryItemFromData(data, CBGetMessage(self)->onErrorReceived); if (NOT self->items[x]) { CBGetMessage(self)->onErrorReceived(CB_ERROR_INIT_FAIL,"Could not create a new CBInventoryItem in CBInventoryBroadcastDeserialise for inventory broadcast number %u.",x); CBReleaseObject(data); return 0; } // Deserialise uint8_t len = CBInventoryItemDeserialise(self->items[x]); if (NOT len){ CBGetMessage(self)->onErrorReceived(CB_ERROR_MESSAGE_DESERIALISATION_BAD_BYTES,"CBInventoryBroadcast cannot be deserialised because of an error with the CBInventoryItem number %u.",x); CBReleaseObject(data); return 0; } // Adjust length data->length = len; CBReleaseObject(data); cursor += len; } return cursor; }
uint32_t CBInventoryBroadcastDeserialise(CBInventoryBroadcast * self){ CBByteArray * bytes = CBGetMessage(self)->bytes; if (NOT bytes) { CBGetMessage(self)->events->onErrorReceived(CB_ERROR_MESSAGE_DESERIALISATION_NULL_BYTES,"Attempting to deserialise a CBInventoryBroadcast with no bytes."); return 0; } if (bytes->length < 37) { CBGetMessage(self)->events->onErrorReceived(CB_ERROR_MESSAGE_DESERIALISATION_BAD_BYTES,"Attempting to deserialise a CBInventoryBroadcast with less bytes than required for one item."); return 0; } CBVarInt itemNum = CBVarIntDecode(bytes, 0); if (itemNum.val > 1388) { CBGetMessage(self)->events->onErrorReceived(CB_ERROR_MESSAGE_DESERIALISATION_BAD_BYTES,"Attempting to deserialise a CBInventoryBroadcast with a var int over 1388."); return 0; } // Run through the items and deserialise each one. self->items = malloc(sizeof(*self->items) * (size_t)itemNum.val); self->itemNum = itemNum.val; uint16_t cursor = itemNum.size; for (uint16_t x = 0; x < itemNum.val; x++) { // Make new CBInventoryItem from the rest of the data. CBByteArray * data = CBByteArraySubReference(bytes, cursor, bytes->length-cursor); self->items[x] = CBNewInventoryItemFromData(data, CBGetMessage(self)->events); // Deserialise uint8_t len = CBInventoryItemDeserialise(self->items[x]); if (NOT len) { CBGetMessage(self)->events->onErrorReceived(CB_ERROR_MESSAGE_DESERIALISATION_BAD_BYTES,"CBInventoryBroadcast cannot be deserialised because of an error with the CBInventoryItem number %u.",x); // Release bytes CBReleaseObject(data); // Because of failure release the CBInventoryItems for (uint16_t y = 0; y < x + 1; y++) { CBReleaseObject(self->items[y]); } free(self->items); self->items = NULL; self->itemNum = 0; return 0; } // Adjust length data->length = len; CBReleaseObject(data); cursor += len; } return cursor; }