+
+/*
+ * This function is going to be DEPRECATED soon.
+ */
+enum asn_strtol_result_e
+asn_strtol(const char *str, const char *end, long *lp) {
+ const char *endp = end;
+
+ switch(asn_strtol_lim(str, &endp, lp)) {
+ case ASN_STRTOL_ERROR_RANGE:
+ return ASN_STRTOL_ERROR_RANGE;
+ case ASN_STRTOL_ERROR_INVAL:
+ return ASN_STRTOL_ERROR_INVAL;
+ case ASN_STRTOL_EXPECT_MORE:
+ return ASN_STRTOL_ERROR_INVAL; /* Retain old behavior */
+ case ASN_STRTOL_OK:
+ return ASN_STRTOL_OK;
+ case ASN_STRTOL_EXTRA_DATA:
+ return ASN_STRTOL_ERROR_INVAL; /* Retain old behavior */
+ }
+
+ return ASN_STRTOL_ERROR_INVAL; /* Retain old behavior */
+}
+
+/*
+ * Parse the number in the given string until the given *end position,
+ * returning the position after the last parsed character back using the
+ * same (*end) pointer.
+ * WARNING: This behavior is different from the standard strtol(3).
+ */
+enum asn_strtol_result_e
+asn_strtol_lim(const char *str, const char **end, long *lp) {
+ int sign = 1;
+ long l;
+
+ const long upper_boundary = LONG_MAX / 10;
+ long last_digit_max = LONG_MAX % 10;
+
+ if(str >= *end) return ASN_STRTOL_ERROR_INVAL;
+
+ switch(*str) {
+ case '-':
+ last_digit_max++;
+ sign = -1;
+ /* FALL THROUGH */
+ case '+':
+ str++;
+ if(str >= *end) {
+ *end = str;
+ return ASN_STRTOL_EXPECT_MORE;
+ }
+ }
+
+ for(l = 0; str < (*end); str++) {
+ switch(*str) {
+ case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
+ case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: {
+ int d = *str - '0';
+ if(l < upper_boundary) {
+ l = l * 10 + d;
+ } else if(l == upper_boundary) {
+ if(d <= last_digit_max) {
+ if(sign > 0) {
+ l = l * 10 + d;
+ } else {
+ sign = 1;
+ l = -l * 10 - d;
+ }
+ } else {
+ *end = str;
+ return ASN_STRTOL_ERROR_RANGE;
+ }
+ } else {
+ *end = str;
+ return ASN_STRTOL_ERROR_RANGE;
+ }
+ }
+ continue;
+ default:
+ *end = str;
+ *lp = sign * l;
+ return ASN_STRTOL_EXTRA_DATA;
+ }
+ }
+
+ *end = str;
+ *lp = sign * l;
+ return ASN_STRTOL_OK;
+}
+