Exemplo n.º 1
0
PJ_DEF(pj_str_t*) pj_generate_unique_string(pj_str_t *str)
{
    char *p, *end;

    PJ_CHECK_STACK();

    if (guid_chars[0] == '\0') {
	pj_enter_critical_section();
	if (guid_chars[0] == '\0') {
	    init_guid_chars();
	}
	pj_leave_critical_section();
    }

    /* This would only work if PJ_GUID_STRING_LENGTH is multiple of 2 bytes */
    pj_assert(PJ_GUID_STRING_LENGTH % 2 == 0);

    for (p=str->ptr, end=p+PJ_GUID_STRING_LENGTH; p<end; ) {
	/* Assumes rand() only has 16bit randomness */
	unsigned short val = pj_rand();
	*p++ = guid_chars[(val >> 8)   & 63];
	*p++ = guid_chars[(val & 0xFF) & 63];
    }

    str->slen = PJ_GUID_STRING_LENGTH;
    return str;
}
Exemplo n.º 2
0
PJ_DEF(pj_str_t*) pj_generate_unique_string(pj_str_t *str)
{
    char *p, *end;

    PJ_CHECK_STACK();

    if (guid_chars[0] == '\0') {
	pj_enter_critical_section();
	if (guid_chars[0] == '\0') {
	    init_guid_chars();
	}
	pj_leave_critical_section();
    }

    /* This would only work if PJ_GUID_STRING_LENGTH is multiple of 2 bytes */
    pj_assert(PJ_GUID_STRING_LENGTH % 2 == 0);

    for (p=str->ptr, end=p+PJ_GUID_STRING_LENGTH; p<end; ) {
	pj_uint32_t rand_val = pj_rand();
	pj_uint32_t rand_idx = RAND_MAX;

	for ( ; rand_idx>0 && p<end; rand_idx>>=8, rand_val>>=8, p++) {
	    *p = guid_chars[(rand_val & 0xFF) & 63];
	}
    }

    str->slen = PJ_GUID_STRING_LENGTH;
    return str;
}