iPXE
Functions
string.c File Reference

String functions. More...

#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
void * generic_memset (void *dest, int character, size_t len)
 Fill memory region.
void * generic_memcpy (void *dest, const void *src, size_t len)
 Copy memory region.
void * generic_memmove (void *dest, const void *src, size_t len)
 Copy (possibly overlapping) memory region.
int memcmp (const void *first, const void *second, size_t len)
 Compare memory regions.
void * memchr (const void *src, int character, size_t len)
 Find character within a memory region.
void * memswap (void *first, void *second, size_t len)
 Swap memory regions.
int strcmp (const char *first, const char *second)
 Compare strings.
int strncmp (const char *first, const char *second, size_t max)
 Compare strings.
int strcasecmp (const char *first, const char *second)
 Compare case-insensitive strings.
size_t strlen (const char *src)
 Get length of string.
size_t strnlen (const char *src, size_t max)
 Get length of string.
char * strchr (const char *src, int character)
 Find character within a string.
char * strrchr (const char *src, int character)
 Find rightmost character within a string.
char * strstr (const char *haystack, const char *needle)
 Find substring.
char * strcpy (char *dest, const char *src)
 Copy string.
char * strncpy (char *dest, const char *src, size_t max)
 Copy string.
char * strcat (char *dest, const char *src)
 Concatenate string.
char * strdup (const char *src)
 Duplicate string.
char * strndup (const char *src, size_t max)
 Duplicate string.
unsigned int digit_value (unsigned int character)
 Calculate digit value.
static const char * strtoul_pre (const char *string, int *negate, int *base)
 Preprocess string for strtoul() or strtoull()
unsigned long strtoul (const char *string, char **endp, int base)
 Convert string to numeric value.
unsigned long long strtoull (const char *string, char **endp, int base)
 Convert string to numeric value.

Detailed Description

String functions.

Definition in file string.c.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
void* generic_memset ( void *  dest,
int  character,
size_t  len 
)

Fill memory region.

Parameters:
destDestination region
characterFill character
lenLength
Return values:
destDestination region

Definition at line 46 of file string.c.

References dest.

                                                                {
        uint8_t *dest_bytes = dest;

        while ( len-- )
                *(dest_bytes++) = character;
        return dest;
}
void* generic_memcpy ( void *  dest,
const void *  src,
size_t  len 
)

Copy memory region.

Parameters:
destDestination region
srcSource region
lenLength
Return values:
destDestination region

Definition at line 62 of file string.c.

References dest, and src.

Referenced by generic_memmove().

                                                                  {
        const uint8_t *src_bytes = src;
        uint8_t *dest_bytes = dest;

        while ( len-- )
                *(dest_bytes++) = *(src_bytes++);
        return dest;
}
void* generic_memmove ( void *  dest,
const void *  src,
size_t  len 
)

Copy (possibly overlapping) memory region.

Parameters:
destDestination region
srcSource region
lenLength
Return values:
destDestination region

Definition at line 79 of file string.c.

References dest, generic_memcpy(), and len.

                                                                   {
        const uint8_t *src_bytes = ( src + len );
        uint8_t *dest_bytes = ( dest + len );

        if ( dest < src )
                return generic_memcpy ( dest, src, len );
        while ( len-- )
                *(--dest_bytes) = *(--src_bytes);
        return dest;
}
int memcmp ( const void *  first,
const void *  second,
size_t  len 
)

Compare memory regions.

Parameters:
firstFirst region
secondSecond region
lenLength
Return values:
diffDifference

Definition at line 98 of file string.c.

References first.

Referenced by apply_syslog_settings(), asn1_compare(), asn1_okx(), base16_decode_okx(), base64_decode_okx(), bofm_en(), cachedhcp_probe(), ccmp_decrypt(), check_bios_interrupts(), cipher_decrypt_okx(), cipher_encrypt_okx(), com32_identify(), deflate_okx(), dhcp_deliver(), dhcpv6_check_duid(), digest_frag_okx(), dns_copy_okx(), dns_encode_okx(), eapol_key_rx(), efi_block_boot_image(), efi_file_get_info(), efi_find_table(), efi_guid_ntoa(), efi_pxe_ip_filter(), efi_pxe_udp_read(), elf_load(), elfboot_probe(), eoib_find(), eoib_find_peer(), eoib_rx_av(), eth_probe(), fc_els_logo_logout(), fc_peer_examine(), fc_peer_get_port_id(), fc_peer_get_wwn(), fc_peer_login(), fc_port_deliver(), fc_port_login(), fc_ulp_login(), fcels_exec(), fcoe_fip_rx(), fcoe_fip_rx_advertisement(), fcoe_rx(), find_ibdev(), find_netdev_by_ll_addr(), hvm_cpuid_base(), ib_find_path_cache_entry(), ib_find_qp_mgid(), ib_mcast_detach(), ib_mi_handle(), ib_path_complete(), inet6_aton_okx(), int13_parse_eltorito(), int13con_find(), ipoib_find_remac(), ipoib_map_remac(), ipv6_has_addr(), ipv6_is_fragment(), ipv6_route_okx(), is_autoboot_ll_addr(), iscsi_handle_chap_r_value(), linda_verify_uc_ram(), loopback_wait(), mac_address_from_string_specs(), memcpy_test_speed(), mlx_memory_cmp_priv(), ne_probe(), neighbour_find(), net80211_rx(), ntlm_authenticate_okx(), ntlm_data_okx(), ntlm_key_okx(), nvs_verify(), ocsp_compare_responder_key_hash(), pcnet32_setup_mac_addr(), peerblk_done(), peerblk_parse_block(), peerdist_discovery_reply(), peerdist_info_block_okx(), peerdist_info_passphrase_okx(), peerdist_info_segment_okx(), pem_marker(), png_probe(), rsa_verify(), rsdp_find_rsdt_range(), sandev_parse_iso9660(), script_probe(), slam_pull_header(), string_test_exec(), strstr(), tftp_rx(), tkip_decrypt(), tkip_mix_1(), tls_new_ciphertext(), tls_new_finished(), tls_new_server_hello(), trivial_memcmp_user(), udp_demux(), vmbus_find_driver(), wpa_derive_ptk(), wpa_handle_3_of_4(), x509_chain_okx(), x509_check_dnsname(), x509_check_ipaddress(), x509_check_root(), x509_fingerprint_okx(), xcm_find(), and xve_find().

                                                                 {
        const uint8_t *first_bytes = first;
        const uint8_t *second_bytes = second;
        int diff;

        while ( len-- ) {
                diff = ( *(second_bytes++) - *(first_bytes++) );
                if ( diff )
                        return diff;
        }
        return 0;
}
void* memchr ( const void *  src,
int  character,
size_t  len 
)

Find character within a memory region.

Parameters:
srcSource region
characterCharacter to find
lenLength
Return values:
foundFound character, or NULL if not found

Definition at line 119 of file string.c.

References NULL, and src.

Referenced by line_buffer(), rsa_decrypt(), string_test_exec(), and trivial_memchr_user().

                                                             {
        const uint8_t *src_bytes = src;

        for ( ; len-- ; src_bytes++ ) {
                if ( *src_bytes == character )
                        return ( ( void * ) src_bytes );
        }
        return NULL;
}
void* memswap ( void *  first,
void *  second,
size_t  len 
)

Swap memory regions.

Parameters:
firstFirst region
secondSecond region
lenLength
Return values:
firstFirst region

Definition at line 137 of file string.c.

References first.

Referenced by arp_rx(), gdbudp_recv(), string_test_exec(), and tls_change_cipher().

                                                         {
        uint8_t *first_bytes = first;
        uint8_t *second_bytes = second;
        uint8_t temp;

        for ( ; len-- ; first_bytes++, second_bytes++ ) {
                temp = *first_bytes;
                *first_bytes = *second_bytes;
                *second_bytes = temp;
        }
        return first;
}
int strcmp ( const char *  first,
const char *  second 
)
int strncmp ( const char *  first,
const char *  second,
size_t  max 
)

Compare strings.

Parameters:
firstFirst string
secondSecond string
maxMaximum length to compare
Return values:
diffDifference

Definition at line 170 of file string.c.

Referenced by dhcp_discovery_rx(), http_parse_status(), iscsi_handle_string(), match_long_option(), nfs_uri_symlink(), parse_uri(), peerdist_discovery_reply_tag(), strcmp(), string_test_exec(), and undinet_probe().

                                                                  {
        const uint8_t *first_bytes = ( ( const uint8_t * ) first );
        const uint8_t *second_bytes = ( ( const uint8_t * ) second );
        int diff;

        for ( ; max-- ; first_bytes++, second_bytes++ ) {
                diff = ( *second_bytes - *first_bytes );
                if ( diff )
                        return diff;
                if ( ! *first_bytes )
                        return 0;
        }
        return 0;
}
int strcasecmp ( const char *  first,
const char *  second 
)

Compare case-insensitive strings.

Parameters:
firstFirst string
secondSecond string
Return values:
diffDifference

Definition at line 192 of file string.c.

References toupper().

Referenced by com32_identify(), comboot_identify(), efi_file_find(), efi_local_check_volume_name(), efi_snp_hii_fetch(), efi_snp_hii_store(), http_authentication(), http_digest_authenticate(), http_format_metadata_flavor(), http_parse_connection(), http_parse_content_encoding(), http_parse_digest_auth(), http_parse_header(), http_parse_transfer_encoding(), parse_fc_els_handler(), string_test_exec(), sundance_probe(), and tftp_process_option().

                                                         {
        const uint8_t *first_bytes = ( ( const uint8_t * ) first );
        const uint8_t *second_bytes = ( ( const uint8_t * ) second );
        int diff;

        for ( ; ; first_bytes++, second_bytes++ ) {
                diff = ( toupper ( *second_bytes ) -
                         toupper ( *first_bytes ) );
                if ( diff )
                        return diff;
                if ( ! *first_bytes )
                        return 0;
        }
}
size_t strlen ( const char *  src)

Get length of string.

Parameters:
srcString
Return values:
lenLength

Definition at line 213 of file string.c.

References strnlen().

Referenced by _print_label(), add_menu_item(), add_parameter(), autovivify_child_settings(), base16_decoded_max_len(), base16_encode_okx(), base64_decoded_max_len(), base64_encode_okx(), bzimage_load_initrd(), bzimage_set_cmdline(), cmdline_init(), comboot_copy_cmdline(), concat_args(), create_menu(), create_parameters(), dns_decode_okx(), dns_list_okx(), draw_editbox(), draw_menu_item(), edit_string(), efi_block_boot_image(), efi_file_varlen(), efi_ifr_package(), efi_image_cmdline(), efi_image_path(), efi_local_open_resolved(), efi_snp_hii_append(), efi_snp_hii_store(), generic_settings_store(), guestinfo_fetch_type(), guestrpc_command(), http_digest_update(), http_format_basic_auth(), http_parse_headers(), http_parse_ntlm_auth(), ib_srp_parse_byte_string(), ib_srp_parse_root_path(), ibft_set_string(), init_editbox(), insert_delete(), ipv6_sock_aton(), iscsi_handle_chap_c_value(), iscsi_handle_chap_i_value(), iscsi_handle_chap_r_value(), iscsi_parse_root_path(), iwlist(), linebuf_accumulated_okx(), login_ui(), match_long_option(), net80211_marshal_request_info(), netdev_fetch_bustype(), netdev_fetch_chip(), netdev_fetch_ifname(), nfs_uri_symlink(), parse_net_args(), parse_settings_name(), parse_string_setting(), parse_uri(), peerblk_retrieval_uri(), peerdisc_create(), peerdisc_discovered(), peerdisc_socket_rx(), peerdisc_socket_tx(), peerdist_discovery_reply(), peerdist_discovery_reply_tag(), peerdist_discovery_reply_values(), pem_marker(), prf_sha1(), pxenv_file_cmdline(), resolv(), resolv_setting(), resolve_path(), show_menu(), slam_parse_multicast_address(), snprintf_okx(), strcat(), string_copy(), string_test_exec(), strstr(), sync_console(), tftp_process_multicast(), tftp_send_error(), tftp_send_rrq(), tls_send_client_hello(), trivial_strlen_user(), uri_copy_fields(), uri_decode_inplace(), uri_encode_string(), uri_format_okx(), uri_pxe_okx(), validator_start_download(), version_fetch(), wpa_psk_start(), x509_check_dnsname(), xenbus_probe(), xenbus_probe_device(), xenbus_probe_type(), xenstore_dump(), xenstore_request(), xenstore_response(), and xenstore_send_string().

                                  {

        return strnlen ( src, ~( ( size_t ) 0 ) );
}
size_t strnlen ( const char *  src,
size_t  max 
)

Get length of string.

Parameters:
srcString
maxMaximum length
Return values:
lenLength

Definition at line 225 of file string.c.

References len.

Referenced by efi_pecoff_debug_name(), iscsi_handle_strings(), string_test_exec(), strlen(), strndup(), tftp_rx_oack(), and used_len_string().

                                               {
        const uint8_t *src_bytes = ( ( const uint8_t * ) src );
        size_t len = 0;

        while ( max-- && *(src_bytes++) )
                len++;
        return len;
}
char* strchr ( const char *  src,
int  character 
)

Find character within a string.

Parameters:
srcString
characterCharacter to find
Return values:
foundFound character, or NULL if not found

Definition at line 241 of file string.c.

References NULL.

Referenced by base64_decode(), bzimage_load_initrd(), cmdline_strip(), comboot_fetch_kernel(), dns_resolv(), efi_pecoff_debug_name(), http_parse_header(), ipv6_sock_aton(), iscsi_handle_string(), iscsi_handle_targetaddress_value(), netbios_domain(), nfs_uri_init(), parse_setting_name(), parse_settings_name(), parse_uri(), slam_parse_multicast_address(), string_test_exec(), tftp_process_multicast(), uri_character_escaped(), and x509_check_dnsname().

                                                 {
        const uint8_t *src_bytes = ( ( const uint8_t * ) src );

        for ( ; ; src_bytes++ ) {
                if ( *src_bytes == character )
                        return ( ( char * ) src_bytes );
                if ( ! *src_bytes )
                        return NULL;
        }
}
char* strrchr ( const char *  src,
int  character 
)

Find rightmost character within a string.

Parameters:
srcString
characterCharacter to find
Return values:
foundFound character, or NULL if not found

Definition at line 259 of file string.c.

References NULL, and start.

Referenced by basename(), com32_identify(), comboot_identify(), dirname(), efi_pecoff_debug_name(), nfs_uri_next_mountpoint(), parse_uri(), and string_test_exec().

                                                  {
        const uint8_t *src_bytes = ( ( const uint8_t * ) src );
        const uint8_t *start = src_bytes;

        while ( *src_bytes )
                src_bytes++;
        for ( src_bytes-- ; src_bytes >= start ; src_bytes-- ) {
                if ( *src_bytes == character )
                        return ( ( char * ) src_bytes );
        }
        return NULL;
}
char* strstr ( const char *  haystack,
const char *  needle 
)

Find substring.

Parameters:
haystackString
needleSubstring
Return values:
foundFound substring, or NULL if not found

Definition at line 279 of file string.c.

References len, memcmp(), NULL, and strlen().

Referenced by bzimage_parse_cmdline(), bzimage_parse_cpio_cmdline(), cmdline_strip(), comboot_fetch_kernel(), parse_uri(), and string_test_exec().

                                                           {
        size_t len = strlen ( needle );

        for ( ; *haystack ; haystack++ ) {
                if ( memcmp ( haystack, needle, len ) == 0 )
                        return ( ( char * ) haystack );
        }
        return NULL;
}
char* strcpy ( char *  dest,
const char *  src 
)

Copy string.

Parameters:
destDestination string
srcSource string
Return values:
destDestination string

Definition at line 296 of file string.c.

References dest.

Referenced by add_menu_item(), add_parameter(), cmdline_strip(), create_menu(), create_parameters(), efi_ifr_package(), ib_srp_parse_root_path(), ibft_set_string(), inet6_ntoa(), iscsi_parse_root_path(), net80211_prepare_assoc(), net80211_probe_start(), net80211_probe_step(), nfs_uri_symlink(), peerdisc_discovered(), peerdist_discovery_reply(), resolv_setting(), strcat(), string_test_exec(), and tftp_send_error().

                                              {
        const uint8_t *src_bytes = ( ( const uint8_t * ) src );
        uint8_t *dest_bytes = ( ( uint8_t * ) dest );

        /* We cannot use strncpy(), since that would pad the destination */
        for ( ; ; src_bytes++, dest_bytes++ ) {
                *dest_bytes = *src_bytes;
                if ( ! *dest_bytes )
                        break;
        }
        return dest;
}
char* strncpy ( char *  dest,
const char *  src,
size_t  max 
)

Copy string.

Parameters:
destDestination string
srcSource string
maxMaximum length
Return values:
destDestination string

Definition at line 317 of file string.c.

References dest, and max.

Referenced by buildarch_fetch(), efi_block_install(), int13_install(), netdev_fetch_bustype(), netdev_fetch_chip(), netdev_fetch_ifname(), platform_fetch(), slk_set(), string_test_exec(), and version_fetch().

                                                           {
        const uint8_t *src_bytes = ( ( const uint8_t * ) src );
        uint8_t *dest_bytes = ( ( uint8_t * ) dest );

        for ( ; max ; max--, src_bytes++, dest_bytes++ ) {
                *dest_bytes = *src_bytes;
                if ( ! *dest_bytes )
                        break;
        }
        while ( max-- )
                *(dest_bytes++) = '\0';
        return dest;
}
char* strcat ( char *  dest,
const char *  src 
)

Concatenate string.

Parameters:
destDestination string
srcSource string
Return values:
destDestination string

Definition at line 338 of file string.c.

References dest, strcpy(), and strlen().

Referenced by _print_label(), and string_test_exec().

                                              {

        strcpy ( ( dest + strlen ( dest ) ), src );
        return dest;
}
char* strdup ( const char *  src)

Duplicate string.

Parameters:
srcSource string
Return values:
dupDuplicated string, or NULL if allocation failed

Definition at line 350 of file string.c.

References strndup().

Referenced by expand_settings(), history_append(), history_store(), http_parse_ntlm_auth(), image_set_cmdline(), image_set_name(), ipv6_sock_aton(), iscsi_handle_targetaddress_value(), iscsi_parse_root_path(), nfs_parse_uri(), nfs_uri_init(), nfs_uri_next_mountpoint(), oncrpc_init_cred_sys(), readline_history(), resolve_path(), string_test_exec(), and system().

                                  {

        return strndup ( src, ~( ( size_t ) 0 ) );
}
char* strndup ( const char *  src,
size_t  max 
)

Duplicate string.

Parameters:
srcSource string
maxMaximum length
Return values:
dupDuplicated string, or NULL if allocation failed

Definition at line 362 of file string.c.

References len, malloc(), memcpy(), and strnlen().

Referenced by nfs_deliver(), strdup(), and string_test_exec().

                                               {
        size_t len = strnlen ( src, max );
        char *dup;

        dup = malloc ( len + 1 /* NUL */ );
        if ( dup ) {
                memcpy ( dup, src, len );
                dup[len] = '\0';
        }
        return dup;
}
unsigned int digit_value ( unsigned int  character)

Calculate digit value.

Parameters:
characterDigit character
Return values:
digitDigit value

Invalid digits will be returned as a value greater than or equal to the numeric base.

Definition at line 383 of file string.c.

Referenced by hex_decode(), string_test_exec(), strtoul(), and strtoull().

                                                    {

        if ( character >= 'a' )
                return ( character - ( 'a' - 10 ) );
        if ( character >= 'A' )
                return ( character - ( 'A' - 10 ) );
        if ( character <= '9' )
                return ( character - '0' );
        return character;
}
static const char* strtoul_pre ( const char *  string,
int *  negate,
int *  base 
) [static]

Preprocess string for strtoul() or strtoull()

Parameters:
stringString
negateFinal value should be negated
baseNumeric base
Return values:
stringRemaining string

Definition at line 402 of file string.c.

References isspace(), and string.

Referenced by strtoul(), and strtoull().

                                                                               {

        /* Skip any leading whitespace */
        while ( isspace ( *string ) )
                string++;

        /* Process arithmetic sign, if present */
        *negate = 0;
        if ( *string == '-' ) {
                string++;
                *negate = 1;
        } else if ( *string == '+' ) {
                string++;
        }

        /* Process base, if present */
        if ( *base == 0 ) {
                *base = 10;
                if ( *string == '0' ) {
                        string++;
                        *base = 8;
                        if ( ( *string & ~0x20 ) == 'X' ) {
                                string++;
                                *base = 16;
                        }
                }
        }

        return string;
}
unsigned long strtoul ( const char *  string,
char **  endp,
int  base 
)

Convert string to numeric value.

Parameters:
stringString
endpEnd pointer (or NULL)
baseNumeric base (or zero to autodetect)
Return values:
valueNumeric value

Definition at line 441 of file string.c.

References base, digit_value(), strtoul_pre(), and value.

Referenced by aoe_parse_uri(), bzimage_parse_cmdline(), bzimage_parse_cpio_cmdline(), efi_snp_hii_store(), fc_aton(), fc_id_aton(), ftp_parse_value(), ftp_reply(), gdbserial_init(), http_parse_content_length(), http_parse_retry_after(), http_parse_status(), http_rx_chunk_len(), ib_srp_parse_integer(), inet6_aton(), inet_aton(), iscsi_handle_chap_i_value(), iscsi_handle_targetaddress_value(), iscsi_parse_root_path(), mac_address_from_string_specs(), parse_int_setting(), parse_integer(), parse_setting_tag(), pnm_ascii(), scsi_parse_lun(), slam_parse_multicast_address(), string_test_exec(), tftp_process_blksize(), tftp_process_multicast(), tftp_process_tsize(), uri_decode(), uri_port(), and xenstore_read_num().

                                                                    {
        unsigned long value = 0;
        unsigned int digit;
        int negate;

        /* Preprocess string */
        string = strtoul_pre ( string, &negate, &base );

        /* Process digits */
        for ( ; ; string++ ) {
                digit = digit_value ( *string );
                if ( digit >= ( unsigned int ) base )
                        break;
                value = ( ( value * base ) + digit );
        }

        /* Negate value if, applicable */
        if ( negate )
                value = -value;

        /* Fill in end pointer, if applicable */
        if ( endp )
                *endp = ( ( char * ) string );

        return value;
}
unsigned long long strtoull ( const char *  string,
char **  endp,
int  base 
)

Convert string to numeric value.

Parameters:
stringString
endpEnd pointer (or NULL)
baseNumeric base (or zero to autodetect)
Return values:
valueNumeric value

Definition at line 476 of file string.c.

References base, digit_value(), strtoul_pre(), and value.

                                                                          {
        unsigned long long value = 0;
        unsigned int digit;
        int negate;

        /* Preprocess string */
        string = strtoul_pre ( string, &negate, &base );

        /* Process digits */
        for ( ; ; string++ ) {
                digit = digit_value ( *string );
                if ( digit >= ( unsigned int ) base )
                        break;
                value = ( ( value * base ) + digit );
        }

        /* Negate value if, applicable */
        if ( negate )
                value = -value;

        /* Fill in end pointer, if applicable */
        if ( endp )
                *endp = ( ( char * ) string );

        return value;
}