Change Bitmap_CopyBlock to Bitmap_UNSAFE_CopyBlock to signify that it doesn't check arguments and will corrupt memory if used improperly

This commit is contained in:
UnknownShadow200 2019-07-12 22:46:45 +10:00
parent 9bb8dfbcfb
commit 5b47f939c3
11 changed files with 67 additions and 51 deletions

View File

@ -15,7 +15,7 @@ BitmapCol BitmapCol_Scale(BitmapCol value, float t) {
return value; return value;
} }
void Bitmap_CopyBlock(int srcX, int srcY, int dstX, int dstY, Bitmap* src, Bitmap* dst, int size) { void Bitmap_UNSAFE_CopyBlock(int srcX, int srcY, int dstX, int dstY, Bitmap* src, Bitmap* dst, int size) {
int x, y; int x, y;
for (y = 0; y < size; y++) { for (y = 0; y < size; y++) {
BitmapCol* srcRow = Bitmap_GetRow(src, srcY + y) + srcX; BitmapCol* srcRow = Bitmap_GetRow(src, srcY + y) + srcX;

View File

@ -48,7 +48,7 @@ typedef struct Bitmap_ { uint8_t* Scan0; int Width, Height; } Bitmap;
/* Copies a rectangle of pixels from one bitmap to another. */ /* Copies a rectangle of pixels from one bitmap to another. */
/* NOTE: If src and dst are the same, src and dst rectangles MUST NOT overlap. */ /* NOTE: If src and dst are the same, src and dst rectangles MUST NOT overlap. */
/* NOTE: Rectangles are NOT checked for whether they lie inside the bitmaps. */ /* NOTE: Rectangles are NOT checked for whether they lie inside the bitmaps. */
void Bitmap_CopyBlock(int srcX, int srcY, int dstX, int dstY, Bitmap* src, Bitmap* dst, int size); void Bitmap_UNSAFE_CopyBlock(int srcX, int srcY, int dstX, int dstY, Bitmap* src, Bitmap* dst, int size);
/* Allocates a new bitmap of the given dimensions. */ /* Allocates a new bitmap of the given dimensions. */
/* NOTE: You are responsible for freeing its memory! */ /* NOTE: You are responsible for freeing its memory! */
void Bitmap_Allocate(Bitmap* bmp, int width, int height); void Bitmap_Allocate(Bitmap* bmp, int width, int height);

View File

@ -274,7 +274,7 @@ static void Http_ParseHeader(struct HttpRequest* req, const String* line) {
valueEnd = String_IndexOf(&tmp, ';'); valueEnd = String_IndexOf(&tmp, ';');
if (valueEnd >= 0) tmp.length = valueEnd; if (valueEnd >= 0) tmp.length = valueEnd;
req->Cookies->Separator = '='; req->Cookies->separator = '=';
EntryList_Set(req->Cookies, &name, &tmp); EntryList_Set(req->Cookies, &name, &tmp);
} }
} }
@ -298,12 +298,12 @@ static void Http_SetRequestHeaders(struct HttpRequest* req) {
} }
if (req->Data) Http_AddHeader("Content-Type", &contentType); if (req->Data) Http_AddHeader("Content-Type", &contentType);
if (!req->Cookies || !req->Cookies->Entries.count) return; if (!req->Cookies || !req->Cookies->entries.count) return;
String_InitArray(cookies, cookiesBuffer); String_InitArray(cookies, cookiesBuffer);
for (i = 0; i < req->Cookies->Entries.count; i++) { for (i = 0; i < req->Cookies->entries.count; i++) {
if (i) String_AppendConst(&cookies, "; "); if (i) String_AppendConst(&cookies, "; ");
str = StringsBuffer_UNSAFE_Get(&req->Cookies->Entries, i); str = StringsBuffer_UNSAFE_Get(&req->Cookies->entries, i);
String_AppendString(&cookies, &str); String_AppendString(&cookies, &str);
} }
Http_AddHeader("Cookie", &cookies); Http_AddHeader("Cookie", &cookies);

View File

@ -320,9 +320,9 @@ void Hotkeys_Init(void) {
uint8_t modifiers; uint8_t modifiers;
bool more; bool more;
for (i = 0; i < Options.Entries.count; i++) { for (i = 0; i < Options.entries.count; i++) {
entry = StringsBuffer_UNSAFE_Get(&Options.Entries, i); entry = StringsBuffer_UNSAFE_Get(&Options.entries, i);
String_UNSAFE_Separate(&entry, Options.Separator, &key, &value); String_UNSAFE_Separate(&entry, Options.separator, &key, &value);
if (!String_CaselessStarts(&key, &prefix)) continue; if (!String_CaselessStarts(&key, &prefix)) continue;
/* Format is: key&modifiers = more-input&text */ /* Format is: key&modifiers = more-input&text */

View File

@ -14,7 +14,7 @@ static StringsBuffer Options_Changed;
int Options_ChangedCount(void) { return Options_Changed.count; } int Options_ChangedCount(void) { return Options_Changed.count; }
void Options_Free(void) { void Options_Free(void) {
StringsBuffer_Clear(&Options.Entries); StringsBuffer_Clear(&Options.entries);
StringsBuffer_Clear(&Options_Changed); StringsBuffer_Clear(&Options_Changed);
} }
@ -138,17 +138,17 @@ void Options_Load(void) {
String entry, key, value; String entry, key, value;
int i; int i;
if (!Options.Path) { if (!Options.path) {
EntryList_Init(&Options, "options-default.txt", '='); EntryList_Init(&Options, "options-default.txt", '=');
EntryList_Init(&Options, "options.txt", '='); EntryList_Init(&Options, "options.txt", '=');
} else { } else {
/* Reset all the unchanged options */ /* Reset all the unchanged options */
for (i = Options.Entries.count - 1; i >= 0; i--) { for (i = Options.entries.count - 1; i >= 0; i--) {
entry = StringsBuffer_UNSAFE_Get(&Options.Entries, i); entry = StringsBuffer_UNSAFE_Get(&Options.entries, i);
String_UNSAFE_Separate(&entry, '=', &key, &value); String_UNSAFE_Separate(&entry, '=', &key, &value);
if (Options_HasChanged(&key)) continue; if (Options_HasChanged(&key)) continue;
StringsBuffer_Remove(&Options.Entries, i); StringsBuffer_Remove(&Options.entries, i);
} }
/* Load only options which have not changed */ /* Load only options which have not changed */

View File

@ -993,11 +993,11 @@ static ReturnCode FontData_Init(const String* path, struct FontData* data, FT_Op
void Font_GetNames(StringsBuffer* buffer) { void Font_GetNames(StringsBuffer* buffer) {
String entry, name, path; String entry, name, path;
int i; int i;
if (!font_list.Entries.count) Font_Init(); if (!font_list.entries.count) Font_Init();
for (i = 0; i < font_list.Entries.count; i++) { for (i = 0; i < font_list.entries.count; i++) {
entry = StringsBuffer_UNSAFE_Get(&font_list.Entries, i); entry = StringsBuffer_UNSAFE_Get(&font_list.entries, i);
String_UNSAFE_Separate(&entry, font_list.Separator, &name, &path); String_UNSAFE_Separate(&entry, font_list.separator, &name, &path);
/* Only want Regular fonts here */ /* Only want Regular fonts here */
if (name.length < 2 || name.buffer[name.length - 1] != 'R') continue; if (name.length < 2 || name.buffer[name.length - 1] != 'R') continue;
@ -1016,7 +1016,7 @@ static String Font_LookupOf(const String* fontName, const char type) {
String Font_Lookup(const String* fontName, int style) { String Font_Lookup(const String* fontName, int style) {
String path; String path;
if (!font_list.Entries.count) Font_Init(); if (!font_list.entries.count) Font_Init();
path = String_Empty; path = String_Empty;
if (style & FONT_STYLE_BOLD) path = Font_LookupOf(fontName, 'B'); if (style & FONT_STYLE_BOLD) path = Font_LookupOf(fontName, 'B');
@ -1132,9 +1132,9 @@ static void Font_DirCallback(const String* path, void* obj) {
if (String_CaselessEnds(path, &fonExt)) return; if (String_CaselessEnds(path, &fonExt)) return;
/* If font is already known good, skip it */ /* If font is already known good, skip it */
for (i = 0; i < font_list.Entries.count; i++) { for (i = 0; i < font_list.entries.count; i++) {
entry = StringsBuffer_UNSAFE_Get(&font_list.Entries, i); entry = StringsBuffer_UNSAFE_Get(&font_list.entries, i);
String_UNSAFE_Separate(&entry, font_list.Separator, &name, &value); String_UNSAFE_Separate(&entry, font_list.separator, &name, &value);
String_UNSAFE_Separate(&value, ',', &fontPath, &index); String_UNSAFE_Separate(&value, ',', &fontPath, &index);
if (String_CaselessEquals(path, &fontPath)) return; if (String_CaselessEquals(path, &fontPath)) return;
@ -1604,7 +1604,7 @@ ReturnCode Process_StartOpen(const String* args) {
} }
ReturnCode Process_StartShell(void) { ReturnCode Process_StartShell(void) {
static const String args = String_FromConst("cmd.exe /C start cmd /C update.bat"); static const String args = String_FromConst("cmd.exe /C start cmd /C " UPDATE_FILENAME);
TCHAR str[300]; TCHAR str[300];
/* args must be modifiable, otherwise access violation */ /* args must be modifiable, otherwise access violation */
Platform_ConvertString(str, &args); Platform_ConvertString(str, &args);

View File

@ -409,11 +409,11 @@ static ReturnCode ModernPatcher_PatchTile(struct Stream* data, struct TilePatch*
ReturnCode res; ReturnCode res;
if ((res = Png_Decode(&bmp, data))) return res; if ((res = Png_Decode(&bmp, data))) return res;
Bitmap_CopyBlock(0, 0, tile->x1 * 16, tile->y1 * 16, &bmp, &terrainBmp, 16); Bitmap_UNSAFE_CopyBlock(0, 0, tile->x1 * 16, tile->y1 * 16, &bmp, &terrainBmp, 16);
/* only quartz needs copying to two tiles */ /* only quartz needs copying to two tiles */
if (tile->y2) { if (tile->y2) {
Bitmap_CopyBlock(0, 0, tile->x2 * 16, tile->y2 * 16, &bmp, &terrainBmp, 16); Bitmap_UNSAFE_CopyBlock(0, 0, tile->x2 * 16, tile->y2 * 16, &bmp, &terrainBmp, 16);
} }
Mem_Free(bmp.Scan0); Mem_Free(bmp.Scan0);
@ -441,7 +441,7 @@ static ReturnCode ModernPatcher_MakeAnimations(struct Stream* s, struct Stream*
Bitmap_Init(anim, 512, 16, anim_data); Bitmap_Init(anim, 512, 16, anim_data);
for (i = 0; i < 512; i += 16) { for (i = 0; i < 512; i += 16) {
Bitmap_CopyBlock(0, i, i, 0, &bmp, &anim, 16); Bitmap_UNSAFE_CopyBlock(0, i, i, 0, &bmp, &anim, 16);
} }
Mem_Free(bmp.Scan0); Mem_Free(bmp.Scan0);
@ -504,7 +504,7 @@ static ReturnCode TexPatcher_NewFiles(struct Stream* s) {
} }
static void TexPatcher_PatchTile(Bitmap* src, int srcX, int srcY, int dstX, int dstY) { static void TexPatcher_PatchTile(Bitmap* src, int srcX, int srcY, int dstX, int dstY) {
Bitmap_CopyBlock(srcX, srcY, dstX * 16, dstY * 16, src, &terrainBmp, 16); Bitmap_UNSAFE_CopyBlock(srcX, srcY, dstX * 16, dstY * 16, src, &terrainBmp, 16);
} }
static ReturnCode TexPatcher_Terrain(struct Stream* s) { static ReturnCode TexPatcher_Terrain(struct Stream* s) {

View File

@ -250,7 +250,7 @@ static void Animations_Draw(struct AnimationData* data, TextureLoc texLoc, int s
#endif #endif
} else { } else {
srcX = data->FrameX + data->State * size; srcX = data->FrameX + data->State * size;
Bitmap_CopyBlock(srcX, data->FrameY, 0, 0, &anims_bmp, &frame, size); Bitmap_UNSAFE_CopyBlock(srcX, data->FrameY, 0, 0, &anims_bmp, &frame, size);
} }
tex = Atlas1D.TexIds[dstX]; tex = Atlas1D.TexIds[dstX];
@ -439,7 +439,7 @@ static void Atlas_Convert2DTo1D(void) {
atlasX = Atlas2D_TileX(tile) * tileSize; atlasX = Atlas2D_TileX(tile) * tileSize;
atlasY = Atlas2D_TileY(tile) * tileSize; atlasY = Atlas2D_TileY(tile) * tileSize;
Bitmap_CopyBlock(atlasX, atlasY, 0, y * tileSize, Bitmap_UNSAFE_CopyBlock(atlasX, atlasY, 0, y * tileSize,
&Atlas2D.Bmp, &atlas1D, tileSize); &Atlas2D.Bmp, &atlas1D, tileSize);
} }
Atlas1D.TexIds[i] = Gfx_CreateTexture(&atlas1D, true, Gfx.Mipmaps); Atlas1D.TexIds[i] = Gfx_CreateTexture(&atlas1D, true, Gfx.Mipmaps);
@ -477,7 +477,7 @@ static GfxResourceID Atlas_LoadTile_Raw(TextureLoc texLoc, Bitmap* element) {
int x = Atlas2D_TileX(texLoc), y = Atlas2D_TileY(texLoc); int x = Atlas2D_TileX(texLoc), y = Atlas2D_TileY(texLoc);
if (y >= Atlas2D.RowsCount) return GFX_NULL; if (y >= Atlas2D.RowsCount) return GFX_NULL;
Bitmap_CopyBlock(x * size, y * size, 0, 0, &Atlas2D.Bmp, element, size); Bitmap_UNSAFE_CopyBlock(x * size, y * size, 0, 0, &Atlas2D.Bmp, element, size);
return Gfx_CreateTexture(element, false, Gfx.Mipmaps); return Gfx_CreateTexture(element, false, Gfx.Mipmaps);
} }

View File

@ -221,7 +221,7 @@ void EntryList_Load(struct EntryList* list, EntryList_Filter filter) {
ReturnCode res; ReturnCode res;
String_InitArray(path, pathBuffer); String_InitArray(path, pathBuffer);
String_AppendConst(&path, list->Path); String_AppendConst(&path, list->path);
res = Stream_OpenFile(&stream, &path); res = Stream_OpenFile(&stream, &path);
if (res == ReturnCode_FileNotFound) return; if (res == ReturnCode_FileNotFound) return;
@ -247,11 +247,11 @@ void EntryList_Load(struct EntryList* list, EntryList_Filter filter) {
/* If don't prevent this here, client aborts in StringsBuffer_Add */ /* If don't prevent this here, client aborts in StringsBuffer_Add */
if (entry.length > STRINGSBUFFER_LEN_MASK) { if (entry.length > STRINGSBUFFER_LEN_MASK) {
entry.length = 0; entry.length = 0;
String_Format1(&entry, "Skipping extremely long line in %c, file may have been corrupted", list->Path); String_Format1(&entry, "Skipping extremely long line in %c, file may have been corrupted", list->path);
Logger_WarnFunc(&entry); continue; Logger_WarnFunc(&entry); continue;
} }
String_UNSAFE_Separate(&entry, list->Separator, &key, &value); String_UNSAFE_Separate(&entry, list->separator, &key, &value);
EntryList_Set(list, &key, &value); EntryList_Set(list, &key, &value);
} }
@ -266,13 +266,13 @@ void EntryList_Save(struct EntryList* list) {
ReturnCode res; ReturnCode res;
String_InitArray(path, pathBuffer); String_InitArray(path, pathBuffer);
String_AppendConst(&path, list->Path); String_AppendConst(&path, list->path);
res = Stream_CreateFile(&stream, &path); res = Stream_CreateFile(&stream, &path);
if (res) { Logger_Warn2(res, "creating", &path); return; } if (res) { Logger_Warn2(res, "creating", &path); return; }
for (i = 0; i < list->Entries.count; i++) { for (i = 0; i < list->entries.count; i++) {
entry = StringsBuffer_UNSAFE_Get(&list->Entries, i); entry = StringsBuffer_UNSAFE_Get(&list->entries, i);
res = Stream_WriteLine(&stream, &entry); res = Stream_WriteLine(&stream, &entry);
if (res) { Logger_Warn2(res, "writing to", &path); break; } if (res) { Logger_Warn2(res, "writing to", &path); break; }
} }
@ -283,7 +283,7 @@ void EntryList_Save(struct EntryList* list) {
int EntryList_Remove(struct EntryList* list, const String* key) { int EntryList_Remove(struct EntryList* list, const String* key) {
int i = EntryList_Find(list, key); int i = EntryList_Find(list, key);
if (i >= 0) StringsBuffer_Remove(&list->Entries, i); if (i >= 0) StringsBuffer_Remove(&list->entries, i);
return i; return i;
} }
@ -292,22 +292,22 @@ void EntryList_Set(struct EntryList* list, const String* key, const String* valu
String_InitArray(entry, entryBuffer); String_InitArray(entry, entryBuffer);
if (value->length) { if (value->length) {
String_Format3(&entry, "%s%r%s", key, &list->Separator, value); String_Format3(&entry, "%s%r%s", key, &list->separator, value);
} else { } else {
String_Copy(&entry, key); String_Copy(&entry, key);
} }
EntryList_Remove(list, key); EntryList_Remove(list, key);
StringsBuffer_Add(&list->Entries, &entry); StringsBuffer_Add(&list->entries, &entry);
} }
String EntryList_UNSAFE_Get(struct EntryList* list, const String* key) { String EntryList_UNSAFE_Get(struct EntryList* list, const String* key) {
String curEntry, curKey, curValue; String curEntry, curKey, curValue;
int i; int i;
for (i = 0; i < list->Entries.count; i++) { for (i = 0; i < list->entries.count; i++) {
curEntry = StringsBuffer_UNSAFE_Get(&list->Entries, i); curEntry = StringsBuffer_UNSAFE_Get(&list->entries, i);
String_UNSAFE_Separate(&curEntry, list->Separator, &curKey, &curValue); String_UNSAFE_Separate(&curEntry, list->separator, &curKey, &curValue);
if (String_CaselessEquals(key, &curKey)) return curValue; if (String_CaselessEquals(key, &curKey)) return curValue;
} }
@ -318,9 +318,9 @@ int EntryList_Find(struct EntryList* list, const String* key) {
String curEntry, curKey, curValue; String curEntry, curKey, curValue;
int i; int i;
for (i = 0; i < list->Entries.count; i++) { for (i = 0; i < list->entries.count; i++) {
curEntry = StringsBuffer_UNSAFE_Get(&list->Entries, i); curEntry = StringsBuffer_UNSAFE_Get(&list->entries, i);
String_UNSAFE_Separate(&curEntry, list->Separator, &curKey, &curValue); String_UNSAFE_Separate(&curEntry, list->separator, &curKey, &curValue);
if (String_CaselessEquals(key, &curKey)) return i; if (String_CaselessEquals(key, &curKey)) return i;
} }
@ -328,7 +328,7 @@ int EntryList_Find(struct EntryList* list, const String* key) {
} }
void EntryList_Init(struct EntryList* list, const char* path, char separator) { void EntryList_Init(struct EntryList* list, const char* path, char separator) {
list->Path = path; list->path = path;
list->Separator = separator; list->separator = separator;
EntryList_Load(list, NULL); EntryList_Load(list, NULL);
} }

View File

@ -55,9 +55,9 @@ int Convert_ToBase64(const uint8_t* src, int len, char* dst);
int Convert_FromBase64(const char* src, int len, uint8_t* dst); int Convert_FromBase64(const char* src, int len, uint8_t* dst);
struct EntryList { struct EntryList {
const char* Path; const char* path;
char Separator; char separator;
StringsBuffer Entries; StringsBuffer entries;
}; };
typedef bool (*EntryList_Filter)(const String* entry); typedef bool (*EntryList_Filter)(const String* entry);

View File

@ -2792,7 +2792,23 @@ static int32_t Window_HandleInputEvent(struct android_app* app, AInputEvent* ev)
/* TODO: Do something with input here.. */ /* TODO: Do something with input here.. */
int32_t type = AInputEvent_getType(ev); int32_t type = AInputEvent_getType(ev);
Platform_Log1("INP MSG: %i", &type); Platform_Log1("INP MSG: %i", &type);
switch (type) {
case AINPUT_EVENT_TYPE_MOTION:
{
/* TODO Fix this */
int x = (int)AMotionEvent_getX(ev, 0);
int y = (int)AMotionEvent_getY(ev, 0);
Platform_Log2("TOUCH: %i,%i", &x, &y);
Mouse_SetPosition(x, y);
int action = AMotionEvent_getAction(ev) & AMOTION_EVENT_ACTION_MASK;
if (action == AMOTION_EVENT_ACTION_DOWN) Mouse_SetPressed(MOUSE_LEFT, true);
if (action == AMOTION_EVENT_ACTION_UP) Mouse_SetPressed(MOUSE_LEFT, false);
return 0; return 0;
}
}
return 1;
} }
static void Window_HandleAppEvent(struct android_app* app, int32_t cmd) { static void Window_HandleAppEvent(struct android_app* app, int32_t cmd) {