ASCIIZToMaxLenStr

Converts an ASCIIZ (null-terminated) string to a length-preceded string that is not longer than the specified maximum length.

Library:LibC
Service:Characters and Strings

Syntax

  #include <string.h> 
   
  int ASCIIZToMaxLenStr (
     char         *lenString,   
     const char   *ASCIIZstring,   
     size_t        maximumLength);
  

Parameters

lenString

(OUT) Points to the destination (length-preceded) string.

ASCIIZstring

(IN) Points to the source string in ASCIIZ format.

maximumLength

(IN) Specifies the maximum number of characters to place in the new string.

Return Values

If successful, returns 0; otherwise, returns -1.

Remarks

A length-preceded string has the length of the string in the first byte, followed by the characters in the string; it cannot exceed 255 characters. If the ASCIIZstring string is longer that 255 characters, this function returns -1, and lenString contains maximumLength characters. The remaining characters of the ASCIIZstring are not copied to lenString.

Because length-preceded strings only have one byte to store the size of the string, the maximum size of the string is 255. Passing a maximum size larger than 255 produces unpredictable results.

Example

  #include <string.h> 
  #include <errno.h> 
   
  main() 
  {  
     char srcString[256];  
     char destString[256];  
     int ccode;  
     size_t maxSize;  
     strcpy(srcString,"This is the message");  
     maxSize = 100;  
     ccode = ASCIIZToMaxLenStr(destString, srcString, maxSize);  
     if(ccode == ESUCCESS)  
        printf("The string fit\n");  
     else  
        printf("The string was too long to fit in the allotted
                space.\n");  
  }