本文整理汇总了C++中safe_strlen函数的典型用法代码示例。如果您正苦于以下问题:C++ safe_strlen函数的具体用法?C++ safe_strlen怎么用?C++ safe_strlen使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了safe_strlen函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: replace_char
/*
* Replace all 'c' characters in string 'src' with the subtsring 'rep'
* The returned string is allocated and must be freed by the caller.
*/
char* replace_char(const char* src, const char c, const char* rep)
{
size_t i, j, k, count=0, str_len = safe_strlen(src), rep_len = safe_strlen(rep);
char* res;
if ((src == NULL) || (rep == NULL))
return NULL;
for (i=0; i<str_len; i++) {
if (src[i] == c)
count++;
}
res = (char*)malloc(str_len + count*rep_len + 1);
if (res == NULL)
return NULL;
for (i=0,j=0; i<str_len; i++) {
if (src[i] == c) {
for(k=0; k<rep_len; k++)
res[j++] = rep[k];
} else {
res[j++] = src[i];
}
}
res[j] = 0;
return res;
}
示例2: safe_cat_string
/** A safe cat of a string.
*/
void safe_cat_string(char* dest, size_t dest_len, const char* cat)
{
size_t current_len = safe_strlen(dest);
size_t input_len = safe_strlen(cat);
if (dest_len < current_len + input_len + 1) {
printf("Error catting string!\n");
return;
}
memcpy(dest + current_len, cat, input_len);
dest[current_len + input_len] = '\0';
}
示例3: get_link_type
static link_type
get_link_type(cmark_node *node)
{
size_t title_len, url_len;
cmark_node *link_text;
char *realurl;
int realurllen;
bool isemail = false;
if (node->type != CMARK_NODE_LINK) {
return NO_LINK;
}
const char* url = cmark_node_get_url(node);
cmark_chunk url_chunk = cmark_chunk_literal(url);
url_len = safe_strlen(url);
if (url_len == 0 || scan_scheme(&url_chunk, 0) == 0) {
return NO_LINK;
}
const char* title = cmark_node_get_title(node);
title_len = safe_strlen(title);
// if it has a title, we can't treat it as an autolink:
if (title_len > 0) {
return NORMAL_LINK;
}
link_text = node->first_child;
cmark_consolidate_text_nodes(link_text);
realurl = (char*)url;
realurllen = url_len;
if (strncmp(realurl, "mailto:", 7) == 0) {
realurl += 7;
realurllen -= 7;
isemail = true;
}
if (realurllen == link_text->as.literal.len &&
strncmp(realurl,
(char*)link_text->as.literal.data,
link_text->as.literal.len) == 0) {
if (isemail) {
return EMAIL_AUTOLINK;
} else {
return URL_AUTOLINK;
}
} else {
return NORMAL_LINK;
}
}
示例4: shortest_unused_backtick_sequence
static int
shortest_unused_backtick_sequence(const char *code)
{
int32_t used = 1;
int current = 0;
size_t i = 0;
size_t code_len = safe_strlen(code);
while (i <= code_len) {
if (code[i] == '`') {
current++;
} else {
if (current) {
used |= (1 << current);
}
current = 0;
}
i++;
}
// return number of first bit that is 0:
i = 0;
while (used & 1) {
used = used >> 1;
i++;
}
return i;
}
示例5: open_loc_file
/*
* Open a localization file and store its file name, with special case
* when dealing with the embedded loc file.
*/
FILE* open_loc_file(const char* filename)
{
FILE* fd = NULL;
wchar_t *wfilename = NULL;
const char* tmp_ext = ".tmp";
if (filename == NULL)
return NULL;
if (loc_filename != embedded_loc_filename) {
safe_free(loc_filename);
}
if (safe_strcmp(tmp_ext, &filename[safe_strlen(filename)-4]) == 0) {
loc_filename = embedded_loc_filename;
} else {
loc_filename = safe_strdup(filename);
}
wfilename = utf8_to_wchar(filename);
if (wfilename == NULL) {
uprintf(conversion_error, filename);
goto out;
}
fd = _wfopen(wfilename, L"rb");
if (fd == NULL) {
uprintf("localization: could not open '%s'\n", filename);
}
out:
safe_free(wfilename);
return fd;
}
示例6: GetUnusedDriveLetter
/*
* Return the next unused drive letter from the system
*/
char GetUnusedDriveLetter(void)
{
DWORD size;
char drive_letter = 'Z'+1, *drive, drives[26*4 + 1]; /* "D:\", "E:\", etc., plus one NUL */
size = GetLogicalDriveStringsA(sizeof(drives), drives);
if (size == 0) {
uprintf("GetLogicalDriveStrings failed: %s\n", WindowsErrorString());
goto out;
}
if (size > sizeof(drives)) {
uprintf("GetLogicalDriveStrings: Buffer too small (required %d vs. %d)\n", size, sizeof(drives));
goto out;
}
for (drive_letter = 'C'; drive_letter < 'Z'; drive_letter++) {
for (drive = drives ;*drive; drive += safe_strlen(drive)+1) {
if (!isalpha(*drive))
continue;
if (drive_letter == (char)toupper((int)*drive))
break;
}
if (!*drive)
break;
}
out:
return (drive_letter>'Z')?0:drive_letter;
}
示例7: GetLastError
const char *windows_error_str(DWORD retval)
{
static char err_string[ERR_BUFFER_SIZE];
DWORD error_code, format_error;
DWORD size;
ssize_t i;
error_code = retval ? retval : GetLastError();
safe_sprintf(err_string, ERR_BUFFER_SIZE, "[%u] ", (unsigned int)error_code);
// Translate codes returned by SetupAPI. The ones we are dealing with are either
// in 0x0000xxxx or 0xE000xxxx and can be distinguished from standard error codes.
// See http://msdn.microsoft.com/en-us/library/windows/hardware/ff545011.aspx
switch (error_code & 0xE0000000) {
case 0:
error_code = HRESULT_FROM_WIN32(error_code); // Still leaves ERROR_SUCCESS unmodified
break;
case 0xE0000000:
error_code = 0x80000000 | (FACILITY_SETUPAPI << 16) | (error_code & 0x0000FFFF);
break;
default:
break;
}
size = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error_code,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), &err_string[safe_strlen(err_string)],
ERR_BUFFER_SIZE - (DWORD)safe_strlen(err_string), NULL);
if (size == 0) {
format_error = GetLastError();
if (format_error)
safe_sprintf(err_string, ERR_BUFFER_SIZE,
"Windows error code %u (FormatMessage error code %u)",
(unsigned int)error_code, (unsigned int)format_error);
else
safe_sprintf(err_string, ERR_BUFFER_SIZE, "Unknown error code %u", (unsigned int)error_code);
}
else {
// Remove CR/LF terminators
for (i = safe_strlen(err_string) - 1; (i >= 0) && ((err_string[i] == 0x0A) || (err_string[i] == 0x0D)); i--)
err_string[i] = 0;
}
return err_string;
}
示例8: SetUpdateCheck
/*
* Initial update check setup
*/
BOOL SetUpdateCheck(void)
{
BOOL enable_updates;
DWORD commcheck = GetTickCount();
notification_info more_info = { IDD_UPDATE_POLICY, UpdateCallback };
char filename[MAX_PATH] = "", exename[] = APPLICATION_NAME ".exe";
size_t fn_len, exe_len;
// Test if we have access to the registry. If not, forget it.
WriteRegistryKey32(REGKEY_HKCU, REGKEY_COMM_CHECK, commcheck);
if (ReadRegistryKey32(REGKEY_HKCU, REGKEY_COMM_CHECK) != commcheck)
return FALSE;
reg_commcheck = TRUE;
// If the update interval is not set, this is the first time we run so prompt the user
if (ReadRegistryKey32(REGKEY_HKCU, REGKEY_UPDATE_INTERVAL) == 0) {
// Add a hack for people who'd prefer the app not to prompt about update settings on first run.
// If the executable is called "<app_name>.exe", without version, we disable the prompt
GetModuleFileNameU(NULL, filename, sizeof(filename));
fn_len = safe_strlen(filename);
exe_len = safe_strlen(exename);
if ((fn_len > exe_len) && (safe_stricmp(&filename[fn_len-exe_len], exename) == 0)) {
dprintf("Short name used - Disabling initial update policy prompt\n");
enable_updates = TRUE;
} else {
enable_updates = notification(MSG_QUESTION, &more_info, APPLICATION_NAME " update policy",
"Do you want to allow " APPLICATION_NAME " to check for application updates online?");
}
if (!enable_updates) {
WriteRegistryKey32(REGKEY_HKCU, REGKEY_UPDATE_INTERVAL, -1);
return FALSE;
}
// If the user hasn't set the interval in the dialog, set to default
if ( (ReadRegistryKey32(REGKEY_HKCU, REGKEY_UPDATE_INTERVAL) == 0) ||
((ReadRegistryKey32(REGKEY_HKCU, REGKEY_UPDATE_INTERVAL) == -1) && enable_updates) )
WriteRegistryKey32(REGKEY_HKCU, REGKEY_UPDATE_INTERVAL, 86400);
}
return TRUE;
}
示例9: fix_config
// Apply various workarounds to Linux config files
static void fix_config(const char* psz_fullpath, const char* psz_path, const char* psz_basename, EXTRACT_PROPS* props)
{
size_t i, nul_pos;
char *iso_label = NULL, *usb_label = NULL, *src, *dst;
nul_pos = safe_strlen(psz_fullpath);
src = safe_strdup(psz_fullpath);
if (src == NULL)
return;
for (i=0; i<nul_pos; i++)
if (src[i] == '/') src[i] = '\\';
// Workaround for config files requiring an ISO label for kernel append that may be
// different from our USB label. Oh, and these labels must have spaces converted to \x20.
if ((props->is_syslinux_cfg) || (props->is_grub_cfg)) {
iso_label = replace_char(img_report.label, ' ', "\\x20");
usb_label = replace_char(img_report.usb_label, ' ', "\\x20");
if ((iso_label != NULL) && (usb_label != NULL)) {
if (replace_in_token_data(src, (props->is_syslinux_cfg) ? "append" : "linuxefi", iso_label, usb_label, TRUE) != NULL)
uprintf(" Patched %s: '%s' ⇨ '%s'\n", src, iso_label, usb_label);
}
safe_free(iso_label);
safe_free(usb_label);
}
// Fix dual BIOS + EFI support for tails and other ISOs
if ( (props->is_syslinux_cfg) && (safe_stricmp(psz_path, efi_dirname) == 0) &&
(safe_stricmp(psz_basename, syslinux_cfg[0]) == 0) &&
(!img_report.has_efi_syslinux) && (dst = safe_strdup(src)) ) {
dst[nul_pos-12] = 's'; dst[nul_pos-11] = 'y'; dst[nul_pos-10] = 's';
CopyFileA(src, dst, TRUE);
uprintf("Duplicated %s to %s\n", src, dst);
free(dst);
}
// Workaround for FreeNAS
if (props->is_grub_cfg) {
iso_label = malloc(MAX_PATH);
usb_label = malloc(MAX_PATH);
if ((iso_label != NULL) && (usb_label != NULL)) {
safe_sprintf(iso_label, MAX_PATH, "cd9660:/dev/iso9660/%s", img_report.label);
safe_sprintf(usb_label, MAX_PATH, "msdosfs:/dev/msdosfs/%s", img_report.usb_label);
if (replace_in_token_data(src, "set", iso_label, usb_label, TRUE) != NULL)
uprintf(" Patched %s: '%s' ⇨ '%s'\n", src, iso_label, usb_label);
}
safe_free(iso_label);
safe_free(usb_label);
}
free(src);
}
示例10: get_sanitized_token_data_buffer
static __inline char* get_sanitized_token_data_buffer(const char* token, unsigned int n, const char* buffer, size_t buffer_size)
{
size_t i;
char* data = get_token_data_buffer(token, n, buffer, buffer_size);
if (data != NULL) {
for (i=0; i<safe_strlen(data); i++) {
if ((data[i] == '\\') && (data[i+1] == 'n')) {
data[i] = '\r';
data[i+1] = '\n';
}
}
}
return data;
}
示例11: sanitize_filename
// Ensure filenames do not contain invalid FAT32 or NTFS characters
static __inline BOOL sanitize_filename(char* filename)
{
size_t i, j;
BOOL ret = FALSE;
char unauthorized[] = {'<', '>', ':', '|', '*', '?'};
// Must start after the drive part (D:\...) so that we don't eliminate the first column
for (i=2; i<safe_strlen(filename); i++) {
for (j=0; j<sizeof(unauthorized); j++) {
if (filename[i] == unauthorized[j]) {
filename[i] = '_';
ret = TRUE;
}
}
}
return ret;
}
示例12: pt_changes_feed_build_url
/** build a URL, returned char* needs to be free'd */
char* pt_changes_feed_build_url(const char* server_name,
const char* db,
const pt_changes_feed handle)
{
char *ret_str;
char* end_of_string;
size_t size_of_string = safe_strlen(server_name) +
safe_strlen(db) +
safe_strlen(handle->extra_opts) +
safe_strlen("//_changes?heartbeat=&feed=continuous") + 2048;
ret_str = calloc(1, size_of_string);
// Add the changes feed base URL
snprintf(ret_str, size_of_string, "%s/%s/_changes", server_name, db);
if (!handle->continuous && !handle->heartbeats &&
!safe_strlen(handle->extra_opts)) return ret_str;
// If we get here, it means that we have added options. There is *no*
// checking at this point!
safe_cat_string(ret_str, size_of_string, "?");
// Hearbeat
if (handle->heartbeats) {
snprintf(ret_str + safe_strlen(ret_str),size_of_string - safe_strlen(ret_str),
"heartbeat=%i&",handle->heartbeats);
}
// Continuous
if (handle->continuous) safe_cat_string(ret_str, size_of_string, "feed=continuous&");
// Adding extra options passed by the user
if (safe_strlen(handle->extra_opts)) safe_cat_string(ret_str, size_of_string, handle->extra_opts);
// If we have an ampersand on the end, remove it
end_of_string = &ret_str[safe_strlen(ret_str)-1];
if (*end_of_string == '&') *end_of_string = '\0';
return ret_str;
}
示例13: parse_update
/*
* Parse an update data file and populates a rufus_update structure.
* NB: since this is remote data, and we're running elevated, it *IS* considered
* potentially malicious, even if it comes from a supposedly trusted server.
* len should be the size of the buffer, including the zero terminator
*/
void parse_update(char* buf, size_t len)
{
size_t i;
char *data = NULL, *token;
char allowed_rtf_chars[] = "abcdefghijklmnopqrstuvwxyz|~-_:*'";
char allowed_std_chars[] = "\r\n ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!\"$%^&+=<>(){}[].,;#@/?";
// strchr includes the NUL terminator in the search, so take care of backslash before NUL
if ((buf == NULL) || (len < 2) || (len > 65536) || (buf[len-1] != 0) || (buf[len-2] == '\\'))
return;
// Sanitize the data - Not a silver bullet, but it helps
len = safe_strlen(buf)+1; // Someone may be inserting NULs
for (i=0; i<len-1; i++) {
// Check for valid RTF sequences as well as allowed chars if not RTF
if (buf[i] == '\\') {
// NB: we have a zero terminator, so we can afford a +1 without overflow
if (strchr(allowed_rtf_chars, buf[i+1]) == NULL) {
buf[i] = ' ';
}
} else if ((strchr(allowed_rtf_chars, buf[i]) == NULL) && (strchr(allowed_std_chars, buf[i]) == NULL)) {
buf[i] = ' ';
}
}
for (i=0; i<3; i++)
update.version[i] = 0;
update.platform_min[0] = 5;
update.platform_min[1] = 2; // XP or later
safe_free(update.download_url);
safe_free(update.release_notes);
if ((data = get_sanitized_token_data_buffer("version", 1, buf, len)) != NULL) {
for (i=0; (i<3) && ((token = strtok((i==0)?data:NULL, ".")) != NULL); i++) {
update.version[i] = (uint16_t)atoi(token);
}
safe_free(data);
}
if ((data = get_sanitized_token_data_buffer("platform_min", 1, buf, len)) != NULL) {
for (i=0; (i<2) && ((token = strtok((i==0)?data:NULL, ".")) != NULL); i++) {
update.platform_min[i] = (uint32_t)atoi(token);
}
safe_free(data);
}
update.download_url = get_sanitized_token_data_buffer("download_url", 1, buf, len);
update.release_notes = get_sanitized_token_data_buffer("release_notes", 1, buf, len);
}
示例14: longest_backtick_sequence
static int longest_backtick_sequence(const char *code) {
int longest = 0;
int current = 0;
size_t i = 0;
size_t code_len = safe_strlen(code);
while (i <= code_len) {
if (code[i] == '`') {
current++;
} else {
if (current > longest) {
longest = current;
}
current = 0;
}
i++;
}
return longest;
}
示例15: process_syslog
/*
* Send individual lines of the syslog section pointed by buffer back to the main application
* xbuffer's payload MUST start at byte 1 to accomodate the SYSLOG_MESSAGE prefix
*/
DWORD process_syslog(char* buffer, DWORD size)
{
DWORD i, write_size, junk, start = 0;
char* xbuffer;
char* ins_string = "<ins>";
char conversion_error[] = " ERROR: Unable to convert log entry to UTF-8";
if (buffer == NULL) return 0;
// CR/LF breakdown
for (i=0; i<size; i++) {
if ((buffer[i] == 0x0D) || (buffer[i] == 0x0A)) {
write_size = i-start + 1;
do {
buffer[i++] = 0;
} while ( ((buffer[i] == 0x0D) || (buffer[i] == 0x0A)) && (i <= size) );
// The setupapi.dev.log uses a dubious method to mark its current position
// If there's any "<ins>" line in any log file, it's game over then
if (safe_strcmp(ins_string, buffer + start) == 0) {
return start;
}
// The logs are using the system locale. Convert to UTF8 (with extra leading byte)
xbuffer = xlocale_to_utf8(&buffer[start]);
if (xbuffer == NULL) {
xbuffer = conversion_error;
}
// This is where we use the extra start byte
xbuffer[0] = IC_SYSLOG_MESSAGE;
WriteFile(pipe_handle, xbuffer, (DWORD)safe_strlen(&xbuffer[1])+2, &junk, NULL);
if (xbuffer != conversion_error) {
free(xbuffer);
}
start = i;
}
}
// start does not necessarily equate size, if there are truncated lines at the end
return start;
}