1 /* $NetBSD: in_cksum.c,v 1.7 1997/09/02 13:18:15 thorpej Exp $ */
2
3 /*-
4 * SPDX-License-Identifier: BSD-4-Clause
5 *
6 * Copyright (c) 1988, 1992, 1993
7 * The Regents of the University of California. All rights reserved.
8 * Copyright (c) 1996
9 * Matt Thomas <matt@3am-software.com>
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 * @(#)in_cksum.c 8.1 (Berkeley) 6/10/93
40 */
41
42 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
43 __FBSDID("$FreeBSD$");
44
45 #include "los_base.h"
46 #include "sys/types.h"
47
48 #define ADDCARRY(x) (x > 65535 ? x -= 65535 : x)
49 #define REDUCE32 \
50 { \
51 q_util.q = sum; \
52 sum = q_util.s[0] + q_util.s[1] + q_util.s[2] + q_util.s[3]; \
53 }
54 #define REDUCE16 \
55 { \
56 q_util.q = sum; \
57 l_util.l = q_util.s[0] + q_util.s[1] + q_util.s[2] + q_util.s[3]; \
58 sum = l_util.s[0] + l_util.s[1]; \
59 ADDCARRY(sum); \
60 }
61
62 union un_l_util {
63 u_int16_t s[2];
64 u_int32_t l;
65 };
66
67 union un_q_util {
68 u_int16_t s[4];
69 u_int32_t l[2];
70 u_int64_t q;
71 };
72
73 /*do_cksum is implemented in in_cksum_arm.S*/
74 extern unsigned int do_cksum(const void * buf, int len);
75
_do_cksum(const void * buf,int len)76 static u_int64_t _do_cksum(const void *buf, int len)
77 {
78 u_int64_t sum;
79 union un_q_util q_util;
80
81 sum = do_cksum(buf, len);
82 REDUCE32;
83 return (sum);
84 }
85
86 /* This function is for LWIP_CHKSUM */
in_cksum(const void * buf,int len)87 unsigned short in_cksum(const void *buf, int len)
88 {
89 u_int64_t sum = 0;
90 union un_q_util q_util;
91 union un_l_util l_util;
92
93 if (((int)buf) & 1) {
94 sum = _do_cksum(buf, len) << 8;
95 } else {
96 sum = _do_cksum(buf, len);
97 }
98
99 REDUCE16;
100 return (unsigned short)(~sum & 0xffff);
101 }
102
103 /* This function is for LWIP_CHKSUM_COPY */
in_cksum_copy(const void * src,void * dst,int len)104 unsigned short in_cksum_copy(const void *src, void *dst, int len)
105 {
106 u_int64_t sum = 0;
107 union un_q_util q_util;
108 union un_l_util l_util;
109 int src_len = strlen(src);
110
111 if (src_len >= len) {
112 src_len = len;
113 }
114 if (memcpy_s(dst, len, src, src_len) != EOK) {
115 return (unsigned short)(sum);
116 }
117
118 if (((long)dst) & 1) {
119 sum = _do_cksum(dst, len) << 8;
120 }else {
121 sum = _do_cksum(dst, len);
122 }
123
124 REDUCE16;
125 return (unsigned short)(~sum & 0xffff);
126 }
127
128 /*As usb_rtl8188eus used csum_partial, so wrapper this interface for usb_rtl8188*/
csum_partial(const void * buf,int len,unsigned int wsum)129 unsigned int csum_partial(const void *buf, int len, unsigned int wsum)
130 {
131 u_int64_t sum = 0;
132 union un_q_util q_util;
133
134 if (((int)buf) & 1) {
135 sum = _do_cksum(buf, len) << 8;
136 } else {
137 sum = _do_cksum(buf, len);
138 }
139 if(wsum > 0) {
140 sum += (unsigned int)wsum;
141 REDUCE32;
142 }
143
144 return (unsigned int)(sum);
145 }
146
147 /* Keep this function for compatible */
csum_partial_copy_nocheck(const void * src,void * dst,int len,unsigned int wsum)148 unsigned int csum_partial_copy_nocheck(const void *src, void *dst, int len, unsigned int wsum)
149 {
150 u_int64_t sum = 0;
151 union un_q_util q_util;
152 int src_len = strlen(src);
153
154 if (src_len >= len) {
155 src_len = len;
156 }
157 if (memcpy_s(dst, len, src, src_len) != EOK) {
158 return (unsigned int)(sum);
159 }
160
161 if (((int)dst) & 1) {
162 sum = _do_cksum(dst, len) << 8;
163 }else {
164 sum = _do_cksum(dst, len);
165 }
166
167 if(wsum > 0) {
168 sum += (unsigned int)wsum;
169 REDUCE32;
170 }
171
172 return (unsigned int)(sum);
173 }
174
175