c - How can a file contain null bytes? -


how possible files can contain null bytes in operating systems written in language null-terminating strings (namely, c)?

for example, if run shell code:

$ printf "hello\00, world!" > test.txt $ xxd test.txt 0000000: 4865 6c6c 6f00 2c20 576f 726c 6421       hello., world! 

i see null byte in test.txt (at least in os x). if c uses null-terminating strings, , os x written in c, how come file isn't terminated @ null byte, resulting in file containing hello instead of hello\00, world!? there fundamental difference between files , strings?

null-terminated strings c construct used determine end of sequence of characters intended used string. string manipulation functions such strcmp, strcpy, strchr, , others use construct perform duties.

but can still read , write binary data contains null bytes within program , files. can't treat them strings.

here's example of how works:

#include <stdio.h> #include <stdlib.h>  int main() {     file *fp = fopen("out1","w");     if (fp == null) {         perror("fopen failed");         exit(1);     }      int a1[] = { 0x12345678, 0x33220011, 0x0, 0x445566 };     char a2[] =  { 0x22, 0x33, 0x0, 0x66 };     char a3[] = "hello\x0world";      // writes whole array     fwrite(a1, sizeof(a1[0]), 4, fp);     //     fwrite(a2, sizeof(a2[0]), 4, fp);     // not write whole array -- "hello" written     fprintf(fp, "%s\n", a3);     //     fwrite(a3, sizeof(a3[0]), 12, fp);     fclose(fp);     return 0; } 

contents of out1:

[dbush@db-centos tmp]$ xxd out1 0000000: 7856 3412 1100 2233 0000 0000 6655 4400  xv4..."3....fud. 0000010: 2233 0066 4865 6c6c 6f0a 4865 6c6c 6f00  "3.fhello.hello. 0000020: 576f 726c 6400                           world. 

for first array, because use fwrite function , tell write 4 elements size of int, values in array appear in file. can see output values written, values 32-bit, , each value written in little-endian byte order. can see second , fourth elements of array each contain 1 null byte, while third value being 0 has 4 null bytes, , appear in file.

we use fwrite on second array, contains elements of type char, , again see array elements appear in file. in particular, third value in array 0, consists of single null byte appears in file.

the third array first written fprintf function using %s format specifier expects string. writes first 5 bytes of array file before encountering null byte, after stops reading array. prints newline character (0x0a) per format.

the third array written file again, time using fwrite. string constant "hello\x0world" contains 12 bytes: 5 "hello", 1 explicit null byte, 5 "world", , 1 null byte implicitly ends string constant. since fwrite given full size of array (12), writes of bytes. indeed, looking @ file contents, see each of bytes.

as side note, in each of fwrite calls, i've hardcoded size of array third parameter instead of using more dynamic expression such sizeof(a1)/sizeof(a1[0]) make more clear how many bytes being written in each case.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -