File: MAPIStoreTypes.m

package info (click to toggle)
sogo 2.2.9%2Bgit20141017-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 43,812 kB
  • sloc: objc: 115,592; python: 5,696; sh: 1,380; perl: 861; makefile: 240; xml: 114; sql: 53; php: 43
file content (370 lines) | stat: -rw-r--r-- 9,462 bytes parent folder | download | duplicates (2)
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/* MAPIStoreTypes.m - this file is part of SOGo
 *
 * Copyright (C) 2010-2012 Inverse inc.
 *
 * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
 *
 * This file is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * This file is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#import <Foundation/NSArray.h>
#import <Foundation/NSCalendarDate.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSException.h>
#import <Foundation/NSNull.h>
#import <Foundation/NSString.h>

#import "NSArray+MAPIStore.h"
#import "NSData+MAPIStore.h"
#import "NSDate+MAPIStore.h"

#import "MAPIStoreTypes.h"

#undef DEBUG
#include <mapistore/mapistore.h>
#include <mapistore/mapistore_errors.h>

NSTimeZone *utcTZ;

uint8_t *
MAPIBoolValue (void *memCtx, BOOL value)
{
  uint8_t *boolValue;

  boolValue = talloc_zero (memCtx, uint8_t);
  *boolValue = value;

  return boolValue;
}

uint32_t *
MAPILongValue (void *memCtx, uint32_t value)
{
  uint32_t *longValue;

  longValue = talloc_zero (memCtx, uint32_t);
  *longValue = value;

  return longValue;
}

uint64_t *
MAPILongLongValue (void *memCtx, uint64_t value)
{
  uint64_t *llongValue;

  llongValue = talloc_zero (memCtx, uint64_t);
  *llongValue = value;

  return llongValue;
}

double *
MAPIDoubleValue (void *memCtx, double value)
{
  double *doubleValue;

  doubleValue = talloc_zero (memCtx, double);
  *doubleValue = value;

  return doubleValue;
}

id
NSObjectFromMAPISPropValue (const struct mapi_SPropValue *value)
{
  short int valueType;
  id result;

  valueType = (value->ulPropTag & 0xffff);
  switch (valueType)
    {
    case PT_NULL:
      result = [NSNull null];
      break;
    case PT_SHORT:
      result = [NSNumber numberWithUnsignedShort: value->value.i];
      break;
    case PT_LONG:
    case PT_ERROR:
      result = [NSNumber numberWithUnsignedLong: value->value.l];
      break;
    case PT_I8:
      result = [NSNumber numberWithUnsignedLongLong: value->value.d];
      break;
    case PT_BOOLEAN:
      result = [NSNumber numberWithBool: (value->value.b ? YES : NO)];
      break;
    case PT_DOUBLE:
      result = [NSNumber numberWithDouble: value->value.dbl];
      break;
    case PT_UNICODE:
      result = [NSString stringWithUTF8String: value->value.lpszW];
      break;
    case PT_STRING8:
      result = [NSString stringWithUTF8String: value->value.lpszA];
      break;
    case PT_SYSTIME:
      result = [NSCalendarDate dateFromFileTime: &(value->value.ft)];
      break;
    case PT_BINARY:
    case PT_SVREID:
      result = [NSData dataWithShortBinary: &value->value.bin];
      break;
    case PT_CLSID:
      result = [NSData dataWithGUID: &value->value.lpguid];
      break;

    case PT_MV_LONG:
      result = [NSArray arrayFromMAPIMVLong: &value->value.MVl];
      break;
    case PT_MV_STRING8:
      result = [NSArray arrayFromMAPIMVString: &value->value.MVszA];
      break;
    case PT_MV_UNICODE:
      result = [NSArray arrayFromMAPIMVUnicode: &value->value.MVszW];
      break;
    case PT_MV_CLSID:
      result = [NSArray arrayFromMAPIMVGuid: &value->value.MVguid];
      break;
    case PT_MV_BINARY:
      result = [NSArray arrayFromMAPIMVBinary: &value->value.MVbin];
      break;

    default:
// #define	PT_UNSPECIFIED		0x0
// #define	PT_I2			0x2
// #define	PT_CURRENCY		0x6
// #define	PT_APPTIME		0x7
// #define	PT_ERROR		0xa
// #define	PT_OBJECT		0xd
// #define	PT_I8			0x14
// #define	PT_SRESTRICT		0xFD
// #define	PT_ACTIONS		0xFE
      result = [NSNull null];
      abort();
      NSLog (@"%s: object type not handled: %d (0x%.4x)",
             __PRETTY_FUNCTION__, valueType, valueType);
    }

  return result;
}

id
NSObjectFromSPropValue (const struct SPropValue *value)
{
  short int valueType;
  id result;

  valueType = (value->ulPropTag & 0xffff);
  switch (valueType)
    {
    case PT_NULL:
      result = [NSNull null];
      break;
    case PT_SHORT:
      result = [NSNumber numberWithShort: value->value.i];
      break;
    case PT_LONG:
    case PT_ERROR:
      result = [NSNumber numberWithLong: value->value.l];
      break;
    case PT_I8:
      result = [NSNumber numberWithUnsignedLongLong: value->value.d];
      break;
    case PT_BOOLEAN:
      result = [NSNumber numberWithBool: (value->value.b ? YES : NO)];
      break;
    case PT_DOUBLE:
      result = [NSNumber numberWithDouble: value->value.dbl];
      break;
    case PT_UNICODE:
      result = (value->value.lpszW
                ? [NSString stringWithUTF8String: value->value.lpszW]
                : (id) @"");
      break;
    case PT_STRING8:
      result = (value->value.lpszA
                ? [NSString stringWithUTF8String: value->value.lpszA]
                : (id) @"");
      break;
    case PT_SYSTIME:
      result = [NSCalendarDate dateFromFileTime: &(value->value.ft)];
      break;
    case PT_BINARY:
    case PT_SVREID:
		// lpProps->value.bin = *((const struct Binary_r *)data);

      result
        = [NSData dataWithBinary:
                    (const struct Binary_r *) &(value->value.bin)];
      break;
    case PT_CLSID:
      result = [NSData dataWithFlatUID: value->value.lpguid];
      break;
    case PT_MV_SHORT:
      result = [NSArray arrayFromMVShort: &value->value.MVi];
      break;
    case PT_MV_LONG:
      result = [NSArray arrayFromMVLong: &value->value.MVl];
      break;
    case PT_MV_I8:
      result = [NSArray arrayFromMVUI8: &value->value.MVui8];
      break;
    case PT_MV_STRING8:
      result = [NSArray arrayFromMVString: &value->value.MVszA];
      break;
    case PT_MV_UNICODE:
      result = [NSArray arrayFromMVUnicode: &value->value.MVszW];
      break;
    case PT_MV_CLSID:
      result = [NSArray arrayFromMVGuid: &value->value.MVguid];
      break;
    case PT_MV_BINARY:
      result = [NSArray arrayFromMVBinary: &value->value.MVbin];
      break;
    case PT_MV_SYSTIME:
      result = [NSArray arrayFromMVFileTime: &value->value.MVft];
      break;

    default:
// #define	PT_UNSPECIFIED		0x0
// #define	PT_I2			0x2
// #define	PT_CURRENCY		0x6
// #define	PT_APPTIME		0x7
// #define	PT_ERROR		0xa
// #define	PT_OBJECT		0xd
// #define	PT_I8			0x14
// #define	PT_SRESTRICT		0xFD
// #define	PT_ACTIONS		0xFE
      result = [NSNull null];
      abort();
      NSLog (@"%s: object type not handled: %d (0x%.4x)",
             __PRETTY_FUNCTION__, valueType, valueType);
    }

  return result;
}

id
NSObjectFromValuePointer (enum MAPITAGS propTag, const void *data)
{
  struct SPropValue sPropValue;
  id result;

  if (set_SPropValue_proptag(&sPropValue, propTag, data))
    result = NSObjectFromSPropValue (&sPropValue);
  else
    result = nil;

  return result;
}

static uint64_t
_reverseCN (uint64_t cn)
{
  return ((cn & UINT64_C (0x00000000000000ff)) << 56
          | (cn & UINT64_C (0x000000000000ff00)) << 40
          | (cn & UINT64_C (0x0000000000ff0000)) << 24
          | (cn & UINT64_C (0x00000000ff000000)) << 8
          | (cn & UINT64_C (0x000000ff00000000)) >> 8
          | (cn & UINT64_C (0x0000ff0000000000)) >> 24
          | (cn & UINT64_C (0x00ff000000000000)) >> 40
          | (cn & UINT64_C (0xff00000000000000)) >> 56);
}

NSComparisonResult
MAPICNCompare (uint64_t cn1, uint64_t cn2, void *unused)
{
  NSComparisonResult result;

  if (cn1 == cn2)
    result = NSOrderedSame;
  else if (_reverseCN (cn1) < _reverseCN (cn2))
    result = NSOrderedAscending;
  else
    result = NSOrderedDescending;

  return result;
}

NSComparisonResult
MAPIChangeKeyGUIDCompare (id ck1, id ck2, void *unused)
{
  NSUInteger count;
  const unsigned char *ptr1, *ptr2;
  NSComparisonResult result = NSOrderedSame;

  if ([ck1 length] < 16)
    {
      NSLog (@"ck1 has a length < 16");
      abort ();
    }
  if ([ck2 length] < 16)
    {
      NSLog (@"ck2 has a length < 16");
      abort ();
    }

  ptr1 = [ck1 bytes];
  ptr2 = [ck2 bytes];
  for (count = 0; result == NSOrderedSame && count < 16; count++)
    {
      if (*ptr1 < *ptr2)
        result = NSOrderedAscending;
      else if (*ptr1 > *ptr2)
        result = NSOrderedDescending;
      else
        {
          ptr1++;
          ptr2++;
        }
    }

  return result;
}

void
MAPIStoreDumpMessageProperties (NSDictionary *properties)
{
  NSNumber *key;
  NSArray *allKeys;
  NSUInteger count, max;
  NSUInteger keyAsInt;
  id value;

  allKeys = [properties allKeys];
  max = [allKeys count];

  NSLog (@"message properties (%d):", max);

  value = [properties objectForKey: @"recipients"];
   if (value)
    NSLog (@"  recipients: %@", value);

  for (count = 0; count < max; count++)
    {
      key = [allKeys objectAtIndex: count];
      if ([key isKindOfClass: [NSNumber class]])
        {
          keyAsInt = [key intValue];
          value = [properties objectForKey: key];
          NSLog (@"  0x%.4x: %@ (%@)",
                 keyAsInt, value,
	  	 NSStringFromClass ([value class]));
        }
    }
}