Coverage Report

Created: 2026-02-26 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/libfreerdp/primitives/prim_set.c
Line
Count
Source
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
#include <freerdp/config.h>
18
19
#include <string.h>
20
21
#include <freerdp/types.h>
22
#include <freerdp/primitives.h>
23
24
#include "prim_internal.h"
25
#include "prim_set.h"
26
27
/* ========================================================================= */
28
static pstatus_t general_set_8u(BYTE val, BYTE* WINPR_RESTRICT pDst, UINT32 len)
29
0
{
30
0
  memset((void*)pDst, (int)val, (size_t)len);
31
0
  return PRIMITIVES_SUCCESS;
32
0
}
33
34
/* ------------------------------------------------------------------------- */
35
static pstatus_t general_zero(void* WINPR_RESTRICT pDst, size_t len)
36
0
{
37
0
  memset(pDst, 0, len);
38
0
  return PRIMITIVES_SUCCESS;
39
0
}
40
41
/* ========================================================================= */
42
static pstatus_t general_set_32s(INT32 val, INT32* WINPR_RESTRICT pDst, UINT32 len)
43
0
{
44
0
  INT32* dptr = pDst;
45
0
  size_t span = 0;
46
0
  size_t remaining = 0;
47
48
0
  if (len < 256)
49
0
  {
50
0
    while (len--)
51
0
      *dptr++ = val;
52
53
0
    return PRIMITIVES_SUCCESS;
54
0
  }
55
56
  /* else quadratic growth memcpy algorithm */
57
0
  span = 1;
58
0
  *dptr = val;
59
0
  remaining = len - 1;
60
0
  primitives_t* prims = primitives_get();
61
62
0
  while (remaining)
63
0
  {
64
0
    size_t thiswidth = span;
65
66
0
    if (thiswidth > remaining)
67
0
      thiswidth = remaining;
68
69
0
    const size_t s = thiswidth << 2;
70
0
    WINPR_ASSERT(thiswidth <= INT32_MAX);
71
0
    const pstatus_t rc = prims->copy_8u((BYTE*)dptr, (BYTE*)(dptr + span), (INT32)s);
72
0
    if (rc != PRIMITIVES_SUCCESS)
73
0
      return rc;
74
0
    remaining -= thiswidth;
75
0
    span <<= 1;
76
0
  }
77
78
0
  return PRIMITIVES_SUCCESS;
79
0
}
80
81
/* ------------------------------------------------------------------------- */
82
static pstatus_t general_set_32u(UINT32 val, UINT32* WINPR_RESTRICT pDst, UINT32 len)
83
0
{
84
0
  UINT32* dptr = pDst;
85
0
  size_t span = 0;
86
0
  size_t remaining = 0;
87
0
  primitives_t* prims = NULL;
88
89
0
  if (len < 256)
90
0
  {
91
0
    while (len--)
92
0
      *dptr++ = val;
93
94
0
    return PRIMITIVES_SUCCESS;
95
0
  }
96
97
  /* else quadratic growth memcpy algorithm */
98
0
  span = 1;
99
0
  *dptr = val;
100
0
  remaining = len - 1;
101
0
  prims = primitives_get();
102
103
0
  while (remaining)
104
0
  {
105
0
    size_t thiswidth = span;
106
107
0
    if (thiswidth > remaining)
108
0
      thiswidth = remaining;
109
110
0
    const size_t s = thiswidth << 2;
111
0
    WINPR_ASSERT(thiswidth <= INT32_MAX);
112
0
    const pstatus_t rc = prims->copy_8u((BYTE*)dptr, (BYTE*)(dptr + span), (INT32)s);
113
0
    if (rc != PRIMITIVES_SUCCESS)
114
0
      return rc;
115
116
0
    remaining -= thiswidth;
117
0
    span <<= 1;
118
0
  }
119
120
0
  return PRIMITIVES_SUCCESS;
121
0
}
122
123
/* ------------------------------------------------------------------------- */
124
void primitives_init_set(primitives_t* WINPR_RESTRICT prims)
125
0
{
126
  /* Start with the default. */
127
0
  prims->set_8u = general_set_8u;
128
0
  prims->set_32s = general_set_32s;
129
0
  prims->set_32u = general_set_32u;
130
0
  prims->zero = general_zero;
131
0
}
132
133
void primitives_init_set_opt(primitives_t* WINPR_RESTRICT prims)
134
0
{
135
0
  primitives_init_set(prims);
136
0
  primitives_init_set_sse2(prims);
137
0
}