Skip to content
This repository was archived by the owner on Jul 29, 2024. It is now read-only.

Commit 58b3593

Browse files
authored
Safe string functions refactor (#422)
Port over from SVT-AV1 Author: John Stebbins <[email protected]> - Consolidate safe string functions in App - Duplicate string functions in lib with different names These functions are not desired in the lib's API, but are used in both the lib and the App. They can't have the same name since this causes duplicate symbols in statically link executable. Signed-off-by: Jun Tian <[email protected]>
1 parent d0004f1 commit 58b3593

File tree

9 files changed

+660
-475
lines changed

9 files changed

+660
-475
lines changed

Source/App/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ include_directories(${PROJECT_SOURCE_DIR}/Source/API/
1313
set(SvtHevcEncApp_Source
1414
EbAppConfig.c
1515
EbAppContext.c
16-
EbAppFifo.c
16+
EbAppString.c
1717
EbAppMain.c
1818
EbAppProcessCmd.c
1919
EbTime.c
@@ -29,6 +29,7 @@ add_executable(SvtHevcEncApp
2929
../API/EbApi.h
3030
EbAppConfig.h
3131
EbAppContext.h
32+
EbAppString.h
3233
../API/EbErrorCodes.h)
3334

3435
#********** SET COMPILE FLAGS************

Source/App/EbAppConfig.h

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <stdio.h>
1010

11+
#include "EbAppString.h"
1112
#include "EbApi.h"
1213

1314
// Define Cross-Platform 64-bit fseek() and ftell()
@@ -20,16 +21,6 @@ typedef __int64 off64_t;
2021
#define ftello64 ftell
2122
#endif
2223

23-
#ifndef _RSIZE_T_DEFINED
24-
typedef size_t rsize_t;
25-
#define _RSIZE_T_DEFINED
26-
#endif /* _RSIZE_T_DEFINED */
27-
28-
#ifndef _ERRNO_T_DEFINED
29-
#define _ERRNO_T_DEFINED
30-
typedef int32_t errno_t;
31-
#endif /* _ERRNO_T_DEFINED */
32-
3324
/** The APPEXITCONDITIONTYPE type is used to define the App main loop exit
3425
conditions.
3526
*/
@@ -142,27 +133,6 @@ typedef struct EB_PARAM_PORTDEFINITIONTYPE {
142133
} \
143134
appMallocCount++;
144135

145-
/* string copy */
146-
extern errno_t strcpy_ss(char *dest, rsize_t dmax, const char *src);
147-
148-
/* fitted string copy */
149-
extern errno_t strncpy_ss(char *dest, rsize_t dmax, const char *src, rsize_t slen);
150-
151-
/* string length */
152-
extern rsize_t strnlen_ss(const char *s, rsize_t smax);
153-
154-
#define EB_STRNCPY(dst, max_size, src, count) \
155-
strncpy_ss(dst, max_size, src, count)
156-
157-
#define EB_STRCPY(dst, size, src) \
158-
strcpy_ss(dst, size, src)
159-
160-
#define EB_STRCMP(target,token) \
161-
strcmp(target,token)
162-
163-
#define EB_STRLEN(target, max_size) \
164-
strnlen_ss(target, max_size)
165-
166136
#define EB_APP_MEMORY() \
167137
printf("Total Number of Mallocs in App: %d\n", appMallocCount); \
168138
printf("Total App Memory: %.2lf KB\n\n",*totalAppMemory/(double)1024);

Source/App/EbAppInputy4m.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
#define CHROMA_MAX 4
1010

11-
#include "EbAppConfig.h"
1211
#include <ctype.h>
1312

1413
/* copy a string until a specified character or a new line is found */

Source/App/EbAppFifo.c renamed to Source/App/EbAppString.c

Lines changed: 1 addition & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,8 @@
11

2-
#include "EbAppConfig.h"
2+
#include "EbAppString.h"
33

44
/* SAFE STRING LIBRARY */
55

6-
#ifndef EOK
7-
#define EOK ( 0 )
8-
#endif
9-
10-
#ifndef ESZEROL
11-
#define ESZEROL ( 401 ) /* length is zero */
12-
#endif
13-
14-
#ifndef ESLEMIN
15-
#define ESLEMIN ( 402 ) /* length is below min */
16-
#endif
17-
18-
#ifndef ESLEMAX
19-
#define ESLEMAX ( 403 ) /* length exceeds max */
20-
#endif
21-
22-
#ifndef ESNULLP
23-
#define ESNULLP ( 400 ) /* null ptr */
24-
#endif
25-
26-
#ifndef ESOVRLP
27-
#define ESOVRLP ( 404 ) /* overlap undefined */
28-
#endif
29-
30-
#ifndef ESEMPTY
31-
#define ESEMPTY ( 405 ) /* empty string */
32-
#endif
33-
34-
#ifndef ESNOSPC
35-
#define ESNOSPC ( 406 ) /* not enough space for s2 */
36-
#endif
37-
38-
#ifndef ESUNTERM
39-
#define ESUNTERM ( 407 ) /* unterminated string */
40-
#endif
41-
42-
#ifndef ESNODIFF
43-
#define ESNODIFF ( 408 ) /* no difference */
44-
#endif
45-
46-
#ifndef ESNOTFND
47-
#define ESNOTFND ( 409 ) /* not found */
48-
#endif
49-
50-
#define RSIZE_MAX_MEM ( 256UL << 20 ) /* 256MB */
51-
52-
#define RCNEGATE(x) (x)
53-
#define RSIZE_MAX_STR ( 4UL << 10 ) /* 4KB */
54-
#define sl_default_handler ignore_handler_s
55-
#define EXPORT_SYMBOL(sym)
56-
57-
#ifndef sldebug_printf
58-
#define sldebug_printf(...)
59-
#endif
60-
61-
/*
62-
* Function used by the libraries to invoke the registered
63-
* runtime-constraint handler. Always needed.
64-
*/
65-
66-
typedef void(*constraint_handler_t) (const char * /* msg */,
67-
void * /* ptr */,
68-
errno_t /* error */);
69-
extern void ignore_handler_s(const char *msg, void *ptr, errno_t error);
70-
71-
/*
72-
* Function used by the libraries to invoke the registered
73-
* runtime-constraint handler. Always needed.
74-
*/
75-
extern void invoke_safe_str_constraint_handler(
76-
const char *msg,
77-
void *ptr,
78-
errno_t error);
79-
80-
81-
static inline void handle_error(char *orig_dest, rsize_t orig_dmax,
82-
char *err_msg, errno_t err_code)
83-
{
84-
(void)orig_dmax;
85-
*orig_dest = '\0';
86-
87-
invoke_safe_str_constraint_handler(err_msg, NULL, err_code);
88-
return;
89-
}
906
static constraint_handler_t str_handler = NULL;
917

928
void
@@ -111,7 +27,6 @@ void ignore_handler_s(const char *msg, void *ptr, errno_t error)
11127
(msg) ? msg : "Null message");
11228
return;
11329
}
114-
EXPORT_SYMBOL(ignore_handler_s)
11530

11631
errno_t
11732
strncpy_ss(char *dest, rsize_t dmax, const char *src, rsize_t slen)
@@ -236,7 +151,6 @@ strncpy_ss(char *dest, rsize_t dmax, const char *src, rsize_t slen)
236151
ESNOSPC);
237152
return RCNEGATE(ESNOSPC);
238153
}
239-
EXPORT_SYMBOL(strncpy_ss)
240154

241155
errno_t
242156
strcpy_ss(char *dest, rsize_t dmax, const char *src)
@@ -331,7 +245,6 @@ strcpy_ss(char *dest, rsize_t dmax, const char *src)
331245
ESNOSPC);
332246
return RCNEGATE(ESNOSPC);
333247
}
334-
EXPORT_SYMBOL(strcpy_ss)
335248

336249
rsize_t
337250
strnlen_ss(const char *dest, rsize_t dmax)
@@ -363,6 +276,5 @@ strnlen_ss(const char *dest, rsize_t dmax)
363276

364277
return RCNEGATE(count);
365278
}
366-
EXPORT_SYMBOL(strnlen_ss)
367279

368280
/* SAFE STRING LIBRARY */

Source/App/EbAppString.h

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
/*------------------------------------------------------------------
2+
* strncpy_s.c / strcpy_s.c / strnlen_s.c
3+
*
4+
* October 2008, Bo Berry
5+
*
6+
* Copyright � 2008-2011 by Cisco Systems, Inc
7+
* All rights reserved.
8+
9+
* safe_str_constraint.c
10+
*
11+
* October 2008, Bo Berry
12+
* 2012, Jonathan Toppins <[email protected]>
13+
*
14+
* Copyright � 2008, 2009, 2012 Cisco Systems
15+
* All rights reserved.
16+
17+
* ignore_handler_s.c
18+
*
19+
* 2012, Jonathan Toppins <[email protected]>
20+
*
21+
* Copyright � 2012 Cisco Systems
22+
* All rights reserved.
23+
*
24+
* Permission is hereby granted, free of charge, to any person
25+
* obtaining a copy of this software and associated documentation
26+
* files (the "Software"), to deal in the Software without
27+
* restriction, including without limitation the rights to use,
28+
* copy, modify, merge, publish, distribute, sublicense, and/or
29+
* sell copies of the Software, and to permit persons to whom the
30+
* Software is furnished to do so, subject to the following
31+
* conditions:
32+
*
33+
* The above copyright notice and this permission notice shall be
34+
* included in all copies or substantial portions of the Software.
35+
*
36+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
37+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
38+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
39+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
40+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
41+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
42+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
43+
* OTHER DEALINGS IN THE SOFTWARE.
44+
*------------------------------------------------------------------
45+
*/
46+
#ifndef EbAppString_h
47+
#define EbAppString_h
48+
49+
#include <stddef.h>
50+
#include <stdint.h>
51+
52+
#ifdef __cplusplus
53+
extern "C" {
54+
#endif // __cplusplus
55+
56+
#ifndef _RSIZE_T_DEFINED
57+
typedef size_t rsize_t;
58+
#define _RSIZE_T_DEFINED
59+
#endif /* _RSIZE_T_DEFINED */
60+
61+
#ifndef _ERRNO_T_DEFINED
62+
#define _ERRNO_T_DEFINED
63+
typedef int32_t errno_t;
64+
#endif /* _ERRNO_T_DEFINED */
65+
66+
#ifndef EOK
67+
#define EOK ( 0 )
68+
#endif
69+
70+
#ifndef ESZEROL
71+
#define ESZEROL ( 401 ) /* length is zero */
72+
#endif
73+
74+
#ifndef ESLEMIN
75+
#define ESLEMIN ( 402 ) /* length is below min */
76+
#endif
77+
78+
#ifndef ESLEMAX
79+
#define ESLEMAX ( 403 ) /* length exceeds max */
80+
#endif
81+
82+
#ifndef ESNULLP
83+
#define ESNULLP ( 400 ) /* null ptr */
84+
#endif
85+
86+
#ifndef ESOVRLP
87+
#define ESOVRLP ( 404 ) /* overlap undefined */
88+
#endif
89+
90+
#ifndef ESEMPTY
91+
#define ESEMPTY ( 405 ) /* empty string */
92+
#endif
93+
94+
#ifndef ESNOSPC
95+
#define ESNOSPC ( 406 ) /* not enough space for s2 */
96+
#endif
97+
98+
#ifndef ESUNTERM
99+
#define ESUNTERM ( 407 ) /* unterminated string */
100+
#endif
101+
102+
#ifndef ESNODIFF
103+
#define ESNODIFF ( 408 ) /* no difference */
104+
#endif
105+
106+
#ifndef ESNOTFND
107+
#define ESNOTFND ( 409 ) /* not found */
108+
#endif
109+
110+
#define RSIZE_MAX_STR ( 4UL << 10 ) /* 4KB */
111+
#define RCNEGATE(x) (x)
112+
113+
#ifndef sldebug_printf
114+
#define sldebug_printf(...)
115+
#endif
116+
117+
typedef void(*constraint_handler_t) (const char * /* msg */,
118+
void * /* ptr */,
119+
errno_t /* error */);
120+
121+
/*
122+
* Function used by the libraries to invoke the registered
123+
* runtime-constraint handler. Always needed.
124+
*/
125+
extern void invoke_safe_str_constraint_handler(
126+
const char *msg,
127+
void *ptr,
128+
errno_t error);
129+
130+
static inline void handle_error(char *orig_dest, rsize_t orig_dmax,
131+
char *err_msg, errno_t err_code)
132+
{
133+
(void)orig_dmax;
134+
*orig_dest = '\0';
135+
136+
invoke_safe_str_constraint_handler(err_msg, NULL, err_code);
137+
return;
138+
}
139+
140+
#define sl_default_handler ignore_handler_s
141+
extern void ignore_handler_s(const char *msg, void *ptr, errno_t error);
142+
143+
/* string copy */
144+
errno_t strcpy_ss(
145+
char *dest, rsize_t dmax, const char *src);
146+
147+
/* fitted string copy */
148+
errno_t strncpy_ss(
149+
char *dest, rsize_t dmax, const char *src, rsize_t slen);
150+
151+
/* string length */
152+
rsize_t strnlen_ss(
153+
const char *s, rsize_t smax);
154+
155+
#define EB_STRNCPY(dst, dst_size, src, count) \
156+
strncpy_ss(dst, dst_size, src, count)
157+
158+
#define EB_STRCPY(dst, size, src) \
159+
strcpy_ss(dst, size, src)
160+
161+
#define EB_STRCMP(target,token) \
162+
strcmp(target,token)
163+
164+
#define EB_STRLEN(target, max_size) \
165+
strnlen_ss(target, max_size)
166+
167+
#ifdef __cplusplus
168+
}
169+
#endif // __cplusplus
170+
171+
#endif // EbAppString_h
172+
/* File EOF */

Source/Lib/Codec/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ set(Codec_Source
9191
EbSei.c
9292
EbTransforms.c
9393
EbTransQuantBuffers.c
94+
EbString.c
9495
EbUtility.c)
9596

9697
if(COMPILE_AS_CPP)
@@ -187,6 +188,7 @@ add_library(SvtHevcEnc
187188
EbTransforms.h
188189
EbTransformUnit.h
189190
EbTransQuantBuffers.h
191+
EbString.h
190192
EbUtility.h)
191193

192194
if(UNIX)

0 commit comments

Comments
 (0)