Skip to content

Commit 170447f

Browse files
committed
Fix INCBIN/BIN unquoted filenames
asm6's documentation states the following syntaxes are allowed: INCBIN foo.bin INCBIN foo.bin, $400 INCBIN foo.bin, $200, $2000 The 1st example works, but the 2nd and 3rd fail with a vague error of "Can't open file." The filename attempted to be opened isn't shown, so the user has no idea what's wrong (had it been, I suspect this bug would have been fixed a lot sooner). However, if you put the filename part in double-quotes, all 3 forms work correctly. The bug: incbin() calls getfilename() (the only piece of code that uses the getfilename() function). getfilename() calls getword() with the 3rd argument (mcheck) being 0. An mcheck of 0 causes the function to *not* call strtok() against "mathy letters", meaning it acts like strchr() but with multiple letters that cause it to stop. So in the case of the string "foo.bin, $200, $2000", the string "foo.bin," is returned and treated as the filename (note the trailing comma). The fix: extend getword() to support an mcheck of 2, which calls strtok(dst,",") and thus only gets the filename portion; also consider that the size/offset arguments to INCBIN are comma-delimited. I could have simply changed getword() to use an mcheck value of 1, but that would allow syntactically-incorrect things to work, such as "INCBIN foo.bin^ $200". Finally, document all of this in readme.txt, as well as add some basic test case code in test_cases/incbin. (Note there is no simple way way to implement these tests, and I dislike "test suite frameworks," so just refer to the comments in the test code for now.)
1 parent 06ab0f1 commit 170447f

File tree

6 files changed

+62
-6
lines changed

6 files changed

+62
-6
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
*.exe
22
*.o
33
*.obj
4-
asm6f
4+
asm6f
5+
test_cases/incbin/incbin.bin

asm6f.c

+7-4
Original file line numberDiff line numberDiff line change
@@ -913,13 +913,16 @@ int eval(char **str,int precedence) {
913913
}
914914

915915
//copy next word from src into dst and advance src
916-
//mcheck=1 to crop mathy stuff (0 for filenames,etc)
916+
//mcheck=0 to crop nothing
917+
//mcheck=1 to crop mathy stuff
918+
//mcheck=2 to crop comma (e.g. for incbin)
917919
void getword(char *dst,char **src,int mcheck) {
918920
*src+=strspn(*src,whitesp);//eatwhitespace
919921
strncpy(dst,*src,WORDMAX-1);
920922
dst[WORDMAX-1]=0;
921923
strtok(dst,whitesp);//no trailing whitespace
922-
if(mcheck) strtok(dst,mathy);
924+
if(mcheck==1) strtok(dst,mathy);
925+
else if(mcheck==2) strtok(dst,",");
923926
*src+=strlen(dst);
924927
if(**src==':') (*src)++;//cheesy fix for rept/macro listing
925928
}
@@ -943,7 +946,7 @@ void getfilename(char *dst, char **next) {
943946
*next=end;
944947
}
945948
} else {
946-
getword(dst,next,0);
949+
getword(dst,next,2);
947950
}
948951
}
949952

@@ -2887,4 +2890,4 @@ void nes2chrbram(label *id, char **next) {
28872890
errmsg=OutOfRange;
28882891

28892892
ines_include++; use_nes2 = 1;
2890-
}
2893+
}

readme.txt

+16-1
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,21 @@ xaa (X And A) HIGHLY UNSTABLE!!!
169169

170170
lax is currently not supported.
171171

172+
--------------------------------------------------------------
173+
Assembler directive clarifications
174+
--------------------------------------------------------------
175+
176+
INCLUDE/INCSRC
177+
INCBIN/BIN
178+
179+
Filenames can be double-quoted, permitting names that contain
180+
spaces, ex.:
181+
182+
INCLUDE "some file.bin"
183+
INCBIN "some file.bin"
184+
INCBIN "some file.bin", $400
185+
INCBIN "some file.bin", $200, $2000
186+
172187
--------------------------------------------------------------
173188
Additional assembler directives
174189
--------------------------------------------------------------
@@ -275,4 +290,4 @@ freem's To-Do List
275290
* add ca65 debug format for NintendulatorDX
276291

277292
--------------------------------------------------------------
278-
<EOF>
293+
<EOF>
512 Bytes
Binary file not shown.

test_cases/incbin/filename.bin

512 Bytes
Binary file not shown.

test_cases/incbin/incbin.asm

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
; General INCBIN/BIN string parser test
2+
; Author: Jeremy Chadwick <jdc@koitsu.org>
3+
4+
; Quoted filename
5+
6+
incbin "filename.bin" ; entire file (512) size: 512
7+
incbin "filename.bin",$100 ; bytes 256-512 (256) size: 768
8+
incbin "filename.bin",$100,16 ; bytes 256-272 (16) size: 784
9+
incbin "filename.bin",$100, 16 ; bytes 256-272 (16) size: 800
10+
incbin "filename.bin", $100 ; bytes 256-512 (256) size: 1056
11+
incbin "filename.bin", $100,16 ; bytes 256-272 (16) size: 1072
12+
incbin "filename.bin", $100, 16 ; bytes 256-272 (16) size: 1088
13+
14+
; Unquoted filename
15+
16+
incbin filename.bin ; entire file (512) size: 1600
17+
incbin filename.bin,$100 ; bytes 256-512 (256) size: 1856
18+
incbin filename.bin,$100,16 ; bytes 256-272 (16) size: 1872
19+
incbin filename.bin,$100, 16 ; bytes 256-272 (16) size: 1888
20+
incbin filename.bin, $100 ; bytes 256-512 (256) size: 2144
21+
incbin filename.bin, $100,16 ; bytes 256-272 (16) size: 2160
22+
incbin filename.bin, $100, 16 ; bytes 256-272 (16) size: 2176
23+
24+
; Equates and math expressions used as params or args
25+
26+
DATAFILE EQU "filename.bin"
27+
FOO = (256+184+40)-4+4/2*2 ; FOO = 480
28+
incbin DATAFILE, FOO ; bytes 480-512 (32) size: 2208
29+
incbin filename.bin, FOO+1, $10 ; bytes 481-497 (16) size: 2224
30+
31+
; Filenames with spaces (must be quoted)
32+
33+
incbin "filename with spaces.bin" ; entire file (512) size: 2736
34+
incbin "filename with spaces.bin",$100,16 ; bytes 256-272 (16) size: 2752
35+
incbin "filename with spaces.bin", $100, 16 ; bytes 256-272 (16) size: 2768
36+
37+
; Resulting file MD5: 47ecec73da1168b0c0f243d0773b1c76

0 commit comments

Comments
 (0)