/* * Check for DNAME type. Nothing is allowed below it */ static void check_dname(zone_type* zone) { domain_type* domain; for(domain = zone->apex; domain && domain_is_subdomain(domain, zone->apex); domain=domain_next(domain)) { if(domain->is_existing) { /* there may not be DNAMEs above it */ domain_type* parent = domain->parent; #ifdef NSEC3 if(domain_has_only_NSEC3(domain, NULL)) continue; #endif while(parent) { if(domain_find_rrset_any(parent, TYPE_DNAME)) { zc_error("While checking node %s,", domain_to_string(domain)); zc_error("DNAME at %s has data below it. " "This is not allowed (rfc 2672).", domain_to_string(parent)); return; } parent = parent->parent; } } } }
zc_hashtable_t *zc_hashtable_new(size_t a_size, zc_hashtable_hash_fn hash, zc_hashtable_equal_fn equal, zc_hashtable_del_fn key_del, zc_hashtable_del_fn value_del) { zc_hashtable_t *a_table; a_table = calloc(1, sizeof(*a_table)); if (!a_table) { zc_error("calloc fail, errno[%d]", errno); return NULL; } a_table->tab = calloc(a_size, sizeof(*(a_table->tab))); if (!a_table->tab) { zc_error("calloc fail, errno[%d]", errno); free(a_table); return NULL; } a_table->tab_size = a_size; a_table->nelem = 0; a_table->hash = hash; a_table->equal = equal; /* these two could be NULL */ a_table->key_del = key_del; a_table->value_del = value_del; return a_table; }
int zc_hashtable_put(zc_hashtable_t * a_table, void *a_key, void *a_value) { int rc = 0; unsigned int i; zc_hashtable_entry_t *p = NULL; i = a_table->hash(a_key) % a_table->tab_size; for (p = (a_table->tab)[i]; p; p = p->next) { if (a_table->equal(a_key, p->key)) break; } if (p) { if (a_table->key_del) { a_table->key_del(p->key); } if (a_table->value_del) { a_table->value_del(p->value); } p->key = a_key; p->value = a_value; return 0; } else { if (a_table->nelem > a_table->tab_size * 1.3) { rc = zc_hashtable_rehash(a_table); if (rc) { zc_error("rehash fail"); return -1; } } p = calloc(1, sizeof(*p)); if (!p) { zc_error("calloc fail, errno[%d]", errno); return -1; } p->hash_key = a_table->hash(a_key); p->key = a_key; p->value = a_value; p->next = NULL; p->prev = NULL; i = p->hash_key % a_table->tab_size; if ((a_table->tab)[i]) { (a_table->tab)[i]->prev = p; p->next = (a_table->tab)[i]; } (a_table->tab)[i] = p; a_table->nelem++; } return 0; }
zlog_buf_t *zlog_buf_new(size_t buf_size_min, size_t buf_size_max, const char *truncate_str) { zlog_buf_t *a_buf; if (buf_size_min == 0) { zc_error("buf_size_min == 0, not allowed"); return NULL; } if (buf_size_max != 0 && buf_size_max < buf_size_min) { zc_error("buf_size_max[%lu] < buf_size_min[%lu] && buf_size_max != 0", (unsigned long)buf_size_max, (unsigned long)buf_size_min); return NULL; } a_buf = calloc(1, sizeof(*a_buf)); if (!a_buf) { zc_error("calloc fail, errno[%d]", errno); return NULL; } if (truncate_str) { if (strlen(truncate_str) > sizeof(a_buf->truncate_str) - 1) { zc_error("truncate_str[%s] overflow", truncate_str); goto err; } else { strcpy(a_buf->truncate_str, truncate_str); } a_buf->truncate_str_len = strlen(truncate_str); } a_buf->size_min = buf_size_min; a_buf->size_max = buf_size_max; a_buf->size_real = a_buf->size_min; a_buf->start = calloc(1, a_buf->size_real); if (!a_buf->start) { zc_error("calloc fail, errno[%d]", errno); goto err; } a_buf->tail = a_buf->start; a_buf->end_plus_1 = a_buf->start + a_buf->size_real; a_buf->end = a_buf->end_plus_1 - 1; //zlog_buf_profile(a_buf, ZC_DEBUG); return a_buf; err: zlog_buf_del(a_buf); return NULL; }
/* return 0: success * return <0: fail, set size_real to -1; * return >0: by conf limit, can't extend size * increment must > 0 */ static int zlog_buf_resize(zlog_buf_t * a_buf, size_t increment) { int rc = 0; size_t new_size = 0; size_t len = 0; char *p = NULL; if (a_buf->size_max != 0 && a_buf->size_real >= a_buf->size_max) { zc_error("a_buf->size_real[%ld] >= a_buf->size_max[%ld]", a_buf->size_real, a_buf->size_max); return 1; } if (a_buf->size_max == 0) { /* unlimit */ new_size = a_buf->size_real + 1.5 * increment; } else { /* limited */ if (a_buf->size_real + increment <= a_buf->size_max) { new_size = a_buf->size_real + increment; } else { new_size = a_buf->size_max; rc = 1; } } len = a_buf->tail - a_buf->start; p = realloc(a_buf->start, new_size); if (!p) { zc_error("realloc fail, errno[%d]", errno); free(a_buf->start); a_buf->start = NULL; a_buf->tail = NULL; a_buf->end = NULL; a_buf->end_plus_1 = NULL; /* set size_real = -1, so other func know buf is unavailiable */ a_buf->size_real = -1; return -1; } else { a_buf->start = p; a_buf->tail = p + len; //memset(a_buf->tail, 0x00, new_size - len); a_buf->size_real = new_size; a_buf->end_plus_1 = a_buf->start + new_size; a_buf->end = a_buf->end_plus_1 - 1; } return rc; }
void zc_hashtable_del(zc_hashtable_t * a_table) { size_t i; zc_hashtable_entry_t *p; zc_hashtable_entry_t *q; if (!a_table) { zc_error("a_table[%p] is NULL, just do nothing", a_table); return; } for (i = 0; i < a_table->tab_size; i++) { for (p = (a_table->tab)[i]; p; p = q) { q = p->next; if (a_table->key_del) { a_table->key_del(p->key); } if (a_table->value_del) { a_table->value_del(p->value); } free(p); } } if (a_table->tab) free(a_table->tab); free(a_table); return; }
size_t zc_parse_byte_size(char *astring) { /* Parse size in bytes depending on the suffix. Valid suffixes are KB, MB and GB */ char *p; char *q; size_t sz; long res; int c, m; zc_assert(astring, 0); /* clear space */ for (p = q = astring; *p != '\0'; p++) { if (isspace(*p)) { continue; } else { *q = *p; q++; } } *q = '\0'; sz = strlen(astring); res = strtol(astring, (char **)NULL, 10); if (res <= 0) return 0; if (astring[sz - 1] == 'B' || astring[sz - 1] == 'b') { c = astring[sz - 2]; m = 1024; } else { c = astring[sz - 1]; m = 1000; } switch (c) { case 'K': case 'k': res *= m; break; case 'M': case 'm': res *= m * m; break; case 'G': case 'g': res *= m * m * m; break; default: if (!isdigit(c)) { zc_error("Wrong suffix parsing " "size in bytes for string [%s], ignoring suffix", astring); } break; } return (res); }
void zc_hashtable_remove(zc_hashtable_t * a_table, const void *a_key) { zc_hashtable_entry_t *p; unsigned int i; if (!a_table || !a_key) { zc_error("a_table[%p] or a_key[%p] is NULL, just do nothing", a_table, a_key); return; } i = a_table->hash(a_key) % a_table->tab_size; for (p = (a_table->tab)[i]; p; p = p->next) { if (a_table->equal(a_key, p->key)) break; } if (!p) { zc_error("p[%p] not found in hashtable", p); return; } if (a_table->key_del) { a_table->key_del(p->key); } if (a_table->value_del) { a_table->value_del(p->value); } if (p->next) { p->next->prev = p->prev; } if (p->prev) { p->prev->next = p->next; } else { unsigned int i; i = p->hash_key % a_table->tab_size; a_table->tab[i] = p->next; } free(p); a_table->nelem--; return; }
int zlog_buf_vprintf(zlog_buf_t * a_buf, const char *format, va_list args) { va_list ap; size_t size_left; int nwrite; if (!a_buf->start) { zc_error("pre-use of zlog_buf_resize fail, so can't convert"); return -1; } va_copy(ap, args); size_left = a_buf->end_plus_1 - a_buf->tail; nwrite = vsnprintf(a_buf->tail, size_left, format, ap); if (nwrite >= 0 && nwrite < size_left) { a_buf->tail += nwrite; //*(a_buf->tail) = '\0'; return 0; } else if (nwrite < 0) { zc_error("vsnprintf fail, errno[%d]", errno); zc_error("nwrite[%d], size_left[%ld], format[%s]", nwrite, size_left, format); return -1; } else if (nwrite >= size_left) { int rc; //zc_debug("nwrite[%d]>=size_left[%ld],format[%s],resize", nwrite, size_left, format); rc = zlog_buf_resize(a_buf, nwrite - size_left + 1); if (rc > 0) { zc_error("conf limit to %ld, can't extend, so truncate", a_buf->size_max); va_copy(ap, args); size_left = a_buf->end_plus_1 - a_buf->tail; vsnprintf(a_buf->tail, size_left, format, ap); a_buf->tail += size_left - 1; //*(a_buf->tail) = '\0'; zlog_buf_truncate(a_buf); return 1; } else if (rc < 0) { zc_error("zlog_buf_resize fail"); return -1; } else { //zc_debug("zlog_buf_resize succ, to[%ld]", a_buf->size_real); va_copy(ap, args); size_left = a_buf->end_plus_1 - a_buf->tail; nwrite = vsnprintf(a_buf->tail, size_left, format, ap); if (nwrite < 0) { zc_error("vsnprintf fail, errno[%d]", errno); zc_error("nwrite[%d], size_left[%ld], format[%s]", nwrite, size_left, format); return -1; } else { a_buf->tail += nwrite; //*(a_buf->tail) = '\0'; return 0; } } } return 0; }
static int zlog_spec_write_mdc(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf) { zlog_mdc_kv_t *a_mdc_kv; a_mdc_kv = zlog_mdc_get_kv(a_thread->mdc, a_spec->mdc_key); if (!a_mdc_kv) { zc_error("zlog_mdc_get_kv key[%s] fail", a_spec->mdc_key); return 0; } return zlog_buf_append(a_buf, a_mdc_kv->value, a_mdc_kv->value_len); }
bool unlock_file(LOCK_FD fd) { if (fd == INVALID_LOCK_FD) { return true; } #ifdef _WIN32 bool ret = CloseHandle(fd); if (ret == false) { DWORD err = GetLastError(); zc_error("unlock file error : %d ", err); } #else bool ret = close(fd) == 0; if (ret == false) { zc_error("unlock file error : %s ", strerror(errno)); } #endif return ret; }
LOCK_FD lock_file(char* path) { if (!path || strlen(path) <= 0) { return INVALID_LOCK_FD; } #ifdef _WIN32 LOCK_FD fd = CreateFile(path, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (fd == INVALID_LOCK_FD) { DWORD err = GetLastError(); zc_error("lock file error : %d ", err); } #else LOCK_FD fd = open(path, O_RDWR | O_CREAT | O_EXCL, S_IRWXU | S_IRWXG | S_IRWXO); if (fd == INVALID_LOCK_FD) { zc_error("lock file error : %s ", strerror(errno)); } #endif return fd; }
zc_arraylist_t *zc_arraylist_new(zc_arraylist_del_fn del) { zc_arraylist_t *a_list; a_list = (zc_arraylist_t *) calloc(1, sizeof(zc_arraylist_t)); if (!a_list) { zc_error("calloc fail, errno[%d]", errno); return NULL; } a_list->size = ARRAY_LIST_DEFAULT_SIZE; a_list->len = 0; /* this could be NULL */ a_list->del = del; a_list->array = (void **)calloc(a_list->size, sizeof(void *)); if (!a_list->array) { zc_error("calloc fail, errno[%d]", errno); free(a_list); return NULL; } return a_list; }
int zc_arraylist_set(zc_arraylist_t * a_list, int idx, void *data) { if (idx > a_list->size - 1) { if (zc_arraylist_expand_inner(a_list, idx)) { zc_error("expand_internal fail"); return -1; } } if (a_list->array[idx] && a_list->del) a_list->del(a_list->array[idx]); a_list->array[idx] = data; if (a_list->len <= idx) a_list->len = idx + 1; return 0; }
static int zlog_spec_gen_archive_path_reformat(zlog_spec_t * a_spec, zlog_thread_t * a_thread) { int rc; zlog_buf_restart(a_thread->pre_path_buf); rc = a_spec->write_buf(a_spec, a_thread, a_thread->pre_path_buf); if (rc < 0) { zc_error("a_spec->gen_buf fail"); return -1; } else if (rc > 0) { /* buf is full, try printf */ } return zlog_buf_adjust_append(a_thread->archive_path_buf, zlog_buf_str(a_thread->pre_path_buf), zlog_buf_len(a_thread->pre_path_buf), a_spec->left_adjust, a_spec->min_width, a_spec->max_width); }
static int zc_arraylist_expand_inner(zc_arraylist_t * a_list, int max) { void *tmp; int new_size; new_size = zc_max(a_list->size * 2, max); tmp = realloc(a_list->array, new_size * sizeof(void *)); if (!tmp) { zc_error("realloc fail, errno[%d]", errno); free(a_list->array); return -1; } a_list->array = (void **)tmp; memset(a_list->array + a_list->size, 0x00, (new_size - a_list->size) * sizeof(void *)); a_list->size = new_size; return 0; }
/* assum idx < len */ static int zc_arraylist_insert_inner(zc_arraylist_t * a_list, int idx, void *data) { if (a_list->array[idx] == NULL) { a_list->array[idx] = data; return 0; } if (a_list->len > a_list->size - 1) { if (zc_arraylist_expand_inner(a_list, 0)) { zc_error("expand_internal fail"); return -1; } } memmove(a_list->array + idx + 1, a_list->array + idx, (a_list->len - idx) * sizeof(void *)); a_list->array[idx] = data; a_list->len++; return 0; }
int zlog_buf_printf_hex(zlog_buf_t * a_buf, uint32_t ui32, int width) { unsigned char *p; char *q; unsigned char tmp[ZLOG_INT32_LEN + 1]; size_t num_len, zero_len, out_len; static unsigned char hex[] = "0123456789abcdef"; //static unsigned char HEX[] = "0123456789ABCDEF"; if (a_buf->size_real < 0) { zc_error("pre-use of zlog_buf_resize fail, so can't convert"); return -1; } p = tmp + ZLOG_INT32_LEN; do { /* the "(uint32_t)" cast disables the BCC's warning */ *--p = hex[(uint32_t) (ui32 & 0xf)]; } while (ui32 >>= 4); #if 0 } else { /* is_hex == 2 */
static int zc_hashtable_rehash(zc_hashtable_t * a_table) { size_t i; size_t j; size_t tab_size; zc_hashtable_entry_t **tab; zc_hashtable_entry_t *p; zc_hashtable_entry_t *q; tab_size = 2 * a_table->tab_size; tab = calloc(tab_size, sizeof(*tab)); if (!tab) { zc_error("calloc fail, errno[%d]", errno); return -1; } for (i = 0; i < a_table->tab_size; i++) { for (p = (a_table->tab)[i]; p; p = q) { q = p->next; p->next = NULL; p->prev = NULL; j = p->hash_key % tab_size; if (tab[j]) { tab[j]->prev = p; p->next = tab[j]; } tab[j] = p; } } free(a_table->tab); a_table->tab = tab; a_table->tab_size = tab_size; return 0; }
int main() { zc_error("sscanf fail"); int here; return 0; }
/* * Convert an APL RR RDATA element. */ uint16_t * zparser_conv_apl_rdata(region_type *region, char *str) { int negated = 0; uint16_t address_family; uint8_t prefix; uint8_t maximum_prefix; uint8_t length; uint8_t address[IP6ADDRLEN]; char *colon = strchr(str, ':'); char *slash = strchr(str, '/'); int af; int rc; uint16_t rdlength; uint16_t *r; uint8_t *t; char *end; long p; if (!colon) { zc_error("address family separator is missing"); return NULL; } if (!slash) { zc_error("prefix separator is missing"); return NULL; } *colon = '\0'; *slash = '\0'; if (*str == '!') { negated = 1; ++str; } if (strcmp(str, "1") == 0) { address_family = htons(1); af = AF_INET; length = sizeof(in_addr_t); maximum_prefix = length * 8; } else if (strcmp(str, "2") == 0) { address_family = htons(2); af = AF_INET6; length = IP6ADDRLEN; maximum_prefix = length * 8; } else { zc_error("invalid address family '%s'", str); return NULL; } rc = inet_pton(af, colon + 1, address); if (rc == 0) { zc_error("invalid address '%s'", colon + 1); return NULL; } else if (rc == -1) { zc_error("inet_pton failed: %s", strerror(errno)); return NULL; } /* Strip trailing zero octets. */ while (length > 0 && address[length - 1] == 0) --length; p = strtol(slash + 1, &end, 10); if (p < 0 || p > maximum_prefix) { zc_error("prefix not in the range 0 .. %d", maximum_prefix); return NULL; } else if (*end != '\0') { zc_error("invalid prefix '%s'", slash + 1); return NULL; } prefix = (uint8_t) p; rdlength = (sizeof(address_family) + sizeof(prefix) + sizeof(length) + length); r = alloc_rdata(region, rdlength); t = (uint8_t *) (r + 1); memcpy(t, &address_family, sizeof(address_family)); t += sizeof(address_family); memcpy(t, &prefix, sizeof(prefix)); t += sizeof(prefix); memcpy(t, &length, sizeof(length)); if (negated) { *t |= APL_NEGATION_MASK; } t += sizeof(length); memcpy(t, address, length); return r; }
static int zlog_spec_write_usrmsg(zlog_spec_t * a_spec, zlog_thread_t * a_thread, zlog_buf_t * a_buf) { if (a_thread->event->generate_cmd == ZLOG_FMT) { if (a_thread->event->str_format) { return zlog_buf_vprintf(a_buf, a_thread->event->str_format, a_thread->event->str_args); } else { return zlog_buf_append(a_buf, "format=(null)", sizeof("format=(null)")-1); } } else if (a_thread->event->generate_cmd == ZLOG_HEX) { int rc; long line_offset; long byte_offset; /* thread buf start == null or len <= 0 */ if (a_thread->event->hex_buf == NULL) { rc = zlog_buf_append(a_buf, "buf=(null)", sizeof("buf=(null)")-1); goto zlog_hex_exit; } rc = zlog_buf_append(a_buf, ZLOG_HEX_HEAD, sizeof(ZLOG_HEX_HEAD)-1); if (rc) { goto zlog_hex_exit; } line_offset = 0; byte_offset = 0; while (1) { unsigned char c; rc = zlog_buf_append(a_buf, "\n", 1); if (rc) goto zlog_hex_exit; rc = zlog_buf_printf_dec64(a_buf, line_offset + 1, 10); if (rc) goto zlog_hex_exit; rc = zlog_buf_append(a_buf, " ", 3); if (rc) goto zlog_hex_exit; for (byte_offset = 0; byte_offset < 16; byte_offset++) { if (line_offset * 16 + byte_offset < a_thread->event->hex_buf_len) { c = *((unsigned char *)a_thread->event->hex_buf + line_offset * 16 + byte_offset); rc = zlog_buf_printf_hex(a_buf, c, 2); if (rc) goto zlog_hex_exit; rc = zlog_buf_append(a_buf, " ", 1); if (rc) goto zlog_hex_exit; } else { rc = zlog_buf_append(a_buf, " ", 3); if (rc) goto zlog_hex_exit; } } rc = zlog_buf_append(a_buf, " ", 2); if (rc) goto zlog_hex_exit; for (byte_offset = 0; byte_offset < 16; byte_offset++) { if (line_offset * 16 + byte_offset < a_thread->event->hex_buf_len) { c = *((unsigned char *)a_thread->event->hex_buf + line_offset * 16 + byte_offset); if (c >= 32 && c <= 126) { rc = zlog_buf_append(a_buf,(char*)&c, 1); if (rc) goto zlog_hex_exit; } else { rc = zlog_buf_append(a_buf, ".", 1); if (rc) goto zlog_hex_exit; } } else { rc = zlog_buf_append(a_buf, " ", 1); if (rc) goto zlog_hex_exit; } } if (line_offset * 16 + byte_offset >= a_thread->event->hex_buf_len) { break; } line_offset++; } zlog_hex_exit: if (rc < 0) { zc_error("write hex msg fail"); return -1; } else if (rc > 0) { zc_error("write hex msg, buf is full"); return 1; } return 0; } return 0; }
/* * Reads the specified zone into the memory * nsd_options can be NULL if no config file is passed. */ unsigned int zonec_read(const char* name, const char* zonefile, zone_type* zone) { const dname_type *dname; totalrrs = 0; startzonec = time(NULL); parser->errors = 0; dname = dname_parse(parser->rr_region, name); if (!dname) { zc_error("incorrect zone name '%s'", name); return 1; } #ifndef ROOT_SERVER /* Is it a root zone? Are we a root server then? Idiot proof. */ if (dname->label_count == 1) { zc_error("not configured as a root server"); return 1; } #endif /* Open the zone file */ if (!zone_open(zonefile, 3600, CLASS_IN, dname)) { zc_error("cannot open '%s': %s", zonefile, strerror(errno)); return 1; } parser->current_zone = zone; /* Parse and process all RRs. */ yyparse(); /* remove origin if it was unused */ if(parser->origin != error_domain) domain_table_deldomain(parser->db, parser->origin); /* rr_region has been emptied by now */ dname = dname_parse(parser->rr_region, name); /* check if zone file contained a correct SOA record */ if (!parser->current_zone) { zc_error("zone configured as '%s' has no content.", name); } else if(!parser->current_zone->soa_rrset || parser->current_zone->soa_rrset->rr_count == 0) { zc_error("zone configured as '%s' has no SOA record.", name); } else if(dname_compare(domain_dname( parser->current_zone->soa_rrset->rrs[0].owner), dname) != 0) { zc_error("zone configured as '%s', but SOA has owner '%s'.", name, domain_to_string( parser->current_zone->soa_rrset->rrs[0].owner)); } region_free_all(parser->rr_region); parser_flush(); fclose(yyin); if(!zone_is_slave(zone->opts)) check_dname(zone); parser->filename = NULL; return parser->errors; }
int zlog_buf_printf_dec64(zlog_buf_t * a_buf, uint64_t ui64, int width) { unsigned char *p; char *q; unsigned char tmp[ZLOG_INT64_LEN + 1]; size_t num_len, zero_len, out_len; uint32_t ui32; if (a_buf->size_real < 0) { zc_error("pre-use of zlog_buf_resize fail, so can't convert"); return -1; } p = tmp + ZLOG_INT64_LEN; if (ui64 <= ZLOG_MAX_UINT32_VALUE) { /* * To divide 64-bit numbers and to find remainders * on the x86 platform gcc and icc call the libc functions * [u]divdi3() and [u]moddi3(), they call another function * in its turn. On FreeBSD it is the qdivrem() function, * its source code is about 170 lines of the code. * The glibc counterpart is about 150 lines of the code. * * For 32-bit numbers and some divisors gcc and icc use * a inlined multiplication and shifts. For example, * unsigned "i32 / 10" is compiled to * * (i32 * 0xCCCCCCCD) >> 35 */ ui32 = (uint32_t) ui64; do { *--p = (unsigned char) (ui32 % 10 + '0'); } while (ui32 /= 10); } else { do { *--p = (unsigned char) (ui64 % 10 + '0'); } while (ui64 /= 10); } /* zero or space padding */ num_len = (tmp + ZLOG_INT64_LEN) - p; if (width > num_len) { zero_len = width - num_len; out_len = width; } else { zero_len = 0; out_len = num_len; } if ((q = a_buf->tail + out_len) > a_buf->end) { int rc; //zc_debug("size_left not enough, resize"); rc = zlog_buf_resize(a_buf, out_len - (a_buf->end - a_buf->tail)); if (rc > 0) { size_t len_left; zc_error("conf limit to %ld, can't extend, so output", a_buf->size_max); len_left = a_buf->end - a_buf->tail; if (len_left <= zero_len) { zero_len = len_left; num_len = 0; } else if (len_left > zero_len) { /* zero_len not changed */ num_len = len_left - zero_len; } memset(a_buf->tail, '0', zero_len); memcpy(a_buf->tail + zero_len, p, num_len); a_buf->tail += len_left; //*(a_buf->tail) = '\0'; zlog_buf_truncate(a_buf); return 1; } else if (rc < 0) { zc_error("zlog_buf_resize fail"); return -1; } else { //zc_debug("zlog_buf_resize succ, to[%ld]", a_buf->size_real); q = a_buf->tail + out_len; /* re-calculate p*/ } } memset(a_buf->tail, '0', zero_len); memcpy(a_buf->tail + zero_len, p, num_len); a_buf->tail = q; //*(a_buf->tail) = '\0'; return 0; }
/* if width > num_len, 0 padding, else output num */ int zlog_buf_printf_dec32(zlog_buf_t * a_buf, uint32_t ui32, int width) { unsigned char *p; char *q; unsigned char tmp[ZLOG_INT32_LEN + 1]; size_t num_len, zero_len, out_len; if (a_buf->size_real < 0) { zc_error("pre-use of zlog_buf_resize fail, so can't convert"); return -1; } p = tmp + ZLOG_INT32_LEN; do { *--p = (unsigned char) (ui32 % 10 + '0'); } while (ui32 /= 10); /* zero or space padding */ num_len = (tmp + ZLOG_INT32_LEN) - p; if (width > num_len) { zero_len = width - num_len; out_len = width; } else { zero_len = 0; out_len = num_len; } if ((q = a_buf->tail + out_len) > a_buf->end) { int rc; //zc_debug("size_left not enough, resize"); rc = zlog_buf_resize(a_buf, out_len - (a_buf->end - a_buf->tail)); if (rc > 0) { size_t len_left; zc_error("conf limit to %ld, can't extend, so output", a_buf->size_max); len_left = a_buf->end - a_buf->tail; if (len_left <= zero_len) { zero_len = len_left; num_len = 0; } else if (len_left > zero_len) { /* zero_len not changed */ num_len = len_left - zero_len; } memset(a_buf->tail, '0', zero_len); memcpy(a_buf->tail + zero_len, p, num_len); a_buf->tail += len_left; //*(a_buf->tail) = '\0'; zlog_buf_truncate(a_buf); return 1; } else if (rc < 0) { zc_error("zlog_buf_resize fail"); return -1; } else { //zc_debug("zlog_buf_resize succ, to[%ld]", a_buf->size_real); q = a_buf->tail + out_len; /* re-calculate p*/ } } memset(a_buf->tail, '0', zero_len); memcpy(a_buf->tail + zero_len, p, num_len); a_buf->tail = q; //*(a_buf->tail) = '\0'; return 0; }
int main(int argc, char** argv) { zlog_buf_t *a_buf; char *aa; a_buf = zlog_buf_new(10, 20, "ABC"); if (!a_buf) { zc_error("zlog_buf_new fail"); return -1; } #if 0 zlog_buf_printf(a_buf, "1234567890"); zc_error("a_buf->start[%s]", a_buf->start); zc_error("------------"); zlog_buf_restart(a_buf); zlog_buf_printf(a_buf, "123456789012345"); zc_error("a_buf->start[%s]", a_buf->start); zc_error("------------"); zlog_buf_restart(a_buf); zlog_buf_printf(a_buf, "1234567890123456789"); zc_error("a_buf->start[%s]", a_buf->start); zc_error("------------"); zlog_buf_restart(a_buf); zlog_buf_printf(a_buf, "12345678901234567890"); zc_error("a_buf->start[%s]", a_buf->start); zc_error("------------"); zlog_buf_restart(a_buf); zlog_buf_printf(a_buf, "1234567890123456789012345"); zc_error("a_buf->start[%s]", a_buf->start); zc_error("------------"); aa = "123456789"; zlog_buf_append(a_buf, aa, strlen(aa)); zc_error("a_buf->start[%s]", a_buf->start); zc_error("------------"); aa = "0"; zlog_buf_append(a_buf, aa, strlen(aa)); zc_error("a_buf->start[%s]", a_buf->start); zc_error("------------"); aa = "12345"; zlog_buf_append(a_buf, aa, strlen(aa)); zc_error("a_buf->start[%s]", a_buf->start); zc_error("------------"); aa = "6789"; zlog_buf_append(a_buf, aa, strlen(aa)); zc_error("a_buf->start[%s]", a_buf->start); zc_error("------------"); aa = "0"; zlog_buf_append(a_buf, aa, strlen(aa)); zc_error("a_buf->start[%s]", a_buf->start); zc_error("------------"); aa = "22345"; zlog_buf_append(a_buf, aa, strlen(aa)); zc_error("a_buf->start[%s]", a_buf->start); zc_error("------------"); aa = "abc"; int i,j; for (i = 0; i <= 5; i++) { for (j = 0; j <= 5; j++) { zlog_buf_restart(a_buf); zc_error("left[1],max[%d],min[%d]", i, j); zlog_buf_adjust_append(a_buf, aa, strlen(aa), 1, i, j); zc_error("a_buf->start[%s]", a_buf->start); zc_error("-----"); zlog_buf_restart(a_buf); zc_error("left[0],max[%d],min[%d]", i, j); zlog_buf_adjust_append(a_buf, aa, strlen(aa), 0, i, j); zc_error("a_buf->start[%s]", a_buf->start); zc_error("------------"); } } #endif aa = "1234567890"; zc_error("left[0],max[%d],min[%d]", 15, 5); zlog_buf_adjust_append(a_buf, aa, strlen(aa), 0, 15, 5); zc_error("a_buf->start[%s]", a_buf->start); zc_error("------------"); aa = "1234567890"; zlog_buf_restart(a_buf); zc_error("left[0],max[%d],min[%d]", 25, 5); zlog_buf_adjust_append(a_buf, aa, strlen(aa), 1, 25, 5); zc_error("a_buf->start[%s]", a_buf->start); zc_error("------------"); zlog_buf_restart(a_buf); zc_error("left[0],max[%d],min[%d]", 19, 5); zlog_buf_adjust_append(a_buf, aa, strlen(aa), 0, 19, 5); zc_error("a_buf->start[%s]", a_buf->start); zc_error("------------"); zlog_buf_restart(a_buf); zc_error("left[0],max[%d],min[%d]", 20, 5); zlog_buf_adjust_append(a_buf, aa, strlen(aa), 0, 20, 5); zc_error("a_buf->start[%s]", a_buf->start); zc_error("------------"); zlog_buf_del(a_buf); return 0; }
int zc_str_replace_env(char *str, size_t str_size) { char *p; char *q; char fmt[MAXLEN_CFG_LINE + 1]; char env_key[MAXLEN_CFG_LINE + 1]; char env_value[MAXLEN_CFG_LINE + 1]; int str_len; int env_value_len; str_len = strlen(str); q = str; do { int nscan = 0; int nread = 0; p = strchr(q, '%'); if (!p) { /* can't find more % */ break; } memset(fmt, 0x00, sizeof(fmt)); memset(env_key, 0x00, sizeof(env_key)); memset(env_value, 0x00, sizeof(env_value)); nscan = sscanf(p + 1, "%[.0-9-]%n", fmt + 1, &nread); if (nscan == 1) { fmt[0] = '%'; fmt[nread + 1] = 's'; } else { nread = 0; strcpy(fmt, "%s"); } q = p + 1 + nread; nscan = sscanf(q, "E(%[^)])%n", env_key, &nread); if (nscan == 0) { continue; } q += nread; if (*(q - 1) != ')') { zc_error("in string[%s] can't find match )", p); return -1; } env_value_len = snprintf(env_value, sizeof(env_value), fmt, getenv(env_key)); if (env_value_len < 0 || env_value_len >= sizeof(env_value)) { zc_error("snprintf fail, errno[%d], evn_value_len[%d]", errno, env_value_len); return -1; } str_len = str_len - (q - p) + env_value_len; if (str_len > str_size - 1) { zc_error("repalce env_value[%s] cause overlap", env_value); return -1; } memmove(p + env_value_len, q, strlen(q) + 1); memcpy(p, env_value, env_value_len); } while (1); return 0; }