1 | /******************************************************************** |
2 | * Copyright (c) 2013 - 2014, Pivotal Inc. |
3 | * All rights reserved. |
4 | * |
5 | * Author: Zhanwei Wang |
6 | ********************************************************************/ |
7 | /******************************************************************** |
8 | * 2014 - |
9 | * open source under Apache License Version 2.0 |
10 | ********************************************************************/ |
11 | /** |
12 | * Licensed to the Apache Software Foundation (ASF) under one |
13 | * or more contributor license agreements. See the NOTICE file |
14 | * distributed with this work for additional information |
15 | * regarding copyright ownership. The ASF licenses this file |
16 | * to you under the Apache License, Version 2.0 (the |
17 | * "License"); you may not use this file except in compliance |
18 | * with the License. You may obtain a copy of the License at |
19 | * |
20 | * http://www.apache.org/licenses/LICENSE-2.0 |
21 | * |
22 | * Unless required by applicable law or agreed to in writing, software |
23 | * distributed under the License is distributed on an "AS IS" BASIS, |
24 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
25 | * See the License for the specific language governing permissions and |
26 | * limitations under the License. |
27 | */ |
28 | #ifndef _HDFS_LIBHDFS3_COMMON_BIGENDIAN_H_ |
29 | #define _HDFS_LIBHDFS3_COMMON_BIGENDIAN_H_ |
30 | |
31 | #include <arpa/inet.h> |
32 | #include <cstring> |
33 | |
34 | namespace Hdfs { |
35 | namespace Internal { |
36 | |
37 | static inline int16_t ReadBigEndian16FromArray(const char * buffer) { |
38 | int16_t retval; |
39 | retval = ntohs(*reinterpret_cast<const int16_t *>(buffer)); |
40 | return retval; |
41 | } |
42 | |
43 | static inline int32_t ReadBigEndian32FromArray(const char * buffer) { |
44 | int32_t retval; |
45 | retval = ntohl(*reinterpret_cast<const int32_t *>(buffer)); |
46 | return retval; |
47 | } |
48 | |
49 | static inline char * WriteBigEndian16ToArray(int16_t value, char * buffer) { |
50 | int16_t bigValue = htons(value); |
51 | memcpy(buffer, reinterpret_cast<const char *>(&bigValue), sizeof(int16_t)); |
52 | return buffer + sizeof(int16_t); |
53 | } |
54 | |
55 | static inline char * WriteBigEndian32ToArray(int32_t value, char * buffer) { |
56 | int32_t bigValue = htonl(value); |
57 | memcpy(buffer, reinterpret_cast<const char *>(&bigValue), sizeof(int32_t)); |
58 | return buffer + sizeof(int32_t); |
59 | } |
60 | |
61 | } |
62 | } |
63 | |
64 | #endif /* _HDFS_LIBHDFS3_COMMON_BIGENDIAN_H_ */ |
65 | |