Changeset 003c413 in mainline
- Timestamp:
- 2020-09-19T18:55:28Z (4 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- d145ecb
- Parents:
- 0ee3157
- Location:
- uspace/lib/riff
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/riff/include/riff/chunk.h
r0ee3157 r003c413 48 48 extern errno_t riff_write_uint32(riffw_t *, uint32_t); 49 49 50 extern errno_t riff_ropen(const char *, riff r_t **);50 extern errno_t riff_ropen(const char *, riff_rchunk_t *, riffr_t **); 51 51 extern errno_t riff_rclose(riffr_t *); 52 extern errno_t riff_read_uint32(riffr_t *, uint32_t *); 53 extern errno_t riff_rchunk_start(riffr_t *, riff_rchunk_t *); 54 extern errno_t riff_rchunk_end(riffr_t *, riff_rchunk_t *); 55 extern errno_t riff_rchunk_read(riffr_t *, riff_rchunk_t *, void *, size_t, 56 size_t *); 52 extern errno_t riff_read_uint32(riff_rchunk_t *, uint32_t *); 53 extern errno_t riff_rchunk_start(riff_rchunk_t *, riff_rchunk_t *); 54 extern errno_t riff_rchunk_end(riff_rchunk_t *); 55 extern errno_t riff_read(riff_rchunk_t *, void *, size_t, size_t *); 57 56 58 57 #endif -
uspace/lib/riff/include/types/riff/chunk.h
r0ee3157 r003c413 43 43 typedef uint32_t riff_cksize_t; 44 44 45 /** RIFF writer */ 46 typedef struct { 47 FILE *f; 48 /** Chunk start offset */ 49 long ckstart; 50 } riffw_t; 51 52 /** RIFF reader */ 53 typedef struct { 54 FILE *f; 55 } riffr_t; 56 45 57 /** RIFF chunk for reading */ 46 58 typedef struct { 59 riffr_t *riffr; 47 60 long ckstart; 48 61 riff_ckid_t ckid; … … 61 74 riff_cksize_t cksize; 62 75 } riff_ckinfo_t; 63 64 /** RIFF writer */65 typedef struct {66 FILE *f;67 /** Chunk start offset */68 long ckstart;69 } riffw_t;70 71 /** RIFF reader */72 typedef struct {73 FILE *f;74 } riffr_t;75 76 76 77 enum { -
uspace/lib/riff/src/chunk.c
r0ee3157 r003c413 117 117 return EIO; 118 118 119 wchunk->ckstart = pos ;119 wchunk->ckstart = pos + 2 * sizeof(uint32_t); 120 120 121 121 rc = riff_write_uint32(rw, ckid); … … 151 151 return EIO; 152 152 153 cksize = pos - wchunk->ckstart - 8;154 155 if (fseek(rw->f, wchunk->ckstart +4, SEEK_SET) < 0)153 cksize = pos - wchunk->ckstart; 154 155 if (fseek(rw->f, wchunk->ckstart - 4, SEEK_SET) < 0) 156 156 return EIO; 157 157 … … 190 190 * 191 191 * @param fname File name 192 * @param riffck Place to store root (RIFF) chunk 192 193 * @param rrr Place to store pointer to RIFF reader 193 194 * … … 195 196 * file.. 196 197 */ 197 errno_t riff_ropen(const char *fname, riff r_t **rrr)198 errno_t riff_ropen(const char *fname, riff_rchunk_t *riffck, riffr_t **rrr) 198 199 { 199 200 riffr_t *rr; 200 errno_t rc; 201 riff_rchunk_t fchunk; 202 long fsize; 203 errno_t rc; 204 int rv; 201 205 202 206 rr = calloc(1, sizeof(riffr_t)); … … 211 215 goto error; 212 216 } 217 218 rv = fseek(rr->f, 0, SEEK_END); 219 if (rv < 0) { 220 rc = EIO; 221 goto error; 222 } 223 224 fsize = ftell(rr->f); 225 if (fsize < 0) { 226 rc = EIO; 227 goto error; 228 } 229 230 rv = fseek(rr->f, 0, SEEK_SET); 231 if (rv < 0) { 232 rc = EIO; 233 goto error; 234 } 235 236 fchunk.riffr = rr; 237 fchunk.ckstart = 0; 238 fchunk.cksize = fsize; 239 240 rc = riff_rchunk_start(&fchunk, riffck); 241 if (rc != EOK) 242 goto error; 213 243 214 244 *rrr = rr; … … 237 267 /** Read uint32_t from RIFF file. 238 268 * 239 * @param r r RIFF reader269 * @param rchunk RIFF chunk 240 270 * @param v Place to store value 241 271 * @return EOK on success, EIO on error. 242 272 */ 243 errno_t riff_read_uint32(riff r_t *rr, uint32_t *v)273 errno_t riff_read_uint32(riff_rchunk_t *rchunk, uint32_t *v) 244 274 { 245 275 uint32_t vle; 246 247 if (fread(&vle, 1, sizeof(vle), rr->f) < sizeof(vle)) 248 return EIO; 276 errno_t rc; 277 size_t nread; 278 279 rc = riff_read(rchunk, &vle, sizeof(vle), &nread); 280 if (rc != EOK) 281 return rc; 249 282 250 283 *v = uint32_t_le2host(vle); … … 254 287 /** Start reading RIFF chunk. 255 288 * 256 * @param rr RIFF reader289 * @param parent Parent chunk 257 290 * @param rchunk Pointer to chunk structure to fill in 258 291 * 259 292 * @return EOK on success, EIO on error. 260 293 */ 261 errno_t riff_rchunk_start(riff r_t *rr, riff_rchunk_t *rchunk)294 errno_t riff_rchunk_start(riff_rchunk_t *parent, riff_rchunk_t *rchunk) 262 295 { 263 296 errno_t rc; 264 297 long pos; 265 298 266 pos = ftell( rr->f);299 pos = ftell(parent->riffr->f); 267 300 if (pos < 0) { 268 301 rc = EIO; … … 270 303 } 271 304 272 rchunk->ckstart = pos; 273 rc = riff_read_uint32(rr, &rchunk->ckid); 305 rchunk->riffr = parent->riffr; 306 rchunk->ckstart = pos + 8; 307 rc = riff_read_uint32(parent, &rchunk->ckid); 274 308 if (rc != EOK) 275 309 goto error; 276 rc = riff_read_uint32( rr, &rchunk->cksize);310 rc = riff_read_uint32(parent, &rchunk->cksize); 277 311 if (rc != EOK) 278 312 goto error; … … 290 324 static long riff_rchunk_get_end(riff_rchunk_t *rchunk) 291 325 { 292 return rchunk->ckstart + 8 +rchunk->cksize;326 return rchunk->ckstart + rchunk->cksize; 293 327 } 294 328 … … 313 347 * Seek to the first byte after end of chunk. 314 348 * 315 * @param rr RIFF reader316 349 * @param rchunk Chunk structure 317 350 * @return EOK on success, EIO on error. 318 351 */ 319 errno_t riff_rchunk_end(riff r_t *rr, riff_rchunk_t *rchunk)352 errno_t riff_rchunk_end(riff_rchunk_t *rchunk) 320 353 { 321 354 long ckend; 322 355 323 356 ckend = riff_rchunk_get_ndpos(rchunk); 324 if (fseek(rr->f, ckend, SEEK_SET) < 0) 325 return EIO; 326 357 if (fseek(rchunk->riffr->f, ckend, SEEK_SET) < 0) 358 return EIO; 359 360 rchunk->riffr = NULL; 327 361 return EOK; 328 362 } … … 334 368 * of bytes read is returned in @a *nbytes (can even be 0). 335 369 * 336 * @param rr RIFF reader 337 * @param rchunk RIFF chunk 370 * @param rchunk RIFF chunk for reading 338 371 * @param buf Buffer to read to 339 372 * @param bytes Number of bytes to read … … 343 376 * EIO on I/O error. 344 377 */ 345 errno_t riff_r chunk_read(riffr_t *rr, riff_rchunk_t *rchunk, void *buf,346 size_t bytes, size_t*nread)378 errno_t riff_read(riff_rchunk_t *rchunk, void *buf, size_t bytes, 379 size_t *nread) 347 380 { 348 381 long pos; … … 350 383 long toread; 351 384 352 pos = ftell(r r->f);385 pos = ftell(rchunk->riffr->f); 353 386 if (pos < 0) 354 387 return EIO; … … 364 397 } 365 398 366 *nread = fread(buf, 1, toread, r r->f);399 *nread = fread(buf, 1, toread, rchunk->riffr->f); 367 400 if (*nread == 0) 368 401 return EIO; -
uspace/lib/riff/src/rwave.c
r0ee3157 r003c413 255 255 } 256 256 257 rc = riff_ropen(fname, &wr-> rr);257 rc = riff_ropen(fname, &wr->wave, &wr->rr); 258 258 if (rc != EOK) { 259 259 assert(rc == EIO || rc == ENOMEM); … … 261 261 } 262 262 263 rc = riff_rchunk_start(wr->rr, &wr->wave);264 if (rc != EOK) {265 assert(rc == EIO);266 goto error; 267 } 268 269 rc = riff_read_uint32( wr->rr, &form_id);263 if (wr->wave.ckid != CKID_RIFF) { 264 printf("Not RIFF file\n"); 265 rc = ENOMEM; 266 goto error; 267 } 268 269 rc = riff_read_uint32(&wr->wave, &form_id); 270 270 if (rc != EOK) { 271 271 assert(rc == EIO); … … 279 279 } 280 280 281 rc = riff_rchunk_start( wr->rr, &fmt);281 rc = riff_rchunk_start(&wr->wave, &fmt); 282 282 if (rc != EOK) { 283 283 assert(rc == EIO); … … 291 291 } 292 292 293 rc = riff_r chunk_read(wr->rr,&fmt, &wfmt, sizeof(rwave_fmt_t), &nread);293 rc = riff_read(&fmt, &wfmt, sizeof(rwave_fmt_t), &nread); 294 294 if (rc != EOK) { 295 295 printf("error reading fmt chunk\n"); … … 304 304 } 305 305 306 rc = riff_rchunk_end( wr->rr,&fmt);306 rc = riff_rchunk_end(&fmt); 307 307 if (rc != EOK) { 308 308 assert(rc == EIO); … … 318 318 } 319 319 320 rc = riff_rchunk_start( wr->rr, &wr->data);320 rc = riff_rchunk_start(&wr->wave, &wr->data); 321 321 if (rc != EOK) { 322 322 assert(rc == EIO); … … 353 353 errno_t rc; 354 354 355 rc = riff_r chunk_read(wr->rr,&wr->data, buf, bytes, nread);355 rc = riff_read(&wr->data, buf, bytes, nread); 356 356 if (rc != EOK) { 357 357 assert(rc == EIO || rc == ELIMIT); … … 373 373 errno_t rc; 374 374 375 rc = riff_rchunk_end( wr->rr,&wr->wave);375 rc = riff_rchunk_end(&wr->wave); 376 376 if (rc != EOK) { 377 377 assert(rc == EIO);
Note:
See TracChangeset
for help on using the changeset viewer.