a1c5bbac09479110f7fb3cd334f88e63bea66705
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / adapter_util / pkix / der_dec.c
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      LICENSE-2.0" target="_blank">http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19
20  ******************************************************************/
21
22 #include "der_dec.h"
23
24 /**
25  * Computes length.
26  */
27 PKIError DecodeLength(ByteArray *code, size_t *length)
28 {
29     FUNCTION_INIT(
30         CHECK_NULL_BYTE_ARRAY_PTR(code, PKI_NULL_PASSED);
31     );
32     CHECK_INC_BYTE_ARRAY_PTR(code, 1);
33
34     if ((*(code->data)) < LEN_LONG)
35     {
36         *length = *(code->data);
37         CHECK_INC_BYTE_ARRAY_PTR(code, 1);
38     }
39     else
40     {
41         uint8_t i = 0;
42         uint8_t blocksNum = *(code->data) - LEN_LONG;
43         CHECK_INC_BYTE_ARRAY_PTR(code, 1);
44         CHECK_LESS(blocksNum, 5, PKI_WRONG_OCTET_LEN);
45         *length = 0;
46
47         for (i = 0; i < blocksNum; ++i)
48         {
49             *length |= *(code->data) << ((blocksNum - i - 1) * SIZE_OF_BYTE);
50             CHECK_INC_BYTE_ARRAY_PTR(code, 1);
51         }
52     }
53
54     //should be: length  <=  array size
55     CHECK_LESS_EQUAL(*length, code->len, PKI_WRONG_OCTET_LEN);
56     FUNCTION_CLEAR();
57 }