This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

tighten up tns_strtosz

  • Loading branch information...
rfk committed Apr 4, 2011
1 parent 943557e commit 91c40a6596d40513d5d0c88b7a76c810c70fa1e8
Showing with 31 additions and 11 deletions.
  1. +31 −11 tnetstring/tns_core.c
View
@@ -277,28 +277,48 @@ tns_strtosz(const char *data, size_t len, size_t *sz, char **end)
eod = data + len;
// The first character must be a digit.
- c = *pos;
- if(c < '0' || c > '9') {
- return -1;
- }
- value = c - '0';
- pos++;
-
// The netstring spec explicitly forbits padding zeros.
// If it's a zero, it must be the only char in the string.
- if(value == 0) {
- *sz = value;
+ c = *pos++;
+ switch(c) {
+ case '0':
+ *sz = 0;
*end = (char*) pos;
return 0;
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ value = c - '0';
+ break;
+ default:
+ return -1;
}
// Consume all other digits.
while(pos < eod) {
c = *pos;
- if(c < '0' || c > '9') {
+ switch(c) {
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ break;
+ default:
*sz = value;
*end = (char*) pos;
- return 0;
+ return 0;
}
value = (value * 10) + (c - '0');
pos++;

0 comments on commit 91c40a6

Please sign in to comment.