Esempio n. 1
0
/* {{{ */
zend_bool pthreads_globals_init(){
	if (!PTHREADS_G(init)&&!PTHREADS_G(failed)) {
		PTHREADS_G(init)=1;
		if (!(PTHREADS_G(monitor)=pthreads_monitor_alloc()))
			PTHREADS_G(failed)=1;
		if (PTHREADS_G(failed)) {
		    PTHREADS_G(init)=0;
		} else {
		    zend_hash_init(
		    	&PTHREADS_G(objects), 64, NULL, (dtor_func_t) NULL, 1);
			zend_hash_init(
				&PTHREADS_G(gstrings), 64, NULL, (dtor_func_t) NULL, 1);
		}

#define INIT_STRING(n, v) PTHREADS_G(strings).n = zend_string_init(v, 1)
		INIT_STRING(run, ZEND_STRL("run"));
		INIT_STRING(session.cache_limiter, ZEND_STRL("cache_limiter"));
		INIT_STRING(session.use_cookies, ZEND_STRL("use_cookies"));
#undef INIT_STRING
		
		ZVAL_STR(&PTHREADS_G(strings).worker, zend_string_init(ZEND_STRL("worker"), 1));

		return PTHREADS_G(init);
	} else return 0;
} /* }}} */
Esempio n. 2
0
IOString *IOProcess_readProcess(const char *command) {
	
	FILE *proccess;
	proccess = popen(command, "r");
	const size_t readBuffer = 4096;
	
	size_t bufferSize = readBuffer;
	size_t currentBuffer = 0;
	char *bufferString = malloc(bufferSize + 1);
	memset(&bufferString[0], 0, bufferSize + 1);
	char *responseString = malloc(bufferSize + 1);
	memset(&responseString[0], 0, bufferSize + 1);
	
	if (proccess != NULL) {
		
		while (1) {
			char *line;
			line = fgets(bufferString, readBuffer, proccess);
			
			if (line == NULL) {
				break;
			}else{
				
				size_t lineSize = strlen(line);
				memcpy(&responseString[currentBuffer], line, lineSize);
				currentBuffer += lineSize;
				
				if(currentBuffer > (bufferSize - 256)) {
					bufferSize += readBuffer;
					responseString = realloc(responseString, bufferSize + 1);
					
					if(responseString != NULL) {
						memset(&responseString[currentBuffer], 0, readBuffer + 1);
					}
				}
			}
		}
		
		pclose(proccess);
	}else{
		free(bufferString);
		free(responseString);
		return NULL;
	}
	
	memset(&responseString[currentBuffer], 0, sizeof(char));
	free(bufferString);
	IOString *retval = INIT_STRING(responseString);
	free(responseString);
	return retval;
}
Esempio n. 3
0
// Generate a string representation of a buffer with adjustable number base (2-36).
char* memstr(const char* buf, size_t length, u_int8_t base)
{
    // Base 1 would be interesting (ie. infinite). Past base 36 and we need to start picking printable character sets.
    if (base < 2 || base > 36) return NULL;
    
    // Determine how many characters will be needed in the output for each byte of input (256 bits, 8 bits to a byte).
    u_int8_t chars_per_byte = (256 / base / 8);
    
    // Patches bases over 32.
    if (chars_per_byte < 1) chars_per_byte = 1;
    
    // Size of the result string.
    size_t rlength = (length * chars_per_byte);
    char* result;
    INIT_STRING(result, rlength + 1);
    
    // Init the string's bytes to the character zero so that positions we don't write to have something printable.
    memset(result, '0', malloc_size(result));
    
    // We build the result string from the tail, so here's the index in that string, starting at the end.
    u_int8_t ridx = rlength - 1;
    for (size_t byte = 0; byte < length; byte++) {
        unsigned char c = buf[byte];    // Grab the current char from the input.
        while (c != 0 && ridx >= 0) {   // Iterate until we're out of input or output.
            u_int8_t idx = c % base;
            result[ridx] = (idx < 10 ? '0' + idx : 'A' + (idx-10)); // GO ASCII! 0-9 then A-Z (up to base 36, see above).
            c /= base;                  // Shave off the processed portion.
            ridx--;                     // Move left in the result string.
        }
    }
    
    // Cap the string.
    result[rlength] = '\0';
    
    // Send it back.
    return result;
}
Esempio n. 4
0
PyMODINIT_FUNC
init_zope_security_checker(void)
{
  PyObject* m;

  CheckerType.tp_new = PyType_GenericNew;
  if (PyType_Ready(&CheckerType) < 0)
    return;

  _defaultChecker = PyObject_CallFunction((PyObject*)&CheckerType, "{}");
  if (_defaultChecker == NULL)
    return;

#define INIT_STRING(S) \
if((str_##S = PyString_InternFromString(#S)) == NULL) return

  INIT_STRING(checkPermission);
  INIT_STRING(__Security_checker__);
  INIT_STRING(interaction);

  if ((_checkers = PyDict_New()) == NULL)
    return;

  NoProxy = PyObject_CallObject((PyObject*)&PyBaseObject_Type, NULL);
  if (NoProxy == NULL)
    return;

  if ((m = PyImport_ImportModule("zope.security._proxy")) == NULL) return;
  if ((Proxy = PyObject_GetAttrString(m, "_Proxy")) == NULL) return;
  Py_DECREF(m);

  if ((m = PyImport_ImportModule("zope.security._definitions")) == NULL) return;
  thread_local = PyObject_GetAttrString(m, "thread_local");
  if (thread_local == NULL) return;
  Py_DECREF(m);

  if ((m = PyImport_ImportModule("zope.security.interfaces")) == NULL) return;
  ForbiddenAttribute = PyObject_GetAttrString(m, "ForbiddenAttribute");
  if (ForbiddenAttribute == NULL) return;
  Unauthorized = PyObject_GetAttrString(m, "Unauthorized");
  if (Unauthorized == NULL) return;
  Py_DECREF(m);

  if ((m = PyImport_ImportModule("zope.security.checker")) == NULL) return;
  CheckerPublic = PyObject_GetAttrString(m, "CheckerPublic");
  if (CheckerPublic == NULL) return;
  Py_DECREF(m);

  if ((_available_by_default = PyList_New(0)) == NULL) return;

  m = Py_InitModule3("_zope_security_checker", module_methods,
                     "C optimizations for zope.security.checker");

  if (m == NULL)
    return;

#define EXPORT(N) Py_INCREF(N); PyModule_AddObject(m, #N, N)

  EXPORT(_checkers);
  EXPORT(NoProxy);
  EXPORT(_defaultChecker);
  EXPORT(_available_by_default);

  Py_INCREF(&CheckerType);
  PyModule_AddObject(m, "Checker", (PyObject *)&CheckerType);
}
Esempio n. 5
0
static int
init_strings(void)
{
#define INIT_STRING(S)                              \
  if (!(py_ ## S = INTERN(#S)))  \
    return -1;
  INIT_STRING(keys);
  INIT_STRING(setstate);
  INIT_STRING(timeTime);
  INIT_STRING(__dict__);
  INIT_STRING(_p_changed);
  INIT_STRING(_p_deactivate);
  INIT_STRING(__getattr__);
  INIT_STRING(__setattr__);
  INIT_STRING(__delattr__);
  INIT_STRING(__slotnames__);
  INIT_STRING(__getnewargs__);
  INIT_STRING(__getstate__);
  INIT_STRING(unsaved);
  INIT_STRING(ghost);
  INIT_STRING(saved);
  INIT_STRING(changed);
  INIT_STRING(sticky);
#undef INIT_STRING
  return 0;
}