pEp_utility.cpp
author Markus Schaber <markus@pep-security.net>
Wed, 01 Feb 2017 21:37:07 +0100
changeset 232 4c6d44bed519
parent 224 b0290a75f7a5
child 251 ded328cde93f
child 258 794f2468ccec
permissions -rw-r--r--
COM-48 Implement timeout, and signal the app to poll inbox faster when waiting
on KeySync queue with a timeout.

First implementation draft.
vb@10
     1
#include "stdafx.h"
vb@33
     2
#include "pEp_utility.h"
vb@33
     3
vb@33
     4
using namespace ATL;
vb@10
     5
vb@10
     6
namespace pEp {
vb@10
     7
    namespace utility {
vb@10
     8
        pEp_identity_cpp::pEp_identity_cpp(const ::pEp_identity *_ident)
vb@10
     9
            : me(false)
vb@10
    10
        {
vb@212
    11
			if (!_ident)
vb@212
    12
				return;
vb@212
    13
vb@10
    14
            if (_ident->address)
vb@10
    15
                address = _ident->address;
vb@10
    16
            if (_ident->fpr)
vb@10
    17
                fpr = _ident->fpr;
vb@10
    18
            if (_ident->user_id)
vb@10
    19
                user_id = _ident->user_id;
vb@10
    20
            if (_ident->username)
vb@10
    21
                username = _ident->username;
markus@172
    22
            comm_type = (pEpComType) _ident->comm_type;
vb@10
    23
            lang = _ident->lang;
vb@217
    24
			flags = (int) _ident->flags;
vb@10
    25
        }
vb@10
    26
markus@172
    27
        pEp_identity_cpp::pEp_identity_cpp(const pEpIdentity *_ident)
vb@10
    28
            : me(false)
vb@10
    29
        {
vb@212
    30
			if (!_ident)
vb@212
    31
				return;
vb@212
    32
vb@212
    33
			if (_ident->Address)
markus@180
    34
                address = utf8_string(_ident->Address);
markus@180
    35
            if (_ident->Fpr)
markus@180
    36
                fpr = utf8_string(_ident->Fpr);
markus@180
    37
            if (_ident->UserId)
markus@180
    38
                user_id = utf8_string(_ident->UserId);
markus@180
    39
            if (_ident->UserName)
markus@180
    40
                username = utf8_string(_ident->UserName);
markus@180
    41
            comm_type = _ident->CommType;
markus@180
    42
            if (_ident->Lang)
markus@180
    43
                lang = utf8_string(_ident->Lang);
vb@217
    44
			flags = (int)_ident->Flags;
vb@10
    45
        }
vb@10
    46
vb@10
    47
        pEp_identity * pEp_identity_cpp::to_pEp_identity()
vb@10
    48
        {
vb@10
    49
            ::pEp_identity *_ident = ::new_identity(this->address.c_str(), this->fpr.c_str(), this->user_id.c_str(), this->username.c_str());
vb@10
    50
            assert(_ident);
vb@10
    51
            if (_ident == NULL)
vb@10
    52
                throw bad_alloc();
vb@10
    53
vb@10
    54
            _ident->comm_type = (::PEP_comm_type) this->comm_type;
vb@10
    55
            _ident->me = this->me;
vb@10
    56
vb@10
    57
            assert(this->lang.size() == 0 || this->lang.size() == 2);
vb@10
    58
vb@10
    59
            if (this->lang.size()) {
vb@10
    60
                _ident->lang[0] = this->lang[0];
vb@10
    61
                _ident->lang[1] = this->lang[1];
vb@10
    62
            }
vb@10
    63
vb@217
    64
			_ident->flags = (identity_flags) this->flags;
vb@217
    65
vb@10
    66
            return _ident;
vb@10
    67
        }
vb@10
    68
markus@172
    69
        pEpIdentity * pEp_identity_cpp::to_pEp_identity_s()
vb@10
    70
        {
markus@172
    71
            pEpIdentity *_ident = (pEpIdentity *) calloc(1, sizeof(pEpIdentity));
vb@10
    72
            assert(_ident);
vb@10
    73
            if (_ident == NULL)
vb@10
    74
                throw bad_alloc();
vb@10
    75
markus@180
    76
            _ident->Address = utf16_bstr(this->address);
markus@180
    77
            _ident->CommType = this->comm_type;
markus@180
    78
            _ident->Fpr = utf16_bstr(this->fpr);
markus@180
    79
            _ident->Lang = utf16_bstr(this->lang);
markus@180
    80
            _ident->UserName = utf16_bstr(this->username);
markus@180
    81
            _ident->UserId = utf16_bstr(this->user_id);
vb@219
    82
			_ident->Flags = (pEpIdentityFlags) this->flags;
vb@10
    83
vb@10
    84
            return _ident;
vb@10
    85
        }
vb@10
    86
markus@172
    87
        void copy_identity(pEpIdentity * ident_s, const pEp_identity * ident)
vb@10
    88
        {
vb@10
    89
            assert(ident_s);
vb@212
    90
			if (!ident_s)
vb@212
    91
				throw invalid_argument("ident_s");
vb@10
    92
markus@172
    93
            ::memset(ident_s, 0, sizeof(pEpIdentity));
vb@10
    94
            if (ident) {
vb@10
    95
                if (ident->address)
markus@180
    96
                    ident_s->Address = utf16_bstr(ident->address);
vb@10
    97
                if (ident->fpr)
markus@180
    98
                    ident_s->Fpr = utf16_bstr(ident->fpr);
vb@10
    99
                if (ident->user_id)
markus@180
   100
                    ident_s->UserId = utf16_bstr(ident->user_id);
vb@10
   101
                if (ident->username)
markus@180
   102
                    ident_s->UserName = utf16_bstr(ident->username);
markus@180
   103
                ident_s->CommType = (pEpComType) ident->comm_type;
vb@10
   104
                if (ident->lang)
markus@180
   105
                    ident_s->Lang = utf16_bstr(ident->lang);
vb@219
   106
				ident_s->Flags = (pEpIdentityFlags) ident->flags;
vb@10
   107
            }
vb@10
   108
        }
vb@10
   109
markus@172
   110
        ::pEp_identity *new_identity(const pEpIdentity * ident)
vb@10
   111
        {
vb@37
   112
            if (ident == NULL)
vb@37
   113
                return NULL;
vb@37
   114
vb@10
   115
            ::pEp_identity *_ident;
vb@10
   116
vb@10
   117
            string _address;
vb@10
   118
            string _fpr;
vb@10
   119
            string _user_id;
vb@10
   120
            string _username;
vb@10
   121
markus@180
   122
            if (ident->Address)
markus@180
   123
                _address = utf8_string(ident->Address);
markus@180
   124
            if (ident->Fpr) {
markus@180
   125
                _fpr = utf8_string(ident->Fpr);
vb@10
   126
                for (auto p = _fpr.begin(); p != _fpr.end(); ++p) {
vb@10
   127
                    if (*p >= 'A' && *p <= 'Z')
vb@10
   128
                        continue;
vb@10
   129
                    if (*p >= '0' && *p <= '9')
vb@10
   130
                        continue;
vb@10
   131
                    throw invalid_argument("invalid hex digits in fingerprint");
vb@10
   132
                }
vb@10
   133
            }
markus@180
   134
            if (ident->UserId)
markus@180
   135
                _user_id = utf8_string(ident->UserId);
markus@180
   136
            if (ident->UserName)
markus@180
   137
                _username = utf8_string(ident->UserName);
vb@10
   138
vb@10
   139
            _ident = ::new_identity(_address.c_str(), _fpr.c_str(), _user_id.c_str(), _username.c_str());
vb@10
   140
            assert(_ident);
vb@10
   141
            if (_ident == NULL)
vb@10
   142
                throw bad_alloc();
vb@10
   143
markus@180
   144
            _ident->comm_type = (PEP_comm_type) ident->CommType;
vb@10
   145
markus@180
   146
            if (ident->Lang) {
markus@180
   147
                string _lang = utf8_string(ident->Lang);
vb@10
   148
                if (_lang.length() != 0) {
vb@10
   149
                    if (_lang.length() != 2) {
vb@10
   150
                        ::free_identity(_ident);
vb@10
   151
                        throw invalid_argument("invalid language code");
vb@10
   152
                    }
vb@10
   153
                    if (_lang[0] < 'a' || _lang[0] > 'z') {
vb@10
   154
                        ::free_identity(_ident);
vb@10
   155
                        throw invalid_argument("invalid language code");
vb@10
   156
                    }
vb@10
   157
                    if (_lang[1] < 'a' || _lang[1] > 'z') {
vb@10
   158
                        ::free_identity(_ident);
vb@10
   159
                        throw invalid_argument("invalid language code");
vb@10
   160
                    }
vb@10
   161
                    _ident->lang[0] = _lang[0];
vb@10
   162
                    _ident->lang[1] = _lang[1];
vb@10
   163
                }
vb@10
   164
            }
vb@10
   165
vb@217
   166
			_ident->flags = (identity_flags_t)ident->Flags;
vb@217
   167
vb@10
   168
            return _ident;
vb@10
   169
        }
vb@33
   170
vb@33
   171
        template< class T2, class T > T2 from_C(T *tl);
vb@33
   172
vb@41
   173
        BSTR bstr(char *s)
vb@33
   174
        {
vb@42
   175
            if (s == NULL)
vb@33
   176
                return _bstr_t(L"").Detach();
vb@42
   177
vb@42
   178
            return utf16_bstr(s);
vb@33
   179
        }
vb@33
   180
markus@172
   181
        template<> Blob *from_C< Blob *, bloblist_t >(bloblist_t *tl)
vb@33
   182
        {
vb@212
   183
			assert(tl);
vb@212
   184
vb@33
   185
            CComSafeArray<BYTE> sa;
vb@212
   186
			if (tl) {
vb@212
   187
				sa.Create(tl->size);
vb@212
   188
				if (tl->size) {
vb@212
   189
					char *data;
vb@212
   190
					SafeArrayAccessData(sa, (void **)&data);
vb@212
   191
					memcpy(data, tl->value, tl->size);
vb@212
   192
					SafeArrayUnaccessData(sa);
vb@212
   193
				}
vb@212
   194
			}
vb@212
   195
			else {
vb@212
   196
				sa.Create((ULONG)0);
vb@212
   197
			}
vb@33
   198
markus@172
   199
            Blob *_blob = new Blob();
vb@33
   200
vb@33
   201
            _blob->value = sa.Detach();
markus@180
   202
            _blob->MimeType = bstr(tl->mime_type);
markus@180
   203
            _blob->Filename = bstr(tl->filename);
vb@33
   204
vb@33
   205
            return _blob;
vb@33
   206
        }
vb@33
   207
vb@33
   208
        template< class T > int length(T *);
vb@33
   209
vb@33
   210
        template< class T2, class T > SAFEARRAY * array_from_C(T *tl)
vb@33
   211
        {
vb@39
   212
            if (tl == NULL)
vb@39
   213
                return newSafeArray<T2>(0);
vb@39
   214
vb@33
   215
            int len = length<T>(tl);
vb@33
   216
vb@33
   217
            LPSAFEARRAY sa = newSafeArray<T2>(len);
vb@33
   218
            LONG lbound, ubound;
vb@33
   219
            SafeArrayGetLBound(sa, 1, &lbound);
vb@33
   220
            SafeArrayGetUBound(sa, 1, &ubound);
vb@33
   221
vb@33
   222
            T *_tl = tl;
vb@38
   223
            for (LONG i = lbound; i <= ubound; _tl = _tl->next, i++)
vb@33
   224
                SafeArrayPutElement(sa, &i, from_C<T2 *, T>(_tl));
vb@33
   225
vb@33
   226
            return sa;
vb@33
   227
        }
vb@33
   228
markus@172
   229
        void clear_identity_s(pEpIdentity& ident)
vb@41
   230
        {
markus@180
   231
            SysFreeString(ident.Address);
markus@180
   232
            SysFreeString(ident.Fpr);
markus@180
   233
            SysFreeString(ident.Lang);
markus@180
   234
            SysFreeString(ident.UserName);
markus@180
   235
            SysFreeString(ident.UserId);
vb@41
   236
markus@172
   237
            memset(&ident, 0, sizeof(pEpIdentity));
vb@41
   238
        }
vb@41
   239
markus@172
   240
        template<> pEpIdentity from_C< pEpIdentity, pEp_identity >(pEp_identity *tl)
vb@33
   241
        {
markus@172
   242
            pEpIdentity _ident;
vb@41
   243
            memset(&_ident, 0, sizeof(_ident));
vb@41
   244
vb@39
   245
            if (tl)
vb@39
   246
                copy_identity(&_ident, tl);
vb@33
   247
            return _ident;
vb@33
   248
        }
vb@33
   249
markus@172
   250
        pEpIdentity identity_s(pEp_identity *ident)
vb@33
   251
        {
markus@172
   252
            return from_C< pEpIdentity, pEp_identity >(ident);
vb@33
   253
        }
vb@33
   254
markus@172
   255
        template<> pEpIdentity *from_C< pEpIdentity *, identity_list >(identity_list *il)
vb@33
   256
        {
markus@172
   257
            pEpIdentity *ident = new pEpIdentity();
markus@172
   258
            memset(ident, 0, sizeof(pEpIdentity));
vb@41
   259
vb@39
   260
            if (il)
vb@39
   261
                copy_identity(ident, il->ident);
vb@33
   262
            return ident;
vb@33
   263
        }
vb@33
   264
markus@171
   265
        template<> StringPair *from_C< StringPair *, stringpair_list_t >(stringpair_list_t * sp)
vb@33
   266
        {
markus@171
   267
            StringPair *fld = new StringPair();
vb@39
   268
            if (sp) {
markus@180
   269
                fld->Name = bstr(sp->value->key);
markus@180
   270
                fld->Value = bstr(sp->value->value);
vb@39
   271
            }
vb@33
   272
            return fld;
vb@33
   273
        }
vb@33
   274
vb@33
   275
        template<> int length<identity_list>(identity_list *tl)
vb@33
   276
        {
vb@33
   277
            return identity_list_length(tl);
vb@33
   278
        }
vb@33
   279
vb@33
   280
        template<> int length<bloblist_t>(bloblist_t *tl)
vb@33
   281
        {
vb@33
   282
            return bloblist_length(tl);
vb@33
   283
        }
vb@33
   284
vb@33
   285
        template<> int length<stringpair_list_t>(stringpair_list_t *tl)
vb@33
   286
        {
vb@33
   287
            return stringpair_list_length(tl);
vb@33
   288
        }
vb@33
   289
markus@172
   290
        void clear_text_message(TextMessage *msg)
vb@41
   291
        {
vb@210
   292
			assert(msg);
vb@210
   293
			if (!msg)
vb@210
   294
				return;
vb@210
   295
markus@180
   296
            SysFreeString(msg->Id);
markus@182
   297
            SysFreeString(msg->ShortMsg);
markus@182
   298
            SysFreeString(msg->LongMsg);
markus@182
   299
            SysFreeString(msg->LongMsgFormatted);
markus@180
   300
            SafeArrayDestroy(msg->Attachments);
markus@180
   301
            clear_identity_s(msg->From);
markus@180
   302
            SafeArrayDestroy(msg->To);
markus@180
   303
            clear_identity_s(msg->RecvBy);
markus@180
   304
            SafeArrayDestroy(msg->Cc);
markus@180
   305
            SafeArrayDestroy(msg->Bcc);
markus@180
   306
            SafeArrayDestroy(msg->ReplyTo);
markus@180
   307
            SafeArrayDestroy(msg->References);
markus@180
   308
            SafeArrayDestroy(msg->Keywords);
markus@180
   309
            SysFreeString(msg->Comments);
markus@180
   310
            SafeArrayDestroy(msg->OptFields);
vb@41
   311
markus@172
   312
            memset(msg, 0, sizeof(TextMessage));
vb@41
   313
        }
vb@41
   314
markus@172
   315
        void text_message_from_C(TextMessage *msg2, const ::message *msg)
vb@33
   316
        {
vb@37
   317
            assert(msg2);
vb@33
   318
            assert(msg);
vb@33
   319
vb@211
   320
			if (!msg2) {
vb@211
   321
				msg2 = (TextMessage *)calloc(1, sizeof(TextMessage));
vb@211
   322
				assert(msg2);
vb@211
   323
				if (!msg2)
vb@211
   324
					throw bad_alloc();
vb@211
   325
			}
vb@211
   326
			else {
vb@211
   327
				clear_text_message(msg2);
vb@211
   328
			}
vb@211
   329
vb@211
   330
			if (!msg)
vb@211
   331
				return;
vb@41
   332
markus@180
   333
            msg2->Dir = (pEpMsgDirection) msg->dir;
markus@180
   334
            msg2->Id = bstr(msg->id);
markus@182
   335
            msg2->ShortMsg = bstr(msg->shortmsg);
markus@182
   336
            msg2->LongMsg = bstr(msg->longmsg);
markus@182
   337
            msg2->LongMsgFormatted = bstr(msg->longmsg_formatted);
markus@180
   338
            msg2->Attachments = array_from_C<Blob, bloblist_t>(msg->attachments);
vb@39
   339
            if (msg->sent)
markus@180
   340
                msg2->Sent = mktime(msg->sent);
vb@39
   341
            if (msg->recv)
markus@180
   342
                msg2->Recv = mktime(msg->recv);
markus@180
   343
            msg2->From = identity_s(msg->from);
markus@180
   344
            msg2->To = array_from_C<pEpIdentity, identity_list>(msg->to);
markus@180
   345
            msg2->RecvBy = identity_s(msg->recv_by);
markus@180
   346
            msg2->Cc = array_from_C<pEpIdentity, identity_list>(msg->cc);
markus@180
   347
            msg2->Bcc = array_from_C<pEpIdentity, identity_list>(msg->bcc);
markus@180
   348
            msg2->ReplyTo = array_from_C<pEpIdentity, identity_list>(msg->reply_to);
markus@180
   349
            msg2->References = string_array(msg->references);
markus@180
   350
            msg2->Keywords = string_array(msg->keywords);
markus@180
   351
            msg2->Comments = bstr(msg->comments);
markus@180
   352
            msg2->OptFields = array_from_C<StringPair, stringpair_list_t>(msg->opt_fields);
vb@33
   353
        }
vb@33
   354
vb@37
   355
        char * str(BSTR s)
vb@33
   356
        {
vb@37
   357
            string str = utf8_string(s);
vb@37
   358
            char *_s = _strdup(str.c_str());
vb@33
   359
            if (_s == NULL)
vb@33
   360
                throw bad_alloc();
vb@33
   361
vb@37
   362
            return _s;
vb@33
   363
        }
vb@33
   364
markus@172
   365
        void clear_blob(Blob& b)
vb@41
   366
        {
markus@180
   367
            SysFreeString(b.Filename);
markus@180
   368
            SysFreeString(b.MimeType);
vb@41
   369
            SafeArrayDestroy(b.value);
markus@172
   370
            memset(&b, 0, sizeof(Blob));
vb@41
   371
        }
vb@41
   372
vb@33
   373
        bloblist_t *bloblist(SAFEARRAY *sa)
vb@33
   374
        {
vb@37
   375
            if (sa == NULL)
vb@37
   376
                return NULL;
vb@37
   377
vb@33
   378
            LONG lbound, ubound;
vb@33
   379
            SafeArrayGetLBound(sa, 1, &lbound);
vb@33
   380
            SafeArrayGetUBound(sa, 1, &ubound);
vb@33
   381
vb@33
   382
            size_t size = ubound - lbound + 1;
vb@33
   383
            if (size <= 0)
vb@33
   384
                return NULL;
vb@33
   385
vb@33
   386
            bloblist_t *bl = new_bloblist(NULL, 0, NULL, NULL);
vb@33
   387
            if (bl == NULL)
vb@33
   388
                throw bad_alloc();
vb@33
   389
vb@33
   390
            bloblist_t *_bl = bl;
vb@33
   391
            for (LONG i = lbound; i <= ubound; i++) {
markus@172
   392
                Blob b;
markus@172
   393
                memset(&b, 0, sizeof(Blob));
vb@33
   394
                SafeArrayGetElement(sa, &i, &b);
vb@33
   395
vb@38
   396
                LONG _lbound, _ubound;
vb@38
   397
                SafeArrayGetLBound(b.value, 1, &_lbound);
vb@38
   398
                SafeArrayGetUBound(b.value, 1, &_ubound);
vb@38
   399
                size_t size = _ubound - _lbound + 1;
vb@38
   400
vb@136
   401
                char *buffer;
vb@136
   402
                if (size) {
vb@224
   403
                    buffer = (char *) malloc(size + 1);
vb@136
   404
                    if (buffer == NULL)
vb@136
   405
                        throw bad_alloc();
vb@33
   406
vb@136
   407
                    char *data;
vb@33
   408
vb@136
   409
                    SafeArrayAccessData(b.value, (void **) &data);
vb@136
   410
                    memcpy(buffer, data, size);
vb@224
   411
					buffer[size] = 0; // safeguard
vb@136
   412
                    SafeArrayUnaccessData(sa);
vb@136
   413
                }
vb@136
   414
                else {
vb@136
   415
                    buffer = _strdup("");
vb@136
   416
                    if (buffer == NULL)
vb@136
   417
                        throw bad_alloc();
vb@33
   418
vb@136
   419
                }
markus@180
   420
                _bl = bloblist_add(_bl, buffer, size, str(b.MimeType), str(b.Filename));
vb@33
   421
                if (_bl == NULL) {
vb@42
   422
                    free(buffer);
vb@41
   423
                    clear_blob(b);
vb@33
   424
                    free_bloblist(bl);
vb@33
   425
                    throw bad_alloc();
vb@33
   426
                }
vb@33
   427
vb@41
   428
                clear_blob(b);
vb@33
   429
            }
vb@33
   430
vb@33
   431
            return bl;
vb@33
   432
        }
vb@33
   433
vb@33
   434
        identity_list *identities(SAFEARRAY * sa)
vb@33
   435
        {
vb@37
   436
            if (sa == NULL)
vb@37
   437
                return NULL;
vb@37
   438
vb@33
   439
            LONG lbound, ubound;
vb@33
   440
            SafeArrayGetLBound(sa, 1, &lbound);
vb@33
   441
            SafeArrayGetUBound(sa, 1, &ubound);
vb@33
   442
vb@33
   443
            size_t size = ubound - lbound + 1;
vb@33
   444
            if (size <= 0)
vb@33
   445
                return NULL;
vb@33
   446
vb@33
   447
            identity_list *il = new_identity_list(NULL);
vb@33
   448
vb@33
   449
            identity_list *_il = il;
vb@33
   450
            for (LONG i = lbound; i <= ubound; i++) {
markus@172
   451
                pEpIdentity s;
markus@172
   452
                memset(&s, 0, sizeof(pEpIdentity));
vb@33
   453
                SafeArrayGetElement(sa, &i, &s);
vb@33
   454
vb@33
   455
                pEp_identity *ident;
vb@33
   456
                try {
vb@33
   457
                    ident = new_identity(&s);
vb@33
   458
                }
vb@33
   459
                catch (bad_alloc&) {
vb@41
   460
                    clear_identity_s(s);
vb@33
   461
                    throw bad_alloc();
vb@33
   462
                }
vb@33
   463
vb@41
   464
                clear_identity_s(s);
vb@33
   465
vb@33
   466
                _il = identity_list_add(_il, ident);
vb@33
   467
                if (_il == NULL) {
vb@33
   468
                    free_identity_list(il);
vb@33
   469
                    throw bad_alloc();
vb@33
   470
                }
vb@33
   471
            }
vb@33
   472
vb@33
   473
            return il;
vb@33
   474
        }
vb@33
   475
markus@171
   476
        stringpair_t *new_stringpair(StringPair *fld)
vb@33
   477
        {
vb@212
   478
			stringpair_t *pair;
vb@212
   479
vb@212
   480
			if (!fld) {
vb@212
   481
				pair = ::new_stringpair(NULL, NULL);
vb@212
   482
			}
vb@212
   483
			else {
vb@212
   484
				pair = ::new_stringpair(str(fld->Name), str(fld->Value));
vb@212
   485
			}
vb@33
   486
            if (pair == NULL)
vb@33
   487
                throw bad_alloc();
vb@33
   488
vb@33
   489
            return pair;
vb@33
   490
        }
vb@33
   491
markus@171
   492
        void clear_opt_field(StringPair& f)
vb@41
   493
        {
markus@180
   494
            SysFreeString(f.Name);
markus@180
   495
            SysFreeString(f.Value);
markus@171
   496
            memset(&f, 0, sizeof(StringPair));
vb@41
   497
        }
vb@41
   498
vb@33
   499
        stringpair_list_t *stringpair_list(SAFEARRAY * sa)
vb@33
   500
        {
vb@37
   501
            if (sa == NULL)
vb@37
   502
                return NULL;
vb@37
   503
vb@33
   504
            LONG lbound, ubound;
vb@33
   505
            SafeArrayGetLBound(sa, 1, &lbound);
vb@33
   506
            SafeArrayGetUBound(sa, 1, &ubound);
vb@33
   507
vb@33
   508
            size_t size = ubound - lbound + 1;
vb@33
   509
            if (size <= 0)
vb@33
   510
                return NULL;
vb@33
   511
vb@33
   512
            stringpair_list_t *il = new_stringpair_list(NULL);
vb@33
   513
vb@33
   514
            stringpair_list_t *_il = il;
vb@33
   515
            for (LONG i = lbound; i <= ubound; i++) {
markus@171
   516
                StringPair s;
markus@171
   517
                memset(&s, 0, sizeof(StringPair));
vb@33
   518
                SafeArrayGetElement(sa, &i, &s);
vb@33
   519
vb@33
   520
                stringpair_t *pair;
vb@33
   521
                try {
vb@33
   522
                    pair = new_stringpair(&s);
vb@33
   523
                }
vb@33
   524
                catch (bad_alloc&) {
vb@41
   525
                    clear_opt_field(s);
vb@33
   526
                    throw bad_alloc();
vb@33
   527
                }
vb@33
   528
vb@41
   529
                clear_opt_field(s);
vb@33
   530
vb@33
   531
                _il = stringpair_list_add(_il, pair);
vb@33
   532
                if (_il == NULL) {
vb@33
   533
                    free_stringpair_list(il);
vb@33
   534
                    throw bad_alloc();
vb@33
   535
                }
vb@33
   536
            }
vb@33
   537
vb@33
   538
            return il;
vb@33
   539
        }
vb@33
   540
        
markus@172
   541
        ::message * text_message_to_C(TextMessage *msg)
vb@33
   542
        {
vb@33
   543
            assert(msg);
vb@212
   544
			if (!msg)
vb@212
   545
				throw invalid_argument("msg");
vb@33
   546
markus@180
   547
            ::message * msg2 = new_message((PEP_msg_direction) msg->Dir);
vb@37
   548
            if (msg2 == NULL)
vb@37
   549
                throw bad_alloc();
vb@33
   550
markus@180
   551
            msg2->id = str(msg->Id);
markus@182
   552
            msg2->shortmsg = str(msg->ShortMsg);
markus@182
   553
            msg2->longmsg = str(msg->LongMsg);
markus@182
   554
            msg2->longmsg_formatted = str(msg->LongMsgFormatted);
markus@180
   555
            msg2->attachments = bloblist(msg->Attachments);
markus@180
   556
            msg2->sent = new_timestamp(msg->Sent);
markus@180
   557
            msg2->recv = new_timestamp(msg->Recv);
markus@180
   558
            msg2->from = new_identity(&msg->From);
markus@180
   559
            msg2->to = identities(msg->To);
markus@180
   560
            msg2->recv_by = new_identity(&msg->RecvBy);
markus@180
   561
            msg2->cc = identities(msg->Cc);
markus@180
   562
            msg2->bcc = identities(msg->Bcc);
markus@180
   563
            msg2->reply_to = identities(msg->ReplyTo);
markus@180
   564
            msg2->references = new_stringlist(msg->References);
markus@180
   565
            msg2->keywords = new_stringlist(msg->Keywords);
markus@180
   566
            msg2->comments = str(msg->Comments);
markus@180
   567
            msg2->opt_fields = stringpair_list(msg->OptFields);
vb@33
   568
vb@33
   569
            return msg2;
vb@33
   570
        }
markus@177
   571
krista@157
   572
		void opt_field_array_from_C(stringpair_list_t* spair_list, LPSAFEARRAY* pair_list_out) {
krista@157
   573
			assert(spair_list);
krista@157
   574
			assert(pair_list_out);
krista@157
   575
			
krista@157
   576
			if (!spair_list)
krista@157
   577
				return;
krista@157
   578
markus@171
   579
			*pair_list_out = array_from_C<StringPair, stringpair_list_t>(spair_list);
krista@157
   580
		}
markus@177
   581
krista@158
   582
		void clear_opt_field_array(LPSAFEARRAY* opt_field_array) {
vb@212
   583
			if (opt_field_array){
vb@212
   584
				SafeArrayDestroy(*opt_field_array);
vb@212
   585
				*opt_field_array = NULL;
vb@212
   586
			}
krista@158
   587
		}
vb@10
   588
    }
vb@10
   589
}