Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

code consolidation #26

Open
x42 opened this issue Feb 17, 2024 · 2 comments
Open

code consolidation #26

x42 opened this issue Feb 17, 2024 · 2 comments

Comments

@x42
Copy link
Contributor

x42 commented Feb 17, 2024

int uriIsIPv4( const char *s, int size, char **err ) {
int octets = 0;
const char *currentOctetStart = s;
char prev = 0;
for ( int i = 0; i <= size; i++ ) {
if ( prev == 0 ) {
if ( IS_DIGIT(*(s+i)) ) {
currentOctetStart = (s+i);
prev = 'd';
continue;
}
if ( *(s+i) == '.' ) {
if ( err ) {
_laaf_util_snprintf_realloc( err, NULL, 0, "IPV4 parser error : can't start with a single '.'" );
}
return 0;
}
}
if ( prev == 'p' ) {
if ( IS_DIGIT(*(s+i)) ) {
currentOctetStart = (s+i);
prev = 'd';
continue;
}
if ( *(s+i) == '.' ) {
if ( err ) {
_laaf_util_snprintf_realloc( err, NULL, 0, "IPV4 parser error : can't have successive '.'" );
}
return 0;
}
}
if ( prev == 'd' ) {
if ( IS_DIGIT(*(s+i)) ) {
prev = 'd';
continue;
}
if ( i == size || *(s+i) == '.' ) { // period
int octet = atoi(currentOctetStart);
if ( octet > 255 ) {
if ( err ) {
_laaf_util_snprintf_realloc( err, NULL, 0, "IPV4 parser error : octet %i is too high : %.*s", (octets), (int)((s+i) - currentOctetStart), currentOctetStart );
}
return 0;
}
if ( i+1 == size ) {
if ( err ) {
_laaf_util_snprintf_realloc( err, NULL, 0, "IPV4 parser error : can't end with a single '.'" );
}
return 0;
}
prev = 'p';
octets++;
continue;
}
}
if ( i == size ) {
break;
}
if ( err ) {
_laaf_util_snprintf_realloc( err, NULL, 0, "IPV4 parser error : illegal char '%c' (0x%02x)", *(s+i), *(s+i) );
}
return 0;
}

Is there any reason why you don't just use sscanf (s, "%d.%d.%d.%d", &a, &b, c&, &d) == 4 here? or better yet just use libcurl for all methods in this file?

@agfline
Copy link
Owner

agfline commented Feb 19, 2024

Well, I had this URIParser from another project and I thought I could reuse it here. This also keeps libaaf dependency free. Have you experienced any issue with it ?
Note that the same logic applies to RIFFParser.

@x42
Copy link
Contributor Author

x42 commented Feb 19, 2024

It just seems rather fragile, specifically with an int size in the API. Why not a size_t? (IMHO the function should do a NULL termination check, rather than requiring the caller to provide the correct length).

It is not something I'd wish to maintain, particularly since there are established and well maintained solutions out there that can be used with free/libre software. So I was curious why you wrote and included it here.

I understand that may be fun to write it to learn and understand..

The public functions uriDecodeString, uriIsIPv6 etc should have a libaaf_ prefix to prevent potential symbol conflicts.

Also you don't need a NULL check in uriFree and other places, free(3) handles NULL just fine, you can safely call free(NULL).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants