1 /* FreeRDP: A Remote Desktop Protocol Client
2  * Routines to set a chunk of memory to a constant.
3  * vi:ts=4 sw=4:
4  *
5  * (c) Copyright 2012 Hewlett-Packard Development Company, L.P.
6  * Licensed under the Apache License, Version 2.0 (the "License"); you may
7  * not use this file except in compliance with the License. You may obtain
8  * a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
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
12  * or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  *
15  */
16 
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20 
21 #include <string.h>
22 
23 #include <freerdp/types.h>
24 #include <freerdp/primitives.h>
25 
26 #include "prim_internal.h"
27 
28 /* ========================================================================= */
general_set_8u(BYTE val,BYTE * pDst,UINT32 len)29 static pstatus_t general_set_8u(BYTE val, BYTE* pDst, UINT32 len)
30 {
31 	memset((void*)pDst, (int)val, (size_t)len);
32 	return PRIMITIVES_SUCCESS;
33 }
34 
35 /* ------------------------------------------------------------------------- */
general_zero(void * pDst,size_t len)36 static pstatus_t general_zero(void* pDst, size_t len)
37 {
38 	memset(pDst, 0, len);
39 	return PRIMITIVES_SUCCESS;
40 }
41 
42 /* ========================================================================= */
general_set_32s(INT32 val,INT32 * pDst,UINT32 len)43 static pstatus_t general_set_32s(INT32 val, INT32* pDst, UINT32 len)
44 {
45 	INT32* dptr = (INT32*)pDst;
46 	size_t span, remaining;
47 	primitives_t* prims;
48 
49 	if (len < 256)
50 	{
51 		while (len--)
52 			*dptr++ = val;
53 
54 		return PRIMITIVES_SUCCESS;
55 	}
56 
57 	/* else quadratic growth memcpy algorithm */
58 	span = 1;
59 	*dptr = val;
60 	remaining = len - 1;
61 	prims = primitives_get();
62 
63 	while (remaining)
64 	{
65 		size_t thiswidth = span;
66 
67 		if (thiswidth > remaining)
68 			thiswidth = remaining;
69 
70 		prims->copy_8u((BYTE*)dptr, (BYTE*)(dptr + span), thiswidth << 2);
71 		remaining -= thiswidth;
72 		span <<= 1;
73 	}
74 
75 	return PRIMITIVES_SUCCESS;
76 }
77 
78 /* ------------------------------------------------------------------------- */
general_set_32u(UINT32 val,UINT32 * pDst,UINT32 len)79 static pstatus_t general_set_32u(UINT32 val, UINT32* pDst, UINT32 len)
80 {
81 	UINT32* dptr = (UINT32*)pDst;
82 	size_t span, remaining;
83 	primitives_t* prims;
84 
85 	if (len < 256)
86 	{
87 		while (len--)
88 			*dptr++ = val;
89 
90 		return PRIMITIVES_SUCCESS;
91 	}
92 
93 	/* else quadratic growth memcpy algorithm */
94 	span = 1;
95 	*dptr = val;
96 	remaining = len - 1;
97 	prims = primitives_get();
98 
99 	while (remaining)
100 	{
101 		size_t thiswidth = span;
102 
103 		if (thiswidth > remaining)
104 			thiswidth = remaining;
105 
106 		prims->copy_8u((BYTE*)dptr, (BYTE*)(dptr + span), thiswidth << 2);
107 		remaining -= thiswidth;
108 		span <<= 1;
109 	}
110 
111 	return PRIMITIVES_SUCCESS;
112 }
113 
114 /* ------------------------------------------------------------------------- */
primitives_init_set(primitives_t * prims)115 void primitives_init_set(primitives_t* prims)
116 {
117 	/* Start with the default. */
118 	prims->set_8u = general_set_8u;
119 	prims->set_32s = general_set_32s;
120 	prims->set_32u = general_set_32u;
121 	prims->zero = general_zero;
122 }
123