static void au_stream_rewind(bstreamhandle h) { au_filehdr_t au; (void) lseek(h->bstr_fd, 0L, SEEK_SET); if (read(h->bstr_fd, &au, sizeof (au)) != sizeof (au)) { return; } if (lseek(h->bstr_fd, (long)(BE32_TO_CPU(au.au_offset)), SEEK_SET) < 0) { return; } }
void mask_generation(uint8_t *seed, size_t seed_len, uint8_t *data, size_t data_len) { sha1_ctx_t ctx; uint8_t mask[SHA1_DIGEST_LENGTH]; uint32_t i, len, counter = 0; while (data_len > 0) { sha1_init(&ctx); sha1_update(&ctx, seed, seed_len); sha1_update(&ctx, (uint8_t*)&counter, 4); sha1_final(&ctx, mask); counter = CPU_TO_BE32(BE32_TO_CPU(counter) + 1); len = (data_len < SHA1_DIGEST_LENGTH) ? data_len : SHA1_DIGEST_LENGTH; for (i = 0; i < len; i++) *data++ ^= mask[i]; data_len -= len; } }
bstreamhandle open_au_read_stream(char *fname) { bstreamhandle h; int fd, sav; au_filehdr_t *au; struct stat st; uint32_t data_size; au = NULL; str_errno = 0; fd = open(fname, O_RDONLY); if (fd < 0) return (NULL); if (fstat(fd, &st) < 0) { goto au_open_failed; } if ((st.st_mode & S_IFMT) != S_IFREG) { str_errno = STR_ERR_NO_REG_FILE; goto au_open_failed; } au = (au_filehdr_t *)my_zalloc(sizeof (*au)); if (read(fd, au, sizeof (*au)) != sizeof (*au)) { str_errno = STR_ERR_AU_READ_ERR; goto au_open_failed; } au->au_magic = BE32_TO_CPU(au->au_magic); au->au_offset = BE32_TO_CPU(au->au_offset); au->au_data_size = BE32_TO_CPU(au->au_data_size); au->au_encoding = BE32_TO_CPU(au->au_encoding); au->au_sample_rate = BE32_TO_CPU(au->au_sample_rate); au->au_channels = BE32_TO_CPU(au->au_channels); if (au->au_magic != AUDIO_AU_FILE_MAGIC) { str_errno = STR_ERR_AU_BAD_HEADER; goto au_open_failed; } if ((au->au_encoding != AUDIO_AU_ENCODING_LINEAR_16) || (au->au_sample_rate != 44100) || (au->au_channels != 2)) { str_errno = STR_ERR_AU_UNSUPPORTED_FORMAT; goto au_open_failed; } if (au->au_data_size != AUDIO_AU_UNKNOWN_SIZE) { if ((au->au_offset + au->au_data_size) != st.st_size) { str_errno = STR_ERR_AU_BAD_HEADER; goto au_open_failed; } data_size = au->au_data_size; } else { data_size = st.st_size - au->au_offset; } if (data_size == 0) { str_errno = STR_ERR_AU_UNSUPPORTED_FORMAT; goto au_open_failed; } if (lseek(fd, au->au_offset, SEEK_SET) < 0) { goto au_open_failed; } free(au); h = (bstreamhandle)my_zalloc(sizeof (*h)); h->bstr_fd = fd; h->bstr_read = file_stream_read_wrbo; h->bstr_close = file_stream_close; h->bstr_size = audio_stream_size; h->bstr_rewind = au_stream_rewind; h->bstr_private = (void *)data_size; return (h); au_open_failed: sav = errno; (void) close(fd); if (au != NULL) free(au); errno = sav; return (NULL); }