1. Си / Говнокод #17245

    +136

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    int Sys_FunctionCmp(void *f1, void *f2) {
    
    	int i, j, l;
    	byte func_end[32] = {0xC3, 0x90, 0x90, 0x00};
    	byte *ptr, *ptr2;
    	byte *f1_ptr, *f2_ptr;
    
    	ptr = (byte *) f1;
    	if (*(byte *)ptr == 0xE9) {
    		//Com_Printf("f1 %p1 jmp %d\n", (int *) f1, *(int*)(ptr+1));
    		f1_ptr = (byte*)(((byte*)f1) + (*(int *)(ptr+1)) + 5);
    	}
    	else {
    		f1_ptr = ptr;
    	}
    	//Com_Printf("f1 ptr %p\n", f1_ptr);
    
    	ptr = (byte *) f2;
    	if (*(byte *)ptr == 0xE9) {
    		//Com_Printf("f2 %p jmp %d\n", (int *) f2, *(int*)(ptr+1));
    		f2_ptr = (byte*)(((byte*)f2) + (*(int *)(ptr+1)) + 5);
    	}
    	else {
    		f2_ptr = ptr;
    	}
    	//Com_Printf("f2 ptr %p\n", f2_ptr);
    
    #ifdef _DEBUG
    	sprintf((char *)func_end, "%c%c%c%c%c%c%c", 0x5F, 0x5E, 0x5B, 0x8B, 0xE5, 0x5D, 0xC3);
    #endif
    	for (i = 0; i < 1024; i++) {
    		for (j = 0; func_end[j]; j++) {
    			if (f1_ptr[i+j] != func_end[j])
    				break;
    		}
    		if (!func_end[j]) {
    			break;
    		}
    	}
    #ifdef _DEBUG
    	l = i + 7;
    #else
    	l = i + 2;
    #endif
    	//Com_Printf("function length = %d\n", l);
    
    	for (i = 0; i < l; i++) {
    		// check for a potential function call
    		if (*((byte *) &f1_ptr[i]) == 0xE8) {
    			// get the function pointers in case this really is a function call
    			ptr = (byte *) (((byte *) &f1_ptr[i]) + (*(int *) &f1_ptr[i+1])) + 5;
    			ptr2 = (byte *) (((byte *) &f2_ptr[i]) + (*(int *) &f2_ptr[i+1])) + 5;
    			// if it was a function call and both f1 and f2 call the same function
    			if (ptr == ptr2) {
    				i += 4;
    				continue;
    			}
    		}
    		if (f1_ptr[i] != f2_ptr[i])
    			return qfalse;
    	}
    	return qtrue;
    }

    Хмм...

    gost, 03 Декабря 2014

    Комментарии (5)
  2. Си / Говнокод #17244

    +132

    1. 1
    2. 2
    3. 3
    #ifndef M_PI
    #define M_PI		3.14159265358979323846f	// matches value in gcc v2 math.h
    #endif

    Сишкопроблемы.

    gost, 03 Декабря 2014

    Комментарии (15)
  3. Си / Говнокод #17234

    +133

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    bool findImageToleranceIn(CTSInfo *info, bitmap *imageToFind, int32_t *x, int32_t *y, int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint16_t tolerance)
    {
        int I, J, XX, YY;
        info->tol = tolerance;
        int dX = (x2 - x1) - (imageToFind->width - 1);
        int dY = (y2 - y1) - (imageToFind->height - 1);
        for (I = 0; I < dY; ++I)
        {
            for (J = 0; J < dX; ++J)
            {
                for (YY = 0; YY < imageToFind->height; ++YY)
                {
                    for (XX = 0; XX < imageToFind->width; ++XX)
                    {
                        rgb32* pixel = &imageToFind->pixels[YY * imageToFind->width + XX];
                        rgb32* targetPixel = &info->targetImage->pixels[(YY + I) * info->targetImage->width + (XX + J)];
                        if (pixel->a != 0)
                        {
                            if (!(*info->ctsFuncPtr)(info, pixel, targetPixel))
                            {
                                goto Skip;
                            }
                        }
                    }
                }
                *x = J + x1;
                *y = I + y1;
                return true;
                Skip:
                continue;
            }
        }
        *x = -1;
        *y = -1;
        return false;
    }

    В чём здесь сакральный смысл GoTo?

    Cynicrus, 01 Декабря 2014

    Комментарии (4)
  4. Си / Говнокод #17231

    +132

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    bool bitmap_to_24bit_string(bitmap *bmp, char **str, uint32_t *len)
    {
        if (!bmp || !bmp->pixels)
        return false;
        int I, J;
        uint32_t size = ((bmp->width * 24 + 31) / 32) * 4 * bmp->height;
        rgb24 *pixels = malloc(size);
        if (pixels)
        {
            for (I = 0; I < bmp->height; ++I)
            {
                for (J = 0; J < bmp->width; ++J)
                {
                    pixels[I * bmp->width + J].b = bmp->pixels[I * bmp->width + J].r;
                    pixels[I * bmp->width + J].g = bmp->pixels[I * bmp->width + J].g;
                    pixels[I * bmp->width + J].r = bmp->pixels[I * bmp->width + J].b;
                }
            }
            uint32_t destlen = compressBound(size);
            *str = malloc(destlen);
            if (*str)
            {
                if (compress((Bytef *)*str, (uLongf *)&destlen, (Bytef *)pixels, size) == Z_OK)
                {
                    free(pixels);
                    pixels = NULL;
                    char *b64str;
                    uint32_t b64_len;
                    if (base64encode((const uint8_t *)*str, destlen, &b64str, &b64_len))
                    {
                        free(*str);
                        *str = b64str;
                        *len = b64_len + 2;
                        b64str = malloc(*len);
                        if (b64str)
                        {
                            b64str[0] = 'm';
                            strncpy(&b64str[1], *str, b64_len);
                            free(*str);
                            *str = b64str;
                            (*str)[b64_len + 1] = '';
                            return true;
                        }
                    }
                }
                free(*str);
                *len = 0;
                *str = NULL;
            }
            free(pixels);
        }
        return false;
    }

    Ещё подкину в общую копилку

    Cynicrus, 01 Декабря 2014

    Комментарии (1)
  5. Си / Говнокод #17227

    +136

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    char *stringFromDTM(MDTM *dtm)
    {
        if (dtm->count < 1)
        return "";
        uint32_t size = (sizeof(MDTMPoint) + sizeof(uint32_t)) * dtm->count;
        void *data = calloc(1, size);
        uint32_t *ptr = data;
        *(ptr++) = size;
        uint32_t index;
        for (index = 0; index < dtm->count; index++)
        *(ptr++) = dtm->points[index].x;
        for (index = 0; index < dtm->count; index++)
        *(ptr++) = dtm->points[index].y;
        for (index = 0; index < dtm->count; index++)
        *(ptr++) = dtm->points[index].color;
        for (index = 0; index < dtm->count; index++)
        *(ptr++) = dtm->points[index].tol;
        for (index = 0; index < dtm->count; index++)
        *(ptr++) = dtm->points[index].size;
        for (index = 0; index < dtm->count; index++)
        *(ptr++) = dtm->points[index].x;
        bool *bptr = (bool *)ptr;
        for (index = 0; index < dtm->count; index++)
        *(bptr++) = dtm->points[index].bad;
        uint32_t len = compressBound(size);
        char *buffer = malloc(len);
        if (compress((Bytef *)buffer, (uLongf *)&len, data, size) == Z_OK)
        {
            free(data);
            char *compressed = malloc(len + sizeof(uint32_t));
            *((uint32_t *)(compressed)) = len;
            strcpy(compressed + sizeof(uint32_t), buffer);
            free(buffer);
        }
        free(buffer);
        free(data);
        return "";
    }

    Я так и не раскурил, почему так, а не иначе.

    Cynicrus, 01 Декабря 2014

    Комментарии (13)
  6. Си / Говнокод #17179

    +133

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    if (*wpPat == L'\\')
        {
          wpCharStart=wpPat;
          if (++wpPat >= wpMaxPat) goto Error;
    
          if (*wpPat == L'x')
          {
            if (++wpPat >= wpMaxPat) goto Error;
    
            if (*wpPat == L'{')
            {
              wpStrTmp=++wpPat;
              for (;;)
              {
                if (wpPat >= wpMaxPat) goto Error;
                if (*wpPat++ == L'}') break;
              }
              if (lpREGroupItem->nGroupLen != -1 && !bClassOpen)
              {
                nPatChar=(int)hex2decW(wpStrTmp, (wpPat - 1) - wpStrTmp);
                if (nPatChar == -1)
                {
                  wpPat=wpStrTmp;
                  goto Error;
                }
                if (nPatChar <= MAXWORD)
                  lpREGroupItem->nGroupLen+=1;
                else
                  lpREGroupItem->nGroupLen+=2;
              }
            }
            else
            {
              if (wpPat + 2 > wpMaxPat)
                goto Error;
              wpPat+=2;
              if (lpREGroupItem->nGroupLen != -1 && !bClassOpen)
                ++lpREGroupItem->nGroupLen;
            }
          }
          else if (*wpPat == L'u')
          {
            if (wpPat + 5 > wpMaxPat)
              goto Error;
            wpPat+=5;
            if (lpREGroupItem->nGroupLen != -1 && !bClassOpen)
              ++lpREGroupItem->nGroupLen;
          }

    Регулярные велосипеды.
    Akelpad.

    gost, 25 Ноября 2014

    Комментарии (1)
  7. Си / Говнокод #17178

    +133

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    if (wParam == MI_CMDLINEBEGIN)
              return xstrcpyW((void *)lParam, wpCmdLineBegin);
            if (wParam == MI_CMDLINEEND)
              return xstrcpyW((void *)lParam, wpCmdLineEnd);
            if (wParam == MI_SHOWMODIFY)
              return moCur.dwShowModify;
            if (wParam == MI_STATUSPOSTYPE)
              return moCur.dwStatusPosType;
            if (wParam == MI_STATUSUSERFORMAT)
              return xstrcpynW((void *)lParam, moCur.wszStatusUserFormat, MAX_PATH);
            if (wParam == MI_WORDBREAKCUSTOM)
              return moCur.dwWordBreakCustom;
            if (wParam == MI_PAINTOPTIONS)
              return moCur.dwPaintOptions;
            if (wParam == MI_EDITSTYLE)
              return moCur.dwEditStyle;
            if (wParam == MI_RICHEDITCLASS)
              return moCur.bRichEditClass;
            if (wParam == MI_AKELADMINRESIDENT)
              return moCur.bAkelAdminResident;
            if (wParam == MI_DATELOGFORMAT)
              return xstrcpynW((void *)lParam, moCur.wszDateLogFormat, 128);
            if (wParam == MI_DATEINSERTFORMAT)
              return xstrcpynW((void *)lParam, moCur.wszDateInsertFormat, 128);
            if (wParam == MI_AKELUPDATEROPTIONS)
              return xstrcpynW((void *)lParam, moCur.wszAkelUpdaterOptions, MAX_PATH);
            if (wParam == MI_URLCOMMAND)
              return xstrcpynW((void *)lParam, moCur.wszUrlCommand, MAX_PATH);
            if (wParam == MI_TABNAMEFIND)
              return xstrcpynW((void *)lParam, moCur.wszTabNameFind, MAX_PATH);
            if (wParam == MI_TABNAMEREP)
              return xstrcpynW((void *)lParam, moCur.wszTabNameRep, MAX_PATH);
            if (wParam == MI_ONTOP)
              return moCur.bOnTop;
            if (wParam == MI_STATUSBAR)
              return moCur.bStatusBar;
            if (wParam == MI_KEEPSPACE)
              return moCur.bKeepSpace;
            if (wParam == MI_WATCHFILE)
              return moCur.bWatchFile;
            if (wParam == MI_SAVETIME)
              return moCur.bSaveTime;
            if (wParam == MI_SINGLEOPENFILE)
              return moCur.bSingleOpenFile;
            if (wParam == MI_SINGLEOPENPROGRAM)
              return moCur.dwSingleOpenProgram;
            if (wParam == MI_TABOPTIONSMDI)
              return moCur.dwTabOptionsMDI;
            if (wParam == MI_EXECUTECOMMAND)

    И такого больше сотни строк.
    Akelpad.

    gost, 25 Ноября 2014

    Комментарии (14)
  8. Си / Говнокод #17177

    +132

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    LRESULT CALLBACK MainProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
      if (uMsg == WM_CREATE)
      {
        int *lpSBParts;
        int iBorders[3];
        CLIENTCREATESTRUCT ccs;
        DWORD dwClassStyle;
        RECT rcRect;
        HMENU hMenu;
    
        hMainWnd=hWnd;
        hPopupEdit=GetSubMenu(hPopupMenu, MENU_POPUP_EDIT);
        hPopupView=GetSubMenu(hPopupMenu, MENU_POPUP_VIEW);
        hPopupCodepage=GetSubMenu(hPopupMenu, MENU_POPUP_CODEPAGE);
        hPopupOpenCodepage=GetSubMenu(hPopupCodepage, MENU_POPUP_CODEPAGE_OPEN);
        hPopupSaveCodepage=GetSubMenu(hPopupCodepage, MENU_POPUP_CODEPAGE_SAVE);
        hPopupHeadline=GetSubMenu(hPopupMenu, MENU_POPUP_HEADLINE);
    
        hMenu=GetSubMenu(hMainMenu, MENU_FILE_POSITION);
        hMenuRecentFiles=GetSubMenu(hMenu, MENU_FILE_RECENTFILES_4X);
        hMenu=GetSubMenu(hMainMenu, MENU_VIEW_POSITION);
        hMenuLanguage=GetSubMenu(hMenu, MENU_VIEW_LANGUAGE_4X);

    Плотная простынка, спать можно.
    Akelpad.

    gost, 25 Ноября 2014

    Комментарии (0)
  9. Си / Говнокод #17173

    +132

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    static inline uint64_t eight_bytes_to_uint64(uint8_t *buf) {
        return (uint64_t) four_bytes_to_uint32(buf) << 32 | four_bytes_to_uint32(buf + sizeof(uint32_t));
    }
    
    static inline uint32_t four_bytes_to_uint32(uint8_t *buf) {
        return (uint32_t) two_bytes_to_uint16(buf) << 16 | two_bytes_to_uint16(buf + sizeof(uint16_t));
    }
    
    static inline uint16_t two_bytes_to_uint16(uint8_t *buf) {
        return buf[0] << 8 | buf[1];
    }

    Чё-то вспомнились Schemeовые времена и ФП.

    Buffer overflow исключён на 146%.

    codemonkey, 25 Ноября 2014

    Комментарии (0)
  10. Си / Говнокод #17172

    +134

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    //// AEM_* AkelEdit control messages
    
      if (uMsg >= WM_USER)
      {
        if (uMsg >= AEM_CANUNDO)
          goto CanUndo;
    
        //Text retrieval and modification
        if (uMsg == AEM_EXSETTEXTA)
        {
          AESETTEXTA *at=(AESETTEXTA *)lParam;
    
          return AE_SetTextAnsi(ae, at->nCodePage, at->pText, at->dwTextLen, at->nNewLine);
        }
        if (uMsg == AEM_EXSETTEXTW)
        {
          AESETTEXTW *at=(AESETTEXTW *)lParam;
    
          return AE_SetText(ae, at->pText, at->dwTextLen, at->nNewLine, FALSE);
        }
        if (uMsg == AEM_SETTEXTA)
        {
          return AE_SetTextAnsi(ae, CP_ACP, (char *)lParam, wParam, AELB_ASINPUT);
        }
        if (uMsg == AEM_SETTEXTW)
        {
          return AE_SetText(ae, (wchar_t *)lParam, wParam, AELB_ASINPUT, FALSE);
        }
        if (uMsg == AEM_APPENDTEXTA)
        {
          AEAPPENDTEXTA *at=(AEAPPENDTEXTA *)lParam;
    
          AE_AppendTextAnsi(ae, at->nCodePage, at->pText, at->dwTextLen, at->nNewLine);
          return 0;
        }
        if (uMsg == AEM_APPENDTEXTW)
        {
          AEAPPENDTEXTW *at=(AEAPPENDTEXTW *)lParam;
    
          AE_AppendText(ae, at->pText, at->dwTextLen, at->nNewLine);
          return 0;
        }
        if (uMsg == AEM_REPLACESELA)
        {
          AEREPLACESELA *rs=(AEREPLACESELA *)lParam;
    
          AE_ReplaceSelAnsi(ae, rs->nCodePage, rs->pText, rs->dwTextLen, rs->nNewLine, rs->dwFlags, rs->ciInsertStart, rs->ciInsertEnd);
          return 0;
        }
        if (uMsg == AEM_REPLACESELW)
        {
          AEREPLACESELW *rs=(AEREPLACESELW *)lParam;
    
          AE_ReplaceSel(ae, rs->pText, rs->dwTextLen, rs->nNewLine, rs->dwFlags, rs->ciInsertStart, rs->ciInsertEnd);
          return 0;
        }
        if (uMsg == AEM_GETTEXTRANGEA)
        {
          AETEXTRANGEA *tr=(AETEXTRANGEA *)lParam;
    
          return AE_GetTextRangeAnsi(ae, tr->nCodePage, tr->lpDefaultChar, tr->lpUsedDefChar, &tr->cr.ciMin, &tr->cr.ciMax, tr->pBuffer, tr->dwBufferMax, tr->nNewLine, tr->bColumnSel, tr->bFillSpaces);
        }

    И далее около сотни if'ов в таком духе.
    AkelPad

    gost, 25 Ноября 2014

    Комментарии (1)