This commit is contained in:
twinaphex 2020-01-30 16:49:52 +01:00
parent db0ddbefe8
commit 553d363cb9
9 changed files with 215 additions and 151 deletions

View File

@ -48,12 +48,13 @@ static int netlink_socket(void)
struct sockaddr_nl l_addr;
int l_socket = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
if(l_socket < 0)
if (l_socket < 0)
return -1;
memset(&l_addr, 0, sizeof(l_addr));
l_addr.nl_family = AF_NETLINK;
if(bind(l_socket, (struct sockaddr *)&l_addr, sizeof(l_addr)) < 0)
if (bind(l_socket, (struct sockaddr *)&l_addr, sizeof(l_addr)) < 0)
{
close(l_socket);
return -1;
@ -91,67 +92,77 @@ static int netlink_recv(int p_socket, void *p_buffer, size_t p_len)
struct iovec l_iov = { p_buffer, p_len };
struct sockaddr_nl l_addr;
for(;;)
for (;;)
{
l_msg.msg_name = (void *)&l_addr;
l_msg.msg_namelen = sizeof(l_addr);
l_msg.msg_iov = &l_iov;
l_msg.msg_iovlen = 1;
l_msg.msg_control = NULL;
l_msg.msg_controllen = 0;
l_msg.msg_flags = 0;
int l_result = recvmsg(p_socket, &l_msg, 0);
int l_result;
if(l_result < 0)
l_msg.msg_name = (void *)&l_addr;
l_msg.msg_namelen = sizeof(l_addr);
l_msg.msg_iov = &l_iov;
l_msg.msg_iovlen = 1;
l_msg.msg_control = NULL;
l_msg.msg_controllen = 0;
l_msg.msg_flags = 0;
l_result = recvmsg(p_socket, &l_msg, 0);
if (l_result < 0)
{
if(errno == EINTR)
if (errno == EINTR)
continue;
return -2;
}
if(l_msg.msg_flags & MSG_TRUNC) /* buffer too small */
if (l_msg.msg_flags & MSG_TRUNC) /* buffer too small */
return -1;
return l_result;
}
}
static struct nlmsghdr *getNetlinkResponse(int p_socket, int *p_size, int *p_done)
static struct nlmsghdr *getNetlinkResponse(int p_socket,
int *p_size, int *p_done)
{
size_t l_size = 4096;
size_t l_size = 4096;
void *l_buffer = NULL;
for(;;)
for (;;)
{
int l_read;
free(l_buffer);
l_buffer = malloc(l_size);
if (l_buffer == NULL)
if (!l_buffer)
return NULL;
int l_read = netlink_recv(p_socket, l_buffer, l_size);
l_read = netlink_recv(p_socket, l_buffer, l_size);
*p_size = l_read;
if(l_read == -2)
if (l_read == -2)
{
free(l_buffer);
return NULL;
}
if(l_read >= 0)
if (l_read >= 0)
{
pid_t l_pid = getpid();
struct nlmsghdr *l_hdr;
for(l_hdr = (struct nlmsghdr *)l_buffer; NLMSG_OK(l_hdr, (unsigned int)l_read); l_hdr = (struct nlmsghdr *)NLMSG_NEXT(l_hdr, l_read))
for (l_hdr = (struct nlmsghdr *)l_buffer;
NLMSG_OK(l_hdr, (unsigned int)l_read);
l_hdr = (struct nlmsghdr *)NLMSG_NEXT(l_hdr, l_read))
{
if((pid_t)l_hdr->nlmsg_pid != l_pid || (int)l_hdr->nlmsg_seq != p_socket)
if ( (pid_t)l_hdr->nlmsg_pid != l_pid ||
(int)l_hdr->nlmsg_seq != p_socket)
continue;
if(l_hdr->nlmsg_type == NLMSG_DONE)
if (l_hdr->nlmsg_type == NLMSG_DONE)
{
*p_done = 1;
break;
}
if(l_hdr->nlmsg_type == NLMSG_ERROR)
if (l_hdr->nlmsg_type == NLMSG_ERROR)
{
free(l_buffer);
return NULL;
@ -166,8 +177,8 @@ static struct nlmsghdr *getNetlinkResponse(int p_socket, int *p_size, int *p_don
static NetlinkList *newListItem(struct nlmsghdr *p_data, unsigned int p_size)
{
NetlinkList *l_item = malloc(sizeof(NetlinkList));
if (l_item == NULL)
NetlinkList *l_item = (NetlinkList*)malloc(sizeof(NetlinkList));
if (!l_item)
return NULL;
l_item->m_next = NULL;
@ -179,7 +190,8 @@ static NetlinkList *newListItem(struct nlmsghdr *p_data, unsigned int p_size)
static void freeResultList(NetlinkList *p_list)
{
NetlinkList *l_cur;
while(p_list)
while (p_list)
{
l_cur = p_list;
p_list = p_list->m_next;
@ -190,29 +202,30 @@ static void freeResultList(NetlinkList *p_list)
static NetlinkList *getResultList(int p_socket, int p_request)
{
if(netlink_send(p_socket, p_request) < 0)
int l_size;
NetlinkList *l_list = NULL;
NetlinkList *l_end = NULL;
int l_done = 0;
if (netlink_send(p_socket, p_request) < 0)
return NULL;
NetlinkList *l_list = NULL;
NetlinkList *l_end = NULL;
int l_size;
int l_done = 0;
while(!l_done)
while (!l_done)
{
NetlinkList *l_item = NULL;
struct nlmsghdr *l_hdr = getNetlinkResponse(p_socket, &l_size, &l_done);
if(!l_hdr)
if (!l_hdr)
goto error;
l_item = newListItem(l_hdr, l_size);
if (!l_item)
goto error;
if(!l_list)
l_list = l_item;
if (!l_list)
l_list = l_item;
else
l_end->m_next = l_item;
l_end = l_item;
l_end = l_item;
}
return l_list;
@ -267,12 +280,12 @@ static void makeSockaddr(sa_family_t p_family, struct sockaddr *p_dest, void *p_
static void addToEnd(struct ifaddrs **p_resultList, struct ifaddrs *p_entry)
{
if(!*p_resultList)
if (!*p_resultList)
*p_resultList = p_entry;
else
{
struct ifaddrs *l_cur = *p_resultList;
while(l_cur->ifa_next)
while (l_cur->ifa_next)
l_cur = l_cur->ifa_next;
l_cur->ifa_next = p_entry;
}
@ -288,7 +301,8 @@ static int interpretLink(struct nlmsghdr *p_hdr, struct ifaddrs **p_resultList)
size_t l_dataSize = 0;
size_t l_rtaSize = NLMSG_PAYLOAD(p_hdr, sizeof(struct ifinfomsg));
for(l_rta = IFLA_RTA(l_info); RTA_OK(l_rta, l_rtaSize); l_rta = RTA_NEXT(l_rta, l_rtaSize))
for (l_rta = IFLA_RTA(l_info); RTA_OK(l_rta, l_rtaSize);
l_rta = RTA_NEXT(l_rta, l_rtaSize))
{
size_t l_rtaDataSize = RTA_PAYLOAD(l_rta);
switch(l_rta->rta_type)
@ -308,8 +322,8 @@ static int interpretLink(struct nlmsghdr *p_hdr, struct ifaddrs **p_resultList)
}
}
l_entry = malloc(sizeof(struct ifaddrs) + sizeof(int) + l_nameSize + l_addrSize + l_dataSize);
if (l_entry == NULL)
l_entry = (sruct ifaddrs*)malloc(sizeof(struct ifaddrs) + sizeof(int) + l_nameSize + l_addrSize + l_dataSize);
if (!l_entry)
return -1;
memset(l_entry, 0, sizeof(struct ifaddrs));
@ -320,16 +334,20 @@ static int interpretLink(struct nlmsghdr *p_hdr, struct ifaddrs **p_resultList)
char *l_addr = l_name + l_nameSize;
char *l_data = l_addr + l_addrSize;
// save the interface index so we can look it up when handling the addresses.
/* save the interface index so we can look
* it up when handling the addresses. */
memcpy(l_index, &l_info->ifi_index, sizeof(int));
l_entry->ifa_flags = l_info->ifi_flags;
l_rtaSize = NLMSG_PAYLOAD(p_hdr, sizeof(struct ifinfomsg));
for(l_rta = IFLA_RTA(l_info); RTA_OK(l_rta, l_rtaSize); l_rta = RTA_NEXT(l_rta, l_rtaSize))
l_rtaSize = NLMSG_PAYLOAD(p_hdr, sizeof(struct ifinfomsg));
for (l_rta = IFLA_RTA(l_info); RTA_OK(l_rta, l_rtaSize);
l_rta = RTA_NEXT(l_rta, l_rtaSize))
{
void *l_rtaData = RTA_DATA(l_rta);
void *l_rtaData = RTA_DATA(l_rta);
size_t l_rtaDataSize = RTA_PAYLOAD(l_rta);
switch(l_rta->rta_type)
{
case IFLA_ADDRESS:
@ -339,8 +357,8 @@ static int interpretLink(struct nlmsghdr *p_hdr, struct ifaddrs **p_resultList)
makeSockaddr(AF_PACKET, (struct sockaddr *)l_addr, l_rtaData, l_rtaDataSize);
((struct sockaddr_ll *)l_addr)->sll_ifindex = l_info->ifi_index;
((struct sockaddr_ll *)l_addr)->sll_hatype = l_info->ifi_type;
if(l_rta->rta_type == IFLA_ADDRESS)
l_entry->ifa_addr = (struct sockaddr *)l_addr;
if (l_rta->rta_type == IFLA_ADDRESS)
l_entry->ifa_addr = (struct sockaddr *)l_addr;
else
l_entry->ifa_broadaddr = (struct sockaddr *)l_addr;
l_addr += NLMSG_ALIGN(l_addrLen);
@ -364,17 +382,19 @@ static int interpretLink(struct nlmsghdr *p_hdr, struct ifaddrs **p_resultList)
return 0;
}
static struct ifaddrs *findInterface(int p_index, struct ifaddrs **p_links, int p_numLinks)
static struct ifaddrs *findInterface(int p_index,
struct ifaddrs **p_links, int p_numLinks)
{
int l_num = 0;
int l_num = 0;
struct ifaddrs *l_cur = *p_links;
while(l_cur && l_num < p_numLinks)
while (l_cur && l_num < p_numLinks)
{
int l_index;
char *l_indexPtr = ((char *)l_cur) + sizeof(struct ifaddrs);
memcpy(&l_index, l_indexPtr, sizeof(int));
if(l_index == p_index)
if (l_index == p_index)
return l_cur;
l_cur = l_cur->ifa_next;
@ -383,21 +403,24 @@ static struct ifaddrs *findInterface(int p_index, struct ifaddrs **p_links, int
return NULL;
}
static int interpretAddr(struct nlmsghdr *p_hdr, struct ifaddrs **p_resultList, int p_numLinks)
static int interpretAddr(struct nlmsghdr *p_hdr,
struct ifaddrs **p_resultList, int p_numLinks)
{
size_t l_nameSize = 0;
size_t l_addrSize = 0;
int l_addedNetmask = 0;
struct ifaddrmsg *l_info = (struct ifaddrmsg *)NLMSG_DATA(p_hdr);
struct rtattr *l_rta;
size_t l_rtaSize;
size_t l_nameSize = 0;
size_t l_addrSize = 0;
int l_addedNetmask = 0;
struct ifaddrmsg *l_info = (struct ifaddrmsg *)NLMSG_DATA(p_hdr);
struct ifaddrs *l_interface = findInterface(l_info->ifa_index, p_resultList, p_numLinks);
if(l_info->ifa_family == AF_PACKET)
if (l_info->ifa_family == AF_PACKET)
return 0;
size_t l_rtaSize = NLMSG_PAYLOAD(p_hdr, sizeof(struct ifaddrmsg));
struct rtattr *l_rta;
l_rtaSize = NLMSG_PAYLOAD(p_hdr, sizeof(struct ifaddrmsg));
for(l_rta = IFA_RTA(l_info); RTA_OK(l_rta, l_rtaSize); l_rta = RTA_NEXT(l_rta, l_rtaSize))
for (l_rta = IFA_RTA(l_info); RTA_OK(l_rta, l_rtaSize);
l_rta = RTA_NEXT(l_rta, l_rtaSize))
{
size_t l_rtaDataSize = RTA_PAYLOAD(l_rta);
@ -405,7 +428,7 @@ static int interpretAddr(struct nlmsghdr *p_hdr, struct ifaddrs **p_resultList,
{
case IFA_ADDRESS:
case IFA_LOCAL:
if((l_info->ifa_family == AF_INET || l_info->ifa_family == AF_INET6) && !l_addedNetmask)
if ((l_info->ifa_family == AF_INET || l_info->ifa_family == AF_INET6) && !l_addedNetmask)
{
/* make room for netmask */
l_addrSize += NLMSG_ALIGN(calcAddrLen(l_info->ifa_family, l_rtaDataSize));
@ -422,8 +445,8 @@ static int interpretAddr(struct nlmsghdr *p_hdr, struct ifaddrs **p_resultList,
}
}
struct ifaddrs *l_entry = malloc(sizeof(struct ifaddrs) + l_nameSize + l_addrSize);
if (l_entry == NULL)
struct ifaddrs *l_entry = (struct ifaddrs*)malloc(sizeof(struct ifaddrs) + l_nameSize + l_addrSize);
if (!l_entry)
return -1;
memset(l_entry, 0, sizeof(struct ifaddrs));
@ -433,11 +456,12 @@ static int interpretAddr(struct nlmsghdr *p_hdr, struct ifaddrs **p_resultList,
char *l_addr = l_name + l_nameSize;
l_entry->ifa_flags = l_info->ifa_flags;
if(l_interface)
if (l_interface)
l_entry->ifa_flags |= l_interface->ifa_flags;
l_rtaSize = NLMSG_PAYLOAD(p_hdr, sizeof(struct ifaddrmsg));
for(l_rta = IFA_RTA(l_info); RTA_OK(l_rta, l_rtaSize); l_rta = RTA_NEXT(l_rta, l_rtaSize))
for (l_rta = IFA_RTA(l_info); RTA_OK(l_rta, l_rtaSize);
l_rta = RTA_NEXT(l_rta, l_rtaSize))
{
void *l_rtaData = RTA_DATA(l_rta);
size_t l_rtaDataSize = RTA_PAYLOAD(l_rta);
@ -449,24 +473,24 @@ static int interpretAddr(struct nlmsghdr *p_hdr, struct ifaddrs **p_resultList,
{
size_t l_addrLen = calcAddrLen(l_info->ifa_family, l_rtaDataSize);
makeSockaddr(l_info->ifa_family, (struct sockaddr *)l_addr, l_rtaData, l_rtaDataSize);
if(l_info->ifa_family == AF_INET6)
if (l_info->ifa_family == AF_INET6)
{
if(IN6_IS_ADDR_LINKLOCAL((struct in6_addr *)l_rtaData) || IN6_IS_ADDR_MC_LINKLOCAL((struct in6_addr *)l_rtaData))
if (IN6_IS_ADDR_LINKLOCAL((struct in6_addr *)l_rtaData) || IN6_IS_ADDR_MC_LINKLOCAL((struct in6_addr *)l_rtaData))
((struct sockaddr_in6 *)l_addr)->sin6_scope_id = l_info->ifa_index;
}
if(l_rta->rta_type == IFA_ADDRESS)
if (l_rta->rta_type == IFA_ADDRESS)
{
/* apparently in a point-to-point network IFA_ADDRESS
* contains the dest address and IFA_LOCAL contains the local address */
if(l_entry->ifa_addr)
if (l_entry->ifa_addr)
l_entry->ifa_dstaddr = (struct sockaddr *)l_addr;
else
l_entry->ifa_addr = (struct sockaddr *)l_addr;
}
else if(l_rta->rta_type == IFA_LOCAL)
else if (l_rta->rta_type == IFA_LOCAL)
{
if(l_entry->ifa_addr)
if (l_entry->ifa_addr)
l_entry->ifa_dstaddr = l_entry->ifa_addr;
l_entry->ifa_addr = (struct sockaddr *)l_addr;
}
@ -485,7 +509,7 @@ static int interpretAddr(struct nlmsghdr *p_hdr, struct ifaddrs **p_resultList,
}
}
if(l_entry->ifa_addr &&
if (l_entry->ifa_addr &&
( l_entry->ifa_addr->sa_family == AF_INET
|| l_entry->ifa_addr->sa_family == AF_INET6))
{
@ -498,9 +522,9 @@ static int interpretAddr(struct nlmsghdr *p_hdr, struct ifaddrs **p_resultList,
l_mask[0] = '\0';
for(i=0; i<(l_prefix/8); ++i)
for (i=0; i<(l_prefix/8); ++i)
l_mask[i] = 0xff;
if(l_prefix % 8)
if (l_prefix % 8)
l_mask[i] = 0xff << (8 - (l_prefix % 8));
makeSockaddr(l_entry->ifa_addr->sa_family,
@ -516,23 +540,26 @@ static int interpretLinks(int p_socket, NetlinkList *p_netlinkList,
struct ifaddrs **p_resultList)
{
int l_numLinks = 0;
pid_t l_pid = getpid();
for(; p_netlinkList; p_netlinkList = p_netlinkList->m_next)
pid_t l_pid = getpid();
for (; p_netlinkList; p_netlinkList = p_netlinkList->m_next)
{
struct nlmsghdr *l_hdr = NULL;
unsigned int l_nlsize = p_netlinkList->m_size;
for(l_hdr = p_netlinkList->m_data; NLMSG_OK(l_hdr, l_nlsize); l_hdr = NLMSG_NEXT(l_hdr, l_nlsize))
for (l_hdr = p_netlinkList->m_data; NLMSG_OK(l_hdr, l_nlsize);
l_hdr = NLMSG_NEXT(l_hdr, l_nlsize))
{
if((pid_t)l_hdr->nlmsg_pid != l_pid || (int)l_hdr->nlmsg_seq != p_socket)
if ( (pid_t)l_hdr->nlmsg_pid != l_pid ||
(int)l_hdr->nlmsg_seq != p_socket)
continue;
if(l_hdr->nlmsg_type == NLMSG_DONE)
if (l_hdr->nlmsg_type == NLMSG_DONE)
break;
if(l_hdr->nlmsg_type == RTM_NEWLINK)
if (l_hdr->nlmsg_type == RTM_NEWLINK)
{
if(interpretLink(l_hdr, p_resultList) == -1)
if (interpretLink(l_hdr, p_resultList) == -1)
return -1;
++l_numLinks;
}
@ -545,20 +572,22 @@ static int interpretAddrs(int p_socket, NetlinkList *p_netlinkList,
struct ifaddrs **p_resultList, int p_numLinks)
{
pid_t l_pid = getpid();
for(; p_netlinkList; p_netlinkList = p_netlinkList->m_next)
for (; p_netlinkList; p_netlinkList = p_netlinkList->m_next)
{
struct nlmsghdr *l_hdr = NULL;
unsigned int l_nlsize = p_netlinkList->m_size;
for(l_hdr = p_netlinkList->m_data; NLMSG_OK(l_hdr, l_nlsize); l_hdr = NLMSG_NEXT(l_hdr, l_nlsize))
for (l_hdr = p_netlinkList->m_data; NLMSG_OK(l_hdr, l_nlsize);
l_hdr = NLMSG_NEXT(l_hdr, l_nlsize))
{
if((pid_t)l_hdr->nlmsg_pid != l_pid || (int)l_hdr->nlmsg_seq != p_socket)
if ( (pid_t)l_hdr->nlmsg_pid != l_pid
|| (int)l_hdr->nlmsg_seq != p_socket)
continue;
if(l_hdr->nlmsg_type == NLMSG_DONE)
if (l_hdr->nlmsg_type == NLMSG_DONE)
break;
if(l_hdr->nlmsg_type == RTM_NEWADDR)
if (l_hdr->nlmsg_type == RTM_NEWADDR)
{
if (interpretAddr(l_hdr, p_resultList, p_numLinks) == -1)
return -1;
@ -570,34 +599,40 @@ static int interpretAddrs(int p_socket, NetlinkList *p_netlinkList,
int getifaddrs(struct ifaddrs **ifap)
{
int l_socket = 0;
int l_result = 0;
if(!ifap)
NetlinkList *l_linkResults;
NetlinkList *l_addrResults;
int l_numLinks;
int l_socket = 0;
int l_result = 0;
if (!ifap)
return -1;
*ifap = NULL;
*ifap = NULL;
l_socket = netlink_socket();
if(l_socket < 0)
if (l_socket < 0)
return -1;
NetlinkList *l_linkResults = getResultList(l_socket, RTM_GETLINK);
if(!l_linkResults)
l_linkResults = getResultList(l_socket, RTM_GETLINK);
if (!l_linkResults)
{
close(l_socket);
return -1;
}
NetlinkList *l_addrResults = getResultList(l_socket, RTM_GETADDR);
if(!l_addrResults)
l_addrResults = getResultList(l_socket, RTM_GETADDR);
if (!l_addrResults)
{
close(l_socket);
freeResultList(l_linkResults);
return -1;
}
int l_numLinks = interpretLinks(l_socket, l_linkResults, ifap);
l_numLinks = interpretLinks(l_socket, l_linkResults, ifap);
if(l_numLinks == -1 || interpretAddrs(l_socket, l_addrResults, ifap, l_numLinks) == -1)
if ( l_numLinks == -1 ||
interpretAddrs(l_socket, l_addrResults, ifap, l_numLinks) == -1)
l_result = -1;
freeResultList(l_linkResults);
@ -610,10 +645,10 @@ void freeifaddrs(struct ifaddrs *ifa)
{
struct ifaddrs *l_cur = NULL;
while(ifa)
while (ifa)
{
l_cur = ifa;
ifa = ifa->ifa_next;
ifa = ifa->ifa_next;
free(l_cur);
}
}

View File

@ -424,8 +424,7 @@ static void cpulist_parse(CpuList* list, char **buf, ssize_t length)
q = end;
/* Get first value */
p = parse_decimal(p, q, &start_value);
if (p == NULL)
if (!(p = parse_decimal(p, q, &start_value)))
return;
end_value = start_value;
@ -435,8 +434,7 @@ static void cpulist_parse(CpuList* list, char **buf, ssize_t length)
*/
if (p < q && *p == '-')
{
p = parse_decimal(p+1, q, &end_value);
if (p == NULL)
if (!(p = parse_decimal(p+1, q, &end_value)))
return;
}

View File

@ -1162,7 +1162,7 @@ static void test_config_file_parse_contains(
if (!val)
return;
if (out == NULL)
if (!out)
out = strdup("");
if (strcmp(out, val) != 0)
abort();

View File

@ -63,7 +63,10 @@ static void cdfs_determine_sector_size(cdfs_track_t* track)
static void cdfs_seek_track_sector(cdfs_track_t* track, unsigned int sector)
{
intfstream_seek(track->stream, sector * track->stream_sector_size + track->stream_sector_header_size + track->first_sector_offset, SEEK_SET);
intfstream_seek(track->stream,
sector * track->stream_sector_size +
track->stream_sector_header_size +
track->first_sector_offset, SEEK_SET);
}
void cdfs_seek_sector(cdfs_file_t* file, unsigned int sector)
@ -73,12 +76,12 @@ void cdfs_seek_sector(cdfs_file_t* file, unsigned int sector)
{
if (sector != file->current_sector)
{
file->current_sector = sector;
file->current_sector = sector;
file->sector_buffer_valid = 0;
}
file->pos = file->current_sector * 2048;
file->current_sector_offset = 0;
file->pos = file->current_sector * 2048;
file->current_sector_offset = 0;
}
}
@ -86,8 +89,8 @@ static int cdfs_find_file(cdfs_file_t* file, const char* path)
{
uint8_t buffer[2048], *tmp;
int sector, path_length;
const char* slash = strrchr(path, '\\');
if (slash)
{
/* navigate the path to the directory record for the file */
@ -105,12 +108,13 @@ static int cdfs_find_file(cdfs_file_t* file, const char* path)
{
int offset;
/* find the cd information (always 16 frames in) */
/* find the CD information (always 16 frames in) */
cdfs_seek_track_sector(file->track, 16);
intfstream_read(file->track->stream, buffer, sizeof(buffer));
/* the directory_record starts at 156 bytes into the sector.
* the sector containing the root directory contents is a 3 byte value that is 2 bytes into the directory_record. */
* the sector containing the root directory contents is a
* 3 byte value that is 2 bytes into the directory_record. */
offset = 156 + 2;
sector = buffer[offset] | (buffer[offset + 1] << 8) | (buffer[offset + 2] << 16);
}
@ -120,21 +124,26 @@ static int cdfs_find_file(cdfs_file_t* file, const char* path)
intfstream_read(file->track->stream, buffer, sizeof(buffer));
path_length = strlen(path);
tmp = buffer;
tmp = buffer;
while (tmp < buffer + sizeof(buffer))
{
/* the first byte of the record is the length of the record - if 0, we reached the end of the data */
/* The first byte of the record is the length of
* the record - if 0, we reached the end of the data */
if (!*tmp)
break;
/* filename is 33 bytes into the record and the format is "FILENAME;version" or "DIRECTORY" */
/* filename is 33 bytes into the record and
* the format is "FILENAME;version" or "DIRECTORY" */
if ((tmp[33 + path_length] == ';' || tmp[33 + path_length] == '\0') &&
strncasecmp((const char*)(tmp + 33), path, path_length) == 0)
{
/* the file size is in bytes 10-13 of the record */
file->size = tmp[10] | (tmp[11] << 8) | (tmp[12] << 16) | (tmp[13] << 24);
file->size = tmp[10] | (tmp[11] << 8)
| (tmp[12] << 16) | (tmp[13] << 24);
/* the file contents are in the sector identified in bytes 2-4 of the record */
/* the file contents are in the sector identified
* in bytes 2-4 of the record */
sector = tmp[2] | (tmp[3] << 8) | (tmp[4] << 16);
return sector;
}
@ -153,15 +162,17 @@ int cdfs_open_file(cdfs_file_t* file, cdfs_track_t* track, const char* path)
memset(file, 0, sizeof(*file));
file->track = track;
file->track = track;
file->current_sector = -1;
if (path)
file->first_sector = cdfs_find_file(file, path);
else if (file->track->stream_sector_size)
{
file->first_sector = 0;
file->size = (intfstream_get_size(file->track->stream) / file->track->stream_sector_size) * 2048;
file->size = (intfstream_get_size(
file->track->stream) / file->track->stream_sector_size)
* 2048;
}
else
file->first_sector = -1;
@ -189,12 +200,14 @@ int64_t cdfs_read_file(cdfs_file_t* file, void* buffer, uint64_t len)
{
if (remaining >= len)
{
memcpy(buffer, &file->sector_buffer[file->current_sector_offset], len);
memcpy(buffer,
&file->sector_buffer[file->current_sector_offset], len);
file->current_sector_offset += len;
return len;
}
memcpy(buffer, &file->sector_buffer[file->current_sector_offset], remaining);
memcpy(buffer,
&file->sector_buffer[file->current_sector_offset], remaining);
buffer = (char*)buffer + remaining;
bytes_read += remaining;
len -= remaining;
@ -204,11 +217,11 @@ int64_t cdfs_read_file(cdfs_file_t* file, void* buffer, uint64_t len)
++file->current_sector;
file->current_sector_offset = 0;
file->sector_buffer_valid = 0;
file->sector_buffer_valid = 0;
}
else if (file->current_sector < file->first_sector)
{
file->current_sector = file->first_sector;
file->current_sector = file->first_sector;
file->current_sector_offset = 0;
}
@ -217,8 +230,9 @@ int64_t cdfs_read_file(cdfs_file_t* file, void* buffer, uint64_t len)
cdfs_seek_track_sector(file->track, file->current_sector);
intfstream_read(file->track->stream, buffer, 2048);
buffer = (char*)buffer + 2048;
buffer = (char*)buffer + 2048;
bytes_read += 2048;
++file->current_sector;
len -= 2048;
@ -243,7 +257,8 @@ void cdfs_close_file(cdfs_file_t* file)
{
if (file)
{
/* not really anything to do here, just clear out the first_sector so read() won't do anything */
/* not really anything to do here, just
* clear out the first_sector so read() won't do anything */
file->first_sector = -1;
}
}
@ -314,21 +329,26 @@ static void cdfs_skip_spaces(const char** ptr)
++(*ptr);
}
static cdfs_track_t* cdfs_wrap_stream(intfstream_t* stream, unsigned first_sector_offset)
static cdfs_track_t* cdfs_wrap_stream(
intfstream_t* stream, unsigned first_sector_offset)
{
cdfs_track_t* track;
cdfs_track_t* track = NULL;
if (stream == NULL)
if (!stream)
return NULL;
track = (cdfs_track_t*)calloc(1, sizeof(*track));
track->stream = stream;
track = (cdfs_track_t*)
calloc(1, sizeof(*track));
track->stream = stream;
track->first_sector_offset = first_sector_offset;
cdfs_determine_sector_size(track);
return track;
}
static cdfs_track_t* cdfs_open_cue_track(const char* path, unsigned int track_index)
static cdfs_track_t* cdfs_open_cue_track(
const char* path, unsigned int track_index)
{
char* cue = NULL;
const char* line = NULL;
@ -442,10 +462,9 @@ static cdfs_track_t* cdfs_open_cue_track(const char* path, unsigned int track_in
if (found_track && index_number == 1)
{
if (strstr(current_track_path, "/") || strstr(current_track_path, "\\"))
{
if ( strstr(current_track_path, "/") ||
strstr(current_track_path, "\\"))
strncpy(track_path, current_track_path, sizeof(track_path));
}
else
{
fill_pathname_basedir(track_path, path, sizeof(track_path));
@ -462,7 +481,10 @@ static cdfs_track_t* cdfs_open_cue_track(const char* path, unsigned int track_in
if (string_is_empty(track_path))
return NULL;
track = cdfs_wrap_stream(intfstream_open_file(track_path, RETRO_VFS_FILE_ACCESS_READ, RETRO_VFS_FILE_ACCESS_HINT_NONE), track_offset);
track = cdfs_wrap_stream(intfstream_open_file(
track_path, RETRO_VFS_FILE_ACCESS_READ,
RETRO_VFS_FILE_ACCESS_HINT_NONE), track_offset);
if (track && track->stream_sector_size == 0)
{
track->stream_sector_size = sector_size;
@ -482,11 +504,14 @@ static cdfs_track_t* cdfs_open_chd_track(const char* path, int32_t track_index)
intfstream_t* intf_stream;
cdfs_track_t* track;
intf_stream = intfstream_open_chd_track(path, RETRO_VFS_FILE_ACCESS_READ, RETRO_VFS_FILE_ACCESS_HINT_NONE, track_index);
intf_stream = intfstream_open_chd_track(path,
RETRO_VFS_FILE_ACCESS_READ, RETRO_VFS_FILE_ACCESS_HINT_NONE,
track_index);
if (!intf_stream)
return NULL;
track = cdfs_wrap_stream(intf_stream, intfstream_get_offset_to_start(intf_stream));
track = cdfs_wrap_stream(intf_stream,
intfstream_get_offset_to_start(intf_stream));
if (track && track->stream_sector_header_size == 0)
{
@ -502,7 +527,8 @@ static cdfs_track_t* cdfs_open_chd_track(const char* path, int32_t track_index)
}
#endif
struct cdfs_track_t* cdfs_open_track(const char* path, unsigned int track_index)
struct cdfs_track_t* cdfs_open_track(const char* path,
unsigned int track_index)
{
const char* ext = path_get_extension(path);
@ -538,8 +564,11 @@ cdfs_track_t* cdfs_open_raw_track(const char* path)
{
const char* ext = path_get_extension(path);
if (string_is_equal_noncase(ext, "bin") || string_is_equal_noncase(ext, "iso"))
return cdfs_wrap_stream(intfstream_open_file(path, RETRO_VFS_FILE_ACCESS_READ, RETRO_VFS_FILE_ACCESS_HINT_NONE), 0);
if ( string_is_equal_noncase(ext, "bin") ||
string_is_equal_noncase(ext, "iso"))
return cdfs_wrap_stream(intfstream_open_file(path,
RETRO_VFS_FILE_ACCESS_READ,
RETRO_VFS_FILE_ACCESS_HINT_NONE), 0);
/* unsupported file type */
return NULL;

View File

@ -100,7 +100,7 @@ enum rwav_state rwav_iterate(rwav_iterator_t *iter)
samples = malloc(rwav->subchunk2size);
if (samples == NULL)
if (!samples)
return RWAV_ITERATE_ERROR;
rwav->numchannels = data[22] | data[23] << 8;

View File

@ -782,10 +782,12 @@ dg__dynarr_grow(void** arr, dg__dynarr_md* md, size_t itemsize, size_t min_neede
DG_DYNARR_INLINE void
dg__dynarr_init(void** p, dg__dynarr_md* md, void* buf, size_t buf_cap)
{
*p = buf;
md->cnt = 0;
if(buf == NULL) md->cap = 0;
else md->cap = (DG__DYNARR_SIZE_T_MSB | buf_cap);
*p = buf;
md->cnt = 0;
if(!buf)
md->cap = 0;
else
md->cap = (DG__DYNARR_SIZE_T_MSB | buf_cap);
}
DG_DYNARR_INLINE int
@ -952,7 +954,7 @@ dg__dynarr_grow(void** arr, dg__dynarr_md* md, size_t itemsize, size_t min_neede
else
{
void* p = DG_DYNARR_REALLOC(*arr, itemsize, md->cnt, newcap);
if(p == NULL) DG_DYNARR_FREE(*arr); /* realloc failed, at least don't leak memory */
if (!p) DG_DYNARR_FREE(*arr); /* realloc failed, at least don't leak memory */
*arr = p;
}

View File

@ -189,7 +189,7 @@ int ssl_socket_receive_all_blocking(void *state_data, void *data_, size_t size)
mbedtls_net_set_block(&state->net_ctx);
while (1)
for (;;)
{
/* mbedtls_ssl_read wants non-const data but it only reads it, so this cast is safe */
int ret = mbedtls_ssl_read(&state->ctx, (unsigned char*)data, size);

View File

@ -451,7 +451,7 @@ static void threaded_worker(void *userdata)
/* Get first task to run */
task = tasks_running.front;
if (task == NULL)
if (!task)
{
scond_wait(worker_cond, running_lock);
slock_unlock(running_lock);

View File

@ -144,7 +144,7 @@ static void tpool_worker(void *arg)
* Also, the working_cnt can't be changed (except the thread holding the lock).
* At this point if there isn't any work processing and if there is no work
* signal this is the case. */
if (!tp->stop && tp->working_cnt == 0 && tp->work_first == NULL)
if (!tp->stop && tp->working_cnt == 0 && !tp->work_first)
scond_signal(tp->working_cond);
slock_unlock(tp->work_mutex);
}