Skip to content

Commit

Permalink
StrLib: add AsciiGetString and AsciiGetNextString
Browse files Browse the repository at this point in the history
this allows iterating over single-buffer string-arrays
like the ones used by fdt blobs.
  • Loading branch information
M1cha committed Apr 29, 2018
1 parent eeccb75 commit 10d19d5
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Include/Library/StrLib.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
#ifndef __LIBRARY_STR_H__
#define __LIBRARY_STR_H__

typedef struct {
CONST CHAR8 *Current;
CONST CHAR8 *End;
} ASCII_GETSTRING_STATE;

CHAR8 *strtok(CHAR8 *s, CONST CHAR8 *delim);
CHAR8 *strtok_r(CHAR8 *s, CONST CHAR8 *delim, CHAR8 **last);
UINTN strlcat(CHAR8 *dst, CONST CHAR8 *src, UINTN siz);
UINTN strspn(CHAR8 CONST *s, CHAR8 CONST *accept);
CHAR8 *strchr(CONST CHAR8 *s, INTN c);
CHAR8 *strpbrk(CONST CHAR8 *s, CONST CHAR8 *accept);

RETURN_STATUS
EFIAPI
AsciiGetNextString (
IN OUT ASCII_GETSTRING_STATE *State,
OUT CONST CHAR8 **String
);

RETURN_STATUS
EFIAPI
AsciiGetString (
IN CONST CHAR8 *StringArray,
IN UINTN MaxSize,
IN OUT ASCII_GETSTRING_STATE *State,
OUT CONST CHAR8 **String
);

#endif
2 changes: 2 additions & 0 deletions Library/StrLib/StrLib.inf
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
strchr.c
strpbrk.c

getstring.c

[Packages]
MdePkg/MdePkg.dec
EFIDroidPkg/EFIDroidPkg.dec
39 changes: 39 additions & 0 deletions Library/StrLib/getstring.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <Library/BaseLib.h>
#include <Library/StrLib.h>

RETURN_STATUS
EFIAPI
AsciiGetNextString (
IN OUT ASCII_GETSTRING_STATE *State,
OUT CONST CHAR8 **String
)
{
if (State->Current >= State->End) {
return RETURN_NOT_FOUND;
}

UINTN CurrentLength = AsciiStrnSizeS(State->Current, State->End - State->Current);
if (State->Current + CurrentLength > State->End) {
return RETURN_UNSUPPORTED;
}

*String = State->Current;
State->Current += CurrentLength;

return RETURN_SUCCESS;
}

RETURN_STATUS
EFIAPI
AsciiGetString (
IN CONST CHAR8 *StringArray,
IN UINTN MaxSize,
IN OUT ASCII_GETSTRING_STATE *State,
OUT CONST CHAR8 **String
)
{
State->Current = StringArray;
State->End = StringArray + MaxSize;

return AsciiGetNextString(State, String);
}

0 comments on commit 10d19d5

Please sign in to comment.