int t2_getnumpasses() { int n; if (!bio_read(1)) return 1; if (!bio_read(1)) return 2; if ((n=bio_read(2))!=3) return 3+n; if ((n=bio_read(5))!=31) return 6+n; return 37+bio_read(7); }
static int t2_getnumpasses(opj_bio_t *bio) { int n; if (!bio_read(bio, 1)) return 1; if (!bio_read(bio, 1)) return 2; if ((n = bio_read(bio, 2)) != 3) return (3 + n); if ((n = bio_read(bio, 5)) != 31) return (6 + n); return (37 + bio_read(bio, 7)); }
static int t2_getcommacode(opj_bio_t *bio) { int n; for (n = 0; bio_read(bio, 1); n++) { ; } return n; }
/* WARNING: The non-copying interface is largely untested as of yet * and may contain bugs. */ static ossl_ssize_t bio_nread0(BIO *bio, char **buf) { struct bio_bio_st *b, *peer_b; ossl_ssize_t num; BIO_clear_retry_flags(bio); if (!bio->init) return 0; b = bio->ptr; assert(b != NULL); assert(b->peer != NULL); peer_b = b->peer->ptr; assert(peer_b != NULL); assert(peer_b->buf != NULL); peer_b->request = 0; if (peer_b->len == 0) { char dummy; /* avoid code duplication -- nothing available for reading */ return bio_read(bio, &dummy, 1); /* returns 0 or -1 */ } num = peer_b->len; if (
static S32 t2_getcommacode(opj_bio_t *bio) { S32 n; for (n = 0; bio_read(bio, 1); n++) { ; } return n; }
/* * filesystem operations */ static s32_t cpiofs_mount(struct mount * m, char * dev, s32_t flag) { struct blkdev * blk; struct cpio_newc_header header; if(dev == NULL) return EINVAL; blk = (struct blkdev *)m->m_dev; if(!blk) return EACCES; if(get_blkdev_total_size(blk) <= sizeof(struct cpio_newc_header)) return EINTR; if(bio_read(blk, (u8_t *)(&header), 0, sizeof(struct cpio_newc_header)) != sizeof(struct cpio_newc_header)) return EIO; if(strncmp((const char *)(header.c_magic), (const char *)"070701", 6) != 0) return EINVAL; m->m_flags = (flag & MOUNT_MASK) | MOUNT_RDONLY; m->m_root->v_data = 0; m->m_data = NULL; return 0; }
static s32_t cpiofs_read(struct vnode * node, struct file * fp, void * buf, loff_t size, loff_t * result) { struct blkdev * dev = (struct blkdev *)node->v_mount->m_dev; loff_t off; loff_t len; *result = 0; if(node->v_type == VDIR) return EISDIR; if(node->v_type != VREG) return EINVAL; if(fp->f_offset >= node->v_size) return 0; if(node->v_size - fp->f_offset < size) size = node->v_size - fp->f_offset; off = (loff_t)((s32_t)(node->v_data)); len = bio_read(dev, (u8_t *)buf, (off + fp->f_offset), size); fp->f_offset += len; *result = len; return 0; }
static s32_t devfs_read(struct vnode * node, struct file * fp, void * buf, loff_t size, loff_t * result) { struct device * dev = (struct device *)(node->v_data); struct chrdev * chr; struct blkdev * blk; loff_t len; *result = 0; if(node->v_type == VDIR) return EISDIR; if(node->v_type == VCHR) { chr = (struct chrdev *)(dev->priv); len = chr->read(chr, buf, size); fp->f_offset = 0; *result = len; return 0; } else if(node->v_type == VBLK) { blk = (struct blkdev *)(dev->priv); len = bio_read(blk, buf, fp->f_offset, size); fp->f_offset += len; *result = len; return 0; } return -1; }
/* * Generic VFS read function for filesystems that provide bio_vec block lists * to the VFS. */ ssize_t bio_file_read(struct file *file, char *buf, size_t len, unsigned long *pos) { if (*pos > file->f_inode->i_size) return 0; len = MIN(len, file->f_inode->i_size - *pos); return bio_read(file->f_inode->i_bio, buf, len, pos); }
static OPJ_UINT32 t2_getcommacode(opj_bio_t *bio) { OPJ_UINT32 n = 0; while (bio_read(bio, 1)) { ++n; } return n; }
int key_state_read_plaintext (struct key_state_ssl *ks_ssl, struct buffer *buf, int maxlen) { int ret = 0; perf_push (PERF_BIO_READ_PLAINTEXT); ASSERT (NULL != ks_ssl); ret = bio_read (ks_ssl->ssl_bio, buf, maxlen, "tls_read_plaintext"); perf_pop (); return ret; }
int key_state_read_ciphertext (struct key_state_ssl *ks_ssl, struct buffer *buf, int maxlen) { int ret = 0; perf_push (PERF_BIO_READ_CIPHERTEXT); ASSERT (NULL != ks_ssl); ret = bio_read (ks_ssl->ct_out, buf, maxlen, "tls_read_ciphertext"); perf_pop (); return ret; }
/* WARNING: The non-copying interface is largely untested as of yet * and may contain bugs. */ static ossl_ssize_t bio_nread0(BIO *bio, char **buf) { struct bio_bio_st *b, *peer_b; ossl_ssize_t num; BIO_clear_retry_flags(bio); if (!bio->init) return 0; b = bio->ptr; assert(b != NULL); assert(b->peer != NULL); peer_b = b->peer->ptr; assert(peer_b != NULL); assert(peer_b->buf != NULL); peer_b->request = 0; if (peer_b->len == 0) { char dummy; /* avoid code duplication -- nothing available for reading */ return bio_read(bio, &dummy, 1); /* returns 0 or -1 */ } num = peer_b->len; if (peer_b->size < peer_b->offset + num) /* no ring buffer wrap-around for non-copying interface */ num = peer_b->size - peer_b->offset; assert(num > 0); if (buf != NULL) *buf = peer_b->buf + peer_b->offset; return num; }
/* </summary> */ int tgt_decode(tgt_tree_t * tree, int leafno, int threshold) { tgt_node_t *stk[31]; tgt_node_t **stkptr; tgt_node_t *node; int low; stkptr = stk; node = &tree->nodes[leafno]; while (node->parent) { *stkptr++ = node; node = node->parent; } low = 0; for (;;) { if (low > node->low) { node->low = low; } else { low = node->low; } while (low < threshold && low < node->value) { if (bio_read(1)) { node->value = low; } else { ++low; } } node->low = low; if (stkptr == stk) { break; } node = *--stkptr; } return (node->value < threshold) ? 1 : 0; }
S32 tgt_decode(opj_bio_t *bio, opj_tgt_tree_t *tree, S32 leafno, S32 threshold) { opj_tgt_node_t *stk[31]; opj_tgt_node_t **stkptr; opj_tgt_node_t *node; S32 low; stkptr = stk; node = &tree->nodes[leafno]; while (node->parent) { *stkptr++ = node; node = node->parent; } low = 0; for (;;) { if (low > node->low) { node->low = low; } else { low = node->low; } while (low < threshold && low < node->value) { if (bio_read(bio, 1)) { node->value = low; } else { ++low; } } node->low = low; if (stkptr == stk) { break; } node = *--stkptr; } return (node->value < threshold) ? 1 : 0; }
static int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_tile_t *tile, opj_tcp_t *tcp, opj_pi_iterator_t *pi, opj_packet_info_t *pack_info) { int bandno, cblkno; unsigned char *c = src; opj_cp_t *cp = t2->cp; int compno = pi->compno; /* component value */ int resno = pi->resno; /* resolution level value */ int precno = pi->precno; /* precinct value */ int layno = pi->layno; /* quality layer value */ opj_tcd_resolution_t* res = &tile->comps[compno].resolutions[resno]; unsigned char *hd = NULL; int present; opj_bio_t *bio = NULL; /* BIO component */ if (layno == 0) { for (bandno = 0; bandno < res->numbands; bandno++) { opj_tcd_band_t *band = &res->bands[bandno]; opj_tcd_precinct_t *prc = &band->precincts[precno]; if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue; tgt_reset(prc->incltree); tgt_reset(prc->imsbtree); for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno]; cblk->numsegs = 0; } } } /* SOP markers */ if (tcp->csty & J2K_CP_CSTY_SOP) { if ((*c) != 0xff || (*(c + 1) != 0x91)) { opj_event_msg(t2->cinfo, EVT_WARNING, "Expected SOP marker\n"); } else { c += 6; } /** TODO : check the Nsop value */ } /* When the marker PPT/PPM is used the packet header are store in PPT/PPM marker This part deal with this caracteristic step 1: Read packet header in the saved structure step 2: Return to codestream for decoding */ bio = bio_create(); if (cp->ppm == 1) { /* PPM */ hd = cp->ppm_data; bio_init_dec(bio, hd, cp->ppm_len); } else if (tcp->ppt == 1) { /* PPT */ hd = tcp->ppt_data; bio_init_dec(bio, hd, tcp->ppt_len); } else { /* Normal Case */ hd = c; bio_init_dec(bio, hd, src+len-hd); } present = bio_read(bio, 1); if (!present) { bio_inalign(bio); hd += bio_numbytes(bio); bio_destroy(bio); /* EPH markers */ if (tcp->csty & J2K_CP_CSTY_EPH) { if ((*hd) != 0xff || (*(hd + 1) != 0x92)) { printf("Error : expected EPH marker\n"); } else { hd += 2; } } /* << INDEX */ /* End of packet header position. Currently only represents the distance to start of packet // Will be updated later by incrementing with packet start value*/ if(pack_info) { pack_info->end_ph_pos = (int)(c - src); } /* INDEX >> */ if (cp->ppm == 1) { /* PPM case */ cp->ppm_len += cp->ppm_data-hd; cp->ppm_data = hd; return (c - src); } if (tcp->ppt == 1) { /* PPT case */ tcp->ppt_len+=tcp->ppt_data-hd; tcp->ppt_data = hd; return (c - src); } return (hd - src); } for (bandno = 0; bandno < res->numbands; bandno++) { opj_tcd_band_t *band = &res->bands[bandno]; opj_tcd_precinct_t *prc = &band->precincts[precno]; if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue; for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { int included, increment, n, segno; opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno]; /* if cblk not yet included before --> inclusion tagtree */ if (!cblk->numsegs) { included = tgt_decode(bio, prc->incltree, cblkno, layno + 1); /* else one bit */ } else { included = bio_read(bio, 1); } /* if cblk not included */ if (!included) { cblk->numnewpasses = 0; continue; } /* if cblk not yet included --> zero-bitplane tagtree */ if (!cblk->numsegs) { int i, numimsbs; for (i = 0; !tgt_decode(bio, prc->imsbtree, cblkno, i); i++) { ; } numimsbs = i - 1; cblk->numbps = band->numbps - numimsbs; cblk->numlenbits = 3; } /* number of coding passes */ cblk->numnewpasses = t2_getnumpasses(bio); increment = t2_getcommacode(bio); /* length indicator increment */ cblk->numlenbits += increment; segno = 0; if (!cblk->numsegs) { t2_init_seg(cblk, segno, tcp->tccps[compno].cblksty, 1); } else { segno = cblk->numsegs - 1; if (cblk->segs[segno].numpasses == cblk->segs[segno].maxpasses) { ++segno; t2_init_seg(cblk, segno, tcp->tccps[compno].cblksty, 0); } } n = cblk->numnewpasses; do { cblk->segs[segno].numnewpasses = int_min(cblk->segs[segno].maxpasses - cblk->segs[segno].numpasses, n); cblk->segs[segno].newlen = bio_read(bio, cblk->numlenbits + int_floorlog2(cblk->segs[segno].numnewpasses)); n -= cblk->segs[segno].numnewpasses; if (n > 0) { ++segno; t2_init_seg(cblk, segno, tcp->tccps[compno].cblksty, 0); } } while (n > 0); } } if (bio_inalign(bio)) { bio_destroy(bio); return -999; } hd += bio_numbytes(bio); bio_destroy(bio); /* EPH markers */ if (tcp->csty & J2K_CP_CSTY_EPH) { if ((*hd) != 0xff || (*(hd + 1) != 0x92)) { opj_event_msg(t2->cinfo, EVT_ERROR, "Expected EPH marker\n"); return -999; } else { hd += 2; } } /* << INDEX */ /* End of packet header position. Currently only represents the distance to start of packet // Will be updated later by incrementing with packet start value*/ if(pack_info) { pack_info->end_ph_pos = (int)(hd - src); } /* INDEX >> */ if (cp->ppm==1) { cp->ppm_len+=cp->ppm_data-hd; cp->ppm_data = hd; } else if (tcp->ppt == 1) { tcp->ppt_len+=tcp->ppt_data-hd; tcp->ppt_data = hd; } else { c=hd; } for (bandno = 0; bandno < res->numbands; bandno++) { opj_tcd_band_t *band = &res->bands[bandno]; opj_tcd_precinct_t *prc = &band->precincts[precno]; if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue; for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) { opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno]; opj_tcd_seg_t *seg = NULL; if (!cblk->numnewpasses) continue; if (!cblk->numsegs) { seg = &cblk->segs[0]; cblk->numsegs++; cblk->len = 0; } else { seg = &cblk->segs[cblk->numsegs - 1]; if (seg->numpasses == seg->maxpasses) { seg++; cblk->numsegs++; } } do { if (c + seg->newlen > src + len) { return -999; } #ifdef USE_JPWL /* we need here a j2k handle to verify if making a check to the validity of cblocks parameters is selected from user (-W) */ /* let's check that we are not exceeding */ if ((cblk->len + seg->newlen) > 8192) { opj_event_msg(t2->cinfo, EVT_WARNING, "JPWL: segment too long (%d) for codeblock %d (p=%d, b=%d, r=%d, c=%d)\n", seg->newlen, cblkno, precno, bandno, resno, compno); if (!JPWL_ASSUME) { opj_event_msg(t2->cinfo, EVT_ERROR, "JPWL: giving up\n"); return -999; } seg->newlen = 8192 - cblk->len; opj_event_msg(t2->cinfo, EVT_WARNING, " - truncating segment to %d\n", seg->newlen); break; }; #endif /* USE_JPWL */ cblk->data = (unsigned char*) opj_realloc(cblk->data, (cblk->len + seg->newlen) * sizeof(unsigned char)); memcpy(cblk->data + cblk->len, c, seg->newlen); if (seg->numpasses == 0) { seg->data = &cblk->data; seg->dataindex = cblk->len; } c += seg->newlen; cblk->len += seg->newlen; seg->len += seg->newlen; seg->numpasses += seg->numnewpasses; cblk->numnewpasses -= seg->numnewpasses; if (cblk->numnewpasses > 0) { seg++; cblk->numsegs++; } } while (cblk->numnewpasses > 0); } } return (c - src); }
static s32_t cpiofs_readdir(struct vnode * node, struct file * fp, struct dirent * dir) { struct blkdev * dev = (struct blkdev *)node->v_mount->m_dev; struct cpio_newc_header header; char path[MAX_PATH]; char name[MAX_NAME]; u32_t size, name_size, mode; loff_t off = 0; char buf[9]; s32_t i = 0; if(fp->f_offset == 0) { dir->d_type = DT_DIR; strlcpy((char *)&dir->d_name, (const char *)".", sizeof(dir->d_name)); } else if(fp->f_offset == 1) { dir->d_type = DT_DIR; strlcpy((char *)&dir->d_name, (const char *)"..", sizeof(dir->d_name)); } else { while(1) { bio_read(dev, (u8_t *)(&header), off, sizeof(struct cpio_newc_header)); if(strncmp((const char *)(header.c_magic), (const char *)"070701", 6) != 0) return ENOENT; buf[8] = '\0'; memcpy(buf, (const s8_t *)(header.c_filesize), 8); size = strtoul((const char *)buf, NULL, 16); memcpy(buf, (const s8_t *)(header.c_namesize), 8); name_size = strtoul((const char *)buf, NULL, 16); memcpy(buf, (const s8_t *)(header.c_mode), 8); mode = strtoul((const char *)buf, NULL, 16); bio_read(dev, (u8_t *)path, off + sizeof(struct cpio_newc_header), (loff_t)name_size); if( (size == 0) && (mode == 0) && (name_size == 11) && (strncmp(path, (const char *)"TRAILER!!!", 10) == 0) ) return ENOENT; off = off + sizeof(struct cpio_newc_header) + (((name_size + 1) & ~3) + 2) + size; off = (off + 3) & ~3; if(!get_next_token(path, (const char *)node->v_path, name)) continue; if(i++ == fp->f_offset - 2) { off = 0; break; } } if(mode & 0040000) dir->d_type = DT_DIR; else dir->d_type = DT_REG; strlcpy((char *)&dir->d_name, name, sizeof(name)); } dir->d_fileno = (u32_t)fp->f_offset; dir->d_namlen = (u16_t)strlen((const char *)dir->d_name); fp->f_offset++; return 0; }
static bool t2_read_packet_header( opj_t2_t* p_t2, opj_tcd_tile_t *p_tile, opj_tcp_t *p_tcp, opj_pi_iterator_t *p_pi, bool * p_is_data_present, OPJ_BYTE *p_src_data, OPJ_UINT32 * p_data_read, OPJ_UINT32 p_max_length, opj_packet_info_t *p_pack_info) { /* loop */ OPJ_UINT32 bandno, cblkno; OPJ_UINT32 l_nb_code_blocks; OPJ_UINT32 l_remaining_length; OPJ_UINT32 l_header_length; OPJ_UINT32 * l_modified_length_ptr = 00; OPJ_BYTE *l_current_data = p_src_data; opj_cp_t *l_cp = p_t2->cp; opj_bio_t *l_bio = 00; /* BIO component */ opj_tcd_band_t *l_band = 00; opj_tcd_cblk_dec_t* l_cblk = 00; opj_tcd_resolution_t* l_res = &p_tile->comps[p_pi->compno].resolutions[p_pi->resno]; OPJ_BYTE *l_header_data = 00; OPJ_BYTE **l_header_data_start = 00; OPJ_UINT32 l_present; if (p_pi->layno == 0) { l_band = l_res->bands; /* reset tagtrees */ for (bandno = 0; bandno < l_res->numbands; ++bandno) { opj_tcd_precinct_t *l_prc = &l_band->precincts[p_pi->precno]; if ( ! ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0))) { tgt_reset(l_prc->incltree); tgt_reset(l_prc->imsbtree); l_cblk = l_prc->cblks.dec; l_nb_code_blocks = l_prc->cw * l_prc->ch; for (cblkno = 0; cblkno < l_nb_code_blocks; ++cblkno) { l_cblk->numsegs = 0; l_cblk->real_num_segs = 0; ++l_cblk; } } ++l_band; } } /* SOP markers */ if (p_tcp->csty & J2K_CP_CSTY_SOP) { if ((*l_current_data) != 0xff || (*(l_current_data + 1) != 0x91)) { // TODO opj_event_msg(t2->cinfo->event_mgr, EVT_WARNING, "Expected SOP marker\n"); } else { l_current_data += 6; } /** TODO : check the Nsop value */ } /* When the marker PPT/PPM is used the packet header are store in PPT/PPM marker This part deal with this caracteristic step 1: Read packet header in the saved structure step 2: Return to codestream for decoding */ l_bio = bio_create(); if (! l_bio) { return false; } if (l_cp->ppm == 1) { /* PPM */ l_header_data_start = &l_cp->ppm_data; l_header_data = *l_header_data_start; l_modified_length_ptr = &(l_cp->ppm_len); } else if (p_tcp->ppt == 1) { /* PPT */ l_header_data_start = &(p_tcp->ppt_data); l_header_data = *l_header_data_start; l_modified_length_ptr = &(p_tcp->ppt_len); } else { /* Normal Case */ l_header_data_start = &(l_current_data); l_header_data = *l_header_data_start; l_remaining_length = p_src_data+p_max_length-l_header_data; l_modified_length_ptr = &(l_remaining_length); } bio_init_dec(l_bio, l_header_data,*l_modified_length_ptr); l_present = bio_read(l_bio, 1); if (!l_present) { bio_inalign(l_bio); l_header_data += bio_numbytes(l_bio); bio_destroy(l_bio); /* EPH markers */ if (p_tcp->csty & J2K_CP_CSTY_EPH) { if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) { printf("Error : expected EPH marker\n"); } else { l_header_data += 2; } } l_header_length = (l_header_data - *l_header_data_start); *l_modified_length_ptr -= l_header_length; *l_header_data_start += l_header_length; /* << INDEX */ // End of packet header position. Currently only represents the distance to start of packet // Will be updated later by incrementing with packet start value if (p_pack_info) { p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data); } /* INDEX >> */ * p_is_data_present = false; *p_data_read = l_current_data - p_src_data; return true; } l_band = l_res->bands; for (bandno = 0; bandno < l_res->numbands; ++bandno) { opj_tcd_precinct_t *l_prc = &(l_band->precincts[p_pi->precno]); if ((l_band->x1-l_band->x0 == 0)||(l_band->y1-l_band->y0 == 0)) { ++l_band; continue; } l_nb_code_blocks = l_prc->cw * l_prc->ch; l_cblk = l_prc->cblks.dec; for (cblkno = 0; cblkno < l_nb_code_blocks; cblkno++) { OPJ_UINT32 l_included,l_increment, l_segno; OPJ_INT32 n; /* if cblk not yet included before --> inclusion tagtree */ if (!l_cblk->numsegs) { l_included = tgt_decode(l_bio, l_prc->incltree, cblkno, p_pi->layno + 1); /* else one bit */ } else { l_included = bio_read(l_bio, 1); } /* if cblk not included */ if (!l_included) { l_cblk->numnewpasses = 0; ++l_cblk; continue; } /* if cblk not yet included --> zero-bitplane tagtree */ if (!l_cblk->numsegs) { OPJ_UINT32 i = 0; while (!tgt_decode(l_bio, l_prc->imsbtree, cblkno, i)) { ++i; } l_cblk->numbps = l_band->numbps + 1 - i; l_cblk->numlenbits = 3; } /* number of coding passes */ l_cblk->numnewpasses = t2_getnumpasses(l_bio); l_increment = t2_getcommacode(l_bio); /* length indicator increment */ l_cblk->numlenbits += l_increment; l_segno = 0; if (!l_cblk->numsegs) { if (! t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 1)) { bio_destroy(l_bio); return false; } } else { l_segno = l_cblk->numsegs - 1; if (l_cblk->segs[l_segno].numpasses == l_cblk->segs[l_segno].maxpasses) { ++l_segno; if (! t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) { bio_destroy(l_bio); return false; } } } n = l_cblk->numnewpasses; do { l_cblk->segs[l_segno].numnewpasses = int_min(l_cblk->segs[l_segno].maxpasses - l_cblk->segs[l_segno].numpasses, n); l_cblk->segs[l_segno].newlen = bio_read(l_bio, l_cblk->numlenbits + uint_floorlog2(l_cblk->segs[l_segno].numnewpasses)); n -= l_cblk->segs[l_segno].numnewpasses; if (n > 0) { ++l_segno; if (! t2_init_seg(l_cblk, l_segno, p_tcp->tccps[p_pi->compno].cblksty, 0)) { bio_destroy(l_bio); return false; } } } while (n > 0); ++l_cblk; } ++l_band; } if (bio_inalign(l_bio)) { bio_destroy(l_bio); return false; } l_header_data += bio_numbytes(l_bio); bio_destroy(l_bio); /* EPH markers */ if (p_tcp->csty & J2K_CP_CSTY_EPH) { if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) { // TODO opj_event_msg(t2->cinfo->event_mgr, EVT_ERROR, "Expected EPH marker\n"); } else { l_header_data += 2; } } l_header_length = (l_header_data - *l_header_data_start); *l_modified_length_ptr -= l_header_length; *l_header_data_start += l_header_length; /* << INDEX */ // End of packet header position. Currently only represents the distance to start of packet // Will be updated later by incrementing with packet start value if (p_pack_info) { p_pack_info->end_ph_pos = (OPJ_INT32)(l_current_data - p_src_data); } /* INDEX >> */ * p_is_data_present = true; *p_data_read = l_current_data - p_src_data; return true; }
static int cmd_bio(int argc, const cmd_args *argv) { int rc = 0; if (argc < 2) { printf("not enough arguments:\n"); usage: printf("%s list\n", argv[0].str); printf("%s read <device> <address> <offset> <len>\n", argv[0].str); printf("%s write <device> <address> <offset> <len>\n", argv[0].str); printf("%s erase <device> <offset> <len>\n", argv[0].str); printf("%s ioctl <device> <request> <arg>\n", argv[0].str); printf("%s remove <device>\n", argv[0].str); #if WITH_LIB_PARTITION printf("%s partscan <device> [offset]\n", argv[0].str); #endif return -1; } if (!strcmp(argv[1].str, "list")) { bio_dump_devices(); } else if (!strcmp(argv[1].str, "read")) { if (argc < 6) { printf("not enough arguments:\n"); goto usage; } addr_t address = argv[3].u; off_t offset = argv[4].u; // XXX use long size_t len = argv[5].u; bdev_t *dev = bio_open(argv[2].str); if (!dev) { printf("error opening block device\n"); return -1; } time_t t = current_time(); ssize_t err = bio_read(dev, (void *)address, offset, len); t = current_time() - t; dprintf(INFO, "bio_read returns %d, took %u msecs (%d bytes/sec)\n", (int)err, (uint)t, (uint32_t)((uint64_t)err * 1000 / t)); bio_close(dev); rc = err; } else if (!strcmp(argv[1].str, "write")) { if (argc < 6) { printf("not enough arguments:\n"); goto usage; } addr_t address = argv[3].u; off_t offset = argv[4].u; // XXX use long size_t len = argv[5].u; bdev_t *dev = bio_open(argv[2].str); if (!dev) { printf("error opening block device\n"); return -1; } time_t t = current_time(); ssize_t err = bio_write(dev, (void *)address, offset, len); t = current_time() - t; dprintf(INFO, "bio_write returns %d, took %u msecs (%d bytes/sec)\n", (int)err, (uint)t, (uint32_t)((uint64_t)err * 1000 / t)); bio_close(dev); rc = err; } else if (!strcmp(argv[1].str, "erase")) { if (argc < 5) { printf("not enough arguments:\n"); goto usage; } off_t offset = argv[3].u; // XXX use long size_t len = argv[4].u; bdev_t *dev = bio_open(argv[2].str); if (!dev) { printf("error opening block device\n"); return -1; } time_t t = current_time(); ssize_t err = bio_erase(dev, offset, len); t = current_time() - t; dprintf(INFO, "bio_erase returns %d, took %u msecs (%d bytes/sec)\n", (int)err, (uint)t, (uint32_t)((uint64_t)err * 1000 / t)); bio_close(dev); rc = err; } else if (!strcmp(argv[1].str, "ioctl")) { if (argc < 4) { printf("not enough arguments:\n"); goto usage; } int request = argv[3].u; int arg = (argc == 5) ? argv[4].u : 0; bdev_t *dev = bio_open(argv[2].str); if (!dev) { printf("error opening block device\n"); return -1; } int err = bio_ioctl(dev, request, (void *)arg); dprintf(INFO, "bio_ioctl returns %d\n", err); bio_close(dev); rc = err; } else if (!strcmp(argv[1].str, "remove")) { if (argc < 3) { printf("not enough arguments:\n"); goto usage; } bdev_t *dev = bio_open(argv[2].str); if (!dev) { printf("error opening block device\n"); return -1; } bio_unregister_device(dev); bio_close(dev); #if WITH_LIB_PARTITION } else if (!strcmp(argv[1].str, "partscan")) { if (argc < 3) { printf("not enough arguments:\n"); goto usage; } off_t offset = 0; if (argc > 3) offset = argv[3].u; rc = partition_publish(argv[2].str, offset); dprintf(INFO, "partition_publish returns %d\n", rc); #endif } else { printf("unrecognized subcommand\n"); goto usage; } return rc; }
int flock_load(bio *fd,uint8_t mver,uint8_t ignoreflag) { uint8_t loadbuff[FLOCK_REC_SIZE]; const uint8_t *ptr; int32_t r; uint32_t inode,sessionid; uint64_t owner; uint8_t ltype; inodelocks *il; lock *l; if (mver!=0x10) { return -1; } for (;;) { r = bio_read(fd,loadbuff,FLOCK_REC_SIZE); if (r!=FLOCK_REC_SIZE) { return -1; } ptr = loadbuff; inode = get32bit(&ptr); owner = get64bit(&ptr); sessionid = get32bit(&ptr); ltype = get8bit(&ptr); if (inode==0 && owner==0 && sessionid==0) { return 0; } if (of_checknode(sessionid,inode)==0) { if (ignoreflag) { mfs_syslog(LOG_ERR,"loading flock_locks: lock on closed file !!! (ignoring)"); continue; } else { mfs_syslog(LOG_ERR,"loading flock_locks: lock on closed file !!!"); return -1; } } // add lock il = flock_inode_find(inode); if (il==NULL) { il = flock_inode_new(inode); } if (il->active!=NULL && (il->active->ltype==LTYPE_WRITER || ltype==LTYPE_WRITER)) { if (ignoreflag) { mfs_syslog(LOG_ERR,"loading flock_locks: wrong lock !!! (ignoring)"); continue; } else { mfs_syslog(LOG_ERR,"loading flock_locks: wrong lock !!!"); return -1; } } l = malloc(sizeof(lock)); l->owner = owner; l->sessionid = sessionid; l->state = STATE_ACTIVE; l->ltype = ltype; l->lock_instances = NULL; l->parent = il; l->next = NULL; l->prev = NULL; flock_do_lock_inode_attach(l); } return 0; // unreachable }
int ipc_read(int fd, void *buf, size_t count) { return bio_read(fd, buf, count); }
int t2_decode_packet(unsigned char *src, int len, tcd_tile_t *tile, j2k_tcp_t *tcp, int compno, int resno, int precno, int layno) { int bandno, cblkno; tcd_tilecomp_t *tilec=&tile->comps[compno]; tcd_resolution_t *res=&tilec->resolutions[resno]; unsigned char *c=src; int present; if (layno==0) { for (bandno=0; bandno<res->numbands; bandno++) { tcd_band_t *band=&res->bands[bandno]; tcd_precinct_t *prc=&band->precincts[precno]; tgt_reset(prc->incltree); tgt_reset(prc->imsbtree); for (cblkno=0; cblkno<prc->cw*prc->ch; cblkno++) { tcd_cblk_t *cblk=&prc->cblks[cblkno]; cblk->numsegs=0; } } } if (tcp->csty&J2K_CP_CSTY_SOP) { c+=6; } bio_init_dec(c, src+len-c); present=bio_read(1); if (!present) { bio_inalign(); c+=bio_numbytes(); return c-src; } for (bandno=0; bandno<res->numbands; bandno++) { tcd_band_t *band=&res->bands[bandno]; tcd_precinct_t *prc=&band->precincts[precno]; for (cblkno=0; cblkno<prc->cw*prc->ch; cblkno++) { int included, increment, n; tcd_cblk_t *cblk=&prc->cblks[cblkno]; tcd_seg_t *seg; if (!cblk->numsegs) { included=tgt_decode(prc->incltree, cblkno, layno+1); } else { included=bio_read(1); } if (!included) { cblk->numnewpasses=0; continue; } if (!cblk->numsegs) { int i, numimsbs; for (i=0; !tgt_decode(prc->imsbtree, cblkno, i); i++) {} numimsbs=i-1; cblk->numbps=band->numbps-numimsbs; cblk->numlenbits=3; } cblk->numnewpasses=t2_getnumpasses(); increment=t2_getcommacode(); cblk->numlenbits+=increment; if (!cblk->numsegs) { seg=&cblk->segs[0]; t2_init_seg(seg, tcp->tccps[compno].cblksty); } else { seg=&cblk->segs[cblk->numsegs-1]; if (seg->numpasses==seg->maxpasses) { t2_init_seg(++seg, tcp->tccps[compno].cblksty); } } n=cblk->numnewpasses; do { seg->numnewpasses=int_min(seg->maxpasses-seg->numpasses, n); seg->newlen=bio_read(cblk->numlenbits+int_floorlog2(seg->numnewpasses)); n-=seg->numnewpasses; if (n>0) { t2_init_seg(++seg, tcp->tccps[compno].cblksty); } } while (n>0); } } bio_inalign(); c+=bio_numbytes(); if (tcp->csty&J2K_CP_CSTY_EPH) { c+=2; } for (bandno=0; bandno<res->numbands; bandno++) { tcd_band_t *band=&res->bands[bandno]; tcd_precinct_t *prc=&band->precincts[precno]; for (cblkno=0; cblkno<prc->cw*prc->ch; cblkno++) { tcd_cblk_t *cblk=&prc->cblks[cblkno]; tcd_seg_t *seg; if (!cblk->numnewpasses) continue; if (!cblk->numsegs) { seg=&cblk->segs[cblk->numsegs++]; cblk->len=0; } else { seg=&cblk->segs[cblk->numsegs-1]; if (seg->numpasses==seg->maxpasses) { seg++; cblk->numsegs++; } } do { if (c+seg->newlen>src+len) longjmp(j2k_error, 1); memcpy(cblk->data+cblk->len, c, seg->newlen); if (seg->numpasses==0) { seg->data=cblk->data+cblk->len; } c+=seg->newlen; cblk->len+=seg->newlen; seg->len+=seg->newlen; seg->numpasses+=seg->numnewpasses; cblk->numnewpasses-=seg->numnewpasses; if (cblk->numnewpasses>0) { seg++; cblk->numsegs++; } } while (cblk->numnewpasses>0); } } return c-src; }
int sclass_load(bio *fd,uint8_t mver,int ignoreflag) { uint8_t *databuff = NULL; const uint8_t *ptr; uint32_t labelmask; uint32_t chunkcount; uint16_t sclassid; uint16_t arch_delay; uint8_t create_mode; uint8_t create_labelscnt; uint8_t keep_labelscnt; uint8_t arch_labelscnt; uint8_t descrleng; uint8_t nleng; uint8_t admin_only; uint8_t name[MAXSCLASSNLENG]; uint8_t i,j; uint8_t orgroup; uint8_t hdrleng; if (mver<0x16) { // skip label descriptions for (i=0 ; i<26 ; i++) { if (bio_read(fd,&descrleng,1)!=1) { int err = errno; fputc('\n',stderr); errno = err; mfs_errlog(LOG_ERR,"loading storage class data: read error"); return -1; } if (descrleng>128) { mfs_syslog(LOG_ERR,"loading storage class data: description too long"); return -1; } bio_skip(fd,descrleng); } } if (mver==0x10) { orgroup = 1; } else { if (bio_read(fd,&orgroup,1)!=1) { int err = errno; fputc('\n',stderr); errno = err; mfs_errlog(LOG_ERR,"loading storage class: read error"); return -1; } if (orgroup>MASKORGROUP) { if (ignoreflag) { mfs_syslog(LOG_ERR,"loading storage class data: too many or-groups - ignore"); } else { mfs_syslog(LOG_ERR,"loading storage class data: too many or-groups"); return -1; } } } if (orgroup<1) { mfs_syslog(LOG_ERR,"loading storage class data: zero or-groups !!!"); return -1; } databuff = malloc(3U*9U*4U*(uint32_t)orgroup); passert(databuff); hdrleng = (mver==0x12)?11:(mver<=0x13)?3:(mver<=0x14)?5:(mver<=0x15)?8:10; while (1) { if (bio_read(fd,databuff,hdrleng)!=hdrleng) { int err = errno; fputc('\n',stderr); errno = err; mfs_errlog(LOG_ERR,"loading storage class data: read error"); free(databuff); databuff=NULL; return -1; } ptr = databuff; sclassid = get16bit(&ptr); if (mver>0x15) { nleng = get8bit(&ptr); admin_only = get8bit(&ptr); create_mode = get8bit(&ptr); arch_delay = get16bit(&ptr); create_labelscnt = get8bit(&ptr); keep_labelscnt = get8bit(&ptr); arch_labelscnt = get8bit(&ptr); chunkcount = 0; } else if (mver>0x14) { nleng = 0; admin_only = 0; create_mode = get8bit(&ptr); arch_delay = get16bit(&ptr); create_labelscnt = get8bit(&ptr); keep_labelscnt = get8bit(&ptr); arch_labelscnt = get8bit(&ptr); chunkcount = 0; } else if (mver>0x13) { nleng = 0; admin_only = 0; create_mode = get8bit(&ptr); create_labelscnt = get8bit(&ptr); keep_labelscnt = get8bit(&ptr); arch_labelscnt = keep_labelscnt; arch_delay = 0; chunkcount = 0; } else { nleng = 0; admin_only = 0; create_labelscnt = get8bit(&ptr); keep_labelscnt = create_labelscnt; arch_labelscnt = create_labelscnt; create_mode = CREATE_MODE_STD; arch_delay = 0; if (mver==0x12) { chunkcount = get32bit(&ptr); ptr+=4; } else { chunkcount = 0; } } if (nleng==0) { if (sclassid>=FIRSTSCLASSID) { nleng = snprintf((char*)name,MAXSCLASSNLENG,"sclass_%"PRIu32,(uint32_t)(sclassid+1-FIRSTSCLASSID)); } else { nleng = 0; } } else { if (bio_read(fd,name,nleng)!=nleng) { int err = errno; fputc('\n',stderr); errno = err; mfs_errlog(LOG_ERR,"loading storage class data: read error"); free(databuff); databuff=NULL; return -1; } } if (sclassid==0 && create_labelscnt==0 && keep_labelscnt==0 && arch_labelscnt==0 && chunkcount==0 && arch_delay==0) { break; } if (create_labelscnt==0 || create_labelscnt>MAXLABELSCNT || keep_labelscnt==0 || keep_labelscnt>MAXLABELSCNT || arch_labelscnt==0 || arch_labelscnt>MAXLABELSCNT) { mfs_arg_syslog(LOG_ERR,"loading storage class data: data format error (sclassid: %"PRIu16" ; create_mode: %"PRIu8" ; create_labelscnt: %"PRIu8" ; keep_labelscnt: %"PRIu8" ; arch_labelscnt: %"PRIu8" ; arch_delay: %"PRIu16")",sclassid,create_mode,create_labelscnt,keep_labelscnt,arch_labelscnt,arch_delay); free(databuff); databuff = NULL; return -1; } if (sclassid==0 || sclassid>=MAXSCLASS || nleng==0) { if (ignoreflag) { mfs_arg_syslog(LOG_ERR,"loading storage class data: bad sclassid (%"PRIu16") - ignore",sclassid); if (mver>0x14) { bio_skip(fd,(create_labelscnt+keep_labelscnt+arch_labelscnt)*4*orgroup); } else if (mver>0x13) { bio_skip(fd,(create_labelscnt+keep_labelscnt)*4*orgroup); } else { bio_skip(fd,(create_labelscnt)*4*orgroup); } continue; } else { mfs_arg_syslog(LOG_ERR,"loading storage class data: bad sclassid (%"PRIu16")",sclassid); free(databuff); databuff=NULL; return -1; } } if (mver>0x14) { if (bio_read(fd,databuff,(create_labelscnt+keep_labelscnt+arch_labelscnt)*4*orgroup)!=(create_labelscnt+keep_labelscnt+arch_labelscnt)*4*orgroup) { int err = errno; fputc('\n',stderr); errno = err; mfs_errlog(LOG_ERR,"loading storage class data: read error"); free(databuff); databuff=NULL; return -1; } } else if (mver>0x13) { if (bio_read(fd,databuff,(create_labelscnt+keep_labelscnt)*4*orgroup)!=(create_labelscnt+keep_labelscnt)*4*orgroup) { int err = errno; fputc('\n',stderr); errno = err; mfs_errlog(LOG_ERR,"loading storage class data: read error"); free(databuff); databuff=NULL; return -1; } } else { if (bio_read(fd,databuff,create_labelscnt*4*orgroup)!=create_labelscnt*4*orgroup) { int err = errno; fputc('\n',stderr); errno = err; mfs_errlog(LOG_ERR,"loading storage class data: read error"); free(databuff); databuff=NULL; return -1; } } if (sclassid>=FIRSTSCLASSID && sclasstab[sclassid].nleng>0) { if (ignoreflag) { mfs_syslog(LOG_ERR,"loading storage class data: repeated sclassid - ignore"); if (chunkcount>0) { bio_skip(fd,chunkcount*8); } continue; } else { mfs_syslog(LOG_ERR,"loading storage class data: repeated sclassid"); free(databuff); databuff=NULL; return -1; } } ptr = databuff; for (i=0 ; i<create_labelscnt ; i++) { for (j=0 ; j<MASKORGROUP ; j++) { if (j<orgroup) { labelmask = get32bit(&ptr); } else { labelmask = 0; } sclasstab[sclassid].create_labelmasks[i][j] = labelmask; } } for (i=0 ; i<keep_labelscnt ; i++) { for (j=0 ; j<MASKORGROUP ; j++) { if (mver>0x13) { if (j<orgroup) { labelmask = get32bit(&ptr); } else { labelmask = 0; } } else { labelmask = sclasstab[sclassid].create_labelmasks[i][j]; } sclasstab[sclassid].keep_labelmasks[i][j] = labelmask; } } for (i=0 ; i<arch_labelscnt ; i++) { for (j=0 ; j<MASKORGROUP ; j++) { if (mver>0x14) { if (j<orgroup) { labelmask = get32bit(&ptr); } else { labelmask = 0; } } else { labelmask = sclasstab[sclassid].keep_labelmasks[i][j]; } sclasstab[sclassid].arch_labelmasks[i][j] = labelmask; } } sclasstab[sclassid].create_mode = create_mode; sclasstab[sclassid].arch_delay = arch_delay; sclasstab[sclassid].create_labelscnt = create_labelscnt; sclasstab[sclassid].keep_labelscnt = keep_labelscnt; sclasstab[sclassid].arch_labelscnt = arch_labelscnt; sclasstab[sclassid].admin_only = admin_only; sclasstab[sclassid].nleng = nleng; memcpy(sclasstab[sclassid].name,name,nleng); sclasstab[sclassid].files = 0; sclasstab[sclassid].directories = 0; sclass_fix_has_labels_fields(sclassid); if (sclassid>=firstneverused) { firstneverused = sclassid+1; } if (chunkcount>0) { bio_skip(fd,chunkcount*8); } } free(databuff); databuff=NULL; return 1; }
static s32_t cpiofs_lookup(struct vnode * dnode, char * name, struct vnode * node) { struct blkdev * dev = (struct blkdev *)node->v_mount->m_dev; struct cpio_newc_header header; char path[MAX_PATH]; u32_t size, name_size, mode; loff_t off = 0; s8_t buf[9]; while(1) { bio_read(dev, (u8_t *)(&header), off, sizeof(struct cpio_newc_header)); if(strncmp((const char *)(header.c_magic), (const char *)"070701", 6) != 0) return ENOENT; buf[8] = '\0'; memcpy(buf, (const s8_t *)(header.c_filesize), 8); size = strtoul((const char *)buf, NULL, 16); memcpy(buf, (const s8_t *)(header.c_namesize), 8); name_size = strtoul((const char *)buf, NULL, 16); memcpy(buf, (const s8_t *)(header.c_mode), 8); mode = strtoul((const char *)buf, NULL, 16); bio_read(dev, (u8_t *)path, off + sizeof(struct cpio_newc_header), (loff_t)name_size); if( (size == 0) && (mode == 0) && (name_size == 11) && (strncmp(path, (const char *)"TRAILER!!!", 10) == 0) ) return ENOENT; if(check_path(path, (const char *)(dnode->v_path), (const char *)name)) break; off = off + sizeof(struct cpio_newc_header) + (((name_size + 1) & ~3) + 2) + size; off = (off + 3) & ~3; } if((mode & 00170000) == 0140000) node->v_type = VSOCK; else if((mode & 00170000) == 0120000) node->v_type = VLNK; else if((mode & 00170000) == 0100000) node->v_type = VREG; else if((mode & 00170000) == 0060000) node->v_type = VBLK; else if((mode & 00170000) == 0040000) node->v_type = VDIR; else if((mode & 00170000) == 0020000) node->v_type = VCHR; else if((mode & 00170000) == 0010000) node->v_type = VFIFO; else node->v_type = VREG; node->v_mode = 0; if(mode & 00400) node->v_mode |= S_IRUSR; if(mode & 00200) node->v_mode |= S_IWUSR; if(mode & 00100) node->v_mode |= S_IXUSR; if(mode & 00040) node->v_mode |= S_IRGRP; if(mode & 00020) node->v_mode |= S_IWGRP; if(mode & 00010) node->v_mode |= S_IXGRP; if(mode & 00004) node->v_mode |= S_IROTH; if(mode & 00002) node->v_mode |= S_IWOTH; if(mode & 00001) node->v_mode |= S_IXOTH; node->v_size = size; node->v_data = (void *)((s32_t)(off + sizeof(struct cpio_newc_header) + (((name_size + 1) & ~3) + 2))); return 0; }
int t2_getcommacode() { int n; for (n=0; bio_read(1); n++) {} return n; }
static ssize_t subdev_read(struct bdev *_dev, void *buf, off_t offset, size_t len) { subdev_t *subdev = (subdev_t *)_dev; return bio_read(subdev->parent, buf, offset + subdev->offset * subdev->dev.block_size, len); }
int t2_decode_packet(opj_t2_t* t2, unsigned char *src, int len, opj_tcd_tile_t *tile, opj_tcp_t *tcp, opj_pi_iterator_t *pi) { int bandno, cblkno; unsigned char *c = src; opj_cp_t *cp = t2->cp; int compno = pi->compno; /* component value */ int resno = pi->resno; /* resolution level value */ int precno = pi->precno; /* precinct value */ int layno = pi->layno; /* quality layer value */ opj_tcd_tilecomp_t *tilec = &tile->comps[compno]; opj_tcd_resolution_t *res = &tilec->resolutions[resno]; unsigned char *hd = NULL; int present; opj_bio_t *bio = NULL; /* BIO component */ if (layno == 0) { for (bandno = 0; bandno < res->numbands; bandno++) { opj_tcd_band_t *band = &res->bands[bandno]; opj_tcd_precinct_t *prc = &band->precincts[precno]; if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)||(band->z1-band->z0 == 0)) continue; tgt_reset(prc->incltree); tgt_reset(prc->imsbtree); for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) { opj_tcd_cblk_t *cblk = &prc->cblks[cblkno]; cblk->numsegs = 0; } } } /* SOP markers */ if (tcp->csty & J3D_CP_CSTY_SOP) { if ((*c) != 0xff || (*(c + 1) != 0x91)) { opj_event_msg(t2->cinfo, EVT_WARNING, "Expected SOP marker\n"); } else { c += 6; } /** TODO : check the Nsop value */ } /* When the marker PPT/PPM is used the packet header are store in PPT/PPM marker This part deal with this caracteristic step 1: Read packet header in the saved structure step 2: Return to codestream for decoding */ bio = bio_create(); if (cp->ppm == 1) { /* PPM */ hd = cp->ppm_data; bio_init_dec(bio, hd, cp->ppm_len); } else if (tcp->ppt == 1) { /* PPT */ hd = tcp->ppt_data; bio_init_dec(bio, hd, tcp->ppt_len); } else { /* Normal Case */ hd = c; bio_init_dec(bio, hd, src+len-hd); } present = bio_read(bio, 1); if (!present) { bio_inalign(bio); hd += bio_numbytes(bio); bio_destroy(bio); /* EPH markers */ if (tcp->csty & J3D_CP_CSTY_EPH) { if ((*hd) != 0xff || (*(hd + 1) != 0x92)) { printf("Error : expected EPH marker\n"); } else { hd += 2; } } if (cp->ppm == 1) { /* PPM case */ cp->ppm_len += cp->ppm_data-hd; cp->ppm_data = hd; return (c - src); } if (tcp->ppt == 1) { /* PPT case */ tcp->ppt_len+=tcp->ppt_data-hd; tcp->ppt_data = hd; return (c - src); } return (hd - src); } for (bandno = 0; bandno < res->numbands; bandno++) { opj_tcd_band_t *band = &res->bands[bandno]; opj_tcd_precinct_t *prc = &band->precincts[precno]; if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)||(band->z1-band->z0 == 0)) continue; for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) { int included, increment, n; opj_tcd_cblk_t *cblk = &prc->cblks[cblkno]; opj_tcd_seg_t *seg = NULL; /* if cblk not yet included before --> inclusion tagtree */ if (!cblk->numsegs) { included = tgt_decode(bio, prc->incltree, cblkno, layno + 1); /* else one bit */ } else { included = bio_read(bio, 1); } /* if cblk not included */ if (!included) { cblk->numnewpasses = 0; continue; } /* if cblk not yet included --> zero-bitplane tagtree */ if (!cblk->numsegs) { int i, numimsbs; for (i = 0; !tgt_decode(bio, prc->imsbtree, cblkno, i); i++); numimsbs = i - 1; cblk->numbps = band->numbps - numimsbs; cblk->numlenbits = 3; } /* number of coding passes */ cblk->numnewpasses = t2_getnumpasses(bio); increment = t2_getcommacode(bio); /* length indicator increment */ cblk->numlenbits += increment; if (!cblk->numsegs) { seg = &cblk->segs[0]; t2_init_seg(seg, tcp->tccps[compno].cblksty, 1); } else { seg = &cblk->segs[cblk->numsegs - 1]; if (seg->numpasses == seg->maxpasses) { t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0); } } n = cblk->numnewpasses; do { seg->numnewpasses = int_min(seg->maxpasses - seg->numpasses, n); seg->newlen = bio_read(bio, cblk->numlenbits + int_floorlog2(seg->numnewpasses)); n -= seg->numnewpasses; if (n > 0) { t2_init_seg(++seg, tcp->tccps[compno].cblksty, 0); } } while (n > 0); } } if (bio_inalign(bio)) { bio_destroy(bio); return -999; } hd += bio_numbytes(bio); bio_destroy(bio); /* EPH markers */ if (tcp->csty & J3D_CP_CSTY_EPH) { if ((*hd) != 0xff || (*(hd + 1) != 0x92)) { opj_event_msg(t2->cinfo, EVT_ERROR, "Expected EPH marker\n"); return -999; } else { hd += 2; } } if (cp->ppm==1) { cp->ppm_len+=cp->ppm_data-hd; cp->ppm_data = hd; } else if (tcp->ppt == 1) { tcp->ppt_len+=tcp->ppt_data-hd; tcp->ppt_data = hd; } else { c=hd; } for (bandno = 0; bandno < res->numbands; bandno++) { opj_tcd_band_t *band = &res->bands[bandno]; opj_tcd_precinct_t *prc = &band->precincts[precno]; if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)||(band->z1-band->z0 == 0)) continue; for (cblkno = 0; cblkno < prc->cblkno[0] * prc->cblkno[1] * prc->cblkno[2]; cblkno++) { opj_tcd_cblk_t *cblk = &prc->cblks[cblkno]; opj_tcd_seg_t *seg = NULL; if (!cblk->numnewpasses) continue; if (!cblk->numsegs) { seg = &cblk->segs[0]; cblk->numsegs++; cblk->len = 0; } else { seg = &cblk->segs[cblk->numsegs - 1]; if (seg->numpasses == seg->maxpasses) { seg++; cblk->numsegs++; } } do { if (c + seg->newlen > src + len) { return -999; } memcpy(cblk->data + cblk->len, c, seg->newlen); if (seg->numpasses == 0) { seg->data = cblk->data + cblk->len; } c += seg->newlen; cblk->len += seg->newlen; seg->len += seg->newlen; seg->numpasses += seg->numnewpasses; cblk->numnewpasses -= seg->numnewpasses; if (cblk->numnewpasses > 0) { seg++; cblk->numsegs++; } } while (cblk->numnewpasses > 0); } } return (c - src); }
int ext2_mount(bdev_t *dev, fscookie *cookie) { int err; LTRACEF("dev %p\n", dev); ext2_t *ext2 = malloc(sizeof(ext2_t)); ext2->dev = dev; err = bio_read(dev, &ext2->sb, 1024, sizeof(struct ext2_super_block)); if (err < 0) goto err; endian_swap_superblock(&ext2->sb); /* see if the superblock is good */ if (ext2->sb.s_magic != EXT2_SUPER_MAGIC) { err = -1; return err; } /* calculate group count, rounded up */ ext2->s_group_count = (ext2->sb.s_blocks_count + ext2->sb.s_blocks_per_group - 1) / ext2->sb.s_blocks_per_group; /* print some info */ LTRACEF("rev level %d\n", ext2->sb.s_rev_level); LTRACEF("compat features 0x%x\n", ext2->sb.s_feature_compat); LTRACEF("incompat features 0x%x\n", ext2->sb.s_feature_incompat); LTRACEF("ro compat features 0x%x\n", ext2->sb.s_feature_ro_compat); LTRACEF("block size %d\n", EXT2_BLOCK_SIZE(ext2->sb)); LTRACEF("inode size %d\n", EXT2_INODE_SIZE(ext2->sb)); LTRACEF("block count %d\n", ext2->sb.s_blocks_count); LTRACEF("blocks per group %d\n", ext2->sb.s_blocks_per_group); LTRACEF("group count %d\n", ext2->s_group_count); LTRACEF("inodes per group %d\n", ext2->sb.s_inodes_per_group); /* we only support dynamic revs */ if (ext2->sb.s_rev_level > EXT2_DYNAMIC_REV) { err = -2; return err; } /* make sure it doesn't have any ro features we don't support */ if (ext2->sb.s_feature_ro_compat & ~(EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|EXT2_FEATURE_RO_COMPAT_LARGE_FILE)) { err = -3; return err; } /* read in all the group descriptors */ ext2->gd = malloc(sizeof(struct ext2_group_desc) * ext2->s_group_count); err = bio_read(ext2->dev, (void *)ext2->gd, (EXT2_BLOCK_SIZE(ext2->sb) == 4096) ? 4096 : 2048, sizeof(struct ext2_group_desc) * ext2->s_group_count); if (err < 0) { err = -4; return err; } int i; for (i=0; i < ext2->s_group_count; i++) { endian_swap_group_desc(&ext2->gd[i]); LTRACEF("group %d:\n", i); LTRACEF("\tblock bitmap %d\n", ext2->gd[i].bg_block_bitmap); LTRACEF("\tinode bitmap %d\n", ext2->gd[i].bg_inode_bitmap); LTRACEF("\tinode table %d\n", ext2->gd[i].bg_inode_table); LTRACEF("\tfree blocks %d\n", ext2->gd[i].bg_free_blocks_count); LTRACEF("\tfree inodes %d\n", ext2->gd[i].bg_free_inodes_count); LTRACEF("\tused dirs %d\n", ext2->gd[i].bg_used_dirs_count); } /* initialize the block cache */ ext2->cache = bcache_create(ext2->dev, EXT2_BLOCK_SIZE(ext2->sb), 4); /* load the first inode */ err = ext2_load_inode(ext2, EXT2_ROOT_INO, &ext2->root_inode); if (err < 0) goto err; // TRACE("successfully mounted volume\n"); *cookie = ext2; return 0; err: LTRACEF("exiting with err code %d\n", err); free(ext2); return err; }
static int cmd_bio(int argc, const cmd_args *argv) { int rc = 0; if (argc < 2) { notenoughargs: printf("not enough arguments:\n"); usage: printf("%s list\n", argv[0].str); printf("%s read <device> <address> <offset> <len>\n", argv[0].str); printf("%s write <device> <address> <offset> <len>\n", argv[0].str); printf("%s dump <device> <offset> <len>\n", argv[0].str); printf("%s erase <device> <offset> <len>\n", argv[0].str); printf("%s ioctl <device> <request> <arg>\n", argv[0].str); printf("%s remove <device>\n", argv[0].str); printf("%s test <device>\n", argv[0].str); #if WITH_LIB_PARTITION printf("%s partscan <device> [offset]\n", argv[0].str); #endif #if WITH_LIB_CKSUM printf("%s crc32 <device> <offset> <len> [repeat]\n", argv[0].str); #endif return -1; } if (!strcmp(argv[1].str, "list")) { bio_dump_devices(); } else if (!strcmp(argv[1].str, "read")) { if (argc < 6) goto notenoughargs; addr_t address = argv[3].u; off_t offset = argv[4].u; // XXX use long size_t len = argv[5].u; bdev_t *dev = bio_open(argv[2].str); if (!dev) { printf("error opening block device\n"); return -1; } lk_time_t t = current_time(); ssize_t err = bio_read(dev, (void *)address, offset, len); t = current_time() - t; dprintf(INFO, "bio_read returns %d, took %u msecs (%d bytes/sec)\n", (int)err, (uint)t, (uint32_t)((uint64_t)err * 1000 / t)); bio_close(dev); rc = err; } else if (!strcmp(argv[1].str, "write")) { if (argc < 6) goto notenoughargs; addr_t address = argv[3].u; off_t offset = argv[4].u; // XXX use long size_t len = argv[5].u; bdev_t *dev = bio_open(argv[2].str); if (!dev) { printf("error opening block device\n"); return -1; } lk_time_t t = current_time(); ssize_t err = bio_write(dev, (void *)address, offset, len); t = current_time() - t; dprintf(INFO, "bio_write returns %d, took %u msecs (%d bytes/sec)\n", (int)err, (uint)t, (uint32_t)((uint64_t)err * 1000 / t)); bio_close(dev); rc = err; } else if (!strcmp(argv[1].str, "dump")) { if (argc < 5) { printf("not enough arguments:\n"); goto usage; } off_t offset = argv[3].u; // XXX use long size_t len = argv[4].u; bdev_t *dev = bio_open(argv[2].str); if (!dev) { printf("error opening block device\n"); return -1; } uint8_t *buf = memalign(CACHE_LINE, 256); ssize_t err = 0; while (len > 0) { size_t amt = MIN(256, len); ssize_t err = bio_read(dev, buf, offset, amt); if (err < 0) { dprintf(ALWAYS, "read error %s %zu@%zu (err %d)\n", argv[2].str, amt, (size_t)offset, (int)err); break; } DEBUG_ASSERT((size_t)err <= amt); hexdump8_ex(buf, err, offset); if ((size_t)err != amt) { dprintf(ALWAYS, "short read from %s @%zu (wanted %zu, got %zu)\n", argv[2].str, (size_t)offset, amt, (size_t)err); break; } offset += amt; len -= amt; } bio_close(dev); rc = err; } else if (!strcmp(argv[1].str, "erase")) { if (argc < 5) goto notenoughargs; off_t offset = argv[3].u; // XXX use long size_t len = argv[4].u; bdev_t *dev = bio_open(argv[2].str); if (!dev) { printf("error opening block device\n"); return -1; } lk_time_t t = current_time(); ssize_t err = bio_erase(dev, offset, len); t = current_time() - t; dprintf(INFO, "bio_erase returns %d, took %u msecs (%d bytes/sec)\n", (int)err, (uint)t, (uint32_t)((uint64_t)err * 1000 / t)); bio_close(dev); rc = err; } else if (!strcmp(argv[1].str, "ioctl")) { if (argc < 4) goto notenoughargs; int request = argv[3].u; int arg = (argc == 5) ? argv[4].u : 0; bdev_t *dev = bio_open(argv[2].str); if (!dev) { printf("error opening block device\n"); return -1; } int err = bio_ioctl(dev, request, (void *)arg); dprintf(INFO, "bio_ioctl returns %d\n", err); bio_close(dev); rc = err; } else if (!strcmp(argv[1].str, "remove")) { if (argc < 3) goto notenoughargs; bdev_t *dev = bio_open(argv[2].str); if (!dev) { printf("error opening block device\n"); return -1; } bio_unregister_device(dev); bio_close(dev); } else if (!strcmp(argv[1].str, "test")) { if (argc < 3) goto notenoughargs; bdev_t *dev = bio_open(argv[2].str); if (!dev) { printf("error opening block device\n"); return -1; } int err = bio_test_device(dev); bio_close(dev); rc = err; #if WITH_LIB_PARTITION } else if (!strcmp(argv[1].str, "partscan")) { if (argc < 3) goto notenoughargs; off_t offset = 0; if (argc > 3) offset = argv[3].u; rc = partition_publish(argv[2].str, offset); dprintf(INFO, "partition_publish returns %d\n", rc); #endif #if WITH_LIB_CKSUM } else if (!strcmp(argv[1].str, "crc32")) { if (argc < 5) goto notenoughargs; off_t offset = argv[3].u; // XXX use long size_t len = argv[4].u; bdev_t *dev = bio_open(argv[2].str); if (!dev) { printf("error opening block device\n"); return -1; } void *buf = malloc(dev->block_size); bool repeat = false; if (argc >= 6 && !strcmp(argv[5].str, "repeat")) { repeat = true; } do { ulong crc = 0; off_t pos = offset; while (pos < offset + len) { ssize_t err = bio_read(dev, buf, pos, MIN(len - (pos - offset), dev->block_size)); if (err <= 0) { printf("error reading at offset 0x%llx\n", offset + pos); break; } crc = crc32(crc, buf, err); pos += err; } printf("crc 0x%08lx\n", crc); } while (repeat); bio_close(dev); free(buf); #endif } else { printf("unrecognized subcommand\n"); goto usage; } return rc; }