#include "uoptions.h"
#include "pkg_genc.h"
#include "filetools.h"
+#include "charstr.h"
+#include "unicode/errorcode.h"
#define MAX_COLUMN ((uint32_t)(0xFFFFFFFFU))
/* prototypes --------------------------------------------------------------- */
static void
-getOutFilename(const char *inFilename, const char *destdir, char *outFilename, char *entryName, const char *newSuffix, const char *optFilename);
+getOutFilename(
+ const char *inFilename,
+ const char *destdir,
+ char *outFilename,
+ int32_t outFilenameCapacity,
+ char *entryName,
+ int32_t entryNameCapacity,
+ const char *newSuffix,
+ const char *optFilename);
static uint32_t
write8(FileStream *out, uint8_t byte, uint32_t column);
}
U_CAPI void U_EXPORT2
-writeAssemblyCode(const char *filename, const char *destdir, const char *optEntryPoint, const char *optFilename, char *outFilePath) {
+writeAssemblyCode(
+ const char *filename,
+ const char *destdir,
+ const char *optEntryPoint,
+ const char *optFilename,
+ char *outFilePath,
+ size_t outFilePathCapacity) {
uint32_t column = MAX_COLUMN;
- char entry[64];
- uint32_t buffer[1024];
- char *bufferStr = (char *)buffer;
+ char entry[96];
+ union {
+ uint32_t uint32s[1024];
+ char chars[4096];
+ } buffer;
FileStream *in, *out;
- size_t i, length;
+ size_t i, length, count;
in=T_FileStream_open(filename, "rb");
if(in==NULL) {
exit(U_FILE_ACCESS_ERROR);
}
- getOutFilename(filename, destdir, bufferStr, entry, ".S", optFilename);
- out=T_FileStream_open(bufferStr, "w");
+ getOutFilename(
+ filename,
+ destdir,
+ buffer.chars,
+ sizeof(buffer.chars),
+ entry,
+ sizeof(entry),
+ ".S",
+ optFilename);
+ out=T_FileStream_open(buffer.chars, "w");
if(out==NULL) {
- fprintf(stderr, "genccode: unable to open output file %s\n", bufferStr);
+ fprintf(stderr, "genccode: unable to open output file %s\n", buffer.chars);
exit(U_FILE_ACCESS_ERROR);
}
if (outFilePath != NULL) {
- uprv_strcpy(outFilePath, bufferStr);
+ if (uprv_strlen(buffer.chars) >= outFilePathCapacity) {
+ fprintf(stderr, "genccode: filename too long\n");
+ exit(U_ILLEGAL_ARGUMENT_ERROR);
+ }
+ uprv_strcpy(outFilePath, buffer.chars);
}
#if defined (WINDOWS_WITH_GNUC) && U_PLATFORM != U_PF_CYGWIN
}
}
- sprintf(bufferStr, assemblyHeader[assemblyHeaderIndex].header,
+ count = snprintf(
+ buffer.chars, sizeof(buffer.chars),
+ assemblyHeader[assemblyHeaderIndex].header,
entry, entry, entry, entry,
entry, entry, entry, entry);
- T_FileStream_writeLine(out, bufferStr);
+ if (count >= sizeof(buffer.chars)) {
+ fprintf(stderr, "genccode: entry name too long (long filename?)\n");
+ exit(U_ILLEGAL_ARGUMENT_ERROR);
+ }
+ T_FileStream_writeLine(out, buffer.chars);
T_FileStream_writeLine(out, assemblyHeader[assemblyHeaderIndex].beginLine);
for(;;) {
- memset(buffer, 0, sizeof(buffer));
- length=T_FileStream_read(in, buffer, sizeof(buffer));
+ memset(buffer.uint32s, 0, sizeof(buffer.uint32s));
+ length=T_FileStream_read(in, buffer.uint32s, sizeof(buffer.uint32s));
if(length==0) {
break;
}
- for(i=0; i<(length/sizeof(buffer[0])); i++) {
- column = write32(out, buffer[i], column);
+ for(i=0; i<(length/sizeof(buffer.uint32s[0])); i++) {
+ // TODO: What if the last read sees length not as a multiple of 4?
+ column = write32(out, buffer.uint32s[i], column);
}
}
T_FileStream_writeLine(out, "\n");
- sprintf(bufferStr, assemblyHeader[assemblyHeaderIndex].footer,
+ count = snprintf(
+ buffer.chars, sizeof(buffer.chars),
+ assemblyHeader[assemblyHeaderIndex].footer,
entry, entry, entry, entry,
entry, entry, entry, entry);
- T_FileStream_writeLine(out, bufferStr);
+ if (count >= sizeof(buffer.chars)) {
+ fprintf(stderr, "genccode: entry name too long (long filename?)\n");
+ exit(U_ILLEGAL_ARGUMENT_ERROR);
+ }
+ T_FileStream_writeLine(out, buffer.chars);
if(T_FileStream_error(in)) {
fprintf(stderr, "genccode: file read error while generating from file %s\n", filename);
}
U_CAPI void U_EXPORT2
-writeCCode(const char *filename, const char *destdir, const char *optName, const char *optFilename, char *outFilePath) {
+writeCCode(
+ const char *filename,
+ const char *destdir,
+ const char *optName,
+ const char *optFilename,
+ char *outFilePath,
+ size_t outFilePathCapacity) {
uint32_t column = MAX_COLUMN;
- char buffer[4096], entry[64];
+ char buffer[4096], entry[96];
FileStream *in, *out;
- size_t i, length;
+ size_t i, length, count;
in=T_FileStream_open(filename, "rb");
if(in==NULL) {
}
if(optName != NULL) { /* prepend 'icudt28_' */
- strcpy(entry, optName);
- strcat(entry, "_");
+ // +2 includes the _ and the NUL
+ if (uprv_strlen(optName) + 2 > sizeof(entry)) {
+ fprintf(stderr, "genccode: entry name too long (long filename?)\n");
+ exit(U_ILLEGAL_ARGUMENT_ERROR);
+ }
+ strcpy(entry, optName);
+ strcat(entry, "_");
} else {
- entry[0] = 0;
+ entry[0] = 0;
}
- getOutFilename(filename, destdir, buffer, entry+uprv_strlen(entry), ".c", optFilename);
+ getOutFilename(
+ filename,
+ destdir,
+ buffer,
+ sizeof(buffer),
+ entry + uprv_strlen(entry),
+ sizeof(entry) - uprv_strlen(entry),
+ ".c",
+ optFilename);
+
if (outFilePath != NULL) {
+ if (uprv_strlen(buffer) >= outFilePathCapacity) {
+ fprintf(stderr, "genccode: filename too long\n");
+ exit(U_ILLEGAL_ARGUMENT_ERROR);
+ }
uprv_strcpy(outFilePath, buffer);
}
+
out=T_FileStream_open(buffer, "w");
if(out==NULL) {
fprintf(stderr, "genccode: unable to open output file %s\n", buffer);
magic numbers we must still use the initial double.
[grhoten 4/24/2003]
*/
- sprintf(buffer,
+ count = snprintf(buffer, sizeof(buffer),
"#ifndef IN_GENERATED_CCODE\n"
"#define IN_GENERATED_CCODE\n"
"#define U_DISABLE_RENAMING 1\n"
" const char *bytes; \n"
"} %s={ 0.0, \n",
entry);
+ if (count >= sizeof(buffer)) {
+ fprintf(stderr, "genccode: entry name too long (long filename?)\n");
+ exit(U_ILLEGAL_ARGUMENT_ERROR);
+ }
T_FileStream_writeLine(out, buffer);
for(;;) {
T_FileStream_writeLine(out, "\"\n};\nU_CDECL_END\n");
#else
/* Function renaming shouldn't be done in data */
- sprintf(buffer,
+ count = snprintf(buffer, sizeof(buffer),
"#ifndef IN_GENERATED_CCODE\n"
"#define IN_GENERATED_CCODE\n"
"#define U_DISABLE_RENAMING 1\n"
" uint8_t bytes[%ld]; \n"
"} %s={ 0.0, {\n",
(long)T_FileStream_size(in), entry);
+ if (count >= sizeof(buffer)) {
+ fprintf(stderr, "genccode: entry name too long (long filename?)\n");
+ exit(U_ILLEGAL_ARGUMENT_ERROR);
+ }
T_FileStream_writeLine(out, buffer);
for(;;) {
#endif
static void
-getOutFilename(const char *inFilename, const char *destdir, char *outFilename, char *entryName, const char *newSuffix, const char *optFilename) {
+getOutFilename(
+ const char *inFilename,
+ const char *destdir,
+ char *outFilename,
+ int32_t outFilenameCapacity,
+ char *entryName,
+ int32_t entryNameCapacity,
+ const char *newSuffix,
+ const char *optFilename) {
const char *basename=findBasename(inFilename), *suffix=uprv_strrchr(basename, '.');
+ icu::CharString outFilenameBuilder;
+ icu::CharString entryNameBuilder;
+ icu::ErrorCode status;
+
/* copy path */
if(destdir!=NULL && *destdir!=0) {
- do {
- *outFilename++=*destdir++;
- } while(*destdir!=0);
- if(*(outFilename-1)!=U_FILE_SEP_CHAR) {
- *outFilename++=U_FILE_SEP_CHAR;
- }
- inFilename=basename;
+ outFilenameBuilder.append(destdir, status);
+ outFilenameBuilder.ensureEndsWithFileSeparator(status);
} else {
- while(inFilename<basename) {
- *outFilename++=*inFilename++;
- }
+ outFilenameBuilder.append(inFilename, basename - inFilename, status);
}
+ inFilename=basename;
if(suffix==NULL) {
/* the filename does not have a suffix */
- uprv_strcpy(entryName, inFilename);
+ entryNameBuilder.append(inFilename, status);
if(optFilename != NULL) {
- uprv_strcpy(outFilename, optFilename);
+ outFilenameBuilder.append(optFilename, status);
} else {
- uprv_strcpy(outFilename, inFilename);
+ outFilenameBuilder.append(inFilename, status);
}
- uprv_strcat(outFilename, newSuffix);
+ outFilenameBuilder.append(newSuffix, status);
} else {
- char *saveOutFilename = outFilename;
+ int32_t saveOutFilenameLength = outFilenameBuilder.length();
/* copy basename */
while(inFilename<suffix) {
- if(*inFilename=='-') {
- /* iSeries cannot have '-' in the .o objects. */
- *outFilename++=*entryName++='_';
- inFilename++;
- }
- else {
- *outFilename++=*entryName++=*inFilename++;
- }
+ // iSeries cannot have '-' in the .o objects.
+ char c = (*inFilename=='-') ? '_' : *inFilename;
+ outFilenameBuilder.append(c, status);
+ entryNameBuilder.append(c, status);
+ inFilename++;
}
/* replace '.' by '_' */
- *outFilename++=*entryName++='_';
+ outFilenameBuilder.append('_', status);
+ entryNameBuilder.append('_', status);
++inFilename;
/* copy suffix */
- while(*inFilename!=0) {
- *outFilename++=*entryName++=*inFilename++;
- }
-
- *entryName=0;
+ outFilenameBuilder.append(inFilename, status);
+ entryNameBuilder.append(inFilename, status);
if(optFilename != NULL) {
- uprv_strcpy(saveOutFilename, optFilename);
- uprv_strcat(saveOutFilename, newSuffix);
- } else {
- /* add ".c" */
- uprv_strcpy(outFilename, newSuffix);
+ outFilenameBuilder.truncate(saveOutFilenameLength);
+ outFilenameBuilder.append(optFilename, status);
}
+ // add ".c"
+ outFilenameBuilder.append(newSuffix, status);
+ }
+
+ if (status.isFailure()) {
+ fprintf(stderr, "genccode: error building filename or entrypoint\n");
+ exit(status.get());
}
+
+ if (outFilenameBuilder.length() >= outFilenameCapacity) {
+ fprintf(stderr, "genccode: output filename too long\n");
+ exit(U_ILLEGAL_ARGUMENT_ERROR);
+ }
+
+ if (entryNameBuilder.length() >= entryNameCapacity) {
+ fprintf(stderr, "genccode: entry name too long (long filename?)\n");
+ exit(U_ILLEGAL_ARGUMENT_ERROR);
+ }
+
+ uprv_strcpy(outFilename, outFilenameBuilder.data());
+ uprv_strcpy(entryName, entryNameBuilder.data());
}
#ifdef CAN_GENERATE_OBJECTS
}
U_CAPI void U_EXPORT2
-writeObjectCode(const char *filename, const char *destdir, const char *optEntryPoint, const char *optMatchArch, const char *optFilename, char *outFilePath) {
+writeObjectCode(
+ const char *filename,
+ const char *destdir,
+ const char *optEntryPoint,
+ const char *optMatchArch,
+ const char *optFilename,
+ char *outFilePath,
+ size_t outFilePathCapacity) {
/* common variables */
char buffer[4096], entry[96]={ 0 };
FileStream *in, *out;
}
size=T_FileStream_size(in);
- getOutFilename(filename, destdir, buffer, entry+entryOffset, newSuffix, optFilename);
+ getOutFilename(
+ filename,
+ destdir,
+ buffer,
+ sizeof(buffer),
+ entry + entryOffset,
+ sizeof(entry) - entryOffset,
+ newSuffix,
+ optFilename);
+
if (outFilePath != NULL) {
+ if (uprv_strlen(buffer) >= outFilePathCapacity) {
+ fprintf(stderr, "genccode: filename too long\n");
+ exit(U_ILLEGAL_ARGUMENT_ERROR);
+ }
uprv_strcpy(outFilePath, buffer);
}