int process_verify(char* path, char* hash) {
    size_t computed_hash_len = 0u;
    uint8_t computed_hash[EVP_MAX_MD_SIZE];
    uint8_t hash_buf[EVP_MAX_MD_SIZE * 2];
    if (process_hash(path, computed_hash, &computed_hash_len)) {
        return 1;
    }

    strcpy((char*) hash_buf, hash);
    for (size_t i = 0; i < computed_hash_len; i++) {
        if (!isxdigit(hash_buf[2*i]) || !isxdigit(hash_buf[2*i+1])) {
            printf(
                "ERROR: invalid hash byte (%c%c)\n",
                hash_buf[2*i],
                hash_buf[2*i+1]);
            return 1;
        }

        hash_buf[i] =
            (digittoint(hash_buf[2*i]) << 4)
            | digittoint(hash_buf[2*i+1]);
    }

    if (!memcmp(computed_hash, hash_buf, computed_hash_len)) {
        printf("OK\n");
    } else {
        printf("MISMATCH\n");
    }

    return 0;
}
Пример #2
0
/*
 * Scan a string of hexadecimal digits (the format nan(3) expects) and
 * make a bit array (using the local endianness). We stop when we
 * encounter an invalid character, NUL, etc.  If we overflow, we do
 * the same as gcc's __builtin_nan(), namely, discard the high order bits.
 *
 * The format this routine accepts needs to be compatible with what is used
 * in contrib/gdtoa/hexnan.c (for strtod/scanf) and what is used in
 * __builtin_nan(). In fact, we're only 100% compatible for strings we
 * consider valid, so we might be violating the C standard. But it's
 * impossible to use nan(3) portably anyway, so this seems good enough.
 */
void
_scan_nan(uint32_t *words, int num_words, const char *s)
{
	int si;		/* index into s */
	int bitpos;	/* index into words (in bits) */

	bzero(words, num_words * sizeof(uint32_t));

	/* Allow a leading '0x'. (It's expected, but redundant.) */
	if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
		s += 2;

	/* Scan forwards in the string, looking for the end of the sequence. */
	for (si = 0; isxdigit(s[si]); si++)
		;

	/* Scan backwards, filling in the bits in words[] as we go. */
#if _BYTE_ORDER == _LITTLE_ENDIAN
	for (bitpos = 0; bitpos < 32 * num_words; bitpos += 4) {
#else
	for (bitpos = 32 * num_words - 4; bitpos >= 0; bitpos -= 4) {
#endif
		if (--si < 0)
			break;
		words[bitpos / 32] |= digittoint(s[si]) << (bitpos % 32);
	}
}

double
nan(const char *s)
{
	union {
		double d;
		uint32_t bits[2];
	} u;

	_scan_nan(u.bits, 2, s);
#if _BYTE_ORDER == _LITTLE_ENDIAN
	u.bits[1] |= 0x7ff80000;
#else
	u.bits[0] |= 0x7ff80000;
#endif
	return (u.d);
}

float
nanf(const char *s)
{
	union {
		float f;
		uint32_t bits[1];
	} u;

	_scan_nan(u.bits, 1, s);
	u.bits[0] |= 0x7fc00000;
	return (u.f);
}

#if (LDBL_MANT_DIG == 53)
__weak_alias(nanl, nan);
static int parseDigits(const char* str, const char* end) {
    int result = 0;
    for (; str < end; ++str) {
        if (!isdigit(*str))
            return 0;
        result = 10*result + digittoint(*str);
    }
    return result;
}
static void
parse_ports(const char *portspec)
{
	const char *p, *q;
	int port, end;

	if (ports == NULL)
		if ((ports = (int *)calloc(65536 / INT_BIT, sizeof(int))) == NULL)
			err(1, "calloc()");
	p = portspec;
	while (*p != '\0') {
		if (!isdigit(*p))
			errx(1, "syntax error in port range");
		for (q = p; *q != '\0' && isdigit(*q); ++q)
			/* nothing */ ;
		for (port = 0; p < q; ++p)
			port = port * 10 + digittoint(*p);
		if (port < 0 || port > 65535)
			errx(1, "invalid port number");
		SET_PORT(port);
		switch (*p) {
		case '-':
			++p;
			break;
		case ',':
			++p;
			/* fall through */
		case '\0':
		default:
			continue;
		}
		for (q = p; *q != '\0' && isdigit(*q); ++q)
			/* nothing */ ;
		for (end = 0; p < q; ++p)
			end = end * 10 + digittoint(*p);
		if (end < port || end > 65535)
			errx(1, "invalid port number");
		while (port++ < end)
			SET_PORT(port);
		if (*p == ',')
			++p;
	}
}
Пример #5
0
static int
getdata(const char *arg, u_int8_t *data, int maxlen)
{
	const char *cp = arg;
	int len;

	if (cp[0] == '0' && (cp[1] == 'x' || cp[1] == 'X'))
		cp += 2;
	len = 0;
	while (*cp) {
		int b0, b1;
		if (cp[0] == ':' || cp[0] == '-' || cp[0] == '.') {
			cp++;
			continue;
		}
		if (!isxdigit(cp[0])) {
			fprintf(stderr, "%s: invalid data value %c (not hex)\n",
				progname, cp[0]);
			exit(-1);
		}
		b0 = digittoint(cp[0]);
		if (cp[1] != '\0') {
			if (!isxdigit(cp[1])) {
				fprintf(stderr, "%s: invalid data value %c "
					"(not hex)\n", progname, cp[1]);
				exit(-1);
			}
			b1 = digittoint(cp[1]);
			cp += 2;
		} else {			/* fake up 0<n> */
			b1 = b0, b0 = 0;
			cp += 1;
		}
		if (len > maxlen) {
			fprintf(stderr,
				"%s: too much data in %s, max %u bytes\n",
				progname, arg, maxlen);
		}
		data[len++] = (b0<<4) | b1;
	}
	return len;
}
static char convertEscape(const char **in) {
    char c = *++(*in);
    switch (c) {
        case 'u': {
            // \u is a Unicode escape; 4 hex digits follow.
            const char* digits = *in + 1;
            *in += 4;
            int uc = (digittoint(digits[0]) << 12) | (digittoint(digits[1]) << 8) |
            (digittoint(digits[2]) <<  4) | (digittoint(digits[3]));
            if (uc > 127)
                LOGW("SQLiteJsonCollator can't correctly compare \\u%.4s", digits);
            return (char)uc;
        }
        case 'b':   return '\b';
        case 'n':   return '\n';
        case 'r':   return '\r';
        case 't':   return '\t';
        default:    return c;
    }
}
Пример #7
0
/**
 * Convert string into a bitset. Inverse of bitset_to_str().
 *
 */
bitset *str_to_bitset(char *str, char **end)
{
	int			nbits = 0;
	int			bytes;
	int			n;
	int			pos;
	int			b;
        int                     len;
	bitset                  *bp;
        char                    dst[1024];

        if (!str)
                return NULL;

        /* hex string has 0x prefix */
        if (str[0] == '0' && str[1] == 'x')
                str = str + 2;

        len = strlen(str);
        if (len % 2) {
                nbits = (len + 1) << 2;
                bytes = NUM_BYTES(nbits);
                pos = (bytes << 3) - 5;
        } else {
                nbits = len << 2;
                bytes = NUM_BYTES(nbits);
                pos = (bytes << 3) - 1;
        }

        if (0)
                hex2binary(str, dst);

	bp = bitset_new(nbits);

	for (; *str != '\0' && isxdigit(*str) && pos >= 0; str++) {
		b = digittoint(*str);
		for (n = 3; n >= 0; n--, pos--) {
			if (b & (1 << n)) {
				bitset_set(bp, pos);
			}
		}
	}

	if (end != NULL)
                *end = str - 1;

	return bp;
}
Пример #8
0
/*
 * Convert a hexadecimal string containing len digits into an integer.
 */
unsigned int
hex_str_to_int(char *str, int len, char **end)
{
	int				n;
	unsigned int	val = 0;

	for (n = 0; n < len && *str != '\0' && isxdigit(*str); n++, str++) {
		val <<= 4;
		val += digittoint(*str);
	}

	if (end != NULL) {
		*end = str;
	}

	return val;
}
Пример #9
0
char *colon_convert( char *source )
{
	char *result = NULL;
    
    
	int colon_count = 0;
	char *temp = source;
	int found = 0;
	
	while( *temp != '\0' )
	{
		if( *(temp++) == ':' ) colon_count++;
	}
	
	if( colon_count > 0 )
	{
		result = malloc( strlen(source) + (colon_count*4) + 1 );
		char *dest = result;
		
		while( *source != '\0' )
		{
			if( *source == ':' )
			{
				if( source[1] != '\0' || source[2] != '\0' )
				{
					if( is_cap_hex(source[1]) && is_cap_hex(source[2]) )
					{
						char in_buffer;

						in_buffer = (digittoint(source[1]) << 4) + digittoint(source[2]);
						*(dest++) = in_buffer;

						found = 1;
						source += 3;
					}
					else
					{
						*(dest++) = *(source++);
					}
				}
				else
				{
					*(dest++) = *(source++);
				}
			}
			else
			{
				*(dest++) = *(source++);
			}
		}
		
        *dest = '\0';
        
		if( found == 0 )
		{
			free( result );
			result = NULL;
		}
	}

	return result;	
}
JNIEXPORT jint JNICALL Java_com_couchbase_lite_android_SQLiteJsonCollator_testDigitToInt
(JNIEnv *env, jclass clazz, jint digit) {
    int result = digittoint(digit);
    return result;
}
Пример #11
0
bool parseVersionString(CFStringRef string, struct Version *outVersion) {
    if (!string) {
        return false;
    }
    bool parsed = true;

    unsigned myMajor = 0U, myMinor = 0U, myIncremental = 0U, myReleaseType = releaseType_release, myDevelopment = 0U;

    CFIndex maxAllocation = getpagesize();
    CFRange range = { 0, CFStringGetLength(string) };
    Boolean canConvert = CFStringGetBytes(string, range,
                                          kCFStringEncodingUTF8,
                                          /*lossByte*/ 0U,
                                          /*isExternalRepresentation*/ false,
                                          /*buffer*/ NULL,
                                          maxAllocation,
                                          &maxAllocation);
    if (!canConvert) {
        return false;
    }

    char *buf = malloc(maxAllocation);
    if (!buf) {
        return false;
    }

    CFIndex i = 0;
    CFIndex length = 0;
    canConvert = CFStringGetBytes(string, range,
                                  kCFStringEncodingUTF8,
                                  /*lossByte*/ 0U,
                                  /*isExternalRepresentation*/ false,
                                  (UInt8 *)buf,
                                  maxAllocation,
                                  &length);
    if (canConvert) {
        //converted to UTF-8 successfully. parse it.

        while ((i < length) && isspace(buf[i])) {
            ++i;
        }
        if (!isdigit(buf[i])) {
            parsed = false;
            goto end;
        }

        //major version
        while (i < length) {
            if (!isdigit(buf[i])) {
                break;
            }
            myMajor *= 10U;
            myMajor += digittoint(buf[i++]);
        }
        if (i >= length) {
            goto end;
        }

        //separator
        if (buf[i] != '.') {
            goto end;
        }
        ++i;

        //minor version
        while (i < length) {
            if (!isdigit(buf[i])) {
                break;
            }
            myMinor *= 10U;
            myMinor += digittoint(buf[i++]);
        }
        if (i >= length) {
            goto end;
        }

        //separator
        if (buf[i] == '.') {
            ++i;

            //incremental version
            while (i < length) {
                if (!isdigit(buf[i])) {
                    break;
                }
                myIncremental *= 10U;
                myIncremental += digittoint(buf[i++]);
            }
            if (i >  length) {
                goto end;
            }
        }

        //release type
        if (i != length) {
            while ((i < length) && isspace(buf[i])) ++i;

            if (i < length) {
                char releaseTypeChar = tolower(buf[i++]);
                switch (releaseTypeChar) {
                case 'b':
                    myReleaseType = releaseType_beta;
                    break;
                case 'a':
                    myReleaseType = releaseType_alpha;
                    break;
                case 'd':
                    myReleaseType = releaseType_development;
                    break;
                case 's':
                    myReleaseType = releaseType_svn;
                    if ((i < length) && (buf[i] == 'v')) {
                        ++i;
                        if ((i < length) && (buf[i] == 'n')) {
                            ++i;
                        }
                    }
                    break;
                case 'h':
                    myReleaseType = releaseType_svn;
                    if ((i < length) && (buf[i] == 'g')) {
                        ++i;
                    }
                    break;
                }

                while ((i < length) && isspace(buf[i])) {
                    ++i;
                }
                //for example: "0.6.2 SVN r1558". we want to skip the 'r'.
                if ((i < length) && (myReleaseType == releaseType_svn) && (tolower(buf[i]) == 'r')) {
                    ++i;
                }

                //if there's no development version,
                //	default to 0 for releases and svn versions,
                //	and 1 for development versions, alphas, and betas.
                if (i == length) {
                    myDevelopment = ((myReleaseType != releaseType_release) && (myReleaseType != releaseType_svn));
                } else {
                    //development version
                    while (i < length) {
                        if (!isdigit(buf[i])) {
                            break;
                        }
                        myDevelopment *= 10U;
                        myDevelopment += digittoint(buf[i++]);
                    } //while(i < length)
                } //if(++i != length)
            } //if(i < length)
        } //if(i != length)

        while ((i < length) && isspace(buf[i])) {
            ++i;
        }
        if (i < length)
            parsed = false;
    } //if(canConvert)

end:
    free(buf);

    if (outVersion) {
        outVersion->major		= myMajor;
        outVersion->minor		= myMinor;
        outVersion->incremental	= myIncremental;
        outVersion->releaseType	= myReleaseType;
        outVersion->development	= myDevelopment;
    }

    return parsed;
}