1 /* $Id$ */
2 
3 #ifndef __C2MAN__
4 #include <stdlib.h>
5 #include <string.h>
6 #endif
7 
8 #include "imports.h"
9 #include "method.h"
10 #include "libming.h"
11 
12 int
writeSWFImportBlockToMethod(SWFBlock block,SWFByteOutputMethod method,void * data)13 writeSWFImportBlockToMethod(SWFBlock block, SWFByteOutputMethod method, void *data)
14 {	SWFImportBlock imports = (SWFImportBlock) block;
15 	struct importitem *ip;
16 	int length, count;
17 	char *p;
18 	length = 3 + strlen(imports->filename);
19 	for(ip = imports->importlist, count = 0 ; ip ; ip = ip->next)
20 	{	length += 3 + strlen(ip->name);
21 		count++;
22 	}
23 
24 	for(p = imports->filename ; *p ; )
25 		method(*p++, data);
26 	method(0, data);
27 
28 	if(block->swfVersion >= 8)
29 	{
30 		method(1, data);
31 		method(0, data);
32 	}
33 
34 	methodWriteUInt16(count, method, data);
35 	for(ip = imports->importlist ; ip ; ip = ip->next)
36 	{	methodWriteUInt16(ip->id, method, data);
37 		for(p = ip->name ; *p ; )
38 			method(*p++, data);
39 		method(0, data);
40 	}
41 	return length;
42 }
43 
44 int
completeSWFImportBlock(SWFBlock block)45 completeSWFImportBlock(SWFBlock block)
46 {	SWFImportBlock imports = (SWFImportBlock) block;
47 	struct importitem *ip;
48 	int length = 3 + strlen(imports->filename);
49 	for(ip = imports->importlist ; ip ; ip = ip->next)
50 		length += 3 + strlen(ip->name);
51 
52 	/* SWF_IMPORTASSETS is deprecated with version >= 8. */
53 	if(block->swfVersion >= 8)
54 	{
55 		block->type = SWF_IMPORTASSETS2;
56 		length += 2;
57 	}
58 
59 	return length;
60 }
61 
62 void
destroySWFImportBlock(SWFImportBlock importBlock)63 destroySWFImportBlock(SWFImportBlock importBlock)
64 {
65 	struct importitem *ip, *nip;
66 	if(importBlock->filename)
67 		free(importBlock->filename);
68 	for(ip = importBlock->importlist ; ip ; ip = nip)
69 	{	nip = ip->next;
70 		if(ip->name)
71 			free(ip->name);
72 		free(ip);
73 	}
74 	free(importBlock);
75 }
76 
cpy(const char * text)77 static char *cpy(const char *text)
78 {	char *res, *p;
79 	p = res = (char *)malloc(strlen(text)+1);
80 	while((*p++ = *text++))
81 		;
82 	return res;
83 }
84 
85 SWFImportBlock
newSWFImportBlock(const char * filename)86 newSWFImportBlock(const char *filename)
87 {	SWFImportBlock iblock = (SWFImportBlock) malloc(sizeof(struct SWFImportBlock_s));
88 
89 	BLOCK(iblock)->type = SWF_IMPORTASSETS;
90 
91 	BLOCK(iblock)->writeBlock = (writeSWFBlockMethod) writeSWFImportBlockToMethod;
92 	BLOCK(iblock)->complete = completeSWFImportBlock;
93 	BLOCK(iblock)->dtor = (destroySWFBlockMethod) destroySWFImportBlock;
94 	BLOCK(iblock)->isDefined = 0;
95 	BLOCK(iblock)->completed = 0;
96 	iblock->filename = cpy(filename);
97 	iblock->importlist = NULL;
98 	return iblock;
99 }
100