📄 octstr.c.debug
字号:
seems_valid(text);}void octstr_shrink_blanks(Octstr *text){ int i, j, end; seems_valid(text); gw_assert(!text->immutable); end = octstr_len(text); /* Shrink white spaces to one */ for (i = 0; i < end; i++) { if (isspace(octstr_get_char(text, i))) { /* Change the remaining space into single space. */ if (octstr_get_char(text, i) != ' ') octstr_set_char(text, i, ' '); j = i = i + 1; while (isspace(octstr_get_char(text, j))) j ++; if (j - i > 1) octstr_delete(text, i, j - i); } } seems_valid(text);}void octstr_insert_data(Octstr *ostr, long pos, const char *data, long len){ seems_valid(ostr); gw_assert(!ostr->immutable); gw_assert(pos <= ostr->len); if (len == 0) return; octstr_grow(ostr, ostr->len + len); if (ostr->len > pos) { /* only if neccessary */ memmove(ostr->data + pos + len, ostr->data + pos, ostr->len - pos); } memcpy(ostr->data + pos, data, len); ostr->len += len; ostr->data[ostr->len] = '\0'; seems_valid(ostr);}void octstr_append_data(Octstr *ostr, const char *data, long len){ gw_assert(ostr != NULL); octstr_insert_data(ostr, ostr->len, data, len);}void octstr_append_impl(Octstr *ostr1, Octstr *ostr2, const char *file, long line, const char *func){ gw_assert(ostr1 != NULL); octstr_insert_impl(ostr1, ostr2, ostr1->len, file, line, func);}void octstr_append_cstr(Octstr *ostr, const char *cstr){ octstr_insert_data(ostr, ostr->len, cstr, strlen(cstr));}void octstr_append_char(Octstr *ostr, int ch){ unsigned char c = ch; gw_assert(ch >= 0); gw_assert(ch <= UCHAR_MAX); octstr_insert_data(ostr, ostr->len, &c, 1);}void octstr_delete(Octstr *ostr1, long pos, long len){ seems_valid(ostr1); gw_assert(!ostr1->immutable); if (pos > ostr1->len) pos = ostr1->len; if (pos + len > ostr1->len) len = ostr1->len - pos; if (len > 0) { memmove(ostr1->data + pos, ostr1->data + pos + len, ostr1->len - pos - len); ostr1->len -= len; ostr1->data[ostr1->len] = '\0'; } seems_valid(ostr1);}Octstr *octstr_read_file(const char *filename){ FILE *f; Octstr *os; char buf[4096]; long n; gw_assert(filename != NULL); f = fopen(filename, "r"); if (f == NULL) { error(errno, "fopen failed: couldn't open `%s'", filename); return NULL; } os = octstr_create(""); if (os == NULL) goto error; while ((n = fread(buf, 1, sizeof(buf), f)) > 0) octstr_insert_data(os, octstr_len(os), buf, n); (void) fclose(f); return os;error: (void) fclose(f); octstr_destroy(os); return NULL;}Octstr *octstr_read_pipe(FILE *f){ Octstr *os; char buf[4096]; gw_assert(f != NULL); os = octstr_create(""); if (os == NULL) goto error; while (fgets(buf, sizeof(buf), f) != NULL) octstr_append_data(os, buf, strlen(buf)); return os;error: octstr_destroy(os); return NULL;}List *octstr_split_words(Octstr *ostr){ unsigned char *p; List *list; Octstr *word; long i, start, end; seems_valid(ostr); list = list_create(); p = ostr->data; i = 0; for (; ; ) { while (i < ostr->len && isspace(*p)) { ++p; ++i; } start = i; while (i < ostr->len && !isspace(*p)) { ++p; ++i; } end = i; if (start == end) break; word = octstr_create_from_data(ostr->data + start, end - start); list_append(list, word); } return list;}List *octstr_split(Octstr *os, Octstr *sep){ List *list; long next, pos, seplen; list = list_create(); pos = 0; seplen = octstr_len(sep); while ((next = octstr_search(os, sep, pos)) != -1) { list_append(list, octstr_copy(os, pos, next - pos)); pos = next + seplen; } if (pos < octstr_len(os)) list_append(list, octstr_copy(os, pos, octstr_len(os))); return list;}int octstr_item_match(void *item, void *pattern){ return octstr_compare(item, pattern) == 0;}int octstr_item_case_match(void *item, void *pattern){ return octstr_case_compare(item, pattern) == 0;}void octstr_dump(Octstr *ostr, int level){ unsigned char *p, *d, buf[1024], charbuf[256]; long pos; const int octets_per_line = 8; int c, this_line_begins_at; if (ostr == NULL) return; seems_valid(ostr); debug("gwlib.octstr", 0, "%*sOctet string at %p:", level, "", (void *) ostr); debug("gwlib.octstr", 0, "%*s len: %lu", level, "", (unsigned long) ostr->len); debug("gwlib.octstr", 0, "%*s size: %lu", level, "", (unsigned long) ostr->size); debug("gwlib.octstr", 0, "%*s immutable: %d", level, "", ostr->immutable); buf[0] = '\0'; p = buf; d = charbuf; this_line_begins_at = 0; for (pos = 0; pos < octstr_len(ostr); ) { c = octstr_get_char(ostr, pos); sprintf(p, "%02x ", c); p = strchr(p, '\0'); if (isprint(c)) *d++ = c; else *d++ = '.'; ++pos; if (pos - this_line_begins_at == octets_per_line) { *d = '\0'; debug("gwlib.octstr", 0, "%*s data: %s %s", level, "", buf, charbuf); buf[0] = '\0'; charbuf[0] = '\0'; p = buf; d = charbuf; this_line_begins_at = pos; } } if (pos - this_line_begins_at > 0) { *d = '\0'; debug("gwlib.octstr", 0, "%*s data: %-*.*s %s", level, "", octets_per_line*3, octets_per_line*3, buf, charbuf); } debug("gwlib.octstr", 0, "%*sOctet string dump ends.", level, "");}void octstr_dump_short(Octstr *ostr, int level, const char *name){ char buf[100]; char *p; long i; int c; if (ostr == NULL) { debug("gwlib.octstr", 0, "%*s%s: NULL", level, "", name); return; } seems_valid(ostr); if (ostr->len < 20) { p = buf; for (i = 0; i < ostr->len; i++) { c = ostr->data[i]; if (c == '\n') { *p++ = '\\'; *p++ = 'n'; } else if (!isprint(c)) { break; } else if (c == '"') { *p++ = '\\'; *p++ = '"'; } else if (c == '\\') { *p++ = '\\'; *p++ = '\\'; } else { *p++ = c; } } if (i == ostr->len) { *p++ = 0; /* We got through the loop without hitting nonprintable * characters. */ debug("gwlib.octstr", 0, "%*s%s: \"%s\"", level, "", name, buf); return; } } debug("gwlib.octstr", 0, "%*s%s:", level, "", name); octstr_dump(ostr, level + 1);}void octstr_url_encode(Octstr *ostr){ int i, n, newlen; unsigned char *str, *str2, *hexits; unsigned char c; Octstr *res; seems_valid(ostr); res = octstr_create(""); if (ostr->immutable || ostr->len == 0) return; str = ostr->data; n = 0; for (i = 0; i < ostr->len; i++) if (!is_safe[*str++]) n++; newlen = ostr->len + 2 * n; res->len = newlen; res->size = res->len + 1; res->data = gw_malloc(res->size); hexits = "0123456789ABCDEF"; str = ostr->data; str2 = res->data; for (i = 0; i < ostr->len; i++) { c = *str++; if (!is_safe[c]) { *str2++ = '%'; *str2++ = hexits[c >> 4 & 0xf]; *str2++ = hexits[c & 0xf]; } else if (c == ' ') *str2++ = '+'; else *str2++ = c; } *str2 = 0; seems_valid(res); octstr_truncate(ostr, 0); octstr_insert(ostr, res, 0); octstr_destroy(res);}int octstr_url_decode(Octstr *ostr){ long value; unsigned char *string = ostr->data; unsigned char *dptr = ostr->data; unsigned char buf[3]; /* buffer for strtol conversion */ buf[2] = '\0'; seems_valid(ostr); gw_assert(!ostr->immutable); if (ostr->len == 0) return 0; do { if (*string == '%') { if (*(string + 1) == '\0' || *(string + 2) == '\0') goto error; buf[0] = *(string + 1); buf[1] = *(string + 2); value = strtol(buf, NULL, 16); if (value >= 0 && value < 256) { *dptr = (unsigned char)value; string += 3; dptr++; continue; } warning(0, "Garbage encoded (value = %ld)", value); } if (*string == '+') { *dptr++ = ' '; string++; } else *dptr++ = *string++; } while (*string); /* we stop here because it terimates encoded string */ *dptr = '\0'; ostr->len = (dptr - ostr->data); seems_valid(ostr); return 0;error: *dptr = '\0'; ostr->len = (dptr - ostr->data); warning(0, "octstr_url_decode: corrupted end-of-string <%s>", string); seems_valid(ostr); return -1;}long octstr_get_bits(Octstr *ostr, long bitpos, int numbits){ long pos; long result; int mask; int shiftwidth; seems_valid(ostr); gw_assert(bitpos >= 0); gw_assert(numbits <= 32); gw_assert(numbits >= 0); pos = bitpos / 8; bitpos = bitpos % 8; /* This also takes care of the len == 0 case */ if (pos >= ostr->len) return 0; mask = (1 << numbits) - 1; /* It's easy if the range fits in one octet */ if (bitpos + numbits <= 8) { /* shiftwidth is the number of bits to ignore on the right. * bitpos 0 is the leftmost bit. */ shiftwidth = 8 - (bitpos + numbits); return (ostr->data[pos] >> shiftwidth) & mask; } /* Otherwise... */ result = 0; while (bitpos + numbits > 8) { result = (result << 8) | ostr->data[pos]; numbits -= (8 - bitpos); bitpos = 0; pos++; if (pos >= ostr->len) return (result << numbits) & mask; } gw_assert(bitpos == 0); result <<= numbits; result |= ostr->data[pos] >> (8 - numbits); return result & mask;}void octstr_set_bits(Octstr *ostr, long bitpos, int numbits, unsigned long value){ long pos; unsigned long mask; int shiftwidth; int bits; int maxlen; int c; seems_valid(ostr); gw_assert(!ostr->immutable); gw_assert(bitpos >= 0); gw_assert(numbits <= 32); gw_assert(numbits >= 0); maxlen = (bitpos + numbits + 7) / 8; if (maxlen > ostr->len) { octstr_grow(ostr, maxlen); /* Make sure the new octets start out with value 0 */ for (pos = ostr->len; pos < maxlen; pos++) { ostr->data[pos] = 0; } ostr->len = maxlen; ostr->data[maxlen] = 0; } mask = (1 << numbits) - 1; /* mask is also the largest value that fits */ gw_assert(value <= mask); pos = bitpos / 8; bitpos = bitpos % 8; /* Does the range fit in one octet? */ if (bitpos + numbits <= 8) { /* shiftwidth is the number of bits to ignore on the right. * bitpos 0 is the leftmost bit. */ shiftwidth = 8 - (bitpos + numbits); /* Extract the bits we don't want to affect */ c = ostr->data[pos] & ~(mask << shiftwidth); c |= value << shiftwidth; gw_assert(pos < ostr->len); ostr->data[pos] = c; return; } /* Otherwise... */ /* If speed is a problem here, we could have separate cases for * the first octet (which may have bitpos > 0), and the rest, * which don't. */ while (bitpos + numbits > 8) { /* We want this many bits from the value */ bits = 8 - bitpos; /* There are this many bits to their right in the value */ shiftwidth = numbits - bits; /* Construct a mask for "bits" bits on the far right */ mask = (1 << bits) - 1; /* Get the bits we want */ c = (value >> shiftwidth) & mask; /* Merge them with the bits that are already there */ gw_assert(pos < ostr->len); ostr->data[pos] = (ostr->data[pos] & ~mask) | c; numbits -= (8 - bitpos); bitpos = 0; pos++; } gw_assert(bitpos == 0); gw_assert(pos < ostr->len); /* Set remaining bits. This is just like the single-octet case * before the loop, except that we know bitpos is 0. */ mask = (1 << numbits) - 1; shiftwidth = 8 - numbits; c = ostr->data[pos] & ~(mask << shiftwidth); c |= value << shiftwidth; ostr->data[pos] = c; seems_valid(ostr);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -