void fillInt16Array(char *mem, int16 *array) { char *memptr; int i, n_elements; memptr = mem; /* skip some elements to get to data */ memptr += 3*SIZE32; /* get number of elements */ n_elements = ntohl( *(int32 *) memptr ); memptr += 2*SIZE32; for(i=0; i<n_elements; i++){ /* Need to skip an int, which is size of this array element */ /* This is always same for float32 */ memptr += SIZE32; /* copy in converted to host */ NTOH16(memptr, &array[i]); /* next element */ memptr += SIZE16; } }
/* Alloc buffer for var bind decoding */ static struct x_var_bind * var_bind_alloc(uint8_t **buffer, uint8_t flag, enum agentx_err_code *err) { struct x_var_bind *vb; uint16_t type; uint32_t oid_len, val_len; uint8_t *buf, *buf1; buf = *buffer; /* value type */ if (flag & NETWORD_BYTE_ORDER) { type = NTOH16(*(uint16_t *)buf); } else { type = *(uint16_t *)buf; } buf += 4; /* oid length */ buf1 = buf; oid_len = agentx_value_dec_try(buf1, flag, ASN1_TAG_OBJID); if (oid_len / sizeof(uint32_t) > ASN1_OID_MAX_LEN) { *err = AGENTX_ERR_VB_OID_LEN; return NULL; } if (oid_len > 0) { buf += 4 + oid_len - 5 * sizeof(uint32_t); } else { buf += 4; } /* value length */ val_len = agentx_value_dec_try(buf, flag, type); if (val_len > ASN1_VALUE_MAX_LEN) { *err = AGENTX_ERR_VB_VALUE_LEN; return NULL; } /* varbind allocation */ vb = vb_new(oid_len, val_len); if (vb == NULL) { *err = AGENTX_ERR_VB_VAR; return NULL; } /* OID assignment */ vb->oid_len = agentx_value_dec(&buf1, flag, ASN1_TAG_OBJID, vb->oid); /* value assignment */ vb->val_type = type; vb->val_len = agentx_value_dec(&buf, flag, type, vb->value); *buffer = buf; *err = AGENTX_ERR_OK; return vb; }
int bit_file::read_field(FILE* f, char* field, size_t maxlen) { u16 len; if (fread(&len, 1, 2, f) != 2) return -1; len = NTOH16(len); if (field == NULL) { fseek(f, len, SEEK_CUR); return 0; } maxlen = maxlen - 1 < len ? maxlen - 1 : len; if (fread(field, 1, maxlen, f) != maxlen) return -1; field[maxlen] = 0; if (len > maxlen) fseek(f, len - maxlen, SEEK_CUR); return 0; }
/* Parse PDU header */ static AGENTX_ERR_CODE_E pdu_hdr_parse(struct agentx_datagram *xdg, uint8_t **buffer) { AGENTX_ERR_CODE_E err; uint8_t *buf; err = AGENTX_ERR_OK; buf = *buffer; /* PDU header */ xdg->pdu_hdr.version = *buf++; xdg->pdu_hdr.type = *buf++; xdg->pdu_hdr.flags = *buf++; xdg->pdu_hdr.reserved = *buf++; if (xdg->pdu_hdr.flags & NETWORD_BYTE_ORDER) { xdg->pdu_hdr.session_id = NTOH32(*(uint32_t *)buf); buf += sizeof(uint32_t); xdg->pdu_hdr.transaction_id = NTOH32(*(uint32_t *)buf); buf += sizeof(uint32_t); xdg->pdu_hdr.packet_id = NTOH32(*(uint32_t *)buf); buf += sizeof(uint32_t); xdg->pdu_hdr.payload_length = NTOH32(*(uint32_t *)buf); buf += sizeof(uint32_t); } else { xdg->pdu_hdr.session_id = *(uint32_t *)buf; buf += sizeof(uint32_t); xdg->pdu_hdr.transaction_id = *(uint32_t *)buf; buf += sizeof(uint32_t); xdg->pdu_hdr.packet_id = *(uint32_t *)buf; buf += sizeof(uint32_t); xdg->pdu_hdr.payload_length = *(uint32_t *)buf; buf += sizeof(uint32_t); } /* Optinal context */ if (xdg->pdu_hdr.flags & NON_DEFAULT_CONTEXT) { if (xdg->pdu_hdr.flags & NETWORD_BYTE_ORDER) { xdg->ctx_len = NTOH32(*(uint32_t *)buf); } else { xdg->ctx_len = *(uint32_t *)buf; } if (xdg->ctx_len + 1 > sizeof(xdg->context)) { err = AGENTX_ERR_PDU_CTX_LEN; *buffer = buf; return err; } buf += sizeof(uint32_t); memcpy(xdg->context, buf, xdg->ctx_len); buf += uint_sizeof(xdg->ctx_len); xdg->pdu_hdr.payload_length -= 4 + uint_sizeof(xdg->ctx_len); } /* additional data */ switch (xdg->pdu_hdr.type) { case AGENTX_PDU_CLOSE: xdg->u.close.reason = *buf; buf += sizeof(uint32_t); xdg->pdu_hdr.payload_length -= sizeof(uint32_t); break; case AGENTX_PDU_GETBULK: if (xdg->pdu_hdr.flags & NETWORD_BYTE_ORDER) { xdg->u.getbulk.non_rep = NTOH16(*(uint16_t *)buf); } else { xdg->u.getbulk.non_rep = *(uint16_t *)buf; } buf += sizeof(uint16_t); if (xdg->pdu_hdr.flags & NETWORD_BYTE_ORDER) { xdg->u.getbulk.max_rep = NTOH16(*(uint16_t *)buf); } else { xdg->u.getbulk.max_rep = *(uint16_t *)buf; } buf += sizeof(uint16_t); xdg->pdu_hdr.payload_length -= 2 * sizeof(uint16_t); break; case AGENTX_PDU_RESPONSE: if (xdg->pdu_hdr.flags & NETWORD_BYTE_ORDER) { xdg->u.response.sys_up_time = NTOH32(*(uint32_t *)buf); } else { xdg->u.response.sys_up_time = *(uint32_t *)buf; } buf += sizeof(uint32_t); if (xdg->pdu_hdr.flags & NETWORD_BYTE_ORDER) { xdg->u.response.error = NTOH16(*(uint16_t *)buf); } else { xdg->u.response.error = *(uint16_t *)buf; } buf += sizeof(uint16_t); xdg->u.response.index = *(uint16_t *)buf; if (xdg->pdu_hdr.flags & NETWORD_BYTE_ORDER) { xdg->u.response.index = NTOH16(*(uint16_t *)buf); } else { xdg->u.response.index = *(uint16_t *)buf; } buf += sizeof(uint16_t); xdg->pdu_hdr.payload_length -= sizeof(uint32_t) + 2 * sizeof(uint16_t); default: break; } *buffer = buf; return err; }
void pgsql_store_binary(int32 idlType, int16 isarray, char *input, UCHAR *output) { switch(idlType) { case IDL_TYP_FLOAT: /* atof actually returns double */ if (isarray) fillNumArray(input, output, 4); else NTOH32(input, output); break; case IDL_TYP_DOUBLE: if (isarray) fillNumArray(input, output, 8); else NTOH64(input, output); break; case IDL_TYP_BYTE: if (isarray) fillNumArray(input, output, 1); else *(UCHAR *) output = *(UCHAR *) input; break; case IDL_TYP_INT: if (isarray) fillNumArray(input, output, 2); else NTOH16(input, output); break; case IDL_TYP_UINT: if (isarray) fillNumArray(input, output, 2); else NTOH16(input, output); break; case IDL_TYP_LONG: if (isarray) fillNumArray(input, output, 4); else NTOH32(input, output); break; case IDL_TYP_ULONG: if (isarray) fillNumArray(input, output, 4); else NTOH32(input, output); break; case IDL_TYP_LONG64: if (isarray) fillNumArray(input, output, 8); else NTOH64(input, output); break; case IDL_TYP_ULONG64: if (isarray) fillNumArray(input, output, 8); else NTOH64(input, output); break; case IDL_TYP_STRING: if (isarray) fillStringArray(input, (IDL_STRING *) output); else IDL_StrStore( (IDL_STRING *) output, input); break; default: printf("Unsupported type %d found\n", idlType); fflush(stdout); break; } }
void fillNumArray(char *mem, UCHAR* array, int32 nbytes) { //char *memptr, *arrptr; UCHAR* memptr; UCHAR* arrptr; int i, n_elements; memptr = (UCHAR *) mem; arrptr = (UCHAR *) array; /* skip some elements to get to data */ memptr += 3*SIZE32; /* get number of elements */ n_elements = ntohl( *(int32 *) memptr ); memptr += 2*SIZE32; switch(nbytes) { case 1: for (i=0; i< n_elements; i++) { /* Need to skip an int, which is size of this array element */ /* This is always same for float32 */ memptr += SIZE32; *arrptr = *memptr; /* next element */ memptr += nbytes; arrptr += nbytes; } break; case 2: for(i=0; i<n_elements; i++) { /* Need to skip an int, which is size of this array element */ /* This is always same for float32 */ memptr += SIZE32; /* copy in converted to host */ NTOH16(memptr, arrptr); /* next element */ memptr += nbytes; arrptr += nbytes; } break; case 4: for(i=0; i<n_elements; i++) { /* Need to skip an int, which is size of this array element */ /* This is always same for float32 */ memptr += SIZE32; /* copy in converted to host */ NTOH32(memptr, arrptr); /* next element */ memptr += nbytes; arrptr += nbytes; } break; case 8: for(i=0; i<n_elements; i++) { /* Need to skip an int, which is size of this array element */ /* This is always same for float32 */ memptr += SIZE32; /* copy in converted to host */ NTOH64(memptr, arrptr); /* next element */ memptr += nbytes; arrptr += nbytes; } break; default: printf("nbytes must be 2,4, or 8\n"); fflush(stdout); break; } }