Skip to content

Commit

Permalink
revert
Browse files Browse the repository at this point in the history
  • Loading branch information
rdmark committed Feb 15, 2025
1 parent 4cdc09f commit 5b81d6f
Showing 1 changed file with 49 additions and 74 deletions.
123 changes: 49 additions & 74 deletions libatalk/vfs/ea_ad.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,106 +120,81 @@ static char *mtoupath(const struct vol *vol, const char *mpath)
*
* Verifies magic and version.
*/
static int unpack_header(struct ea * restrict ea)
static int pack_header(struct ea * restrict ea)
{
int ret = 0;
unsigned int count = 0;
unsigned int count = 0, eacount = 0;
uint16_t uint16;
uint32_t uint32;
char *buf;
size_t remaining;

/* Check magic and version */
buf = ea->ea_data;
memcpy(&uint32, buf, sizeof(uint32_t));
if (uint32 != htonl(EA_MAGIC)) {
LOG(log_error, logtype_afpd, "unpack_header: wrong magic 0x%08x", uint32);
ret = -1;
goto exit;
}
buf += 4;
memcpy(&uint16, buf, sizeof(uint16_t));
if (uint16 != htons(EA_VERSION)) {
LOG(log_error, logtype_afpd, "unpack_header: wrong version 0x%04x", uint16);
ret = -1;
goto exit;
}
buf += 2;
size_t bufsize = EA_HEADER_SIZE;

/* Get EA count */
memcpy(&uint16, buf, sizeof(uint16_t));
ea->ea_count = ntohs(uint16);
LOG(log_debug, logtype_afpd, "unpack_header: number of EAs: %u", ea->ea_count);
buf += 2;
char *buf = ea->ea_data + EA_HEADER_SIZE;

LOG(log_debug, logtype_afpd, "pack_header('%s'): ea_count: %u, ea_size: %u",
ea->filename, ea->ea_count, ea->ea_size);

if (ea->ea_count == 0)
/* nothing to do, magic, version and count are still valid in buffer */
return 0;

/* Check magic and version */
buf = ea->ea_data;
remaining = ea->ea_size;
while(count < ea->ea_count) { /* the names */
/* Check if its a deleted entry */
if ( ! ((*ea->ea_entries)[count].ea_name)) {
count++;
continue;
}

if (remaining < EA_HEADER_SIZE) {
ret = -1;
goto exit;
bufsize += (*(ea->ea_entries))[count].ea_namelen + 1;
count++;
eacount++;
}

/* Allocate storage for the ea_entries array */
ea->ea_entries = malloc(sizeof(struct ea_entry) * ea->ea_count);
if ( ! ea->ea_entries) {
LOG(log_error, logtype_afpd, "unpack_header: OOM");
ret = -1;
goto exit;
bufsize += (eacount * 4); /* header + ea_size for each EA */
if (bufsize > ea->ea_size) {
/* we must realloc */
if ( ! (buf = realloc(ea->ea_data, bufsize)) ) {
LOG(log_error, logtype_afpd, "pack_header: OOM");
return -1;
}
ea->ea_data = buf;
}
ea->ea_size = bufsize;

/* copy count */
uint16 = htons(eacount);
memcpy(ea->ea_data + EA_COUNT_OFF, &uint16, 2);

count = 0;
buf = ea->ea_data + EA_HEADER_SIZE;
while (count < ea->ea_count) {
/* Check if we have enough bytes for EA size (4) + at least 1 char for name */
if (remaining < 5) {
ret = -1;
goto cleanup;
/* Check if its a deleted entry */
if ( ! ((*ea->ea_entries)[count].ea_name)) {
count++;
continue;
}

memcpy(&uint32, buf, 4); /* EA size */
buf += 4;
remaining -= 4;

/* Validate string length fits in remaining buffer */
size_t namelen = strnlen(buf, remaining);
if (namelen == remaining) { /* No null terminator found */
ret = -1;
goto cleanup;
}
/* First: EA size */
uint32 = htonl((*(ea->ea_entries))[count].ea_size);
memcpy(buf, &uint32, 4);
buf += 4;

/* Rest of the existing code */
(*(ea->ea_entries))[count].ea_size = ntohl(uint32);
(*(ea->ea_entries))[count].ea_name = strdup(buf);
if (! (*(ea->ea_entries))[count].ea_name) {
ret = -1;
goto cleanup;
}

(*(ea->ea_entries))[count].ea_namelen = namelen;
buf += namelen + 1;
remaining -= namelen + 1;
/* Second: EA name as C-string */
strcpy(buf, (*(ea->ea_entries))[count].ea_name);
buf += (*(ea->ea_entries))[count].ea_namelen + 1;

LOG(log_maxdebug, logtype_afpd, "unpack_header: entry no:%u,\"%s\", size: %u, namelen: %u", count,
LOG(log_maxdebug, logtype_afpd, "pack_header: entry no:%u,\"%s\", size: %u, namelen: %u", count,
(*(ea->ea_entries))[count].ea_name,
(*(ea->ea_entries))[count].ea_size,
(*(ea->ea_entries))[count].ea_namelen);

count++;
}

cleanup:
while (count > 0) {
count--;
free((*(ea->ea_entries))[count].ea_name);
}
free(ea->ea_entries);
ea->ea_entries = NULL;
ea->ea_count = eacount;

exit:
return ret;
LOG(log_debug, logtype_afpd, "pack_header('%s'): ea_count: %u, ea_size: %u",
ea->filename, ea->ea_count, ea->ea_size);

return 0;
}

/*
Expand Down

0 comments on commit 5b81d6f

Please sign in to comment.