import{OGGS as p}from"./ogg-misc.js";export const MIN_PAGE_HEADER_SIZE=27,MAX_PAGE_HEADER_SIZE=282,MAX_PAGE_SIZE=MAX_PAGE_HEADER_SIZE+65025;export class OggReader{constructor(t){this.reader=t,this.pos=0}readBytes(t){const{view:e,offset:s}=this.reader.getViewAndOffset(this.pos,this.pos+t);return this.pos+=t,new Uint8Array(e.buffer,s,t)}readU8(){const{view:t,offset:e}=this.reader.getViewAndOffset(this.pos,this.pos+1);return this.pos+=1,t.getUint8(e)}readU32(){const{view:t,offset:e}=this.reader.getViewAndOffset(this.pos,this.pos+4);return this.pos+=4,t.getUint32(e,!0)}readI32(){const{view:t,offset:e}=this.reader.getViewAndOffset(this.pos,this.pos+4);return this.pos+=4,t.getInt32(e,!0)}readI64(){const t=this.readU32();return this.readI32()*4294967296+t}readAscii(t){const{view:e,offset:s}=this.reader.getViewAndOffset(this.pos,this.pos+t);this.pos+=t;let o="";for(let r=0;r<t;r++)o+=String.fromCharCode(e.getUint8(s+r));return o}readPageHeader(){const t=this.pos;if(this.readU32()!==p)return null;this.pos+=1;const s=this.readU8(),o=this.readI64(),r=this.readU32(),a=this.readU32(),i=this.readU32(),h=this.readU8(),d=new Uint8Array(h);for(let n=0;n<h;n++)d[n]=this.readU8();const f=27+h,c=d.reduce((n,g)=>n+g,0),u=f+c;return{headerStartPos:t,totalSize:u,dataStartPos:t+f,dataSize:c,headerType:s,granulePosition:o,serialNumber:r,sequenceNumber:a,checksum:i,lacingValues:d}}findNextPageHeader(t){for(;this.pos<t-3;){const e=this.readU32(),s=e&255,o=e>>>8&255,r=e>>>16&255,a=e>>>24&255,i=79;if(!(s!==i&&o!==i&&r!==i&&a!==i)){if(this.pos-=4,e===p)return!0;this.pos+=1}}return!1}}
