mirror of
https://github.com/BertoldVdb/ZoneDetect.git
synced 2026-04-27 14:57:40 +00:00
added cc flags and enforced stricter compliance to C99
This commit is contained in:
parent
36126a8b96
commit
7d0b7e8832
2 changed files with 46 additions and 52 deletions
|
|
@ -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
|
||||||
|
|
@ -43,7 +43,7 @@ all: $(EXECUTABLE)
|
||||||
$(EXECUTABLE): $(OBJECTS_OBJ)
|
$(EXECUTABLE): $(OBJECTS_OBJ)
|
||||||
$(CC) $(LDFLAGS) $(OBJECTS_OBJ) -o $@
|
$(CC) $(LDFLAGS) $(OBJECTS_OBJ) -o $@
|
||||||
$(STRIP) $@
|
$(STRIP) $@
|
||||||
|
|
||||||
|
|
||||||
obj/%.o: src/%.c $(INCLUDES_SRC)
|
obj/%.o: src/%.c $(INCLUDES_SRC)
|
||||||
$(CC) $(CFLAGS) $< -o $@
|
$(CC) $(CFLAGS) $< -o $@
|
||||||
|
|
|
||||||
|
|
@ -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>
|
||||||
|
|
@ -56,26 +57,25 @@ 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;
|
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(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 *buffer = library->mapping + *index;
|
||||||
uint8_t *bufferEnd = library->mapping + library->length - 1;
|
uint8_t *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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -95,7 +95,7 @@ static unsigned int ZDDecodeVariableLengthSigned(ZoneDetect *library, uint32_t *
|
||||||
{
|
{
|
||||||
uint32_t value = 0;
|
uint32_t value = 0;
|
||||||
unsigned int retVal = ZDDecodeVariableLengthUnsigned(library, index, &value);
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -124,9 +124,8 @@ static char *ZDParseString(ZoneDetect *library, uint32_t *index)
|
||||||
char *str = malloc(strLength + 1);
|
char *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(char *));
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -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,7 +288,7 @@ 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;
|
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;
|
||||||
|
|
@ -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);
|
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);
|
||||||
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
@ -427,13 +423,13 @@ ZoneDetectResult *ZDLookup(ZoneDetect *library, float lat, float lon, float *saf
|
||||||
{
|
{
|
||||||
int32_t latFixedPoint = ZDFloatToFixedPoint(lat, 90, library->precision);
|
int32_t latFixedPoint = ZDFloatToFixedPoint(lat, 90, library->precision);
|
||||||
int32_t lonFixedPoint = ZDFloatToFixedPoint(lon, 180, library->precision);
|
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(ZoneDetectResult));
|
||||||
if(!results) {
|
if(!results) {
|
||||||
|
|
@ -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,7 +456,7 @@ 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);
|
ZDLookupResult lookupResult = ZDPointInPolygon(library, library->dataOffset + polygonIndex, latFixedPoint, lonFixedPoint, (safezone) ? &distanceSqrMin : NULL);
|
||||||
if(lookupResult == ZD_LOOKUP_PARSE_ERROR) {
|
if(lookupResult == ZD_LOOKUP_PARSE_ERROR) {
|
||||||
|
|
@ -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(char *));
|
||||||
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]);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue