Esempio n. 1
0
/* Serialize 'pref' to a buffer 'buf_ptr' of size 'buf_len_cb_ptr'.
   Return TRUE if ok, FALSE if failed (e.g. buffer is not large enough).
   If 'buf_ptr' is NULL, returns desired size in 'buf_len_cb_ptr'.
   */
static int prefs_serialize_pref(prefs_data *pref, TCHAR **buf_ptr, size_t *buf_len_cb_ptr)
{
    size_t  len_cb;
    TCHAR * name_with_type;
    int     f_ok;

    assert(pref);
    assert(pref->name);
    assert(pref_type_valid(pref->type));
    assert(buf_len_cb_ptr);

    if (!buf_ptr) {
        len_cb  = netstr_tstrn_serialized_len_cb(TYPE_PREFIX_CCH_LEN + tstr_len(pref->name));
        if (PT_INT == pref->type)
            len_cb += netstr_int_serialized_len_cb(*pref->data.data_int);
        else if (PT_STRING == pref->type)
            len_cb += netstr_tstr_serialized_len_cb(*pref->data.data_str);
        else
            assert(0);
        *buf_len_cb_ptr = len_cb;
        return TRUE;
    }

    name_with_type = pref_tstr_with_type(pref->name, pref->type);
    if (!name_with_type) return FALSE;
    f_ok = netstr_tstr_serialize(name_with_type, buf_ptr, buf_len_cb_ptr);
    free((void*)name_with_type);
    if (!f_ok) 
        return FALSE;

    if (PT_INT == pref->type)
        f_ok = netstr_int_serialize(*pref->data.data_int, buf_ptr, buf_len_cb_ptr);
    else if (PT_STRING == pref->type)
        f_ok = netstr_tstr_serialize(*pref->data.data_str, buf_ptr, buf_len_cb_ptr);
    else
        assert(0);

    if (!f_ok)
        return FALSE;

    return TRUE;
}
Esempio n. 2
0
int netstr_int_serialize(int num, TCHAR **buf_ptr, size_t *buf_len_cb_ptr)
{
    TCHAR * num_str;
    int     f_ok;

    assert(buf_len_cb_ptr);
    if (!buf_len_cb_ptr)
        return FALSE;

    if (!buf_ptr)
    {
        *buf_len_cb_ptr += netstr_int_serialized_len_cb(num);
        return TRUE;
    }

    num_str = tstr_printf(_T("%d"), num);
    if (!num_str)
        return FALSE;

    f_ok = netstr_tstr_serialize(num_str, buf_ptr, buf_len_cb_ptr);
    free((void*)num_str);
    return f_ok;
}