001/* JOrbis
002 * Copyright (C) 2000 ymnk, JCraft,Inc.
003 *  
004 * Written by: 2000 ymnk<ymnk@jcaft.com>
005 *   
006 * Many thanks to 
007 *   Monty <monty@xiph.org> and 
008 *   The XIPHOPHORUS Company http://www.xiph.org/ .
009 * JOrbis has been based on their awesome works, Vorbis codec.
010 *   
011 * This program is free software; you can redistribute it and/or
012 * modify it under the terms of the GNU Library General Public License
013 * as published by the Free Software Foundation; either version 2 of
014 * the License, or (at your option) any later version.
015   
016 * This program is distributed in the hope that it will be useful,
017 * but WITHOUT ANY WARRANTY; without even the implied warranty of
018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
019 * GNU Library General Public License for more details.
020 * 
021 * You should have received a copy of the GNU Library General Public
022 * License along with this program; if not, write to the Free Software
023 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
024 */
025
026package com.jcraft.jorbis;
027
028import com.jcraft.jogg.*;
029
030// the comments are not part of vorbis_info so that vorbis_info can be
031// static storage
032public class Comment{
033  private static byte[] _vorbis="vorbis".getBytes();
034
035  private static final int OV_EFAULT=-129;
036  private static final int OV_EIMPL=-130;
037
038  // unlimited user comment fields.  libvorbis writes 'libvorbis'
039  // whatever vendor is set to in encode
040  public byte[][] user_comments;
041  public int[] comment_lengths; 
042  public int comments;
043  public byte[] vendor;
044
045  public void init(){
046    user_comments=null;
047    comments=0;
048    vendor=null;
049  }
050
051  public void add(String comment){
052    add(comment.getBytes());
053  }
054
055  private void add(byte[] comment){
056    byte[][] foo=new byte[comments+2][];
057    if(user_comments!=null){
058      System.arraycopy(user_comments, 0, foo, 0, comments);
059    }
060    user_comments=foo;
061
062    int[] goo=new int[comments+2];
063    if(comment_lengths!=null){
064      System.arraycopy(comment_lengths, 0, goo, 0, comments);
065    }
066    comment_lengths=goo;
067
068    byte[] bar=new byte[comment.length+1];
069    System.arraycopy(comment, 0, bar, 0, comment.length);
070    user_comments[comments]=bar;
071    comment_lengths[comments]=comment.length;
072    comments++;
073    user_comments[comments]=null;
074  }
075
076  public void add_tag(String tag, String contents){
077    if(contents==null) contents="";
078    add(tag+"="+contents);
079  }
080
081/*
082  private void add_tag(byte[] tag, byte[] contents){
083    byte[] foo=new byte[tag.length+contents.length+1];
084    int j=0; 
085    for(int i=0; i<tag.length; i++){foo[j++]=tag[i];}
086    foo[j++]=(byte)'='; j++;
087    for(int i=0; i<contents.length; i++){foo[j++]=tag[i];}
088    add(foo);
089  }
090*/
091 
092  // This is more or less the same as strncasecmp - but that doesn't exist
093  // * everywhere, and this is a fairly trivial function, so we include it
094  static boolean tagcompare(byte[] s1, byte[] s2, int n){
095    int c=0;
096    byte u1, u2;
097    while(c < n){
098      u1=s1[c]; u2=s2[c];
099      if(u1>='A')u1=(byte)(u1-'A'+'a');
100      if(u2>='A')u2=(byte)(u2-'A'+'a');
101      if(u1!=u2){ return false; }
102      c++;
103    }
104    return true;
105  }
106
107  public String query(String tag){
108    return query(tag, 0);
109  }
110
111  public String query(String tag, int count){
112    int foo=query(tag.getBytes(), count);
113    if(foo==-1)return null;
114    byte[] comment=user_comments[foo];
115    for(int i=0; i<comment_lengths[foo]; i++){
116      if(comment[i]=='='){
117        return new String(comment, i+1, comment_lengths[foo]-(i+1));
118      }
119    }
120    return null;
121  }
122
123  private int query(byte[] tag, int count){
124    int i=0;
125    int found = 0;
126    int taglen = tag.length;
127    byte[] fulltag = new byte[taglen+2];
128    System.arraycopy(tag, 0, fulltag, 0, tag.length);
129    fulltag[tag.length]=(byte)'=';
130
131    for(i=0;i<comments;i++){
132      if(tagcompare(user_comments[i], fulltag, taglen)){
133        if(count==found){
134          // We return a pointer to the data, not a copy
135          //return user_comments[i] + taglen + 1;
136          return i;
137        }
138        else{ found++; }
139      }
140    }
141    return -1;
142  }
143
144  int unpack(Buffer opb){
145    int vendorlen=opb.read(32);
146    if(vendorlen<0){
147      //goto err_out;
148      clear();
149      return(-1);
150    }
151    vendor=new byte[vendorlen+1];
152    opb.read(vendor,vendorlen);
153    comments=opb.read(32);
154    if(comments<0){
155      //goto err_out;
156      clear();
157      return(-1);
158    }
159    user_comments=new byte[comments+1][];
160    comment_lengths=new int[comments+1];
161            
162    for(int i=0;i<comments;i++){
163      int len=opb.read(32);
164      if(len<0){
165        //goto err_out;
166        clear();
167        return(-1);
168      }
169      comment_lengths[i]=len;
170      user_comments[i]=new byte[len+1];
171      opb.read(user_comments[i], len);
172    }     
173    if(opb.read(1)!=1){
174      //goto err_out; // EOP check
175      clear();
176      return(-1);
177
178    }
179    return(0);
180//  err_out:
181//    comment_clear(vc);
182//    return(-1);
183  }
184
185  int pack(Buffer opb){
186    byte[] temp="Xiphophorus libVorbis I 20000508".getBytes();
187
188    // preamble
189    opb.write(0x03,8);
190    opb.write(_vorbis);
191
192    // vendor
193    opb.write(temp.length,32);
194    opb.write(temp);
195
196    // comments
197
198    opb.write(comments,32);
199    if(comments!=0){
200      for(int i=0;i<comments;i++){
201        if(user_comments[i]!=null){
202          opb.write(comment_lengths[i],32);
203          opb.write(user_comments[i]);
204        }
205        else{
206          opb.write(0,32);
207        }
208      }
209    }
210    opb.write(1,1);
211    return(0);
212  }
213
214  public int header_out(Packet op){
215    Buffer opb=new Buffer();
216    opb.writeinit();
217
218    if(pack(opb)!=0) return OV_EIMPL;
219
220    op.packet_base = new byte[opb.bytes()];
221    op.packet=0;
222    op.bytes=opb.bytes();
223    System.arraycopy(opb.buffer(), 0, op.packet_base, 0, op.bytes);
224    op.b_o_s=0;
225    op.e_o_s=0;
226    op.granulepos=0;
227    return 0;
228  }
229 
230  void clear(){
231    for(int i=0;i<comments;i++)
232      user_comments[i]=null;
233    user_comments=null;
234    vendor=null;
235  }
236
237  public String getVendor(){
238    return new String(vendor, 0, vendor.length-1);
239  }
240  public String getComment(int i){
241    if(comments<=i)return null;
242    return new String(user_comments[i], 0, user_comments[i].length-1);
243  }
244  public String toString(){
245    String foo="Vendor: "+new String(vendor, 0, vendor.length-1);
246    for(int i=0; i<comments; i++){
247      foo=foo+"\nComment: "+new String(user_comments[i], 0, user_comments[i].length-1);
248    }
249    foo=foo+"\n";
250    return foo;
251  }
252}