• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *    http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "app_common.h"
17 
HapGetInt64(const unsigned char * buf,int len)18 long long HapGetInt64(const unsigned char *buf, int len)
19 {
20     if (buf == NULL) {
21         return 0;
22     }
23     unsigned long long value = 0;
24     if (len != sizeof(long long)) {
25         return 0;
26     }
27     while (len-- > 0) {
28         value = (value << BYTE_BITS) | (*(buf + len));
29     }
30     return (long long)value;
31 }
32 
HapGetInt(const unsigned char * buf,int len)33 int HapGetInt(const unsigned char *buf, int len)
34 {
35     unsigned int value = HapGetUnsignedInt(buf, len);
36     return (int)value;
37 }
38 
HapGetUnsignedInt(const unsigned char * buf,int len)39 unsigned int HapGetUnsignedInt(const unsigned char *buf, int len)
40 {
41     if (buf == NULL) {
42         return 0;
43     }
44     unsigned int value = 0;
45     if (len != sizeof(int)) {
46         return 0;
47     }
48     while (len-- > 0) {
49         value = (value << BYTE_BITS) | (*(buf + len));
50     }
51     return value;
52 }
53 
HapGetShort(const unsigned char * buf,int len)54 short HapGetShort(const unsigned char *buf, int len)
55 {
56     if (buf == NULL) {
57         return 0;
58     }
59     unsigned short value = 0;
60     if (len != sizeof(short)) {
61         return 0;
62     }
63     while (len-- > 0) {
64         value = (value << BYTE_BITS) | (*(buf + len));
65     }
66     return (short)value;
67 }
68 
HapPutInt32(unsigned char * buf,int len,int value)69 void HapPutInt32(unsigned char *buf, int len, int value)
70 {
71     if (buf == NULL || len < sizeof(int)) {
72         return;
73     }
74     int i;
75     unsigned int var = (unsigned int)value;
76     for (i = 0; i < sizeof(int); i++) {
77         buf[i] = var;
78         var = var >> (BYTE_BITS);
79     }
80     return;
81 }
82