-
Notifications
You must be signed in to change notification settings - Fork 0
/
safe_string.cpp
133 lines (106 loc) · 2.69 KB
/
safe_string.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/******************************************************************************
*
* File: safe_string.cpp
* -----
*
* Copyright Statement:
* --------------------
* This software is protected by Copyright and the information contained
* herein is confidential.
* The software could not be copied and the information contained herein
* could not be used or disclosed except with the written permission of
* ...
*
* Project:
* --------
*
* Description:
* ------------
* The implementation of the Csafe_string class.
*
*
* Modification History:
* ---------------------
* Date Version Author Details
* ---- ------- ------ -------
* 1.1 ChenYao Original
*
******************************************************************************
*
*
*
*****************************************************************************/
#include "safe_string.h"
/*!
* Includes
*****************************************************************************/
#include <string.h>
#include <stdio.h>
#include <malloc.h>
#include <wchar.h>
int _cdecl safe_sprintf(char *buffer, size_t size,
const char *format, ...)
{
if (NULL == buffer
|| NULL == format
|| 0 >= size)
return -1;
memset(buffer, 0, size * sizeof(char));
va_list ap;
va_start(ap, format);
int res = -1;
res = _vsnprintf_s(buffer, size, _TRUNCATE,
format, ap);
va_end(ap);
return res;
}
int _cdecl safe_sprintf(wchar_t *buffer, size_t size,
const wchar_t *format, ...)
{
if (NULL == buffer
|| NULL == format
|| 0 >= size)
return -1;
memset(buffer, 0, size * sizeof(wchar_t));
va_list ap;
va_start(ap, format);
int res = -1;
res = _vsnwprintf_s(buffer, size, _TRUNCATE,
format, ap);
va_end(ap);
return res;
}
int _cdecl safe_overwrite(char *buffer, size_t size,
const char *format, ...)
{
if (NULL == buffer || NULL == format || 0 >= size)
return -1;
char *interim = (char*) calloc(size, sizeof(char));
va_list ap;
va_start(ap, format);
int res = -1;
res = _vsnprintf_s(interim, size, _TRUNCATE,
format, ap);
va_end(ap);
memset(buffer, 0, size * sizeof(char));
memcpy(buffer, interim, size);
free(interim), interim = NULL;
return res;
}
int _cdecl safe_overwrite(wchar_t *buffer, size_t size,
const wchar_t *format, ...)
{
if (NULL == buffer || NULL == format || 0 >= size)
return -1;
wchar_t *interim = (wchar_t*) calloc(size, sizeof(wchar_t));
va_list ap;
va_start(ap, format);
int res = -1;
res = _vsnwprintf_s(interim, size, _TRUNCATE,
format, ap);
va_end(ap);
wmemset(buffer, 0, size);
wmemcpy(buffer, interim, size);
free(interim), interim = NULL;
return res;
}