Merge pull request #9 from povm/cleanup

Enforced overall compliance to standard
This commit is contained in:
Bertold Van den Bergh 2019-04-05 23:46:35 +02:00 committed by GitHub
commit 7fb89bf6e1
3 changed files with 74 additions and 80 deletions

View file

@ -28,7 +28,7 @@ CCARCH=
CC=$(CCARCH)gcc CC=$(CCARCH)gcc
STRIP=$(CCARCH)strip STRIP=$(CCARCH)strip
CFLAGS=-fPIC -O3 -Wall -c -fmessage-length=0 -Werror -ffunction-sections -fdata-sections CFLAGS=-fPIC -O3 -std=gnu99 -pedantic -Wall -Wextra -Wconversion -Werror -c -fmessage-length=0 -ffunction-sections -fdata-sections
LDFLAGS=-shared LDFLAGS=-shared
EXECUTABLE=libzonedetect.so EXECUTABLE=libzonedetect.so
@ -49,4 +49,4 @@ obj/%.o: src/%.c $(INCLUDES_SRC)
$(CC) $(CFLAGS) $< -o $@ $(CC) $(CFLAGS) $< -o $@
clean: clean:
rm -r $(OBJECTS_OBJ) $(EXECUTABLE) rm -rf $(OBJECTS_OBJ) $(EXECUTABLE)

View file

@ -26,6 +26,7 @@
*/ */
#include <sys/mman.h> #include <sys/mman.h>
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -55,27 +56,26 @@ struct ZoneDetectOpaque {
static int32_t ZDFloatToFixedPoint(float input, float scale, unsigned int precision) static int32_t ZDFloatToFixedPoint(float input, float scale, unsigned int precision)
{ {
float inputScaled = input / scale; const float inputScaled = input / scale;
return inputScaled * (float)(1 << (precision - 1)); return (int32_t)(inputScaled * (float)(1 << (precision - 1)));
} }
static unsigned int ZDDecodeVariableLengthUnsigned(ZoneDetect *library, uint32_t *index, uint32_t *result) static unsigned int ZDDecodeVariableLengthUnsigned(const ZoneDetect *library, uint32_t *index, uint32_t *result)
{ {
uint32_t value = 0; if(*index >= (uint32_t)library->length) {
unsigned int i = 0, shift = 0;
if(*index >= library->length) {
return 0; return 0;
} }
uint8_t *buffer = library->mapping + *index; uint8_t *const buffer = library->mapping + *index;
uint8_t *bufferEnd = library->mapping + library->length - 1; uint8_t *const bufferEnd = library->mapping + library->length - 1;
uint32_t value = 0;
unsigned int i = 0, shift = 0;
while(1) { while(1) {
value |= (buffer[i] & 0x7F) << shift; value |= (uint32_t)((buffer[i] & UINT8_C(0x7F)) << shift);
shift += 7; shift += 7u;
if(!(buffer[i] & 0x80)) { if(!(buffer[i] & UINT8_C(0x80))) {
break; break;
} }
@ -91,15 +91,15 @@ static unsigned int ZDDecodeVariableLengthUnsigned(ZoneDetect *library, uint32_t
return i; return i;
} }
static unsigned int ZDDecodeVariableLengthSigned(ZoneDetect *library, uint32_t *index, int32_t *result) static unsigned int ZDDecodeVariableLengthSigned(const ZoneDetect *library, uint32_t *index, int32_t *result)
{ {
uint32_t value = 0; uint32_t value = 0;
unsigned int retVal = ZDDecodeVariableLengthUnsigned(library, index, &value); const unsigned int retVal = ZDDecodeVariableLengthUnsigned(library, index, &value);
*result = (value & 1) ? -(value / 2) : (value / 2); *result = (value & 1) ? -(int32_t)(value / 2) : (int32_t)(value / 2);
return retVal; return retVal;
} }
static char *ZDParseString(ZoneDetect *library, uint32_t *index) static char *ZDParseString(const ZoneDetect *library, uint32_t *index)
{ {
uint32_t strLength; uint32_t strLength;
if(!ZDDecodeVariableLengthUnsigned(library, index, &strLength)) { if(!ZDDecodeVariableLengthUnsigned(library, index, &strLength)) {
@ -121,12 +121,11 @@ static char *ZDParseString(ZoneDetect *library, uint32_t *index)
} }
} }
char *str = malloc(strLength + 1); char *const str = malloc(strLength + 1);
if(str) { if(str) {
unsigned int i; for(size_t i = 0; i < strLength; i++) {
for(i = 0; i < strLength; i++) { str[i] = (char)(library->mapping[strOffset + i] ^ UINT8_C(0x80));
str[i] = library->mapping[strOffset + i] ^ 0x80;
} }
str[strLength] = 0; str[strLength] = 0;
} }
@ -157,11 +156,10 @@ static int ZDParseHeader(ZoneDetect *library)
return -1; return -1;
} }
uint32_t index = 7; uint32_t index = UINT32_C(7);
library->fieldNames = malloc(library->numFields * sizeof(char *)); library->fieldNames = malloc(library->numFields * sizeof *library->fieldNames);
unsigned int i; for(size_t i = 0; i < library->numFields; i++) {
for(i = 0; i < library->numFields; i++) {
library->fieldNames[i] = ZDParseString(library, &index); library->fieldNames[i] = ZDParseString(library, &index);
} }
@ -206,7 +204,7 @@ static int ZDPointInBox(int32_t xl, int32_t x, int32_t xr, int32_t yl, int32_t y
return 0; return 0;
} }
static ZDLookupResult ZDPointInPolygon(ZoneDetect *library, uint32_t polygonIndex, int32_t latFixedPoint, int32_t lonFixedPoint, uint64_t *distanceSqrMin) static ZDLookupResult ZDPointInPolygon(const ZoneDetect *library, uint32_t polygonIndex, int32_t latFixedPoint, int32_t lonFixedPoint, uint64_t *distanceSqrMin)
{ {
uint32_t numVertices; uint32_t numVertices;
int32_t pointLat = 0, pointLon = 0, diffLat = 0, diffLon = 0, firstLat = 0, firstLon = 0, prevLat = 0, prevLon = 0; int32_t pointLat = 0, pointLon = 0, diffLat = 0, diffLon = 0, firstLat = 0, firstLon = 0, prevLat = 0, prevLon = 0;
@ -218,9 +216,8 @@ static ZDLookupResult ZDPointInPolygon(ZoneDetect *library, uint32_t polygonInde
int prevQuadrant = 0, winding = 0; int prevQuadrant = 0, winding = 0;
uint32_t i; for(size_t i = 0; i <= (size_t)numVertices; i++) {
for(i = 0; i <= numVertices; i++) { if(i < (size_t)numVertices) {
if(i < numVertices) {
if(!ZDDecodeVariableLengthSigned(library, &polygonIndex, &diffLat)) return ZD_LOOKUP_PARSE_ERROR; if(!ZDDecodeVariableLengthSigned(library, &polygonIndex, &diffLat)) return ZD_LOOKUP_PARSE_ERROR;
if(!ZDDecodeVariableLengthSigned(library, &polygonIndex, &diffLon)) return ZD_LOOKUP_PARSE_ERROR; if(!ZDDecodeVariableLengthSigned(library, &polygonIndex, &diffLon)) return ZD_LOOKUP_PARSE_ERROR;
pointLat += diffLat; pointLat += diffLat;
@ -279,7 +276,7 @@ static ZDLookupResult ZDPointInPolygon(ZoneDetect *library, uint32_t polygonInde
/* Calculate the parameters of y=ax+b if needed */ /* Calculate the parameters of y=ax+b if needed */
if(!lineIsStraight && (distanceSqrMin || windingNeedCompare)) { if(!lineIsStraight && (distanceSqrMin || windingNeedCompare)) {
a = ((float)pointLat - (float)prevLat) / ((float)pointLon - prevLon); a = ((float)pointLat - (float)prevLat) / ((float)pointLon - (float)prevLon);
b = (float)pointLat - a * (float)pointLon; b = (float)pointLat - a * (float)pointLon;
} }
@ -291,14 +288,14 @@ static ZDLookupResult ZDPointInPolygon(ZoneDetect *library, uint32_t polygonInde
} }
/* Check if the target is on the border */ /* Check if the target is on the border */
int32_t intersectLon = ((float)latFixedPoint - b) / a; const int32_t intersectLon = (int32_t)(((float)latFixedPoint - b) / a);
if(intersectLon == lonFixedPoint) { if(intersectLon == lonFixedPoint) {
if(distanceSqrMin) *distanceSqrMin = 0; if(distanceSqrMin) *distanceSqrMin = 0;
return ZD_LOOKUP_ON_BORDER_SEGMENT; return ZD_LOOKUP_ON_BORDER_SEGMENT;
} }
/* Ok, it's not. In which direction did we go round the target? */ /* Ok, it's not. In which direction did we go round the target? */
int sign = (intersectLon < lonFixedPoint) ? 2 : -2; const int sign = (intersectLon < lonFixedPoint) ? 2 : -2;
if(quadrant == 2 || quadrant == 3) { if(quadrant == 2 || quadrant == 3) {
winding += sign; winding += sign;
} else { } else {
@ -314,32 +311,32 @@ static ZDLookupResult ZDPointInPolygon(ZoneDetect *library, uint32_t polygonInde
closestLat = (a * ((float)lonFixedPoint + a * (float)latFixedPoint) + b) / (a * a + 1); closestLat = (a * ((float)lonFixedPoint + a * (float)latFixedPoint) + b) / (a * a + 1);
} else { } else {
if(pointLon == prevLon) { if(pointLon == prevLon) {
closestLon = pointLon; closestLon = (float)pointLon;
closestLat = latFixedPoint; closestLat = (float)latFixedPoint;
} else { } else {
closestLon = lonFixedPoint; closestLon = (float)lonFixedPoint;
closestLat = pointLat; closestLat = (float)pointLat;
} }
} }
int closestInBox = ZDPointInBox(pointLon, closestLon, prevLon, pointLat, closestLat, prevLat); const int closestInBox = ZDPointInBox(pointLon, (int32_t)closestLon, prevLon, pointLat, (int32_t)closestLat, prevLat);
int64_t diffLat, diffLon; int64_t diffLat, diffLon;
if(closestInBox) { if(closestInBox) {
/* Calculate squared distance to segment. */ /* Calculate squared distance to segment. */
diffLat = closestLat - latFixedPoint; diffLat = (int64_t)(closestLat - (float)latFixedPoint);
diffLon = (closestLon - lonFixedPoint); diffLon = (int64_t)(closestLon - (float)lonFixedPoint);
} else { } else {
/* /*
* Calculate squared distance to vertices * Calculate squared distance to vertices
* It is enough to check the current point since the polygon is closed. * It is enough to check the current point since the polygon is closed.
*/ */
diffLat = pointLat - latFixedPoint; diffLat = (int64_t)(pointLat - latFixedPoint);
diffLon = (pointLon - lonFixedPoint); diffLon = (int64_t)(pointLon - lonFixedPoint);
} }
/* Note: lon has half scale */ /* Note: lon has half scale */
uint64_t distanceSqr = diffLat * diffLat + diffLon * diffLon * 4; uint64_t distanceSqr = (uint64_t)(diffLat * diffLat) + (uint64_t)(diffLon * diffLon) * 4;
if(distanceSqr < *distanceSqrMin) *distanceSqrMin = distanceSqr; if(distanceSqr < *distanceSqrMin) *distanceSqrMin = distanceSqr;
} }
} }
@ -366,8 +363,7 @@ void ZDCloseDatabase(ZoneDetect *library)
{ {
if(library) { if(library) {
if(library->fieldNames) { if(library->fieldNames) {
unsigned int i; for(size_t i = 0; i < (size_t)library->numFields; i++) {
for(i = 0; i < library->numFields; i++) {
if(library->fieldNames[i]) { if(library->fieldNames[i]) {
free(library->fieldNames[i]); free(library->fieldNames[i]);
} }
@ -378,7 +374,7 @@ void ZDCloseDatabase(ZoneDetect *library)
free(library->notice); free(library->notice);
} }
if(library->mapping) { if(library->mapping) {
munmap(library->mapping, library->length); munmap(library->mapping, (size_t)(library->length));
} }
if(library->fd >= 0) { if(library->fd >= 0) {
close(library->fd); close(library->fd);
@ -389,7 +385,7 @@ void ZDCloseDatabase(ZoneDetect *library)
ZoneDetect *ZDOpenDatabase(const char *path) ZoneDetect *ZDOpenDatabase(const char *path)
{ {
ZoneDetect *library = (ZoneDetect *)malloc(sizeof(*library)); ZoneDetect *const library = malloc(sizeof *library);
if(library) { if(library) {
memset(library, 0, sizeof(*library)); memset(library, 0, sizeof(*library));
@ -405,7 +401,7 @@ ZoneDetect *ZDOpenDatabase(const char *path)
} }
lseek(library->fd, 0, SEEK_SET); lseek(library->fd, 0, SEEK_SET);
library->mapping = mmap(NULL, library->length, PROT_READ, MAP_PRIVATE | MAP_FILE, library->fd, 0); library->mapping = mmap(NULL, (size_t)library->length, PROT_READ, MAP_PRIVATE | MAP_FILE, library->fd, 0);
if(!library->mapping) { if(!library->mapping) {
goto fail; goto fail;
} }
@ -423,19 +419,19 @@ fail:
return NULL; return NULL;
} }
ZoneDetectResult *ZDLookup(ZoneDetect *library, float lat, float lon, float *safezone) ZoneDetectResult *ZDLookup(const ZoneDetect *library, float lat, float lon, float *safezone)
{ {
int32_t latFixedPoint = ZDFloatToFixedPoint(lat, 90, library->precision); const int32_t latFixedPoint = ZDFloatToFixedPoint(lat, 90, library->precision);
int32_t lonFixedPoint = ZDFloatToFixedPoint(lon, 180, library->precision); const int32_t lonFixedPoint = ZDFloatToFixedPoint(lon, 180, library->precision);
unsigned int numResults = 0; size_t numResults = 0;
uint64_t distanceSqrMin = -1; uint64_t distanceSqrMin = (uint64_t)-1;
/* Iterate over all polygons */ /* Iterate over all polygons */
uint32_t bboxIndex = library->bboxOffset; uint32_t bboxIndex = library->bboxOffset;
int32_t metadataIndex = 0; uint32_t metadataIndex = 0;
int32_t polygonIndex = 0; uint32_t polygonIndex = 0;
ZoneDetectResult *results = malloc(sizeof(ZoneDetectResult)); ZoneDetectResult *results = malloc(sizeof *results);
if(!results) { if(!results) {
return NULL; return NULL;
} }
@ -450,7 +446,7 @@ ZoneDetectResult *ZDLookup(ZoneDetect *library, float lat, float lon, float *saf
if(!ZDDecodeVariableLengthSigned(library, &bboxIndex, &metadataIndexDelta)) break; if(!ZDDecodeVariableLengthSigned(library, &bboxIndex, &metadataIndexDelta)) break;
if(!ZDDecodeVariableLengthUnsigned(library, &bboxIndex, &polygonIndexDelta)) break; if(!ZDDecodeVariableLengthUnsigned(library, &bboxIndex, &polygonIndexDelta)) break;
metadataIndex += metadataIndexDelta; metadataIndex += (uint32_t)metadataIndexDelta;
polygonIndex += polygonIndexDelta; polygonIndex += polygonIndexDelta;
if(latFixedPoint >= minLat) { if(latFixedPoint >= minLat) {
@ -460,13 +456,13 @@ ZoneDetectResult *ZDLookup(ZoneDetect *library, float lat, float lon, float *saf
/* Indices valid? */ /* Indices valid? */
if(library->metadataOffset + metadataIndex >= library->dataOffset) continue; if(library->metadataOffset + metadataIndex >= library->dataOffset) continue;
if(library->dataOffset + polygonIndex >= library->length) continue; if(library->dataOffset + polygonIndex >= (uint32_t)library->length) continue;
ZDLookupResult lookupResult = ZDPointInPolygon(library, library->dataOffset + polygonIndex, latFixedPoint, lonFixedPoint, (safezone) ? &distanceSqrMin : NULL); const ZDLookupResult lookupResult = ZDPointInPolygon(library, library->dataOffset + polygonIndex, latFixedPoint, lonFixedPoint, (safezone) ? &distanceSqrMin : NULL);
if(lookupResult == ZD_LOOKUP_PARSE_ERROR) { if(lookupResult == ZD_LOOKUP_PARSE_ERROR) {
break; break;
} else if(lookupResult != ZD_LOOKUP_NOT_IN_ZONE) { } else if(lookupResult != ZD_LOOKUP_NOT_IN_ZONE) {
ZoneDetectResult *newResults = realloc(results, sizeof(ZoneDetectResult) * (numResults + 2)); ZoneDetectResult *const newResults = realloc(results, sizeof *newResults * (numResults + 2));
if(newResults) { if(newResults) {
results = newResults; results = newResults;
@ -488,11 +484,10 @@ ZoneDetectResult *ZDLookup(ZoneDetect *library, float lat, float lon, float *saf
} }
/* Clean up results */ /* Clean up results */
unsigned int i, j; for(size_t i = 0; i < numResults; i++) {
for(i = 0; i < numResults; i++) {
int insideSum = 0; int insideSum = 0;
ZDLookupResult overrideResult = ZD_LOOKUP_IGNORE; ZDLookupResult overrideResult = ZD_LOOKUP_IGNORE;
for(j = i; j < numResults; j++) { for(size_t j = i; j < numResults; j++) {
if(results[i].metaId == results[j].metaId) { if(results[i].metaId == results[j].metaId) {
ZDLookupResult tmpResult = results[j].lookupResult; ZDLookupResult tmpResult = results[j].lookupResult;
results[j].lookupResult = ZD_LOOKUP_IGNORE; results[j].lookupResult = ZD_LOOKUP_IGNORE;
@ -520,8 +515,8 @@ ZoneDetectResult *ZDLookup(ZoneDetect *library, float lat, float lon, float *saf
} }
/* Remove zones to ignore */ /* Remove zones to ignore */
unsigned int newNumResults = 0; size_t newNumResults = 0;
for(i = 0; i < numResults; i++) { for(size_t i = 0; i < numResults; i++) {
if(results[i].lookupResult != ZD_LOOKUP_IGNORE) { if(results[i].lookupResult != ZD_LOOKUP_IGNORE) {
results[newNumResults] = results[i]; results[newNumResults] = results[i];
newNumResults++; newNumResults++;
@ -530,11 +525,11 @@ ZoneDetectResult *ZDLookup(ZoneDetect *library, float lat, float lon, float *saf
numResults = newNumResults; numResults = newNumResults;
/* Lookup metadata */ /* Lookup metadata */
for(i = 0; i < numResults; i++) { for(size_t i = 0; i < numResults; i++) {
uint32_t tmpIndex = library->metadataOffset + results[i].metaId; uint32_t tmpIndex = library->metadataOffset + results[i].metaId;
results[i].data = malloc(library->numFields * sizeof(char *)); results[i].data = malloc(library->numFields * sizeof *results[i].data);
if(results[i].data) { if(results[i].data) {
for(j = 0; j < library->numFields; j++) { for(size_t j = 0; j < library->numFields; j++) {
results[i].data[j] = ZDParseString(library, &tmpIndex); results[i].data[j] = ZDParseString(library, &tmpIndex);
} }
} }
@ -547,7 +542,7 @@ ZoneDetectResult *ZDLookup(ZoneDetect *library, float lat, float lon, float *saf
results[numResults].data = NULL; results[numResults].data = NULL;
if(safezone) { if(safezone) {
*safezone = sqrtf(distanceSqrMin) * 90 / (float)(1 << (library->precision - 1)); *safezone = sqrtf((float)distanceSqrMin) * 90 / (float)(1 << (library->precision - 1));
} }
return results; return results;
@ -563,8 +558,7 @@ void ZDFreeResults(ZoneDetectResult *results)
while(results[index].lookupResult != ZD_LOOKUP_END) { while(results[index].lookupResult != ZD_LOOKUP_END) {
if(results[index].data) { if(results[index].data) {
unsigned int i; for(size_t i = 0; i < (size_t)results[index].numFields; i++) {
for(i = 0; i < results[index].numFields; i++) {
if(results[index].data[i]) { if(results[index].data[i]) {
free(results[index].data[i]); free(results[index].data[i]);
} }
@ -576,12 +570,12 @@ void ZDFreeResults(ZoneDetectResult *results)
free(results); free(results);
} }
const char *ZDGetNotice(ZoneDetect *library) const char *ZDGetNotice(const ZoneDetect *library)
{ {
return library->notice; return library->notice;
} }
uint8_t ZDGetTableType(ZoneDetect *library) uint8_t ZDGetTableType(const ZoneDetect *library)
{ {
return library->tableType; return library->tableType;
} }

View file

@ -27,8 +27,8 @@
#include <stdint.h> #include <stdint.h>
#ifndef _ZONEDETECT_H_ #ifndef INCL_ZONEDETECT_H_
#define _ZONEDETECT_H_ #define INCL_ZONEDETECT_H_
typedef enum { typedef enum {
ZD_LOOKUP_IGNORE = -3, ZD_LOOKUP_IGNORE = -3,
@ -60,15 +60,15 @@ extern "C" {
ZoneDetect *ZDOpenDatabase(const char *path); ZoneDetect *ZDOpenDatabase(const char *path);
void ZDCloseDatabase(ZoneDetect *library); void ZDCloseDatabase(ZoneDetect *library);
ZoneDetectResult *ZDLookup(ZoneDetect *library, float lat, float lon, float *safezone); ZoneDetectResult *ZDLookup(const ZoneDetect *library, float lat, float lon, float *safezone);
void ZDFreeResults(ZoneDetectResult *results); void ZDFreeResults(ZoneDetectResult *results);
const char *ZDGetNotice(ZoneDetect *library); const char *ZDGetNotice(const ZoneDetect *library);
uint8_t ZDGetTableType(ZoneDetect *library); uint8_t ZDGetTableType(const ZoneDetect *library);
const char *ZDLookupResultToString(ZDLookupResult result); const char *ZDLookupResultToString(ZDLookupResult result);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif #endif // INCL_ZONEDETECT_H_