static bool decode_struct(DecodeBuffer* input, PyObject* output, PyObject* spec_seq) { int spec_seq_len = PyTuple_Size(spec_seq); if (spec_seq_len == -1) { return false; } while (true) { TType type; int16_t tag; PyObject* item_spec; PyObject* fieldval = NULL; StructItemSpec parsedspec; type = readByte(input); if (type == -1) { return false; } if (type == T_STOP) { break; } tag = readI16(input); if (INT_CONV_ERROR_OCCURRED(tag)) { return false; } if (tag >= 0 && tag < spec_seq_len) { item_spec = PyTuple_GET_ITEM(spec_seq, tag); } else { item_spec = Py_None; } if (item_spec == Py_None) { if (!skip(input, type)) { return false; } else { continue; } } if (!parse_struct_item_spec(&parsedspec, item_spec)) { return false; } if (parsedspec.type != type) { PyErr_SetString(PyExc_TypeError, "struct field had wrong type while reading"); return false; } fieldval = decode_val(input, parsedspec.type, parsedspec.typeargs); if (fieldval == NULL) { return false; } if (PyObject_SetAttr(output, parsedspec.attrname, fieldval) == -1) { Py_DECREF(fieldval); return false; } Py_DECREF(fieldval); } return true; }
int om_pgsql_write(struct filed *f, int flags, struct m_msg *m, void *ctx) { void *r; struct om_pgsql_ctx *c; int err, i; char query[MAX_QUERY], err_buf[512], facility[18], priority[18], *p; m_dprintf(MSYSLOG_INFORMATIVE, "om_pgsql_write: entering [%s] [%s]\n", m->msg, f->f_prevline); c = (struct om_pgsql_ctx *) ctx; if ((c->h) == NULL) { m_dprintf(MSYSLOG_SERIOUS, "om_pgsql_write: error, no " "connection\n"); return (-1); } if ((c->PQstatus(c->h)) == CONNECTION_BAD) { /* try to reconnect */ (c->PQreset(c->h)); /* connection can't be established */ if ((c->PQstatus(c->h)) == CONNECTION_BAD) { c->lost++; if (c->lost == 1) { logerror("om_pgsql_write: Lost connection!"); } } return (1); } if (c->flags & OM_PGSQL_FACILITY) { if ((p = decode_val(m->fac<<3, CODE_FACILITY)) != NULL) snprintf(facility, sizeof(facility), "'%s',", p); else snprintf(facility, sizeof(facility), "'%d',", m->fac<<3); } if (c->flags & OM_PGSQL_PRIORITY) { if ((p = decode_val(m->pri, CODE_PRIORITY)) != NULL) snprintf(priority, sizeof(priority), "'%s',", p); else snprintf(priority, sizeof(priority), "'%d',", m->pri); } /* table, YYYY-Mmm-dd, hh:mm:ss, host, $1 (msg) */ i = snprintf(query, sizeof(query), "INSERT INTO %s (%s%sdate, time, " "host, message) VALUES(%s%s'%.4d-%.2d-%.2d', '%.2d:%.2d:%.2d', '%s', $1);", c->table, (c->flags & OM_PGSQL_FACILITY)? "facility, " : "", (c->flags & OM_PGSQL_PRIORITY)? "priority, " : "", (c->flags & OM_PGSQL_FACILITY)? facility : "", (c->flags & OM_PGSQL_PRIORITY)? priority : "", f->f_tm.tm_year + 1900, f->f_tm.tm_mon + 1, f->f_tm.tm_mday, f->f_tm.tm_hour, f->f_tm.tm_min, f->f_tm.tm_sec, f->f_prevhost); if (c->lost) { /* * Report lost messages, but 2 of them are lost of * connection and this one (which we are going * to log anyway) */ snprintf(err_buf, sizeof(err_buf), "om_pgsql_write: %i " "messages were lost due to lack of connection", c->lost - 2); /* count reset */ c->lost = 0; m_dprintf(MSYSLOG_INFORMATIVE2, "om_pgsql_write: query [%s] with msg $1 [%s]\n", query, err_buf); const char *paramValues[1]; paramValues[0] = err_buf; r = (c->PQexecParams(c->h, query, 1, NULL, paramValues, NULL, NULL, 0)); if ((c->PQresultStatus(r)) != PGRES_COMMAND_OK) { m_dprintf(MSYSLOG_SERIOUS, "om_pgsql_write: %s\n", (c->PQresultErrorMessage(r))); return (-1); } (c->PQclear(r)); } m_dprintf(MSYSLOG_INFORMATIVE2, "om_pgsql_write: query [%s] with msg $1 [%s]\n", query, m->msg); const char *paramValues[1]; paramValues[0] = (m->msg); r = (c->PQexecParams(c->h, query, 1, NULL, paramValues, NULL, NULL, 0)); err = 1; if ((c->PQresultStatus(r)) != PGRES_COMMAND_OK) { m_dprintf(MSYSLOG_INFORMATIVE, "%s\n", (c->PQresultErrorMessage(r))); err = -1; } (c->PQclear(r)); return (err); }
// Returns a new reference. static PyObject* decode_val(DecodeBuffer* input, TType type, PyObject* typeargs, long string_limit, long container_limit) { switch (type) { case T_BOOL: { int8_t v = readByte(input); if (INT_CONV_ERROR_OCCURRED(v)) { return NULL; } switch (v) { case 0: Py_RETURN_FALSE; case 1: Py_RETURN_TRUE; // Don't laugh. This is a potentially serious issue. default: PyErr_SetString(PyExc_TypeError, "boolean out of range"); return NULL; } break; } case T_I08: { int8_t v = readByte(input); if (INT_CONV_ERROR_OCCURRED(v)) { return NULL; } return PyInt_FromLong(v); } case T_I16: { int16_t v = readI16(input); if (INT_CONV_ERROR_OCCURRED(v)) { return NULL; } return PyInt_FromLong(v); } case T_I32: { int32_t v = readI32(input); if (INT_CONV_ERROR_OCCURRED(v)) { return NULL; } return PyInt_FromLong(v); } case T_I64: { int64_t v = readI64(input); if (INT_CONV_ERROR_OCCURRED(v)) { return NULL; } // TODO(dreiss): Find out if we can take this fastpath always when // sizeof(long) == sizeof(long long). if (CHECK_RANGE(v, LONG_MIN, LONG_MAX)) { return PyInt_FromLong((long) v); } return PyLong_FromLongLong(v); } case T_DOUBLE: { double v = readDouble(input); if (v == -1.0 && PyErr_Occurred()) { return false; } return PyFloat_FromDouble(v); } case T_STRING: { Py_ssize_t len = readI32(input); char* buf; if (!readBytes(input, &buf, len)) { return NULL; } if (!check_length_limit(len, string_limit)) { return NULL; } if (is_utf8(typeargs)) return PyUnicode_DecodeUTF8(buf, len, 0); else return PyString_FromStringAndSize(buf, len); } case T_LIST: case T_SET: { SetListTypeArgs parsedargs; int32_t len; PyObject* ret = NULL; int i; bool use_tuple = false; if (!parse_set_list_args(&parsedargs, typeargs)) { return NULL; } if (!checkTypeByte(input, parsedargs.element_type)) { return NULL; } len = readI32(input); if (!check_length_limit(len, container_limit)) { return NULL; } use_tuple = type == T_LIST && parsedargs.immutable; ret = use_tuple ? PyTuple_New(len) : PyList_New(len); if (!ret) { return NULL; } for (i = 0; i < len; i++) { PyObject* item = decode_val(input, parsedargs.element_type, parsedargs.typeargs, string_limit, container_limit); if (!item) { Py_DECREF(ret); return NULL; } if (use_tuple) { PyTuple_SET_ITEM(ret, i, item); } else { PyList_SET_ITEM(ret, i, item); } } // TODO(dreiss): Consider biting the bullet and making two separate cases // for list and set, avoiding this post facto conversion. if (type == T_SET) { PyObject* setret; setret = parsedargs.immutable ? PyFrozenSet_New(ret) : PySet_New(ret); Py_DECREF(ret); return setret; } return ret; } case T_MAP: { int32_t len; int i; MapTypeArgs parsedargs; PyObject* ret = NULL; if (!parse_map_args(&parsedargs, typeargs)) { return NULL; } if (!checkTypeByte(input, parsedargs.ktag)) { return NULL; } if (!checkTypeByte(input, parsedargs.vtag)) { return NULL; } len = readI32(input); if (!check_length_limit(len, container_limit)) { return NULL; } ret = PyDict_New(); if (!ret) { goto error; } for (i = 0; i < len; i++) { PyObject* k = NULL; PyObject* v = NULL; k = decode_val(input, parsedargs.ktag, parsedargs.ktypeargs, string_limit, container_limit); if (k == NULL) { goto loop_error; } v = decode_val(input, parsedargs.vtag, parsedargs.vtypeargs, string_limit, container_limit); if (v == NULL) { goto loop_error; } if (PyDict_SetItem(ret, k, v) == -1) { goto loop_error; } Py_DECREF(k); Py_DECREF(v); continue; // Yuck! Destructors, anyone? loop_error: Py_XDECREF(k); Py_XDECREF(v); goto error; } if (parsedargs.immutable) { PyObject* thrift = PyImport_ImportModule("thrift.Thrift"); PyObject* cls = NULL; PyObject* arg = NULL; if (!thrift) { goto error; } cls = PyObject_GetAttrString(thrift, "TFrozenDict"); if (!cls) { goto error; } arg = PyTuple_New(1); PyTuple_SET_ITEM(arg, 0, ret); return PyObject_CallObject(cls, arg); } return ret; error: Py_XDECREF(ret); return NULL; } case T_STRUCT: { StructTypeArgs parsedargs; if (!parse_struct_args(&parsedargs, typeargs)) { return NULL; } return decode_struct(input, Py_None, parsedargs.klass, parsedargs.spec, string_limit, container_limit); } case T_STOP: case T_VOID: case T_UTF16: case T_UTF8: case T_U64: default: PyErr_SetString(PyExc_TypeError, "Unexpected TType"); return NULL; } }
static PyObject* decode_struct(DecodeBuffer* input, PyObject* output, PyObject* klass, PyObject* spec_seq, long string_limit, long container_limit) { int spec_seq_len = PyTuple_Size(spec_seq); bool immutable = output == Py_None; PyObject* kwargs = NULL; if (spec_seq_len == -1) { return NULL; } if (immutable) { kwargs = PyDict_New(); if (!kwargs) { PyErr_SetString(PyExc_TypeError, "failed to prepare kwargument storage"); return NULL; } } while (true) { TType type; int16_t tag; PyObject* item_spec; PyObject* fieldval = NULL; StructItemSpec parsedspec; type = readByte(input); if (type == -1) { goto error; } if (type == T_STOP) { break; } tag = readI16(input); if (INT_CONV_ERROR_OCCURRED(tag)) { goto error; } if (tag >= 0 && tag < spec_seq_len) { item_spec = PyTuple_GET_ITEM(spec_seq, tag); } else { item_spec = Py_None; } if (item_spec == Py_None) { if (!skip(input, type)) { goto error; } else { continue; } } if (!parse_struct_item_spec(&parsedspec, item_spec)) { goto error; } if (parsedspec.type != type) { if (!skip(input, type)) { PyErr_Format(PyExc_TypeError, "struct field had wrong type: expected %d but got %d", parsedspec.type, type); goto error; } else { continue; } } fieldval = decode_val(input, parsedspec.type, parsedspec.typeargs, string_limit, container_limit); if (fieldval == NULL) { goto error; } if ((immutable && PyDict_SetItem(kwargs, parsedspec.attrname, fieldval) == -1) || (!immutable && PyObject_SetAttr(output, parsedspec.attrname, fieldval) == -1)) { Py_DECREF(fieldval); goto error; } Py_DECREF(fieldval); } if (immutable) { PyObject* args = PyTuple_New(0); PyObject* ret = NULL; if (!args) { PyErr_SetString(PyExc_TypeError, "failed to prepare argument storage"); goto error; } ret = PyObject_Call(klass, args, kwargs); Py_DECREF(kwargs); Py_DECREF(args); return ret; } Py_INCREF(output); return output; error: Py_XDECREF(kwargs); return NULL; }
// Returns a new reference. static PyObject* decode_val(DecodeBuffer* input, TType type, PyObject* typeargs) { switch (type) { case T_BOOL: { int8_t v = readByte(input); if (INT_CONV_ERROR_OCCURRED(v)) { return NULL; } switch (v) { case 0: Py_RETURN_FALSE; case 1: Py_RETURN_TRUE; // Don't laugh. This is a potentially serious issue. default: PyErr_SetString(PyExc_TypeError, "boolean out of range"); return NULL; } break; } case T_I08: { int8_t v = readByte(input); if (INT_CONV_ERROR_OCCURRED(v)) { return NULL; } return PyInt_FromLong(v); } case T_I16: { int16_t v = readI16(input); if (INT_CONV_ERROR_OCCURRED(v)) { return NULL; } return PyInt_FromLong(v); } case T_I32: { int32_t v = readI32(input); if (INT_CONV_ERROR_OCCURRED(v)) { return NULL; } return PyInt_FromLong(v); } case T_I64: { int64_t v = readI64(input); if (INT_CONV_ERROR_OCCURRED(v)) { return NULL; } // TODO(dreiss): Find out if we can take this fastpath always when // sizeof(long) == sizeof(long long). if (CHECK_RANGE(v, LONG_MIN, LONG_MAX)) { return PyInt_FromLong((long) v); } return PyLong_FromLongLong(v); } case T_DOUBLE: { double v = readDouble(input); if (v == -1.0 && PyErr_Occurred()) { return false; } return PyFloat_FromDouble(v); } case T_STRING: { Py_ssize_t len = readI32(input); char* buf; if (!readBytes(input, &buf, len)) { return NULL; } return PyString_FromStringAndSize(buf, len); } case T_LIST: case T_SET: { SetListTypeArgs parsedargs; int32_t len; PyObject* ret = NULL; int i; if (!parse_set_list_args(&parsedargs, typeargs)) { return NULL; } if (!checkTypeByte(input, parsedargs.element_type)) { return NULL; } len = readI32(input); if (!check_ssize_t_32(len)) { return NULL; } ret = PyList_New(len); if (!ret) { return NULL; } for (i = 0; i < len; i++) { PyObject* item = decode_val(input, parsedargs.element_type, parsedargs.typeargs); if (!item) { Py_DECREF(ret); return NULL; } PyList_SET_ITEM(ret, i, item); } // TODO(dreiss): Consider biting the bullet and making two separate cases // for list and set, avoiding this post facto conversion. if (type == T_SET) { PyObject* setret; #if (PY_VERSION_HEX < 0x02050000) // hack needed for older versions setret = PyObject_CallFunctionObjArgs((PyObject*)&PySet_Type, ret, NULL); #else // official version setret = PySet_New(ret); #endif Py_DECREF(ret); return setret; } return ret; } case T_MAP: { int32_t len; int i; MapTypeArgs parsedargs; PyObject* ret = NULL; if (!parse_map_args(&parsedargs, typeargs)) { return NULL; } if (!checkTypeByte(input, parsedargs.ktag)) { return NULL; } if (!checkTypeByte(input, parsedargs.vtag)) { return NULL; } len = readI32(input); if (!check_ssize_t_32(len)) { return false; } ret = PyDict_New(); if (!ret) { goto error; } for (i = 0; i < len; i++) { PyObject* k = NULL; PyObject* v = NULL; k = decode_val(input, parsedargs.ktag, parsedargs.ktypeargs); if (k == NULL) { goto loop_error; } v = decode_val(input, parsedargs.vtag, parsedargs.vtypeargs); if (v == NULL) { goto loop_error; } if (PyDict_SetItem(ret, k, v) == -1) { goto loop_error; } Py_DECREF(k); Py_DECREF(v); continue; // Yuck! Destructors, anyone? loop_error: Py_XDECREF(k); Py_XDECREF(v); goto error; } return ret; error: Py_XDECREF(ret); return NULL; } case T_STRUCT: { StructTypeArgs parsedargs; if (!parse_struct_args(&parsedargs, typeargs)) { return NULL; } PyObject* ret = PyObject_CallObject(parsedargs.klass, NULL); if (!ret) { return NULL; } if (!decode_struct(input, ret, parsedargs.spec)) { Py_DECREF(ret); return NULL; } return ret; } case T_STOP: case T_VOID: case T_UTF16: case T_UTF8: case T_U64: default: PyErr_SetString(PyExc_TypeError, "Unexpected TType"); return NULL; } }
int huffman_dec_64(uint8_t *in, short *out, short *last_block, huff_tree_t *dc_tree, huff_tree_t *ac_tree) { //printf("DECODE HUFF\n\n\n"); memset(out, 0, sizeof(short) * 64); uint8_t store = in[0]; int p = 0; int in_pos = 0; int out_pos = 0; // find dc // read bit from store huff_tree_t *tree = dc_tree; huff_node_t *cur = tree->root; int dc_found = 0; int dc_len = -1; while (dc_found == 0) { while (p < 8) { int bit = (store >> (8 - p - 1)) & 1; p++; //printf("read bit %d\n", bit); if (bit == 0) { //printf("go left\n"); cur = cur->left; } else { //printf("go right\n"); cur = cur->right; } if (cur->payload != NULL) { if (cur->payload_type == DC) { dc_payload_t *pl = (dc_payload_t*)cur->payload; //printf("got DC payload, cat: %d\n", pl->cat); dc_len = pl->cat; dc_found = 1; break; } } } if (p >= 8) { in_pos++; store = in[in_pos]; p = 0; } // didn't find dc yet } //printf("p: %d, in_pos: %d\n", p, in_pos); int dc_val = read_n_bits(in, &store, &p, &in_pos, dc_len); int dval = decode_val(dc_val, dc_len); //printf("dc_val: %s [%d]\n", int2bin(dc_val, NULL), dval); //printf("p: %d, in_pos: %d\n", p, in_pos); if (last_block != NULL) { dval = last_block[0] - dval; } out[out_pos] = dval; out_pos++; // decode ac's // should go until EOB // while (!not_found) cur = ac_tree->root; int eob_found = 0; while (eob_found == 0 && out_pos < 64) { while (p < 8 && out_pos < 64) { int bit = (store >> (8 - p - 1)) & 1; p++; //printf("read bit %d\n", bit); if (bit == 0) { //printf("go left\n"); cur = cur->left; } else { //printf("go right\n"); cur = cur->right; } if (cur == NULL) { goto err; } if (cur->payload != NULL) { if (cur->payload_type == AC) { ac_payload_t *pl = (ac_payload_t*)cur->payload; //printf("got AC payload, cat: %d, run: %d\n", pl->cat, pl->run); if (pl->cat == 0 && pl->run == 0) { eob_found = 1; } else if (pl->cat == 0 && pl->run == 0xF) { for (int z = 0; z < pl->run; z++) { out[out_pos++] = 0; if (out_pos >= 64) { //assert(0); goto done; } } cur = ac_tree->root; } else { // read ac_val int aclen = pl->cat; int acval = read_n_bits(in, &store, &p, &in_pos, aclen); int dval = decode_val(acval, aclen); for (int z = 0; z < pl->run; z++) { out[out_pos++] = 0; if (out_pos >= 64) { //assert(0); goto done; } } if (out_pos >= 64) goto done; out[out_pos++] = dval; if (out_pos >= 64) { //assert(0); goto done; } // reset tree cur = ac_tree->root; } break; } } } if (p >= 8 && eob_found == 0) { //printf("extend\n"); in_pos++; store = in[in_pos]; p = 0; } } done: //printf("DECODE HUFF DONE\n\n\n"); return 0; err: printf("ERROR DECODING\n"); return 1; }
int om_oracle8i_write(struct filed *f, int flags, struct m_msg *m, void *ctx) { struct om_oracle8i_ctx *c; /* These should be preserved between calls, so they can be reused. */ static void *bnd_prio = NULL, *bnd_fac = NULL, *bnd_tstamp = NULL, *bnd_host = NULL, *bnd_msg = NULL; char err_buf[512]; char facility[16], priority[16], tstamp[100], *p; int err; m_dprintf(MSYSLOG_INFORMATIVE, "om_oracle8i_write: entering [%s] [%s]\n", m->msg, f->f_prevline); c = (struct om_oracle8i_ctx *)ctx; if (!c->loggedon) { err = c->OCILogon(c->env, c->err, &c->svc, c->user, strlen(c->user), c->passwd, strlen(c->passwd), c->db, strlen(c->db)); if (OCI_SUCCESS != err) { return -1; } else { c->loggedon = 1; } } /* Bind the placeholders in the INSERT statement. */ if ((c->flags & OM_ORACLE8I_PRIORITY && (err = c->OCIBindByName(c->stmt, &bnd_prio, c->err, ":PRIORITY", -1, priority, sizeof priority, SQLT_STR, NULL, NULL, NULL, 0, NULL, OCI_DEFAULT))) || (c->flags & OM_ORACLE8I_FACILITY && (err = c->OCIBindByName(c->stmt, &bnd_fac, c->err, ":FACILITY", -1, facility, sizeof facility, SQLT_STR, NULL, NULL, NULL, 0, NULL, OCI_DEFAULT))) || (err = c->OCIBindByName(c->stmt, &bnd_tstamp, c->err, ":TIMESTAMP", -1, tstamp, sizeof tstamp, SQLT_STR, NULL, NULL, NULL, 0, NULL, OCI_DEFAULT)) || (err = c->OCIBindByName(c->stmt, &bnd_host, c->err, ":HOST", -1, f->f_prevhost, strlen(f->f_prevhost)+1, SQLT_STR, NULL, NULL, NULL, 0, NULL, OCI_DEFAULT)) || (err = c->OCIBindByName(c->stmt, &bnd_msg, c->err, ":MESSAGE", -1, m->msg, strlen(m->msg)+1, SQLT_STR, NULL, NULL, NULL, 0, NULL, OCI_DEFAULT))) { c->OCIErrorGet(c->err, 1, NULL, &err, err_buf, sizeof err_buf, OCI_HTYPE_ERROR); m_dprintf(MSYSLOG_SERIOUS, "om_oracle8i_write: Error " "binding the placeholders for [%s]\n", err_buf); return -1; } /* Assign the proper values to the bound variables. */ if (c->flags & OM_ORACLE8I_FACILITY) { if ((p = decode_val(m->fac << 3, CODE_FACILITY)) != NULL) snprintf(facility, sizeof (facility), "'%s',", p); else snprintf(facility, sizeof (facility), "'%d',", m->fac << 3); } if (c->flags & OM_ORACLE8I_PRIORITY) { if ((p = decode_val(m->pri, CODE_PRIORITY)) != NULL) snprintf(priority, sizeof (priority), "'%s',", p); else snprintf(priority, sizeof (priority), "'%d',", m->pri); } snprintf(tstamp, sizeof tstamp, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d", f->f_tm.tm_year + 1900, f->f_tm.tm_mon + 1, f->f_tm.tm_mday, f->f_tm.tm_hour, f->f_tm.tm_min, f->f_tm.tm_sec); tstamp[sizeof (tstamp) - 1] = '\0'; /* Execute the INSERT statement. */ err = c->OCIStmtExecute(c->svc, c->stmt, c->err, 1, 0, NULL, NULL, OCI_COMMIT_ON_SUCCESS); if (OCI_SUCCESS != err) { c->OCIErrorGet(c->err, 1, NULL, &err, err_buf, sizeof err_buf, OCI_HTYPE_ERROR); m_dprintf(MSYSLOG_SERIOUS, "om_oracle8i_write: Error " "executing statement [%s]\n", err_buf); return -1; } return 1; }