summaryrefslogtreecommitdiffstats
path: root/songdbj/com/jcraft/jorbis/Comment.java
blob: f83b7cb9851a0e39b9e3f1bae8e67dfb118d73c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/* JOrbis
 * Copyright (C) 2000 ymnk, JCraft,Inc.
 *  
 * Written by: 2000 ymnk<ymnk@jcraft.com>
 *   
 * Many thanks to 
 *   Monty <monty@xiph.org> and 
 *   The XIPHOPHORUS Company http://www.xiph.org/ .
 * JOrbis has been based on their awesome works, Vorbis codec.
 *   
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public License
 * as published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
   
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 * 
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

package com.jcraft.jorbis;

import com.jcraft.jogg.*;

// the comments are not part of vorbis_info so that vorbis_info can be
// static storage
public class Comment{
  private static byte[] _vorbis="vorbis".getBytes();

  private static final int OV_EFAULT=-129;
  private static final int OV_EIMPL=-130;

  // unlimited user comment fields.  libvorbis writes 'libvorbis'
  // whatever vendor is set to in encode
  public byte[][] user_comments;
  public int[] comment_lengths; 
  public int comments;
  public byte[] vendor;

  public void init(){
    user_comments=null;
    comments=0;
    vendor=null;
  }

  public void add(String comment){
    add(comment.getBytes());
  }

  private void add(byte[] comment){
    byte[][] foo=new byte[comments+2][];
    if(user_comments!=null){
      System.arraycopy(user_comments, 0, foo, 0, comments);
    }
    user_comments=foo;

    int[] goo=new int[comments+2];
    if(comment_lengths!=null){
      System.arraycopy(comment_lengths, 0, goo, 0, comments);
    }
    comment_lengths=goo;

    byte[] bar=new byte[comment.length+1];
    System.arraycopy(comment, 0, bar, 0, comment.length);
    user_comments[comments]=bar;
    comment_lengths[comments]=comment.length;
    comments++;
    user_comments[comments]=null;
  }

  public void add_tag(String tag, String contents){
    if(contents==null) contents="";
    add(tag+"="+contents);
  }

/*
  private void add_tag(byte[] tag, byte[] contents){
    byte[] foo=new byte[tag.length+contents.length+1];
    int j=0; 
    for(int i=0; i<tag.length; i++){foo[j++]=tag[i];}
    foo[j++]=(byte)'='; j++;
    for(int i=0; i<contents.length; i++){foo[j++]=tag[i];}
    add(foo);
  }
*/
 
  // This is more or less the same as strncasecmp - but that doesn't exist
  // * everywhere, and this is a fairly trivial function, so we include it
  static boolean tagcompare(byte[] s1, byte[] s2, int n){
    int c=0;
    byte u1, u2;
    while(c < n){
      u1=s1[c]; u2=s2[c];
      if('Z'>=u1 && u1>='A')u1=(byte)(u1-'A'+'a');
      if('Z'>=u2 && u2>='A')u2=(byte)(u2-'A'+'a');
      if(u1!=u2){ return false; }
      c++;
    }
    return true;
  }

  public String query(String tag){
    return query(tag, 0);
  }

  public String query(String tag, int count){
    int foo=query(tag.getBytes(), count);
    if(foo==-1)return null;
    byte[] comment=user_comments[foo];
    for(int i=0; i<comment_lengths[foo]; i++){
      if(comment[i]=='='){
        return new String(comment, i+1, comment_lengths[foo]-(i+1));
      }
    }
    return null;
  }

  private int query(byte[] tag, int count){
    int i=0;
    int found = 0;
    int fulltaglen = tag.length + 1;
    byte[] fulltag = new byte[fulltaglen];
    System.arraycopy(tag, 0, fulltag, 0, tag.length);
    fulltag[tag.length]=(byte)'=';

    for(i=0;i<comments;i++){
      if(tagcompare(user_comments[i], fulltag, fulltaglen)){
        if(count==found){
 	  // We return a pointer to the data, not a copy
          //return user_comments[i] + taglen + 1;
          return i;
	}
        else{ found++; }
      }
    }
    return -1;
  }

  int unpack(Buffer opb){
    int vendorlen=opb.read(32);
    if(vendorlen<0){
      //goto err_out;
      clear();
      return(-1);
    }
    vendor=new byte[vendorlen+1];
    opb.read(vendor,vendorlen);
    comments=opb.read(32);
    if(comments<0){
      //goto err_out;
      clear();
      return(-1);
    }
    user_comments=new byte[comments+1][];
    comment_lengths=new int[comments+1];
	    
    for(int i=0;i<comments;i++){
      int len=opb.read(32);
      if(len<0){
	//goto err_out;
	clear();
	return(-1);
      }
      comment_lengths[i]=len;
      user_comments[i]=new byte[len+1];
      opb.read(user_comments[i], len);
    }	  
    if(opb.read(1)!=1){
      //goto err_out; // EOP check
      clear();
      return(-1);

    }
    return(0);
//  err_out:
//    comment_clear(vc);
//    return(-1);
  }

  int pack(Buffer opb){
    byte[] temp="Xiphophorus libVorbis I 20000508".getBytes();

    // preamble
    opb.write(0x03,8);
    opb.write(_vorbis);

    // vendor
    opb.write(temp.length,32);
    opb.write(temp);

    // comments

    opb.write(comments,32);
    if(comments!=0){
      for(int i=0;i<comments;i++){
	if(user_comments[i]!=null){
	  opb.write(comment_lengths[i],32);
	  opb.write(user_comments[i]);
	}
	else{
	  opb.write(0,32);
	}
      }
    }
    opb.write(1,1);
    return(0);
  }

  public int header_out(Packet op){
    Buffer opb=new Buffer();
    opb.writeinit();

    if(pack(opb)!=0) return OV_EIMPL;

    op.packet_base = new byte[opb.bytes()];
    op.packet=0;
    op.bytes=opb.bytes();
    System.arraycopy(opb.buffer(), 0, op.packet_base, 0, op.bytes);
    op.b_o_s=0;
    op.e_o_s=0;
    op.granulepos=0;
    return 0;
  }
 
  void clear(){
    for(int i=0;i<comments;i++)
      user_comments[i]=null;
    user_comments=null;
    vendor=null;
  }

  public String getVendor(){
    return new String(vendor, 0, vendor.length-1);
  }
  public String getComment(int i){
    if(comments<=i)return null;
    return new String(user_comments[i], 0, user_comments[i].length-1);
  }
  public String toString(){
    String foo="Vendor: "+new String(vendor, 0, vendor.length-1);
    for(int i=0; i<comments; i++){
      foo=foo+"\nComment: "+new String(user_comments[i], 0, user_comments[i].length-1);
    }
    foo=foo+"\n";
    return foo;
  }
}