Skip to content

Instantly share code, notes, and snippets.

@kkevlar
Created May 19, 2018 18:48
Show Gist options
  • Save kkevlar/9454fc3aa5f9fd2e3e8888d34a23efe3 to your computer and use it in GitHub Desktop.
Save kkevlar/9454fc3aa5f9fd2e3e8888d34a23efe3 to your computer and use it in GitHub Desktop.
Packing for mytar
#define UID_PACKING_LEN 8
int header_insert_special_int(char* where, size_t size, int32_t val)
{
int err = 0;
if (val < 0 || size < sizeof(val))
{
err++;
}
else
{
memset(where, 0, size);
*(int32_t *)(where+size-sizeof(val)) = htonl(val);
*where |= 0x80;
}
return err;
}
int header_extract_special_int(char* where, int len)
{
int32_t val = -1;
if ( (len>=sizeof(val)) && (where[0] & 0x80))
{
val = *(int32_t *)(where + len - sizeof(val));
val = ntohl(val);
}
return val;
}
void header_set_uid_bigsafe(char* buf, int32_t uid)
{
int toobig = 07777777;
/* Tests if bitpacking is needed */
if(uid > toobig)
{
header_insert_special_int(buf,UID_PACKING_LEN,uid);
}
else
{
/* Do normal formatting as octal */
}
}
void header_parse_uid_bigsafe(char* uidstring, uid_t* uid)
{
/* Tests if bitpacking was used */
if(uidstring[0] == '\0')
{
*uid = header_extract_special_int(uidstring, UID_PACKING_LEN);
}
else
{
/* Do normal parsing from octal */
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment