1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
/* $Id: dbstream.c,v 1.8 2003/12/31 16:10:04 twogood Exp $ */
#include <rapi.h>
#include <synce_log.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
static uint16_t dbstream_read16(const uint8_t** stream)
{
uint16_t value = letoh16(*(uint16_t*)*stream);
*stream += sizeof(uint16_t);
return value;
}
static uint32_t dbstream_read32(const uint8_t** stream)
{
uint32_t value = letoh32(*(uint32_t*)*stream);
*stream += sizeof(uint32_t);
return value;
}
static WCHAR* dbstream_read_string(const uint8_t** stream)
{
WCHAR* str = (WCHAR*)*stream;
/*synce_trace_wstr(str);*/
*stream += sizeof(WCHAR) * (wstrlen(str) + 1);
return str;
}
/*
* Code to convert a database stream to an array of CEPROPVAL structures.
*
* No memory will be allocated; strings and BLOBs will point into the input stream.
*/
bool dbstream_to_propvals(/*{{{*/
const uint8_t* stream,
uint32_t count,
CEPROPVAL* propval)
{
unsigned i;
memset(propval, 0, count * sizeof(CEPROPVAL));
for (i = 0; i < count; i++)
{
propval[i].propid = dbstream_read32(&stream);
if (propval[i].propid & 0x400) /* CEVT_FLAG_EMPTY */
{
/* this flags seems to suggest an empty field */
continue;
}
switch (propval[i].propid & 0xffff)
{
case CEVT_I2:
propval[i].val.iVal = (int16_t)dbstream_read16(&stream);
break;
case CEVT_I4:
propval[i].val.lVal = (int32_t)dbstream_read32(&stream);
break;
case CEVT_UI2:
propval[i].val.uiVal = dbstream_read16(&stream);
break;
case CEVT_UI4:
propval[i].val.ulVal = dbstream_read32(&stream);
break;
#if 0
/* what size? */
case CEVT_BOOL:
printf("0x%08x/%u", propval[i].val.boolVal, propval[i].val.boolVal); break;
#endif
case CEVT_LPWSTR:
propval[i].val.lpwstr = dbstream_read_string(&stream);
break;
case CEVT_FILETIME:
propval[i].val.filetime.dwLowDateTime = dbstream_read32(&stream);
propval[i].val.filetime.dwHighDateTime = dbstream_read32(&stream);
break;
case CEVT_BLOB:
propval[i].val.blob.dwCount = dbstream_read32(&stream);
propval[i].val.blob.lpb = (void*)stream;
stream += propval[i].val.blob.dwCount;
break;
default:
synce_error("unknown data type for propid %08x", propval[i].propid);
return false;
}
}
return true;
}/*}}}*/
static void dbstream_write16(uint8_t** stream, uint16_t value)
{
*(uint16_t*)*stream = htole16(value);
*stream += sizeof(uint16_t);
}
static void dbstream_write32(uint8_t** stream, uint32_t value)
{
*(uint32_t*)*stream = htole32(value);
*stream += sizeof(uint32_t);
}
static void dbstream_write_string(uint8_t** stream, WCHAR* str)
{
if (stream && *stream && str)
{
size_t size = sizeof(WCHAR) * (wstrlen(str) + 1);
memcpy(*stream, str, size);
*stream += size;
}
else
synce_error("One or more parameters are NULL!");
}
/*
* Code to convert an array of CEPROPVAL structures to a database stream.
*/
bool dbstream_from_propvals(/*{{{*/
CEPROPVAL* propval,
uint32_t count,
uint8_t** result,
size_t* result_size)
{
bool success = false;
unsigned i;
uint8_t* data = NULL;
uint8_t* stream = NULL;
size_t size = 8;
/*
* Find out stream size
*/
for (i = 0; i < count; i++)/*{{{*/
{
size += 4;
if (propval[i].propid & 0x400) /* CEVT_FLAG_EMPTY */
{
/* this flags seems to suggest an empty field */
continue;
}
switch (propval[i].propid & 0xffff)
{
case CEVT_I2:
case CEVT_UI2:
size += 2;
break;
case CEVT_I4:
case CEVT_UI4:
size += 4;
break;
#if 0
/* what size? */
case CEVT_BOOL:
#endif
case CEVT_LPWSTR:
size += (wstrlen(propval[i].val.lpwstr) + 1) * 2;
break;
case CEVT_FILETIME:
size += 8;
break;
case CEVT_BLOB:
size += 4 + propval[i].val.blob.dwCount;
break;
default:
synce_error("unknown data type for propid %08x", propval[i].propid);
goto exit;
}
}/*}}}*/
stream = data = calloc(1, size);
dbstream_write32(&stream, count);
dbstream_write32(&stream, 0);
/*
* Create stream
*/
for (i = 0; i < count; i++)/*{{{*/
{
dbstream_write32(&stream, propval[i].propid);
if (propval[i].propid & 0x400) /* CEVT_FLAG_EMPTY */
{
/* this flags seems to suggest an empty field */
continue;
}
switch (propval[i].propid & 0xffff)
{
case CEVT_I2:
dbstream_write16(&stream, propval[i].val.iVal);
break;
case CEVT_I4:
dbstream_write32(&stream, propval[i].val.lVal);
break;
case CEVT_UI2:
dbstream_write16(&stream, propval[i].val.uiVal);
break;
case CEVT_UI4:
dbstream_write32(&stream, propval[i].val.ulVal);
break;
#if 0
/* what size? */
case CEVT_BOOL:
printf("0x%08x/%u", propval[i].val.boolVal, propval[i].val.boolVal); break;
#endif
case CEVT_LPWSTR:
if (propval[i].val.lpwstr)
dbstream_write_string(&stream, propval[i].val.lpwstr);
else
synce_warning("String for propid %08x is NULL!", propval[i].propid);
break;
case CEVT_FILETIME:
dbstream_write32(&stream, propval[i].val.filetime.dwLowDateTime);
dbstream_write32(&stream, propval[i].val.filetime.dwHighDateTime);
break;
case CEVT_BLOB:
assert(propval[i].val.blob.lpb);
dbstream_write32(&stream, propval[i].val.blob.dwCount);
memcpy(stream, propval[i].val.blob.lpb, propval[i].val.blob.dwCount);
stream += propval[i].val.blob.dwCount;
break;
default:
synce_error("unknown data type for propid %08x", propval[i].propid);
goto exit;
}
}/*}}}*/
if (stream != (data + size))
{
synce_error("Unexpected stream size, your memory may have become corrupted.");
goto exit;
}
success = true;
exit:
if (success)
{
if (result)
*result = data;
if (result_size)
*result_size = size;
}
else
{
if (data)
free(data);
*result = NULL;
*result_size = 0;
}
return success;
}/*}}}*/
|