/* * returns the length of the variable */ int v_length(var_t *var) { char tmpsb[64]; switch (var->type) { case V_STR: return strlen((char *) var->v.p.ptr); case V_UDS: return uds_length(var); case V_HASH: return hash_length(var); case V_PTR: ltostr(var->v.ap.p, tmpsb); return strlen(tmpsb); case V_INT: ltostr(var->v.i, tmpsb); return strlen(tmpsb); case V_NUM: ftostr(var->v.n, tmpsb); return strlen(tmpsb); case V_ARRAY: return var->v.a.size; } return 1; }
/* * returns the length of the variable */ int v_length(var_t *var) { char tmpsb[64]; switch (var->type) { case V_STR: return strlen(var->v.p.ptr); case V_MAP: return map_length(var); case V_PTR: ltostr(var->v.ap.p, tmpsb); return strlen(tmpsb); case V_INT: ltostr(var->v.i, tmpsb); return strlen(tmpsb); case V_NUM: ftostr(var->v.n, tmpsb); return strlen(tmpsb); case V_ARRAY: return var->v.a.size; case V_REF: return v_length(var->v.ref); } return 1; }
/* * returns the string representation of the variable */ char *v_str(var_t *arg) { char *buffer; switch (arg->type) { case V_INT: buffer = malloc(64); ltostr(arg->v.i, buffer); break; case V_NUM: buffer = malloc(64); ftostr(arg->v.n, buffer); break; case V_STR: buffer = strdup(arg->v.p.ptr); break; case V_ARRAY: case V_MAP: buffer = map_to_str(arg); break; case V_FUNC: case V_PTR: buffer = malloc(5); strcpy(buffer, "func"); break; default: buffer = malloc(1); buffer[0] = '\0'; break; } return buffer; }
int LD3dsExporter::getMaterial(int colorNumber) { IntIntMap::iterator it = m_colorNumbers.find(colorNumber); if (it == m_colorNumbers.end()) { int material = (int)m_colorNumbers.size(); Lib3dsMaterial *mat = lib3ds_material_new((std::string("ldraw_") + ltostr(colorNumber)).c_str()); lib3ds_file_insert_material(m_file, mat, -1); int r, g, b, a; LDLPalette *pPalette = m_topModel->getMainModel()->getPalette(); pPalette->getRGBA(colorNumber, r, g, b, a); LDLColorInfo colorInfo = pPalette->getAnyColorInfo(colorNumber); mat->diffuse[0] = r / 255.0f; mat->diffuse[1] = g / 255.0f; mat->diffuse[2] = b / 255.0f; mat->transparency = 1.0f - a / 255.0f; mat->two_sided = 1; if (colorInfo.rubber) { mat->specular[0] = mat->specular[1] = mat->specular[2] = 0.05f; } mat->shading = LIB3DS_SHADING_PHONG; m_colorNumbers[colorNumber] = material; return material; } else { return it->second; } }
char * /* addr of terminating null */ ltostr ( char *str, /* output string */ long val, /* value to be converted */ unsigned base) /* conversion base */ { ldiv_t r; /* result of val / base */ if (base > 36) /* no conversion if wrong base */ { str = '\0'; return str; } if (val < 0) *str++ = '-'; r = ldiv (labs(val), base); /* output digits of val/base first */ if (r.quot > 0) str = ltostr (str, r.quot, base); /* output last digit */ *str++ = "0123456789abcdefghijklmnopqrstuvwxyz"[(int)r.rem]; *str = '\0'; return str; }
void dump_binary(void *p, size_t s) { char dump[256+1]; char temp[256+1]; unsigned char uc = 0; unsigned short us = 0; unsigned int ui = 0; unsigned long ul = 0; int ii; memset(dump, 0x00, sizeof(dump)); memset(temp, 0x00, sizeof(dump)); switch(s) { case 1: uc = *(unsigned char *)p; sprintf(dump, "%08s", ltostr(uc, 2)); break; case 2: us = *(unsigned short *)p; sprintf(dump, "%016s", ultostr(us, 2)); break; case 4: ui = *(unsigned int *)p; sprintf(dump, "%032s", ultostr(ui, 2)); break; case 8: ul = *(unsigned long *)p; sprintf(dump, "%064s", ultostr(ul, 2)); break; case 16: ul = *(unsigned long *)p; sprintf(dump, "%064s", ultostr(ul, 2)); ul = *(((unsigned long *)p)+1); sprintf(dump+64, "%064s", ultostr(ul, 2)); break; } for(ii=0; ii<6; ii++) temp[ii] = ' '; for(ii=0; ii<s*8; ii++) { temp[ii+6] = (ii+1)/10>9 ? 'A' - 10 + (ii+1)/10 : '0' + (ii+1)/10; } temp[ii+6] = '\0'; puts(temp); for(ii=0; ii<s*8; ii++) { temp[ii+6] = '0' + ((ii+1)%10); } temp[ii+6] = '\0'; puts(temp); sprintf(temp, "[%2d] [%s]\n", s, dump); puts(temp); }
void write_log(int semid, int logfd, char* log_msg){ char* prefix = "[Client "; char* client_id = ltostr(getpid()); char* suffix = ": "; //time_t seconds = time(NULL); struct timeval tv; gettimeofday(&tv, NULL); struct tm* date_time = localtime(&tv.tv_sec); char* format = "%d.%m.%y %H:%M:%S."; int size = strlen(format) + 6; char* date = (char*)malloc((size + 1) * sizeof(char)); strftime(date, size, format, date_time); char* microsecs = ltostr(tv.tv_usec % (1000 * 1000)); char* postfix = "] "; char* postpostfix = "\n"; char* str_log = (char*)malloc((strlen(log_msg) + strlen(prefix) + strlen(client_id) + strlen(suffix) + strlen(date) + strlen(microsecs) + strlen(postfix) + strlen(postpostfix)) * sizeof(char)); str_log[0] = 0; strcat(str_log, prefix); strcat(str_log, client_id); strcat(str_log, suffix); strcat(str_log, date); strcat(str_log, microsecs); strcat(str_log, postfix); strcat(str_log, log_msg); strcat(str_log, postpostfix); struct sembuf sem_oper; sem_oper.sem_num = 0; sem_oper.sem_flg = 0; sem_oper.sem_op = -1; if(semop(semid, &sem_oper, 1) < 0) error_prex("decrease semafor"); write_from_str(str_log, logfd); sem_oper.sem_op = 1; if(semop(semid, &sem_oper, 1) < 0) error_prex("increase cemafor"); }
std::string LD3dsExporter::getMeshName(LDLModel *model, Lib3dsMesh *&pMesh) { std::string modelName; std::string meshName = "LDXM_"; StringIntMap::iterator it; int index; if (model != NULL) { char *filename = filenameFromPath(model->getFilename()); size_t dotSpot; modelName = filename; dotSpot = modelName.rfind('.'); delete filename; if (dotSpot < modelName.size()) { modelName = modelName.substr(0, dotSpot); } } else { modelName = "no_name"; } it = m_names.find(modelName); if (it == m_names.end()) { index = (int)m_names.size() + 1; meshName += ltostr(index); pMesh = NULL; } else { index = it->second; meshName += ltostr(index); pMesh = m_meshes[meshName]; } //m_names[modelName] = index; return meshName; }
/* * converts the variable to string-variable */ void v_tostr(var_t *arg) { if (arg->type != V_STR) { char *tmp; int l; tmp = tmp_alloc(64); switch (arg->type) { case V_UDS: uds_to_str(arg, tmp, 64); uds_free(arg); break; case V_HASH: hash_to_str(arg, tmp, 64); hash_free_var(arg); break; case V_PTR: ltostr(arg->v.ap.p, tmp); break; case V_INT: ltostr(arg->v.i, tmp); break; case V_NUM: ftostr(arg->v.n, tmp); break; default: err_varisarray(); tmp_free(tmp); return; } l = strlen(tmp) + 1; arg->type = V_STR; arg->v.p.ptr = tmp_alloc(l); arg->v.p.size = l; strcpy(arg->v.p.ptr, tmp); tmp_free(tmp); } }
int dump_heap(struct heap **heap_dump, int pid_id) { char dump_file[30]; char *dump_data = (char*)malloc(100); FILE *df = NULL; int hs = 0; int bytes = 0; bzero(dump_data,100); ptrace(PTRACE_ATTACH, pid_id, NULL, NULL); sprintf(dump_file,"%d.dump",pid_id); df = fopen(dump_file,"a+"); if(df == NULL) { printf("* Error creating file %s, quitting.\n", dump_file); ptrace(PTRACE_DETACH, pid_id, NULL, NULL); return -1; } printf("--------------------------------------------------------------\n"); printf("* Dumping memory to %s this can take awhile.\n", dump_file); while(hs < heap_structure_size) { while(heap_dump[hs]->saddress <= heap_dump[hs]->address) { ltostr(dump_data,peekdata(heap_dump[hs]->saddress,pid_id)); bytes += fwrite(dump_data,1,sizeof(dump_data),df); heap_dump[hs]->saddress++; } printf("* Wrote %d bytes.\n", bytes); bytes = 0; hs++; } fclose(df); ptrace(PTRACE_DETACH, pid_id, NULL, NULL); printf("* Finished.\n"); return 0; }
char *to_binary(void *p, size_t s) { unsigned char uc = 0; unsigned short us = 0; unsigned int ui = 0; unsigned long ul = 0; static char *buff; buff = buff ? buff : malloc(s); memset(buff, 0x00, s); switch(s) { case 1: uc = *(unsigned char *)p; sprintf(buff, "%08s", ltostr(uc, 2)); break; case 2: us = *(unsigned short *)p; sprintf(buff, "%016s", ultostr(us, 2)); break; case 4: ui = *(unsigned int *)p; sprintf(buff, "%032s", ultostr(ui, 2)); break; case 8: ul = *(unsigned long *)p; sprintf(buff, "%064s", ultostr(ul, 2)); break; case 16: ul = *(unsigned long *)p; sprintf(buff, "%064s", ultostr(ul, 2)); ul = *(((unsigned long *)p)+1); sprintf(buff+64, "%064s", ultostr(ul, 2)); break; } return buff; }
static int cgi_create_env(server *srv, connection *con, plugin_data *p, buffer *cgi_handler) { pid_t pid; #ifdef HAVE_IPV6 char b2[INET6_ADDRSTRLEN + 1]; #endif int to_cgi_fds[2]; int from_cgi_fds[2]; struct stat st; #ifndef __WIN32 if (cgi_handler->used > 1) { /* stat the exec file */ if (-1 == (stat(cgi_handler->ptr, &st))) { log_error_write(srv, __FILE__, __LINE__, "sbss", "stat for cgi-handler", cgi_handler, "failed:", strerror(errno)); return -1; } } if (pipe(to_cgi_fds)) { log_error_write(srv, __FILE__, __LINE__, "ss", "pipe failed:", strerror(errno)); return -1; } if (pipe(from_cgi_fds)) { log_error_write(srv, __FILE__, __LINE__, "ss", "pipe failed:", strerror(errno)); return -1; } /* fork, execve */ switch (pid = fork()) { case 0: { /* child */ char **args; int argc; int i = 0; char buf[32]; size_t n; char_array env; char *c; const char *s; server_socket *srv_sock = con->srv_socket; /* move stdout to from_cgi_fd[1] */ close(STDOUT_FILENO); dup2(from_cgi_fds[1], STDOUT_FILENO); close(from_cgi_fds[1]); /* not needed */ close(from_cgi_fds[0]); /* move the stdin to to_cgi_fd[0] */ close(STDIN_FILENO); dup2(to_cgi_fds[0], STDIN_FILENO); close(to_cgi_fds[0]); /* not needed */ close(to_cgi_fds[1]); /* HACK: * this is not nice, but it works * * we feed the stderr of the CGI to our errorlog, if possible */ if (srv->errorlog_mode == ERRORLOG_FILE) { close(STDERR_FILENO); dup2(srv->errorlog_fd, STDERR_FILENO); } /* create environment */ env.ptr = NULL; env.size = 0; env.used = 0; cgi_env_add(&env, CONST_STR_LEN("SERVER_SOFTWARE"), CONST_STR_LEN(PACKAGE_NAME"/"PACKAGE_VERSION)); if (!buffer_is_empty(con->server_name)) { cgi_env_add(&env, CONST_STR_LEN("SERVER_NAME"), CONST_BUF_LEN(con->server_name)); } else { #ifdef HAVE_IPV6 s = inet_ntop(srv_sock->addr.plain.sa_family, srv_sock->addr.plain.sa_family == AF_INET6 ? (const void *) &(srv_sock->addr.ipv6.sin6_addr) : (const void *) &(srv_sock->addr.ipv4.sin_addr), b2, sizeof(b2)-1); #else s = inet_ntoa(srv_sock->addr.ipv4.sin_addr); #endif cgi_env_add(&env, CONST_STR_LEN("SERVER_NAME"), s, strlen(s)); } cgi_env_add(&env, CONST_STR_LEN("GATEWAY_INTERFACE"), CONST_STR_LEN("CGI/1.1")); s = get_http_version_name(con->request.http_version); cgi_env_add(&env, CONST_STR_LEN("SERVER_PROTOCOL"), s, strlen(s)); ltostr(buf, #ifdef HAVE_IPV6 ntohs(srv_sock->addr.plain.sa_family == AF_INET6 ? srv_sock->addr.ipv6.sin6_port : srv_sock->addr.ipv4.sin_port) #else ntohs(srv_sock->addr.ipv4.sin_port) #endif ); cgi_env_add(&env, CONST_STR_LEN("SERVER_PORT"), buf, strlen(buf)); #ifdef HAVE_IPV6 s = inet_ntop(srv_sock->addr.plain.sa_family, srv_sock->addr.plain.sa_family == AF_INET6 ? (const void *) &(srv_sock->addr.ipv6.sin6_addr) : (const void *) &(srv_sock->addr.ipv4.sin_addr), b2, sizeof(b2)-1); #else s = inet_ntoa(srv_sock->addr.ipv4.sin_addr); #endif cgi_env_add(&env, CONST_STR_LEN("SERVER_ADDR"), s, strlen(s)); s = get_http_method_name(con->request.http_method); cgi_env_add(&env, CONST_STR_LEN("REQUEST_METHOD"), s, strlen(s)); if (!buffer_is_empty(con->request.pathinfo)) { cgi_env_add(&env, CONST_STR_LEN("PATH_INFO"), CONST_BUF_LEN(con->request.pathinfo)); } cgi_env_add(&env, CONST_STR_LEN("REDIRECT_STATUS"), CONST_STR_LEN("200")); if (!buffer_is_empty(con->uri.query)) { cgi_env_add(&env, CONST_STR_LEN("QUERY_STRING"), CONST_BUF_LEN(con->uri.query)); } if (!buffer_is_empty(con->request.orig_uri)) { cgi_env_add(&env, CONST_STR_LEN("REQUEST_URI"), CONST_BUF_LEN(con->request.orig_uri)); } #ifdef HAVE_IPV6 s = inet_ntop(con->dst_addr.plain.sa_family, con->dst_addr.plain.sa_family == AF_INET6 ? (const void *) &(con->dst_addr.ipv6.sin6_addr) : (const void *) &(con->dst_addr.ipv4.sin_addr), b2, sizeof(b2)-1); #else s = inet_ntoa(con->dst_addr.ipv4.sin_addr); #endif cgi_env_add(&env, CONST_STR_LEN("REMOTE_ADDR"), s, strlen(s)); ltostr(buf, #ifdef HAVE_IPV6 ntohs(con->dst_addr.plain.sa_family == AF_INET6 ? con->dst_addr.ipv6.sin6_port : con->dst_addr.ipv4.sin_port) #else ntohs(con->dst_addr.ipv4.sin_port) #endif ); cgi_env_add(&env, CONST_STR_LEN("REMOTE_PORT"), buf, strlen(buf)); if (!buffer_is_empty(con->authed_user)) { cgi_env_add(&env, CONST_STR_LEN("REMOTE_USER"), CONST_BUF_LEN(con->authed_user)); } /* request.content_length < SSIZE_MAX, see request.c */ ltostr(buf, con->request.content_length); cgi_env_add(&env, CONST_STR_LEN("CONTENT_LENGTH"), buf, strlen(buf)); cgi_env_add(&env, CONST_STR_LEN("SCRIPT_FILENAME"), CONST_BUF_LEN(con->physical.path)); cgi_env_add(&env, CONST_STR_LEN("SCRIPT_NAME"), CONST_BUF_LEN(con->uri.path)); cgi_env_add(&env, CONST_STR_LEN("DOCUMENT_ROOT"), CONST_BUF_LEN(con->physical.doc_root)); /* for valgrind */ if (NULL != (s = getenv("LD_PRELOAD"))) { cgi_env_add(&env, CONST_STR_LEN("LD_PRELOAD"), s, strlen(s)); } if (NULL != (s = getenv("LD_LIBRARY_PATH"))) { cgi_env_add(&env, CONST_STR_LEN("LD_LIBRARY_PATH"), s, strlen(s)); } #ifdef __CYGWIN__ /* CYGWIN needs SYSTEMROOT */ if (NULL != (s = getenv("SYSTEMROOT"))) { cgi_env_add(&env, CONST_STR_LEN("SYSTEMROOT"), s, strlen(s)); } #endif for (n = 0; n < con->request.headers->used; n++) { data_string *ds; ds = (data_string *)con->request.headers->data[n]; if (ds->value->used && ds->key->used) { size_t j; buffer_reset(p->tmp_buf); if (0 != strcasecmp(ds->key->ptr, "CONTENT-TYPE")) { buffer_copy_string(p->tmp_buf, "HTTP_"); p->tmp_buf->used--; /* strip \0 after HTTP_ */ } buffer_prepare_append(p->tmp_buf, ds->key->used + 2); for (j = 0; j < ds->key->used - 1; j++) { char cr = '_'; if (light_isalpha(ds->key->ptr[j])) { /* upper-case */ cr = ds->key->ptr[j] & ~32; } else if (light_isdigit(ds->key->ptr[j])) { /* copy */ cr = ds->key->ptr[j]; } p->tmp_buf->ptr[p->tmp_buf->used++] = cr; } p->tmp_buf->ptr[p->tmp_buf->used++] = '\0'; cgi_env_add(&env, CONST_BUF_LEN(p->tmp_buf), CONST_BUF_LEN(ds->value)); } } for (n = 0; n < con->environment->used; n++) { data_string *ds; ds = (data_string *)con->environment->data[n]; if (ds->value->used && ds->key->used) { size_t j; buffer_reset(p->tmp_buf); buffer_prepare_append(p->tmp_buf, ds->key->used + 2); for (j = 0; j < ds->key->used - 1; j++) { p->tmp_buf->ptr[p->tmp_buf->used++] = isalpha((unsigned char)ds->key->ptr[j]) ? toupper((unsigned char)ds->key->ptr[j]) : '_'; } p->tmp_buf->ptr[p->tmp_buf->used++] = '\0'; cgi_env_add(&env, CONST_BUF_LEN(p->tmp_buf), CONST_BUF_LEN(ds->value)); } } if (env.size == env.used) { env.size += 16; env.ptr = realloc(env.ptr, env.size * sizeof(*env.ptr)); } env.ptr[env.used] = NULL; /* set up args */ argc = 3; args = malloc(sizeof(*args) * argc); i = 0; if (cgi_handler->used > 1) { args[i++] = cgi_handler->ptr; } args[i++] = con->physical.path->ptr; args[i++] = NULL; /* search for the last / */ if (NULL != (c = strrchr(con->physical.path->ptr, '/'))) { *c = '\0'; /* change to the physical directory */ if (-1 == chdir(con->physical.path->ptr)) { log_error_write(srv, __FILE__, __LINE__, "ssb", "chdir failed:", strerror(errno), con->physical.path); } *c = '/'; } /* we don't need the client socket */ for (i = 3; i < 256; i++) { if (i != srv->errorlog_fd) close(i); } /* exec the cgi */ execve(args[0], args, env.ptr); log_error_write(srv, __FILE__, __LINE__, "sss", "CGI failed:", strerror(errno), args[0]); /* */ SEGFAULT(); break; } case -1: /* error */ log_error_write(srv, __FILE__, __LINE__, "ss", "fork failed:", strerror(errno)); break; default: { handler_ctx *hctx; /* father */ close(from_cgi_fds[1]); close(to_cgi_fds[0]); if (con->request.content_length) { chunkqueue *cq = con->request_content_queue; chunk *c; assert(chunkqueue_length(cq) == (off_t)con->request.content_length); /* there is content to send */ for (c = cq->first; c; c = cq->first) { int r = 0; /* copy all chunks */ switch(c->type) { case FILE_CHUNK: if (c->file.mmap.start == MAP_FAILED) { if (-1 == c->file.fd && /* open the file if not already open */ -1 == (c->file.fd = open(c->file.name->ptr, O_RDONLY))) { log_error_write(srv, __FILE__, __LINE__, "ss", "open failed: ", strerror(errno)); close(from_cgi_fds[0]); close(to_cgi_fds[1]); return -1; } c->file.mmap.length = c->file.length; if (MAP_FAILED == (c->file.mmap.start = mmap(0, c->file.mmap.length, PROT_READ, MAP_SHARED, c->file.fd, 0))) { log_error_write(srv, __FILE__, __LINE__, "ssbd", "mmap failed: ", strerror(errno), c->file.name, c->file.fd); close(from_cgi_fds[0]); close(to_cgi_fds[1]); return -1; } close(c->file.fd); c->file.fd = -1; /* chunk_reset() or chunk_free() will cleanup for us */ } if ((r = write(to_cgi_fds[1], c->file.mmap.start + c->offset, c->file.length - c->offset)) < 0) { switch(errno) { case ENOSPC: con->http_status = 507; break; default: con->http_status = 403; break; } } break; case MEM_CHUNK: if ((r = write(to_cgi_fds[1], c->mem->ptr + c->offset, c->mem->used - c->offset - 1)) < 0) { switch(errno) { case ENOSPC: con->http_status = 507; break; default: con->http_status = 403; break; } } break; case UNUSED_CHUNK: break; } if (r > 0) { c->offset += r; cq->bytes_out += r; } else { break; } chunkqueue_remove_finished_chunks(cq); } } close(to_cgi_fds[1]); /* register PID and wait for them asyncronously */ con->mode = p->id; buffer_reset(con->physical.path); hctx = cgi_handler_ctx_init(); hctx->remote_conn = con; hctx->plugin_data = p; hctx->pid = pid; hctx->fd = from_cgi_fds[0]; hctx->fde_ndx = -1; con->plugin_ctx[p->id] = hctx; fdevent_register(srv->ev, hctx->fd, cgi_handle_fdevent, hctx); fdevent_event_add(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN); if (-1 == fdevent_fcntl_set(srv->ev, hctx->fd)) { log_error_write(srv, __FILE__, __LINE__, "ss", "fcntl failed: ", strerror(errno)); fdevent_event_del(srv->ev, &(hctx->fde_ndx), hctx->fd); fdevent_unregister(srv->ev, hctx->fd); log_error_write(srv, __FILE__, __LINE__, "sd", "cgi close:", hctx->fd); close(hctx->fd); cgi_handler_ctx_free(hctx); con->plugin_ctx[p->id] = NULL; return -1; } break; } } return 0; #else return -1; #endif }
/* * add two variables * result = a + b */ void v_add(var_t *result, var_t *a, var_t *b) { char tmpsb[64]; if (a->type == V_STR && b->type == V_STR) { result->type = V_STR; result->v.p.ptr = malloc(strlen(a->v.p.ptr) + strlen(b->v.p.ptr) + 1); strcpy(result->v.p.ptr, a->v.p.ptr); strcat(result->v.p.ptr, b->v.p.ptr); result->v.p.size = strlen(result->v.p.ptr) + 1; return; } else if (a->type == V_INT && b->type == V_INT) { result->type = V_INT; result->v.i = a->v.i + b->v.i; return; } else if (a->type == V_NUM && b->type == V_NUM) { result->type = V_NUM; result->v.n = a->v.n + b->v.n; return; } else if (a->type == V_NUM && b->type == V_INT) { result->type = V_NUM; result->v.n = a->v.n + b->v.i; return; } else if (a->type == V_INT && b->type == V_NUM) { result->type = V_NUM; result->v.n = a->v.i + b->v.n; return; } else if (a->type == V_STR && (b->type == V_INT || b->type == V_NUM)) { if (is_number(a->v.p.ptr)) { result->type = V_NUM; if (b->type == V_INT) { result->v.n = b->v.i + v_getval(a); } else { result->v.n = b->v.n + v_getval(a); } } else { result->type = V_STR; result->v.p.ptr = (char *)malloc(strlen(a->v.p.ptr) + 64); strcpy(result->v.p.ptr, a->v.p.ptr); if (b->type == V_INT) { ltostr(b->v.i, tmpsb); } else { ftostr(b->v.n, tmpsb); } strcat(result->v.p.ptr, tmpsb); result->v.p.size = strlen(result->v.p.ptr) + 1; } } else if ((a->type == V_INT || a->type == V_NUM) && b->type == V_STR) { if (is_number(b->v.p.ptr)) { result->type = V_NUM; if (a->type == V_INT) { result->v.n = a->v.i + v_getval(b); } else { result->v.n = a->v.n + v_getval(b); } } else { result->type = V_STR; result->v.p.ptr = (char *)malloc(strlen(b->v.p.ptr) + 64); if (a->type == V_INT) { ltostr(a->v.i, tmpsb); } else { ftostr(a->v.n, tmpsb); } strcpy(result->v.p.ptr, tmpsb); strcat(result->v.p.ptr, b->v.p.ptr); result->v.p.size = strlen(result->v.p.ptr) + 1; } } }
char *createPresenceMsg(int state, char *text, char *to, char *from, int priority){ char *presText = NULL; int buffersize = 0; char *tmp = NULL; int ret = 0; xmlDocPtr doc = NULL; xmlNodePtr root_node = NULL; xmlChar *xmlbuff = NULL; switch(state){ case PRES_CHAT: presText="chat"; break; case PRES_DND: presText="dnd"; break; case PRES_AWAY: presText="away"; break; case PRES_XA: presText="xa"; break; case PRES_UNAVAILABLE: presText="unavailable"; break; } /* rfc3921 says priority has to be bigger than -129 and lower than 128 */ if(priority != 0 && priority >= -128 && priority <= 127){ tmp = malloc(sizeof(size_t)*8+1); if(tmp){ ret=ltostr(priority,10,tmp,sizeof(size_t)*8+1); if(ret != 0) tmp=NULL; } } doc = xmlNewDoc(BAD_CAST "1.0"); root_node = xmlNewNode(NULL, BAD_CAST "presence"); xmlDocSetRootElement(doc, root_node); xmlNewProp(root_node, BAD_CAST "xml:lang", BAD_CAST "en"); if(state == PRES_UNAVAILABLE) xmlNewProp(root_node, BAD_CAST "type", BAD_CAST presText); if(presText && state != PRES_UNAVAILABLE) xmlNewChild(root_node, NULL, BAD_CAST "show", BAD_CAST presText); if(to) if(strlen(to)>0) xmlNewProp(root_node, BAD_CAST "to", BAD_CAST to); if(from) if(strlen(from)>0) xmlNewProp(root_node, BAD_CAST "from", BAD_CAST from); if(text) if(strlen(text)>0 && state != PRES_AVAILABLE) xmlNewChild(root_node, NULL, BAD_CAST "status", BAD_CAST text); if(state != PRES_UNAVAILABLE){ if(tmp){ xmlNewChild(root_node, NULL, BAD_CAST "priority", BAD_CAST tmp); free(tmp); } } xmlDocDumpFormatMemory(doc, &xmlbuff, &buffersize, 1); xmlFreeDoc(doc); xmlCleanupParser(); xmlMemoryDump(); return (char *)xmlbuff; }
/*********************** xitoa.c ***************************/ char *_itoa(int i) { return ltostr((long) i, 10); }
/* * add two variables * result = a + b */ void v_add(var_t *result, var_t *a, var_t *b) { char tmpsb[INT_STR_LEN]; if (a->type == V_STR && b->type == V_STR) { int length = strlen(a->v.p.ptr) + strlen(b->v.p.ptr); result->type = V_STR; result->v.p.ptr = malloc(length + 1); strcpy(result->v.p.ptr, a->v.p.ptr); strcat(result->v.p.ptr, b->v.p.ptr); result->v.p.ptr[length] = '\0'; result->v.p.length = length + 1; return; } else if (a->type == V_INT && b->type == V_INT) { result->type = V_INT; result->v.i = a->v.i + b->v.i; return; } else if (a->type == V_NUM && b->type == V_NUM) { result->type = V_NUM; result->v.n = a->v.n + b->v.n; return; } else if (a->type == V_NUM && b->type == V_INT) { result->type = V_NUM; result->v.n = a->v.n + b->v.i; return; } else if (a->type == V_INT && b->type == V_NUM) { result->type = V_NUM; result->v.n = a->v.i + b->v.n; return; } else if (a->type == V_STR && (b->type == V_INT || b->type == V_NUM)) { if (is_number(a->v.p.ptr)) { result->type = V_NUM; if (b->type == V_INT) { result->v.n = b->v.i + v_getval(a); } else { result->v.n = b->v.n + v_getval(a); } } else { result->type = V_STR; result->v.p.ptr = (char *)malloc(strlen(a->v.p.ptr) + INT_STR_LEN); strcpy(result->v.p.ptr, a->v.p.ptr); if (b->type == V_INT) { ltostr(b->v.i, tmpsb); } else { ftostr(b->v.n, tmpsb); } strcat(result->v.p.ptr, tmpsb); result->v.p.length = strlen(result->v.p.ptr) + 1; } } else if ((a->type == V_INT || a->type == V_NUM) && b->type == V_STR) { if (is_number(b->v.p.ptr)) { result->type = V_NUM; if (a->type == V_INT) { result->v.n = a->v.i + v_getval(b); } else { result->v.n = a->v.n + v_getval(b); } } else { result->type = V_STR; result->v.p.ptr = (char *)malloc(strlen(b->v.p.ptr) + INT_STR_LEN); if (a->type == V_INT) { ltostr(a->v.i, tmpsb); } else { ftostr(a->v.n, tmpsb); } strcpy(result->v.p.ptr, tmpsb); strcat(result->v.p.ptr, b->v.p.ptr); result->v.p.length = strlen(result->v.p.ptr) + 1; } } else if (b->type == V_MAP) { char *map = map_to_str(b); v_set(result, a); v_strcat(result, map); free(map); } }