cencode-1.2/0000755000175000017500000000000011301302520012237 5ustar ydeliaydeliacencode-1.2/COPYING0000664000175000017500000000271611301302467013314 0ustar ydeliaydeliaCopyright (c) 2006-2008, Yuri D'Elia. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - Neither the name of Yuri D'Elia nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. cencode-1.2/Makefile0000664000175000017500000000123411301302467013713 0ustar ydeliaydelia# Makefile for cencode (for pmake or gmake) # Copyright(c) 2006-2008 by wave++ (Yuri D'Elia) # Distributed under Revised BSD license without ANY warranty. # configuration TARGETS := cencode all: $(TARGETS) CENCODE_OBJECTS := cencode.o # parameters CPPFLAGS += -MD CXXFLAGS += $(CWARN) # suffixes, rules CXXCOMPILE = $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $< CXXLINK = $(CXX) $(CXXFLAGS) $(LDFLAGS) -o $@ .SUFFIXES: .cc .o .cc.o: $(CXXCOMPILE) # targets cencode: $(CENCODE_OBJECTS) $(CXXLINK) $(CENCODE_OBJECTS) # stubs .PHONY: all clean distclean clean: rm -rf $(TARGETS) core *.[do] distclean: clean rm -rf *[#~] sinclude *.d cencode-1.2/cencode.10000664000175000017500000000464211301302467013743 0ustar ydeliaydelia.\" cencode.1: cencode manual .\" Copyright(c) 2006 by wave++ "Yuri D'Elia" .\" Distributed under Revised BSD license (see COPYING) WITHOUT ANY WARRANTY. .\" .Dd January 27, 2008 .Dt CENCODE 1 .\" .\" .Sh NAME .Nm cencode .Nd encode a stream into c-string escape sequences .\" .\" .Sh SYNOPSIS .Nm .Op Fl hrp .Op Fl c Ar columns .Op Fl q Ar qualifiers .Op Fl s Ar symbol .Op Ar file ... .\" .\" .Sh DESCRIPTION .Nm encodes a stream or a file into quoted c-string escape sequences, suitable for use directly into C code. .Pp .Nm is an universal alternative to dedicated export formats like XPM, unportable resource forks, linker flags and windows resources where dynamic loading of external data is not possible or undesired. .\" .\" .Sh OPTIONS .\" .Bl -tag -compact .It Fl h Display short usage information. .It Fl r When .Fl s is specified, output data in "readable" format. Output is spaced, indented and wrapped automatically at 79 columns. .It Fl p When .Fl s is specified, output data in "packed" format. No spacing or indent is used, and lines are wrapped at 255 columns in order to save space. This is the default. .It Fl c Ar columns When .Fl s is specified, wrap the output (last printable character) at the specified number of columns. Defaults to 79 in readable format or 255 in packed format. .It Fl q Ar qualifiers When .Fl s is specified, specify the symbol qualifiers. The default is "const". .It Fl s Ar sym Output the whole sequence as a quoted C symbol named sym. .El .Pp If no files are specified, .Nm reads the stream from standard input. .Pp When no symbol name is specified, .Nm outputs the encoded stream as if surrounded by quotes in a single line of infinite length, ideal for post-processing. No final newline is produced. .\" .\" .Sh EXAMPLES Convert the file .Pa data into the named string .Ar dataString .Pp .Dl cencode -s dataString data > file.c .Pp Encode multiple files into a single string: .Pp .Dl cat file1 file2 file3 | cencode -s sym > file.c .\" .\" .Sh NOTES Embedding unstructured binary content into executables should be avoided at all costs. .Pp The output generated by wrapping lines beyond 255 columns may not be readable by all compilers, text editors or tools. .\" .\" .Sh DIAGNOSTICS .Ex -std .\" .\" .Sh SEE ALSO .Xr lzo 1 .\" .\" .Sh AUTHORS .Nm is distributed under Revised BSD license (see COPYING) .Em WITHOUT ANY WARRANTY . Copyright(c) 2006 by .An "Yuri D'Elia" Aq wavexx@users.sf.net . cencode-1.2/cencode.h0000664000175000017500000000206011301302467014022 0ustar ydeliaydelia/* * cencode - A simple file-to-Cstring encoder * Copyright(c) 2007-2008 of wave++ (Yuri D'Elia) * Distributed under Revised BSD license without ANY warranty. */ #ifndef cencode_h #define cencode_h // system headers #include #include static inline int iscesc(int c) { switch(c) { case '\a': return 'a'; case '\b': return 'b'; case '\f': return 'f'; case '\n': return 'n'; case '\r': return 'r'; case '\t': return 't'; case '\v': return 'v'; case '\\': return '\\'; case '\"': return '"'; } return 0; } static inline int isctrig(int prev, int cur) { return (prev == '?' && cur == '?'); } static inline int escapeChar(char buf[8], int prev, int cur) { if(int esc = iscesc(cur)) { buf[0] = '\\'; buf[1] = esc; return 2; } if(!isprint(cur)) return sprintf(buf, "\\%o", cur); int len = 0; if(prev != EOF && (isctrig(prev, cur) || (!iscesc(prev) && !isprint(prev) && isdigit(cur)))) { buf[0] = '"'; buf[1] = '"'; len = 2; } buf[len++] = cur; return len; } #endif cencode-1.2/cencode.cc0000664000175000017500000000530511301302467014165 0ustar ydeliaydelia/* * cencode - A simple file-to-Cstring encoder - utility * Copyright(c) 2007-2008 of wave++ (Yuri D'Elia) * Distributed under Revised BSD license without ANY warranty. */ // local headers #include "cencode.h" // system headers #include using std::cout; using std::cerr; using std::cin; #include using std::ifstream; using std::istream; // c system headers #include #include struct EncodeParams { const char* sym; const char* qualifiers; unsigned columns; bool packed; }; void encode(istream& fd, const EncodeParams& params) { int len; unsigned col = 0; int prev = EOF; char buf[8]; if(params.sym) { if(params.qualifiers) { cout << params.qualifiers; col = strlen(params.qualifiers); } cout << " char "; col += 6; cout << params.sym; col += strlen(params.sym); if(params.packed) { cout << "[]=\""; col += 4; } else { cout << "[] =\n \""; col = 3; } } for(int cur = fd.get(); fd; cur = fd.get()) { len = escapeChar(buf, prev, cur); if(params.sym && col + len + 2 > params.columns) { len = escapeChar(buf, EOF, cur); if(params.packed) { cout << "\"\n\""; col = 1; } else { cout << "\"\n \""; col = 3; } } cout.write(buf, len); col += len; prev = cur; } if(!fd && !fd.eof()) throw 0; if(params.sym) { if(col + 3 > params.columns) cout << "\"\n;\n"; else cout << "\";\n"; } } int usage(const char* prg) { cerr << prg << " usage: " << prg << " [-hrp] [-c cols] [-q qualifiers] [-s sym] [file ...]\n"; return EXIT_FAILURE; } int main(int argc, char* argv[]) try { const char* columns = NULL; EncodeParams params; params.sym = NULL; params.qualifiers = "const"; params.packed = true; // args int arg; while((arg = getopt(argc, argv, "hs:c:rpq:")) != -1) switch(arg) { case 's': params.sym = optarg; break; case 'c': columns = optarg; break; case 'r': params.packed = false; break; case 'p': params.packed = true; break; case 'q': params.qualifiers = optarg; break; case 'h': default: return usage(argv[0]); } if(columns) params.columns = strtoul(columns, NULL, 0); else params.columns = (params.packed? 255: 79); // encode const char* file = argv[optind++]; if(!file) encode(cin, params); else do { ifstream fd(file); if(!fd) throw 0; encode(fd, params); if(params.sym) break; } while(file = argv[optind++]); return EXIT_SUCCESS; } catch(int) { perror(argv[0]); return EXIT_FAILURE; } cencode-1.2/NEWS0000664000175000017500000000033111301302467012747 0ustar ydeliaydeliacencode 1.2 ----------- * Fix build on newer GLIBC versions. cencode 1.1 ----------- * Column wrapping is now customizable. * Symbol qualifiers are now customizable. * cencode now defaults to packed output style.