/* Serialize the contents of the opaque ppr struct. * Writes number of bytes copied, zero on error. * Returns error code, BCME_OK if successful. */ int ppr_serialize(const ppr_t* pprptr, uint8* buf, uint buflen, uint* bytes_copied) { int err = BCME_OK; if (buflen <= sizeof(ppr_ser_mem_flag_t)) { err = BCME_BUFTOOSHORT; } else { ppr_ser_mem_flag_t *smem_flag = (ppr_ser_mem_flag_t *)buf; uint32 flag = NTOH32(smem_flag->flag); /* check if memory contains a valid flag, if not, use current * condition (num of chains, txbf etc.) to serialize data. */ if (NTOH32(smem_flag->magic_word) != PPR_SER_MEM_WORD) { flag = ppr_get_flag(); } if (buflen >= ppr_ser_size_by_flag(flag, pprptr->ch_bw)) { *bytes_copied = ppr_serialize_data(pprptr, buf, flag); } else { err = BCME_BUFTOOSHORT; } } return err; }
int bit_file::alloc_read_data(FILE* f) { unsigned int i; // Read length if (fread(&length, 4, 1, f) != 1) return -1; length = NTOH32(length); data = (u8*)malloc(length); if (data == NULL) return -1; if (fread(data, 1, length, f) != length) { clear(); return -1; } // Reverse bytes for (i = 0; i < length; i++) data[i] = reverse8(data[i]); return 0; }
/* Deserialize the contents of a buffer into an opaque ppr struct. * Creates an opaque structure referenced by *pptrptr, NULL on error. * Returns error code, BCME_OK if successful. */ int ppr_deserialize(ppr_t* pprptr, const uint8* buf, uint buflen) { const uint8* bptr = buf; int err = BCME_OK; ASSERT(pprptr); if ((buflen > SER_HDR_LEN) && (bptr != NULL) && (*bptr == PPR_SERIALIZATION_VER)) { const ppr_deser_header_t * ser_head = (const ppr_deser_header_t *)bptr; wl_tx_bw_t ch_bw = ser_head->bw; /* struct size plus header */ uint32 ser_size = ppr_pwrs_size(ch_bw) + SER_HDR_LEN; if ((buflen >= ser_size) && (ch_bw == pprptr->ch_bw)) { uint32 flags = NTOH32(ser_head->flags); ppr_set_cmn_val(pprptr, WL_RATE_DISABLED); ppr_deser_cpy(pprptr, bptr + sizeof(*ser_head), flags, ch_bw); } else if (buflen < ser_size) { err = BCME_BUFTOOSHORT; } else { err = BCME_BADARG; } } else if (buflen <= SER_HDR_LEN) { err = BCME_BUFTOOSHORT; } else if (bptr == NULL) { err = BCME_BADARG; } else { err = BCME_VERSION; } return err; }
/* Deserialize the contents of a buffer into an opaque ppr struct. * Creates an opaque structure referenced by *pptrptr, NULL on error. * Returns error code, BCME_OK if successful. */ int ppr_deserialize_create(osl_t *osh, const uint8* buf, uint buflen, ppr_t** pprptr) { const uint8* bptr = buf; int err = BCME_OK; ppr_t* lpprptr = NULL; if ((buflen > SER_HDR_LEN) && (bptr != NULL) && (*bptr == PPR_SERIALIZATION_VER)) { const ppr_deser_header_t * ser_head = (const ppr_deser_header_t *)bptr; wl_tx_bw_t ch_bw = ser_head->bw; /* struct size plus header */ uint32 ser_size = ppr_pwrs_size(ch_bw) + SER_HDR_LEN; if ((buflen >= ser_size) && ((lpprptr = ppr_create(osh, ch_bw)) != NULL)) { uint32 flags = NTOH32(ser_head->flags); /* set the data with default value before deserialize */ ppr_set_cmn_val(lpprptr, WL_RATE_DISABLED); ppr_deser_cpy(lpprptr, bptr + sizeof(*ser_head), flags, ch_bw); } else if (buflen < ser_size) { err = BCME_BUFTOOSHORT; } else { err = BCME_NOMEM; } } else if (buflen <= SER_HDR_LEN) { err = BCME_BUFTOOSHORT; } else if (bptr == NULL) { err = BCME_BADARG; } else { err = BCME_VERSION; } *pprptr = lpprptr; return err; }
void fill32array(char *mem, char *array) { char *memptr, *arrptr; int i, n_elements; memptr = mem; arrptr = array; /* 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 */ NTOH32(memptr, arrptr); /* next element */ memptr += SIZE32; arrptr += SIZE32; } }
static void det_unbind_mw(struct det_mw * const mw) { /* Determine if the MW is currently bound. */ if (!mw->attr.base.length) return; /* Remove the r_key from the map. */ wiremap_write_lock_bh(); idr_remove(&det_wire_map, NTOH32(mw->attr.base.r_key)); wiremap_write_unlock_bh(); atomic_dec(&mw->mr->windows); mw->mr = NULL; mw->attr.base.length = 0; mw->attr.base.access = 0; mw->attr.base.r_key = HTON32(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; } }