| 1 | /*************************************************************************** |
|---|
| 2 | * Copyright (c) 2006-2008, Broadcom Corporation |
|---|
| 3 | * All Rights Reserved |
|---|
| 4 | * Confidential Property of Broadcom Corporation |
|---|
| 5 | * |
|---|
| 6 | * THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE |
|---|
| 7 | * AGREEMENT BETWEEN THE USER AND BROADCOM. YOU HAVE NO RIGHT TO USE OR |
|---|
| 8 | * EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT. |
|---|
| 9 | * |
|---|
| 10 | * $brcm_Workfile: bfile_stdio.c $ |
|---|
| 11 | * $brcm_Revision: 6 $ |
|---|
| 12 | * $brcm_Date: 9/12/08 6:56p $ |
|---|
| 13 | * |
|---|
| 14 | * Module Description: |
|---|
| 15 | * |
|---|
| 16 | * Revision History: |
|---|
| 17 | * |
|---|
| 18 | * $brcm_Log: /BSEAV/lib/bfile/bfile_stdio.c $ |
|---|
| 19 | * |
|---|
| 20 | * 6 9/12/08 6:56p vsilyaev |
|---|
| 21 | * PR 31887: Log reads and seeks that are used by bmedia framework |
|---|
| 22 | * |
|---|
| 23 | * 5 9/11/08 10:35a vsilyaev |
|---|
| 24 | * PR 45201: PR 10479_DVD 10627_DVD: Replaced all fseek and ftell with |
|---|
| 25 | * fseeko/ftello |
|---|
| 26 | * |
|---|
| 27 | * 4 8/6/08 7:19p vsilyaev |
|---|
| 28 | * PR 45201: Use 64 bit ready functions to support DIVX HD (where files |
|---|
| 29 | * could be larger than 4GB) |
|---|
| 30 | * |
|---|
| 31 | * 3 3/5/08 10:44a erickson |
|---|
| 32 | * PR40307: b_stdio_seek must return current position |
|---|
| 33 | * |
|---|
| 34 | * 2 8/31/06 9:46a vsilyaev |
|---|
| 35 | * PR 23826: Fixed warnings |
|---|
| 36 | * |
|---|
| 37 | * 1 4/7/06 12:31p vsilyaev |
|---|
| 38 | * PR 20680: Added bfile library |
|---|
| 39 | * |
|---|
| 40 | * |
|---|
| 41 | ***************************************************************************/ |
|---|
| 42 | #include "bstd.h" |
|---|
| 43 | #include "bfile_stdio.h" |
|---|
| 44 | #include "bkni.h" |
|---|
| 45 | |
|---|
| 46 | BDBG_MODULE(bfile_stdio); |
|---|
| 47 | |
|---|
| 48 | BDBG_OBJECT_ID(bfile_io_read_stdio); |
|---|
| 49 | struct bfile_io_read_stdio { |
|---|
| 50 | struct bfile_io_read ops; /* shall be the first member */ |
|---|
| 51 | FILE *fin; |
|---|
| 52 | BDBG_OBJECT(bfile_io_read_stdio) |
|---|
| 53 | }; |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | static ssize_t |
|---|
| 57 | b_stdio_read(bfile_io_read_t fd, void *buf, size_t length) |
|---|
| 58 | { |
|---|
| 59 | struct bfile_io_read_stdio *f=(void *)fd; |
|---|
| 60 | BDBG_OBJECT_ASSERT(f, bfile_io_read_stdio); |
|---|
| 61 | BDBG_MSG(("b_stdio_read: %#lx %lu:%u", (unsigned long)fd, (unsigned long)ftello(f->fin), (unsigned)length)); |
|---|
| 62 | return fread(buf, 1, length, f->fin); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | static off_t |
|---|
| 66 | b_stdio_seek(bfile_io_read_t fd, off_t offset, int whence) |
|---|
| 67 | { |
|---|
| 68 | struct bfile_io_read_stdio *f=(void *)fd; |
|---|
| 69 | int rc; |
|---|
| 70 | BDBG_OBJECT_ASSERT(f, bfile_io_read_stdio); |
|---|
| 71 | |
|---|
| 72 | BDBG_MSG(("b_stdio_seek: %#lx %lu:%d", (unsigned long)fd, (unsigned long)offset, whence)); |
|---|
| 73 | rc = fseeko(f->fin, offset, whence); |
|---|
| 74 | if (rc!=0) { return -1;} |
|---|
| 75 | return ftello(f->fin); |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | static int |
|---|
| 79 | b_stdio_bounds(bfile_io_read_t fd, off_t *first, off_t *last) |
|---|
| 80 | { |
|---|
| 81 | struct bfile_io_read_stdio *f=(void *)fd; |
|---|
| 82 | off_t cur; |
|---|
| 83 | int rc; |
|---|
| 84 | BDBG_OBJECT_ASSERT(f, bfile_io_read_stdio); |
|---|
| 85 | |
|---|
| 86 | *first = *last = 0; |
|---|
| 87 | cur = ftello(f->fin); |
|---|
| 88 | if (cur<0) { return -1; } |
|---|
| 89 | |
|---|
| 90 | rc = fseeko(f->fin, 0, SEEK_END); |
|---|
| 91 | if (rc<0) { return rc; } |
|---|
| 92 | |
|---|
| 93 | *first = 0; |
|---|
| 94 | *last = ftello(f->fin); |
|---|
| 95 | if (*last<0) { return -1; } |
|---|
| 96 | rc = fseeko(f->fin, cur, SEEK_SET); |
|---|
| 97 | if (rc<0) {return rc;} |
|---|
| 98 | return 0; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | static const struct bfile_io_read b_stdio_read_ops = { |
|---|
| 102 | b_stdio_read, |
|---|
| 103 | b_stdio_seek, |
|---|
| 104 | b_stdio_bounds, |
|---|
| 105 | BIO_DEFAULT_PRIORITY |
|---|
| 106 | }; |
|---|
| 107 | |
|---|
| 108 | bfile_io_read_t |
|---|
| 109 | bfile_stdio_read_attach(FILE *fin) |
|---|
| 110 | { |
|---|
| 111 | struct bfile_io_read_stdio *f; |
|---|
| 112 | f=BKNI_Malloc(sizeof(*f)); |
|---|
| 113 | if (!f) { return NULL; } |
|---|
| 114 | BDBG_OBJECT_INIT(f, bfile_io_read_stdio); |
|---|
| 115 | f->ops = b_stdio_read_ops; |
|---|
| 116 | f->fin = fin; |
|---|
| 117 | return &f->ops; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | void |
|---|
| 121 | bfile_stdio_read_detach(bfile_io_read_t file) |
|---|
| 122 | { |
|---|
| 123 | struct bfile_io_read_stdio *f=(void *)file; |
|---|
| 124 | BDBG_OBJECT_ASSERT(f, bfile_io_read_stdio); |
|---|
| 125 | |
|---|
| 126 | BDBG_OBJECT_DESTROY(f, bfile_io_read_stdio); |
|---|
| 127 | BKNI_Free(f); |
|---|
| 128 | |
|---|
| 129 | return; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | BDBG_OBJECT_ID(bfile_io_write_stdio); |
|---|
| 133 | struct bfile_io_write_stdio { |
|---|
| 134 | struct bfile_io_write ops; |
|---|
| 135 | FILE *fout; |
|---|
| 136 | BDBG_OBJECT(bfile_io_write_stdio) |
|---|
| 137 | }; |
|---|
| 138 | |
|---|
| 139 | static ssize_t |
|---|
| 140 | b_stdio_write(bfile_io_write_t fd, const void *buf, size_t length) |
|---|
| 141 | { |
|---|
| 142 | struct bfile_io_write_stdio *f=(void *)fd; |
|---|
| 143 | BDBG_OBJECT_ASSERT(f, bfile_io_write_stdio ); |
|---|
| 144 | |
|---|
| 145 | return fwrite(buf, 1, length, f->fout); |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | static off_t |
|---|
| 149 | b_stdio_trim(bfile_io_write_t fd, off_t trim_pos) |
|---|
| 150 | { |
|---|
| 151 | BSTD_UNUSED(fd); |
|---|
| 152 | BSTD_UNUSED(trim_pos); |
|---|
| 153 | BDBG_WRN(("trim is not supported")); |
|---|
| 154 | return 0; |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | static const struct bfile_io_write b_stdio_write_ops = { |
|---|
| 158 | b_stdio_write, |
|---|
| 159 | b_stdio_trim, |
|---|
| 160 | BIO_DEFAULT_PRIORITY |
|---|
| 161 | }; |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | bfile_io_write_t |
|---|
| 165 | bfile_stdio_write_attach(FILE *fout) |
|---|
| 166 | { |
|---|
| 167 | struct bfile_io_write_stdio *f; |
|---|
| 168 | f=BKNI_Malloc(sizeof(*f)); |
|---|
| 169 | if (!f) { return NULL; } |
|---|
| 170 | BDBG_OBJECT_INIT(f, bfile_io_write_stdio ); |
|---|
| 171 | f->ops = b_stdio_write_ops; |
|---|
| 172 | f->fout = fout; |
|---|
| 173 | return &f->ops; |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | void |
|---|
| 177 | bfile_stdio_write_detach(bfile_io_read_t fd) |
|---|
| 178 | { |
|---|
| 179 | struct bfile_io_write_stdio *f=(void *)fd; |
|---|
| 180 | BDBG_OBJECT_ASSERT(f, bfile_io_write_stdio ); |
|---|
| 181 | |
|---|
| 182 | BDBG_OBJECT_DESTROY(f, bfile_io_write_stdio); |
|---|
| 183 | BKNI_Free(f); |
|---|
| 184 | |
|---|
| 185 | return; |
|---|
| 186 | } |
|---|
| 187 | |
|---|