static void g_dec_drvrdesc(u_char *ptr, struct g_apple_softc *sc) { sc->dd_bsiz = be16dec(ptr + 2); sc->dd_blkcnt = be32dec(ptr + 4); sc->dd_drvrcnt = be32dec(ptr + 16); }
static unsigned char * fifolog_reader_chop(struct fifolog_reader *fr, fifolog_reader_render_t *func, void *priv) { u_char *p, *q; uint32_t v, w, u; p = fr->obuf; q = fr->obuf + (fr->olen - fr->ff->zs->avail_out); while (1) { /* Make sure we have a complete header */ if (p + 5 >= q) return (p); w = 4; u = be32dec(p); if (u & FIFOLOG_TIMESTAMP) { fr->now = be32dec(p + 4); w += 4; } if (u & FIFOLOG_LENGTH) { v = p[w]; w++; if (p + w + v >= q) return (p); } else { for (v = 0; p + v + w < q && p[v + w] != '\0'; v++) continue; if (p + v + w >= q) return (p); v++; } func(priv, fr->now, u, p + w, v); p += w + v; } }
static int fifolog_reader_findsync(const struct fifolog_file *ff, off_t *o) { int e; unsigned seq, seqs; assert(*o < ff->logsize); e = fifolog_int_read(ff, *o); if (e) err(1, "Read error (%d) while looking for SYNC", e); seq = be32dec(ff->recbuf); if (*o == 0 && seq == 0) return (0); if (ff->recbuf[4] & FIFOLOG_FLG_SYNC) return (1); /* That was easy... */ while(1) { assert(*o < ff->logsize); (*o)++; seq++; if (*o == ff->logsize) return (2); /* wraparound */ e = fifolog_int_read(ff, *o); if (e) err(1, "Read error (%d) while looking for SYNC", e); seqs = be32dec(ff->recbuf); if (seqs != seq) return (3); /* End of log */ if (ff->recbuf[4] & FIFOLOG_FLG_SYNC) return (1); /* Bingo! */ } }
/* * Decode the relevant fields of a sun disk label, and return zero if the * magic and checksum works out OK. */ int sunlabel_dec(void const *pp, struct sun_disklabel *sl) { const uint8_t *p; size_t i; u_int u; p = pp; for (i = 0; i < sizeof(sl->sl_text); i++) sl->sl_text[i] = p[SL_TEXT + i]; sl->sl_rpm = be16dec(p + SL_RPM); sl->sl_pcylinders = be16dec(p + SL_PCYLINDERS); sl->sl_sparespercyl = be16dec(p + SL_SPARESPERCYL); sl->sl_interleave = be16dec(p + SL_INTERLEAVE); sl->sl_ncylinders = be16dec(p + SL_NCYLINDERS); sl->sl_acylinders = be16dec(p + SL_ACYLINDERS); sl->sl_ntracks = be16dec(p + SL_NTRACKS); sl->sl_nsectors = be16dec(p + SL_NSECTORS); for (i = 0; i < SUN_NPART; i++) { sl->sl_part[i].sdkp_cyloffset = be32dec(p + SL_PART + (i * SDKP_SIZEOF) + SDKP_CYLOFFSET); sl->sl_part[i].sdkp_nsectors = be32dec(p + SL_PART + (i * SDKP_SIZEOF) + SDKP_NSECTORS); } sl->sl_magic = be16dec(p + SL_MAGIC); for (i = u = 0; i < SUN_SIZE; i += 2) u ^= be16dec(p + i); if (u == 0 && sl->sl_magic == SUN_DKMAGIC) return (0); else return (EINVAL); }
/** * proto_lbs_request_parse(P, R): * Parse the packet ${P} into the LBS request structure ${R}. */ static int proto_lbs_request_parse(const struct wire_packet * P, struct proto_lbs_request * R) { /* Store request ID. */ R->ID = P->ID; /* Sanity-check packet length. */ if (P->len < 4) goto err0; /* Figure out request type. */ R->type = be32dec(&P->buf[0]); /* Parse packet. */ switch (R->type) { case PROTO_LBS_PARAMS: case PROTO_LBS_PARAMS2: if (P->len != 4) goto err0; /* Nothing to parse. */ break; case PROTO_LBS_GET: if (P->len != 12) goto err0; R->r.get.blkno = be64dec(&P->buf[4]); break; case PROTO_LBS_APPEND: if (P->len < 16) goto err0; R->r.append.nblks = be32dec(&P->buf[4]); R->r.append.blkno = be64dec(&P->buf[8]); if (R->r.append.nblks == 0) goto err0; if ((P->len - 16) % R->r.append.nblks) goto err0; R->r.append.blklen = (P->len - 16) / R->r.append.nblks; if ((R->r.append.buf = malloc(P->len - 16)) == NULL) goto err0; memcpy(R->r.append.buf, &P->buf[16], P->len - 16); break; case PROTO_LBS_FREE: if (P->len != 12) goto err0; R->r.free.blkno = be64dec(&P->buf[4]); break; default: goto err0; } /* Success! */ return (0); err0: /* Failure! */ return (-1); }
void read_kdfp(uint8_t *addr, kdfp *kdfp) { uint64_t *N = (uint64_t *) (addr + SALT_LEN); uint32_t *r = (uint32_t *) (N + 1); uint32_t *p = (uint32_t *) (r + 1); memcpy(kdfp->salt, addr, SALT_LEN); kdfp->N = be64dec(N); kdfp->r = be32dec(r); kdfp->p = be32dec(p); }
static void g_dec_apple_partition(u_char *ptr, struct apm_partition *d) { d->am_sig[0] = ptr[0]; d->am_sig[1] = ptr[1]; d->am_mapcnt = be32dec(ptr + 4); d->am_start = be32dec(ptr + 8); d->am_partcnt = be32dec(ptr + 12); memcpy(d->am_name, ptr + 16, 32); memcpy(d->am_type, ptr + 48, 32); }
static int test_be32dec() { uint8_t buf1[] = { 0x01, 0x02, 0x03, 0x04 }; uint8_t buf2[] = { 0xff, 0xef, 0xdf, 0xcf }; if(be32dec(buf1) != 0x01020304U) return -1; if(be32dec(buf2) != 0xffefdfcfU) return -1; return 0; }
static int scryptdec_setup(const uint8_t header[96], uint8_t dk[64], const uint8_t * passwd, size_t passwdlen, size_t maxmem, double maxmemfrac, double maxtime, int verbose) { uint8_t salt[32]; uint8_t hbuf[32]; int logN; uint32_t r; uint32_t p; uint64_t N; SHA256_CTX ctx; uint8_t * key_hmac = &dk[32]; HMAC_SHA256_CTX hctx; int rc; /* Parse N, r, p, salt. */ logN = header[7]; r = be32dec(&header[8]); p = be32dec(&header[12]); memcpy(salt, &header[16], 32); /* Verify header checksum. */ SHA256_Init(&ctx); SHA256_Update(&ctx, header, 48); SHA256_Final(hbuf, &ctx); if (memcmp(&header[48], hbuf, 16)) return (7); /* * Check whether the provided parameters are valid and whether the * key derivation function can be computed within the allowed memory * and CPU time. */ if ((rc = checkparams(maxmem, maxmemfrac, maxtime, logN, r, p, verbose)) != 0) return (rc); /* Compute the derived keys. */ N = (uint64_t)(1) << logN; if (crypto_scrypt(passwd, passwdlen, salt, 32, N, r, p, dk, 64)) return (3); /* Check header signature (i.e., verify password). */ HMAC_SHA256_Init(&hctx, key_hmac, 32); HMAC_SHA256_Update(&hctx, header, 64); HMAC_SHA256_Final(hbuf, &hctx); if (memcmp(hbuf, &header[64], 32)) return (11); /* Success! */ return (0); }
/* * Decode the relevant fields of a sun disk label, and return zero if the * magic and checksum works out OK. */ int sunlabel_dec(void const *pp, struct sun_disklabel *sl) { const uint8_t *p; size_t i; u_int u; uint32_t vtocsane; uint16_t npart; p = pp; for (i = 0; i < sizeof(sl->sl_text); i++) sl->sl_text[i] = p[SL_TEXT + i]; sl->sl_rpm = be16dec(p + SL_RPM); sl->sl_pcylinders = be16dec(p + SL_PCYLINDERS); sl->sl_sparespercyl = be16dec(p + SL_SPARESPERCYL); sl->sl_interleave = be16dec(p + SL_INTERLEAVE); sl->sl_ncylinders = be16dec(p + SL_NCYLINDERS); sl->sl_acylinders = be16dec(p + SL_ACYLINDERS); sl->sl_ntracks = be16dec(p + SL_NTRACKS); sl->sl_nsectors = be16dec(p + SL_NSECTORS); for (i = 0; i < SUN_NPART; i++) { sl->sl_part[i].sdkp_cyloffset = be32dec(p + SL_PART + (i * SDKP_SIZEOF) + SDKP_CYLOFFSET); sl->sl_part[i].sdkp_nsectors = be32dec(p + SL_PART + (i * SDKP_SIZEOF) + SDKP_NSECTORS); } sl->sl_magic = be16dec(p + SL_MAGIC); vtocsane = be32dec(p + SL_VTOC_SANITY); npart = be16dec(p + SL_VTOC_NPART); if (vtocsane == SUN_VTOC_SANE && npart == SUN_NPART) { /* * Seems we've got SVR4-compatible VTOC information * as well, decode it. */ sl->sl_vtoc_sane = vtocsane; sl->sl_vtoc_vers = be32dec(p + SL_VTOC_VERS); memcpy(sl->sl_vtoc_volname, p + SL_VTOC_VOLNAME, SUN_VOLNAME_LEN); sl->sl_vtoc_nparts = SUN_NPART; for (i = 0; i < SUN_NPART; i++) { sl->sl_vtoc_map[i].svtoc_tag = be16dec(p + SL_VTOC_MAP + (i * SVTOC_SIZEOF) + SVTOC_TAG); sl->sl_vtoc_map[i].svtoc_flag = be16dec(p + SL_VTOC_MAP + (i * SVTOC_SIZEOF) + SVTOC_FLAG); } } for (i = u = 0; i < SUN_SIZE; i += 2) u ^= be16dec(p + i); if (u == 0 && sl->sl_magic == SUN_DKMAGIC) return (0); else return (EINVAL); }
static int qcow_probe(struct vdsk *vdsk) { struct qcow_header *hdr; if (vdsk->sectorsize < 512 || vdsk->sectorsize > 4096) return (ENOTBLK); hdr = malloc(vdsk->sectorsize); if (hdr == NULL) return (errno); if (read(vdsk->fd, hdr, vdsk->sectorsize) != vdsk->sectorsize) goto out; if (be32dec(&hdr->magic) != QCOW_MAGIC) { errno = ENXIO; goto out; } errno = 0; out: free(hdr); return (errno); }
void sha256func(unsigned char *hash, const unsigned char *data, int len) { uint32_t S[16], T[16]; int i, r; sha256_init(S); for (r = len; r > -9; r -= 64) { if (r < 64) memset(T, 0, 64); memcpy(T, data + len - r, r > 64 ? 64 : (r < 0 ? 0 : r)); if (r >= 0 && r < 64) ((unsigned char *)T)[r] = 0x80; for (i = 0; i < 16; i++) T[i] = be32dec(T + i); if (r < 56) T[15] = 8 * len; sha256_transform(S, T, 0); } /* memcpy(S + 8, sha256d_hash1 + 8, 32); sha256_init(T); sha256_transform(T, S, 0); */ for (i = 0; i < 8; i++) be32enc((uint32_t *)hash + i, T[i]); }
static uint64_t tdecode(int t, ...) { uint8_t buf[16]; bool be = t > 0; va_list ap; uint64_t val = 777; int i; if (t < 0) t = -t; va_start(ap, t); memset(buf, 0xC1, sizeof(buf)); for (i = 0; i < t; i++) buf[i] = va_arg(ap, int); va_end(ap); if (be) { switch (t) { case 2: val = be16dec(buf); break; case 4: val = be32dec(buf); break; case 8: val = be64dec(buf); break; } } else { switch (t) { case 2: val = le16dec(buf); break; case 4: val = le32dec(buf); break; case 8: val = le64dec(buf); break; } } return val; }
uint32_t isns_req_get_status(struct isns_req *req) { if (req->ir_usedlen < sizeof(struct isns_hdr) + 4) return (-1); return (be32dec(&req->ir_buf[sizeof(struct isns_hdr)])); }
static uint32_t get_tpm_cmd_size(void *data_buffer) { if (!data_buffer) return 0; /* * The command header is formated by: * tag (2 bytes): 80 01 * length (4 bytes): 00 00 00 00 * ordinal(4 bytes): 00 00 00 00 */ return be32dec(data_buffer + 2); }
/* * Decode a big-endian length len vector of (uint8_t) into a length * len/4 vector of (uint32_t). Assumes len is a multiple of 4. */ static void be32dec_vect(uint32_t * dst, const uint8_t * src, size_t len) { size_t i; /* Sanity-check. */ assert(len % 4 == 0); /* Decode vector, one word at a time. */ for (i = 0; i < len / 4; i++) dst[i] = be32dec(src + i * 4); }
const char * fifolog_int_findend(const struct fifolog_file *ff, off_t *last) { off_t o, s; int e; unsigned seq0, seq; fifolog_int_file_assert(ff); o = 0; e = fifolog_int_read(ff, o); if (e) return("Read error, first record"); seq0 = be32dec(ff->recbuf); /* If the first records sequence is zero, the fifolog is empty */ if (seq0 == 0) { *last = o; return (NULL); } /* Do a binary search for a discontinuity in the sequence numbers */ s = ff->logsize / 2; do { e = fifolog_int_read(ff, o + s); if (e) return ("Read error while searching"); seq = be32dec(ff->recbuf); if (seq == seq0 + s) { o += s; seq0 = seq; } s /= 2; assert(o < ff->logsize); } while (s > 0); *last = o; return (NULL); }
static u_int64_t elf_get_word(Elf32_Ehdr *e, void *base, elf_member_t member) { u_int64_t val; val = 0; switch (e->e_ident[EI_CLASS]) { case ELFCLASS32: base = (char *)base + elf32_offsets[member]; switch (e->e_ident[EI_DATA]) { case ELFDATA2MSB: val = be32dec(base); break; case ELFDATA2LSB: val = le32dec(base); break; case ELFDATANONE: errx(1, "invalid data format"); } break; case ELFCLASS64: base = (char *)base + elf64_offsets[member]; switch (e->e_ident[EI_DATA]) { case ELFDATA2MSB: val = be32dec(base); break; case ELFDATA2LSB: val = le32dec(base); break; case ELFDATANONE: errx(1, "invalid data format"); } break; case ELFCLASSNONE: errx(1, "invalid class"); } return val; }
void uuid_dec_be(void const *buf, struct uuid *uuid) { const uint8_t *p = buf; int i; uuid->time_low = be32dec(p); uuid->time_mid = be16dec(p + 4); uuid->time_hi_and_version = be16dec(p + 6); uuid->clock_seq_hi_and_reserved = p[8]; uuid->clock_seq_low = p[9]; for (i = 0; i < _UUID_NODE_LEN; i++) uuid->node[i] = p[10 + i]; }
// // Verifies password hash (also ensures hash integrity at same time) // int Verify(const uint8_t* kdf, const uint8_t* passwd, size_t passwdSize) { uint64_t N=0; uint32_t r=0, p=0; uint8_t dk[64], salt[32], hbuf[32]; uint8_t * key_hmac = &dk[32]; HMAC_SHA256_CTX hctx; SHA256_CTX ctx; /* Parse N, r, p, salt. */ N = (uint64_t)1 << kdf[7]; //Remember, kdf[7] is actually LogN r = be32dec(&kdf[8]); p = be32dec(&kdf[12]); memcpy(salt, &kdf[16], 32); /* Verify hash checksum. */ SHA256_Init(&ctx); SHA256_Update(&ctx, kdf, 48); SHA256_Final(hbuf, &ctx); if (memcmp(&kdf[48], hbuf, 16)) return (7); /* Compute Derived Key */ if (ScryptHashFunction(passwd, passwdSize, salt, 32, N, r, p, dk, 64)) return (3); /* Check hash signature (i.e., verify password). */ HMAC_SHA256_Init(&hctx, key_hmac, 32); HMAC_SHA256_Update(&hctx, kdf, 64); HMAC_SHA256_Final(hbuf, &hctx); if (memcmp(hbuf, &kdf[64], 32)) return (11); return (0); //Success }
static void le_lebuffer_copytodesc(struct lance_softc *sc, void *fromv, int off, int len) { struct le_lebuffer_softc *lesc = (struct le_lebuffer_softc *)sc; caddr_t from = fromv; for (; len >= 8; len -= 8, off += 8, from += 8) bus_write_8(lesc->sc_bres, off, be64dec(from)); for (; len >= 4; len -= 4, off += 4, from += 4) bus_write_4(lesc->sc_bres, off, be32dec(from)); for (; len >= 2; len -= 2, off += 2, from += 2) bus_write_2(lesc->sc_bres, off, be16dec(from)); if (len == 1) bus_write_1(lesc->sc_bres, off, *from); }
int main(void) { static const char *pcap_files[] = { "../../sample/icmp.pcap", "../../sample/tcp.pcap", "../../sample/udp.pcap", "../../sample/tcp6.pcap", "../../sample/udp6.pcap", }; pout("peak packet test suite... "); /* make sure this is always aligned to 8 bytes */ assert(!(sizeof(struct peak_packet) % sizeof(uint64_t))); memset(&net_saddr4, 0, sizeof(net_saddr4)); memset(&net_daddr4, 0, sizeof(net_daddr4)); memset(&net_saddr6, 0, sizeof(net_saddr6)); memset(&net_daddr6, 0, sizeof(net_daddr6)); netaddr4(&net_saddr4, be32dec(&src_ip4)); netaddr4(&net_daddr4, be32dec(&dst_ip4)); netaddr6(&net_saddr6, src_ip6); netaddr6(&net_daddr6, dst_ip6); test_icmp(pcap_files[0]); test_transport_ip4(pcap_files[1]); /* TCP over IPv4 */ test_transport_ip4(pcap_files[2]); /* UDP over IPv4 */ test_transport_ip6(pcap_files[3]); /* TCP over IPv6 */ test_transport_ip6(pcap_files[4]); /* UDP over IPv6 */ pout("ok\n"); return (0); }
// hash exactly 64 bytes (ie, sha256 block size) static void sha256_hash512(uint32_t *hash, const uint32_t *data) { uint32_t _ALIGN(64) S[16]; uint32_t _ALIGN(64) T[64]; uchar _ALIGN(64) E[64*4] = { 0 }; int i; sha256_init(S); for (i = 0; i < 16; i++) T[i] = be32dec(&data[i]); sha256_transform_volatile(S, T); E[3] = 0x80; E[61] = 0x02; // T[15] = 8 * 64 => 0x200; sha256_transform_volatile(S, (uint32_t*)E); for (i = 0; i < 8; i++) be32enc(&hash[i], S[i]); }
static void sha256_hash(unsigned char *hash, const unsigned char *data, int len) { uint32_t _ALIGN(64) S[16]; uint32_t _ALIGN(64) T[64]; int i, r; sha256_init(S); for (r = len; r > -9; r -= 64) { if (r < 64) memset(T, 0, 64); memcpy(T, data + len - r, r > 64 ? 64 : (r < 0 ? 0 : r)); if (r >= 0 && r < 64) ((unsigned char *)T)[r] = 0x80; for (i = 0; i < 16; i++) T[i] = be32dec(T + i); if (r < 56) T[15] = 8 * len; //sha256_transform(S, T, 0); sha256_transform_volatile(S, T); } for (i = 0; i < 8; i++) be32enc((uint32_t *)hash + i, S[i]); }
void hodl_build_block_header( struct work* g_work, uint32_t version, uint32_t *prevhash, uint32_t *merkle_tree, uint32_t ntime, uint32_t nbits ) { int i; memset( g_work->data, 0, sizeof(g_work->data) ); g_work->data[0] = version; if ( have_stratum ) for ( i = 0; i < 8; i++ ) g_work->data[ 1+i ] = le32dec( prevhash + i ); else for (i = 0; i < 8; i++) g_work->data[ 8-i ] = le32dec( prevhash + i ); for ( i = 0; i < 8; i++ ) g_work->data[ 9+i ] = be32dec( merkle_tree + i ); g_work->data[ algo_gate.ntime_index ] = ntime; g_work->data[ algo_gate.nbits_index ] = nbits; g_work->data[22] = 0x80000000; g_work->data[31] = 0x00000280; }
/** * proto_crypt_dec(ibuf, obuf, k): * Decrypt PCRYPT_ESZ bytes from ${ibuf} using the keys in ${k}. If the data * is valid, write it into ${obuf} and return the length; otherwise, return * -1. */ ssize_t proto_crypt_dec(uint8_t ibuf[PCRYPT_ESZ], uint8_t * obuf, struct proto_keys * k) { HMAC_SHA256_CTX ctx; uint8_t hbuf[32]; uint8_t pnum_exp[8]; size_t len; /* Verify HMAC. */ be64enc(pnum_exp, k->pnum); HMAC_SHA256_Init(&ctx, k->k_hmac, 32); HMAC_SHA256_Update(&ctx, ibuf, PCRYPT_MAXDSZ + 4); HMAC_SHA256_Update(&ctx, pnum_exp, 8); HMAC_SHA256_Final(hbuf, &ctx); if (crypto_verify_bytes(hbuf, &ibuf[PCRYPT_MAXDSZ + 4], 32)) return (-1); /* Decrypt the buffer in-place. */ crypto_aesctr_buf(k->k_aes, k->pnum, ibuf, ibuf, PCRYPT_MAXDSZ + 4); /* Increment packet number. */ k->pnum += 1; /* Parse length. */ len = be32dec(&ibuf[PCRYPT_MAXDSZ]); /* Make sure nobody is being evil here... */ if ((len == 0) || (len > PCRYPT_MAXDSZ)) return (-1); /* Copy the bytes into the output buffer. */ memcpy(obuf, ibuf, len); /* Return the decrypted length. */ return (len); }
int pkg_get_myarch(char *dest, size_t sz) { Elf *elf = NULL; GElf_Ehdr elfhdr; GElf_Shdr shdr; Elf_Data *data; Elf_Note note; Elf_Scn *scn = NULL; int fd; char *src = NULL; char *osname; uint32_t version = 0; int ret = EPKG_OK; int i; const char *arch, *abi, *endian_corres_str, *wordsize_corres_str, *fpu; if (elf_version(EV_CURRENT) == EV_NONE) { pkg_emit_error("ELF library initialization failed: %s", elf_errmsg(-1)); return (EPKG_FATAL); } if ((fd = open(_PATH_BSHELL, O_RDONLY)) < 0) { pkg_emit_errno("open", _PATH_BSHELL); snprintf(dest, sz, "%s", "unknown"); return (EPKG_FATAL); } if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) { ret = EPKG_FATAL; pkg_emit_error("elf_begin() failed: %s.", elf_errmsg(-1)); goto cleanup; } if (gelf_getehdr(elf, &elfhdr) == NULL) { ret = EPKG_FATAL; pkg_emit_error("getehdr() failed: %s.", elf_errmsg(-1)); goto cleanup; } while ((scn = elf_nextscn(elf, scn)) != NULL) { if (gelf_getshdr(scn, &shdr) != &shdr) { ret = EPKG_FATAL; pkg_emit_error("getshdr() failed: %s.", elf_errmsg(-1)); goto cleanup; } if (shdr.sh_type == SHT_NOTE) break; } if (scn == NULL) { ret = EPKG_FATAL; pkg_emit_error("failed to get the note section"); goto cleanup; } data = elf_getdata(scn, NULL); src = data->d_buf; while ((uintptr_t)src < ((uintptr_t)data->d_buf + data->d_size)) { memcpy(¬e, src, sizeof(Elf_Note)); src += sizeof(Elf_Note); if (note.n_type == NT_VERSION) break; src += note.n_namesz + note.n_descsz; } if ((uintptr_t)src >= ((uintptr_t)data->d_buf + data->d_size)) { ret = EPKG_FATAL; pkg_emit_error("failed to find the version elf note"); goto cleanup; } osname = src; src += roundup2(note.n_namesz, 4); if (elfhdr.e_ident[EI_DATA] == ELFDATA2MSB) version = be32dec(src); else version = le32dec(src); for (i = 0; osname[i] != '\0'; i++) osname[i] = (char)tolower(osname[i]); wordsize_corres_str = elf_corres_to_string(wordsize_corres, (int)elfhdr.e_ident[EI_CLASS]); arch = elf_corres_to_string(mach_corres, (int) elfhdr.e_machine); #if defined(__DragonFly__) snprintf(dest, sz, "%s:%d.%d", osname, version / 100000, (((version / 100 % 1000)+1)/2)*2); #else snprintf(dest, sz, "%s:%d", osname, version / 100000); #endif switch (elfhdr.e_machine) { case EM_ARM: endian_corres_str = elf_corres_to_string(endian_corres, (int)elfhdr.e_ident[EI_DATA]); /* FreeBSD doesn't support the hard-float ABI yet */ fpu = "softfp"; if ((elfhdr.e_flags & 0xFF000000) != 0) { const char *sh_name = NULL; size_t shstrndx; /* This is an EABI file, the conformance level is set */ abi = "eabi"; /* Find which TARGET_ARCH we are building for. */ elf_getshdrstrndx(elf, &shstrndx); while ((scn = elf_nextscn(elf, scn)) != NULL) { sh_name = NULL; if (gelf_getshdr(scn, &shdr) != &shdr) { scn = NULL; break; } sh_name = elf_strptr(elf, shstrndx, shdr.sh_name); if (sh_name == NULL) continue; if (strcmp(".ARM.attributes", sh_name) == 0) break; } if (scn != NULL && sh_name != NULL) { data = elf_getdata(scn, NULL); /* * Prior to FreeBSD 10.0 libelf would return * NULL from elf_getdata on the .ARM.attributes * section. As this was the first release to * get armv6 support assume a NULL value means * arm. * * This assumption can be removed when 9.x * is unsupported. */ if (data != NULL) { arch = aeabi_parse_arm_attributes( data->d_buf, data->d_size); if (arch == NULL) { ret = EPKG_FATAL; pkg_emit_error( "unknown ARM ARCH"); goto cleanup; } } } else { ret = EPKG_FATAL; pkg_emit_error("Unable to find the " ".ARM.attributes section"); goto cleanup; } } else if (elfhdr.e_ident[EI_OSABI] != ELFOSABI_NONE) { /* * EABI executables all have this field set to * ELFOSABI_NONE, therefore it must be an oabi file. */ abi = "oabi"; } else { /* * We may have failed to positively detect the ABI, * set the ABI to unknown. If we end up here one of * the above cases should be fixed for the binary. */ ret = EPKG_FATAL; pkg_emit_error("unknown ARM ABI"); goto cleanup; } snprintf(dest + strlen(dest), sz - strlen(dest), ":%s:%s:%s:%s:%s", arch, wordsize_corres_str, endian_corres_str, abi, fpu); break; case EM_MIPS: /* * this is taken from binutils sources: * include/elf/mips.h * mapping is figured out from binutils: * gas/config/tc-mips.c */ switch (elfhdr.e_flags & EF_MIPS_ABI) { case E_MIPS_ABI_O32: abi = "o32"; break; case E_MIPS_ABI_N32: abi = "n32"; break; default: if (elfhdr.e_ident[EI_DATA] == ELFCLASS32) abi = "o32"; else if (elfhdr.e_ident[EI_DATA] == ELFCLASS64) abi = "n64"; else abi = "unknown"; break; } endian_corres_str = elf_corres_to_string(endian_corres, (int)elfhdr.e_ident[EI_DATA]); snprintf(dest + strlen(dest), sz - strlen(dest), ":%s:%s:%s:%s", arch, wordsize_corres_str, endian_corres_str, abi); break; default: snprintf(dest + strlen(dest), sz - strlen(dest), ":%s:%s", arch, wordsize_corres_str); break; } cleanup: if (elf != NULL) elf_end(elf); close(fd); return (ret); }
void fifolog_reader_process(struct fifolog_reader *fr, off_t from, fifolog_reader_render_t *func, void *priv, time_t end) { uint32_t seq, lseq; off_t o = from; int i, e; time_t t; u_char *p, *q; z_stream *zs; CHECK_OBJ_NOTNULL(fr, FIFOLOG_READER_MAGIC); zs = fr->ff->zs; lseq = 0; while (1) { e = fifolog_int_read(fr->ff, o); if (e) err(1, "Read error (%d)", e); if (++o >= fr->ff->logsize) o = 0; seq = be32dec(fr->ff->recbuf); if (lseq != 0 && seq != lseq + 1) break; lseq = seq; zs->avail_in = fr->ff->recsize - 5; zs->next_in = fr->ff->recbuf + 5; if (fr->ff->recbuf[4] & FIFOLOG_FLG_1BYTE) zs->avail_in -= fr->ff->recbuf[fr->ff->recsize - 1]; if (fr->ff->recbuf[4] & FIFOLOG_FLG_4BYTE) zs->avail_in -= be32dec(fr->ff->recbuf + fr->ff->recsize - 4); if (fr->ff->recbuf[4] & FIFOLOG_FLG_SYNC) { i = inflateReset(zs); assert(i == Z_OK); zs->next_out = fr->obuf; zs->avail_out = fr->olen; t = be32dec(fr->ff->recbuf + 5); if (t > end) break; zs->next_in += 4; zs->avail_in -= 4; } while(zs->avail_in > 0) { i = inflate(zs, 0); if (i == Z_BUF_ERROR) { #if 1 fprintf(stderr, "Z_BUF_ERROR [%d,%d] [%d,%d,%d]\n", (int)(zs->next_in - fr->ff->recbuf), zs->avail_in, (int)(zs->next_out - fr->obuf), zs->avail_out, fr->olen); exit (250); #else i = Z_OK; #endif } if (i == Z_STREAM_END) { i = inflateReset(zs); } if (i != Z_OK) { fprintf(stderr, "inflate = %d\n", i); exit (250); } assert(i == Z_OK); if (zs->avail_out != fr->olen) { q = fr->obuf + (fr->olen - zs->avail_out); p = fifolog_reader_chop(fr, func, priv); if (p < q) (void)memmove(fr->obuf, p, q - p); zs->avail_out = fr->olen - (q - p); zs->next_out = fr->obuf + (q - p); } } } }
static int pkg_get_myabi(char *dest, size_t sz) { Elf *elf; Elf_Data *data; Elf_Note note; Elf_Scn *scn; char *src, *osname; const char *abi; GElf_Ehdr elfhdr; GElf_Shdr shdr; int fd, i, ret; uint32_t version; version = 0; ret = -1; scn = NULL; abi = NULL; if (elf_version(EV_CURRENT) == EV_NONE) { warnx("ELF library initialization failed: %s", elf_errmsg(-1)); return (-1); } if ((fd = open("/bin/sh", O_RDONLY)) < 0) { warn("open()"); return (-1); } if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) { ret = -1; warnx("elf_begin() failed: %s.", elf_errmsg(-1)); goto cleanup; } if (gelf_getehdr(elf, &elfhdr) == NULL) { ret = -1; warn("getehdr() failed: %s.", elf_errmsg(-1)); goto cleanup; } while ((scn = elf_nextscn(elf, scn)) != NULL) { if (gelf_getshdr(scn, &shdr) != &shdr) { ret = -1; warn("getshdr() failed: %s.", elf_errmsg(-1)); goto cleanup; } if (shdr.sh_type == SHT_NOTE) break; } if (scn == NULL) { ret = -1; warn("failed to get the note section"); goto cleanup; } data = elf_getdata(scn, NULL); src = data->d_buf; for (;;) { memcpy(¬e, src, sizeof(Elf_Note)); src += sizeof(Elf_Note); if (note.n_type == NT_VERSION) break; src += note.n_namesz + note.n_descsz; } osname = src; src += note.n_namesz; if (elfhdr.e_ident[EI_DATA] == ELFDATA2MSB) version = be32dec(src); else version = le32dec(src); for (i = 0; osname[i] != '\0'; i++) osname[i] = (char)tolower(osname[i]); snprintf(dest, sz, "%s:%d:%s:%s", osname, version / 100000, elf_corres_to_string(mach_corres, (int)elfhdr.e_machine), elf_corres_to_string(wordsize_corres, (int)elfhdr.e_ident[EI_CLASS])); ret = 0; switch (elfhdr.e_machine) { case EM_ARM: snprintf(dest + strlen(dest), sz - strlen(dest), ":%s:%s:%s", elf_corres_to_string(endian_corres, (int)elfhdr.e_ident[EI_DATA]), (elfhdr.e_flags & EF_ARM_NEW_ABI) > 0 ? "eabi" : "oabi", (elfhdr.e_flags & EF_ARM_VFP_FLOAT) > 0 ? "softfp" : "vfp"); break; case EM_MIPS: /* * this is taken from binutils sources: * include/elf/mips.h * mapping is figured out from binutils: * gas/config/tc-mips.c */ switch (elfhdr.e_flags & EF_MIPS_ABI) { case E_MIPS_ABI_O32: abi = "o32"; break; case E_MIPS_ABI_N32: abi = "n32"; break; default: if (elfhdr.e_ident[EI_DATA] == ELFCLASS32) abi = "o32"; else if (elfhdr.e_ident[EI_DATA] == ELFCLASS64) abi = "n64"; break; } snprintf(dest + strlen(dest), sz - strlen(dest), ":%s:%s", elf_corres_to_string(endian_corres, (int)elfhdr.e_ident[EI_DATA]), abi); break; } cleanup: if (elf != NULL) elf_end(elf); close(fd); return (ret); }
off_t fifolog_reader_seek(const struct fifolog_reader *fr, time_t t0) { off_t o, s, st; time_t t, tt; unsigned seq, seqs; const char *retval; int e; CHECK_OBJ_NOTNULL(fr, FIFOLOG_READER_MAGIC); /* * First, find the first SYNC block */ o = 0; e = fifolog_reader_findsync(fr->ff, &o); if (e == 0) return (0); /* empty fifolog */ assert(e == 1); assert(fr->ff->recbuf[4] & FIFOLOG_FLG_SYNC); seq = be32dec(fr->ff->recbuf); t = be32dec(fr->ff->recbuf + 5); if (t > t0) { /* Check if there is a second older part we can use */ retval = fifolog_int_findend(fr->ff, &s); if (retval != NULL) err(1, "%s", retval); s++; e = fifolog_reader_findsync(fr->ff, &s); if (e == 0) return (0); /* empty fifolog */ if (e == 1) { o = s; seq = be32dec(fr->ff->recbuf); t = be32dec(fr->ff->recbuf + 5); } } /* Now do a binary search to find the sync block right before t0 */ s = st = (fr->ff->logsize - o) / 2; while (s > 1) { /* We know we shouldn't wrap */ if (o + st > fr->ff->logsize + 1) { s = st = s / 2; continue; } e = fifolog_int_read(fr->ff, o + st); if (e) { s = st = s / 2; continue; } /* If not in same part, sequence won't match */ seqs = be32dec(fr->ff->recbuf); if (seqs != seq + st) { s = st = s / 2; continue; } /* If not sync block, try next */ if (!(fr->ff->recbuf[4] & FIFOLOG_FLG_SYNC)) { st++; continue; } /* Check timestamp */ tt = be32dec(fr->ff->recbuf + 5); if (tt >= t0) { s = st = s / 2; continue; } o += st; seq = seqs; } fprintf(stderr, "Read from %jx\n", o * fr->ff->recsize); return (o); }