message-part-serialize.c revision 2fe48617ce6a8d296afbbb84c9f53943b48ed8c3
5f5870385cff47efd2f58e7892f251cf13761528Timo Sirainen/* Copyright (C) 2002 Timo Sirainen */
f7423cbbd9dea363a5df18ebb96da055a977ae79Timo Sirainen Serialized a series of SerializedMessageParts:
a3fe8c0c54d87822f4b4f8f0d10caac611861b2bTimo Sirainen root's first children
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen children's first children
c99fe55d4535d839a6ad0735c4719e076a1adb2cTimo Sirainen root's next children
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen/* struct is 8 byte aligned */
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainentypedef struct {
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen unsigned int flags;
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainenstatic void message_part_serialize_part(MessagePart *part,
4d4d6d4745682790c20d759ba93dbea46b812c5dTimo Sirainen /* make sure we have space */
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen buf_size = sizeof(SerializedMessagePart) * (*size);
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen *spart_base = t_buffer_reget(*spart_base, buf_size);
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen /* create serialized part */
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen memset(spart, 0, sizeof(SerializedMessagePart));
b437874782ad048daa155e0ac863c2326c3f5e43Timo Sirainen spart->header_physical_size = part->header_size.physical_size;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen spart->header_virtual_size = part->header_size.virtual_size;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen spart->header_lines = part->header_size.lines;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen spart->body_physical_size = part->body_size.physical_size;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen spart->body_virtual_size = part->body_size.virtual_size;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainenconst void *message_part_serialize(MessagePart *part, size_t *size)
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen spart_base = t_buffer_get(sizeof(SerializedMessagePart) * buf_size);
f158d9a303bb15a6848ca276c9391c7ca52e452bTimo Sirainen message_part_serialize_part(part, NULL, &spart_base, &pos, &buf_size);
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainenmessage_part_deserialize_part(Pool pool, MessagePart *parent,
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen unsigned int i;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen for (i = 0; i < child_count && *count > 0; i++) {
c444eeaa2866152cf62652698aa11b125e8454bcTimo Sirainen part->header_size.physical_size = spart->header_physical_size;
c444eeaa2866152cf62652698aa11b125e8454bcTimo Sirainen part->header_size.virtual_size = spart->header_virtual_size;
78fa3c578c14ee8a612f86cf73b6181c7f16463fTimo Sirainen part->header_size.lines = spart->header_lines;
78fa3c578c14ee8a612f86cf73b6181c7f16463fTimo Sirainen part->body_size.physical_size = spart->body_physical_size;
8451c4b5afc1ff5366438b2766f75b592c33e1ecTimo Sirainen part->body_size.virtual_size = spart->body_virtual_size;
8451c4b5afc1ff5366438b2766f75b592c33e1ecTimo Sirainen part->children = message_part_deserialize_part(pool, part,
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo SirainenMessagePart *message_part_deserialize(Pool pool, const void *data,
a3fe8c0c54d87822f4b4f8f0d10caac611861b2bTimo Sirainen /* make sure it looks valid */
a3fe8c0c54d87822f4b4f8f0d10caac611861b2bTimo Sirainen if (size == 0 || (size % sizeof(SerializedMessagePart)) != 0)
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen return message_part_deserialize_part(pool, NULL, &spart, &count,
01230de017cd273de41143d88e9c18df1243ae8aTimo Sirainen (unsigned int)count);
b8efab7ea8876c0a33a73ee0d08eddada31320f8Timo Sirainenint message_part_serialize_update_header(void *data, size_t size,
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen /* make sure it looks valid */
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen if (size == 0 || (size % sizeof(SerializedMessagePart)) != 0)
f7423cbbd9dea363a5df18ebb96da055a977ae79Timo Sirainen pos_diff = (off_t)hdr_size->physical_size - spart->header_physical_size;
f7423cbbd9dea363a5df18ebb96da055a977ae79Timo Sirainen spart->header_physical_size = hdr_size->physical_size;
f7423cbbd9dea363a5df18ebb96da055a977ae79Timo Sirainen spart->header_virtual_size = hdr_size->virtual_size;
32b78da5dfbbf6a06b3dbdc9278c60b55714f9bcTimo Sirainen /* have to update all positions */
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen /* invalid offset, might cause overflow */
c444eeaa2866152cf62652698aa11b125e8454bcTimo Sirainenint message_part_deserialize_size(const void *data, size_t size,
c444eeaa2866152cf62652698aa11b125e8454bcTimo Sirainen /* make sure it looks valid */
c444eeaa2866152cf62652698aa11b125e8454bcTimo Sirainen if (size == 0 || (size % sizeof(SerializedMessagePart)) != 0)
c444eeaa2866152cf62652698aa11b125e8454bcTimo Sirainen hdr_size->physical_size = spart->header_physical_size;
c444eeaa2866152cf62652698aa11b125e8454bcTimo Sirainen hdr_size->virtual_size = spart->header_virtual_size;
c444eeaa2866152cf62652698aa11b125e8454bcTimo Sirainen body_size->physical_size = spart->body_physical_size;