static bit32 mailbox_index_record_to_buf(struct index_record *record, int version,
                                  unsigned char *buf)
{
    int n;
    bit32 crc;
    uint32_t system_flags = 0;

    memset(buf, 0, INDEX_RECORD_SIZE);

    /* keep the low bits of the offset in the offset field */
    uint32_t cache_offset_field = record->cache_offset & 0xffffffff;
    /* mix in the high bits of the offset to the top half of the version field */
    uint32_t cache_version_field = (uint32_t)record->cache_version | (record->cache_offset & 0xffff00000000) >> 16;

    *((bit32 *)(buf+OFFSET_UID)) = htonl(record->uid);
    *((bit32 *)(buf+OFFSET_INTERNALDATE)) = htonl(record->internaldate);
    *((bit32 *)(buf+OFFSET_SENTDATE)) = htonl(record->sentdate);
    *((bit32 *)(buf+OFFSET_SIZE)) = htonl(record->size);
    *((bit32 *)(buf+OFFSET_HEADER_SIZE)) = htonl(record->header_size);
    if (version >= 12) {
        *((bit32 *)(buf+OFFSET_GMTIME)) = htonl(record->gmtime);
    }
    else {
        /* content_offset was always the same */
        *((bit32 *)(buf+OFFSET_GMTIME)) = htonl(record->header_size);
    }
    *((bit32 *)(buf+OFFSET_CACHE_OFFSET)) = htonl(cache_offset_field);
    *((bit32 *)(buf+OFFSET_LAST_UPDATED)) = htonl(record->last_updated);

    /* serialise system flags and internal flags */
    system_flags = record->system_flags | record->internal_flags;
    *((bit32 *)(buf+OFFSET_SYSTEM_FLAGS)) = htonl(system_flags);

    for (n = 0; n < MAX_USER_FLAGS/32; n++) {
        *((bit32 *)(buf+OFFSET_USER_FLAGS+4*n)) = htonl(record->user_flags[n]);
    }
    if (version > 14) {
        *((bit32 *)(buf+OFFSET_SAVEDATE)) = htonl(record->savedate);
    }
    else {
        *((bit32 *)(buf+OFFSET_SAVEDATE)) = 0; // blank out old content_lines
    }
    *((bit32 *)(buf+OFFSET_CACHE_VERSION)) = htonl(cache_version_field);

    /* versions less than 8 had no modseq */
    if (version < 8) {
        return 0;
    }

    /* versions 8 and 9 only had a smaller UUID, which we will ignore,
     * but the modseq existed and was at offset 72 and 76 */
    if (version < 10) {
        *((bit64 *)(buf+72)) = htonll(record->modseq);
        return 0;
    }

    /* otherwise we have the GUID and MODSEQ in their current place */
    message_guid_export(&record->guid, (char *)buf+OFFSET_MESSAGE_GUID);
    *((bit64 *)(buf+OFFSET_MODSEQ)) = htonll(record->modseq);

    /* version 12 added the CACHE_CRC and RECORD_CRC, but at a lower point */
    if (version < 13) {
        *((bit32 *)(buf+88)) = htonl(record->cache_crc);
        /* calculate the checksum */
        crc = crc32_map((char *)buf, 92);
        *((bit32 *)(buf+92)) = htonl(crc);
        return crc;
    }

    *((bit64 *)(buf+OFFSET_THRID)) = htonll(record->cid);

    /* version 16 added createdmodseq, pushing the CRCs down */
    if (version < 16) {
        *((bit32 *)(buf+96)) = htonl(record->cache_crc);
        crc = crc32_map((char *)buf, 100);
        *((bit32 *)(buf+100)) = htonl(crc);
        return crc;
    }

    *((bit64 *)(buf+OFFSET_CREATEDMODSEQ)) = htonll(record->createdmodseq);
    *((bit32 *)(buf+OFFSET_CACHE_CRC)) = htonl(record->cache_crc);

    /* calculate the checksum */
    crc = crc32_map((char *)buf, OFFSET_RECORD_CRC);
    *((bit32 *)(buf+OFFSET_RECORD_CRC)) = htonl(crc);

    return crc;
}
static bit32 mailbox_index_record_to_buf(struct index_record *record, int version,
                                  unsigned char *buf)
{
    int n;
    bit32 crc;
    uint32_t system_flags = 0;

    memset(buf, 0, INDEX_RECORD_SIZE);

     
    uint32_t cache_offset_field = record->cache_offset & 0xffffffff;
     
    uint32_t cache_version_field = (uint32_t)record->cache_version | (record->cache_offset & 0xffff00000000) >> 16;

    *((bit32 *)(buf+OFFSET_UID)) = htonl(record->uid);
    *((bit32 *)(buf+OFFSET_INTERNALDATE)) = htonl(record->internaldate);
    *((bit32 *)(buf+OFFSET_SENTDATE)) = htonl(record->sentdate);
    *((bit32 *)(buf+OFFSET_SIZE)) = htonl(record->size);
    *((bit32 *)(buf+OFFSET_HEADER_SIZE)) = htonl(record->header_size);
    if (version >= 12) {
        *((bit32 *)(buf+OFFSET_GMTIME)) = htonl(record->gmtime);
    }
    else {
         
        *((bit32 *)(buf+OFFSET_GMTIME)) = htonl(record->header_size);
    }
    *((bit32 *)(buf+OFFSET_CACHE_OFFSET)) = htonl(cache_offset_field);
    *((bit32 *)(buf+OFFSET_LAST_UPDATED)) = htonl(record->last_updated);

     
    system_flags = record->system_flags | record->internal_flags;
    *((bit32 *)(buf+OFFSET_SYSTEM_FLAGS)) = htonl(system_flags);

    for (n = 0; n < MAX_USER_FLAGS/32; n++) {
        *((bit32 *)(buf+OFFSET_USER_FLAGS+4*n)) = htonl(record->user_flags[n]);
    }
    if (version > 14) {
        *((bit32 *)(buf+OFFSET_SAVEDATE)) = htonl(record->savedate);
    }
    else {
        *((bit32 *)(buf+OFFSET_SAVEDATE)) = 0;  
    }
    *((bit32 *)(buf+OFFSET_CACHE_VERSION)) = htonl(cache_version_field);

     
    if (version < 8) {
        return 0;
    }

     
    if (version < 10) {
        *((bit64 *)(buf+72)) = htonll(record->modseq);
        return 0;
    }

     
    message_guid_export(&record->guid, (char *)buf+OFFSET_MESSAGE_GUID);
    *((bit64 *)(buf+OFFSET_MODSEQ)) = htonll(record->modseq);

     
    if (version < 13) {
        *((bit32 *)(buf+88)) = htonl(record->cache_crc);
         
        crc = crc32_map((char *)buf, 92);
        *((bit32 *)(buf+92)) = htonl(crc);
        return crc;
    }

    *((bit64 *)(buf+OFFSET_THRID)) = htonll(record->cid);

     
    if (version < 16) {
        *((bit32 *)(buf+96)) = htonl(record->cache_crc);
        crc = crc32_map((char *)buf, 100);
        *((bit32 *)(buf+100)) = htonl(crc);
        return crc;
    }

    *((bit64 *)(buf+OFFSET_CREATEDMODSEQ)) = htonll(record->createdmodseq);
    *((bit32 *)(buf+OFFSET_CACHE_CRC)) = htonl(record->cache_crc);

     
    crc = crc32_map((char *)buf, OFFSET_RECORD_CRC);
    *((bit32 *)(buf+OFFSET_RECORD_CRC)) = htonl(crc);

    return crc;
}