Allow to set event loop type with api
[platform/core/api/vine.git] / src / vine-auth.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <unistd.h>
18 #include <stdlib.h>
19
20 #include "vine-auth.h"
21 #include "vine-log.h"
22
23 unsigned char *alloc_auth_frame(unsigned char ver,
24                 unsigned char auth_type, unsigned char op,
25                 int data_len, const unsigned char *data, int *size)
26 {
27         *size = 5 + data_len;
28         unsigned char *buf = (unsigned char *)calloc(*size, sizeof(unsigned char));
29         if (buf == NULL) {
30                 VINE_LOGE("Out of memory");
31                 return NULL;
32         }
33
34         buf[0] = ver;
35         buf[1] = auth_type;
36         buf[2] = op;
37         buf[3] = (0xFF00 & data_len) >> 8;
38         buf[4] = (0xFF & data_len);
39         memcpy(&buf[5], data, data_len);
40
41         return buf;
42 }
43
44 void release_auth_frame(unsigned char *buf)
45 {
46         free(buf);
47 }
48
49 bool parse_auth_frame(unsigned char *buf, size_t buf_size,
50                 unsigned char *ver, unsigned char *auth_type,
51                 unsigned char *op, int *data_len, unsigned char **data)
52 {
53         *ver = buf[0];
54         *auth_type = buf[1];
55         *op = buf[2];
56         *data_len = (buf[3] << 8) | (buf[4]);
57         *data = (unsigned char *)malloc(*data_len);
58         if (*data == NULL) {
59                 VINE_LOGE("Out of memory");
60                 return false;
61         }
62         memcpy(*data, &buf[5], *data_len);
63
64         // TODO: Need to check if it is an auth frame or not
65         return true;
66 }