Rename 'utitlity' to 'utility'.
authorDean Looyengoed
Fri, 29 Apr 2016 15:30:10 +0200
changeset 698e47cec9271b
parent 68 8ae7aacc250e
child 70 000c4f33e0f8
Rename 'utitlity' to 'utility'.
pEpCOMServerAdapter.vcxproj
pEpCOMServerAdapter.vcxproj.filters
pEp_utility.cpp
pEp_utitlity.cpp
     1.1 --- a/pEpCOMServerAdapter.vcxproj	Thu Apr 28 22:09:20 2016 +0200
     1.2 +++ b/pEpCOMServerAdapter.vcxproj	Fri Apr 29 15:30:10 2016 +0200
     1.3 @@ -134,7 +134,7 @@
     1.4        <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
     1.5        </PrecompiledHeader>
     1.6      </ClCompile>
     1.7 -    <ClCompile Include="pEp_utitlity.cpp" />
     1.8 +    <ClCompile Include="pEp_utility.cpp" />
     1.9      <ClCompile Include="stdafx.cpp">
    1.10        <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
    1.11        <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
     2.1 --- a/pEpCOMServerAdapter.vcxproj.filters	Thu Apr 28 22:09:20 2016 +0200
     2.2 +++ b/pEpCOMServerAdapter.vcxproj.filters	Fri Apr 29 15:30:10 2016 +0200
     2.3 @@ -37,7 +37,7 @@
     2.4      <ClCompile Include="utf8_helper.cpp">
     2.5        <Filter>Source Files</Filter>
     2.6      </ClCompile>
     2.7 -    <ClCompile Include="pEp_utitlity.cpp">
     2.8 +    <ClCompile Include="pEp_utility.cpp">
     2.9        <Filter>Source Files</Filter>
    2.10      </ClCompile>
    2.11    </ItemGroup>
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/pEp_utility.cpp	Fri Apr 29 15:30:10 2016 +0200
     3.3 @@ -0,0 +1,515 @@
     3.4 +#include "stdafx.h"
     3.5 +#include "pEp_utility.h"
     3.6 +
     3.7 +using namespace ATL;
     3.8 +
     3.9 +namespace pEp {
    3.10 +    namespace utility {
    3.11 +        pEp_identity_cpp::pEp_identity_cpp(const ::pEp_identity *_ident)
    3.12 +            : me(false)
    3.13 +        {
    3.14 +            if (_ident->address)
    3.15 +                address = _ident->address;
    3.16 +            if (_ident->fpr)
    3.17 +                fpr = _ident->fpr;
    3.18 +            if (_ident->user_id)
    3.19 +                user_id = _ident->user_id;
    3.20 +            if (_ident->username)
    3.21 +                username = _ident->username;
    3.22 +            comm_type = (pEp_comm_type) _ident->comm_type;
    3.23 +            lang = _ident->lang;
    3.24 +        }
    3.25 +
    3.26 +        pEp_identity_cpp::pEp_identity_cpp(const pEp_identity_s *_ident)
    3.27 +            : me(false)
    3.28 +        {
    3.29 +            if (_ident->address)
    3.30 +                address = utf8_string(_ident->address);
    3.31 +            if (_ident->fpr)
    3.32 +                fpr = utf8_string(_ident->fpr);
    3.33 +            if (_ident->user_id)
    3.34 +                user_id = utf8_string(_ident->user_id);
    3.35 +            if (_ident->username)
    3.36 +                username = utf8_string(_ident->username);
    3.37 +            comm_type = _ident->comm_type;
    3.38 +            if (_ident->lang)
    3.39 +                lang = utf8_string(_ident->lang);
    3.40 +        }
    3.41 +
    3.42 +        pEp_identity * pEp_identity_cpp::to_pEp_identity()
    3.43 +        {
    3.44 +            ::pEp_identity *_ident = ::new_identity(this->address.c_str(), this->fpr.c_str(), this->user_id.c_str(), this->username.c_str());
    3.45 +            assert(_ident);
    3.46 +            if (_ident == NULL)
    3.47 +                throw bad_alloc();
    3.48 +
    3.49 +            _ident->comm_type = (::PEP_comm_type) this->comm_type;
    3.50 +            _ident->me = this->me;
    3.51 +
    3.52 +            assert(this->lang.size() == 0 || this->lang.size() == 2);
    3.53 +
    3.54 +            if (this->lang.size()) {
    3.55 +                _ident->lang[0] = this->lang[0];
    3.56 +                _ident->lang[1] = this->lang[1];
    3.57 +            }
    3.58 +
    3.59 +            return _ident;
    3.60 +        }
    3.61 +
    3.62 +        pEp_identity_s * pEp_identity_cpp::to_pEp_identity_s()
    3.63 +        {
    3.64 +            pEp_identity_s *_ident = (pEp_identity_s *) calloc(1, sizeof(pEp_identity_s));
    3.65 +            assert(_ident);
    3.66 +            if (_ident == NULL)
    3.67 +                throw bad_alloc();
    3.68 +
    3.69 +            _ident->address = utf16_bstr(this->address);
    3.70 +            _ident->comm_type = this->comm_type;
    3.71 +            _ident->fpr = utf16_bstr(this->fpr);
    3.72 +            _ident->lang = utf16_bstr(this->lang);
    3.73 +            _ident->username = utf16_bstr(this->username);
    3.74 +            _ident->user_id = utf16_bstr(this->user_id);
    3.75 +
    3.76 +            return _ident;
    3.77 +        }
    3.78 +
    3.79 +        void copy_identity(pEp_identity_s * ident_s, const pEp_identity * ident)
    3.80 +        {
    3.81 +            assert(ident_s);
    3.82 +
    3.83 +            ::memset(ident_s, 0, sizeof(pEp_identity_s));
    3.84 +            if (ident) {
    3.85 +                if (ident->address)
    3.86 +                    ident_s->address = utf16_bstr(ident->address);
    3.87 +                if (ident->fpr)
    3.88 +                    ident_s->fpr = utf16_bstr(ident->fpr);
    3.89 +                if (ident->user_id)
    3.90 +                    ident_s->user_id = utf16_bstr(ident->user_id);
    3.91 +                if (ident->username)
    3.92 +                    ident_s->username = utf16_bstr(ident->username);
    3.93 +                ident_s->comm_type = (pEp_comm_type) ident->comm_type;
    3.94 +                if (ident->lang)
    3.95 +                    ident_s->lang = utf16_bstr(ident->lang);
    3.96 +            }
    3.97 +        }
    3.98 +
    3.99 +        ::pEp_identity *new_identity(const pEp_identity_s * ident)
   3.100 +        {
   3.101 +            if (ident == NULL)
   3.102 +                return NULL;
   3.103 +
   3.104 +            ::pEp_identity *_ident;
   3.105 +
   3.106 +            string _address;
   3.107 +            string _fpr;
   3.108 +            string _user_id;
   3.109 +            string _username;
   3.110 +
   3.111 +            if (ident->address)
   3.112 +                _address = utf8_string(ident->address);
   3.113 +            if (ident->fpr) {
   3.114 +                _fpr = utf8_string(ident->fpr);
   3.115 +                for (auto p = _fpr.begin(); p != _fpr.end(); ++p) {
   3.116 +                    if (*p >= 'A' && *p <= 'Z')
   3.117 +                        continue;
   3.118 +                    if (*p >= '0' && *p <= '9')
   3.119 +                        continue;
   3.120 +                    throw invalid_argument("invalid hex digits in fingerprint");
   3.121 +                }
   3.122 +            }
   3.123 +            if (ident->user_id)
   3.124 +                _user_id = utf8_string(ident->user_id);
   3.125 +            if (ident->username)
   3.126 +                _username = utf8_string(ident->username);
   3.127 +
   3.128 +            _ident = ::new_identity(_address.c_str(), _fpr.c_str(), _user_id.c_str(), _username.c_str());
   3.129 +            assert(_ident);
   3.130 +            if (_ident == NULL)
   3.131 +                throw bad_alloc();
   3.132 +
   3.133 +            _ident->comm_type = (PEP_comm_type) ident->comm_type;
   3.134 +
   3.135 +            if (ident->lang) {
   3.136 +                string _lang = utf8_string(ident->lang);
   3.137 +                if (_lang.length() != 0) {
   3.138 +                    if (_lang.length() != 2) {
   3.139 +                        ::free_identity(_ident);
   3.140 +                        throw invalid_argument("invalid language code");
   3.141 +                    }
   3.142 +                    if (_lang[0] < 'a' || _lang[0] > 'z') {
   3.143 +                        ::free_identity(_ident);
   3.144 +                        throw invalid_argument("invalid language code");
   3.145 +                    }
   3.146 +                    if (_lang[1] < 'a' || _lang[1] > 'z') {
   3.147 +                        ::free_identity(_ident);
   3.148 +                        throw invalid_argument("invalid language code");
   3.149 +                    }
   3.150 +                    _ident->lang[0] = _lang[0];
   3.151 +                    _ident->lang[1] = _lang[1];
   3.152 +                }
   3.153 +            }
   3.154 +
   3.155 +            return _ident;
   3.156 +        }
   3.157 +
   3.158 +        template< class T2, class T > T2 from_C(T *tl);
   3.159 +
   3.160 +        BSTR bstr(char *s)
   3.161 +        {
   3.162 +            if (s == NULL)
   3.163 +                return _bstr_t(L"").Detach();
   3.164 +
   3.165 +            return utf16_bstr(s);
   3.166 +        }
   3.167 +
   3.168 +        template<> blob *from_C< blob *, bloblist_t >(bloblist_t *tl)
   3.169 +        {
   3.170 +            CComSafeArray<BYTE> sa;
   3.171 +            sa.Create(tl->size);
   3.172 +
   3.173 +            char *data;
   3.174 +            SafeArrayAccessData(sa, (void **) &data);
   3.175 +            memcpy(data, tl->value, tl->size);
   3.176 +            SafeArrayUnaccessData(sa);
   3.177 +
   3.178 +            blob *_blob = new blob();
   3.179 +
   3.180 +            _blob->value = sa.Detach();
   3.181 +            _blob->mime_type = bstr(tl->mime_type);
   3.182 +            _blob->filename = bstr(tl->filename);
   3.183 +
   3.184 +            return _blob;
   3.185 +        }
   3.186 +
   3.187 +        template< class T > int length(T *);
   3.188 +
   3.189 +        template< class T2, class T > SAFEARRAY * array_from_C(T *tl)
   3.190 +        {
   3.191 +            if (tl == NULL)
   3.192 +                return newSafeArray<T2>(0);
   3.193 +
   3.194 +            int len = length<T>(tl);
   3.195 +
   3.196 +            LPSAFEARRAY sa = newSafeArray<T2>(len);
   3.197 +            LONG lbound, ubound;
   3.198 +            SafeArrayGetLBound(sa, 1, &lbound);
   3.199 +            SafeArrayGetUBound(sa, 1, &ubound);
   3.200 +
   3.201 +            T *_tl = tl;
   3.202 +            for (LONG i = lbound; i <= ubound; _tl = _tl->next, i++)
   3.203 +                SafeArrayPutElement(sa, &i, from_C<T2 *, T>(_tl));
   3.204 +
   3.205 +            return sa;
   3.206 +        }
   3.207 +
   3.208 +        void clear_identity_s(pEp_identity_s& ident)
   3.209 +        {
   3.210 +            SysFreeString(ident.address);
   3.211 +            SysFreeString(ident.fpr);
   3.212 +            SysFreeString(ident.lang);
   3.213 +            SysFreeString(ident.username);
   3.214 +            SysFreeString(ident.user_id);
   3.215 +
   3.216 +            memset(&ident, 0, sizeof(pEp_identity_s));
   3.217 +        }
   3.218 +
   3.219 +        template<> pEp_identity_s from_C< pEp_identity_s, pEp_identity >(pEp_identity *tl)
   3.220 +        {
   3.221 +            pEp_identity_s _ident;
   3.222 +            memset(&_ident, 0, sizeof(_ident));
   3.223 +
   3.224 +            if (tl)
   3.225 +                copy_identity(&_ident, tl);
   3.226 +            return _ident;
   3.227 +        }
   3.228 +
   3.229 +        pEp_identity_s identity_s(pEp_identity *ident)
   3.230 +        {
   3.231 +            return from_C< pEp_identity_s, pEp_identity >(ident);
   3.232 +        }
   3.233 +
   3.234 +        template<> pEp_identity_s *from_C< pEp_identity_s *, identity_list >(identity_list *il)
   3.235 +        {
   3.236 +            pEp_identity_s *ident = new pEp_identity_s();
   3.237 +            memset(ident, 0, sizeof(pEp_identity_s));
   3.238 +
   3.239 +            if (il)
   3.240 +                copy_identity(ident, il->ident);
   3.241 +            return ident;
   3.242 +        }
   3.243 +
   3.244 +        template<> opt_field *from_C< opt_field *, stringpair_list_t >(stringpair_list_t * sp)
   3.245 +        {
   3.246 +            opt_field *fld = new opt_field();
   3.247 +            if (sp) {
   3.248 +                fld->name = bstr(sp->value->key);
   3.249 +                fld->value = bstr(sp->value->value);
   3.250 +            }
   3.251 +            return fld;
   3.252 +        }
   3.253 +
   3.254 +        template<> int length<identity_list>(identity_list *tl)
   3.255 +        {
   3.256 +            return identity_list_length(tl);
   3.257 +        }
   3.258 +
   3.259 +        template<> int length<bloblist_t>(bloblist_t *tl)
   3.260 +        {
   3.261 +            return bloblist_length(tl);
   3.262 +        }
   3.263 +
   3.264 +        template<> int length<stringpair_list_t>(stringpair_list_t *tl)
   3.265 +        {
   3.266 +            return stringpair_list_length(tl);
   3.267 +        }
   3.268 +
   3.269 +        void clear_text_message(text_message *msg)
   3.270 +        {
   3.271 +            SysFreeString(msg->id);
   3.272 +            SysFreeString(msg->shortmsg);
   3.273 +            SysFreeString(msg->longmsg);
   3.274 +            SysFreeString(msg->longmsg_formatted);
   3.275 +            SafeArrayDestroy(msg->attachments);
   3.276 +            clear_identity_s(msg->from);
   3.277 +            SafeArrayDestroy(msg->to);
   3.278 +            clear_identity_s(msg->recv_by);
   3.279 +            SafeArrayDestroy(msg->cc);
   3.280 +            SafeArrayDestroy(msg->bcc);
   3.281 +            SafeArrayDestroy(msg->reply_to);
   3.282 +            SafeArrayDestroy(msg->references);
   3.283 +            SafeArrayDestroy(msg->keywords);
   3.284 +            SysFreeString(msg->comments);
   3.285 +            SafeArrayDestroy(msg->opt_fields);
   3.286 +
   3.287 +            memset(msg, 0, sizeof(text_message));
   3.288 +        }
   3.289 +
   3.290 +        void text_message_from_C(text_message *msg2, ::message *msg)
   3.291 +        {
   3.292 +            assert(msg2);
   3.293 +            assert(msg);
   3.294 +
   3.295 +            clear_text_message(msg2);
   3.296 +
   3.297 +            msg2->dir = (pEp_msg_direction) msg->dir;
   3.298 +            msg2->id = bstr(msg->id);
   3.299 +            msg2->shortmsg = bstr(msg->shortmsg);
   3.300 +            msg2->longmsg = bstr(msg->longmsg);
   3.301 +            msg2->longmsg_formatted = bstr(msg->longmsg_formatted);
   3.302 +            msg2->attachments = array_from_C<blob, bloblist_t>(msg->attachments);
   3.303 +            if (msg->sent)
   3.304 +                msg2->sent = mktime(msg->sent);
   3.305 +            if (msg->recv)
   3.306 +                msg2->recv = mktime(msg->recv);
   3.307 +            msg2->from = identity_s(msg->from);
   3.308 +            msg2->to = array_from_C<pEp_identity_s, identity_list>(msg->to);
   3.309 +            msg2->recv_by = identity_s(msg->recv_by);
   3.310 +            msg2->cc = array_from_C<pEp_identity_s, identity_list>(msg->cc);
   3.311 +            msg2->bcc = array_from_C<pEp_identity_s, identity_list>(msg->bcc);
   3.312 +            msg2->reply_to = array_from_C<pEp_identity_s, identity_list>(msg->reply_to);
   3.313 +            msg2->references = string_array(msg->references);
   3.314 +            msg2->keywords = string_array(msg->keywords);
   3.315 +            msg2->comments = bstr(msg->comments);
   3.316 +            msg2->opt_fields = array_from_C<opt_field, stringpair_list_t>(msg->opt_fields);
   3.317 +        }
   3.318 +
   3.319 +        char * str(BSTR s)
   3.320 +        {
   3.321 +            string str = utf8_string(s);
   3.322 +            char *_s = _strdup(str.c_str());
   3.323 +            if (_s == NULL)
   3.324 +                throw bad_alloc();
   3.325 +
   3.326 +            return _s;
   3.327 +        }
   3.328 +
   3.329 +        void clear_blob(blob& b)
   3.330 +        {
   3.331 +            SysFreeString(b.filename);
   3.332 +            SysFreeString(b.mime_type);
   3.333 +            SafeArrayDestroy(b.value);
   3.334 +            memset(&b, 0, sizeof(blob));
   3.335 +        }
   3.336 +
   3.337 +        bloblist_t *bloblist(SAFEARRAY *sa)
   3.338 +        {
   3.339 +            if (sa == NULL)
   3.340 +                return NULL;
   3.341 +
   3.342 +            LONG lbound, ubound;
   3.343 +            SafeArrayGetLBound(sa, 1, &lbound);
   3.344 +            SafeArrayGetUBound(sa, 1, &ubound);
   3.345 +
   3.346 +            size_t size = ubound - lbound + 1;
   3.347 +            if (size <= 0)
   3.348 +                return NULL;
   3.349 +
   3.350 +            bloblist_t *bl = new_bloblist(NULL, 0, NULL, NULL);
   3.351 +            if (bl == NULL)
   3.352 +                throw bad_alloc();
   3.353 +
   3.354 +            bloblist_t *_bl = bl;
   3.355 +            for (LONG i = lbound; i <= ubound; i++) {
   3.356 +                blob b;
   3.357 +                memset(&b, 0, sizeof(blob));
   3.358 +                SafeArrayGetElement(sa, &i, &b);
   3.359 +
   3.360 +                LONG _lbound, _ubound;
   3.361 +                SafeArrayGetLBound(b.value, 1, &_lbound);
   3.362 +                SafeArrayGetUBound(b.value, 1, &_ubound);
   3.363 +                size_t size = _ubound - _lbound + 1;
   3.364 +
   3.365 +                char *buffer = (char *) malloc(size);
   3.366 +                if (buffer == NULL)
   3.367 +                    throw bad_alloc();
   3.368 +
   3.369 +                char *data;
   3.370 +
   3.371 +                SafeArrayAccessData(b.value, (void **) &data);
   3.372 +                memcpy(buffer, data, size);
   3.373 +                SafeArrayUnaccessData(sa);
   3.374 +
   3.375 +                _bl = bloblist_add(_bl, buffer, size, str(b.mime_type), str(b.filename));
   3.376 +                if (_bl == NULL) {
   3.377 +                    free(buffer);
   3.378 +                    clear_blob(b);
   3.379 +                    free_bloblist(bl);
   3.380 +                    throw bad_alloc();
   3.381 +                }
   3.382 +
   3.383 +                clear_blob(b);
   3.384 +            }
   3.385 +
   3.386 +            return bl;
   3.387 +        }
   3.388 +
   3.389 +        identity_list *identities(SAFEARRAY * sa)
   3.390 +        {
   3.391 +            if (sa == NULL)
   3.392 +                return NULL;
   3.393 +
   3.394 +            LONG lbound, ubound;
   3.395 +            SafeArrayGetLBound(sa, 1, &lbound);
   3.396 +            SafeArrayGetUBound(sa, 1, &ubound);
   3.397 +
   3.398 +            size_t size = ubound - lbound + 1;
   3.399 +            if (size <= 0)
   3.400 +                return NULL;
   3.401 +
   3.402 +            identity_list *il = new_identity_list(NULL);
   3.403 +
   3.404 +            identity_list *_il = il;
   3.405 +            for (LONG i = lbound; i <= ubound; i++) {
   3.406 +                pEp_identity_s s;
   3.407 +                memset(&s, 0, sizeof(pEp_identity_s));
   3.408 +                SafeArrayGetElement(sa, &i, &s);
   3.409 +
   3.410 +                pEp_identity *ident;
   3.411 +                try {
   3.412 +                    ident = new_identity(&s);
   3.413 +                }
   3.414 +                catch (bad_alloc&) {
   3.415 +                    clear_identity_s(s);
   3.416 +                    throw bad_alloc();
   3.417 +                }
   3.418 +
   3.419 +                clear_identity_s(s);
   3.420 +
   3.421 +                _il = identity_list_add(_il, ident);
   3.422 +                if (_il == NULL) {
   3.423 +                    free_identity_list(il);
   3.424 +                    throw bad_alloc();
   3.425 +                }
   3.426 +            }
   3.427 +
   3.428 +            return il;
   3.429 +        }
   3.430 +
   3.431 +        stringpair_t *new_stringpair(opt_field *fld)
   3.432 +        {
   3.433 +            stringpair_t *pair = ::new_stringpair(str(fld->name), str(fld->value));
   3.434 +            if (pair == NULL)
   3.435 +                throw bad_alloc();
   3.436 +
   3.437 +            return pair;
   3.438 +        }
   3.439 +
   3.440 +        void clear_opt_field(opt_field& f)
   3.441 +        {
   3.442 +            SysFreeString(f.name);
   3.443 +            SysFreeString(f.value);
   3.444 +            memset(&f, 0, sizeof(opt_field));
   3.445 +        }
   3.446 +
   3.447 +        stringpair_list_t *stringpair_list(SAFEARRAY * sa)
   3.448 +        {
   3.449 +            if (sa == NULL)
   3.450 +                return NULL;
   3.451 +
   3.452 +            LONG lbound, ubound;
   3.453 +            SafeArrayGetLBound(sa, 1, &lbound);
   3.454 +            SafeArrayGetUBound(sa, 1, &ubound);
   3.455 +
   3.456 +            size_t size = ubound - lbound + 1;
   3.457 +            if (size <= 0)
   3.458 +                return NULL;
   3.459 +
   3.460 +            stringpair_list_t *il = new_stringpair_list(NULL);
   3.461 +
   3.462 +            stringpair_list_t *_il = il;
   3.463 +            for (LONG i = lbound; i <= ubound; i++) {
   3.464 +                opt_field s;
   3.465 +                memset(&s, 0, sizeof(opt_field));
   3.466 +                SafeArrayGetElement(sa, &i, &s);
   3.467 +
   3.468 +                stringpair_t *pair;
   3.469 +                try {
   3.470 +                    pair = new_stringpair(&s);
   3.471 +                }
   3.472 +                catch (bad_alloc&) {
   3.473 +                    clear_opt_field(s);
   3.474 +                    throw bad_alloc();
   3.475 +                }
   3.476 +
   3.477 +                clear_opt_field(s);
   3.478 +
   3.479 +                _il = stringpair_list_add(_il, pair);
   3.480 +                if (_il == NULL) {
   3.481 +                    free_stringpair_list(il);
   3.482 +                    throw bad_alloc();
   3.483 +                }
   3.484 +            }
   3.485 +
   3.486 +            return il;
   3.487 +        }
   3.488 +        
   3.489 +        ::message * text_message_to_C(text_message *msg)
   3.490 +        {
   3.491 +            assert(msg);
   3.492 +
   3.493 +            ::message * msg2 = new_message((PEP_msg_direction) msg->dir);
   3.494 +            if (msg2 == NULL)
   3.495 +                throw bad_alloc();
   3.496 +
   3.497 +            msg2->id = str(msg->id);
   3.498 +            msg2->shortmsg = str(msg->shortmsg);
   3.499 +            msg2->longmsg = str(msg->longmsg);
   3.500 +            msg2->longmsg_formatted = str(msg->longmsg_formatted);
   3.501 +            msg2->attachments = bloblist(msg->attachments);
   3.502 +            msg2->sent = new_timestamp(msg->sent);
   3.503 +            msg2->recv = new_timestamp(msg->recv);
   3.504 +            msg2->from = new_identity(&msg->from);
   3.505 +            msg2->to = identities(msg->to);
   3.506 +            msg2->recv_by = new_identity(&msg->recv_by);
   3.507 +            msg2->cc = identities(msg->cc);
   3.508 +            msg2->bcc = identities(msg->bcc);
   3.509 +            msg2->reply_to = identities(msg->reply_to);
   3.510 +            msg2->references = new_stringlist(msg->references);
   3.511 +            msg2->keywords = new_stringlist(msg->keywords);
   3.512 +            msg2->comments = str(msg->comments);
   3.513 +            msg2->opt_fields = stringpair_list(msg->opt_fields);
   3.514 +
   3.515 +            return msg2;
   3.516 +        }
   3.517 +    }
   3.518 +}
     4.1 --- a/pEp_utitlity.cpp	Thu Apr 28 22:09:20 2016 +0200
     4.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.3 @@ -1,515 +0,0 @@
     4.4 -#include "stdafx.h"
     4.5 -#include "pEp_utility.h"
     4.6 -
     4.7 -using namespace ATL;
     4.8 -
     4.9 -namespace pEp {
    4.10 -    namespace utility {
    4.11 -        pEp_identity_cpp::pEp_identity_cpp(const ::pEp_identity *_ident)
    4.12 -            : me(false)
    4.13 -        {
    4.14 -            if (_ident->address)
    4.15 -                address = _ident->address;
    4.16 -            if (_ident->fpr)
    4.17 -                fpr = _ident->fpr;
    4.18 -            if (_ident->user_id)
    4.19 -                user_id = _ident->user_id;
    4.20 -            if (_ident->username)
    4.21 -                username = _ident->username;
    4.22 -            comm_type = (pEp_comm_type) _ident->comm_type;
    4.23 -            lang = _ident->lang;
    4.24 -        }
    4.25 -
    4.26 -        pEp_identity_cpp::pEp_identity_cpp(const pEp_identity_s *_ident)
    4.27 -            : me(false)
    4.28 -        {
    4.29 -            if (_ident->address)
    4.30 -                address = utf8_string(_ident->address);
    4.31 -            if (_ident->fpr)
    4.32 -                fpr = utf8_string(_ident->fpr);
    4.33 -            if (_ident->user_id)
    4.34 -                user_id = utf8_string(_ident->user_id);
    4.35 -            if (_ident->username)
    4.36 -                username = utf8_string(_ident->username);
    4.37 -            comm_type = _ident->comm_type;
    4.38 -            if (_ident->lang)
    4.39 -                lang = utf8_string(_ident->lang);
    4.40 -        }
    4.41 -
    4.42 -        pEp_identity * pEp_identity_cpp::to_pEp_identity()
    4.43 -        {
    4.44 -            ::pEp_identity *_ident = ::new_identity(this->address.c_str(), this->fpr.c_str(), this->user_id.c_str(), this->username.c_str());
    4.45 -            assert(_ident);
    4.46 -            if (_ident == NULL)
    4.47 -                throw bad_alloc();
    4.48 -
    4.49 -            _ident->comm_type = (::PEP_comm_type) this->comm_type;
    4.50 -            _ident->me = this->me;
    4.51 -
    4.52 -            assert(this->lang.size() == 0 || this->lang.size() == 2);
    4.53 -
    4.54 -            if (this->lang.size()) {
    4.55 -                _ident->lang[0] = this->lang[0];
    4.56 -                _ident->lang[1] = this->lang[1];
    4.57 -            }
    4.58 -
    4.59 -            return _ident;
    4.60 -        }
    4.61 -
    4.62 -        pEp_identity_s * pEp_identity_cpp::to_pEp_identity_s()
    4.63 -        {
    4.64 -            pEp_identity_s *_ident = (pEp_identity_s *) calloc(1, sizeof(pEp_identity_s));
    4.65 -            assert(_ident);
    4.66 -            if (_ident == NULL)
    4.67 -                throw bad_alloc();
    4.68 -
    4.69 -            _ident->address = utf16_bstr(this->address);
    4.70 -            _ident->comm_type = this->comm_type;
    4.71 -            _ident->fpr = utf16_bstr(this->fpr);
    4.72 -            _ident->lang = utf16_bstr(this->lang);
    4.73 -            _ident->username = utf16_bstr(this->username);
    4.74 -            _ident->user_id = utf16_bstr(this->user_id);
    4.75 -
    4.76 -            return _ident;
    4.77 -        }
    4.78 -
    4.79 -        void copy_identity(pEp_identity_s * ident_s, const pEp_identity * ident)
    4.80 -        {
    4.81 -            assert(ident_s);
    4.82 -
    4.83 -            ::memset(ident_s, 0, sizeof(pEp_identity_s));
    4.84 -            if (ident) {
    4.85 -                if (ident->address)
    4.86 -                    ident_s->address = utf16_bstr(ident->address);
    4.87 -                if (ident->fpr)
    4.88 -                    ident_s->fpr = utf16_bstr(ident->fpr);
    4.89 -                if (ident->user_id)
    4.90 -                    ident_s->user_id = utf16_bstr(ident->user_id);
    4.91 -                if (ident->username)
    4.92 -                    ident_s->username = utf16_bstr(ident->username);
    4.93 -                ident_s->comm_type = (pEp_comm_type) ident->comm_type;
    4.94 -                if (ident->lang)
    4.95 -                    ident_s->lang = utf16_bstr(ident->lang);
    4.96 -            }
    4.97 -        }
    4.98 -
    4.99 -        ::pEp_identity *new_identity(const pEp_identity_s * ident)
   4.100 -        {
   4.101 -            if (ident == NULL)
   4.102 -                return NULL;
   4.103 -
   4.104 -            ::pEp_identity *_ident;
   4.105 -
   4.106 -            string _address;
   4.107 -            string _fpr;
   4.108 -            string _user_id;
   4.109 -            string _username;
   4.110 -
   4.111 -            if (ident->address)
   4.112 -                _address = utf8_string(ident->address);
   4.113 -            if (ident->fpr) {
   4.114 -                _fpr = utf8_string(ident->fpr);
   4.115 -                for (auto p = _fpr.begin(); p != _fpr.end(); ++p) {
   4.116 -                    if (*p >= 'A' && *p <= 'Z')
   4.117 -                        continue;
   4.118 -                    if (*p >= '0' && *p <= '9')
   4.119 -                        continue;
   4.120 -                    throw invalid_argument("invalid hex digits in fingerprint");
   4.121 -                }
   4.122 -            }
   4.123 -            if (ident->user_id)
   4.124 -                _user_id = utf8_string(ident->user_id);
   4.125 -            if (ident->username)
   4.126 -                _username = utf8_string(ident->username);
   4.127 -
   4.128 -            _ident = ::new_identity(_address.c_str(), _fpr.c_str(), _user_id.c_str(), _username.c_str());
   4.129 -            assert(_ident);
   4.130 -            if (_ident == NULL)
   4.131 -                throw bad_alloc();
   4.132 -
   4.133 -            _ident->comm_type = (PEP_comm_type) ident->comm_type;
   4.134 -
   4.135 -            if (ident->lang) {
   4.136 -                string _lang = utf8_string(ident->lang);
   4.137 -                if (_lang.length() != 0) {
   4.138 -                    if (_lang.length() != 2) {
   4.139 -                        ::free_identity(_ident);
   4.140 -                        throw invalid_argument("invalid language code");
   4.141 -                    }
   4.142 -                    if (_lang[0] < 'a' || _lang[0] > 'z') {
   4.143 -                        ::free_identity(_ident);
   4.144 -                        throw invalid_argument("invalid language code");
   4.145 -                    }
   4.146 -                    if (_lang[1] < 'a' || _lang[1] > 'z') {
   4.147 -                        ::free_identity(_ident);
   4.148 -                        throw invalid_argument("invalid language code");
   4.149 -                    }
   4.150 -                    _ident->lang[0] = _lang[0];
   4.151 -                    _ident->lang[1] = _lang[1];
   4.152 -                }
   4.153 -            }
   4.154 -
   4.155 -            return _ident;
   4.156 -        }
   4.157 -
   4.158 -        template< class T2, class T > T2 from_C(T *tl);
   4.159 -
   4.160 -        BSTR bstr(char *s)
   4.161 -        {
   4.162 -            if (s == NULL)
   4.163 -                return _bstr_t(L"").Detach();
   4.164 -
   4.165 -            return utf16_bstr(s);
   4.166 -        }
   4.167 -
   4.168 -        template<> blob *from_C< blob *, bloblist_t >(bloblist_t *tl)
   4.169 -        {
   4.170 -            CComSafeArray<BYTE> sa;
   4.171 -            sa.Create(tl->size);
   4.172 -
   4.173 -            char *data;
   4.174 -            SafeArrayAccessData(sa, (void **) &data);
   4.175 -            memcpy(data, tl->value, tl->size);
   4.176 -            SafeArrayUnaccessData(sa);
   4.177 -
   4.178 -            blob *_blob = new blob();
   4.179 -
   4.180 -            _blob->value = sa.Detach();
   4.181 -            _blob->mime_type = bstr(tl->mime_type);
   4.182 -            _blob->filename = bstr(tl->filename);
   4.183 -
   4.184 -            return _blob;
   4.185 -        }
   4.186 -
   4.187 -        template< class T > int length(T *);
   4.188 -
   4.189 -        template< class T2, class T > SAFEARRAY * array_from_C(T *tl)
   4.190 -        {
   4.191 -            if (tl == NULL)
   4.192 -                return newSafeArray<T2>(0);
   4.193 -
   4.194 -            int len = length<T>(tl);
   4.195 -
   4.196 -            LPSAFEARRAY sa = newSafeArray<T2>(len);
   4.197 -            LONG lbound, ubound;
   4.198 -            SafeArrayGetLBound(sa, 1, &lbound);
   4.199 -            SafeArrayGetUBound(sa, 1, &ubound);
   4.200 -
   4.201 -            T *_tl = tl;
   4.202 -            for (LONG i = lbound; i <= ubound; _tl = _tl->next, i++)
   4.203 -                SafeArrayPutElement(sa, &i, from_C<T2 *, T>(_tl));
   4.204 -
   4.205 -            return sa;
   4.206 -        }
   4.207 -
   4.208 -        void clear_identity_s(pEp_identity_s& ident)
   4.209 -        {
   4.210 -            SysFreeString(ident.address);
   4.211 -            SysFreeString(ident.fpr);
   4.212 -            SysFreeString(ident.lang);
   4.213 -            SysFreeString(ident.username);
   4.214 -            SysFreeString(ident.user_id);
   4.215 -
   4.216 -            memset(&ident, 0, sizeof(pEp_identity_s));
   4.217 -        }
   4.218 -
   4.219 -        template<> pEp_identity_s from_C< pEp_identity_s, pEp_identity >(pEp_identity *tl)
   4.220 -        {
   4.221 -            pEp_identity_s _ident;
   4.222 -            memset(&_ident, 0, sizeof(_ident));
   4.223 -
   4.224 -            if (tl)
   4.225 -                copy_identity(&_ident, tl);
   4.226 -            return _ident;
   4.227 -        }
   4.228 -
   4.229 -        pEp_identity_s identity_s(pEp_identity *ident)
   4.230 -        {
   4.231 -            return from_C< pEp_identity_s, pEp_identity >(ident);
   4.232 -        }
   4.233 -
   4.234 -        template<> pEp_identity_s *from_C< pEp_identity_s *, identity_list >(identity_list *il)
   4.235 -        {
   4.236 -            pEp_identity_s *ident = new pEp_identity_s();
   4.237 -            memset(ident, 0, sizeof(pEp_identity_s));
   4.238 -
   4.239 -            if (il)
   4.240 -                copy_identity(ident, il->ident);
   4.241 -            return ident;
   4.242 -        }
   4.243 -
   4.244 -        template<> opt_field *from_C< opt_field *, stringpair_list_t >(stringpair_list_t * sp)
   4.245 -        {
   4.246 -            opt_field *fld = new opt_field();
   4.247 -            if (sp) {
   4.248 -                fld->name = bstr(sp->value->key);
   4.249 -                fld->value = bstr(sp->value->value);
   4.250 -            }
   4.251 -            return fld;
   4.252 -        }
   4.253 -
   4.254 -        template<> int length<identity_list>(identity_list *tl)
   4.255 -        {
   4.256 -            return identity_list_length(tl);
   4.257 -        }
   4.258 -
   4.259 -        template<> int length<bloblist_t>(bloblist_t *tl)
   4.260 -        {
   4.261 -            return bloblist_length(tl);
   4.262 -        }
   4.263 -
   4.264 -        template<> int length<stringpair_list_t>(stringpair_list_t *tl)
   4.265 -        {
   4.266 -            return stringpair_list_length(tl);
   4.267 -        }
   4.268 -
   4.269 -        void clear_text_message(text_message *msg)
   4.270 -        {
   4.271 -            SysFreeString(msg->id);
   4.272 -            SysFreeString(msg->shortmsg);
   4.273 -            SysFreeString(msg->longmsg);
   4.274 -            SysFreeString(msg->longmsg_formatted);
   4.275 -            SafeArrayDestroy(msg->attachments);
   4.276 -            clear_identity_s(msg->from);
   4.277 -            SafeArrayDestroy(msg->to);
   4.278 -            clear_identity_s(msg->recv_by);
   4.279 -            SafeArrayDestroy(msg->cc);
   4.280 -            SafeArrayDestroy(msg->bcc);
   4.281 -            SafeArrayDestroy(msg->reply_to);
   4.282 -            SafeArrayDestroy(msg->references);
   4.283 -            SafeArrayDestroy(msg->keywords);
   4.284 -            SysFreeString(msg->comments);
   4.285 -            SafeArrayDestroy(msg->opt_fields);
   4.286 -
   4.287 -            memset(msg, 0, sizeof(text_message));
   4.288 -        }
   4.289 -
   4.290 -        void text_message_from_C(text_message *msg2, ::message *msg)
   4.291 -        {
   4.292 -            assert(msg2);
   4.293 -            assert(msg);
   4.294 -
   4.295 -            clear_text_message(msg2);
   4.296 -
   4.297 -            msg2->dir = (pEp_msg_direction) msg->dir;
   4.298 -            msg2->id = bstr(msg->id);
   4.299 -            msg2->shortmsg = bstr(msg->shortmsg);
   4.300 -            msg2->longmsg = bstr(msg->longmsg);
   4.301 -            msg2->longmsg_formatted = bstr(msg->longmsg_formatted);
   4.302 -            msg2->attachments = array_from_C<blob, bloblist_t>(msg->attachments);
   4.303 -            if (msg->sent)
   4.304 -                msg2->sent = mktime(msg->sent);
   4.305 -            if (msg->recv)
   4.306 -                msg2->recv = mktime(msg->recv);
   4.307 -            msg2->from = identity_s(msg->from);
   4.308 -            msg2->to = array_from_C<pEp_identity_s, identity_list>(msg->to);
   4.309 -            msg2->recv_by = identity_s(msg->recv_by);
   4.310 -            msg2->cc = array_from_C<pEp_identity_s, identity_list>(msg->cc);
   4.311 -            msg2->bcc = array_from_C<pEp_identity_s, identity_list>(msg->bcc);
   4.312 -            msg2->reply_to = array_from_C<pEp_identity_s, identity_list>(msg->reply_to);
   4.313 -            msg2->references = string_array(msg->references);
   4.314 -            msg2->keywords = string_array(msg->keywords);
   4.315 -            msg2->comments = bstr(msg->comments);
   4.316 -            msg2->opt_fields = array_from_C<opt_field, stringpair_list_t>(msg->opt_fields);
   4.317 -        }
   4.318 -
   4.319 -        char * str(BSTR s)
   4.320 -        {
   4.321 -            string str = utf8_string(s);
   4.322 -            char *_s = _strdup(str.c_str());
   4.323 -            if (_s == NULL)
   4.324 -                throw bad_alloc();
   4.325 -
   4.326 -            return _s;
   4.327 -        }
   4.328 -
   4.329 -        void clear_blob(blob& b)
   4.330 -        {
   4.331 -            SysFreeString(b.filename);
   4.332 -            SysFreeString(b.mime_type);
   4.333 -            SafeArrayDestroy(b.value);
   4.334 -            memset(&b, 0, sizeof(blob));
   4.335 -        }
   4.336 -
   4.337 -        bloblist_t *bloblist(SAFEARRAY *sa)
   4.338 -        {
   4.339 -            if (sa == NULL)
   4.340 -                return NULL;
   4.341 -
   4.342 -            LONG lbound, ubound;
   4.343 -            SafeArrayGetLBound(sa, 1, &lbound);
   4.344 -            SafeArrayGetUBound(sa, 1, &ubound);
   4.345 -
   4.346 -            size_t size = ubound - lbound + 1;
   4.347 -            if (size <= 0)
   4.348 -                return NULL;
   4.349 -
   4.350 -            bloblist_t *bl = new_bloblist(NULL, 0, NULL, NULL);
   4.351 -            if (bl == NULL)
   4.352 -                throw bad_alloc();
   4.353 -
   4.354 -            bloblist_t *_bl = bl;
   4.355 -            for (LONG i = lbound; i <= ubound; i++) {
   4.356 -                blob b;
   4.357 -                memset(&b, 0, sizeof(blob));
   4.358 -                SafeArrayGetElement(sa, &i, &b);
   4.359 -
   4.360 -                LONG _lbound, _ubound;
   4.361 -                SafeArrayGetLBound(b.value, 1, &_lbound);
   4.362 -                SafeArrayGetUBound(b.value, 1, &_ubound);
   4.363 -                size_t size = _ubound - _lbound + 1;
   4.364 -
   4.365 -                char *buffer = (char *) malloc(size);
   4.366 -                if (buffer == NULL)
   4.367 -                    throw bad_alloc();
   4.368 -
   4.369 -                char *data;
   4.370 -
   4.371 -                SafeArrayAccessData(b.value, (void **) &data);
   4.372 -                memcpy(buffer, data, size);
   4.373 -                SafeArrayUnaccessData(sa);
   4.374 -
   4.375 -                _bl = bloblist_add(_bl, buffer, size, str(b.mime_type), str(b.filename));
   4.376 -                if (_bl == NULL) {
   4.377 -                    free(buffer);
   4.378 -                    clear_blob(b);
   4.379 -                    free_bloblist(bl);
   4.380 -                    throw bad_alloc();
   4.381 -                }
   4.382 -
   4.383 -                clear_blob(b);
   4.384 -            }
   4.385 -
   4.386 -            return bl;
   4.387 -        }
   4.388 -
   4.389 -        identity_list *identities(SAFEARRAY * sa)
   4.390 -        {
   4.391 -            if (sa == NULL)
   4.392 -                return NULL;
   4.393 -
   4.394 -            LONG lbound, ubound;
   4.395 -            SafeArrayGetLBound(sa, 1, &lbound);
   4.396 -            SafeArrayGetUBound(sa, 1, &ubound);
   4.397 -
   4.398 -            size_t size = ubound - lbound + 1;
   4.399 -            if (size <= 0)
   4.400 -                return NULL;
   4.401 -
   4.402 -            identity_list *il = new_identity_list(NULL);
   4.403 -
   4.404 -            identity_list *_il = il;
   4.405 -            for (LONG i = lbound; i <= ubound; i++) {
   4.406 -                pEp_identity_s s;
   4.407 -                memset(&s, 0, sizeof(pEp_identity_s));
   4.408 -                SafeArrayGetElement(sa, &i, &s);
   4.409 -
   4.410 -                pEp_identity *ident;
   4.411 -                try {
   4.412 -                    ident = new_identity(&s);
   4.413 -                }
   4.414 -                catch (bad_alloc&) {
   4.415 -                    clear_identity_s(s);
   4.416 -                    throw bad_alloc();
   4.417 -                }
   4.418 -
   4.419 -                clear_identity_s(s);
   4.420 -
   4.421 -                _il = identity_list_add(_il, ident);
   4.422 -                if (_il == NULL) {
   4.423 -                    free_identity_list(il);
   4.424 -                    throw bad_alloc();
   4.425 -                }
   4.426 -            }
   4.427 -
   4.428 -            return il;
   4.429 -        }
   4.430 -
   4.431 -        stringpair_t *new_stringpair(opt_field *fld)
   4.432 -        {
   4.433 -            stringpair_t *pair = ::new_stringpair(str(fld->name), str(fld->value));
   4.434 -            if (pair == NULL)
   4.435 -                throw bad_alloc();
   4.436 -
   4.437 -            return pair;
   4.438 -        }
   4.439 -
   4.440 -        void clear_opt_field(opt_field& f)
   4.441 -        {
   4.442 -            SysFreeString(f.name);
   4.443 -            SysFreeString(f.value);
   4.444 -            memset(&f, 0, sizeof(opt_field));
   4.445 -        }
   4.446 -
   4.447 -        stringpair_list_t *stringpair_list(SAFEARRAY * sa)
   4.448 -        {
   4.449 -            if (sa == NULL)
   4.450 -                return NULL;
   4.451 -
   4.452 -            LONG lbound, ubound;
   4.453 -            SafeArrayGetLBound(sa, 1, &lbound);
   4.454 -            SafeArrayGetUBound(sa, 1, &ubound);
   4.455 -
   4.456 -            size_t size = ubound - lbound + 1;
   4.457 -            if (size <= 0)
   4.458 -                return NULL;
   4.459 -
   4.460 -            stringpair_list_t *il = new_stringpair_list(NULL);
   4.461 -
   4.462 -            stringpair_list_t *_il = il;
   4.463 -            for (LONG i = lbound; i <= ubound; i++) {
   4.464 -                opt_field s;
   4.465 -                memset(&s, 0, sizeof(opt_field));
   4.466 -                SafeArrayGetElement(sa, &i, &s);
   4.467 -
   4.468 -                stringpair_t *pair;
   4.469 -                try {
   4.470 -                    pair = new_stringpair(&s);
   4.471 -                }
   4.472 -                catch (bad_alloc&) {
   4.473 -                    clear_opt_field(s);
   4.474 -                    throw bad_alloc();
   4.475 -                }
   4.476 -
   4.477 -                clear_opt_field(s);
   4.478 -
   4.479 -                _il = stringpair_list_add(_il, pair);
   4.480 -                if (_il == NULL) {
   4.481 -                    free_stringpair_list(il);
   4.482 -                    throw bad_alloc();
   4.483 -                }
   4.484 -            }
   4.485 -
   4.486 -            return il;
   4.487 -        }
   4.488 -        
   4.489 -        ::message * text_message_to_C(text_message *msg)
   4.490 -        {
   4.491 -            assert(msg);
   4.492 -
   4.493 -            ::message * msg2 = new_message((PEP_msg_direction) msg->dir);
   4.494 -            if (msg2 == NULL)
   4.495 -                throw bad_alloc();
   4.496 -
   4.497 -            msg2->id = str(msg->id);
   4.498 -            msg2->shortmsg = str(msg->shortmsg);
   4.499 -            msg2->longmsg = str(msg->longmsg);
   4.500 -            msg2->longmsg_formatted = str(msg->longmsg_formatted);
   4.501 -            msg2->attachments = bloblist(msg->attachments);
   4.502 -            msg2->sent = new_timestamp(msg->sent);
   4.503 -            msg2->recv = new_timestamp(msg->recv);
   4.504 -            msg2->from = new_identity(&msg->from);
   4.505 -            msg2->to = identities(msg->to);
   4.506 -            msg2->recv_by = new_identity(&msg->recv_by);
   4.507 -            msg2->cc = identities(msg->cc);
   4.508 -            msg2->bcc = identities(msg->bcc);
   4.509 -            msg2->reply_to = identities(msg->reply_to);
   4.510 -            msg2->references = new_stringlist(msg->references);
   4.511 -            msg2->keywords = new_stringlist(msg->keywords);
   4.512 -            msg2->comments = str(msg->comments);
   4.513 -            msg2->opt_fields = stringpair_list(msg->opt_fields);
   4.514 -
   4.515 -            return msg2;
   4.516 -        }
   4.517 -    }
   4.518 -}