static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { lua_State *L = (lua_State *)ud; int mode = L == NULL ? 0 : G(L)->egcmode; void *nptr; if (nsize == 0) { c_free(ptr); return NULL; } if (L != NULL && (mode & EGC_ALWAYS)) /* always collect memory if requested */ luaC_fullgc(L); if(nsize > osize && L != NULL) { #if defined(LUA_STRESS_EMERGENCY_GC) luaC_fullgc(L); #endif if(G(L)->memlimit > 0 && (mode & EGC_ON_MEM_LIMIT) && l_check_memlimit(L, nsize - osize)) return NULL; } nptr = (void *)c_realloc(ptr, nsize); if (nptr == NULL && L != NULL && (mode & EGC_ON_ALLOC_FAILURE)) { luaC_fullgc(L); /* emergency full collection. */ nptr = (void *)c_realloc(ptr, nsize); /* try allocation again */ } return nptr; }
void sircc_chan_remove_user(struct sircc_chan *chan, const char *user) { for (size_t i = 0; i < chan->nb_users; i++) { if (strcmp(chan->users[i], user) == 0) { c_free(chan->users[i]); if (i < chan->nb_users - 1) { memmove(chan->users + i, chan->users + i + 1, (chan->nb_users - i - 1) * sizeof(char *)); } chan->nb_users--; if (chan->nb_users == 0) { c_free(chan->users); chan->users = NULL; } else { chan->users = c_realloc(chan->users, chan->nb_users * sizeof(char *)); } break; } } chan->users_sorted = false; }
char *c_getnextline(const int fd, char c) { int i; char buff; char *ptr; ptr = c_malloc(sizeof(char) * 1); ptr[0] = '\0'; i = 0; if (read(fd, &buff, 1) != 1) return (NULL); if (buff <= 31 || buff > 176) c_puterror("fichier corrompu"); ptr = c_realloc(ptr, sizeof(char) * (c_strlen(ptr, '\0') + 2)); ptr[i++] = buff; ptr[i] = '\0'; while (read(fd, &buff, 1) == 1 && buff != '\n' && buff != c) { if (buff <= 31 || buff > 176) c_puterror("fichier corrompu"); ptr = realloc(ptr, sizeof(char) * (c_strlen(ptr, '\0') + 2)); ptr[i++] = buff; ptr[i] = '\0'; } return (ptr); }
void fxSerializeJSONChar(txMachine* the, txJSONSerializer* theSerializer, char c) { if (theSerializer->offset == theSerializer->size) { char* aBuffer; theSerializer->size += 1024; aBuffer = c_realloc(theSerializer->buffer, theSerializer->size); if (!aBuffer) fxSerializeJSONError(the, theSerializer, XS_RANGE_ERROR); theSerializer->buffer = aBuffer; } theSerializer->buffer[theSerializer->offset] = c; theSerializer->offset++; }
void xscGrowBuffer(txMachine* the, xsBuffer* theBuffer, txSize theSize) { txInteger anOffset, aSize; if (theBuffer->current + theSize > theBuffer->top) { anOffset = theBuffer->current - theBuffer->bottom; aSize = (((anOffset + theSize) / 1024) + 1) * 1024; theBuffer->bottom = c_realloc(theBuffer->bottom, aSize); xsElseError(theBuffer->bottom); theBuffer->top = theBuffer->bottom + aSize; theBuffer->current = theBuffer->bottom + anOffset; } }
void fxSerializeJSONChars(txMachine* the, txJSONSerializer* theSerializer, char* s) { txSize aSize = c_strlen(s); if ((theSerializer->offset + aSize) >= theSerializer->size) { char* aBuffer; theSerializer->size += ((aSize / 1024) + 1) * 1024; aBuffer = c_realloc(theSerializer->buffer, theSerializer->size); if (!theSerializer->buffer) fxSerializeJSONError(the, theSerializer, XS_RANGE_ERROR); theSerializer->buffer = aBuffer; } c_memcpy(theSerializer->buffer + theSerializer->offset, s, aSize); theSerializer->offset += aSize; }
void fxSerializeJSONChar(txMachine* the, txJSONSerializer* theSerializer, char c) { //fprintf(stderr, "%c", c); if (theSerializer->offset == theSerializer->size) { char* aBuffer; theSerializer->size += 1024; aBuffer = c_realloc(theSerializer->buffer, theSerializer->size); if (!aBuffer) mxUnknownError("out of memory"); theSerializer->buffer = aBuffer; } theSerializer->buffer[theSerializer->offset] = c; theSerializer->offset++; }
void fxSerializeJSONChars(txMachine* the, txJSONSerializer* theSerializer, char* s) { //fprintf(stderr, "%s", s); txSize aSize = c_strlen(s); if ((theSerializer->offset + aSize) >= theSerializer->size) { char* aBuffer; theSerializer->size += ((aSize / 1024) + 1) * 1024; aBuffer = c_realloc(theSerializer->buffer, theSerializer->size); if (!aBuffer) mxUnknownError("out of memory"); theSerializer->buffer = aBuffer; } c_memcpy(theSerializer->buffer + theSerializer->offset, s, aSize); theSerializer->offset += aSize; }
void scanCharacter(void *data, const char *text, int size) { Scanner* self = data; xsMachine* the = self->the; int textSize = self->textOffset + size + 1; if (textSize > self->textSize) { textSize = (textSize + 8191) & ~0x01fff; self->textBuffer = c_realloc(self->textBuffer, textSize); self->textSize = textSize; } c_memcpy(self->textBuffer + self->textOffset, text, size); if (!self->textOffset) xsVar(LINE) = xsInteger(XML_GetCurrentLineNumber(self->expat)); self->textOffset += size; }
c_strlist_t *c_strlist_expand(c_strlist_t *strlist, size_t size) { if (strlist == NULL || size == 0) { errno = EINVAL; return NULL; } if (strlist->size >= size) { return strlist; } strlist->vector = (char **) c_realloc(strlist->vector, size * sizeof(char *)); if (strlist->vector == NULL) { return NULL; } strlist->size = size; return strlist; }
void sircc_chan_add_user(struct sircc_chan *chan, const char *user, size_t sz) { if (sz == (size_t)-1) sz = strlen(user); for (size_t i = 0; i < chan->nb_users; i++) { if (memcmp(chan->users[i], user, sz) == 0) return; } if (!chan->users) { chan->nb_users = 0; chan->users = c_malloc(sizeof(char *)); } else { chan->users = c_realloc(chan->users, (chan->nb_users + 1) * sizeof(char *)); } chan->users[chan->nb_users] = c_strndup(user, sz); chan->nb_users++; chan->users_sorted = false; }
/* * This hook is called for with the response of a request. Here its checked * if a Set-Cookie header is there for the PHPSESSID. The key is stored into * the webdav session to be added to subsequent requests. */ static void post_request_hook(ne_request *req, void *userdata, const ne_status *status) { const char *set_cookie_header = NULL; const char *sc = NULL; char *key = NULL; (void) userdata; if (dav_session.session_key) return; /* We already have a session cookie, and we should ignore other ones */ if(!(status && req)) return; if( status->klass == 2 || status->code == 401 ) { /* successful request */ set_cookie_header = ne_get_response_header( req, "Set-Cookie" ); if( set_cookie_header ) { DEBUG_WEBDAV(" Set-Cookie found: %s", set_cookie_header); /* try to find a ', ' sequence which is the separator of neon if multiple Set-Cookie * headers are there. * The following code parses a string like this: * Set-Cookie: 50ace6bd8a669=p537brtt048jh8srlp2tuep7em95nh9u98mj992fbqc47d1aecp1; */ sc = set_cookie_header; while(sc) { const char *sc_val = sc; const char *sc_end = sc_val; int cnt = 0; int len = strlen(sc_val); /* The length of the rest of the header string. */ while( cnt < len && *sc_end != ';' && *sc_end != ',') { cnt++; sc_end++; } if( cnt == len ) { /* exit: We are at the end. */ sc = NULL; } else if( *sc_end == ';' ) { /* We are at the end of the session key. */ int keylen = sc_end-sc_val; if( key ) { int oldlen = strlen(key); key = c_realloc(key, oldlen + 2 + keylen+1); strcpy(key + oldlen, "; "); strncpy(key + oldlen + 2, sc_val, keylen); key[oldlen + 2 + keylen] = '\0'; } else { key = c_malloc(keylen+1); strncpy( key, sc_val, keylen ); key[keylen] = '\0'; } /* now search for a ',' to find a potential other header entry */ while(cnt < len && *sc_end != ',') { cnt++; sc_end++; } if( cnt < len ) sc = sc_end+2; /* mind the space after the comma */ else sc = NULL; } else if( *sc_end == ',' ) { /* A new entry is to check. */ if( *(sc_end + 1) == ' ') { sc = sc_end+2; } else { /* error condition */ sc = NULL; } } } } } else { DEBUG_WEBDAV("Request failed, don't take session header."); } if( key ) { DEBUG_WEBDAV("----> Session-key: %s", key); SAFE_FREE(dav_session.session_key); dav_session.session_key = key; } }