void analyze_label_sector(uint8_t *sector, struct vdm_label **dl, struct vdm_boot_info **dbi) { struct vdm_label *l; struct vdm_boot_info *bi; l = (struct vdm_label *)(sector + VDM_LABEL_OFFSET); if (betoh32(l->signature) != VDM_LABEL_SIGNATURE) { l = (struct vdm_label *)(sector + VDM_LABEL_OFFSET_ALT); if (betoh32(l->signature) != VDM_LABEL_SIGNATURE) l = NULL; } if (l != NULL) { bi = (struct vdm_boot_info *) (sector + VDM_BLOCK_SIZE - sizeof *bi); if (betoh32(bi->signature) != VDM_LABEL_SIGNATURE) bi = NULL; } else bi = NULL; *dl = l; *dbi = bi; }
uint32_t print_vdmpart_instance(uint8_t *buf, uint32_t size) { struct vdit_vdmpart_instance entry; if (size < sizeof entry) { printf("\tTRUNCATED ENTRY (%02x bytes, expected %02zx)\n", size, sizeof entry); return 0; } memcpy(&entry, buf, sizeof entry); entry.version = betoh16(entry.version); entry.start_blkno = betoh32(entry.start_blkno); entry.size = betoh32(entry.size); printf("\tvdmpart: version %02x", entry.version); print_vdit_instance_id(&entry.child_instance, " child"); printf("\n"); printf("\t\tstarting block %08x size %08x", entry.start_blkno, entry.size); print_vdit_instance_id(&entry.remap_instance, " remap"); printf("\n"); return size - sizeof entry; }
uint32_t get_vdit_size(int fd, uint32_t secno) { uint8_t sector[VDM_BLOCK_SIZE]; struct vdit_block_header *hdr; uint32_t cursize = 0; int kind = VDIT_BLOCK_HEAD_BE; for (;;) { read_sector(fd, secno, sector); hdr = (struct vdit_block_header *)sector; if (VDM_ID_KIND(&hdr->id) != kind) { printf("unexpected VDIT block kind " "on sector %08x: %02x\n", secno, VDM_ID_KIND(&hdr->id)); return 0; } if (verbose) printf("sector %08x: vdit frag type %02x, length %04x, " "next frag at %08x\n", secno, VDM_ID_KIND(&hdr->id), betoh16(hdr->chunksz), secno); cursize += betoh16(hdr->chunksz); if (betoh32(hdr->nextblk) == VDM_NO_BLK_NUMBER) break; secno = betoh32(hdr->nextblk); kind = VDIT_PORTION_HEADER_BLOCK; } return cursize; }
/* * Retrieves next node, skipping all the children nodes of the pointed node * if passed 0 wil return first node of the tree (root) */ void * fdt_next_node(void *node) { u_int32_t *ptr; if (!tree_inited) return NULL; ptr = node; if (!node) { ptr = tree.tree; return (betoh32(*ptr) == FDT_NODE_BEGIN) ? ptr : NULL; } if (betoh32(*ptr) != FDT_NODE_BEGIN) return NULL; ptr++; ptr = skip_node_name(ptr); ptr = skip_props(ptr); /* skip children */ while (betoh32(*ptr) == FDT_NODE_BEGIN) ptr = fdt_next_node(ptr); return (betoh32(*ptr) == FDT_NODE_END) ? (ptr + 1) : NULL; }
void report(int fd) { uint8_t *vdit, *vdit2, *tmpvdit; size_t vditsize, vditsize2; struct vdm_label *dl; struct vdm_boot_info *bi; struct disklabel *lp; uint8_t sector[VDM_BLOCK_SIZE]; struct vdit_block_header *hdr; read_sector(fd, VDM_LABEL_SECTOR, sector); analyze_label_sector(sector, &dl, &bi); if (dl == NULL) return; printf("label version %04x\n", betoh16(dl->version)); if (bi != NULL) printf("disk boot info: start %08x size %08x version %08x\n", betoh32(bi->boot_start), betoh32(bi->boot_size), betoh32(bi->version)); read_sector(fd, VDIT_SECTOR, sector); hdr = (struct vdit_block_header *)sector; if (VDM_ID_KIND(&hdr->id) != VDIT_BLOCK_HEAD_BE) { lp = (struct disklabel *)(sector + LABELOFFSET); if (lp->d_magic == DISKMAGIC && lp->d_magic2 == DISKMAGIC) { if (verbose) printf("no VDIT but a native OpenBSD label\n"); return; } errx(3, "unexpected block kind on sector %08x: %02x", 1, VDM_ID_KIND(&hdr->id)); } vdit = read_vdit(fd, 1, &vditsize); if (vdit != NULL) { tmpvdit = vdit; while (tmpvdit != NULL) tmpvdit = print_vdit_entry(tmpvdit); vdit2 = read_vdit(fd, betoh32(hdr->secondary_vdit), &vditsize2); if (vdit2 == NULL) printf("can't read backup VDIT\n"); else { if (vditsize2 < vditsize) { printf("WARNING: backup VDIT is smaller " "than main VDIT!\n"); vditsize = vditsize2; } if (memcmp(vdit, vdit2, vditsize) != 0) printf("VDIT and backup VDIT differ!\n"); free(vdit2); } free(vdit); } }
/* * Retrieves node property, the returned pointer is inside the fdt tree, * so we should not modify content pointed by it directly. * A NULL out parameter will cause this function to only return the size. */ int fdt_node_property(void *node, char *name, char **out) { u_int32_t *ptr; u_int32_t nameid; char *tmp; if (!tree_inited) return 0; ptr = (u_int32_t *)node; if (betoh32(*ptr) != FDT_NODE_BEGIN) return 0; ptr = skip_node_name(ptr + 1); while (betoh32(*ptr) == FDT_PROPERTY) { nameid = betoh32(*(ptr + 2)); /* id of name in strings table */ tmp = fdt_get_str(nameid); if (!strcmp(name, tmp)) { if (out != NULL) *out = (char *)(ptr + 3); /* begining of the value */ return betoh32(*(ptr + 1)); /* size of value */ } ptr = skip_property(ptr); } return 0; }
void ikev1_recv(struct iked *env, struct iked_message *msg) { struct ike_header *hdr; if (ibuf_size(msg->msg_data) <= sizeof(*hdr)) { log_debug("%s: short message", __func__); return; } hdr = (struct ike_header *)ibuf_data(msg->msg_data); log_debug("%s: header ispi %s rspi %s" " nextpayload %u version 0x%02x exchange %u flags 0x%02x" " msgid %u length %u", __func__, print_spi(betoh64(hdr->ike_ispi), 8), print_spi(betoh64(hdr->ike_rspi), 8), hdr->ike_nextpayload, hdr->ike_version, hdr->ike_exchange, hdr->ike_flags, betoh32(hdr->ike_msgid), betoh32(hdr->ike_length)); log_debug("%s: IKEv1 not supported", __func__); }
int ikev2_pld_parse(struct iked *env, struct ike_header *hdr, struct iked_message *msg, off_t offset) { log_debug("%s: header ispi %s rspi %s" " nextpayload %s version 0x%02x exchange %s flags 0x%02x" " msgid %d length %d response %d", __func__, print_spi(betoh64(hdr->ike_ispi), 8), print_spi(betoh64(hdr->ike_rspi), 8), print_map(hdr->ike_nextpayload, ikev2_payload_map), hdr->ike_version, print_map(hdr->ike_exchange, ikev2_exchange_map), hdr->ike_flags, betoh32(hdr->ike_msgid), betoh32(hdr->ike_length), msg->msg_response); if (ibuf_size(msg->msg_data) < betoh32(hdr->ike_length)) { log_debug("%s: short message", __func__); return (-1); } offset += sizeof(*hdr); return (ikev2_pld_payloads(env, msg, offset, betoh32(hdr->ike_length), hdr->ike_nextpayload, 0)); }
void read_file(void) { FILE *fp; int i; if (!quiet) printf("Reading file %s\n", vfilename); for (i = 0; i < SGI_SIZE_VOLDIR; ++i) { if (strncmp(vfilename, volhdr->voldir[i].name, strlen(volhdr->voldir[i].name)) == 0) break; } if (i >= SGI_SIZE_VOLDIR) errx(1, "%s: file not found", vfilename); /* XXX assumes volume header starts at 0? */ lseek(fd, betoh32(volhdr->voldir[i].block) * DEV_BSIZE, SEEK_SET); if ((fp = fopen(ufilename, "w")) == NULL) err(1, "open %s", ufilename); i = betoh32(volhdr->voldir[i].bytes); while (i > 0) { if (read(fd, buf, bufsize) != bufsize) err(1, "read file"); fwrite(buf, 1, i > bufsize ? bufsize : i, fp); i -= i > bufsize ? bufsize : i; } fclose(fp); }
int handle_msg_vault(void *data, unsigned int n) { uint8_t cmd; uint32_t id; if (n < 1) return 0; cmd = *(uint8_t *)data; n--; data++; if (cmd == 1) /* LIST */ { int i; uint32_t *buf = malloc(the_vault->num_contents * 8); if (buf == NULL) { write_msg(1, NULL, 0); } else { for (i = 0; i < the_vault->num_contents; i++) { buf[i * 2] = htobe32((uint32_t)&the_vault->contents[i]); buf[i * 2 + 1] = htobe32(the_vault->contents[i].len); } write_msg(1, buf, the_vault->num_contents * 8); free(buf); } } else if (cmd == 2) /* STORE */ { id = htobe32(store_in_vault(0, data, n)); write_msg(2, &id, sizeof(uint32_t)); } else if (cmd == 3 && n >= 4) /* UPDATE */ { id = betoh32(*(uint32_t *)data); id = htobe32(store_in_vault(id, data + 4, n - 4)); write_msg(3, &id, sizeof(uint32_t)); } else if (cmd == 4 && n >= 4) /* RETRIEVE */ { void *outdata; unsigned int len; id = betoh32(*(uint32_t *)data); outdata = retrieve_from_vault(id, &len); if (outdata == NULL) write_msg(4, NULL, 0); else write_msg(4, outdata, len); free(outdata); } return 1; }
/** * Initializes a property object from its database data * * The property's id should be set before it is initialized * * Args: * self: The property being initialized * data: An array of bytes that represent this property in the database (17 bytes) * * Returns: FABRIC_OK on success, other error code on failure */ error_t Fabric_Property_init(Property *self, uint8_t *data) { // A Propery's id must be set externally if (self->id < 1) { return FABRIC_PROPERTY_INVALID_ID; } self->label_id = betoh32(*(labelid_t*)(data)); self->next_property_id = betoh32(*(propertyid_t*)(data + 4)); self->type = data[8]; memcpy(&self->data, (data + 9), 8); return FABRIC_OK; }
/* * show_symtab() * show archive ranlib index (fs5) */ int show_symtab(off_t off, u_long len, const char *name, FILE *fp) { struct ar_hdr ar_head; int *symtab, *ps; char *strtab, *p; int num, rval = 0; int namelen; if ((symtab = malloc(len)) == NULL) { warn("%s: malloc", name); return 1; } if (pread(fileno(fp), symtab, len, off) != len) { free(symtab); warn("%s: pread", name); return 1; } namelen = sizeof(ar_head.ar_name); if ((p = malloc(sizeof(ar_head.ar_name))) == NULL) { warn("%s: malloc", name); free(symtab); return 1; } printf("\nArchive index:\n"); num = betoh32(*symtab); strtab = (char *)(symtab + num + 1); for (ps = symtab + 1; num--; ps++, strtab += strlen(strtab) + 1) { if (pread(fileno(fp), &ar_head, sizeof ar_head, betoh32(*ps)) != sizeof ar_head || memcmp(ar_head.ar_fmag, ARFMAG, sizeof(ar_head.ar_fmag))) { warnx("%s: member pread", name); rval = 1; break; } *p = '\0'; if (mmbr_name(&ar_head, &p, 0, &namelen, fp)) { rval = 1; break; } printf("%s in %s\n", strtab, p); } free(p); free(symtab); return (rval); }
void print_vdit_instance_id(struct vdit_instance_id *buf, const char *descr) { struct vdit_instance_id instance; memcpy(&instance, buf, sizeof instance); instance.generation_timestamp = betoh32(instance.generation_timestamp); instance.system_id = betoh32(instance.system_id); if (instance.generation_timestamp != 0 || instance.system_id != 0 || verbose) printf("%s id %08x:%08x", descr, instance.generation_timestamp, instance.system_id); }
unsigned int fdt_check_head(void *fdt) { struct fdt_head *fh; u_int32_t *ptr; fh = fdt; ptr = (u_int32_t *)fdt; if (betoh32(fh->fh_magic) != FDT_MAGIC) return 0; if (betoh32(fh->fh_version) > FDT_CODE_VERSION) return 0; if (betoh32(*(ptr + (betoh32(fh->fh_struct_off) / 4))) != FDT_NODE_BEGIN) return 0; #ifdef notyet /* check for end signature on version 17 blob */ if ((betoh32(fh->fh_version) >= 17) & (betoh32(*(ptr + (betoh32(fh->fh_struct_off) / 4) + (betoh32(fh->fh_struct_size) / 4))) != FDT_END)) return 0; #endif return betoh32(fh->fh_version); }
/* * Debug methods for printing whole tree, particular odes and properies */ void * fdt_print_property(void *node, int level) { u_int32_t *ptr; char *tmp, *value; int cnt; u_int32_t nameid, size; ptr = (u_int32_t *)node; if (!tree_inited) return NULL; if (betoh32(*ptr) != FDT_PROPERTY) return ptr; /* should never happen */ /* extract property name_id and size */ size = betoh32(*++ptr); nameid = betoh32(*++ptr); for (cnt = 0; cnt < level; cnt++) printf("\t"); tmp = fdt_get_str(nameid); printf("\t%s : ", tmp ? tmp : "NO_NAME"); ptr++; value = (char *)ptr; if (!strcmp(tmp, "device_type") || !strcmp(tmp, "compatible") || !strcmp(tmp, "model") || !strcmp(tmp, "bootargs") || !strcmp(tmp, "linux,stdout-path")) { printf("%s", value); } else if (!strcmp(tmp, "clock-frequency") || !strcmp(tmp, "timebase-frequency")) { printf("%d", betoh32(*((unsigned int *)value))); } else { for (cnt = 0; cnt < size; cnt++) { if ((cnt % sizeof(u_int32_t)) == 0) printf(" "); printf("%02x", value[cnt]); } } ptr += roundup(size, sizeof(u_int32_t)) / sizeof(u_int32_t); printf("\n"); return ptr; }
uint32_t print_vdit_instance_info(uint8_t *buf, uint32_t size) { struct vdit_instance_entry entry; vdit_id_t id; if (size < sizeof entry) { printf("\tTRUNCATED ENTRY (%02x bytes, expected %02zx)\n", size, sizeof entry); return 0; } memcpy(&entry, buf, sizeof entry); entry.version = betoh16(entry.version); printf("\tinstance: version %02x name \"%s\"\n", entry.version, entry.name); memcpy(&id, &entry.subdriver_id, sizeof id); id = betoh32(id); printf("\t\tsubdriver id %08x", id); print_vdit_instance_id(&entry.instance_id, ""); printf(" export %d\n", entry.exported); if (id == subdriver_vdmphys_id) return print_vdmphys_instance(buf, size); if (id == subdriver_vdmpart_id) return print_vdmpart_instance(buf, size); if (id == subdriver_vdmaggr_id) return print_vdmaggr_instance(buf, size); if (id == subdriver_vdmremap_id) return print_vdmremap_instance(buf, size); return size - sizeof entry; }
/* file should be in full USB packet format (ubertooth-dump -f) */ int stream_rx_file(FILE* fp, uint16_t num_blocks, rx_callback cb, void* cb_args) { uint8_t bank = 0; uint8_t buf[BUFFER_SIZE]; size_t nitems; /* unused parameter */ num_blocks = num_blocks; /* fprintf(stderr, "reading %d blocks of 64 bytes from file\n", num_blocks); */ while(1) { uint32_t systime_be; nitems = fread(&systime_be, sizeof(systime_be), 1, fp); if (nitems != 1) return 0; systime = (time_t)betoh32(systime_be); nitems = fread(buf, sizeof(buf[0]), PKT_LEN, fp); if (nitems != PKT_LEN) return 0; (*cb)(cb_args, (usb_pkt_rx *)buf, bank); bank = (bank + 1) % NUM_BANKS; } }
void register_subdriver(uint8_t *buf) { struct vdit_subdriver_entry entry; vdit_id_t id, *regid; memcpy(&entry, buf, sizeof entry); entry.version = betoh16(entry.version); memcpy(&id, &entry.subdriver_id, sizeof id); id = betoh32(id); if (strcmp(entry.name, VDM_SUBDRIVER_VDMPHYS) == 0) regid = &subdriver_vdmphys_id; else if (strcmp(entry.name, VDM_SUBDRIVER_VDMPART) == 0) regid = &subdriver_vdmpart_id; else if (strcmp(entry.name, VDM_SUBDRIVER_VDMAGGR) == 0) regid = &subdriver_vdmaggr_id; else if (strcmp(entry.name, VDM_SUBDRIVER_VDMREMAP) == 0) regid = &subdriver_vdmremap_id; else regid = NULL; if (regid != NULL) { if (*regid != 0) printf("WARNING: subdriver \"%s\" overriden\n", entry.name); *regid = id; } }
void * skip_props(u_int32_t *ptr) { while (betoh32(*ptr) == FDT_PROPERTY) { ptr = skip_property(ptr); } return ptr; }
/* Read an unsigned 32-bit integer from a big-endian file. */ int read_uint32be(int fd, uint32_t* buf) { size_t n; n = read(fd, (char*) buf, 4); *buf = betoh32(*buf); return n; }
UInt32 ByteOrder::ntoh32(UInt32 data) { #ifndef ANDROID return be32toh(data); #else return betoh32(data); #endif //ANDROID }
static int readInt() { int ret = *(int*)(s_data + s_pos); if(s_bigEndian) ret = betoh32(ret); else ret = letoh32(ret); s_pos += sizeof(int); return ret; }
int cksum(int ck, int *p, int size) { /* we assume size is int-aligned */ for (size = (size + sizeof(int) - 1) / sizeof(int); size--; p++ ) ck += betoh32(*p); return ck; }
/** * Initializes a text object from it's stored data * * This does not initialize the text objects text value. * the text value must be set separately. The reason * for this is that it may be expensive in terms of * memory to load the text's value, and there are * situations in which it is sufficient to only load * the size. * * The text objects id should be set before this method * is used. * * Args: * self: The text object being initialized * data: The text object's header (4 bytes) * * Returns: FABRIC_OK on success, other error number of failure */ error_t Fabric_Text_init(Text *self, uint8_t *data) { if (self->id < 1) { return FABRIC_TEXT_INVALID_ID; } self->size = betoh32(*(uint32_t*)data); // The value isn't loaded at initialization time // It must be set externally self->value = NULL; return FABRIC_OK; }
int scsi_cmd(struct scsi_state *s, void *cdb_p, size_t len) { union scsi_cdb *cdb = cdb_p; enum scsi_cmd cmd; size_t alloc_len; if (len < 1) goto error; cmd = cdb->op; switch (cmd) { case SCSI_CMD_TEST_UNIT_READY: break; case SCSI_CMD_INQUIRY: if (len < sizeof(cdb->inquiry)) goto error; if (cdb->inquiry.evpd || cdb->inquiry.page != 0) goto error; scsi_queue_send_data(s, s->inquiry, sizeof(*s->inquiry), betoh16(&cdb->inquiry.alloc_len)); break; case SCSI_CMD_REQUEST_SENSE: scsi_queue_send_data(s, &s->sense, sizeof(s->sense), cdb->request_sense.alloc_len); break; case SCSI_CMD_READ_CAPACITY_10: scsi_queue_send_data(s, &s->read_capacity, sizeof(s->read_capacity), sizeof(s->read_capacity)); break; case SCSI_CMD_READ_10: case SCSI_CMD_WRITE_10: s->cmd_lba = betoh32(&cdb->read_write_10.lba); s->cmd_len = betoh16(&cdb->read_write_10.length); if (s->cmd_lba > s->read_capacity.lba || s->cmd_lba + s->cmd_len > s->read_capacity.lba) { s->cmd_len = 0; goto error; } if (cmd == SCSI_CMD_READ_10) s->dir = SCSI_STATE_DIR_RECV; else s->dir = SCSI_STATE_DIR_SEND; break; } return (0); error: s->sense.sense_key = SCSI_SENSE_ILLEGAL_REQUEST; return (-1); }
/* * Return the size of the FDT. */ u_int32_t fdt_get_size(void *fdt) { if (!fdt) return 0; if (!fdt_check_head(fdt)) return 0; return betoh32(((struct fdt_head *)fdt)->fh_size); }
/* * Utility functions for skipping parts of tree. */ void * skip_property(u_int32_t *ptr) { u_int32_t size; size = betoh32(*(ptr + 1)); /* move forward by magic + size + nameid + rounded up property size */ ptr += 3 + roundup(size, sizeof(u_int32_t)) / sizeof(u_int32_t); return ptr; }
/* * Retrieves next node, skipping all the children nodes of the pointed node */ void * fdt_child_node(void *node) { u_int32_t *ptr; if (!tree_inited) return NULL; ptr = node; if (betoh32(*ptr) != FDT_NODE_BEGIN) return NULL; ptr++; ptr = skip_node_name(ptr); ptr = skip_props(ptr); /* check if there is a child node */ return (betoh32(*ptr) == FDT_NODE_BEGIN) ? (ptr) : NULL; }
int get_media_capabilities(int *cap) { scsireq_t scr; u_char buf[4096]; int error; u_int32_t i, dsz; u_int16_t feature; *cap = 0; memset(buf, 0, sizeof(buf)); memset(&scr, 0, sizeof(scr)); scr.cmd[0] = SCSI_GET_CONFIGURATION; scr.cmd[1] = 1; /* enumerate only "current" features */ *(u_int16_t *)(scr.cmd + 7) = betoh16(sizeof(buf)); scr.flags = SCCMD_ESCAPE | SCCMD_READ; scr.databuf = buf; scr.datalen = sizeof(buf); scr.cmdlen = 10; scr.timeout = 120000; scr.senselen = SENSEBUFLEN; error = ioctl(fd, SCIOCCOMMAND, &scr); if (error == -1 || scr.retsts != 0) return (-1); if (scr.datalen_used < 8) return (-1); /* can't get header */ dsz = betoh32(*(u_int32_t *)buf); if (dsz > scr.datalen_used - 4) dsz = scr.datalen_used - 4; dsz += 4; /* total size of bufer for all features */ i = 8; while (i <= dsz - 4) { if (dsz - i < (u_int32_t)buf[i + 3] + 4) break; /* partial feature descriptor */ feature = betoh16(*(u_int16_t *)(buf + i)); if (feature == MMC_FEATURE_CDRW_CAV) *cap |= MEDIACAP_CDRW_CAV; else if (feature == MMC_FEATURE_CD_TAO) *cap |= MEDIACAP_TAO; else if (feature == MMC_FEATURE_CDRW_WRITE) *cap |= MEDIACAP_CDRW_WRITE; i += 4 + buf[i + 3]; } return (0); }
void checksum_vol(void) { int32_t *l; int i; volhdr->checksum = checksum = 0; l = (int32_t *)buf; for (i = 0; i < sizeof(struct sgilabel) / sizeof(int32_t); ++i) checksum += betoh32(l[i]); volhdr->checksum = htobe32(-checksum); }