42
static const char hex[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
43
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
43
46
* Given a pointer into a data buffer, and to the end of the buffer,
44
47
* find the end of the (putative) line at that position in the data
390
static const char hex[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
391
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
393
394
cur=ep_alloc(MAX_BYTE_STR_LEN+3+1);
510
* Turn an RFC 3986 percent-encoded string into a byte array.
511
* XXX - We don't check for reserved characters.
513
#define HEX_DIGIT_BUF_LEN 3
515
uri_str_to_bytes(const char *uri_str, GByteArray *bytes) {
518
char hex_digit[HEX_DIGIT_BUF_LEN];
520
g_byte_array_set_size(bytes, 0);
528
if (! isascii(*p) || ! isprint(*p))
532
if (*p == '\0') return FALSE;
535
if (*p == '\0') return FALSE;
538
if (! isxdigit(hex_digit[0]) || ! isxdigit(hex_digit[1]))
540
val = (guint8) strtoul(hex_digit, NULL, 16);
541
g_byte_array_append(bytes, &val, 1);
543
g_byte_array_append(bytes, (guint8 *) p, 1);
552
* Given a GByteArray, generate a string from it that shows non-printable
553
* characters as percent-style escapes, and return a pointer to it.
556
format_uri(const GByteArray *bytes, const gchar *reserved_chars)
558
static gchar *fmtbuf[3];
559
static guint fmtbuf_len[3];
561
const gchar *reserved_def = ":/?#[]@!$&'()*+,;= ";
562
const gchar *reserved = reserved_def;
565
gboolean is_reserved = FALSE;
572
reserved = reserved_chars;
575
* Allocate the buffer if it's not already allocated.
577
if (fmtbuf[idx] == NULL) {
578
fmtbuf[idx] = g_malloc(INITIAL_FMTBUF_SIZE);
579
fmtbuf_len[idx] = INITIAL_FMTBUF_SIZE;
581
for (column = 0; column < bytes->len; column++) {
583
* Is there enough room for this character, if it expands to
584
* a percent plus 2 hex digits (which is the most it can
585
* expand to), and also enough room for a terminating '\0'?
587
if (column+2+1 >= fmtbuf_len[idx]) {
589
* Double the buffer's size if it's not big enough.
590
* The size of the buffer starts at 128, so doubling its size
591
* adds at least another 128 bytes, which is more than enough
592
* for one more character plus a terminating '\0'.
594
fmtbuf_len[idx] = fmtbuf_len[idx] * 2;
595
fmtbuf[idx] = g_realloc(fmtbuf[idx], fmtbuf_len[idx]);
597
c = bytes->data[column];
599
if (!isascii(c) || !isprint(c) || c == '%') {
603
for (i = 0; i < strlen(reserved); i++) {
604
if (c == reserved[i])
609
fmtbuf[idx][column] = c;
611
fmtbuf[idx][column] = '%';
613
fmtbuf[idx][column] = hex[c >> 4];
615
fmtbuf[idx][column] = hex[c & 0xF];
618
fmtbuf[idx][column] = '\0';
623
* Create a copy of a GByteArray
625
* @param ba The byte array to be copied.
626
* @return If ba exists, a freshly allocated copy. NULL otherwise.
628
* XXX - Should this be in strutil.c?
631
byte_array_dup(GByteArray *ba) {
637
new_ba = g_byte_array_new();
638
g_byte_array_append(new_ba, ba->data, ba->len);
508
642
#define SUBID_BUF_LEN 5
510
644
oid_str_to_bytes(const char *oid_str, GByteArray *bytes) {
701
* Compare the contents of two GByteArrays
703
* @param ba1 A byte array
704
* @param ba2 A byte array
705
* @return If both arrays are non-NULL and their lengths are equal and
706
* their contents are equal, returns TRUE. Otherwise, returns
709
* XXX - Should this be in strutil.c?
712
byte_array_equal(GByteArray *ba1, GByteArray *ba2) {
716
if (ba1->len != ba2->len)
719
if (memcmp(ba1->data, ba2->data, ba1->len) != 0)
567
726
/* Return a XML escaped representation of the unescaped string.
568
727
* The returned string must be freed when no longer in use. */