Skip to content

Commit

Permalink
C99 inline PoC: Inline strnzcpy() and strnzcpyn()
Browse files Browse the repository at this point in the history
Closes #5620
  • Loading branch information
magnumripper committed Dec 25, 2024
1 parent b11fd1d commit adab3ac
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 35 deletions.
5 changes: 2 additions & 3 deletions src/dmg2john.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@

#include "arch.h"
#include "filevault.h"
#include "misc.h"
#include "jumbo.h"
#include "memory.h"
#include "johnswap.h"
Expand Down Expand Up @@ -104,7 +103,7 @@ static void print_hex(unsigned char *str, int len)
// to link dmg2john.o with misc.o results in linking errors, as parts of misc.o
// depends on other parts of JtR jumbo. These linking problems are not easily
// solvable without refactoring misc.c file.
int strnzcpyn(char *dst, const char *src, int size)
static int strnzcpyn(char *dst, const char *src, int size)
{
char *dptr;
if (!size) return 0;
Expand All @@ -118,7 +117,7 @@ int strnzcpyn(char *dst, const char *src, int size)
return (dptr-dst);
}

char *strnzcat(char *dst, const char *src, int size)
static char *strnzcat(char *dst, const char *src, int size)
{
char *dptr = dst;

Expand Down
33 changes: 3 additions & 30 deletions src/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,21 +296,8 @@ char *strnfcpy(char *dst, const char *src, int size)
return dst;
}

char *strnzcpy(char *dst, const char *src, int size)
{
char *dptr;

if (size < 1)
return dst;
dptr = dst;

while (--size)
if (!(*dptr++ = *src++))
return dst;
*dptr = 0;

return dst;
}
/* C99-style inlining: Code in .h and extern declaration in .c */
extern inline char *strnzcpy(char *dst, const char *src, int size);

char *strnzcpylwr(char *dst, const char *src, int size)
{
Expand All @@ -336,21 +323,7 @@ char *strnzcpylwr(char *dst, const char *src, int size)
return dst;
}

int strnzcpyn(char *dst, const char *src, int size)
{
char *dptr;

if (size < 1)
return 0;
dptr = dst;

while (--size)
if (!(*dptr++ = *src++))
return (dptr-dst)-1;
*dptr = 0;

return (dptr-dst);
}
extern inline int strnzcpyn(char *dst, const char *src, int size);

int strnzcpylwrn(char *dst, const char *src, int size)
{
Expand Down
32 changes: 30 additions & 2 deletions src/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,21 @@ extern char *strnfcpy(char *dst, const char *src, int size);
/*
* Similar to the above, but always NUL terminates the string.
*/
extern char *strnzcpy(char *dst, const char *src, int size);
inline char *strnzcpy(char *dst, const char *src, int size)
{
char *dptr;

if (size < 1)
return dst;
dptr = dst;

while (--size)
if (!(*dptr++ = *src++))
return dst;
*dptr = 0;

return dst;
}

/*
* Similar to the above, but also converts to lowercase in a single pass
Expand All @@ -124,7 +138,21 @@ extern char *strnzcpylwr(char *dst, const char *src, int size);
/*
* Similar to the strnzcpy, but returns the length of the string.
*/
extern int strnzcpyn(char *dst, const char *src, int size);
inline int strnzcpyn(char *dst, const char *src, int size)
{
char *dptr;

if (size < 1)
return 0;
dptr = dst;

while (--size)
if (!(*dptr++ = *src++))
return (dptr-dst)-1;
*dptr = 0;

return (dptr-dst);
}

/*
* Similar to the strnzcpylwr, but returns the length of the string.
Expand Down

0 comments on commit adab3ac

Please sign in to comment.