TEAP (Toolbox for Emotion Analysis using Physiological Signals) doc
tlvread.m
Go to the documentation of this file.
1 function [TLV] = tlvread(fid)
2 % FEFOPEN opens and reads FEF file
3 %
4 % HDR=fefopen(HDR)
5 %
6 %
7 % see also: SOPEN
8 %
9 % References:
10 % [1] <A HREF="ftp://sigftp.cs.tut.fi/pub/eeg-data/standards/cenf060.zip ">About CEN/TC251</A>
11 % [2] http://telecom.htwm.de/ASN1/ber.htm
12 % [3] http://asn1.elibel.tm.fr
13 
14 % $Id: tlvread.m 2205 2009-10-27 12:18:15Z schloegl $
15 % Copyright (C) 2004 Alois Schloegl <a.schloegl@ieee.org>
16 % This is part of the BIOSIG-toolbox http://biosig.sf.net/
17 
18 % This library is free software; you can redistribute it and/or
19 % modify it under the terms of the GNU Library General Public
20 % License as published by the Free Software Foundation; either
21 % Version 2 of the License, or (at your option) any later version.
22 %
23 % This library is distributed in the hope that it will be useful,
24 % but WITHOUT ANY WARRANTY; without even the implied warranty of
25 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 % Library General Public License for more details.
27 %
28 % You should have received a copy of the GNU Library General Public
29 % License along with this library; if not, write to the
30 % Free Software Foundation, Inc., 59 Temple Place - Suite 330,
31 % Boston, MA 02111-1307, USA.
32 
33 if feof(fid), TLV =[]; return; end;
34 
35 tag=fread(fid,1,'uint8');
36 if bitand(tag,31)==31,
37  t = fread(fid,1,'uint8');
38  ac = bitand(t,127);
39  while bitand(t,128),
40  t = fread(fid,1,'uint8');
41  ac = [ac*128 + bitand(t,127)];
42  end;
43  TAG = [tag,ac];
44 else
45  TAG = tag;
46 end;
47 
48 LEN = fread(fid,1,'uint8');
49 if bitand(LEN,128),
50  len = bitand(LEN,127);
51  if len==0,
52  % unbestimmte form
53  LEN = NaN;
54  else
55  % Langform
56  len = fread(fid,len,'uint8');
57  LEN = sum(len.*256.^[length(len)-1:-1:0]');
58  end
59 % else % Kurzform
60 end;
61 
62 class = bitand(tag,192)/64; % get class
63 FLAG.Primitive = ~bitand(tag,32); % check P/C bit
64 
65 
66 if (tag == 0) & (LEN==0)
67  TLV = [];
68  return;
69 end;
70 
71 TLV.TAG = TAG;
72 TLV.Class = class;
73 TLV.PC = FLAG.Primitive;
74 TLV.LEN = LEN;
75 
76 if FLAG.Primitive,
77  if ~isnan(LEN),
78  if class == 0, % Universal
79  classtag = bitand(tag,31);
80  if classtag==0,
81  TLV.reservedBER = fread(fid,LEN,'uchar');
82  elseif classtag==1,
83  TLV.Boolean = fread(fid,LEN,'uchar');
84  elseif classtag==2,
85  TLV.Integer = fread(fid,LEN,'int8');
86  elseif classtag==3,
87  TLV.Bitstring = fread(fid,LEN,'uchar');
88  elseif classtag==4,
89  TLV.OctetString = dec2hex(fread(fid,LEN,'uchar'));
90  elseif classtag==5,
91  TLV.Null = fread(fid,LEN,'uchar');
92  elseif classtag==6,
93  TLV.OID = fread(fid,LEN,'uchar');
94  elseif classtag==7,
95  TLV.ObjectDesc = fread(fid,LEN,'uchar');
96  elseif classtag==8,
97  TLV.external = fread(fid,LEN,'uchar');
98  elseif classtag==9,
99  TLV.real = fread(fid,LEN,'uchar');
100  elseif classtag==10,
101  TLV.enum = fread(fid,LEN,'uchar');
102  elseif classtag==11,
103  TLV.UTF8String = fread(fid,LEN,'uchar');
104  elseif classtag==12,
105  TLV.relativeOID = fread(fid,LEN,'uchar');
106  elseif classtag==13,
107  TLV.OID = fread(fid,LEN,'uchar');
108 
109  elseif classtag==14,
110  TLV.reserved14 = fread(fid,LEN,'uchar');
111  elseif classtag==15,
112  TLV.reserved15 = fread(fid,LEN,'uchar');
113  elseif classtag==16,
114  TLV.sequenceof = fread(fid,LEN,'uchar');
115  elseif classtag==17,
116  TLV.setof = fread(fid,LEN,'uchar');
117  elseif classtag==18,
118  TLV.numericString = fread(fid,LEN,'uchar');
119  elseif classtag==19,
120  TLV.PrintableString = fread(fid,LEN,'uchar');
121  elseif classtag==20,
122  TLV.TeletexStringT61 = fread(fid,LEN,'uchar');
123  elseif classtag==21,
124  TLV.VALUE = fread(fid,LEN,'uchar');
125  elseif classtag==22,
126  TLV.IA5String = fread(fid,LEN,'uchar');
127  elseif classtag==23,
128  TLV.UTCtime = fread(fid,LEN,'uchar');
129  elseif classtag==24,
130  TLV.generalizedTime = fread(fid,LEN,'uchar');
131  elseif classtag==25,
132  TLV.GraphicString = fread(fid,LEN,'uchar');
133  elseif classtag==26,
134  TLV.VisibleStringISO646String = fread(fid,LEN,'uchar');
135  elseif classtag==27,
136  TLV.GeneralString = fread(fid,LEN,'uchar');
137  elseif classtag==28,
138  TLV.UniversalString = fread(fid,LEN,'uchar');
139  elseif classtag==29,
140  TLV.CharacterString = fread(fid,LEN,'uchar');
141  elseif classtag==30,
142  TLV.BMPString = fread(fid,LEN,'uchar');
143  elseif classtag==31,
144  TLV.VALUE = fread(fid,LEN,'uchar');
145  else
146  TLV.VALUE = fread(fid,LEN,'uchar');
147  end;
148 
149  elseif class == 1, % Application
150  %TLV.VALUE = fread(fid,LEN,'uchar');
151  [TLV.VALUE] = tlvread(fid);
152 
153  elseif class == 2, % Context-Specific
154  TLV.VALUE = fread(fid,LEN,'uchar');
155 
156  elseif class == 3, % Private
157  TLV.VALUE = fread(fid,LEN,'uchar');
158 
159  end;
160  else
161  warning('unspecified length %i',ftell(fid));
162  accu = 255;
163  tmp = fread(fid,256,'uint8');
164  TLV.VALUE = [];
165  while ~any((tmp==0) & ([accu;tmp(1:end-1)]==0)) & ~feof(fid)
166  TLV.VALUE = [TLV.VALUE,tmp'];
167  accu = tmp(end);
168  tmp = fread(fid,256,'uint8');
169  end;
170  x = find((tmp==0) & ([accu;tmp(1:end-1)]==0));
171  TLV.VALUE = [TLV.VALUE, tmp(end-min(x))'];
172  status = fseek(fid,min(x)-length(tmp),0);
173  end;
174 
175 else
176  K = 0;
177  VAL = tlvread(fid);
178  while ~isempty(VAL),
179  K = K + 1;
180  TLV.VALUE{K} = VAL;
181  VAL = tlvread(fid);
182  end;
183 end
tlvread
function tlvread(in fid)
fefopen
function fefopen(in arg1)