internal void Add(MacResource res) { resources.Add(res.id, res); }
public MacResourceFork(Stream input) { long start = input.Position; byte[] buf; // Read the resource fork header buf = new byte[16]; if (input.Read(buf, 0, 16) != 16) { throw new MacResourceForkException("Could not read header."); } long dataoffset = start + MacUtil.read32(buf, 0); long mapoffset = start + MacUtil.read32(buf, 4); long typelistoffset, namelistoffset; byte[] typelist; { // Seek to the resource map input.Seek(mapoffset, SeekOrigin.Begin); // Read the resource map header buf = new byte[30]; if (input.Read(buf, 0, 30) != 30) { throw new MacResourceForkException("Could not read resource map header."); } typelistoffset = mapoffset + MacUtil.read16(buf, 24); namelistoffset = mapoffset + MacUtil.read16(buf, 26); int numtypes = MacUtil.read16(buf, 28) + 1; // Create the types dictionary types = new Dictionary <string, MacResourceType>(numtypes); // Read the resource type list int listlen = numtypes * 8; typelist = new byte[listlen]; if (input.Read(typelist, 0, listlen) != listlen) { throw new MacResourceForkException("Could not read resource type list header."); } } // Walk the resource type list for (int i = 0; i < typelist.Length; i += 8) { MacResourceType type; long reflistoffset; byte[] reflist; { // Read the resource type list entry String typename = MacUtil.readString(typelist, i, 4); int numresources = MacUtil.read16(typelist, i + 4); reflistoffset = typelistoffset + MacUtil.read16(typelist, i + 6); // Create the resource type type = new MacResourceType(typename, numresources); types.Add(typename, type); // Seek to the reference list for this type input.Seek(reflistoffset, SeekOrigin.Begin); // Read the reference list int listlen = numresources * 12; reflist = new byte[listlen]; if (input.Read(reflist, 0, listlen) != listlen) { throw new MacResourceForkException("Could not read the reference list."); } } for (int j = 0; j < reflist.Length; j += 12) { // Read the reference list entry int id = MacUtil.read16(reflist, j); long resnameoffset = MacUtil.read16(reflist, j + 2); long resdataoffset = dataoffset + MacUtil.read24(reflist, j + 5); String name = null; if (resnameoffset != -1) { // Seek to the name list entry resnameoffset += namelistoffset; input.Seek(resnameoffset, SeekOrigin.Begin); // Read the resource name int resnamelen = input.ReadByte(); buf = new byte[resnamelen]; if (input.Read(buf, 0, resnamelen) != resnamelen) { throw new MacResourceForkException("Could not read resource name."); } name = MacUtil.readString(buf, 0, resnamelen); } // Seek to the resource data input.Seek(resdataoffset, SeekOrigin.Begin); // Create the resource MacResource res = new MacResource(input, type.type, name, id); type.Add(res); } } }
public MacResourceFork(Stream input) { long start = input.Position; byte[] buf; // Read the resource fork header buf = new byte[16]; if (input.Read(buf, 0, 16) != 16) throw new MacResourceForkException("Could not read header."); long dataoffset = start + MacUtil.read32(buf, 0); long mapoffset = start + MacUtil.read32(buf, 4); long typelistoffset, namelistoffset; byte[] typelist; { // Seek to the resource map input.Seek(mapoffset, SeekOrigin.Begin); // Read the resource map header buf = new byte[30]; if (input.Read(buf, 0, 30) != 30) throw new MacResourceForkException("Could not read resource map header."); typelistoffset = mapoffset + MacUtil.read16(buf, 24); namelistoffset = mapoffset + MacUtil.read16(buf, 26); int numtypes = MacUtil.read16(buf, 28) + 1; // Create the types dictionary types = new Dictionary<string,MacResourceType>(numtypes); // Read the resource type list int listlen = numtypes * 8; typelist = new byte[listlen]; if (input.Read(typelist, 0, listlen) != listlen) throw new MacResourceForkException("Could not read resource type list header."); } // Walk the resource type list for (int i = 0; i < typelist.Length; i += 8) { MacResourceType type; long reflistoffset; byte[] reflist; { // Read the resource type list entry String typename = MacUtil.readString(typelist, i, 4); int numresources = MacUtil.read16(typelist, i + 4); reflistoffset = typelistoffset + MacUtil.read16(typelist, i + 6); // Create the resource type type = new MacResourceType(typename, numresources); types.Add(typename, type); // Seek to the reference list for this type input.Seek(reflistoffset, SeekOrigin.Begin); // Read the reference list int listlen = numresources * 12; reflist = new byte[listlen]; if (input.Read(reflist, 0, listlen) != listlen) throw new MacResourceForkException("Could not read the reference list."); } for (int j = 0; j < reflist.Length; j += 12) { // Read the reference list entry int id = MacUtil.read16(reflist, j); long resnameoffset = MacUtil.read16(reflist, j + 2); long resdataoffset = dataoffset + MacUtil.read24(reflist, j + 5); String name = null; if (resnameoffset != -1) { // Seek to the name list entry resnameoffset += namelistoffset; input.Seek(resnameoffset, SeekOrigin.Begin); // Read the resource name int resnamelen = input.ReadByte(); buf = new byte[resnamelen]; if (input.Read(buf, 0, resnamelen) != resnamelen) throw new MacResourceForkException("Could not read resource name."); name = MacUtil.readString(buf, 0, resnamelen); } // Seek to the resource data input.Seek(resdataoffset, SeekOrigin.Begin); // Create the resource MacResource res = new MacResource(input, type.type, name, id); type.Add(res); } } }